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 * Copyright (c) 2024 SUSE LLC <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Check that waitpid() returns the exit status of a specific child process
11*49cdfc7eSAndroid Build Coastguard Worker * and repeated call on the same process will fail with ECHILD.
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #define MAX_CHILDREN 25
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker static pid_t children[MAX_CHILDREN];
25*49cdfc7eSAndroid Build Coastguard Worker
check_waitpid(pid_t pid,int reaped)26*49cdfc7eSAndroid Build Coastguard Worker static void check_waitpid(pid_t pid, int reaped)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker TEST(waitpid(pid, NULL, 0));
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker if (!reaped && pid == (pid_t)TST_RET) {
31*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "waitpid(%d) returned correct PID", pid);
32*49cdfc7eSAndroid Build Coastguard Worker return;
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker if (reaped && TST_RET == -1 && TST_ERR == ECHILD) {
36*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "waitpid(%d) failed on reaped child",
37*49cdfc7eSAndroid Build Coastguard Worker pid);
38*49cdfc7eSAndroid Build Coastguard Worker return;
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
42*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "waitpid(%d) failed", pid);
43*49cdfc7eSAndroid Build Coastguard Worker return;
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0) {
47*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
48*49cdfc7eSAndroid Build Coastguard Worker "Unexpected waitpid(%d) return value %ld", pid,
49*49cdfc7eSAndroid Build Coastguard Worker TST_RET);
50*49cdfc7eSAndroid Build Coastguard Worker return;
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "waitpid(%d) returned unexpected PID %ld", pid, TST_RET);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker
run(void)56*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker int i;
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < MAX_CHILDREN; i++) {
61*49cdfc7eSAndroid Build Coastguard Worker children[i] = SAFE_FORK();
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker /* Children have nothing to do... */
64*49cdfc7eSAndroid Build Coastguard Worker if (!children[i])
65*49cdfc7eSAndroid Build Coastguard Worker exit(0);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker /* Wait for one specific child */
69*49cdfc7eSAndroid Build Coastguard Worker i = MAX_CHILDREN / 2;
70*49cdfc7eSAndroid Build Coastguard Worker check_waitpid(children[i], 0);
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker /* Try the same child again after it was reaped */
73*49cdfc7eSAndroid Build Coastguard Worker check_waitpid(children[i], 1);
74*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
78*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
79*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1
80*49cdfc7eSAndroid Build Coastguard Worker };
81