aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/setpgid/setpgid03.c
blob: 9ce2603d8a309ec8e3e69578c241b8698a17350f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) International Business Machines  Corp., 2001
 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
 * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
 * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
 */

/*\
 * [Description]
 *
 * Tests setpgid(2) errors:
 *
 * - EPERM The process specified by pid must not be a session leader.
 *
 * - EPERM The calling process, process specified by pid and the target
 *         process group must be in the same session.
 *
 * - EACCESS Proccess cannot change process group ID of a child after child
 *           has performed exec()
 */

#include <unistd.h>
#include <sys/wait.h>
#include "tst_test.h"

#define TEST_APP "setpgid03_child"

static void do_child(void)
{
	SAFE_SETSID();
	TST_CHECKPOINT_WAKE_AND_WAIT(0);
}

static void run(void)
{
	pid_t child_pid;

	child_pid = SAFE_FORK();
	if (!child_pid) {
		do_child();
		return;
	}

	TST_CHECKPOINT_WAIT(0);

	TST_EXP_FAIL(setpgid(child_pid, getppid()), EPERM);
	/* Child did setsid(), so its PGID is set to its PID. */
	TST_EXP_FAIL(setpgid(0, child_pid), EPERM);

	TST_CHECKPOINT_WAKE(0);

	/* child after exec() we are no longer allowed to set pgid */
	child_pid = SAFE_FORK();
	if (!child_pid)
		SAFE_EXECLP(TEST_APP, TEST_APP, NULL);

	TST_CHECKPOINT_WAIT(0);

	TST_EXP_FAIL(setpgid(child_pid, getppid()), EACCES);

	TST_CHECKPOINT_WAKE(0);
}

static struct tst_test test = {
	.test_all = run,
	.forks_child = 1,
	.needs_checkpoints = 1,
};