xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sched_setscheduler/sched_setscheduler04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 Federico Bonfiglio <[email protected]>
4  */
5 
6 /*
7  * [Description]
8  *
9  * Testcases that test if sched_setscheduler with flag
10  * SCHED_RESET_ON_FORK restores children policy to
11  * SCHED_NORMAL.
12  *
13  */
14 
15 #define _GNU_SOURCE
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sched.h>
19 #include <linux/sched.h>
20 
21 #include "tst_test.h"
22 #include "tst_sched.h"
23 
24 struct test_case_t {
25 	int policy;
26 	char *desc;
27 };
28 
29 static struct test_case_t cases[] = {
30 	{
31 		.policy = SCHED_FIFO,
32 		.desc = "SCHED_FIFO"
33 	},
34 	{
35 		.policy = SCHED_RR,
36 		.desc = "SCHED_RR"
37 	}
38 };
39 
test_reset_on_fork(unsigned int i)40 static void test_reset_on_fork(unsigned int i)
41 {
42 	struct sched_variant *tv = &sched_variants[tst_variant];
43 	struct test_case_t *tc = &cases[i];
44 
45 	tst_res(TINFO, "Testing %s variant %s policy", tv->desc, tc->desc);
46 
47 	struct sched_param param = { .sched_priority = 10 };
48 
49 	tv->sched_setscheduler(getpid(), tc->policy | SCHED_RESET_ON_FORK, &param);
50 
51 	pid_t pid = SAFE_FORK();
52 
53 	if (pid) {
54 		if (sched_getscheduler(pid) == SCHED_NORMAL)
55 			tst_res(TPASS, "Policy reset to SCHED_NORMAL");
56 		else
57 			tst_res(TFAIL, "Policy NOT reset to SCHED_NORMAL");
58 
59 		sched_getparam(pid, &param);
60 
61 		/* kernel will return sched_priority 0 if task is not RT Policy */
62 		if (param.sched_priority == 0)
63 			tst_res(TPASS, "Priority set to 0");
64 		else
65 			tst_res(TFAIL, "Priority not set to 0");
66 	}
67 }
68 
69 static struct tst_test test = {
70 	.forks_child = 1,
71 	.caps = (struct tst_cap[]) {
72 		TST_CAP(TST_CAP_REQ, CAP_SYS_NICE),
73 		{}
74 	},
75 	.test_variants = ARRAY_SIZE(sched_variants),
76 	.tcnt = ARRAY_SIZE(cases),
77 	.test = test_reset_on_fork,
78 };
79