xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/kill/kill07.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  *	kill07.c
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker  *	Test case to check that SIGKILL can not be caught.
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
28*49cdfc7eSAndroid Build Coastguard Worker  *	call setup
29*49cdfc7eSAndroid Build Coastguard Worker  *		setup some shared memory
30*49cdfc7eSAndroid Build Coastguard Worker  *	loop if the -i option was given
31*49cdfc7eSAndroid Build Coastguard Worker  *	set up to catch SIGKILL
32*49cdfc7eSAndroid Build Coastguard Worker  *	if SIGKILL is caught set the shared memory flag.
33*49cdfc7eSAndroid Build Coastguard Worker  *	fork a child
34*49cdfc7eSAndroid Build Coastguard Worker  *	execute the kill system call
35*49cdfc7eSAndroid Build Coastguard Worker  *	check the return value
36*49cdfc7eSAndroid Build Coastguard Worker  *	if return value is -1
37*49cdfc7eSAndroid Build Coastguard Worker  *		issue a FAIL message, break remaining tests and cleanup
38*49cdfc7eSAndroid Build Coastguard Worker  *	if we are doing functional testing
39*49cdfc7eSAndroid Build Coastguard Worker  *		if the process was terminated with the expected signal and the
40*49cdfc7eSAndroid Build Coastguard Worker  *		signal was not caught.
41*49cdfc7eSAndroid Build Coastguard Worker  *			issue a PASS message
42*49cdfc7eSAndroid Build Coastguard Worker  *		otherwise
43*49cdfc7eSAndroid Build Coastguard Worker  *			issue a FAIL message
44*49cdfc7eSAndroid Build Coastguard Worker  *	call cleanup
45*49cdfc7eSAndroid Build Coastguard Worker  *
46*49cdfc7eSAndroid Build Coastguard Worker  * USAGE
47*49cdfc7eSAndroid Build Coastguard Worker  *  kill07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
48*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
49*49cdfc7eSAndroid Build Coastguard Worker  *             -f   : Turn off functionality Testing.
50*49cdfc7eSAndroid Build Coastguard Worker  *             -i n : Execute test n times.
51*49cdfc7eSAndroid Build Coastguard Worker  *             -I x : Execute test for x seconds.
52*49cdfc7eSAndroid Build Coastguard Worker  *             -P x : Pause for x seconds between iterations.
53*49cdfc7eSAndroid Build Coastguard Worker  *             -t   : Turn on syscall timing.
54*49cdfc7eSAndroid Build Coastguard Worker  *
55*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
56*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
57*49cdfc7eSAndroid Build Coastguard Worker  *
58*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS
59*49cdfc7eSAndroid Build Coastguard Worker  *	This test should be run as a non-root user.
60*49cdfc7eSAndroid Build Coastguard Worker  */
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
65*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
66*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ipc.h>
67*49cdfc7eSAndroid Build Coastguard Worker #include <sys/shm.h>
68*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
71*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
72*49cdfc7eSAndroid Build Coastguard Worker void sighandler(int sig);
73*49cdfc7eSAndroid Build Coastguard Worker void do_child(void);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "kill07";
76*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
77*49cdfc7eSAndroid Build Coastguard Worker int shmid1;
78*49cdfc7eSAndroid Build Coastguard Worker extern key_t semkey;
79*49cdfc7eSAndroid Build Coastguard Worker int *flag;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker extern int getipckey();
82*49cdfc7eSAndroid Build Coastguard Worker extern void rm_shm(int);
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker #define TEST_SIG SIGKILL
85*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)86*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
89*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
90*49cdfc7eSAndroid Build Coastguard Worker 	int exno, status, nsig, asig, ret;
91*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction my_act, old_act;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	setup();		/* global setup */
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	/* The following loop checks looping state if -i option given */
98*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 		/* reset tst_count in case we are looping */
101*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
102*49cdfc7eSAndroid Build Coastguard Worker 		status = 1;
103*49cdfc7eSAndroid Build Coastguard Worker 		exno = 1;
104*49cdfc7eSAndroid Build Coastguard Worker 		my_act.sa_handler = sighandler;
105*49cdfc7eSAndroid Build Coastguard Worker 		my_act.sa_flags = SA_RESTART;
106*49cdfc7eSAndroid Build Coastguard Worker 		sigemptyset(&my_act.sa_mask);
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 		if ((shmid1 = shmget(semkey, (int)getpagesize(),
109*49cdfc7eSAndroid Build Coastguard Worker 				     0666 | IPC_CREAT)) == -1) {
110*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup,
111*49cdfc7eSAndroid Build Coastguard Worker 				 "Failed to setup shared memory");
112*49cdfc7eSAndroid Build Coastguard Worker 		}
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 		if (*(flag = shmat(shmid1, 0, 0)) == -1) {
115*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup,
116*49cdfc7eSAndroid Build Coastguard Worker 				 "Failed to attatch shared memory:%d", *flag);
117*49cdfc7eSAndroid Build Coastguard Worker 		}
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 		*flag = 0;
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 		/* setup the signal handler */
122*49cdfc7eSAndroid Build Coastguard Worker 		ret = sigaction(TEST_SIG, &my_act, &old_act);
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 		pid = tst_fork();
125*49cdfc7eSAndroid Build Coastguard Worker 		if (pid < 0) {
126*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup, "Fork of child failed");
127*49cdfc7eSAndroid Build Coastguard Worker 		} else if (pid == 0) {
128*49cdfc7eSAndroid Build Coastguard Worker 			do_child();
129*49cdfc7eSAndroid Build Coastguard Worker 		} else {
130*49cdfc7eSAndroid Build Coastguard Worker 			/* sighandler should not catch this signal */
131*49cdfc7eSAndroid Build Coastguard Worker 			/* if it does flag will be set to 1 */
132*49cdfc7eSAndroid Build Coastguard Worker 			sleep(1);
133*49cdfc7eSAndroid Build Coastguard Worker 			TEST(kill(pid, TEST_SIG));
134*49cdfc7eSAndroid Build Coastguard Worker 			waitpid(pid, &status, 0);
135*49cdfc7eSAndroid Build Coastguard Worker 		}
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
138*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, cleanup, "%s failed - errno = %d : %s",
139*49cdfc7eSAndroid Build Coastguard Worker 				 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
140*49cdfc7eSAndroid Build Coastguard Worker 		}
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 		/*
143*49cdfc7eSAndroid Build Coastguard Worker 		 * Check to see if the process was terminated with the
144*49cdfc7eSAndroid Build Coastguard Worker 		 * expected signal.
145*49cdfc7eSAndroid Build Coastguard Worker 		 */
146*49cdfc7eSAndroid Build Coastguard Worker 		nsig = WTERMSIG(status);
147*49cdfc7eSAndroid Build Coastguard Worker 		asig = WIFSIGNALED(status);
148*49cdfc7eSAndroid Build Coastguard Worker 		if ((asig == 0) & (*flag == 1)) {
149*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "SIGKILL was unexpectedly"
150*49cdfc7eSAndroid Build Coastguard Worker 				 " caught");
151*49cdfc7eSAndroid Build Coastguard Worker 		} else if ((asig == 1) & (nsig == TEST_SIG)) {
152*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "received expected signal %d",
153*49cdfc7eSAndroid Build Coastguard Worker 				 nsig);
154*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS,
155*49cdfc7eSAndroid Build Coastguard Worker 				 "Did not catch signal as expected");
156*49cdfc7eSAndroid Build Coastguard Worker 		} else if (nsig) {
157*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
158*49cdfc7eSAndroid Build Coastguard Worker 				 "expected signal %d received %d",
159*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_SIG, nsig);
160*49cdfc7eSAndroid Build Coastguard Worker 		} else {
161*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "No signals received");
162*49cdfc7eSAndroid Build Coastguard Worker 		}
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 		if (shmdt(flag)) {
165*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup, "shmdt failed ");
166*49cdfc7eSAndroid Build Coastguard Worker 		}
167*49cdfc7eSAndroid Build Coastguard Worker 	}
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
170*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
171*49cdfc7eSAndroid Build Coastguard Worker }
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker /*
174*49cdfc7eSAndroid Build Coastguard Worker  * sighandler() - try to catch SIGKILL
175*49cdfc7eSAndroid Build Coastguard Worker  */
176*49cdfc7eSAndroid Build Coastguard Worker 
sighandler(int sig)177*49cdfc7eSAndroid Build Coastguard Worker void sighandler(int sig)
178*49cdfc7eSAndroid Build Coastguard Worker {
179*49cdfc7eSAndroid Build Coastguard Worker 	/* do nothing */
180*49cdfc7eSAndroid Build Coastguard Worker 	*flag = 1;
181*49cdfc7eSAndroid Build Coastguard Worker 	return;
182*49cdfc7eSAndroid Build Coastguard Worker }
183*49cdfc7eSAndroid Build Coastguard Worker 
184*49cdfc7eSAndroid Build Coastguard Worker /*
185*49cdfc7eSAndroid Build Coastguard Worker  * do_child()
186*49cdfc7eSAndroid Build Coastguard Worker  */
do_child(void)187*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
188*49cdfc7eSAndroid Build Coastguard Worker {
189*49cdfc7eSAndroid Build Coastguard Worker 	int exno = 1;
190*49cdfc7eSAndroid Build Coastguard Worker 
191*49cdfc7eSAndroid Build Coastguard Worker 	sleep(300);
192*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Child never received a signal");
193*49cdfc7eSAndroid Build Coastguard Worker 	exit(exno);
194*49cdfc7eSAndroid Build Coastguard Worker }
195*49cdfc7eSAndroid Build Coastguard Worker 
196*49cdfc7eSAndroid Build Coastguard Worker /*
197*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test
198*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)199*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
200*49cdfc7eSAndroid Build Coastguard Worker {
201*49cdfc7eSAndroid Build Coastguard Worker 
202*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
203*49cdfc7eSAndroid Build Coastguard Worker 
204*49cdfc7eSAndroid Build Coastguard Worker 	/*
205*49cdfc7eSAndroid Build Coastguard Worker 	 * Create a temporary directory and cd into it.
206*49cdfc7eSAndroid Build Coastguard Worker 	 * This helps to ensure that a unique msgkey is created.
207*49cdfc7eSAndroid Build Coastguard Worker 	 * See libs/libltpipc/libipc.c for more information.
208*49cdfc7eSAndroid Build Coastguard Worker 	 */
209*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
210*49cdfc7eSAndroid Build Coastguard Worker 
211*49cdfc7eSAndroid Build Coastguard Worker 	/* get an IPC resource key */
212*49cdfc7eSAndroid Build Coastguard Worker 	semkey = getipckey();
213*49cdfc7eSAndroid Build Coastguard Worker 
214*49cdfc7eSAndroid Build Coastguard Worker }
215*49cdfc7eSAndroid Build Coastguard Worker 
216*49cdfc7eSAndroid Build Coastguard Worker /*
217*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all the ONE TIME cleanup for this test at completion
218*49cdfc7eSAndroid Build Coastguard Worker  * or premature exit.
219*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)220*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
221*49cdfc7eSAndroid Build Coastguard Worker {
222*49cdfc7eSAndroid Build Coastguard Worker 
223*49cdfc7eSAndroid Build Coastguard Worker 	/*
224*49cdfc7eSAndroid Build Coastguard Worker 	 * remove the shared memory
225*49cdfc7eSAndroid Build Coastguard Worker 	 */
226*49cdfc7eSAndroid Build Coastguard Worker 	rm_shm(shmid1);
227*49cdfc7eSAndroid Build Coastguard Worker 
228*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
229*49cdfc7eSAndroid Build Coastguard Worker 
230*49cdfc7eSAndroid Build Coastguard Worker }
231