xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getppid/getppid01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2006-2023
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Test whether parent process id that getppid() returns is out of range.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid_max;
17*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)18*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF("/proc/sys/kernel/pid_max", "%d\n", &pid_max);
21*49cdfc7eSAndroid Build Coastguard Worker }
22*49cdfc7eSAndroid Build Coastguard Worker 
verify_getppid(void)23*49cdfc7eSAndroid Build Coastguard Worker static void verify_getppid(void)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	pid_t ppid;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	ppid = getppid();
28*49cdfc7eSAndroid Build Coastguard Worker 	if (ppid > pid_max)
29*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "getppid() returned %d, out of range!", ppid);
30*49cdfc7eSAndroid Build Coastguard Worker 	else
31*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "getppid() returned %d", ppid);
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
35*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
36*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_getppid,
37*49cdfc7eSAndroid Build Coastguard Worker };
38