plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | * |
| 32 | */ |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 33 | /* $Id: kill02.c,v 1.10 2009/08/28 13:20:15 vapier Exp $ */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | /*********************************************************************************** |
| 35 | |
| 36 | OS Test - Silicon Graphics, Inc. |
| 37 | |
| 38 | TEST IDENTIFIER : kill02 Sending a signal to processes with the same process group ID. |
| 39 | |
| 40 | PARENT DOCUMENT : kiltds01 Kill System Call. |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 41 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | AUTHOR : Dave Baumgartner |
| 43 | |
| 44 | CO-PILOT : Barrie Kletscher |
| 45 | |
| 46 | DATE STARTED : 12/30/85 |
| 47 | |
| 48 | TEST ITEMS |
| 49 | |
| 50 | 1. Sending a signal to pid of zero sends to all processes whose process |
| 51 | group ID is equal to the process group ID as the sender. |
| 52 | |
| 53 | 2. Sending a signal to pid of zero does not send to processes in another process group. |
| 54 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | OUTPUT SPECIFICATIONS |
| 56 | |
| 57 | PASS : |
| 58 | kiltcs02 1 PASS The signal was sent to all processes in the process group. |
| 59 | kiltcs02 2 PASS The signal was not sent to selective processes that were not in the process group. |
| 60 | |
| 61 | FAIL : |
| 62 | kiltcs02 1 FAIL The signal was not sent to all processes in the process group. |
| 63 | kiltcs02 2 FAIL The signal was sent to a process that was not in the process group. |
| 64 | |
| 65 | BROK : |
| 66 | kiltcs02 # BROK System call XXX failed. Errno:X, Error message:XXX. |
| 67 | kiltcs02 # BROK Setting to catch unexpected signal %d failed. Errno: %d, Error message %s. |
| 68 | kiltcs02 # BROK Setting to ignore signal %d failed. Errno: %d, Error message %s. |
| 69 | |
| 70 | WARN : |
| 71 | kiltcs02 0 WARN Unexpected signal X was caught. |
| 72 | |
| 73 | SPECIAL PROCEDURAL REQUIREMENTS |
| 74 | |
| 75 | The program must be linked with tst_res.o. |
| 76 | |
| 77 | DETAILED DESCRIPTION |
| 78 | |
| 79 | **Setup** |
| 80 | Set up unexpected signal handling. |
| 81 | Set up one pipe for each process to be created with no blocking for read. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 82 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | **MAIN** |
| 84 | If setup fails exit. |
| 85 | Fork 2 children(1 & 2). |
| 86 | Wait for set up complete messages from the 1st and 2nd child. |
| 87 | Send the signal SIGUSR1 with pid equal to zero. |
| 88 | Sleep a reasonable amount of time so that each child has been swapped in |
| 89 | to process the signal. |
| 90 | Now decide the outcome of the test items by reading from each pipe to find |
| 91 | out if the child was interrupted by the signal and wrote to it. |
| 92 | Remove the second child. |
| 93 | Tell the first child it is time to remove it's child B because the decisions have been made. |
| 94 | Exit. |
| 95 | |
| 96 | **First Child** |
| 97 | Set to catch SIGUSR1 with an int_rout1. |
| 98 | Set up to handle the message from the parent to remove child B. |
| 99 | Fork two children(A & B). |
| 100 | Wait for set up complete messages from child A & child B. |
| 101 | Send a set up complete message to the parent. |
| 102 | Pause until the signal SIGUSR1 comes in from the parent. |
| 103 | Pause until the parent says it is time to remove the child. |
| 104 | Exit. |
| 105 | |
| 106 | **Second Child** |
| 107 | Set to catch SIGUSR1 with an int_rout2. |
| 108 | Set the process group to be something different than the parents. |
| 109 | Send a set up complete message to the parent. |
| 110 | Pause until killed by parent because this child shouldn't receive signal SIGUSR1. |
| 111 | |
| 112 | **Child A** |
| 113 | Set to catch SIGUSR1 with an int_routA. |
| 114 | Send a set up complete message to the parent(First Child). |
| 115 | Pause until the signal SIGUSR1 comes in from the parent. |
| 116 | Exit. |
| 117 | |
| 118 | **Child B** |
| 119 | Set to catch SIGUSR1 with an int_routB. |
| 120 | Set the process group to be something different than the parents(First Child's). |
| 121 | Send a set up complete message to the parent. |
| 122 | Pause until killed by parent because this child shouldn't receive signal SIGUSR1. |
| 123 | |
| 124 | **usr1_rout-Used by all children** |
| 125 | Write to the appropriate pipe that the signal SIGUSR1 was caught. |
| 126 | |
| 127 | **usr2_rout** |
| 128 | Remove child B. |
| 129 | |
| 130 | ******************************************************************************/ |
| 131 | #include <sys/param.h> |
robbiew | e93a324 | 2005-08-31 20:27:12 +0000 | [diff] [blame] | 132 | #include <sys/wait.h> |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | #include <errno.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | #include <fcntl.h> |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 135 | #include <signal.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | #include <string.h> |
| 137 | #include <stdlib.h> |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | #include "usctest.h" |
| 140 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | #define CHAR_SET_FAILED "0" /*Set up failing status transferred through the pipe. */ |
| 142 | #define CHAR_SET_PASSED "1" /*Set up passing status transferred through the pipe. */ |
| 143 | #define SIG_CAUGHT "2" /*Indicates that the signal SIGUSR1 was caught. */ |
| 144 | #define SIG_RECEIVED 1 /*Integer value that indicates that the signal SIGUSR1 */ |
| 145 | /*was caught. */ |
| 146 | #define SIG_NOT_RECD 0 /*Integer value that indicates that the signal SIGUSR1 */ |
| 147 | /*was caught. */ |
| 148 | #define INT_SET_FAILED 0 /*Set up failing status transferred through the pipe. */ |
| 149 | #define INT_SET_PASSED 1 /*Set up passing status transferred through the pipe. */ |
| 150 | #define SLEEP_TIME 10 /*Amount of time the children get to catch the signal */ |
| 151 | #define TRUE 40 /*Child exits with this if execution was as */ |
| 152 | /*expected. */ |
| 153 | #define FALSE 50 /*Child exits with this if it timed out waiting for the */ |
| 154 | /*parents signal. */ |
| 155 | #define TIMEOUT 60 /*Amount of time given in alarm calls. */ |
| 156 | #define CHILD_EXIT(VAR) ((VAR >> 8) & 0377) /*Exit value from the child. */ |
| 157 | #define CHILD_SIG(VAR) (VAR & 0377) /*Signal value from the termination of child. */ |
| 158 | /*from the parent. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | int pid1; /*Return value from 1st fork. Global so that it can be */ |
| 161 | /*used in interrupt handling routines. */ |
| 162 | int pid2; /*Return value from 2nd fork. Global so that it can be */ |
| 163 | /*used in interrupt handling routines. */ |
| 164 | int pidA; /*Return value from 1st fork in child 1. Global so that it */ |
| 165 | /*can be used in interrupt handling routines. */ |
| 166 | int pidB; /*Return value from 2nd fork in child 1. Global so that it */ |
| 167 | /*can be used in interrupt handling routines. */ |
| 168 | int pipe1_fd[2]; /*Pipe file descriptors used for communication */ |
| 169 | /*between child 1 and the 1st parent. */ |
| 170 | int pipe2_fd[2]; /*Pipe file descriptors used for communication */ |
| 171 | /*between child 2 and the 1st parent. */ |
| 172 | int pipeA_fd[2]; /*Pipe file descriptors used for communication */ |
| 173 | /*between child A and the 1st parent. */ |
| 174 | int pipeB_fd[2]; /*Pipe file descriptors used for communication */ |
| 175 | /*between child B and the 1st parent. */ |
| 176 | char pipe_buf[10]; /*Pipe buffer. */ |
| 177 | char buf_tmp1[2], buf_tmp2[2]; /*Temp hold for info read into pipe_buf. */ |
| 178 | int read1_stat = 0; /*Number of characters read from pipe 1. */ |
| 179 | int read2_stat = 0; /*Number of characters read from pipe 2. */ |
| 180 | int readA_stat = 0; /*Number of characters read from pipe A. */ |
| 181 | int readB_stat = 0; /*Number of characters read from pipe B. */ |
| 182 | int alarm_flag = FALSE; /*This flag indicates an alarm time out. */ |
| 183 | char who_am_i = '0'; /*This indicates which process is which when using */ |
| 184 | /*interrupt routine usr1_rout. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | void notify_timeout(); /*Signal handler that the parent enters if it times out */ |
| 187 | /*waiting for the child to indicate its set up status. */ |
| 188 | void parent_rout(); /*This is the parents routine. */ |
| 189 | void child1_rout(); /*This is child 1's routine. */ |
| 190 | void child2_rout(); /*This is child 2's routine. */ |
| 191 | void childA_rout(); /*This is child A's routine. */ |
| 192 | void childB_rout(); /*This is child B's routine. */ |
| 193 | void usr1_rout(); /*This routine is used by all children to indicate that */ |
| 194 | /*they have caught signal SIGUSR1. */ |
| 195 | void par_kill(); /*This routine is called by the original parent to */ |
| 196 | /*remove child 2 and to indicate to child 1 to */ |
| 197 | /*remove its children. */ |
| 198 | void chld1_kill(); /*This routine is used by child 1 to remove itself and */ |
| 199 | /*its children A and B. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
| 201 | void setup(); |
| 202 | void cleanup(); |
| 203 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 204 | char *TCID = "kill02"; /* Test program identifier. */ |
| 205 | int TST_TOTAL = 2; /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | int exp_enos[] = { 0 }; /* Array of expected errnos */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 209 | #ifdef UCLINUX |
| 210 | static char *argv0; |
| 211 | void childA_rout_uclinux(); |
| 212 | void childB_rout_uclinux(); |
| 213 | #endif |
| 214 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | int lc; /* loop counter */ |
| 218 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 220 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 222 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 223 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | argv0 = av[0]; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 225 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | maybe_run_child(&childA_rout_uclinux, "nd", 1, &pipeA_fd[1]); |
| 227 | maybe_run_child(&childB_rout_uclinux, "nd", 2, &pipeB_fd[1]); |
| 228 | maybe_run_child(&child1_rout, "ndddddd", 3, &pipe1_fd[1], &pipe2_fd[1], |
| 229 | &pipeA_fd[0], &pipeA_fd[1], &pipeB_fd[0], &pipeB_fd[1]); |
| 230 | maybe_run_child(&child2_rout, "nd", 4, &pipe2_fd[1]); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 231 | #endif |
| 232 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 233 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 236 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 237 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 238 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | if ((pid1 = FORK_OR_VFORK()) > 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 240 | if ((pid2 = FORK_OR_VFORK()) > 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 241 | (void)parent_rout(); |
| 242 | } else if (pid2 == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 243 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 244 | if (self_exec(argv0, "nd", 4, pipe2_fd[1]) < 0) { |
| 245 | if (kill(pid1, SIGKILL) == -1 |
| 246 | && errno != ESRCH) { |
| 247 | tst_resm(TWARN, |
| 248 | "Child process may not have been killed."); |
| 249 | } |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 250 | tst_brkm(TBROK|TERRNO, cleanup, "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 252 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 253 | (void)child2_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 254 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 255 | } else { |
| 256 | /* |
| 257 | * The second fork failed kill the first child. |
| 258 | */ |
| 259 | if (kill(pid1, SIGKILL) == -1 && errno != ESRCH) { |
| 260 | tst_resm(TWARN, |
| 261 | "Child process may not have been killed."); |
| 262 | } |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 263 | tst_brkm(TBROK|TERRNO, cleanup, "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 264 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 265 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 266 | } else if (pid1 == 0) { |
| 267 | /* |
| 268 | * This is child 1. |
| 269 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 270 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 271 | if (self_exec |
| 272 | (argv0, "ndddddd", 3, pipe1_fd[1], pipe2_fd[1], |
| 273 | pipeA_fd[0], pipeA_fd[1], pipeB_fd[0], |
| 274 | pipeB_fd[1]) < 0) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 275 | tst_brkm(TBROK|TERRNO, cleanup, "self_exec() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 276 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 277 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 278 | (void)child1_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 279 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 280 | } else { |
| 281 | /* |
| 282 | * Fork failed. |
| 283 | */ |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 284 | tst_brkm(TBROK|TERRNO, cleanup, "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 285 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 287 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 288 | cleanup(); |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 289 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 290 | } /* END OF MAIN. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 291 | |
| 292 | /****************************************************************************** |
| 293 | * This is the parents routine. The parent waits for the children 1 and 2 to |
| 294 | * get set up. Then sends the signal and checks the outcome. |
| 295 | *********************************************************************************/ |
| 296 | void parent_rout() |
| 297 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 298 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 299 | * Set to catch the alarm signal SIGALRM. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 300 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 301 | if (signal(SIGALRM, notify_timeout) == SIG_ERR) { |
| 302 | (void)par_kill(); |
| 303 | tst_brkm(TBROK, NULL, |
| 304 | "Could not set to catch the parents time out alarm."); |
| 305 | tst_exit(); |
| 306 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 307 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 308 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 309 | * Setting to catch the timeout alarm worked now let the children start up. |
| 310 | * Set an alarm which causes a time out on the read pipe loop. |
| 311 | * The children will notify the parent that set up is complete |
| 312 | * and the pass/fail status of set up. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 313 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 314 | (void)alarm(TIMEOUT); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 315 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 316 | while ((read(pipe1_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 317 | /*EMPTY*/; |
| 318 | strncpy(buf_tmp1, pipe_buf, 1); |
| 319 | (void)alarm(TIMEOUT); |
| 320 | |
| 321 | while ((read(pipe2_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 322 | /*EMPTY*/; |
| 323 | (void)alarm(0); /*Reset the alarm clock. */ |
| 324 | strncpy(buf_tmp2, pipe_buf, 1); |
| 325 | |
| 326 | /* |
| 327 | * Check the alarm flag. |
| 328 | */ |
| 329 | if (alarm_flag == TRUE) { |
| 330 | tst_brkm(TBROK, NULL, |
| 331 | "The set up of the children failed by timing out."); |
| 332 | (void)par_kill(); |
| 333 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 334 | } |
| 335 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 336 | /* |
| 337 | * Check to see if either child failed in the set up. |
| 338 | */ |
| 339 | if ((strncmp(buf_tmp1, CHAR_SET_FAILED, 1) == 0) || |
| 340 | (strncmp(buf_tmp2, CHAR_SET_FAILED, 1) == 0)) { |
| 341 | /* |
| 342 | * Problems were encountered in the set up of one of the children. |
| 343 | * The error message has been displayed by the child. |
| 344 | */ |
| 345 | (void)par_kill(); |
| 346 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 347 | } |
| 348 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 349 | /* |
| 350 | * Setup passed, now send SIGUSR1 to process id of zero. |
| 351 | */ |
| 352 | TEST(kill(0, SIGUSR1)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 353 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 354 | if (TEST_RETURN == -1) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 355 | tst_brkm(TBROK|TERRNO, NULL, "kill() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 356 | (void)par_kill(); |
| 357 | cleanup(); |
| 358 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 359 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 360 | /* |
| 361 | * Sleep for a while to allow the children to get a chance to |
| 362 | * catch the signal. |
| 363 | */ |
| 364 | (void)sleep(SLEEP_TIME); |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 365 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 366 | /* |
| 367 | * The signal was sent above and time has run out for child response, |
| 368 | * check the outcomes. |
| 369 | */ |
| 370 | read1_stat = read(pipe1_fd[0], pipe_buf, 1); |
| 371 | if (read1_stat == -1 && errno == EAGAIN) |
| 372 | read1_stat = 0; |
| 373 | read2_stat = read(pipe2_fd[0], pipe_buf, 1); |
| 374 | if (read2_stat == -1 && errno == EAGAIN) |
| 375 | read2_stat = 0; |
| 376 | readA_stat = read(pipeA_fd[0], pipe_buf, 1); |
| 377 | if (readA_stat == -1 && errno == EAGAIN) |
| 378 | readA_stat = 0; |
| 379 | readB_stat = read(pipeB_fd[0], pipe_buf, 1); |
| 380 | if (readB_stat == -1 && errno == EAGAIN) |
| 381 | readB_stat = 0; |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 382 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 383 | if (read1_stat == -1 || read2_stat == -1 || |
| 384 | readA_stat == -1 || readB_stat == -1) { |
| 385 | /* |
| 386 | * The read system call failed. |
| 387 | */ |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 388 | tst_brkm(TBROK|TERRNO, NULL, "read() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 389 | (void)par_kill(); |
| 390 | cleanup(); |
| 391 | } |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 392 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 393 | /* |
| 394 | * Check the processes that were supposed to get the signal. |
| 395 | */ |
| 396 | if (read1_stat == SIG_RECEIVED) { |
| 397 | if (readA_stat == SIG_RECEIVED) { |
| 398 | /* |
| 399 | * Both processes, 1 and A, that were supposed to receive |
| 400 | * the signal did receive the signal. |
| 401 | */ |
| 402 | if (STD_FUNCTIONAL_TEST) |
| 403 | tst_resm(TPASS, |
| 404 | "The signal was sent to all processes in the process group."); |
| 405 | else |
| 406 | Tst_count++; |
| 407 | } else { /*Process A didn't receive the signal. */ |
| 408 | tst_resm(TFAIL, |
| 409 | "Process A did not receive the signal."); |
| 410 | } |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 411 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 412 | } else { /*Process 1 didn't receive the signal. */ |
| 413 | tst_resm(TFAIL, "Process 1 did not receive the signal."); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Check the processes that were not supposed to get the signal. |
| 418 | */ |
| 419 | if (read2_stat == SIG_NOT_RECD) { |
| 420 | if (readB_stat == SIG_NOT_RECD) { |
| 421 | /* |
| 422 | * Both processes, 2 and B did not receive the signal. |
| 423 | */ |
| 424 | if (STD_FUNCTIONAL_TEST) |
| 425 | tst_resm(TPASS, |
| 426 | "The signal was not sent to selective processes that were not in the process group."); |
| 427 | else |
| 428 | Tst_count++; |
| 429 | } else { /*Process B received the signal. */ |
| 430 | tst_resm(TFAIL, "Process B received the signal."); |
| 431 | } |
| 432 | |
| 433 | } |
| 434 | |
| 435 | else { /*Process 2 received the signal. */ |
| 436 | |
| 437 | tst_resm(TFAIL, "Process 2 received the signal."); |
| 438 | } |
| 439 | |
| 440 | (void)par_kill(); |
| 441 | |
| 442 | (void)alarm(TIMEOUT); |
| 443 | while ((read(pipe1_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 444 | strncpy(buf_tmp1, pipe_buf, 1); |
| 445 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 446 | } /*End of parent_rout */ |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 447 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 448 | void child1_rout() |
| 449 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 450 | who_am_i = '1'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 451 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 452 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 453 | * Set to catch the SIGUSR1 with int1_rout. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 454 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 455 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 456 | tst_brkm(TBROK, NULL, |
| 457 | "Could not set to catch the childrens signal."); |
| 458 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 459 | exit(0); |
| 460 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 461 | /* |
| 462 | * Create children A & B. |
| 463 | */ |
| 464 | if ((pidA = FORK_OR_VFORK()) > 0) { |
| 465 | /* |
| 466 | * This is the parent(child1), fork again to create child B. |
| 467 | */ |
| 468 | if ((pidB = FORK_OR_VFORK()) == 0) { |
| 469 | /* This is child B. */ |
| 470 | #ifdef UCLINUX |
| 471 | if (self_exec(argv0, "nd", 2, pipeB_fd[1]) < 0) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 472 | tst_brkm(TBROK|TERRNO, NULL, "self_exec() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 473 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 474 | exit(0); |
| 475 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 476 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 477 | (void)childB_rout(); |
| 478 | #endif |
| 479 | } |
| 480 | |
| 481 | else if (pidB == -1) { |
| 482 | /* |
| 483 | * The fork of child B failed kill child A. |
| 484 | */ |
| 485 | if (kill(pidA, SIGKILL) == -1) |
| 486 | tst_resm(TWARN, |
| 487 | "Child process may not have been killed."); |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 488 | tst_brkm(TBROK|TERRNO, NULL, "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 489 | (void)write(pipe2_fd[1], CHAR_SET_FAILED, 1); |
| 490 | exit(0); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | else if (pidA == 0) { |
| 495 | /* This is child A. */ |
| 496 | #ifdef UCLINUX |
| 497 | if (self_exec(argv0, "nd", 1, pipeA_fd[1]) < 0) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 498 | tst_brkm(TBROK|TERRNO, NULL, "self_exec() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 499 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 500 | exit(0); |
| 501 | } |
| 502 | #else |
| 503 | (void)childA_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 504 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 505 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 506 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 507 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 508 | else if (pidA == -1) { |
| 509 | /* |
| 510 | * The fork of child A failed. |
| 511 | */ |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 512 | tst_brkm(TBROK|TERRNO, NULL, "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 513 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 514 | exit(0); |
| 515 | } |
| 516 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 517 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 518 | * Set to catch the SIGUSR2 with chld1_kill. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 519 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 520 | if (signal(SIGUSR2, chld1_kill) == SIG_ERR) { |
| 521 | tst_brkm(TBROK, NULL, |
| 522 | "Could not set to catch the parents signal."); |
| 523 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 524 | (void)chld1_kill(); |
| 525 | exit(0); |
| 526 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 527 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 528 | /* |
| 529 | * Set to catch the alarm signal SIGALRM. |
| 530 | */ |
| 531 | if (signal(SIGALRM, notify_timeout) == SIG_ERR) { |
| 532 | tst_brkm(TBROK, NULL, |
| 533 | "Could not set to catch the childs time out alarm."); |
| 534 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 535 | (void)chld1_kill(); |
| 536 | exit(0); |
| 537 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 538 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 539 | /* |
| 540 | * Setting to catch the signals worked now let the children start up. |
| 541 | * Set an alarm which causes a time out on the pipe read loop. |
| 542 | * The children A & B will notify the parent(child1) that set up is complete |
| 543 | * and the pass/fail status of set up. |
| 544 | */ |
| 545 | (void)alarm(TIMEOUT - 40); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 546 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 547 | while ((read(pipeA_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 548 | /*EMPTY*/; |
| 549 | (void)alarm(TIMEOUT - 40); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 550 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 551 | while ((read(pipeB_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 552 | /*EMPTY*/; |
| 553 | (void)alarm(0); /*Reset the alarm clock. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 554 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 555 | /* |
| 556 | * Check the alarm flag. |
| 557 | */ |
| 558 | if (alarm_flag == TRUE) { |
| 559 | tst_brkm(TBROK, NULL, |
| 560 | "The set up of the children failed by timing out."); |
| 561 | (void)chld1_kill(); |
| 562 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 563 | exit(0); |
| 564 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 565 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 566 | /* |
| 567 | * Send a set up complete message to the parent. |
| 568 | */ |
| 569 | (void)write(pipe1_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 570 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 571 | /* |
| 572 | * Pause until the signal SIGUSR1 or SIGUSR2 is sent from the parent. |
| 573 | */ |
| 574 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 575 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 576 | /* |
| 577 | * Pause until signal SIGUSR2 is sent from the parent. |
| 578 | * This pause will only be executed if SIGUSR2 has not been received yet. |
| 579 | */ |
| 580 | while (1) { |
| 581 | sleep(1); |
| 582 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 583 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 584 | } /*End of child1_rout */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 585 | |
| 586 | /******************************************************************************* |
| 587 | * This is the routine for child 2, which should not receive the parents signal. |
| 588 | ******************************************************************************/ |
| 589 | void child2_rout() |
| 590 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 591 | who_am_i = '2'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 592 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 593 | /* |
| 594 | * Set the process group of this process to be different |
| 595 | * than the other processes. |
| 596 | */ |
| 597 | (void)setpgrp(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 598 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 599 | /* |
| 600 | * Set to catch the SIGUSR1 with usr1_rout. |
| 601 | */ |
| 602 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 603 | tst_brkm(TBROK, cleanup, |
| 604 | "Could not set to catch the parents signal."); |
| 605 | (void)write(pipe2_fd[1], CHAR_SET_FAILED, 1); |
| 606 | exit(0); |
| 607 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 608 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 609 | /* Send a set up complete message to parent. */ |
| 610 | (void)write(pipe2_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 611 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 612 | /* |
| 613 | * Pause until killed by the parent or SIGUSR1 is received. |
| 614 | */ |
| 615 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 616 | } |
| 617 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 618 | /******************************************************************************* |
| 619 | * This is the routine for child A, which should receive the parents signal. |
| 620 | ******************************************************************************/ |
| 621 | void childA_rout() |
| 622 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 623 | who_am_i = 'A'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 624 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 625 | /* Send a set up complete message to parent. */ |
| 626 | write(pipeA_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 627 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 628 | /* |
| 629 | * Pause until killed by the parent or SIGUSR1 is received. |
| 630 | */ |
| 631 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 632 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 633 | exit(0); |
| 634 | } /*End of childA_rout */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 635 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 636 | #ifdef UCLINUX |
| 637 | /******************************************************************************* |
| 638 | * This is the routine for child A after self_exec |
| 639 | ******************************************************************************/ |
| 640 | void childA_rout_uclinux() |
| 641 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 642 | /* Setup the signal handler again */ |
| 643 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 644 | tst_brkm(TBROK, NULL, |
| 645 | "Could not set to catch the childrens signal."); |
| 646 | (void)write(pipeA_fd[1], CHAR_SET_FAILED, 1); |
| 647 | exit(0); |
| 648 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 649 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 650 | childA_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 651 | } |
| 652 | #endif |
| 653 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 654 | /******************************************************************************* |
| 655 | * This is the routine for child B, which should not receive the parents signal. |
| 656 | ******************************************************************************/ |
| 657 | void childB_rout() |
| 658 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 659 | who_am_i = 'B'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 660 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 661 | /* |
| 662 | * Set the process group of this process to be different |
| 663 | * than the other processes. |
| 664 | */ |
| 665 | (void)setpgrp(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 666 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 667 | /* Send a set up complete message to parent(child 1). */ |
| 668 | write(pipeB_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 669 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 670 | /* |
| 671 | * Pause until killed by the parent(child 1) or SIGUSR1 is received. |
| 672 | */ |
| 673 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 674 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 675 | exit(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 676 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 677 | |
| 678 | #ifdef UCLINUX |
| 679 | /******************************************************************************* |
| 680 | * This is the routine for child B after self_exec |
| 681 | ******************************************************************************/ |
| 682 | void childB_rout_uclinux() |
| 683 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 684 | /* Setup the signal handler again */ |
| 685 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 686 | tst_brkm(TBROK, NULL, |
| 687 | "Could not set to catch the childrens signal."); |
| 688 | (void)write(pipeB_fd[1], CHAR_SET_FAILED, 1); |
| 689 | exit(0); |
| 690 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 691 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 692 | childB_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 693 | } |
| 694 | #endif |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 695 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 696 | /******************************************************************************* |
| 697 | * This routine sets up the interprocess communication pipes, signal handling, |
| 698 | * and process group information. |
| 699 | ******************************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 700 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 701 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 702 | int errno_buf; /*indicates the errno if pipe set up fails. */ |
| 703 | int err_flag = FALSE; /*Indicates if an error has occurred in pipe set up. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 704 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 705 | /* |
| 706 | * Set the process group ID to be equal between the parent and children. |
| 707 | */ |
| 708 | (void)setpgrp(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 709 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 710 | /* |
| 711 | * Set to catch unexpected signals. |
| 712 | * SIGCLD is set to be ignored because we do not wait for termination status. |
| 713 | * SIGUSR1 is set to be ignored because this is the signal we are using for |
| 714 | * the test and we are not concerned with the parent getting it. |
| 715 | */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 716 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 717 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 718 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 719 | if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 720 | tst_brkm(TBROK|TFAIL, NULL, "signal(SIGUSR1, SIG_IGN) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 721 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 722 | } |
| 723 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 724 | if (signal(SIGCLD, SIG_IGN) == SIG_ERR) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 725 | tst_brkm(TBROK|TERRNO, NULL, "signal(SIGCLD, SIG_IGN) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 726 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 727 | } |
| 728 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 729 | /* Indicate which errnos are expected */ |
| 730 | TEST_EXP_ENOS(exp_enos); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 731 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 732 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 733 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 734 | /* |
| 735 | * Set up pipe1, pipe2, pipeA, and pipeB. |
| 736 | */ |
| 737 | if ((pipe(pipe1_fd) == -1) |
| 738 | || (fcntl(pipe1_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 739 | errno_buf = errno; |
| 740 | err_flag = TRUE; |
| 741 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 742 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 743 | if ((pipe(pipe2_fd) == -1) |
| 744 | || (fcntl(pipe2_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 745 | errno_buf = errno; |
| 746 | err_flag = TRUE; |
| 747 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 748 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 749 | if ((pipe(pipeA_fd) == -1) |
| 750 | || (fcntl(pipeA_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 751 | errno_buf = errno; |
| 752 | err_flag = TRUE; |
| 753 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 754 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 755 | if ((pipe(pipeB_fd) == -1) |
| 756 | || (fcntl(pipeB_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 757 | errno_buf = errno; |
| 758 | err_flag = TRUE; |
| 759 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 760 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 761 | /* |
| 762 | * Check for errors. |
| 763 | */ |
| 764 | if (err_flag == TRUE) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 765 | tst_brkm(TBROK|TERRNO, NULL, "pipe() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 766 | tst_exit(); |
| 767 | } |
| 768 | return; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 769 | |
| 770 | } |
| 771 | |
| 772 | /*********************************************************** |
| 773 | * This routine indicates that the process caught SIGUSR1. |
| 774 | **********************************************************/ |
| 775 | void usr1_rout() |
| 776 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 777 | switch (who_am_i) { |
| 778 | case '1': |
| 779 | if (write(pipe1_fd[1], SIG_CAUGHT, 1) == -1) |
| 780 | tst_resm(TWARN, |
| 781 | "Writing signal catching status failed in child 1."); |
| 782 | break; |
| 783 | case '2': |
| 784 | if (write(pipe2_fd[1], SIG_CAUGHT, 1) == -1) |
| 785 | tst_resm(TWARN, |
| 786 | "Writing signal catching status failed in child 2."); |
| 787 | break; |
| 788 | case 'A': |
| 789 | if (write(pipeA_fd[1], SIG_CAUGHT, 1) == -1) |
| 790 | tst_resm(TWARN, |
| 791 | "Writing signal catching status failed in child A."); |
| 792 | break; |
| 793 | case 'B': |
| 794 | if (write(pipeB_fd[1], SIG_CAUGHT, 1) == -1) |
| 795 | tst_resm(TWARN, |
| 796 | "Writing signal catching status failed in child B."); |
| 797 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 798 | default: |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 799 | tst_resm(TWARN, |
| 800 | "Unexpected value %d for who_am_i in usr1_rout()", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 801 | who_am_i); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 802 | break; |
| 803 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 804 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 805 | } /*End of usr1_rout */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 806 | |
| 807 | /*********************************************************** |
| 808 | * This routine handles the timeout alarm in the parent, |
| 809 | * which occurs when the child fails to notify the parent |
| 810 | * the status of set up. |
| 811 | **********************************************************/ |
| 812 | void notify_timeout() |
| 813 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 814 | alarm_flag = TRUE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 815 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 816 | } /*End of notify_timeout */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 817 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 818 | /*********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 819 | * This routine handles the procedure for removing the |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 820 | * children forked off during this test. |
| 821 | **********************************************************/ |
| 822 | void par_kill() |
| 823 | { |
robbiew | 8f837d7 | 2005-06-15 15:47:04 +0000 | [diff] [blame] | 824 | int status; |
| 825 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 826 | /* |
| 827 | * Indicate to child1 that it can remove it's children and itself now. |
| 828 | */ |
| 829 | if (kill(pid1, SIGUSR2) == -1 && errno != ESRCH) { |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 830 | tst_resm(TWARN|TERRNO, "kill() failed"); |
| 831 | tst_resm(TWARN, "Child 1 and it's children may still be alive."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 832 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 833 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 834 | /* |
| 835 | * Remove child 2. |
| 836 | */ |
| 837 | if (kill(pid2, SIGKILL) == -1 && errno != ESRCH) |
| 838 | tst_resm(TWARN, "Child2 may still be alive."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 839 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 840 | wait(&status); |
| 841 | return; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 842 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 843 | } /*End of par_kill */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 844 | |
| 845 | /********************************************************************* |
| 846 | * This routine is executed by child 1 when the parent tells it to |
| 847 | * remove it's children and itself. |
| 848 | ********************************************************************/ |
| 849 | void chld1_kill() |
| 850 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 851 | /* |
| 852 | * Remove children A & B. |
| 853 | */ |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 854 | if (kill(pidA, SIGKILL) == -1 && errno != ESRCH) |
| 855 | tst_resm(TWARN|TERRNO, "kill(%d) failed; child 1's(A) child may still be alive", pidA); |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 856 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 857 | (void)write(pipe1_fd[1], CHAR_SET_PASSED, 1); |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 858 | |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 859 | if (kill(pidB, SIGKILL) == -1 && errno != ESRCH) |
| 860 | tst_resm(TWARN|TERRNO, "kill(%d) failed; child 1's(B) child may still be alive", pidB); |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 861 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 862 | exit(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 863 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 864 | } /*End of chld1_kill */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 865 | |
| 866 | /*************************************************************** |
| 867 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 868 | * completion or premature exit. |
| 869 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 870 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 871 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 872 | /* |
| 873 | * print timing stats if that option was specified. |
| 874 | * print errno log if that option was specified. |
| 875 | */ |
| 876 | TEST_CLEANUP; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 877 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 878 | } |