blob: 5f6917cf07defd9375f38e6a341a50eaac6d29f5 [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/* */
Jan Stancek2990b7c2013-05-16 12:07:56 +020014/* You should have received a copy of the GNU General Public License along */
15/* with this program; if not, write to the Free Software Foundation, Inc., */
16/* 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
subrata_modak96cf35e2009-06-15 18:40:53 +000049#include "test.h"
subrata_modak96cf35e2009-06-15 18:40:53 +000050#include "linux_syscall_numbers.h"
51
Jan Stancek9187c0a2013-05-16 12:09:34 +020052struct testcase_t {
53 const char *msg;
54 idtype_t idtype;
55 id_t id;
56 pid_t child;
57 int options;
58 int exp_ret;
59 int exp_errno;
60 void (*setup) (struct testcase_t *);
61 void (*cleanup) (struct testcase_t *);
62};
63
64static void setup(void);
65static void cleanup(void);
66
67static void setup2(struct testcase_t *);
68static void setup3(struct testcase_t *);
69static void setup4(struct testcase_t *);
70static void setup5(struct testcase_t *);
71static void setup6(struct testcase_t *);
72static void cleanup2(struct testcase_t *);
73static void cleanup5(struct testcase_t *);
74static void cleanup6(struct testcase_t *);
75
76struct testcase_t tdat[] = {
77 {
78 .msg = "WNOHANG",
79 .idtype = P_ALL,
80 .id = 0,
81 .options = WNOHANG,
82 .exp_ret = -1,
83 .exp_errno = EINVAL,
84 },
85 {
86 .msg = "WNOHANG | WEXITED no child",
87 .idtype = P_ALL,
88 .id = 0,
89 .options = WNOHANG | WEXITED,
90 .exp_ret = -1,
91 .exp_errno = ECHILD,
92 },
93 {
94 .msg = "WNOHANG | WEXITED with child",
95 .idtype = P_ALL,
96 .id = 0,
97 .options = WNOHANG | WEXITED,
98 .exp_ret = 0,
99 .setup = setup2,
100 .cleanup = cleanup2
101 },
102 {
103 .msg = "P_PGID, WEXITED wait for child",
104 .idtype = P_PGID,
105 .options = WEXITED,
106 .exp_ret = 0,
107 .setup = setup3,
108 },
109 {
110 .msg = "P_PID, WEXITED wait for child",
111 .idtype = P_PID,
112 .options = WEXITED,
113 .exp_ret = 0,
114 .setup = setup4,
115 },
116 {
117 .msg = "P_PID, WSTOPPED | WNOWAIT",
118 .idtype = P_PID,
119 .options = WSTOPPED | WNOWAIT,
120 .exp_ret = 0,
121 .setup = setup5,
122 .cleanup = cleanup5
123 },
124 {
125 .msg = "P_PID, WCONTINUED",
126 .idtype = P_PID,
127 .options = WCONTINUED,
128 .exp_ret = 0,
129 .setup = setup6,
130 .cleanup = cleanup6
131 },
Zeng Linggang18f3fb52014-06-30 16:18:17 +0800132 {
133 .msg = "P_PID, WEXITED not a child of the calling process",
134 .idtype = P_PID,
135 .id = 1,
136 .options = WEXITED,
137 .exp_ret = -1,
138 .exp_errno = ECHILD,
139 .setup = setup2,
140 .cleanup = cleanup2
141 },
Jan Stancek9187c0a2013-05-16 12:09:34 +0200142
143};
144
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200145char *TCID = "waitid02";
Jan Stancek9187c0a2013-05-16 12:09:34 +0200146static int TST_TOTAL = ARRAY_SIZE(tdat);
147static struct tst_checkpoint checkpoint;
subrata_modak96cf35e2009-06-15 18:40:53 +0000148
Jan Stancek9187c0a2013-05-16 12:09:34 +0200149static void makechild(struct testcase_t *t, void (*childfn)(void))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150{
Jan Stancek9187c0a2013-05-16 12:09:34 +0200151 t->child = fork();
152 switch (t->child) {
153 case -1:
154 tst_brkm(TBROK | TERRNO, cleanup, "fork");
155 break;
156 case 0:
157 childfn();
158 exit(0);
159 }
160}
subrata_modak96cf35e2009-06-15 18:40:53 +0000161
Jan Stancek9187c0a2013-05-16 12:09:34 +0200162static void wait4child(pid_t pid)
163{
164 int status;
165 if (waitpid(pid, &status, 0) == -1)
166 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
167 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
168 tst_resm(TFAIL, "child returns %d", status);
169}
170
171static void dummy_child(void)
172{
173}
174
175static void waiting_child(void)
176{
177 TST_CHECKPOINT_CHILD_WAIT(&checkpoint);
178}
179
180static void stopped_child(void)
181{
182 kill(getpid(), SIGSTOP);
183 TST_CHECKPOINT_CHILD_WAIT(&checkpoint);
184}
185
186static void setup2(struct testcase_t *t)
187{
188 makechild(t, waiting_child);
189}
190
191static void cleanup2(struct testcase_t *t)
192{
193 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint);
194 wait4child(t->child);
195}
196
197static void setup3(struct testcase_t *t)
198{
199 t->id = getpgid(0);
200 makechild(t, dummy_child);
201}
202
203static void setup4(struct testcase_t *t)
204{
205 makechild(t, dummy_child);
206 t->id = t->child;
207}
208
209static void setup5(struct testcase_t *t)
210{
211 makechild(t, stopped_child);
212 t->id = t->child;
213}
214
215static void cleanup5(struct testcase_t *t)
216{
217 kill(t->child, SIGCONT);
218 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint);
219 wait4child(t->child);
220}
221
222static void setup6(struct testcase_t *t)
223{
224 siginfo_t infop;
225 makechild(t, stopped_child);
226 t->id = t->child;
227 if (waitid(P_PID, t->child, &infop, WSTOPPED) != 0)
228 tst_brkm(TBROK | TERRNO, cleanup, "waitpid setup6");
229 kill(t->child, SIGCONT);
230}
231
232static void cleanup6(struct testcase_t *t)
233{
234 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint);
235 wait4child(t->child);
subrata_modak96cf35e2009-06-15 18:40:53 +0000236}
237
Jan Stancek2990b7c2013-05-16 12:07:56 +0200238static void setup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800239{
subrata_modak808e5f72009-08-07 09:38:47 +0000240 TEST_PAUSE;
241 tst_tmpdir();
Jan Stancek4caa6312014-10-06 14:57:56 +0200242 TST_CHECKPOINT_CREATE(&checkpoint);
Jan Stancek9187c0a2013-05-16 12:09:34 +0200243}
244
245static void cleanup(void)
246{
Jan Stancek9187c0a2013-05-16 12:09:34 +0200247 tst_rmdir();
248 tst_exit();
249}
250
251static void test_waitid(struct testcase_t *t)
252{
253 siginfo_t infop;
254
255 if (t->setup)
256 t->setup(t);
257
258 tst_resm(TINFO, "%s", t->msg);
259 tst_resm(TINFO, "(%d) waitid(%d, %d, %p, %d)", getpid(), t->idtype,
260 t->id, &infop, t->options);
261 memset(&infop, 0, sizeof(infop));
262
263 TEST(waitid(t->idtype, t->id, &infop, t->options));
264 if (TEST_RETURN == t->exp_ret) {
265 if (TEST_RETURN == -1) {
266 if (TEST_ERRNO == t->exp_errno)
267 tst_resm(TPASS, "exp_errno=%d", t->exp_errno);
268 else
269 tst_resm(TFAIL|TTERRNO, "exp_errno=%d",
270 t->exp_errno);
271 } else {
272 tst_resm(TPASS, "ret: %d", t->exp_ret);
273 }
274 } else {
275 tst_resm(TFAIL|TTERRNO, "ret=%ld expected=%d",
276 TEST_RETURN, t->exp_ret);
277 }
278 tst_resm(TINFO, "si_pid = %d ; si_code = %d ; si_status = %d",
279 infop.si_pid, infop.si_code,
280 infop.si_status);
281
282 if (t->cleanup)
283 t->cleanup(t);
subrata_modak96cf35e2009-06-15 18:40:53 +0000284}
285
Wanlong Gao354ebb42012-12-07 10:10:04 +0800286int main(int ac, char **av)
287{
Jan Stancek9187c0a2013-05-16 12:09:34 +0200288 int lc, testno;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200289 const char *msg;
Garrett Cooper2c282152010-12-16 00:55:50 -0800290
Garrett Cooper45e285d2010-11-22 12:19:25 -0800291 msg = parse_opts(ac, av, NULL, NULL);
Zeng Linggang18f3fb52014-06-30 16:18:17 +0800292 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800293 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak96cf35e2009-06-15 18:40:53 +0000294
subrata_modak808e5f72009-08-07 09:38:47 +0000295 setup();
subrata_modak808e5f72009-08-07 09:38:47 +0000296 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Zeng Linggang18f3fb52014-06-30 16:18:17 +0800297 tst_count = 0;
Jan Stancek9187c0a2013-05-16 12:09:34 +0200298 for (testno = 0; testno < TST_TOTAL; testno++)
299 test_waitid(&tdat[testno]);
subrata_modak808e5f72009-08-07 09:38:47 +0000300 }
301 cleanup();
subrata_modak96cf35e2009-06-15 18:40:53 +0000302 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700303}