xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setpgid/setpgid03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Cyril Hrubis <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Tests setpgid(2) errors:
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * - EPERM The process specified by pid must not be a session leader.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * - EPERM The calling process, process specified by pid and the target
17*49cdfc7eSAndroid Build Coastguard Worker  *         process group must be in the same session.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * - EACCESS Proccess cannot change process group ID of a child after child
20*49cdfc7eSAndroid Build Coastguard Worker  *           has performed exec()
21*49cdfc7eSAndroid Build Coastguard Worker  */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #define TEST_APP "setpgid03_child"
28*49cdfc7eSAndroid Build Coastguard Worker 
do_child(void)29*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETSID();
32*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
run(void)35*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	pid_t child_pid;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	child_pid = SAFE_FORK();
40*49cdfc7eSAndroid Build Coastguard Worker 	if (!child_pid) {
41*49cdfc7eSAndroid Build Coastguard Worker 		do_child();
42*49cdfc7eSAndroid Build Coastguard Worker 		return;
43*49cdfc7eSAndroid Build Coastguard Worker 	}
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(setpgid(child_pid, getppid()), EPERM);
48*49cdfc7eSAndroid Build Coastguard Worker 	/* Child did setsid(), so its PGID is set to its PID. */
49*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(setpgid(0, child_pid), EPERM);
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	/* child after exec() we are no longer allowed to set pgid */
54*49cdfc7eSAndroid Build Coastguard Worker 	child_pid = SAFE_FORK();
55*49cdfc7eSAndroid Build Coastguard Worker 	if (!child_pid)
56*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_EXECLP(TEST_APP, TEST_APP, NULL);
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(setpgid(child_pid, getppid()), EACCES);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
66*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
67*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
68*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
69*49cdfc7eSAndroid Build Coastguard Worker };
70