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