1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Author: Kathy Olmsted
5f08c3bdfSopenharmony_ci * Co-Pilot: Steve Shaw
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci *[Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * - fork returns without error
12f08c3bdfSopenharmony_ci * - fork returns the pid of the child
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <errno.h>
16f08c3bdfSopenharmony_ci#include <string.h>
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include <sys/types.h>
19f08c3bdfSopenharmony_ci#include <sys/wait.h>
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#define	KIDEXIT	42
23f08c3bdfSopenharmony_ci#define FILENAME "childpid"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic int fd = -1;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic void verify_fork(void)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci	int kid_status, term_pid, child_pid, pid, ret;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
32f08c3bdfSopenharmony_ci	if (!pid) {
33f08c3bdfSopenharmony_ci		SAFE_FILE_PRINTF(FILENAME, "%d", getpid());
34f08c3bdfSopenharmony_ci		exit(KIDEXIT);
35f08c3bdfSopenharmony_ci	}
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	term_pid = SAFE_WAITPID(pid, &kid_status, 0);
38f08c3bdfSopenharmony_ci	if (term_pid == pid) {
39f08c3bdfSopenharmony_ci		if (!WIFEXITED(kid_status)) {
40f08c3bdfSopenharmony_ci			tst_res(TFAIL, "child exited abnormally");
41f08c3bdfSopenharmony_ci			return;
42f08c3bdfSopenharmony_ci		}
43f08c3bdfSopenharmony_ci		ret = WEXITSTATUS(kid_status);
44f08c3bdfSopenharmony_ci		if (ret != KIDEXIT)
45f08c3bdfSopenharmony_ci			tst_res(TFAIL, "incorrect child status returned %d", ret);
46f08c3bdfSopenharmony_ci		else
47f08c3bdfSopenharmony_ci			tst_res(TPASS, "correct child status returned %d", ret);
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci		SAFE_FILE_SCANF(FILENAME, "%d", &child_pid);
50f08c3bdfSopenharmony_ci		TST_EXP_EQ_LI(child_pid, pid);
51f08c3bdfSopenharmony_ci	} else {
52f08c3bdfSopenharmony_ci		tst_res(TFAIL, "waitpid() returns %d instead of expected pid %d",
53f08c3bdfSopenharmony_ci				term_pid, pid);
54f08c3bdfSopenharmony_ci	}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	tst_reap_children();
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic void setup(void)
60f08c3bdfSopenharmony_ci{
61f08c3bdfSopenharmony_ci	fd = SAFE_CREAT(FILENAME, 0700);
62f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic void cleanup(void)
66f08c3bdfSopenharmony_ci{
67f08c3bdfSopenharmony_ci	if (fd > -1)
68f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_cistatic struct tst_test test = {
72f08c3bdfSopenharmony_ci	.setup = setup,
73f08c3bdfSopenharmony_ci	.cleanup = cleanup,
74f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
75f08c3bdfSopenharmony_ci	.forks_child = 1,
76f08c3bdfSopenharmony_ci	.test_all = verify_fork,
77f08c3bdfSopenharmony_ci};
78