xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/kill/kill08.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * NAME
22*49cdfc7eSAndroid Build Coastguard Worker  *	kill08.c
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker  *	Test case to check the basic functionality of kill() when kill an
26*49cdfc7eSAndroid Build Coastguard Worker  *	entire process group.
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
29*49cdfc7eSAndroid Build Coastguard Worker  *	call setup
30*49cdfc7eSAndroid Build Coastguard Worker  *	loop if the -i option was given
31*49cdfc7eSAndroid Build Coastguard Worker  *	fork 5 childeren
32*49cdfc7eSAndroid Build Coastguard Worker  *	execute the kill system call
33*49cdfc7eSAndroid Build Coastguard Worker  *	check the return value
34*49cdfc7eSAndroid Build Coastguard Worker  *	if return value is -1
35*49cdfc7eSAndroid Build Coastguard Worker  *		issue a FAIL message, break remaining tests and cleanup
36*49cdfc7eSAndroid Build Coastguard Worker  *	if we are doing functional testing
37*49cdfc7eSAndroid Build Coastguard Worker  *		if the processes were terminated with the expected signal.
38*49cdfc7eSAndroid Build Coastguard Worker  *			issue a PASS message
39*49cdfc7eSAndroid Build Coastguard Worker  *		otherwise
40*49cdfc7eSAndroid Build Coastguard Worker  *			issue a FAIL message
41*49cdfc7eSAndroid Build Coastguard Worker  *	call cleanup
42*49cdfc7eSAndroid Build Coastguard Worker  *
43*49cdfc7eSAndroid Build Coastguard Worker  * USAGE
44*49cdfc7eSAndroid Build Coastguard Worker  *  kill08 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
45*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
46*49cdfc7eSAndroid Build Coastguard Worker  *             -f   : Turn off functionality Testing.
47*49cdfc7eSAndroid Build Coastguard Worker  *             -i n : Execute test n times.
48*49cdfc7eSAndroid Build Coastguard Worker  *             -I x : Execute test for x seconds.
49*49cdfc7eSAndroid Build Coastguard Worker  *             -P x : Pause for x seconds between iterations.
50*49cdfc7eSAndroid Build Coastguard Worker  *             -t   : Turn on syscall timing.
51*49cdfc7eSAndroid Build Coastguard Worker  *
52*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
53*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
54*49cdfc7eSAndroid Build Coastguard Worker  *
55*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS
56*49cdfc7eSAndroid Build Coastguard Worker  *	This test should be run as a non-root user.
57*49cdfc7eSAndroid Build Coastguard Worker  */
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
62*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
63*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
66*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
67*49cdfc7eSAndroid Build Coastguard Worker void do_child(void);
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "kill08";
70*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker #define TEST_SIG SIGKILL
73*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)74*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
77*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid1, pid2;
78*49cdfc7eSAndroid Build Coastguard Worker 	int exno, status, nsig, i;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	setup();		/* global setup */
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	/* The following loop checks looping state if -i option given */
85*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 		/* reset tst_count in case we are looping */
88*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
89*49cdfc7eSAndroid Build Coastguard Worker 		status = 1;
90*49cdfc7eSAndroid Build Coastguard Worker 		exno = 1;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 		/* Fork a process and set the process group so that */
93*49cdfc7eSAndroid Build Coastguard Worker 		/* it is different from this one.  Fork 5 more children. */
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 		pid1 = tst_fork();
96*49cdfc7eSAndroid Build Coastguard Worker 		if (pid1 < 0) {
97*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup, "Fork of first child failed");
98*49cdfc7eSAndroid Build Coastguard Worker 		} else if (pid1 == 0) {
99*49cdfc7eSAndroid Build Coastguard Worker 			setpgrp();
100*49cdfc7eSAndroid Build Coastguard Worker 			for (i = 0; i < 5; i++) {
101*49cdfc7eSAndroid Build Coastguard Worker 				pid2 = tst_fork();
102*49cdfc7eSAndroid Build Coastguard Worker 				if (pid2 < 0) {
103*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TBROK, cleanup, "Fork failed");
104*49cdfc7eSAndroid Build Coastguard Worker 				} else if (pid2 == 0) {
105*49cdfc7eSAndroid Build Coastguard Worker 					do_child();
106*49cdfc7eSAndroid Build Coastguard Worker 				}
107*49cdfc7eSAndroid Build Coastguard Worker 			}
108*49cdfc7eSAndroid Build Coastguard Worker 			/* Kill all processes in this process group */
109*49cdfc7eSAndroid Build Coastguard Worker 			TEST(kill(0, TEST_SIG));
110*49cdfc7eSAndroid Build Coastguard Worker 			pause();
111*49cdfc7eSAndroid Build Coastguard Worker 			exit(exno);
112*49cdfc7eSAndroid Build Coastguard Worker 		} else {
113*49cdfc7eSAndroid Build Coastguard Worker 			waitpid(pid1, &status, 0);
114*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN != 0) {
115*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TFAIL, cleanup, "%s failed - errno = "
116*49cdfc7eSAndroid Build Coastguard Worker 					 "%d : %s", TCID, TEST_ERRNO,
117*49cdfc7eSAndroid Build Coastguard Worker 					 strerror(TEST_ERRNO));
118*49cdfc7eSAndroid Build Coastguard Worker 			}
119*49cdfc7eSAndroid Build Coastguard Worker 		}
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 		/*
122*49cdfc7eSAndroid Build Coastguard Worker 		 * Check to see if the process was terminated with the
123*49cdfc7eSAndroid Build Coastguard Worker 		 * expected signal.
124*49cdfc7eSAndroid Build Coastguard Worker 		 */
125*49cdfc7eSAndroid Build Coastguard Worker 		nsig = WTERMSIG(status);
126*49cdfc7eSAndroid Build Coastguard Worker 		if (nsig == TEST_SIG) {
127*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "received expected signal %d",
128*49cdfc7eSAndroid Build Coastguard Worker 				 nsig);
129*49cdfc7eSAndroid Build Coastguard Worker 		} else {
130*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
131*49cdfc7eSAndroid Build Coastguard Worker 				 "expected signal %d received %d",
132*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_SIG, nsig);
133*49cdfc7eSAndroid Build Coastguard Worker 		}
134*49cdfc7eSAndroid Build Coastguard Worker 	}
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
137*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker /*
141*49cdfc7eSAndroid Build Coastguard Worker  * do_child()
142*49cdfc7eSAndroid Build Coastguard Worker  */
do_child(void)143*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
144*49cdfc7eSAndroid Build Coastguard Worker {
145*49cdfc7eSAndroid Build Coastguard Worker 	int exno = 1;
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	pause();
148*49cdfc7eSAndroid Build Coastguard Worker 	exit(exno);
149*49cdfc7eSAndroid Build Coastguard Worker }
150*49cdfc7eSAndroid Build Coastguard Worker 
151*49cdfc7eSAndroid Build Coastguard Worker /*
152*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test
153*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)154*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
155*49cdfc7eSAndroid Build Coastguard Worker {
156*49cdfc7eSAndroid Build Coastguard Worker 
157*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker /*
161*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all the ONE TIME cleanup for this test at completion
162*49cdfc7eSAndroid Build Coastguard Worker  * or premature exit.
163*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)164*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
165*49cdfc7eSAndroid Build Coastguard Worker {
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker }
168