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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | #define CHAR_SET_FAILED "0" /*Set up failing status transferred through the pipe. */ |
| 141 | #define CHAR_SET_PASSED "1" /*Set up passing status transferred through the pipe. */ |
| 142 | #define SIG_CAUGHT "2" /*Indicates that the signal SIGUSR1 was caught. */ |
| 143 | #define SIG_RECEIVED 1 /*Integer value that indicates that the signal SIGUSR1 */ |
| 144 | /*was caught. */ |
| 145 | #define SIG_NOT_RECD 0 /*Integer value that indicates that the signal SIGUSR1 */ |
| 146 | /*was caught. */ |
| 147 | #define INT_SET_FAILED 0 /*Set up failing status transferred through the pipe. */ |
| 148 | #define INT_SET_PASSED 1 /*Set up passing status transferred through the pipe. */ |
| 149 | #define SLEEP_TIME 10 /*Amount of time the children get to catch the signal */ |
| 150 | #define TRUE 40 /*Child exits with this if execution was as */ |
| 151 | /*expected. */ |
| 152 | #define FALSE 50 /*Child exits with this if it timed out waiting for the */ |
| 153 | /*parents signal. */ |
| 154 | #define TIMEOUT 60 /*Amount of time given in alarm calls. */ |
| 155 | #define CHILD_EXIT(VAR) ((VAR >> 8) & 0377) /*Exit value from the child. */ |
| 156 | #define CHILD_SIG(VAR) (VAR & 0377) /*Signal value from the termination of child. */ |
| 157 | /*from the parent. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | int pid1; /*Return value from 1st fork. Global so that it can be */ |
| 160 | /*used in interrupt handling routines. */ |
| 161 | int pid2; /*Return value from 2nd fork. Global so that it can be */ |
| 162 | /*used in interrupt handling routines. */ |
| 163 | int pidA; /*Return value from 1st fork in child 1. Global so that it */ |
| 164 | /*can be used in interrupt handling routines. */ |
| 165 | int pidB; /*Return value from 2nd fork in child 1. Global so that it */ |
| 166 | /*can be used in interrupt handling routines. */ |
| 167 | int pipe1_fd[2]; /*Pipe file descriptors used for communication */ |
| 168 | /*between child 1 and the 1st parent. */ |
| 169 | int pipe2_fd[2]; /*Pipe file descriptors used for communication */ |
| 170 | /*between child 2 and the 1st parent. */ |
| 171 | int pipeA_fd[2]; /*Pipe file descriptors used for communication */ |
| 172 | /*between child A and the 1st parent. */ |
| 173 | int pipeB_fd[2]; /*Pipe file descriptors used for communication */ |
| 174 | /*between child B and the 1st parent. */ |
| 175 | char pipe_buf[10]; /*Pipe buffer. */ |
| 176 | char buf_tmp1[2], buf_tmp2[2]; /*Temp hold for info read into pipe_buf. */ |
| 177 | int read1_stat = 0; /*Number of characters read from pipe 1. */ |
| 178 | int read2_stat = 0; /*Number of characters read from pipe 2. */ |
| 179 | int readA_stat = 0; /*Number of characters read from pipe A. */ |
| 180 | int readB_stat = 0; /*Number of characters read from pipe B. */ |
| 181 | int alarm_flag = FALSE; /*This flag indicates an alarm time out. */ |
| 182 | char who_am_i = '0'; /*This indicates which process is which when using */ |
| 183 | /*interrupt routine usr1_rout. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | void notify_timeout(); /*Signal handler that the parent enters if it times out */ |
| 186 | /*waiting for the child to indicate its set up status. */ |
| 187 | void parent_rout(); /*This is the parents routine. */ |
| 188 | void child1_rout(); /*This is child 1's routine. */ |
| 189 | void child2_rout(); /*This is child 2's routine. */ |
| 190 | void childA_rout(); /*This is child A's routine. */ |
| 191 | void childB_rout(); /*This is child B's routine. */ |
| 192 | void usr1_rout(); /*This routine is used by all children to indicate that */ |
| 193 | /*they have caught signal SIGUSR1. */ |
| 194 | void par_kill(); /*This routine is called by the original parent to */ |
| 195 | /*remove child 2 and to indicate to child 1 to */ |
| 196 | /*remove its children. */ |
| 197 | void chld1_kill(); /*This routine is used by child 1 to remove itself and */ |
| 198 | /*its children A and B. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
| 200 | void setup(); |
| 201 | void cleanup(); |
| 202 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 203 | char *TCID = "kill02"; |
| 204 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 206 | #ifdef UCLINUX |
| 207 | static char *argv0; |
| 208 | void childA_rout_uclinux(); |
| 209 | void childB_rout_uclinux(); |
| 210 | #endif |
| 211 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 214 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 215 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | |
Garrett Cooper | 42b3538 | 2010-12-19 07:28:44 -0800 | [diff] [blame] | 217 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 219 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 220 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | argv0 = av[0]; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 222 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 223 | maybe_run_child(&childA_rout_uclinux, "nd", 1, &pipeA_fd[1]); |
| 224 | maybe_run_child(&childB_rout_uclinux, "nd", 2, &pipeB_fd[1]); |
| 225 | maybe_run_child(&child1_rout, "ndddddd", 3, &pipe1_fd[1], &pipe2_fd[1], |
| 226 | &pipeA_fd[0], &pipeA_fd[1], &pipeB_fd[0], &pipeB_fd[1]); |
| 227 | maybe_run_child(&child2_rout, "nd", 4, &pipe2_fd[1]); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 228 | #endif |
| 229 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 230 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 234 | tst_count = 0; |
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 | if ((pid1 = FORK_OR_VFORK()) > 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 237 | if ((pid2 = FORK_OR_VFORK()) > 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | (void)parent_rout(); |
| 239 | } else if (pid2 == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 240 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 241 | if (self_exec(argv0, "nd", 4, pipe2_fd[1]) < 0) { |
| 242 | if (kill(pid1, SIGKILL) == -1 |
| 243 | && errno != ESRCH) { |
| 244 | tst_resm(TWARN, |
| 245 | "Child process may not have been killed."); |
| 246 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 247 | tst_brkm(TBROK | TERRNO, cleanup, |
| 248 | "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 249 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 250 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | (void)child2_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 252 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 253 | } else { |
| 254 | /* |
| 255 | * The second fork failed kill the first child. |
| 256 | */ |
| 257 | if (kill(pid1, SIGKILL) == -1 && errno != ESRCH) { |
| 258 | tst_resm(TWARN, |
| 259 | "Child process may not have been killed."); |
| 260 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 261 | tst_brkm(TBROK | TERRNO, cleanup, |
| 262 | "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 263 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 264 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 265 | } else if (pid1 == 0) { |
| 266 | /* |
| 267 | * This is child 1. |
| 268 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 269 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 270 | if (self_exec |
| 271 | (argv0, "ndddddd", 3, pipe1_fd[1], pipe2_fd[1], |
| 272 | pipeA_fd[0], pipeA_fd[1], pipeB_fd[0], |
| 273 | pipeB_fd[1]) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 274 | tst_brkm(TBROK | TERRNO, cleanup, |
| 275 | "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 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +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 | *********************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 296 | void parent_rout(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 305 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 306 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 307 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 308 | * Setting to catch the timeout alarm worked now let the children start up. |
| 309 | * Set an alarm which causes a time out on the read pipe loop. |
| 310 | * The children will notify the parent that set up is complete |
| 311 | * and the pass/fail status of set up. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 312 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 313 | (void)alarm(TIMEOUT); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 314 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 315 | while ((read(pipe1_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 316 | /*EMPTY*/; |
| 317 | strncpy(buf_tmp1, pipe_buf, 1); |
| 318 | (void)alarm(TIMEOUT); |
| 319 | |
| 320 | while ((read(pipe2_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 321 | /*EMPTY*/; |
| 322 | (void)alarm(0); /*Reset the alarm clock. */ |
| 323 | strncpy(buf_tmp2, pipe_buf, 1); |
| 324 | |
| 325 | /* |
| 326 | * Check the alarm flag. |
| 327 | */ |
| 328 | if (alarm_flag == TRUE) { |
| 329 | tst_brkm(TBROK, NULL, |
| 330 | "The set up of the children failed by timing out."); |
| 331 | (void)par_kill(); |
| 332 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 333 | } |
| 334 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 335 | /* |
| 336 | * Check to see if either child failed in the set up. |
| 337 | */ |
| 338 | if ((strncmp(buf_tmp1, CHAR_SET_FAILED, 1) == 0) || |
| 339 | (strncmp(buf_tmp2, CHAR_SET_FAILED, 1) == 0)) { |
| 340 | /* |
| 341 | * Problems were encountered in the set up of one of the children. |
| 342 | * The error message has been displayed by the child. |
| 343 | */ |
| 344 | (void)par_kill(); |
| 345 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 346 | } |
| 347 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 348 | /* |
| 349 | * Setup passed, now send SIGUSR1 to process id of zero. |
| 350 | */ |
| 351 | TEST(kill(0, SIGUSR1)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 352 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 353 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 354 | tst_brkm(TBROK | TERRNO, NULL, "kill() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 355 | (void)par_kill(); |
| 356 | cleanup(); |
| 357 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 358 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 359 | /* |
| 360 | * Sleep for a while to allow the children to get a chance to |
| 361 | * catch the signal. |
| 362 | */ |
| 363 | (void)sleep(SLEEP_TIME); |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 364 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 365 | /* |
| 366 | * The signal was sent above and time has run out for child response, |
| 367 | * check the outcomes. |
| 368 | */ |
| 369 | read1_stat = read(pipe1_fd[0], pipe_buf, 1); |
| 370 | if (read1_stat == -1 && errno == EAGAIN) |
| 371 | read1_stat = 0; |
| 372 | read2_stat = read(pipe2_fd[0], pipe_buf, 1); |
| 373 | if (read2_stat == -1 && errno == EAGAIN) |
| 374 | read2_stat = 0; |
| 375 | readA_stat = read(pipeA_fd[0], pipe_buf, 1); |
| 376 | if (readA_stat == -1 && errno == EAGAIN) |
| 377 | readA_stat = 0; |
| 378 | readB_stat = read(pipeB_fd[0], pipe_buf, 1); |
| 379 | if (readB_stat == -1 && errno == EAGAIN) |
| 380 | readB_stat = 0; |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 381 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 382 | if (read1_stat == -1 || read2_stat == -1 || |
| 383 | readA_stat == -1 || readB_stat == -1) { |
| 384 | /* |
| 385 | * The read system call failed. |
| 386 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 387 | tst_brkm(TBROK | TERRNO, NULL, "read() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 388 | (void)par_kill(); |
| 389 | cleanup(); |
| 390 | } |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 391 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 392 | /* |
| 393 | * Check the processes that were supposed to get the signal. |
| 394 | */ |
| 395 | if (read1_stat == SIG_RECEIVED) { |
| 396 | if (readA_stat == SIG_RECEIVED) { |
| 397 | /* |
| 398 | * Both processes, 1 and A, that were supposed to receive |
| 399 | * the signal did receive the signal. |
| 400 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 401 | tst_resm(TPASS, |
| 402 | "The signal was sent to all processes in the process group."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 403 | } else { /*Process A didn't receive the signal. */ |
| 404 | tst_resm(TFAIL, |
| 405 | "Process A did not receive the signal."); |
| 406 | } |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 407 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 408 | } else { /*Process 1 didn't receive the signal. */ |
| 409 | tst_resm(TFAIL, "Process 1 did not receive the signal."); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Check the processes that were not supposed to get the signal. |
| 414 | */ |
| 415 | if (read2_stat == SIG_NOT_RECD) { |
| 416 | if (readB_stat == SIG_NOT_RECD) { |
| 417 | /* |
| 418 | * Both processes, 2 and B did not receive the signal. |
| 419 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 420 | tst_resm(TPASS, |
| 421 | "The signal was not sent to selective processes that were not in the process group."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 422 | } else { /*Process B received the signal. */ |
| 423 | tst_resm(TFAIL, "Process B received the signal."); |
| 424 | } |
| 425 | |
| 426 | } |
| 427 | |
| 428 | else { /*Process 2 received the signal. */ |
| 429 | |
| 430 | tst_resm(TFAIL, "Process 2 received the signal."); |
| 431 | } |
| 432 | |
| 433 | (void)par_kill(); |
| 434 | |
| 435 | (void)alarm(TIMEOUT); |
| 436 | while ((read(pipe1_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 437 | strncpy(buf_tmp1, pipe_buf, 1); |
| 438 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 439 | } /*End of parent_rout */ |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 440 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 441 | void child1_rout(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 442 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 443 | who_am_i = '1'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 444 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 445 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 446 | * Set to catch the SIGUSR1 with int1_rout. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 447 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 448 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 449 | tst_brkm(TBROK, NULL, |
| 450 | "Could not set to catch the childrens signal."); |
| 451 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 452 | exit(0); |
| 453 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 454 | /* |
| 455 | * Create children A & B. |
| 456 | */ |
| 457 | if ((pidA = FORK_OR_VFORK()) > 0) { |
| 458 | /* |
| 459 | * This is the parent(child1), fork again to create child B. |
| 460 | */ |
| 461 | if ((pidB = FORK_OR_VFORK()) == 0) { |
| 462 | /* This is child B. */ |
| 463 | #ifdef UCLINUX |
| 464 | if (self_exec(argv0, "nd", 2, pipeB_fd[1]) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 465 | tst_brkm(TBROK | TERRNO, NULL, |
| 466 | "self_exec() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 467 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 468 | exit(0); |
| 469 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 470 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 471 | (void)childB_rout(); |
| 472 | #endif |
| 473 | } |
| 474 | |
| 475 | else if (pidB == -1) { |
| 476 | /* |
| 477 | * The fork of child B failed kill child A. |
| 478 | */ |
| 479 | if (kill(pidA, SIGKILL) == -1) |
| 480 | tst_resm(TWARN, |
| 481 | "Child process may not have been killed."); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 482 | tst_brkm(TBROK | TERRNO, NULL, "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 483 | (void)write(pipe2_fd[1], CHAR_SET_FAILED, 1); |
| 484 | exit(0); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | else if (pidA == 0) { |
| 489 | /* This is child A. */ |
| 490 | #ifdef UCLINUX |
| 491 | if (self_exec(argv0, "nd", 1, pipeA_fd[1]) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 492 | tst_brkm(TBROK | TERRNO, NULL, "self_exec() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 493 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 494 | exit(0); |
| 495 | } |
| 496 | #else |
| 497 | (void)childA_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 498 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 499 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 500 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 501 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 502 | else if (pidA == -1) { |
| 503 | /* |
| 504 | * The fork of child A failed. |
| 505 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 506 | tst_brkm(TBROK | TERRNO, NULL, "fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 507 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 508 | exit(0); |
| 509 | } |
| 510 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 511 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 512 | * Set to catch the SIGUSR2 with chld1_kill. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 513 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 514 | if (signal(SIGUSR2, chld1_kill) == SIG_ERR) { |
| 515 | tst_brkm(TBROK, NULL, |
| 516 | "Could not set to catch the parents signal."); |
| 517 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 518 | (void)chld1_kill(); |
| 519 | exit(0); |
| 520 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 521 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 522 | /* |
| 523 | * Set to catch the alarm signal SIGALRM. |
| 524 | */ |
| 525 | if (signal(SIGALRM, notify_timeout) == SIG_ERR) { |
| 526 | tst_brkm(TBROK, NULL, |
| 527 | "Could not set to catch the childs time out alarm."); |
| 528 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 529 | (void)chld1_kill(); |
| 530 | exit(0); |
| 531 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 532 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 533 | /* |
| 534 | * Setting to catch the signals worked now let the children start up. |
| 535 | * Set an alarm which causes a time out on the pipe read loop. |
| 536 | * The children A & B will notify the parent(child1) that set up is complete |
| 537 | * and the pass/fail status of set up. |
| 538 | */ |
| 539 | (void)alarm(TIMEOUT - 40); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 540 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 541 | while ((read(pipeA_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 542 | /*EMPTY*/; |
| 543 | (void)alarm(TIMEOUT - 40); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 544 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 545 | while ((read(pipeB_fd[0], pipe_buf, 1) != 1) && (alarm_flag == FALSE)) |
| 546 | /*EMPTY*/; |
| 547 | (void)alarm(0); /*Reset the alarm clock. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 548 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 549 | /* |
| 550 | * Check the alarm flag. |
| 551 | */ |
| 552 | if (alarm_flag == TRUE) { |
| 553 | tst_brkm(TBROK, NULL, |
| 554 | "The set up of the children failed by timing out."); |
| 555 | (void)chld1_kill(); |
| 556 | (void)write(pipe1_fd[1], CHAR_SET_FAILED, 1); |
| 557 | exit(0); |
| 558 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 559 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 560 | /* |
| 561 | * Send a set up complete message to the parent. |
| 562 | */ |
| 563 | (void)write(pipe1_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 564 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 565 | /* |
| 566 | * Pause until the signal SIGUSR1 or SIGUSR2 is sent from the parent. |
| 567 | */ |
| 568 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 569 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 570 | /* |
| 571 | * Pause until signal SIGUSR2 is sent from the parent. |
| 572 | * This pause will only be executed if SIGUSR2 has not been received yet. |
| 573 | */ |
| 574 | while (1) { |
| 575 | sleep(1); |
| 576 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 577 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 578 | } /*End of child1_rout */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 579 | |
| 580 | /******************************************************************************* |
| 581 | * This is the routine for child 2, which should not receive the parents signal. |
| 582 | ******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 583 | void child2_rout(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 584 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 585 | who_am_i = '2'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 586 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 587 | /* |
| 588 | * Set the process group of this process to be different |
| 589 | * than the other processes. |
| 590 | */ |
| 591 | (void)setpgrp(); |
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 to catch the SIGUSR1 with usr1_rout. |
| 595 | */ |
| 596 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 597 | tst_brkm(TBROK, cleanup, |
| 598 | "Could not set to catch the parents signal."); |
| 599 | (void)write(pipe2_fd[1], CHAR_SET_FAILED, 1); |
| 600 | exit(0); |
| 601 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 602 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 603 | /* Send a set up complete message to parent. */ |
| 604 | (void)write(pipe2_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 605 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 606 | /* |
| 607 | * Pause until killed by the parent or SIGUSR1 is received. |
| 608 | */ |
| 609 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 610 | } |
| 611 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 612 | /******************************************************************************* |
| 613 | * This is the routine for child A, which should receive the parents signal. |
| 614 | ******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 615 | void childA_rout(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 616 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 617 | who_am_i = 'A'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 618 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 619 | /* Send a set up complete message to parent. */ |
| 620 | write(pipeA_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 621 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 622 | /* |
| 623 | * Pause until killed by the parent or SIGUSR1 is received. |
| 624 | */ |
| 625 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 626 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 627 | exit(0); |
| 628 | } /*End of childA_rout */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 629 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 630 | #ifdef UCLINUX |
| 631 | /******************************************************************************* |
| 632 | * This is the routine for child A after self_exec |
| 633 | ******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 634 | void childA_rout_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 635 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 636 | /* Setup the signal handler again */ |
| 637 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 638 | tst_brkm(TBROK, NULL, |
| 639 | "Could not set to catch the childrens signal."); |
| 640 | (void)write(pipeA_fd[1], CHAR_SET_FAILED, 1); |
| 641 | exit(0); |
| 642 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 643 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 644 | childA_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 645 | } |
| 646 | #endif |
| 647 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 648 | /******************************************************************************* |
| 649 | * This is the routine for child B, which should not receive the parents signal. |
| 650 | ******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 651 | void childB_rout(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 652 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 653 | who_am_i = 'B'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 654 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 655 | /* |
| 656 | * Set the process group of this process to be different |
| 657 | * than the other processes. |
| 658 | */ |
| 659 | (void)setpgrp(); |
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 | /* Send a set up complete message to parent(child 1). */ |
| 662 | write(pipeB_fd[1], CHAR_SET_PASSED, 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 663 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 664 | /* |
| 665 | * Pause until killed by the parent(child 1) or SIGUSR1 is received. |
| 666 | */ |
| 667 | (void)pause(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 668 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 669 | exit(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 670 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 671 | |
| 672 | #ifdef UCLINUX |
| 673 | /******************************************************************************* |
| 674 | * This is the routine for child B after self_exec |
| 675 | ******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 676 | void childB_rout_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 677 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 678 | /* Setup the signal handler again */ |
| 679 | if (signal(SIGUSR1, usr1_rout) == SIG_ERR) { |
| 680 | tst_brkm(TBROK, NULL, |
| 681 | "Could not set to catch the childrens signal."); |
| 682 | (void)write(pipeB_fd[1], CHAR_SET_FAILED, 1); |
| 683 | exit(0); |
| 684 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 685 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 686 | childB_rout(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 687 | } |
| 688 | #endif |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 689 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 690 | /******************************************************************************* |
| 691 | * This routine sets up the interprocess communication pipes, signal handling, |
| 692 | * and process group information. |
| 693 | ******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 694 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 695 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 696 | int errno_buf; /*indicates the errno if pipe set up fails. */ |
| 697 | 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] | 698 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 699 | /* |
| 700 | * Set the process group ID to be equal between the parent and children. |
| 701 | */ |
| 702 | (void)setpgrp(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 703 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 704 | /* |
| 705 | * Set to catch unexpected signals. |
| 706 | * SIGCLD is set to be ignored because we do not wait for termination status. |
| 707 | * SIGUSR1 is set to be ignored because this is the signal we are using for |
| 708 | * the test and we are not concerned with the parent getting it. |
| 709 | */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 710 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 711 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 712 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 713 | if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 714 | tst_brkm(TBROK | TFAIL, NULL, |
| 715 | "signal(SIGUSR1, SIG_IGN) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 716 | } |
| 717 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 718 | if (signal(SIGCLD, SIG_IGN) == SIG_ERR) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 719 | tst_brkm(TBROK | TERRNO, NULL, |
| 720 | "signal(SIGCLD, SIG_IGN) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 721 | } |
| 722 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 723 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 724 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 725 | /* |
| 726 | * Set up pipe1, pipe2, pipeA, and pipeB. |
| 727 | */ |
| 728 | if ((pipe(pipe1_fd) == -1) |
| 729 | || (fcntl(pipe1_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 730 | errno_buf = errno; |
| 731 | err_flag = TRUE; |
| 732 | } |
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 | if ((pipe(pipe2_fd) == -1) |
| 735 | || (fcntl(pipe2_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 736 | errno_buf = errno; |
| 737 | err_flag = TRUE; |
| 738 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 739 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 740 | if ((pipe(pipeA_fd) == -1) |
| 741 | || (fcntl(pipeA_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 742 | errno_buf = errno; |
| 743 | err_flag = TRUE; |
| 744 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 745 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 746 | if ((pipe(pipeB_fd) == -1) |
| 747 | || (fcntl(pipeB_fd[0], F_SETFL, O_NDELAY) == -1)) { |
| 748 | errno_buf = errno; |
| 749 | err_flag = TRUE; |
| 750 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 751 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 752 | /* |
| 753 | * Check for errors. |
| 754 | */ |
| 755 | if (err_flag == TRUE) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 756 | tst_brkm(TBROK | TERRNO, NULL, "pipe() failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 757 | } |
| 758 | return; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 759 | |
| 760 | } |
| 761 | |
| 762 | /*********************************************************** |
| 763 | * This routine indicates that the process caught SIGUSR1. |
| 764 | **********************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 765 | void usr1_rout(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 766 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 767 | switch (who_am_i) { |
| 768 | case '1': |
| 769 | if (write(pipe1_fd[1], SIG_CAUGHT, 1) == -1) |
| 770 | tst_resm(TWARN, |
| 771 | "Writing signal catching status failed in child 1."); |
| 772 | break; |
| 773 | case '2': |
| 774 | if (write(pipe2_fd[1], SIG_CAUGHT, 1) == -1) |
| 775 | tst_resm(TWARN, |
| 776 | "Writing signal catching status failed in child 2."); |
| 777 | break; |
| 778 | case 'A': |
| 779 | if (write(pipeA_fd[1], SIG_CAUGHT, 1) == -1) |
| 780 | tst_resm(TWARN, |
| 781 | "Writing signal catching status failed in child A."); |
| 782 | break; |
| 783 | case 'B': |
| 784 | if (write(pipeB_fd[1], SIG_CAUGHT, 1) == -1) |
| 785 | tst_resm(TWARN, |
| 786 | "Writing signal catching status failed in child B."); |
| 787 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 788 | default: |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 789 | tst_resm(TWARN, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 790 | "Unexpected value %d for who_am_i in usr1_rout()", |
| 791 | who_am_i); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 792 | break; |
| 793 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 794 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 795 | } /*End of usr1_rout */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 796 | |
| 797 | /*********************************************************** |
| 798 | * This routine handles the timeout alarm in the parent, |
| 799 | * which occurs when the child fails to notify the parent |
| 800 | * the status of set up. |
| 801 | **********************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 802 | void notify_timeout(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 803 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 804 | alarm_flag = TRUE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 805 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 806 | } /*End of notify_timeout */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 807 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 808 | /*********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 809 | * This routine handles the procedure for removing the |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 810 | * children forked off during this test. |
| 811 | **********************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 812 | void par_kill(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 813 | { |
robbiew | 8f837d7 | 2005-06-15 15:47:04 +0000 | [diff] [blame] | 814 | int status; |
| 815 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 816 | /* |
| 817 | * Indicate to child1 that it can remove it's children and itself now. |
| 818 | */ |
| 819 | if (kill(pid1, SIGUSR2) == -1 && errno != ESRCH) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 820 | tst_resm(TWARN | TERRNO, "kill() failed"); |
| 821 | tst_resm(TWARN, |
| 822 | "Child 1 and it's children may still be alive."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 823 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 824 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 825 | /* |
| 826 | * Remove child 2. |
| 827 | */ |
| 828 | if (kill(pid2, SIGKILL) == -1 && errno != ESRCH) |
| 829 | tst_resm(TWARN, "Child2 may still be alive."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 830 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 831 | wait(&status); |
| 832 | return; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 833 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 834 | } /*End of par_kill */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 835 | |
| 836 | /********************************************************************* |
| 837 | * This routine is executed by child 1 when the parent tells it to |
| 838 | * remove it's children and itself. |
| 839 | ********************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 840 | void chld1_kill(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 841 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 842 | /* |
| 843 | * Remove children A & B. |
| 844 | */ |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 845 | if (kill(pidA, SIGKILL) == -1 && errno != ESRCH) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 846 | tst_resm(TWARN | TERRNO, |
| 847 | "kill(%d) failed; child 1's(A) child may still be alive", |
| 848 | pidA); |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 849 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 850 | (void)write(pipe1_fd[1], CHAR_SET_PASSED, 1); |
mreed10 | 6209170 | 2006-09-06 16:45:02 +0000 | [diff] [blame] | 851 | |
vapier | e7a6ab5 | 2009-08-28 13:20:15 +0000 | [diff] [blame] | 852 | if (kill(pidB, SIGKILL) == -1 && errno != ESRCH) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 853 | tst_resm(TWARN | TERRNO, |
| 854 | "kill(%d) failed; child 1's(B) child may still be alive", |
| 855 | pidB); |
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 | exit(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 858 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 859 | } /*End of chld1_kill */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 860 | |
| 861 | /*************************************************************** |
| 862 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 863 | * completion or premature exit. |
| 864 | ***************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 865 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 866 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 867 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 868 | } |