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