1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2021, BELLSOFT. All rights reserved.
4f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Testcase to test whether sched_setscheduler(2) sets the errnos
11f08c3bdfSopenharmony_ci * correctly.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * [Algorithm]
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * Call sched_setscheduler as a non-root uid, and expect EPERM
16f08c3bdfSopenharmony_ci * to be returned.
17f08c3bdfSopenharmony_ci *
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include <stdlib.h>
21f08c3bdfSopenharmony_ci#include <errno.h>
22f08c3bdfSopenharmony_ci#include <pwd.h>
23f08c3bdfSopenharmony_ci#include <sys/types.h>
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include "tst_test.h"
26f08c3bdfSopenharmony_ci#include "tst_sched.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic uid_t nobody_uid;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void setup(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	struct passwd *pw = SAFE_GETPWNAM("nobody");
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	tst_res(TINFO, "Testing %s variant", sched_variants[tst_variant].desc);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	nobody_uid = pw->pw_uid;
37f08c3bdfSopenharmony_ci}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void run(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	struct sched_variant *tv = &sched_variants[tst_variant];
42f08c3bdfSopenharmony_ci	pid_t pid = SAFE_FORK();
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	if (!pid) {
45f08c3bdfSopenharmony_ci		struct sched_param p = { .sched_priority = 1 };
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci		SAFE_SETEUID(nobody_uid);
48f08c3bdfSopenharmony_ci		TST_EXP_FAIL(tv->sched_setscheduler(0, SCHED_FIFO, &p), EPERM,
49f08c3bdfSopenharmony_ci			     "sched_setscheduler(0, SCHED_FIFO, %d)",
50f08c3bdfSopenharmony_ci			     p.sched_priority);
51f08c3bdfSopenharmony_ci		exit(0);
52f08c3bdfSopenharmony_ci	}
53f08c3bdfSopenharmony_ci	tst_reap_children();
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic struct tst_test test = {
57f08c3bdfSopenharmony_ci	.forks_child = 1,
58f08c3bdfSopenharmony_ci	.needs_root = 1,
59f08c3bdfSopenharmony_ci	.setup = setup,
60f08c3bdfSopenharmony_ci	.test_variants = ARRAY_SIZE(sched_variants),
61f08c3bdfSopenharmony_ci	.test_all = run,
62f08c3bdfSopenharmony_ci};
63