xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/nice/nice01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  * Ported to LTP: Wayne Boyer
5  * Copyright (c) 2016 Cyril Hrubis <[email protected]>
6  * Copyright (c) Linux Test Project, 2003-2022
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Verify that root can provide a negative value to nice() system call and hence
13  * root can decrease the nice value of the process using nice().
14  */
15 
16 #include <unistd.h>
17 #include <errno.h>
18 #include <sys/resource.h>
19 #include "tst_test.h"
20 
21 #define MIN_PRIO        -20
22 
23 static int nice_inc[] = {-1, -12, -50};
24 
verify_nice(unsigned int i)25 static void verify_nice(unsigned int i)
26 {
27 	int new_nice;
28 	int orig_nice;
29 	int exp_nice;
30 	int inc = nice_inc[i];
31 	int delta;
32 
33 	orig_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
34 
35 	TEST(nice(inc));
36 
37 	exp_nice = MAX(MIN_PRIO, (orig_nice + inc));
38 
39 	if (TST_RET != exp_nice) {
40 		tst_res(TFAIL | TTERRNO, "nice(%d) returned %li, expected %i",
41 				inc, TST_RET, exp_nice);
42 		return;
43 	}
44 
45 	if (TST_ERR) {
46 		tst_res(TFAIL | TTERRNO, "nice(%d) failed", inc);
47 		return;
48 	}
49 
50 	new_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
51 
52 	if (new_nice != exp_nice) {
53 		tst_res(TFAIL, "Process priority %i, expected %i",
54 				new_nice, exp_nice);
55 		return;
56 	}
57 
58 	tst_res(TPASS, "nice(%d) passed", inc);
59 
60 	delta = orig_nice - exp_nice;
61 	TEST(nice(delta));
62 	if (TST_ERR)
63 		tst_brk(TBROK | TTERRNO, "nice(%d) failed", delta);
64 }
65 
66 static struct tst_test test = {
67 	.needs_root = 1,
68 	.test = verify_nice,
69 	.tcnt = ARRAY_SIZE(nice_inc),
70 };
71