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