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