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