robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 1 | /* IBM Corporation */ |
| 2 | /* 01/02/2003 Port to LTP avenkat@us.ibm.com */ |
| 3 | /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ |
| 4 | |
| 5 | /* |
| 6 | * |
| 7 | * Copyright (c) International Business Machines Corp., 2002 |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 24 | /*kill2.c */ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 25 | /*====================================================================== |
| 26 | >KEYS: < kill(), wait(), signal() |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | >WHAT: < Check that when a child is killed by its parent, it returns the |
| 28 | < correct values to the waiting parent--the child sets signal to |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 29 | < ignore the kill |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 30 | >HOW: < For each signal: Send that signal to a child that has elected |
| 31 | < to catch the signal, check that the correct status was returned |
| 32 | < to the waiting parent. |
| 33 | < NOTE: Signal 9 (kill) is not catchable, and must be dealt with |
| 34 | < separately. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 35 | >BUGS: < None known |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 36 | ======================================================================*/ |
| 37 | #ifndef _GNU_SOURCE |
| 38 | #define _GNU_SOURCE 1 |
| 39 | #endif |
| 40 | |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <signal.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <unistd.h> |
| 46 | #include <sys/wait.h> |
| 47 | #include <errno.h> |
| 48 | |
| 49 | //char progname[] = "kill2()"; |
| 50 | /***** LTP Port *****/ |
| 51 | #include "test.h" |
| 52 | #include "usctest.h" |
| 53 | #define ITER 3 |
| 54 | #define FAILED 0 |
| 55 | #define PASSED 1 |
| 56 | |
robbiew | ca03b9d | 2003-05-07 19:04:28 +0000 | [diff] [blame] | 57 | char *TCID = "kill12"; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 58 | |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 59 | int local_flag = PASSED; |
| 60 | int block_number; |
| 61 | FILE *temp; |
| 62 | int TST_TOTAL = 1; |
| 63 | extern int Tst_count; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 64 | static int sig; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 65 | |
| 66 | int anyfail(); |
| 67 | int blenter(); |
| 68 | int instress(); |
| 69 | void setup(); |
| 70 | void terror(); |
| 71 | void fail_exit(); |
| 72 | void ok_exit(); |
| 73 | int forkfail(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 74 | void do_child(); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 75 | |
| 76 | /***** ** ** *****/ |
| 77 | |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 78 | int chflag; |
| 79 | |
| 80 | /*--------------------------------------------------------------------*/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | int main(int argc, char **argv) |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 82 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | /***** BEGINNING OF MAIN. *****/ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 84 | int pid, npid; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int nsig, exno, nexno, status; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 86 | int ret_val = 0; |
| 87 | int core; |
| 88 | void chsig(); |
| 89 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 90 | #ifdef UCLINUX |
| 91 | char *msg; |
| 92 | |
| 93 | /* parse standard options */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | if ((msg = |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 95 | parse_opts(argc, argv, NULL, NULL)) != NULL) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 96 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 97 | } |
| 98 | |
| 99 | maybe_run_child(&do_child, "dd", &temp, &sig); |
| 100 | #endif |
| 101 | |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 102 | setup(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | //tempdir(); /* move to new directory */ 12/20/2003 |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 104 | /*--------------------------------------------------------------------*/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | blenter(); /*<<<<<ENTER DATA HERE<<<<<<<< */ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 106 | |
| 107 | exno = 1; |
| 108 | |
| 109 | if (sigset(SIGCLD, chsig) == SIG_ERR) { |
| 110 | fprintf(temp, "\tsigset failed, errno = %d\n", errno); |
| 111 | fail_exit(); |
| 112 | } |
| 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | for (sig = 1; sig < 14; sig++) { |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 115 | fflush(temp); |
| 116 | chflag = 0; |
| 117 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 118 | pid = FORK_OR_VFORK(); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 119 | if (pid < 0) { |
| 120 | forkfail(); |
| 121 | } |
| 122 | |
| 123 | if (pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 124 | #ifdef UCLINUX |
| 125 | if (self_exec(argv[0], "dd", temp, sig) < 0) { |
| 126 | tst_resm(TBROK, "self_exec FAILED - " |
| 127 | "terminating test."); |
| 128 | tst_exit(); |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 129 | return 0; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 130 | } |
| 131 | #else |
| 132 | do_child(); |
| 133 | #endif |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 134 | } else { |
| 135 | //fprintf(temp, "Testing signal %d\n", sig); |
| 136 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | while (!chflag) /* wait for child */ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 138 | sleep(1); |
| 139 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | kill(pid, sig); /* child should ignroe this sig */ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 141 | kill(pid, SIGCLD); /* child should exit */ |
| 142 | |
| 143 | #ifdef BCS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | while ((npid = wait(&status)) != pid |
| 145 | || (npid == -1 && errno == EINTR)) ; |
| 146 | if (npid != pid) { |
| 147 | fprintf(temp, |
| 148 | "wait error: wait returned wrong pid\n"); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 149 | ret_val = 1; |
| 150 | } |
| 151 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | while ((npid = waitpid(pid, &status, 0)) != -1 |
| 153 | || errno == EINTR) ; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
| 156 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | nsig = status & 0177; |
| 158 | core = status & 0200; |
| 159 | nexno = (status & 0xff00) >> 8; |
| 160 | */ |
| 161 | /***** LTP Port *****/ |
| 162 | nsig = WTERMSIG(status); |
| 163 | #ifdef WCOREDUMP |
| 164 | core = WCOREDUMP(status); |
| 165 | #endif |
| 166 | nexno = WIFEXITED(status); |
| 167 | /***** ** ** *****/ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | /* nsig is the signal number returned by wait |
| 170 | it should be 0, except when sig = 9 */ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 171 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 172 | if ((sig == 9) && (nsig != sig)) { |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 173 | fprintf(temp, "wait error: unexpected signal" |
| 174 | " returned when the signal sent was 9" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | " The status of the process is %d \n", |
| 176 | status); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 177 | ret_val = 1; |
| 178 | } |
| 179 | if ((sig != 9) && (nsig != 0)) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 180 | fprintf(temp, "wait error: unexpected signal " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | "returned, the status of the process is " |
| 182 | "%d \n", status); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 183 | ret_val = 1; |
| 184 | } |
| 185 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | /* nexno is the exit number returned by wait |
| 187 | it should be 1, except when sig = 9 */ |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 188 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 189 | if (sig == 9) |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 190 | if (nexno != 0) { |
| 191 | fprintf(temp, "signal error: unexpected" |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 192 | " exit number returned when" |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 193 | " signal sent was 9, the status" |
| 194 | " of the process is %d \n", |
| 195 | status); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | ret_val = 1; |
| 197 | } else; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 198 | else if (nexno != 1) { |
| 199 | fprintf(temp, "signal error: unexpected exit " |
| 200 | "number returned,the status of the" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | " process is %d\n", status); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 202 | ret_val = 1; |
| 203 | } |
| 204 | } |
| 205 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | if (ret_val) |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 207 | local_flag = FAILED; |
| 208 | |
| 209 | /*--------------------------------------------------------------------*/ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 210 | anyfail(); |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 211 | return 0; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 212 | } /******** END OF MAIN. ********/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 214 | /*--------------------------------------------------------------------*/ |
| 215 | |
| 216 | void chsig() |
| 217 | { |
| 218 | chflag++; |
| 219 | } |
| 220 | |
| 221 | /****** LTP Port *****/ |
| 222 | int anyfail() |
| 223 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | (local_flag == FAILED) ? tst_resm(TFAIL, |
| 225 | "Test failed") : tst_resm(TPASS, |
| 226 | "Test passed"); |
| 227 | tst_exit(); |
| 228 | return 0; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 229 | } |
| 230 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 231 | void do_child() |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 232 | { |
| 233 | int exno = 1; |
| 234 | |
| 235 | #ifdef UCLINUX |
| 236 | if (sigset(SIGCLD, chsig) == SIG_ERR) { |
| 237 | fprintf(temp, "\tsigset failed, errno = %d\n", errno); |
| 238 | fail_exit(); |
| 239 | } |
| 240 | #endif |
| 241 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 242 | sigset(sig, SIG_IGN); /* set to ignore signal */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 243 | kill(getppid(), SIGCLD); /* tell parent we are ready */ |
| 244 | while (!chflag) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 245 | sleep(1); /* wait for parent */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 246 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 247 | exit(exno); |
| 248 | } |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 249 | |
| 250 | void setup() |
| 251 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 252 | temp = stderr; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | int blenter() |
| 256 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | //tst_resm(TINFO, "Enter block %d", block_number); |
| 258 | local_flag = PASSED; |
| 259 | return 0; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 260 | } |
| 261 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 262 | void terror(char *message) |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 263 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 264 | tst_resm(TBROK, "Reason: %s:%s", message, strerror(errno)); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void fail_exit() |
| 268 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 269 | local_flag = FAILED; |
| 270 | anyfail(); |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 271 | |
| 272 | } |
| 273 | |
| 274 | int forkfail() |
| 275 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 276 | tst_resm(TBROK, "FORK FAILED - terminating test."); |
| 277 | tst_exit(); |
| 278 | return 0; |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 279 | } |
robbiew | e237cd3 | 2002-12-31 21:21:45 +0000 | [diff] [blame] | 280 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 281 | /****** ** ** *******/ |