etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_kill.c
blob2f4286246ad29c5bf58f8c5a6829b181b15977c0
1 /* $NetBSD: t_kill.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 $");
34 #include <sys/wait.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <pwd.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <unistd.h>
43 #include <atf-c.h>
45 ATF_TC(kill_basic);
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 };
54 pid_t pid;
55 size_t i;
56 int sta;
58 for (i = 0; i < __arraycount(sig); i++) {
60 pid = fork();
61 ATF_REQUIRE(pid >= 0);
63 switch (pid) {
65 case 0:
66 pause();
67 break;
69 default:
70 ATF_REQUIRE(kill(pid, sig[i]) == 0);
73 (void)wait(&sta);
75 if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != sig[i])
76 atf_tc_fail("kill(2) failed to kill child");
80 ATF_TC(kill_err);
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)
88 int rv, sta;
89 pid_t pid;
91 pid = fork();
92 ATF_REQUIRE(pid >= 0);
94 if (pid == 0) {
96 errno = 0;
97 rv = kill(getpid(), -1);
99 if (rv == 0 || errno != EINVAL)
100 _exit(EINVAL);
102 errno = 0;
103 rv = kill(INT_MAX, SIGUSR1);
105 if (rv == 0 || errno != ESRCH)
106 _exit(ESRCH);
108 _exit(EXIT_SUCCESS);
111 (void)wait(&sta);
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)");
125 ATF_TC(kill_perm);
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)
134 struct passwd *pw;
135 pid_t cpid, ppid;
136 uid_t cuid = 0;
137 uid_t puid = 0;
138 int sta;
141 * Test that kill(2) fails when called
142 * for a PID owned by another user.
144 pw = getpwnam("operator");
146 if (pw != NULL)
147 cuid = pw->pw_uid;
149 pw = getpwnam("nobody");
151 if (pw != NULL)
152 puid = pw->pw_uid;
154 if (cuid == 0 || puid == 0 || cuid == puid)
155 atf_tc_fail("getpwnam(3) failed");
157 ppid = fork();
159 if (ppid < 0)
160 _exit(EXIT_FAILURE);
162 if (ppid == 0) {
164 cpid = fork();
166 if (cpid < 0)
167 _exit(EXIT_FAILURE);
169 if (cpid == 0) {
171 if (setuid(cuid) < 0)
172 _exit(EXIT_FAILURE);
173 else {
174 (void)sleep(1);
177 _exit(EXIT_SUCCESS);
181 * Try to kill the child after having
182 * set the real and effective UID.
184 if (setuid(puid) != 0)
185 _exit(EXIT_FAILURE);
187 errno = 0;
189 if (kill(cpid, SIGKILL) == 0)
190 _exit(EPERM);
192 if (errno != EPERM)
193 _exit(EPERM);
195 (void)waitpid(cpid, &sta, 0);
197 _exit(EXIT_SUCCESS);
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;
218 pid_t cpid, ppid;
219 int i, sta;
221 ppid = fork();
222 ATF_REQUIRE(ppid >= 0);
224 if (ppid == 0) {
226 ATF_REQUIRE(setpgid(0, 0) == 0);
228 for (i = 0; i < maxiter; i++) {
230 cpid = fork();
231 ATF_REQUIRE(cpid >= 0);
233 if (cpid == 0)
234 pause();
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);
245 (void)sleep(1);
247 _exit(EXIT_SUCCESS);
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;
265 pid_t cpid, ppid;
266 int i, sta;
268 ppid = fork();
269 ATF_REQUIRE(ppid >= 0);
271 if (ppid == 0) {
273 ATF_REQUIRE(setpgid(0, 0) == 0);
275 for (i = 0; i < maxiter; i++) {
277 cpid = fork();
278 ATF_REQUIRE(cpid >= 0);
280 if (cpid == 0)
281 pause();
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);
291 (void)sleep(1);
293 _exit(EXIT_SUCCESS);
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");
302 ATF_TP_ADD_TCS(tp)
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();