xref: /minix3/tests/lib/libc/sys/t_kill.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_kill.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc  * by Jukka Ruohonen.
9*11be35a1SLionel Sambuc  *
10*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc  * are met:
13*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc  *
19*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc  */
31*11be35a1SLionel Sambuc #include <sys/cdefs.h>
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_kill.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <sys/wait.h>
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #include <errno.h>
37*11be35a1SLionel Sambuc #include <limits.h>
38*11be35a1SLionel Sambuc #include <pwd.h>
39*11be35a1SLionel Sambuc #include <signal.h>
40*11be35a1SLionel Sambuc #include <stdlib.h>
41*11be35a1SLionel Sambuc #include <unistd.h>
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc #include <atf-c.h>
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc ATF_TC(kill_basic);
ATF_TC_HEAD(kill_basic,tc)46*11be35a1SLionel Sambuc ATF_TC_HEAD(kill_basic, tc)
47*11be35a1SLionel Sambuc {
48*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that kill(2) works");
49*11be35a1SLionel Sambuc }
50*11be35a1SLionel Sambuc 
ATF_TC_BODY(kill_basic,tc)51*11be35a1SLionel Sambuc ATF_TC_BODY(kill_basic, tc)
52*11be35a1SLionel Sambuc {
53*11be35a1SLionel Sambuc 	const int sig[] = { SIGHUP, SIGINT, SIGKILL, SIGTERM };
54*11be35a1SLionel Sambuc 	pid_t pid;
55*11be35a1SLionel Sambuc 	size_t i;
56*11be35a1SLionel Sambuc 	int sta;
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(sig); i++) {
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc 		pid = fork();
61*11be35a1SLionel Sambuc 		ATF_REQUIRE(pid >= 0);
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc 		switch (pid) {
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc 		case 0:
66*11be35a1SLionel Sambuc 			pause();
67*11be35a1SLionel Sambuc 			break;
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc 		default:
70*11be35a1SLionel Sambuc 			ATF_REQUIRE(kill(pid, sig[i]) == 0);
71*11be35a1SLionel Sambuc 		}
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 		(void)wait(&sta);
74*11be35a1SLionel Sambuc 
75*11be35a1SLionel Sambuc 		if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != sig[i])
76*11be35a1SLionel Sambuc 			atf_tc_fail("kill(2) failed to kill child");
77*11be35a1SLionel Sambuc 	}
78*11be35a1SLionel Sambuc }
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc ATF_TC(kill_err);
ATF_TC_HEAD(kill_err,tc)81*11be35a1SLionel Sambuc ATF_TC_HEAD(kill_err, tc)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test error conditions of kill(2)");
84*11be35a1SLionel Sambuc }
85*11be35a1SLionel Sambuc 
ATF_TC_BODY(kill_err,tc)86*11be35a1SLionel Sambuc ATF_TC_BODY(kill_err, tc)
87*11be35a1SLionel Sambuc {
88*11be35a1SLionel Sambuc 	int rv, sta;
89*11be35a1SLionel Sambuc 	pid_t pid;
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc 	pid = fork();
92*11be35a1SLionel Sambuc 	ATF_REQUIRE(pid >= 0);
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc 	if (pid == 0) {
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc 		errno = 0;
97*11be35a1SLionel Sambuc 		rv = kill(getpid(), -1);
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 		if (rv == 0 || errno != EINVAL)
100*11be35a1SLionel Sambuc 			_exit(EINVAL);
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc 		errno = 0;
103*11be35a1SLionel Sambuc 		rv = kill(INT_MAX, SIGUSR1);
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc 		if (rv == 0 || errno != ESRCH)
106*11be35a1SLionel Sambuc 			_exit(ESRCH);
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc 		_exit(EXIT_SUCCESS);
109*11be35a1SLionel Sambuc 	}
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc 	(void)wait(&sta);
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc 		if (WEXITSTATUS(sta) == EINVAL)
116*11be35a1SLionel Sambuc 			atf_tc_fail("expected EINVAL, but kill(2) succeeded");
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc 		if (WEXITSTATUS(sta) == ESRCH)
119*11be35a1SLionel Sambuc 			atf_tc_fail("expected ESRCH, but kill(2) succeeded");
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc 		atf_tc_fail("unknown error from kill(2)");
122*11be35a1SLionel Sambuc 	}
123*11be35a1SLionel Sambuc }
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc ATF_TC(kill_perm);
ATF_TC_HEAD(kill_perm,tc)126*11be35a1SLionel Sambuc ATF_TC_HEAD(kill_perm, tc)
127*11be35a1SLionel Sambuc {
128*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test kill(2) permissions");
129*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc 
ATF_TC_BODY(kill_perm,tc)132*11be35a1SLionel Sambuc ATF_TC_BODY(kill_perm, tc)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc 	struct passwd *pw;
135*11be35a1SLionel Sambuc 	pid_t cpid, ppid;
136*11be35a1SLionel Sambuc 	uid_t cuid = 0;
137*11be35a1SLionel Sambuc 	uid_t puid = 0;
138*11be35a1SLionel Sambuc 	int sta;
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc 	/*
141*11be35a1SLionel Sambuc 	 * Test that kill(2) fails when called
142*11be35a1SLionel Sambuc 	 * for a PID owned by another user.
143*11be35a1SLionel Sambuc 	 */
144*11be35a1SLionel Sambuc 	pw = getpwnam("operator");
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc 	if (pw != NULL)
147*11be35a1SLionel Sambuc 		cuid = pw->pw_uid;
148*11be35a1SLionel Sambuc 
149*11be35a1SLionel Sambuc 	pw = getpwnam("nobody");
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc 	if (pw != NULL)
152*11be35a1SLionel Sambuc 		puid = pw->pw_uid;
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc 	if (cuid == 0 || puid == 0 || cuid == puid)
155*11be35a1SLionel Sambuc 		atf_tc_fail("getpwnam(3) failed");
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc 	ppid = fork();
158*11be35a1SLionel Sambuc 
159*11be35a1SLionel Sambuc 	if (ppid < 0)
160*11be35a1SLionel Sambuc 		_exit(EXIT_FAILURE);
161*11be35a1SLionel Sambuc 
162*11be35a1SLionel Sambuc 	if (ppid == 0) {
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc 		cpid = fork();
165*11be35a1SLionel Sambuc 
166*11be35a1SLionel Sambuc 		if (cpid < 0)
167*11be35a1SLionel Sambuc 			_exit(EXIT_FAILURE);
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc 		if (cpid == 0) {
170*11be35a1SLionel Sambuc 
171*11be35a1SLionel Sambuc 			if (setuid(cuid) < 0)
172*11be35a1SLionel Sambuc 				_exit(EXIT_FAILURE);
173*11be35a1SLionel Sambuc 			else {
174*11be35a1SLionel Sambuc 				(void)sleep(1);
175*11be35a1SLionel Sambuc 			}
176*11be35a1SLionel Sambuc 
177*11be35a1SLionel Sambuc 			_exit(EXIT_SUCCESS);
178*11be35a1SLionel Sambuc 		}
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc 		/*
181*11be35a1SLionel Sambuc 		 * Try to kill the child after having
182*11be35a1SLionel Sambuc 		 * set the real and effective UID.
183*11be35a1SLionel Sambuc 		 */
184*11be35a1SLionel Sambuc 		if (setuid(puid) != 0)
185*11be35a1SLionel Sambuc 			_exit(EXIT_FAILURE);
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc 		errno = 0;
188*11be35a1SLionel Sambuc 
189*11be35a1SLionel Sambuc 		if (kill(cpid, SIGKILL) == 0)
190*11be35a1SLionel Sambuc 			_exit(EPERM);
191*11be35a1SLionel Sambuc 
192*11be35a1SLionel Sambuc 		if (errno != EPERM)
193*11be35a1SLionel Sambuc 			_exit(EPERM);
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc 		(void)waitpid(cpid, &sta, 0);
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc 		_exit(EXIT_SUCCESS);
198*11be35a1SLionel Sambuc 	}
199*11be35a1SLionel Sambuc 
200*11be35a1SLionel Sambuc 	(void)waitpid(ppid, &sta, 0);
201*11be35a1SLionel Sambuc 
202*11be35a1SLionel Sambuc 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) == EPERM)
203*11be35a1SLionel Sambuc 		atf_tc_fail("killed a process of another user");
204*11be35a1SLionel Sambuc 
205*11be35a1SLionel Sambuc 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
206*11be35a1SLionel Sambuc 		atf_tc_fail("unknown error from kill(2)");
207*11be35a1SLionel Sambuc }
208*11be35a1SLionel Sambuc 
209*11be35a1SLionel Sambuc ATF_TC(kill_pgrp_neg);
ATF_TC_HEAD(kill_pgrp_neg,tc)210*11be35a1SLionel Sambuc ATF_TC_HEAD(kill_pgrp_neg, tc)
211*11be35a1SLionel Sambuc {
212*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #2");
213*11be35a1SLionel Sambuc }
214*11be35a1SLionel Sambuc 
ATF_TC_BODY(kill_pgrp_neg,tc)215*11be35a1SLionel Sambuc ATF_TC_BODY(kill_pgrp_neg, tc)
216*11be35a1SLionel Sambuc {
217*11be35a1SLionel Sambuc 	const int maxiter = 3;
218*11be35a1SLionel Sambuc 	pid_t cpid, ppid;
219*11be35a1SLionel Sambuc 	int i, sta;
220*11be35a1SLionel Sambuc 
221*11be35a1SLionel Sambuc 	ppid = fork();
222*11be35a1SLionel Sambuc 	ATF_REQUIRE(ppid >= 0);
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc 	if (ppid == 0) {
225*11be35a1SLionel Sambuc 
226*11be35a1SLionel Sambuc 		ATF_REQUIRE(setpgid(0, 0) == 0);
227*11be35a1SLionel Sambuc 
228*11be35a1SLionel Sambuc 		for (i = 0; i < maxiter; i++) {
229*11be35a1SLionel Sambuc 
230*11be35a1SLionel Sambuc 			cpid = fork();
231*11be35a1SLionel Sambuc 			ATF_REQUIRE(cpid >= 0);
232*11be35a1SLionel Sambuc 
233*11be35a1SLionel Sambuc 			if (cpid == 0)
234*11be35a1SLionel Sambuc 				pause();
235*11be35a1SLionel Sambuc 		}
236*11be35a1SLionel Sambuc 
237*11be35a1SLionel Sambuc 		/*
238*11be35a1SLionel Sambuc 		 * Test the variant of killpg(3); if the process number
239*11be35a1SLionel Sambuc 		 * is negative but not -1, the signal should be sent to
240*11be35a1SLionel Sambuc 		 * all processes whose process group ID is equal to the
241*11be35a1SLionel Sambuc 		 * absolute value of the process number.
242*11be35a1SLionel Sambuc 		 */
243*11be35a1SLionel Sambuc 		ATF_REQUIRE(kill(-getpgrp(), SIGKILL) == 0);
244*11be35a1SLionel Sambuc 
245*11be35a1SLionel Sambuc 		(void)sleep(1);
246*11be35a1SLionel Sambuc 
247*11be35a1SLionel Sambuc 		_exit(EXIT_SUCCESS);
248*11be35a1SLionel Sambuc 	}
249*11be35a1SLionel Sambuc 
250*11be35a1SLionel Sambuc 	(void)waitpid(ppid, &sta, 0);
251*11be35a1SLionel Sambuc 
252*11be35a1SLionel Sambuc 	if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
253*11be35a1SLionel Sambuc 		atf_tc_fail("failed to kill(2) a process group");
254*11be35a1SLionel Sambuc }
255*11be35a1SLionel Sambuc 
256*11be35a1SLionel Sambuc ATF_TC(kill_pgrp_zero);
ATF_TC_HEAD(kill_pgrp_zero,tc)257*11be35a1SLionel Sambuc ATF_TC_HEAD(kill_pgrp_zero, tc)
258*11be35a1SLionel Sambuc {
259*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test kill(2) with process group, #1");
260*11be35a1SLionel Sambuc }
261*11be35a1SLionel Sambuc 
ATF_TC_BODY(kill_pgrp_zero,tc)262*11be35a1SLionel Sambuc ATF_TC_BODY(kill_pgrp_zero, tc)
263*11be35a1SLionel Sambuc {
264*11be35a1SLionel Sambuc 	const int maxiter = 3;
265*11be35a1SLionel Sambuc 	pid_t cpid, ppid;
266*11be35a1SLionel Sambuc 	int i, sta;
267*11be35a1SLionel Sambuc 
268*11be35a1SLionel Sambuc 	ppid = fork();
269*11be35a1SLionel Sambuc 	ATF_REQUIRE(ppid >= 0);
270*11be35a1SLionel Sambuc 
271*11be35a1SLionel Sambuc 	if (ppid == 0) {
272*11be35a1SLionel Sambuc 
273*11be35a1SLionel Sambuc 		ATF_REQUIRE(setpgid(0, 0) == 0);
274*11be35a1SLionel Sambuc 
275*11be35a1SLionel Sambuc 		for (i = 0; i < maxiter; i++) {
276*11be35a1SLionel Sambuc 
277*11be35a1SLionel Sambuc 			cpid = fork();
278*11be35a1SLionel Sambuc 			ATF_REQUIRE(cpid >= 0);
279*11be35a1SLionel Sambuc 
280*11be35a1SLionel Sambuc 			if (cpid == 0)
281*11be35a1SLionel Sambuc 				pause();
282*11be35a1SLionel Sambuc 		}
283*11be35a1SLionel Sambuc 
284*11be35a1SLionel Sambuc 		/*
285*11be35a1SLionel Sambuc 		 * If the supplied process number is zero,
286*11be35a1SLionel Sambuc 		 * the signal should be sent to all processes
287*11be35a1SLionel Sambuc 		 * under the current process group.
288*11be35a1SLionel Sambuc 		 */
289*11be35a1SLionel Sambuc 		ATF_REQUIRE(kill(0, SIGKILL) == 0);
290*11be35a1SLionel Sambuc 
291*11be35a1SLionel Sambuc 		(void)sleep(1);
292*11be35a1SLionel Sambuc 
293*11be35a1SLionel Sambuc 		_exit(EXIT_SUCCESS);
294*11be35a1SLionel Sambuc 	}
295*11be35a1SLionel Sambuc 
296*11be35a1SLionel Sambuc 	(void)waitpid(ppid, &sta, 0);
297*11be35a1SLionel Sambuc 
298*11be35a1SLionel Sambuc 	if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
299*11be35a1SLionel Sambuc 		atf_tc_fail("failed to kill(2) a process group");
300*11be35a1SLionel Sambuc }
301*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)302*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
303*11be35a1SLionel Sambuc {
304*11be35a1SLionel Sambuc 
305*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kill_basic);
306*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kill_err);
307*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kill_perm);
308*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kill_pgrp_neg);
309*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kill_pgrp_zero);
310*11be35a1SLionel Sambuc 
311*11be35a1SLionel Sambuc 	return atf_no_error();
312*11be35a1SLionel Sambuc }
313