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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * wait401.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * wait401 - check that a call to wait4() correctly waits for a child |
| 26 | * process to exit |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * loop if that option was specified |
| 30 | * fork a child. |
| 31 | * issue the system call |
| 32 | * check the return value |
| 33 | * if return value == -1 |
| 34 | * issue a FAIL message, break remaining tests and cleanup |
| 35 | * if we are doing functional testing |
| 36 | * issue a PASS message if the wait4 call returned the child's pid |
| 37 | * else |
| 38 | * issue a FAIL message |
| 39 | * call cleanup |
| 40 | * |
| 41 | * USAGE: <for command-line> |
| 42 | * wait401 [-c n] [-f] [-e] [-i n] [-I x] [-P x] [-t] |
| 43 | * where, -c n : Run n copies concurrently. |
| 44 | * -f : Turn off functionality Testing. |
| 45 | * -i n : Execute test n times. |
| 46 | * -I x : Execute test for x seconds. |
| 47 | * -P x : Pause for x seconds between iterations. |
| 48 | * -t : Turn on syscall timing. |
| 49 | * |
| 50 | * History |
| 51 | * 07/2001 John George |
| 52 | * -Ported |
| 53 | * |
| 54 | * Restrictions |
| 55 | * none |
| 56 | */ |
| 57 | |
| 58 | #include "test.h" |
| 59 | #include "usctest.h" |
| 60 | |
| 61 | #include <errno.h> |
| 62 | #define _USE_BSD |
| 63 | #include <sys/types.h> |
| 64 | #include <sys/resource.h> |
| 65 | #include <sys/wait.h> |
| 66 | |
| 67 | void cleanup(void); |
| 68 | void setup(void); |
| 69 | |
plars | 48049f3 | 2003-07-05 03:12:42 +0000 | [diff] [blame] | 70 | char *TCID= "wait401"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | int TST_TOTAL = 1; |
| 72 | extern int Tst_count; |
| 73 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 74 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | { |
| 76 | int lc; /* loop counter */ |
| 77 | char *msg; /* message returned from parse_opts */ |
| 78 | pid_t pid; |
| 79 | int status = 1; |
| 80 | struct rusage *rusage = NULL; |
| 81 | |
| 82 | /* parse standard options */ |
| 83 | if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *) NULL) { |
| 84 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
| 85 | } |
| 86 | |
| 87 | setup(); /* global setup */ |
| 88 | |
| 89 | /* The following loop checks looping state if -i option given */ |
| 90 | |
| 91 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 92 | /* reset Tst_count in case we are looping */ |
| 93 | Tst_count = 0; |
| 94 | |
| 95 | /* |
| 96 | * Allocate some space for the rusage structure |
| 97 | */ |
| 98 | |
| 99 | if ((rusage = (struct rusage *)malloc(sizeof(struct rusage))) |
| 100 | == NULL) { |
| 101 | tst_brkm(TBROK, cleanup, "malloc() failed"); |
| 102 | } |
| 103 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame^] | 104 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
| 106 | if (pid == -1) { |
| 107 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 108 | } |
| 109 | |
| 110 | if (pid == 0) { /* this is the child */ |
| 111 | /* |
| 112 | * sleep for a moment to let us do the test |
| 113 | */ |
| 114 | sleep(1); |
| 115 | exit(0); |
| 116 | } else { /* this is the parent */ |
| 117 | |
| 118 | /* call wait4 with the TEST() macro */ |
| 119 | TEST(wait4(pid, &status, 0, rusage)); |
| 120 | } |
| 121 | |
| 122 | if (TEST_RETURN == -1) { |
| 123 | tst_brkm(TFAIL, cleanup, "%s call failed - errno = %d " |
| 124 | ": %s", TCID, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 125 | } |
| 126 | |
| 127 | if (STD_FUNCTIONAL_TEST) { |
| 128 | /* |
| 129 | * The return from this call should be non-zero. |
| 130 | */ |
| 131 | if (WIFEXITED(status) == 0) { |
| 132 | tst_brkm(TFAIL, cleanup, "%s call succeeded but " |
| 133 | "WIFEXITED() did not return expected value " |
| 134 | "- %d", TCID, WIFEXITED(status)); |
| 135 | } else if (TEST_RETURN != pid) { |
| 136 | tst_resm(TFAIL, "%s did not return the " |
| 137 | "expected value. %d", TCID, |
| 138 | TEST_RETURN); |
| 139 | } else { |
| 140 | |
| 141 | tst_resm(TPASS, "Received child pid as expected."); |
| 142 | } |
| 143 | } |
| 144 | tst_resm(TPASS, "%s call succeeded", TCID); |
| 145 | |
| 146 | /* |
| 147 | * Clean up things in case we are looping. |
| 148 | */ |
| 149 | free(rusage); |
| 150 | rusage = NULL; |
| 151 | } |
| 152 | |
| 153 | cleanup(); |
| 154 | |
| 155 | /*NOTREACHED*/ |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 156 | |
| 157 | return(0); |
| 158 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * setup() - performs all the ONE TIME setup for this test. |
| 163 | */ |
| 164 | void |
| 165 | setup(void) |
| 166 | { |
| 167 | /* capture signals */ |
| 168 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 169 | |
| 170 | /* Pause if that option was specified */ |
| 171 | TEST_PAUSE; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 176 | * or premature exit. |
| 177 | */ |
| 178 | void |
| 179 | cleanup(void) |
| 180 | { |
| 181 | /* |
| 182 | * print timing stats if that option was specified. |
| 183 | * print errno log if that option was specified. |
| 184 | */ |
| 185 | TEST_CLEANUP; |
| 186 | |
| 187 | /* exit with return code appropriate for results */ |
| 188 | tst_exit(); |
| 189 | } |
| 190 | |