blob: dced6fadc3faaf80c9fb1fef3157500774b24fbe [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
Cyril Hrubis8cc41652017-01-02 16:16:11 +010041#define _XOPEN_SOURCE 500
subrata_modak96cf35e2009-06-15 18:40:53 +000042#include <stdio.h>
43#include <errno.h>
44#include <stdlib.h>
45#include <sys/wait.h>
46#include <sys/types.h>
47#include <unistd.h>
48#include <sys/stat.h>
49
subrata_modak96cf35e2009-06-15 18:40:53 +000050#include "test.h"
Cyril Hrubis66374d52017-10-03 13:46:52 +020051#include "safe_macros.h"
Richard Palethorpe817d8092017-07-24 14:48:25 +020052#include "lapi/syscalls.h"
subrata_modak96cf35e2009-06-15 18:40:53 +000053
Jan Stancek9187c0a2013-05-16 12:09:34 +020054struct testcase_t {
55 const char *msg;
56 idtype_t idtype;
57 id_t id;
58 pid_t child;
59 int options;
60 int exp_ret;
61 int exp_errno;
62 void (*setup) (struct testcase_t *);
63 void (*cleanup) (struct testcase_t *);
64};
65
66static void setup(void);
67static void cleanup(void);
68
69static void setup2(struct testcase_t *);
70static void setup3(struct testcase_t *);
71static void setup4(struct testcase_t *);
72static void setup5(struct testcase_t *);
73static void setup6(struct testcase_t *);
74static void cleanup2(struct testcase_t *);
75static void cleanup5(struct testcase_t *);
76static void cleanup6(struct testcase_t *);
77
78struct testcase_t tdat[] = {
79 {
80 .msg = "WNOHANG",
81 .idtype = P_ALL,
82 .id = 0,
83 .options = WNOHANG,
84 .exp_ret = -1,
85 .exp_errno = EINVAL,
86 },
87 {
88 .msg = "WNOHANG | WEXITED no child",
89 .idtype = P_ALL,
90 .id = 0,
91 .options = WNOHANG | WEXITED,
92 .exp_ret = -1,
93 .exp_errno = ECHILD,
94 },
95 {
96 .msg = "WNOHANG | WEXITED with child",
97 .idtype = P_ALL,
98 .id = 0,
99 .options = WNOHANG | WEXITED,
100 .exp_ret = 0,
101 .setup = setup2,
102 .cleanup = cleanup2
103 },
104 {
105 .msg = "P_PGID, WEXITED wait for child",
106 .idtype = P_PGID,
107 .options = WEXITED,
108 .exp_ret = 0,
109 .setup = setup3,
110 },
111 {
112 .msg = "P_PID, WEXITED wait for child",
113 .idtype = P_PID,
114 .options = WEXITED,
115 .exp_ret = 0,
116 .setup = setup4,
117 },
118 {
119 .msg = "P_PID, WSTOPPED | WNOWAIT",
120 .idtype = P_PID,
121 .options = WSTOPPED | WNOWAIT,
122 .exp_ret = 0,
123 .setup = setup5,
124 .cleanup = cleanup5
125 },
126 {
127 .msg = "P_PID, WCONTINUED",
128 .idtype = P_PID,
129 .options = WCONTINUED,
130 .exp_ret = 0,
131 .setup = setup6,
132 .cleanup = cleanup6
133 },
Zeng Linggang18f3fb52014-06-30 16:18:17 +0800134 {
135 .msg = "P_PID, WEXITED not a child of the calling process",
136 .idtype = P_PID,
137 .id = 1,
138 .options = WEXITED,
139 .exp_ret = -1,
140 .exp_errno = ECHILD,
141 .setup = setup2,
142 .cleanup = cleanup2
143 },
Jan Stancek9187c0a2013-05-16 12:09:34 +0200144
145};
146
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200147char *TCID = "waitid02";
Jan Stancek9187c0a2013-05-16 12:09:34 +0200148static int TST_TOTAL = ARRAY_SIZE(tdat);
subrata_modak96cf35e2009-06-15 18:40:53 +0000149
Jan Stancek9187c0a2013-05-16 12:09:34 +0200150static void makechild(struct testcase_t *t, void (*childfn)(void))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151{
Jan Stancek9187c0a2013-05-16 12:09:34 +0200152 t->child = fork();
153 switch (t->child) {
154 case -1:
155 tst_brkm(TBROK | TERRNO, cleanup, "fork");
156 break;
157 case 0:
158 childfn();
159 exit(0);
160 }
161}
subrata_modak96cf35e2009-06-15 18:40:53 +0000162
Jan Stancek9187c0a2013-05-16 12:09:34 +0200163static void wait4child(pid_t pid)
164{
165 int status;
Cyril Hrubis66374d52017-10-03 13:46:52 +0200166 SAFE_WAITPID(cleanup, pid, &status, 0);
Jan Stancek9187c0a2013-05-16 12:09:34 +0200167 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{
Cyril Hrubis9f136a42015-02-17 17:16:09 +0100177 TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
Jan Stancek9187c0a2013-05-16 12:09:34 +0200178}
179
180static void stopped_child(void)
181{
182 kill(getpid(), SIGSTOP);
Cyril Hrubis9f136a42015-02-17 17:16:09 +0100183 TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
Jan Stancek9187c0a2013-05-16 12:09:34 +0200184}
185
186static void setup2(struct testcase_t *t)
187{
188 makechild(t, waiting_child);
189}
190
191static void cleanup2(struct testcase_t *t)
192{
Cyril Hrubis9f136a42015-02-17 17:16:09 +0100193 TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
Jan Stancek9187c0a2013-05-16 12:09:34 +0200194 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);
Cyril Hrubis9f136a42015-02-17 17:16:09 +0100218 TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
Jan Stancek9187c0a2013-05-16 12:09:34 +0200219 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{
Cyril Hrubis9f136a42015-02-17 17:16:09 +0100234 TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
Jan Stancek9187c0a2013-05-16 12:09:34 +0200235 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();
Cyril Hrubis9f136a42015-02-17 17:16:09 +0100242 TST_CHECKPOINT_INIT(tst_rmdir);
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;
Garrett Cooper2c282152010-12-16 00:55:50 -0800289
Cyril Hrubisd6d11d02015-03-09 17:35:43 +0100290 tst_parse_opts(ac, av, NULL, NULL);
subrata_modak96cf35e2009-06-15 18:40:53 +0000291
subrata_modak808e5f72009-08-07 09:38:47 +0000292 setup();
subrata_modak808e5f72009-08-07 09:38:47 +0000293 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Zeng Linggang18f3fb52014-06-30 16:18:17 +0800294 tst_count = 0;
Jan Stancek9187c0a2013-05-16 12:09:34 +0200295 for (testno = 0; testno < TST_TOTAL; testno++)
296 test_waitid(&tdat[testno]);
subrata_modak808e5f72009-08-07 09:38:47 +0000297 }
298 cleanup();
subrata_modak96cf35e2009-06-15 18:40:53 +0000299 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700300}