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');
29 		exit(0);
30 	}
31 
32 	TEST(wait4(pid, &status, 0, &rusage));
33 	if (TST_RET == -1) {
34 		tst_res(TFAIL | TERRNO, "wait4() failed");
35 		return;
36 	}
37 
38 	if (TST_RET != pid) {
39 		tst_res(TFAIL, "waitpid() returned wrong pid %li, expected %i",
40 			TST_RET, pid);
41 	} else {
42 		tst_res(TPASS, "waitpid() returned correct pid %i", pid);
43 	}
44 
45 	if (!WIFEXITED(status)) {
46 		tst_res(TFAIL, "WIFEXITED() not set in status (%s)",
47 		        tst_strstatus(status));
48 		return;
49 	}
50 
51 	tst_res(TPASS, "WIFEXITED() is set in status");
52 
53 	if (WEXITSTATUS(status))
54 		tst_res(TFAIL, "WEXITSTATUS() != 0 but %i", WEXITSTATUS(status));
55 	else
56 		tst_res(TPASS, "WEXITSTATUS() == 0");
57 
58 }
59 
60 static struct tst_test test = {
61 	.forks_child = 1,
62 	.test_all = run,
63 };
64