blob: 17212332d868e5bf23cf29da4536308f13c6e80b [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * kill07.c
23 *
24 * DESCRIPTION
25 * Test case to check that SIGKILL can not be caught.
26 *
27 * ALGORITHM
28 * call setup
29 * setup some shared memory
30 * loop if the -i option was given
31 * set up to catch SIGKILL
32 * if SIGKILL is caught set the shared memory flag.
33 * fork a child
34 * execute the kill system call
subrata_modak4bb656a2009-02-26 12:02:09 +000035 * check the return value
plars865695b2001-08-27 22:15:12 +000036 * if return value is -1
37 * issue a FAIL message, break remaining tests and cleanup
38 * if we are doing functional testing
subrata_modak4bb656a2009-02-26 12:02:09 +000039 * if the process was terminated with the expected signal and the
plars865695b2001-08-27 22:15:12 +000040 * signal was not caught.
41 * issue a PASS message
42 * otherwise
43 * issue a FAIL message
44 * call cleanup
45 *
46 * USAGE
47 * kill07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
48 * where, -c n : Run n copies concurrently.
49 * -f : Turn off functionality Testing.
50 * -i n : Execute test n times.
51 * -I x : Execute test for x seconds.
52 * -P x : Pause for x seconds between iterations.
53 * -t : Turn on syscall timing.
54 *
55 * HISTORY
56 * 07/2001 Ported by Wayne Boyer
57 *
58 * RESTRICTIONS
59 * This test should be run as a non-root user.
60 */
61
62#include "test.h"
plars865695b2001-08-27 22:15:12 +000063
64#include <signal.h>
65#include <errno.h>
66#include <sys/ipc.h>
67#include <sys/shm.h>
68#include <sys/wait.h>
69
70void cleanup(void);
71void setup(void);
72void sighandler(int sig);
robbiewd34d5812005-07-11 22:28:09 +000073void do_child(void);
plars865695b2001-08-27 22:15:12 +000074
subrata_modak56207ce2009-03-23 13:35:39 +000075char *TCID = "kill07";
plars865695b2001-08-27 22:15:12 +000076int TST_TOTAL = 1;
77int shmid1;
robbiew6eaecb22005-12-22 20:18:22 +000078extern key_t semkey;
plars865695b2001-08-27 22:15:12 +000079int *flag;
80
plars865695b2001-08-27 22:15:12 +000081extern int getipckey();
82extern void rm_shm(int);
83
84#define TEST_SIG SIGKILL
85
robbiewb8360ee2003-03-26 22:04:58 +000086int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000087{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020088 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020089 const char *msg;
plars865695b2001-08-27 22:15:12 +000090 pid_t pid;
91 int exno, status, nsig, asig, ret;
92 struct sigaction my_act, old_act;
93
Garrett Cooper45e285d2010-11-22 12:19:25 -080094 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080095 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000096 }
robbiewd34d5812005-07-11 22:28:09 +000097#ifdef UCLINUX
98 maybe_run_child(&do_child, "");
99#endif
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +0000102
103 /* The following loop checks looping state if -i option given */
104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 /* reset tst_count in case we are looping */
107 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108 status = 1;
109 exno = 1;
110 my_act.sa_handler = sighandler;
111 my_act.sa_flags = SA_RESTART;
112 sigemptyset(&my_act.sa_mask);
113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 if ((shmid1 = shmget(semkey, (int)getpagesize(),
115 0666 | IPC_CREAT)) == -1) {
plars865695b2001-08-27 22:15:12 +0000116 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000117 "Failed to setup shared memory");
plars865695b2001-08-27 22:15:12 +0000118 }
119
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200120 if (*(flag = shmat(shmid1, 0, 0)) == -1) {
plars865695b2001-08-27 22:15:12 +0000121 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000122 "Failed to attatch shared memory:%d", *flag);
plars865695b2001-08-27 22:15:12 +0000123 }
124
125 *flag = 0;
126
127 /* setup the signal handler */
128 ret = sigaction(TEST_SIG, &my_act, &old_act);
129
robbiewd34d5812005-07-11 22:28:09 +0000130 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000131 if (pid < 0) {
132 tst_brkm(TBROK, cleanup, "Fork of child failed");
133 } else if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000134#ifdef UCLINUX
135 if (self_exec(av[0], "") < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000136 tst_brkm(TBROK, cleanup,
137 "self_exec of child failed");
robbiewd34d5812005-07-11 22:28:09 +0000138 }
139#else
140 do_child();
141#endif
plars865695b2001-08-27 22:15:12 +0000142 } else {
143 /* sighandler should not catch this signal */
144 /* if it does flag will be set to 1 */
145 sleep(1);
146 TEST(kill(pid, TEST_SIG));
147 waitpid(pid, &status, 0);
148 }
149
150 if (TEST_RETURN == -1) {
151 tst_brkm(TFAIL, cleanup, "%s failed - errno = %d : %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000152 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 }
plars865695b2001-08-27 22:15:12 +0000154
Cyril Hrubise38b9612014-06-02 17:20:57 +0200155 /*
156 * Check to see if the process was terminated with the
157 * expected signal.
158 */
159 nsig = WTERMSIG(status);
160 asig = WIFSIGNALED(status);
161 if ((asig == 0) & (*flag == 1)) {
162 tst_resm(TFAIL, "SIGKILL was unexpectedly"
163 " caught");
164 } else if ((asig == 1) & (nsig == TEST_SIG)) {
165 tst_resm(TINFO, "received expected signal %d",
166 nsig);
167 tst_resm(TPASS,
168 "Did not catch signal as expected");
169 } else if (nsig) {
170 tst_resm(TFAIL,
171 "expected signal %d received %d",
172 TEST_SIG, nsig);
173 } else {
174 tst_resm(TFAIL, "No signals received");
plars865695b2001-08-27 22:15:12 +0000175 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200176
plars865695b2001-08-27 22:15:12 +0000177 if (shmdt(flag)) {
178 tst_brkm(TBROK, cleanup, "shmdt failed ");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 }
plars865695b2001-08-27 22:15:12 +0000180 }
plars865695b2001-08-27 22:15:12 +0000181
Cyril Hrubise38b9612014-06-02 17:20:57 +0200182 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800183 tst_exit();
plars865695b2001-08-27 22:15:12 +0000184}
185
186/*
187 * sighandler() - try to catch SIGKILL
188 */
189
subrata_modak56207ce2009-03-23 13:35:39 +0000190void sighandler(int sig)
plars865695b2001-08-27 22:15:12 +0000191{
192 /* do nothing */
193 *flag = 1;
194 return;
195}
196
197/*
robbiewd34d5812005-07-11 22:28:09 +0000198 * do_child()
199 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400200void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000201{
202 int exno = 1;
203
204 sleep(300);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800205 tst_resm(TINFO, "Child never recieved a signal");
robbiewd34d5812005-07-11 22:28:09 +0000206 exit(exno);
207}
208
209/*
plars865695b2001-08-27 22:15:12 +0000210 * setup() - performs all ONE TIME setup for this test
211 */
subrata_modak56207ce2009-03-23 13:35:39 +0000212void setup(void)
plars865695b2001-08-27 22:15:12 +0000213{
214
plars865695b2001-08-27 22:15:12 +0000215 TEST_PAUSE;
subrata_modakbdbaec52009-02-26 12:14:51 +0000216
subrata_modak56e75bd2009-09-18 17:20:39 +0000217 /*
218 * Create a temporary directory and cd into it.
219 * This helps to ensure that a unique msgkey is created.
220 * See ../lib/libipc.c for more information.
221 */
222 tst_tmpdir();
223
plars865695b2001-08-27 22:15:12 +0000224 /* get an IPC resource key */
225 semkey = getipckey();
226
227}
228
229/*
230 * cleanup() - performs all the ONE TIME cleanup for this test at completion
231 * or premature exit.
232 */
subrata_modak56207ce2009-03-23 13:35:39 +0000233void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000234{
plars865695b2001-08-27 22:15:12 +0000235
subrata_modak56207ce2009-03-23 13:35:39 +0000236 /*
237 * remove the shared memory
238 */
plars865695b2001-08-27 22:15:12 +0000239 rm_shm(shmid1);
240
subrata_modak56e75bd2009-09-18 17:20:39 +0000241 tst_rmdir();
242
Chris Dearmanec6edca2012-10-17 19:54:01 -0700243}