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) 2022 SUSE LLC Avinesh Kumar <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Verify that setpgid(2) syscall fails with:
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - EINVAL when given pgid is less than 0.
14*49cdfc7eSAndroid Build Coastguard Worker * - ESRCH when pid is not the calling process and not a child of
15*49cdfc7eSAndroid Build Coastguard Worker * the calling process.
16*49cdfc7eSAndroid Build Coastguard Worker * - EPERM when an attempt was made to move a process into a nonexisting
17*49cdfc7eSAndroid Build Coastguard Worker * process group.
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker static pid_t pgid, pid, ppid, inval_pgid;
25*49cdfc7eSAndroid Build Coastguard Worker static pid_t negative_pid = -1;
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
28*49cdfc7eSAndroid Build Coastguard Worker pid_t *pid;
29*49cdfc7eSAndroid Build Coastguard Worker pid_t *pgid;
30*49cdfc7eSAndroid Build Coastguard Worker int error;
31*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
32*49cdfc7eSAndroid Build Coastguard Worker {&pid, &negative_pid, EINVAL},
33*49cdfc7eSAndroid Build Coastguard Worker {&ppid, &pgid, ESRCH},
34*49cdfc7eSAndroid Build Coastguard Worker {&pid, &inval_pgid, EPERM}
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker
setup(void)37*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker pid = getpid();
40*49cdfc7eSAndroid Build Coastguard Worker ppid = getppid();
41*49cdfc7eSAndroid Build Coastguard Worker pgid = getpgrp();
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker /*
44*49cdfc7eSAndroid Build Coastguard Worker * pid_max would not be in use by another process and guarantees that
45*49cdfc7eSAndroid Build Coastguard Worker * it corresponds to an invalid PGID, generating EPERM.
46*49cdfc7eSAndroid Build Coastguard Worker */
47*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF("/proc/sys/kernel/pid_max", "%d\n", &inval_pgid);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int n)50*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(setpgid(*tc->pid, *tc->pgid), tc->error,
55*49cdfc7eSAndroid Build Coastguard Worker "setpgid(%d, %d)", *tc->pid, *tc->pgid);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
59*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
60*49cdfc7eSAndroid Build Coastguard Worker .test = run,
61*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases)
62*49cdfc7eSAndroid Build Coastguard Worker };
63