1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) 2012-2018 Cyril Hrubis <chrubis@suse.cz>
5 */
6
7 /*
8 * wait401 - check that a call to wait4() correctly waits for a child
9 * process to exit
10 */
11
12 #include <stdlib.h>
13 #include <errno.h>
14 #define _USE_BSD
15 #include <sys/types.h>
16 #include <sys/resource.h>
17 #include <sys/wait.h>
18 #include "tst_test.h"
19
run(void)20 static void run(void)
21 {
22 pid_t pid;
23 int status = 1;
24 struct rusage rusage;
25
26 pid = SAFE_FORK();
27 if (!pid) {
28 TST_PROCESS_STATE_WAIT(getppid(), 'S', 0);
29 exit(0);
30 }
31
32 TST_EXP_PID_SILENT(wait4(pid, &status, 0, &rusage), "wait4()");
33 if (!TST_PASS)
34 return;
35
36 if (TST_RET != pid) {
37 tst_res(TFAIL, "wait4() returned wrong pid %li, expected %i",
38 TST_RET, pid);
39 } else {
40 tst_res(TPASS, "wait4() returned correct pid %i", pid);
41 }
42
43 if (!WIFEXITED(status)) {
44 tst_res(TFAIL, "WIFEXITED() not set in status (%s)",
45 tst_strstatus(status));
46 return;
47 }
48
49 tst_res(TPASS, "WIFEXITED() is set in status");
50
51 if (WEXITSTATUS(status))
52 tst_res(TFAIL, "WEXITSTATUS() != 0 but %i", WEXITSTATUS(status));
53 else
54 tst_res(TPASS, "WEXITSTATUS() == 0");
55
56 }
57
58 static struct tst_test test = {
59 .forks_child = 1,
60 .test_all = run,
61 };
62