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) 2000 Silicon Graphics, Inc. All Rights Reserved.
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 * Verify that getpid() system call returns process ID in range <2, PID_MAX>.
10*49cdfc7eSAndroid Build Coastguard Worker */
11*49cdfc7eSAndroid Build Coastguard Worker
12*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid_max;
16*49cdfc7eSAndroid Build Coastguard Worker
setup(void)17*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
18*49cdfc7eSAndroid Build Coastguard Worker {
19*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF("/proc/sys/kernel/pid_max", "%d\n", &pid_max);
20*49cdfc7eSAndroid Build Coastguard Worker }
21*49cdfc7eSAndroid Build Coastguard Worker
verify_getpid(void)22*49cdfc7eSAndroid Build Coastguard Worker static void verify_getpid(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
25*49cdfc7eSAndroid Build Coastguard Worker int i;
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100; i++) {
28*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
29*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
30*49cdfc7eSAndroid Build Coastguard Worker pid = getpid();
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker /* pid should not be 1 or out of maximum */
33*49cdfc7eSAndroid Build Coastguard Worker if (pid > 1 && pid <= pid_max)
34*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "getpid() returns %d", pid);
35*49cdfc7eSAndroid Build Coastguard Worker else
36*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
37*49cdfc7eSAndroid Build Coastguard Worker "getpid() returns out of range: %d", pid);
38*49cdfc7eSAndroid Build Coastguard Worker exit(0);
39*49cdfc7eSAndroid Build Coastguard Worker } else {
40*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAIT(NULL);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
46*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
47*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
48*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_getpid,
49*49cdfc7eSAndroid Build Coastguard Worker };
50