plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * waitpid06.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Tests to see if pid's returned from fork and waitpid are same. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Check proper functioning of waitpid with pid = -1 and arg = 0 |
| 29 | * |
| 30 | * USAGE: <for command-line> |
| 31 | * waitpid06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 32 | * where, -c n : Run n copies concurrently. |
| 33 | * -e : Turn on errno logging. |
| 34 | * -i n : Execute test n times. |
| 35 | * -I x : Execute test for x seconds. |
| 36 | * -P x : Pause for x seconds between iterations. |
| 37 | * -t : Turn on syscall timing. |
| 38 | * |
| 39 | * History |
| 40 | * 07/2001 John George |
| 41 | * -Ported |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 42 | * 04/2002 wjhuie sigset cleanups |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * |
| 44 | * Restrictions |
| 45 | * None |
| 46 | */ |
| 47 | |
| 48 | #include <sys/types.h> |
| 49 | #include <signal.h> |
| 50 | #include <errno.h> |
| 51 | #include <sys/wait.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 52 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 54 | static void setup_sigint(void); |
| 55 | static void do_child_1(void); |
| 56 | static void setup(void); |
| 57 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | char *TCID = "waitpid06"; |
| 60 | int TST_TOTAL = 1; |
robbiew | 3d5c735 | 2003-01-13 17:49:51 +0000 | [diff] [blame] | 61 | volatile int intintr; |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 62 | static void inthandlr(); |
| 63 | static void do_exit(void); |
| 64 | static int flag; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
| 66 | #define FAILED 1 |
| 67 | #define MAXKIDS 8 |
| 68 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 69 | #ifdef UCLINUX |
| 70 | static char *argv0; |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 71 | static void do_child_2_uclinux(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
plars | ac85d1c | 2002-06-10 14:52:37 +0000 | [diff] [blame] | 74 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 76 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 77 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | int fail = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | int pid; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 80 | int status; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 82 | msg = parse_opts(argc, argv, NULL, NULL); |
| 83 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 85 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 86 | #ifdef UCLINUX |
| 87 | argv0 = argv[0]; |
| 88 | |
| 89 | maybe_run_child(&do_child_1, "n", 1); |
| 90 | maybe_run_child(&do_child_2_uclinux, "n", 2); |
| 91 | #endif |
| 92 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | setup(); |
| 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 96 | /* reset tst_count in case we are looping */ |
| 97 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 99 | pid = FORK_OR_VFORK(); |
| 100 | if (pid < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_resm(TINFO, "Fork Failed, may be OK under stress"); |
| 102 | exit(pid); |
| 103 | } else if (pid == 0) { |
| 104 | /* |
| 105 | * Child: |
| 106 | * Set up to catch SIGINT. The kids will wait till a |
| 107 | * SIGINT has been received before they proceed. |
| 108 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 109 | #ifdef UCLINUX |
| 110 | if (self_exec(argv[0], "n", 1) < 0) { |
| 111 | tst_resm(TINFO, "self_exec failed"); |
| 112 | exit(pid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 114 | #else |
| 115 | do_child_1(); |
| 116 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | } else { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | fail = 0; |
| 119 | waitpid(pid, &status, 0); |
| 120 | if (WEXITSTATUS(status) != 0) { |
| 121 | tst_resm(TFAIL, "child returned bad status"); |
| 122 | fail = 1; |
| 123 | } |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 124 | if (fail) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_resm(TFAIL, "%s FAILED", TCID); |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 126 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | tst_resm(TPASS, "%s PASSED", TCID); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 130 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 132 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 136 | * setup_sigint() |
| 137 | * Sets up a SIGINT handler |
| 138 | */ |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 139 | static void setup_sigint(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 140 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | if ((sig_t) signal(SIGINT, inthandlr) == SIG_ERR) { |
| 142 | tst_resm(TFAIL, "signal SIGINT failed. " "errno = %d", errno); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 143 | exit(-1); |
| 144 | } |
| 145 | } |
| 146 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 147 | static void do_child_1(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 148 | { |
| 149 | int kid_count, fork_kid_pid[MAXKIDS]; |
| 150 | int ret_val; |
| 151 | int i, j, k, found; |
| 152 | int group1, group2; |
| 153 | int wait_kid_pid[MAXKIDS], status; |
| 154 | |
| 155 | setup_sigint(); |
| 156 | |
| 157 | group1 = getpgrp(); |
| 158 | for (kid_count = 0; kid_count < MAXKIDS; kid_count++) { |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 159 | if (kid_count == (MAXKIDS / 2)) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 160 | group2 = setpgrp(); |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 161 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 162 | intintr = 0; |
| 163 | ret_val = FORK_OR_VFORK(); |
| 164 | if (ret_val == 0) { /* child */ |
| 165 | #ifdef UCLINUX |
| 166 | if (self_exec(argv0, "n", 2) < 0) { |
| 167 | tst_resm(TFAIL, "Fork kid %d failed. " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | "errno = %d", kid_count, errno); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 169 | exit(ret_val); |
| 170 | } |
| 171 | #else |
| 172 | do_exit(); |
| 173 | #endif |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 174 | } else if (ret_val < 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 175 | tst_resm(TFAIL, "Fork kid %d failed. " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | "errno = %d", kid_count, errno); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 177 | exit(ret_val); |
| 178 | } |
| 179 | |
| 180 | /* parent */ |
| 181 | fork_kid_pid[kid_count] = ret_val; |
| 182 | } |
| 183 | |
| 184 | #ifdef UCLINUX |
| 185 | /* Give the kids a chance to setup SIGINT again, since this is |
| 186 | * cleared by exec(). |
| 187 | */ |
| 188 | sleep(3); |
| 189 | #endif |
| 190 | |
| 191 | /* Now send all the kids a SIGINT to tell them to |
| 192 | * proceed |
| 193 | */ |
| 194 | for (i = 0; i < MAXKIDS; i++) { |
| 195 | if (kill(fork_kid_pid[i], SIGINT) < 0) { |
| 196 | tst_resm(TFAIL, "Kill of child %d " |
| 197 | "failed, errno = %d", i, errno); |
| 198 | exit(-1); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Wait till all kids have terminated. Stash away their |
| 204 | * pid's in an array. |
| 205 | */ |
| 206 | kid_count = 0; |
| 207 | errno = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 208 | while (((ret_val = waitpid(-1, &status, 0)) != -1) || (errno == EINTR)) { |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 209 | if (ret_val == -1) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 210 | continue; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 211 | |
| 212 | if (!WIFEXITED(status)) { |
| 213 | tst_resm(TFAIL, "Child %d did not exit " |
| 214 | "normally", ret_val); |
| 215 | flag = FAILED; |
| 216 | printf("status: %d\n", status); |
| 217 | } else { |
| 218 | if (WEXITSTATUS(status) != 3) { |
| 219 | tst_resm(TFAIL, "Child %d" |
| 220 | "exited with wrong " |
| 221 | "status", ret_val); |
| 222 | tst_resm(TFAIL, "Expected 3 " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 223 | "got %d ", WEXITSTATUS(status)); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 224 | flag = FAILED; |
| 225 | } |
| 226 | } |
| 227 | wait_kid_pid[kid_count++] = ret_val; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Check that for every entry in the fork_kid_pid array, |
| 232 | * there is a matching pid in the wait_kid_pid array. If |
| 233 | * not, it's an error. |
| 234 | */ |
| 235 | for (i = 0; i < kid_count; i++) { |
| 236 | found = 0; |
| 237 | for (j = 0; j < MAXKIDS; j++) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | if (fork_kid_pid[j] == wait_kid_pid[i]) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 239 | found = 1; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (!found) { |
| 245 | tst_resm(TFAIL, "Did not find a " |
| 246 | "wait_kid_pid for the " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 247 | "fork_kid_pid of %d", wait_kid_pid[i]); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 248 | for (k = 0; k < MAXKIDS; k++) { |
| 249 | tst_resm(TFAIL, |
| 250 | "fork_kid_pid[%d] = " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | "%d", k, fork_kid_pid[k]); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 252 | } |
| 253 | for (k = 0; k < kid_count; k++) { |
| 254 | tst_resm(TFAIL, |
| 255 | "wait_kid_pid[%d] = " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 256 | "%d", k, wait_kid_pid[k]); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 257 | } |
| 258 | flag = FAILED; |
| 259 | } |
| 260 | } |
| 261 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 262 | if (flag) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 263 | exit(1); |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 264 | else |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 265 | exit(0); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | #ifdef UCLINUX |
| 269 | /* |
| 270 | * do_child_2_uclinux() |
| 271 | * sets up sigint handler again, then calls the normal child 2 function |
| 272 | */ |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 273 | static void do_child_2_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 274 | { |
| 275 | setup_sigint(); |
| 276 | do_exit(); |
| 277 | } |
| 278 | #endif |
| 279 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 280 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 281 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 282 | TEST_PAUSE; |
| 283 | } |
| 284 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 285 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | { |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 287 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 288 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 289 | static void inthandlr(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 290 | { |
| 291 | intintr++; |
| 292 | } |
| 293 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 294 | static void wait_for_parent(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 295 | { |
| 296 | int testvar; |
| 297 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 298 | while (!intintr) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 299 | testvar = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Wanlong Gao | dd62135 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 302 | static void do_exit(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 303 | { |
| 304 | wait_for_parent(); |
| 305 | exit(3); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 306 | } |