subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
| 2 | /* Copyright (c) Crackerjack Project., 2007 */ |
| 3 | /* */ |
| 4 | /* This program is free software; you can redistribute it and/or modify */ |
| 5 | /* it under the terms of the GNU General Public License as published by */ |
| 6 | /* the Free Software Foundation; either version 2 of the License, or */ |
| 7 | /* (at your option) any later version. */ |
| 8 | /* */ |
| 9 | /* This program is distributed in the hope that it will be useful, */ |
| 10 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 11 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ |
| 12 | /* the GNU General Public License for more details. */ |
| 13 | /* */ |
| 14 | /* You should have received a copy of the GNU General Public License */ |
| 15 | /* along with this program; if not, write to the Free Software */ |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 17 | /* */ |
| 18 | /******************************************************************************/ |
| 19 | /******************************************************************************/ |
| 20 | /* */ |
| 21 | /* File: waitid02.c */ |
| 22 | /* */ |
| 23 | /* Description: This tests the waitid() syscall */ |
| 24 | /* */ |
| 25 | /* Usage: <for command-line> */ |
| 26 | /* waitid02 [-c n] [-e][-i n] [-I x] [-p x] [-t] */ |
| 27 | /* where, -c n : Run n copies concurrently. */ |
| 28 | /* -e : Turn on errno logging. */ |
| 29 | /* -i n : Execute test n times. */ |
| 30 | /* -I x : Execute test for x seconds. */ |
| 31 | /* -P x : Pause for x seconds between iterations. */ |
| 32 | /* -t : Turn on syscall timing. */ |
| 33 | /* */ |
| 34 | /* Total Tests: 1 */ |
| 35 | /* */ |
| 36 | /* Test Name: waitid02 */ |
| 37 | /* History: Porting from Crackerjack to LTP is done by */ |
| 38 | /* Manas Kumar Nayak maknayak@in.ibm.com> */ |
| 39 | /******************************************************************************/ |
| 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <errno.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <sys/wait.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <unistd.h> |
| 47 | #include <sys/stat.h> |
| 48 | |
| 49 | /* Harness Specific Include Files. */ |
| 50 | #include "test.h" |
| 51 | #include "usctest.h" |
| 52 | #include "linux_syscall_numbers.h" |
| 53 | |
| 54 | /* Extern Global Variables */ |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 55 | |
| 56 | /* Global Variables */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 57 | char *TCID = "waitid02"; /* Test program identifier. */ |
| 58 | int testno; |
| 59 | int TST_TOTAL = 4; /* total number of tests in this file. */ |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 60 | |
| 61 | /* Extern Global Functions */ |
| 62 | /******************************************************************************/ |
| 63 | /* */ |
| 64 | /* Function: cleanup */ |
| 65 | /* */ |
| 66 | /* Description: Performs all one time clean up for this test on successful */ |
| 67 | /* completion, premature exit or failure. Closes all temporary */ |
| 68 | /* files, removes all temporary directories exits the test with */ |
| 69 | /* appropriate return code by calling tst_exit() function. */ |
| 70 | /* */ |
| 71 | /* Input: None. */ |
| 72 | /* */ |
| 73 | /* Output: None. */ |
| 74 | /* */ |
| 75 | /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ |
| 76 | /* On success - Exits calling tst_exit(). With '0' return code. */ |
| 77 | /* */ |
| 78 | /******************************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | extern void cleanup() |
| 80 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 81 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | TEST_CLEANUP; |
| 83 | tst_rmdir(); |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 84 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | tst_exit(); |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* Local Functions */ |
| 89 | /******************************************************************************/ |
| 90 | /* */ |
| 91 | /* Function: setup */ |
| 92 | /* */ |
| 93 | /* Description: Performs all one time setup for this test. This function is */ |
| 94 | /* typically used to capture signals, create temporary dirs */ |
| 95 | /* and temporary files that may be used in the course of this */ |
| 96 | /* test. */ |
| 97 | /* */ |
| 98 | /* Input: None. */ |
| 99 | /* */ |
| 100 | /* Output: None. */ |
| 101 | /* */ |
| 102 | /* Return: On failure - Exits by calling cleanup(). */ |
| 103 | /* On success - returns 0. */ |
| 104 | /* */ |
| 105 | /******************************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | void setup() |
| 107 | { |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 108 | /* Capture signals if any */ |
| 109 | /* Create temporary directories */ |
| 110 | TEST_PAUSE; |
| 111 | tst_tmpdir(); |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | int main(int ac, char **av) |
| 115 | { |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 116 | id_t pgid; |
| 117 | id_t id1, id2, id3; |
| 118 | siginfo_t infop; |
| 119 | int i = 0; |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 120 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 121 | int lc; |
| 122 | char *msg; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 123 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 124 | msg = parse_opts(ac, av, NULL, NULL); |
| 125 | if (msg != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 126 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 127 | tst_exit(); |
| 128 | } |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 129 | |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 130 | setup(); |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 131 | |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 132 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 133 | tst_count = 0; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | TEST(waitid(P_ALL, 0, &infop, WNOHANG)); |
| 137 | if (TEST_RETURN == -1) |
| 138 | tst_resm(TPASS, |
| 139 | "Success1 ... -1 is returned. error is %d.", |
| 140 | TEST_ERRNO); |
| 141 | else { |
| 142 | tst_resm(TFAIL, "%s Failed1 ...", TCID); |
| 143 | } |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 144 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | /* option == WEXITED | WCONTINUED | WSTOPPED | WNOHANG | WNOWAIT */ |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 146 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | TEST(id1 = fork()); |
| 148 | if (TEST_RETURN == 0) { |
| 149 | tst_resm(TINFO, |
| 150 | "I'm a child 1,my id is %d,gpid is %d", |
| 151 | id1 = getpid(), __getpgid(0)); |
| 152 | sleep(1); |
| 153 | exit(5); |
| 154 | } |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 155 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 156 | TEST(id2 = fork()); |
| 157 | if (TEST_RETURN == 0) { |
| 158 | sleep(3); |
| 159 | tst_resm(TINFO, |
| 160 | "I'm a child 2,my id is %d,gpid is %d", |
| 161 | id2 = getpid(), __getpgid(0)); |
| 162 | exit(7); |
| 163 | } |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 164 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | TEST(id3 = fork()); |
| 166 | if (TEST_RETURN == 0) { |
| 167 | sleep(2); |
| 168 | TEST(kill(id2, SIGCONT)); |
| 169 | tst_resm(TINFO, |
| 170 | "I'm a child 3,my id is %d,gpid is %d", |
| 171 | id3 = getpid(), __getpgid(0)); |
| 172 | exit(6); |
| 173 | } |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 174 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | TEST(waitid(P_ALL, 0, &infop, WNOHANG | WEXITED)); |
| 176 | if (TEST_RETURN == 0) |
| 177 | tst_resm(TPASS, |
| 178 | "Success 2 ...0 is returned.. error is %d.", |
| 179 | TEST_ERRNO); |
| 180 | else { |
| 181 | tst_resm(TFAIL | TTERRNO, "%s Failed 2", TCID); |
| 182 | tst_exit(); |
| 183 | } |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 184 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | tst_resm(TINFO, "I'm a Parent,my id is %d,gpid is %d", |
| 186 | getpid(), pgid = __getpgid(0)); |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 187 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | TEST(waitid(P_PGID, pgid, &infop, WEXITED)); |
| 189 | if (TEST_RETURN == 0) { |
| 190 | tst_resm(TPASS, "Success3 ... 0 is returned."); |
| 191 | tst_resm(TINFO, |
| 192 | "si_pid = %d ; si_code = %d ; si_status = %d", |
| 193 | infop.si_pid, infop.si_code, |
| 194 | infop.si_status); |
| 195 | } else { |
| 196 | tst_resm(TFAIL | TTERRNO, |
| 197 | "Fail3 ... %ld is returned", |
| 198 | TEST_RETURN); |
| 199 | tst_exit(); |
| 200 | } |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 201 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 202 | TEST(kill(id2, SIGSTOP)); |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 203 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 204 | TEST(i = |
| 205 | waitid(P_PID, id2, &infop, WSTOPPED | WNOWAIT)); |
| 206 | if (TEST_RETURN == 0) { |
| 207 | /*EINVAL*/ |
| 208 | tst_resm(TINFO, |
| 209 | "si_pid = %d, si_code = %d, si_status = %d", |
| 210 | infop.si_pid, infop.si_code, |
| 211 | infop.si_status); |
| 212 | tst_resm(TPASS, "Success4 ... 0 is returned"); |
| 213 | } else { |
| 214 | tst_resm(TFAIL | TTERRNO, |
| 215 | "Fail4 ... %d is returned", i); |
| 216 | tst_exit(); |
| 217 | } |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 218 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 219 | TEST(waitid(P_PID, id3, &infop, WEXITED)); |
| 220 | if (TEST_RETURN == 0) { |
| 221 | /*NOCHILD*/ |
| 222 | tst_resm(TINFO, |
| 223 | "si_pid = %d, si_code = %d, si_status = %d", |
| 224 | infop.si_pid, infop.si_code, |
| 225 | infop.si_status); |
| 226 | tst_resm(TPASS, "Success5 ... 0 is returned"); |
| 227 | } else { |
| 228 | tst_resm(TFAIL | TTERRNO, |
| 229 | "Fail5 ... %ld is returned", |
| 230 | TEST_RETURN); |
| 231 | tst_exit(); |
| 232 | } |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 233 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 234 | TEST(i = waitid(P_PID, id2, &infop, WCONTINUED)); |
| 235 | if (TEST_RETURN == 0) { |
| 236 | /*EINVAL*/ |
| 237 | tst_resm(TINFO, |
| 238 | "si_pid = %d, si_code = %d, si_status = %d", |
| 239 | infop.si_pid, infop.si_code, |
| 240 | infop.si_status); |
| 241 | tst_resm(TPASS, "Success6 ... 0 is returned"); |
| 242 | } else { |
| 243 | tst_resm(TFAIL | TTERRNO, |
| 244 | "Fail6 ... %d is returned", i); |
| 245 | tst_exit(); |
| 246 | } |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 247 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 248 | sleep(3); |
| 249 | } |
subrata_modak | 808e5f7 | 2009-08-07 09:38:47 +0000 | [diff] [blame] | 250 | } |
| 251 | cleanup(); |
subrata_modak | 96cf35e | 2009-06-15 18:40:53 +0000 | [diff] [blame] | 252 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 253 | } |