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 | * waitpid03.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check that parent waits unitl specific child has returned. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Parent forks numerous (22 = MAXUPRC - 3) children, and starts waits : |
| 29 | * Should only wait for the specific child, a second wait on the same |
| 30 | * child should return with -1 and not one of the other zombied |
| 31 | * children. |
| 32 | * |
| 33 | * USAGE: <for command-line> |
| 34 | * waitpid03 [-c n] [-i n] [-I x] [-P x] [-t] |
| 35 | * where, -c n : Run n copies concurrently. |
| 36 | * -i n : Execute test n times. |
| 37 | * -I x : Execute test for x seconds. |
| 38 | * -P x : Pause for x seconds between iterations. |
| 39 | * -t : Turn on syscall timing. |
| 40 | * |
| 41 | * History |
| 42 | * 07/2001 John George |
| 43 | * -Ported |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 44 | * 04/2002 wjhuie sigset cleanups |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * |
| 46 | * Restrictions |
| 47 | * None |
| 48 | */ |
| 49 | |
plars | b3c1c25 | 2003-07-08 18:57:47 +0000 | [diff] [blame] | 50 | #define DEBUG 0 |
| 51 | |
plars | e24e78e | 2002-06-10 12:19:25 +0000 | [diff] [blame] | 52 | #include <sys/types.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | #include <signal.h> |
| 54 | #include <errno.h> |
plars | e24e78e | 2002-06-10 12:19:25 +0000 | [diff] [blame] | 55 | #include <sys/wait.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 56 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 58 | static void do_child(int); |
| 59 | static void setup(void); |
| 60 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | char *TCID = "waitpid03"; |
| 63 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | #define MAXUPRC 25 |
| 66 | |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 67 | static int condition_number; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 69 | #ifdef UCLINUX |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 70 | static void do_child_uclinux(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 71 | static int ikids_uclinux; |
| 72 | #endif |
| 73 | |
plars | e24e78e | 2002-06-10 12:19:25 +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 | |
| 79 | int status, pid[25], ret; |
| 80 | |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 81 | msg = parse_opts(argc, argv, NULL, NULL); |
| 82 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 84 | #ifdef UCLINUX |
| 85 | maybe_run_child(&do_child, "d", &ikids_uclinux); |
| 86 | #endif |
| 87 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | setup(); |
| 89 | |
| 90 | /* check for looping state if -i option is given */ |
| 91 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 92 | /* reset tst_count in case we are looping */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | int ikids = 0; |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 94 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Set SIGTERM to SIG_DFL as test driver sets up to ignore |
| 98 | * SIGTERM |
| 99 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | if ((sig_t) signal(SIGTERM, SIG_DFL) == SIG_ERR) { |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 101 | tst_resm(TFAIL, "Signal SIGTERM failed, errno = %d", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | errno); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | while (++ikids < MAXUPRC) { |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 107 | pid[ikids] = FORK_OR_VFORK(); |
| 108 | if (pid[ikids] > 0) { |
plars | b3c1c25 | 2003-07-08 18:57:47 +0000 | [diff] [blame] | 109 | if (DEBUG) |
| 110 | tst_resm(TINFO, "child # %d", ikids); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | } else if (pid[ikids] == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | tst_resm(TFAIL, "cannot open fork #%d", ikids); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } else { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 114 | #ifdef UCLINUX |
| 115 | if (self_exec(argv[0], "d", ikids) < 0) { |
| 116 | tst_resm(TFAIL, "cannot self_exec #%d", |
| 117 | ikids); |
| 118 | } |
| 119 | #else |
| 120 | do_child(ikids); |
| 121 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | for (ikids = 1; ikids < MAXUPRC; ikids++) { |
plars | b3c1c25 | 2003-07-08 18:57:47 +0000 | [diff] [blame] | 126 | if (DEBUG) |
| 127 | tst_resm(TINFO, "Killing #%d", ikids); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | kill(pid[ikids], 15); |
| 129 | } |
| 130 | |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 131 | condition_number = 1; |
| 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | /* Wait on one specific child */ |
plars | b3c1c25 | 2003-07-08 18:57:47 +0000 | [diff] [blame] | 134 | if (DEBUG) |
| 135 | tst_resm(TINFO, "Waiting for child:#%d", MAXUPRC / 2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | ret = waitpid(pid[MAXUPRC / 2], &status, 0); |
| 137 | if (ret != pid[MAXUPRC / 2]) { |
| 138 | tst_resm(TFAIL, "condition %d test failed. " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | "waitpid(%d) returned %d.", |
| 140 | condition_number, pid[MAXUPRC / 2], ret); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } else { |
| 142 | tst_resm(TPASS, "Got correct child PID"); |
| 143 | } |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 144 | condition_number++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * Child has already been waited on, waitpid should return |
| 148 | * -1 |
| 149 | */ |
| 150 | ret = waitpid(pid[MAXUPRC / 2], &status, 0); |
| 151 | if (ret != -1) { |
| 152 | tst_resm(TFAIL, "condition %d test failed", |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 153 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } else { |
| 155 | tst_resm(TPASS, "Condition %d test passed", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 158 | condition_number++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | } |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 162 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 165 | static void do_child(int ikids) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 166 | { |
| 167 | if (DEBUG) |
| 168 | tst_resm(TINFO, "child:%d", ikids); |
| 169 | pause(); |
| 170 | exit(0); |
| 171 | } |
| 172 | |
| 173 | #ifdef UCLINUX |
| 174 | /* |
| 175 | * do_child_uclinux() |
| 176 | * run do_child with the appropriate ikids variable |
| 177 | */ |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 178 | static void do_child_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 179 | { |
| 180 | do_child(ikids_uclinux); |
| 181 | } |
| 182 | #endif |
| 183 | |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 184 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | TEST_PAUSE; |
| 187 | } |
| 188 | |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 189 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | { |
Wanlong Gao | d08183c | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 191 | } |