1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) International Business Machines  Corp., 2001
4 */
5
6/*\
7 * [Description]
8 *
9 * Check that:
10 *
11 * - fork() in parent returns the same pid as getpid() in child
12 * - getppid() in child returns the same pid as getpid() in parent
13 */
14
15#include <errno.h>
16
17#include "tst_test.h"
18
19static pid_t *child_pid;
20
21static void verify_getpid(void)
22{
23	pid_t proc_id;
24	pid_t pid;
25	pid_t pproc_id;
26
27	proc_id = getpid();
28	pid = SAFE_FORK();
29
30	if (pid == 0) {
31		pproc_id = getppid();
32
33		if (pproc_id != proc_id) {
34			tst_res(TFAIL, "child getppid() (%d) != parent getpid() (%d)",
35				pproc_id, proc_id);
36		} else {
37			tst_res(TPASS, "child getppid() == parent getpid() (%d)", proc_id);
38		}
39
40		*child_pid = getpid();
41
42		return;
43	}
44
45	tst_reap_children();
46
47	if (*child_pid != pid)
48		tst_res(TFAIL, "child getpid() (%d) != parent fork() (%d)", *child_pid, pid);
49	else
50		tst_res(TPASS, "child getpid() == parent fork() (%d)", pid);
51}
52
53static void setup(void)
54{
55	child_pid = SAFE_MMAP(NULL, sizeof(pid_t), PROT_READ | PROT_WRITE,
56                              MAP_ANONYMOUS | MAP_SHARED, -1, 0);
57}
58
59static void cleanup(void)
60{
61	SAFE_MUNMAP(child_pid, sizeof(pid_t));
62}
63
64static struct tst_test test = {
65	.forks_child = 1,
66	.setup = setup,
67	.cleanup = cleanup,
68	.test_all = verify_getpid,
69};
70