• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * - fork() in parent returns the same pid as getpid() in child
11  * - getppid() in child returns the same pid as getpid() in parent
12  */
13 
14 #include <errno.h>
15 
16 #include "tst_test.h"
17 
18 static pid_t *child_pid;
19 
verify_getpid(void)20 static void verify_getpid(void)
21 {
22 	pid_t proc_id;
23 	pid_t pid;
24 	pid_t pproc_id;
25 
26 	proc_id = getpid();
27 	pid = SAFE_FORK();
28 
29 	if (pid == 0) {
30 		pproc_id = getppid();
31 
32 		if (pproc_id != proc_id) {
33 			tst_res(TFAIL, "child getppid() (%d) != parent getpid() (%d)",
34 				pproc_id, proc_id);
35 		} else {
36 			tst_res(TPASS, "child getppid() == parent getpid() (%d)", proc_id);
37 		}
38 
39 		*child_pid = getpid();
40 
41 		return;
42 	}
43 
44 	tst_reap_children();
45 
46 	if (*child_pid != pid)
47 		tst_res(TFAIL, "child getpid() (%d) != parent fork() (%d)", *child_pid, pid);
48 	else
49 		tst_res(TPASS, "child getpid() == parent fork() (%d)", pid);
50 }
51 
setup(void)52 static void setup(void)
53 {
54 	child_pid = SAFE_MMAP(NULL, sizeof(pid_t), PROT_READ | PROT_WRITE,
55                               MAP_ANONYMOUS | MAP_SHARED, -1, 0);
56 }
57 
cleanup(void)58 static void cleanup(void)
59 {
60 	SAFE_MUNMAP(child_pid, sizeof(pid_t));
61 }
62 
63 static struct tst_test test = {
64 	.forks_child = 1,
65 	.setup = setup,
66 	.cleanup = cleanup,
67 	.test_all = verify_getpid,
68 };
69