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 |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 22 | * waitpid09.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check ability of parent to wait until child returns, and that the |
| 26 | * child's process id is returned through the waitpid. Check that |
| 27 | * waitpid returns immediately if no child is present. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 28 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * ALGORITHM |
| 30 | * case 0: |
| 31 | * Parent forks a child and waits. Parent should do nothing |
| 32 | * further until child returns. The pid of the forked child |
| 33 | * should match the returned value from the waitpid. |
| 34 | * |
| 35 | * case 1: |
| 36 | * Parent calls a waitpid with no children waiting. Waitpid |
| 37 | * should return a -1 since there are no children to wait for. |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 38 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | * USAGE: <for command-line> |
| 40 | * waitpid09 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 41 | * where, -c n : Run n copies concurrently. |
| 42 | * -e : Turn on errno logging. |
| 43 | * -i n : Execute test n times. |
| 44 | * -I x : Execute test for x seconds. |
| 45 | * -P x : Pause for x seconds between iterations. |
| 46 | * -t : Turn on syscall timing. |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 47 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * History |
| 49 | * 07/2001 John George |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 50 | * -Ported |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 51 | * 04/2002 wjhuie sigset cleanups |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | * |
| 53 | * Restrictions |
| 54 | * None |
| 55 | */ |
| 56 | |
robbiew | 5832f9b | 2002-08-12 14:57:23 +0000 | [diff] [blame] | 57 | #define _GNU_SOURCE 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | #include <sys/types.h> |
| 59 | #include <signal.h> |
| 60 | #include <errno.h> |
| 61 | #include <sys/wait.h> |
robbiew | 5832f9b | 2002-08-12 14:57:23 +0000 | [diff] [blame] | 62 | #include <stdlib.h> |
| 63 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
| 66 | char *TCID = "waitpid09"; |
| 67 | int TST_TOTAL = 1; |
robbiew | 3d5c735 | 2003-01-13 17:49:51 +0000 | [diff] [blame] | 68 | volatile int intintr; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 70 | static void setup(void); |
| 71 | static void cleanup(void); |
| 72 | static void inthandlr(); |
| 73 | static void do_exit(void); |
| 74 | static void setup_sigint(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 75 | #ifdef UCLINUX |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 76 | static void do_exit_uclinux(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 77 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
plars | ac85d1c | 2002-06-10 14:52:37 +0000 | [diff] [blame] | 79 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 81 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 82 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | int fail, pid, status, ret; |
| 85 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 86 | msg = parse_opts(argc, argv, NULL, NULL); |
| 87 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 89 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 90 | #ifdef UCLINUX |
| 91 | maybe_run_child(&do_exit_uclinux, ""); |
| 92 | #endif |
| 93 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | setup(); |
| 95 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 96 | pid = FORK_OR_VFORK(); |
| 97 | if (pid < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | tst_brkm(TFAIL, cleanup, "Fork Failed"); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 99 | } else if (pid == 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | /* |
| 101 | * Child: |
| 102 | * Set up to catch SIGINT. The kids will wait till a |
| 103 | * SIGINT has been received before they proceed. |
| 104 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 105 | setup_sigint(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | /* check for looping state if -i option is given */ |
| 108 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 109 | /* reset tst_count in case we are looping */ |
| 110 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | intintr = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | fail = 0; |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 115 | pid = FORK_OR_VFORK(); |
| 116 | if (pid < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | tst_brkm(TFAIL, cleanup, "Fork failed."); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 118 | } else if (pid == 0) { /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 119 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | if (self_exec(argv[0], "") < 0) { |
| 121 | tst_brkm(TFAIL, cleanup, |
| 122 | "self_exec failed"); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 123 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 124 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | do_exit(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 126 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | } else { /* parent */ |
| 128 | |
| 129 | /* |
| 130 | *Check that waitpid with WNOHANG returns zero |
| 131 | */ |
| 132 | while (((ret = waitpid(pid, &status, WNOHANG)) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | != 0) || (errno == EINTR)) { |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 134 | if (ret == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | continue; |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 136 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | tst_resm(TFAIL, "return value for " |
| 138 | "WNOHANG expected 0 got %d", |
| 139 | ret); |
| 140 | fail = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 142 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | /* Give the kids a chance to setup SIGINT again, since |
| 144 | * this is cleared by exec(). |
| 145 | */ |
| 146 | sleep(3); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 147 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | /* send SIGINT to child to tell it to proceed */ |
| 150 | if (kill(pid, SIGINT) < 0) { |
| 151 | tst_resm(TFAIL, "Kill of child failed, " |
| 152 | "errno = %d", errno); |
| 153 | fail = 1; |
| 154 | } |
| 155 | |
| 156 | while (((ret = waitpid(pid, &status, 0)) != -1) |
| 157 | || (errno == EINTR)) { |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 158 | if (ret == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | continue; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | |
| 161 | if (ret != pid) { |
| 162 | tst_resm(TFAIL, "Expected %d " |
| 163 | "got %d as proc id of " |
| 164 | "child", pid, ret); |
| 165 | fail = 1; |
| 166 | } |
| 167 | |
| 168 | if (status != 0) { |
| 169 | tst_resm(TFAIL, "status value " |
| 170 | "got %d expected 0", |
| 171 | status); |
| 172 | fail = 1; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 177 | pid = FORK_OR_VFORK(); |
| 178 | if (pid < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | tst_brkm(TFAIL, cleanup, "Second fork failed."); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 180 | } else if (pid == 0) { /* child */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | exit(0); |
| 182 | } else { /* parent */ |
| 183 | /* Give the child time to startup and exit */ |
| 184 | sleep(2); |
| 185 | |
| 186 | while (((ret = waitpid(pid, &status, WNOHANG)) |
| 187 | != -1) || (errno == EINTR)) { |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 188 | if (ret == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | continue; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | |
| 191 | if (ret != pid) { |
| 192 | tst_resm(TFAIL, "proc id %d " |
| 193 | "and retval %d do not " |
| 194 | "match", pid, ret); |
| 195 | fail = 1; |
| 196 | } |
| 197 | |
| 198 | if (status != 0) { |
| 199 | tst_resm(TFAIL, "non zero " |
| 200 | "status received %d", |
| 201 | status); |
| 202 | fail = 1; |
| 203 | } |
| 204 | } |
| 205 | } |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 206 | |
| 207 | if (fail) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 208 | tst_resm(TFAIL, "case 1 FAILED"); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 209 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | tst_resm(TPASS, "case 1 PASSED"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 211 | |
| 212 | fail = 0; |
| 213 | ret = waitpid(pid, &status, 0); |
| 214 | |
| 215 | if (ret != -1) { |
| 216 | tst_resm(TFAIL, "Expected -1 got %d", ret); |
| 217 | fail = 1; |
| 218 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | if (errno != ECHILD) { |
| 220 | tst_resm(TFAIL, "Expected ECHILD got %d", |
| 221 | errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | fail = 1; |
| 223 | } |
| 224 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | ret = waitpid(pid, &status, WNOHANG); |
| 226 | if (ret != -1) { |
| 227 | tst_resm(TFAIL, "WNOHANG: Expected -1 got %d", |
| 228 | ret); |
| 229 | fail = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 231 | if (errno != ECHILD) { |
| 232 | tst_resm(TFAIL, "WNOHANG: Expected ECHILD got " |
| 233 | "%d", errno); |
| 234 | fail = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 236 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 237 | if (fail) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | tst_resm(TFAIL, "case 2 FAILED"); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 239 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 240 | tst_resm(TPASS, "case 2 PASSED"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | } |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 242 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 243 | cleanup(); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 244 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | /* wait for the child to return */ |
| 246 | waitpid(pid, &status, 0); |
| 247 | if (WEXITSTATUS(status) != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 248 | tst_brkm(TBROK, cleanup, "child returned bad " |
| 249 | "status"); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 250 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | } |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 252 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 253 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 257 | * setup_sigint() |
| 258 | * sets up a SIGINT handler |
| 259 | */ |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 260 | static void setup_sigint(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 261 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 262 | if ((sig_t) signal(SIGINT, inthandlr) == SIG_ERR) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 263 | tst_brkm(TFAIL, cleanup, "signal SIGINT failed, errno = %d", |
| 264 | errno); |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 265 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 268 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 269 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 270 | TEST_PAUSE; |
| 271 | } |
| 272 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 273 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 274 | { |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 275 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 276 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 277 | static void inthandlr(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 278 | { |
| 279 | intintr++; |
| 280 | } |
| 281 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 282 | static void wait_for_parent(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 283 | { |
| 284 | int testvar; |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 285 | while (!intintr) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | testvar = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 289 | static void do_exit(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 290 | { |
| 291 | wait_for_parent(); |
| 292 | exit(0); |
| 293 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 294 | |
| 295 | #ifdef UCLINUX |
| 296 | /* |
| 297 | * do_exit_uclinux() |
| 298 | * Sets up SIGINT handler again, then calls do_exit |
| 299 | */ |
Wanlong Gao | 4aeecee | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 300 | static void do_exit_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 301 | { |
| 302 | setup_sigint(); |
| 303 | do_exit(); |
| 304 | } |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 305 | #endif |