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