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