1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4f08c3bdfSopenharmony_ci *    AUTHOR		: William Roske
5f08c3bdfSopenharmony_ci *    CO-PILOT		: Dave Fenner
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * For a terminated child, test whether wait(2) can get its pid
11f08c3bdfSopenharmony_ci * and exit status correctly.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <errno.h>
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <sys/wait.h>
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic void verify_wait(void)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	int status, exit_child = 1;
23f08c3bdfSopenharmony_ci	pid_t fpid;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	fpid = SAFE_FORK();
26f08c3bdfSopenharmony_ci	if (fpid == 0)
27f08c3bdfSopenharmony_ci		exit(exit_child);
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	TST_EXP_PID_SILENT(wait(&status));
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	if (!TST_PASS)
32f08c3bdfSopenharmony_ci		return;
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	if (fpid != TST_RET) {
35f08c3bdfSopenharmony_ci		tst_res(TFAIL, "wait() returned pid %ld, expected %d",
36f08c3bdfSopenharmony_ci			TST_RET, fpid);
37f08c3bdfSopenharmony_ci		return;
38f08c3bdfSopenharmony_ci	}
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if (WIFEXITED(status) && WEXITSTATUS(status) == exit_child) {
41f08c3bdfSopenharmony_ci		tst_res(TPASS, "wait() succeeded");
42f08c3bdfSopenharmony_ci		return;
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	tst_res(TFAIL, "wait() reported child %s", tst_strstatus(status));
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic struct tst_test test = {
49f08c3bdfSopenharmony_ci	.test_all = verify_wait,
50f08c3bdfSopenharmony_ci	.forks_child = 1,
51f08c3bdfSopenharmony_ci};
52