blob: 5995cabe88124e2a4df44efeee2b6b21dbbcfbf2 [file] [log] [blame]
subrata_modak96cf35e2009-06-15 18:40:53 +00001/******************************************************************************/
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 Gao4548c6c2012-10-19 18:03:36 +080016/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
subrata_modak96cf35e2009-06-15 18:40:53 +000017/* */
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_modak96cf35e2009-06-15 18:40:53 +000055
56/* Global Variables */
Wanlong Gao354ebb42012-12-07 10:10:04 +080057char *TCID = "waitid02"; /* Test program identifier. */
58int testno;
59int TST_TOTAL = 4; /* total number of tests in this file. */
subrata_modak96cf35e2009-06-15 18:40:53 +000060
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 Gao354ebb42012-12-07 10:10:04 +080079extern void cleanup()
80{
Garrett Cooper2c282152010-12-16 00:55:50 -080081
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 TEST_CLEANUP;
83 tst_rmdir();
subrata_modak96cf35e2009-06-15 18:40:53 +000084
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 tst_exit();
subrata_modak96cf35e2009-06-15 18:40:53 +000086}
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 Gao354ebb42012-12-07 10:10:04 +0800106void setup()
107{
subrata_modak808e5f72009-08-07 09:38:47 +0000108 /* Capture signals if any */
109 /* Create temporary directories */
110 TEST_PAUSE;
111 tst_tmpdir();
subrata_modak96cf35e2009-06-15 18:40:53 +0000112}
113
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114int main(int ac, char **av)
115{
subrata_modak808e5f72009-08-07 09:38:47 +0000116 id_t pgid;
117 id_t id1, id2, id3;
118 siginfo_t infop;
119 int i = 0;
subrata_modak96cf35e2009-06-15 18:40:53 +0000120
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200121 int lc;
122 char *msg;
Garrett Cooper2c282152010-12-16 00:55:50 -0800123
Garrett Cooper45e285d2010-11-22 12:19:25 -0800124 msg = parse_opts(ac, av, NULL, NULL);
125 if (msg != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800126 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak808e5f72009-08-07 09:38:47 +0000127 tst_exit();
128 }
subrata_modak96cf35e2009-06-15 18:40:53 +0000129
subrata_modak808e5f72009-08-07 09:38:47 +0000130 setup();
subrata_modak96cf35e2009-06-15 18:40:53 +0000131
subrata_modak808e5f72009-08-07 09:38:47 +0000132 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800133 tst_count = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 for (testno = 0; testno < TST_TOTAL; ++testno) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800135
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 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_modak96cf35e2009-06-15 18:40:53 +0000144
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 /* option == WEXITED | WCONTINUED | WSTOPPED | WNOHANG | WNOWAIT */
subrata_modak96cf35e2009-06-15 18:40:53 +0000146
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 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_modak808e5f72009-08-07 09:38:47 +0000155
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 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_modak96cf35e2009-06-15 18:40:53 +0000164
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 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_modak96cf35e2009-06-15 18:40:53 +0000174
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 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_modak96cf35e2009-06-15 18:40:53 +0000184
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 tst_resm(TINFO, "I'm a Parent,my id is %d,gpid is %d",
186 getpid(), pgid = __getpgid(0));
subrata_modak808e5f72009-08-07 09:38:47 +0000187
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 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_modak96cf35e2009-06-15 18:40:53 +0000201
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202 TEST(kill(id2, SIGSTOP));
subrata_modak96cf35e2009-06-15 18:40:53 +0000203
Wanlong Gao354ebb42012-12-07 10:10:04 +0800204 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_modak808e5f72009-08-07 09:38:47 +0000218
Wanlong Gao354ebb42012-12-07 10:10:04 +0800219 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_modak808e5f72009-08-07 09:38:47 +0000233
Wanlong Gao354ebb42012-12-07 10:10:04 +0800234 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_modak808e5f72009-08-07 09:38:47 +0000247
Wanlong Gao354ebb42012-12-07 10:10:04 +0800248 sleep(3);
249 }
subrata_modak808e5f72009-08-07 09:38:47 +0000250 }
251 cleanup();
subrata_modak96cf35e2009-06-15 18:40:53 +0000252 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700253}