xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getpid/getpid01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Verify that getpid() system call returns process ID in range <2, PID_MAX>.
10  */
11 
12 #include <stdlib.h>
13 #include "tst_test.h"
14 
15 static pid_t pid_max;
16 
setup(void)17 static void setup(void)
18 {
19 	SAFE_FILE_SCANF("/proc/sys/kernel/pid_max", "%d\n", &pid_max);
20 }
21 
verify_getpid(void)22 static void verify_getpid(void)
23 {
24 	pid_t pid;
25 	int i;
26 
27 	for (i = 0; i < 100; i++) {
28 		pid = SAFE_FORK();
29 		if (pid == 0) {
30 			pid = getpid();
31 
32 			/* pid should not be 1 or out of maximum */
33 			if (pid > 1 && pid <= pid_max)
34 				tst_res(TPASS, "getpid() returns %d", pid);
35 			else
36 				tst_res(TFAIL,
37 					"getpid() returns out of range: %d", pid);
38 			exit(0);
39 		} else {
40 			SAFE_WAIT(NULL);
41 		}
42 	}
43 }
44 
45 static struct tst_test test = {
46 	.setup = setup,
47 	.forks_child = 1,
48 	.test_all = verify_getpid,
49 };
50