plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 35 | * check the return value |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | * if return value is -1 |
| 37 | * issue a FAIL message, break remaining tests and cleanup |
| 38 | * if we are doing functional testing |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 39 | * if the process was terminated with the expected signal and the |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | * 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
| 64 | #include <signal.h> |
| 65 | #include <errno.h> |
| 66 | #include <sys/ipc.h> |
| 67 | #include <sys/shm.h> |
| 68 | #include <sys/wait.h> |
| 69 | |
| 70 | void cleanup(void); |
| 71 | void setup(void); |
| 72 | void sighandler(int sig); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 73 | void do_child(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | char *TCID = "kill07"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | int TST_TOTAL = 1; |
| 77 | int shmid1; |
robbiew | 6eaecb2 | 2005-12-22 20:18:22 +0000 | [diff] [blame] | 78 | extern key_t semkey; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | int *flag; |
| 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | extern int getipckey(); |
| 82 | extern void rm_shm(int); |
| 83 | |
| 84 | #define TEST_SIG SIGKILL |
| 85 | |
robbiew | b8360ee | 2003-03-26 22:04:58 +0000 | [diff] [blame] | 86 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 88 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 89 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | pid_t pid; |
| 91 | int exno, status, nsig, asig, ret; |
| 92 | struct sigaction my_act, old_act; |
| 93 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 94 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 95 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 97 | #ifdef UCLINUX |
| 98 | maybe_run_child(&do_child, ""); |
| 99 | #endif |
| 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
| 103 | /* The following loop checks looping state if -i option given */ |
| 104 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 105 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | /* reset tst_count in case we are looping */ |
| 107 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | if ((shmid1 = shmget(semkey, (int)getpagesize(), |
| 115 | 0666 | IPC_CREAT)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | "Failed to setup shared memory"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 120 | if (*(flag = shmat(shmid1, 0, 0)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | "Failed to attatch shared memory:%d", *flag); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | *flag = 0; |
| 126 | |
| 127 | /* setup the signal handler */ |
| 128 | ret = sigaction(TEST_SIG, &my_act, &old_act); |
| 129 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 130 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | if (pid < 0) { |
| 132 | tst_brkm(TBROK, cleanup, "Fork of child failed"); |
| 133 | } else if (pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 134 | #ifdef UCLINUX |
| 135 | if (self_exec(av[0], "") < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | tst_brkm(TBROK, cleanup, |
| 137 | "self_exec of child failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 138 | } |
| 139 | #else |
| 140 | do_child(); |
| 141 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | TCID, TEST_ERRNO, strerror(TEST_ERRNO)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 155 | /* |
| 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"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 176 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | if (shmdt(flag)) { |
| 178 | tst_brkm(TBROK, cleanup, "shmdt failed "); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 179 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 182 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 183 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
| 187 | * sighandler() - try to catch SIGKILL |
| 188 | */ |
| 189 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | void sighandler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | { |
| 192 | /* do nothing */ |
| 193 | *flag = 1; |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 198 | * do_child() |
| 199 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 200 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 201 | { |
| 202 | int exno = 1; |
| 203 | |
| 204 | sleep(300); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 205 | tst_resm(TINFO, "Child never recieved a signal"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 206 | exit(exno); |
| 207 | } |
| 208 | |
| 209 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | * setup() - performs all ONE TIME setup for this test |
| 211 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | { |
| 214 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | TEST_PAUSE; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 216 | |
subrata_modak | 56e75bd | 2009-09-18 17:20:39 +0000 | [diff] [blame] | 217 | /* |
| 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | /* 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 233 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 236 | /* |
| 237 | * remove the shared memory |
| 238 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | rm_shm(shmid1); |
| 240 | |
subrata_modak | 56e75bd | 2009-09-18 17:20:39 +0000 | [diff] [blame] | 241 | tst_rmdir(); |
| 242 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 243 | } |