1 /* $NetBSD: t_kill.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_kill.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
46 ATF_TC_HEAD(kill_basic, tc)
48 atf_tc_set_md_var(tc, "descr", "Test that kill(2) works");
51 ATF_TC_BODY(kill_basic, tc)
53 const int sig[] = { SIGHUP, SIGINT, SIGKILL, SIGTERM };
58 for (i = 0; i < __arraycount(sig); i++) {
61 ATF_REQUIRE(pid >= 0);
70 ATF_REQUIRE(kill(pid, sig[i]) == 0);
75 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != sig[i])
76 atf_tc_fail("kill(2) failed to kill child");
81 ATF_TC_HEAD(kill_err, tc)
83 atf_tc_set_md_var(tc, "descr", "Test error conditions of kill(2)");
86 ATF_TC_BODY(kill_err, tc)
92 ATF_REQUIRE(pid >= 0);
97 rv = kill(getpid(), -1);
99 if (rv == 0 || errno != EINVAL)
103 rv = kill(INT_MAX, SIGUSR1);
105 if (rv == 0 || errno != ESRCH)
113 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
115 if (WEXITSTATUS(sta) == EINVAL)
116 atf_tc_fail("expected EINVAL, but kill(2) succeeded");
118 if (WEXITSTATUS(sta) == ESRCH)
119 atf_tc_fail("expected ESRCH, but kill(2) succeeded");
121 atf_tc_fail("unknown error from kill(2)");
126 ATF_TC_HEAD(kill_perm, tc)
128 atf_tc_set_md_var(tc, "descr", "Test kill(2) permissions");
129 atf_tc_set_md_var(tc, "require.user", "root");
132 ATF_TC_BODY(kill_perm, tc)
141 * Test that kill(2) fails when called
142 * for a PID owned by another user.
144 pw = getpwnam("operator");
149 pw = getpwnam("nobody");
154 if (cuid == 0 || puid == 0 || cuid == puid)
155 atf_tc_fail("getpwnam(3) failed");
171 if (setuid(cuid) < 0)
181 * Try to kill the child after having
182 * set the real and effective UID.
184 if (setuid(puid) != 0)
189 if (kill(cpid, SIGKILL) == 0)
195 (void)waitpid(cpid, &sta, 0);
200 (void)waitpid(ppid, &sta, 0);
202 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) == EPERM)
203 atf_tc_fail("killed a process of another user");
205 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
206 atf_tc_fail("unknown error from kill(2)");
209 ATF_TC(kill_pgrp_neg);
210 ATF_TC_HEAD(kill_pgrp_neg, tc)
212 atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #2");
215 ATF_TC_BODY(kill_pgrp_neg, tc)
217 const int maxiter = 3;
222 ATF_REQUIRE(ppid >= 0);
226 ATF_REQUIRE(setpgid(0, 0) == 0);
228 for (i = 0; i < maxiter; i++) {
231 ATF_REQUIRE(cpid >= 0);
238 * Test the variant of killpg(3); if the process number
239 * is negative but not -1, the signal should be sent to
240 * all processes whose process group ID is equal to the
241 * absolute value of the process number.
243 ATF_REQUIRE(kill(-getpgrp(), SIGKILL) == 0);
250 (void)waitpid(ppid, &sta, 0);
252 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
253 atf_tc_fail("failed to kill(2) a process group");
256 ATF_TC(kill_pgrp_zero);
257 ATF_TC_HEAD(kill_pgrp_zero, tc)
259 atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #1");
262 ATF_TC_BODY(kill_pgrp_zero, tc)
264 const int maxiter = 3;
269 ATF_REQUIRE(ppid >= 0);
273 ATF_REQUIRE(setpgid(0, 0) == 0);
275 for (i = 0; i < maxiter; i++) {
278 ATF_REQUIRE(cpid >= 0);
285 * If the supplied process number is zero,
286 * the signal should be sent to all processes
287 * under the current process group.
289 ATF_REQUIRE(kill(0, SIGKILL) == 0);
296 (void)waitpid(ppid, &sta, 0);
298 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
299 atf_tc_fail("failed to kill(2) a process group");
305 ATF_TP_ADD_TC(tp, kill_basic);
306 ATF_TP_ADD_TC(tp, kill_err);
307 ATF_TP_ADD_TC(tp, kill_perm);
308 ATF_TP_ADD_TC(tp, kill_pgrp_neg);
309 ATF_TP_ADD_TC(tp, kill_pgrp_zero);
311 return atf_no_error();