1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker * case 0:
8*49cdfc7eSAndroid Build Coastguard Worker * waitpid(pid, WNOHANG) should return 0 if there is a running child
9*49cdfc7eSAndroid Build Coastguard Worker * case 1:
10*49cdfc7eSAndroid Build Coastguard Worker * waitpid(pid, WNOHANG) should return the pid of the child if
11*49cdfc7eSAndroid Build Coastguard Worker * the child has exited
12*49cdfc7eSAndroid Build Coastguard Worker * case 2:
13*49cdfc7eSAndroid Build Coastguard Worker * waitpid(-1, 0) should return -1 with ECHILD if
14*49cdfc7eSAndroid Build Coastguard Worker * there are no children to wait for.
15*49cdfc7eSAndroid Build Coastguard Worker * case 3:
16*49cdfc7eSAndroid Build Coastguard Worker * waitpid(-1, WNOHANG) should return -1 with ECHILD if
17*49cdfc7eSAndroid Build Coastguard Worker * there are no children to wait for.
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker
cleanup_pid(pid_t pid)28*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_pid(pid_t pid)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker if (pid > 0) {
31*49cdfc7eSAndroid Build Coastguard Worker kill(pid, SIGKILL);
32*49cdfc7eSAndroid Build Coastguard Worker waitpid(pid, NULL, 0);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker
case0(void)36*49cdfc7eSAndroid Build Coastguard Worker static void case0(void)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker pid_t pid, ret;
39*49cdfc7eSAndroid Build Coastguard Worker int status;
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
42*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
43*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker exit(0);
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker for (;;) {
49*49cdfc7eSAndroid Build Coastguard Worker ret = waitpid(pid, &status, WNOHANG);
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker if ((ret == -1) && (errno == EINTR))
52*49cdfc7eSAndroid Build Coastguard Worker continue;
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker if (ret == 0)
55*49cdfc7eSAndroid Build Coastguard Worker break;
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "waitpid(WNOHANG) returned %d, expected 0",
58*49cdfc7eSAndroid Build Coastguard Worker ret);
59*49cdfc7eSAndroid Build Coastguard Worker cleanup_pid(pid);
60*49cdfc7eSAndroid Build Coastguard Worker return;
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, NULL, 0);
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "waitpid(pid, WNOHANG) = 0 for a running child");
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker
case1(void)69*49cdfc7eSAndroid Build Coastguard Worker static void case1(void)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker pid_t pid, ret;
72*49cdfc7eSAndroid Build Coastguard Worker int status;
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
75*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0)
76*49cdfc7eSAndroid Build Coastguard Worker exit(0);
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker for (;;) {
79*49cdfc7eSAndroid Build Coastguard Worker ret = waitpid(pid, &status, WNOHANG);
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker if ((ret == -1) && (errno == EINTR))
82*49cdfc7eSAndroid Build Coastguard Worker continue;
83*49cdfc7eSAndroid Build Coastguard Worker if (ret == 0)
84*49cdfc7eSAndroid Build Coastguard Worker continue;
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker if (ret == pid)
87*49cdfc7eSAndroid Build Coastguard Worker break;
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "waitpid(WNOHANG) returned %d, expected %d",
90*49cdfc7eSAndroid Build Coastguard Worker ret, pid);
91*49cdfc7eSAndroid Build Coastguard Worker cleanup_pid(pid);
92*49cdfc7eSAndroid Build Coastguard Worker return;
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker if (!WIFEXITED(status)) {
96*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child exited abnormally");
97*49cdfc7eSAndroid Build Coastguard Worker return;
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker if (WEXITSTATUS(status) != 0) {
101*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child exited with %d, expected 0",
102*49cdfc7eSAndroid Build Coastguard Worker WEXITSTATUS(status));
103*49cdfc7eSAndroid Build Coastguard Worker return;
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "waitpid(pid, WNOHANG) = pid for an exited child");
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
case2(void)109*49cdfc7eSAndroid Build Coastguard Worker static void case2(void)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker pid_t ret;
112*49cdfc7eSAndroid Build Coastguard Worker int status;
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker ret = waitpid(-1, &status, 0);
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker if (ret != -1) {
117*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Expected -1, got %d", ret);
118*49cdfc7eSAndroid Build Coastguard Worker return;
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker if (errno != ECHILD) {
121*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Expected %s, got %s",
122*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(ECHILD), tst_strerrno(errno));
123*49cdfc7eSAndroid Build Coastguard Worker return;
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "waitpid(-1, 0) = -1 with ECHILD if no children");
127*49cdfc7eSAndroid Build Coastguard Worker }
128*49cdfc7eSAndroid Build Coastguard Worker
case3(void)129*49cdfc7eSAndroid Build Coastguard Worker static void case3(void)
130*49cdfc7eSAndroid Build Coastguard Worker {
131*49cdfc7eSAndroid Build Coastguard Worker pid_t ret;
132*49cdfc7eSAndroid Build Coastguard Worker int status;
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker ret = waitpid(-1, &status, WNOHANG);
135*49cdfc7eSAndroid Build Coastguard Worker if (ret != -1) {
136*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "WNOHANG: Expected -1, got %d", ret);
137*49cdfc7eSAndroid Build Coastguard Worker return;
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker if (errno != ECHILD) {
140*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "WNOHANG: Expected %s, got %s",
141*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(ECHILD), tst_strerrno(errno));
142*49cdfc7eSAndroid Build Coastguard Worker return;
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "waitpid(-1, WNOHANG) = -1 with ECHILD if no children");
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker static void (*tests[])(void) = { case0, case1, case2, case3 };
149*49cdfc7eSAndroid Build Coastguard Worker
waitpid09_test(unsigned int id)150*49cdfc7eSAndroid Build Coastguard Worker static void waitpid09_test(unsigned int id)
151*49cdfc7eSAndroid Build Coastguard Worker {
152*49cdfc7eSAndroid Build Coastguard Worker tests[id]();
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
156*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
157*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
158*49cdfc7eSAndroid Build Coastguard Worker .test = waitpid09_test,
159*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tests),
160*49cdfc7eSAndroid Build Coastguard Worker };
161