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 <chrubis@suse.cz>
6 */
7
8 /*
9 * Verify that root can provide a negative value to nice()
10 * and hence root can decrease the nice value of the process
11 * using nice() system call
12 */
13 #include <unistd.h>
14 #include <errno.h>
15 #include <sys/resource.h>
16 #include "tst_test.h"
17
18 #define NICEINC -12
19
verify_nice(void)20 static void verify_nice(void)
21 {
22 int new_nice;
23 int orig_nice;
24
25 orig_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
26
27 TEST(nice(NICEINC));
28
29 if (TST_RET != (orig_nice + NICEINC)) {
30 tst_res(TFAIL | TTERRNO, "nice(%d) returned %li, expected %i",
31 NICEINC, TST_RET, orig_nice + NICEINC);
32 return;
33 }
34
35 if (TST_ERR) {
36 tst_res(TFAIL | TTERRNO, "nice(%d) failed", NICEINC);
37 return;
38 }
39
40 new_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
41
42 if (new_nice != (orig_nice + NICEINC)) {
43 tst_res(TFAIL, "Process priority %i, expected %i",
44 new_nice, orig_nice + NICEINC);
45 return;
46 }
47
48 tst_res(TPASS, "nice(%d) passed", NICEINC);
49
50 TEST(nice(-NICEINC));
51 if (TST_ERR)
52 tst_brk(TBROK | TTERRNO, "nice(-NICEINC) failed");
53 }
54
55 static struct tst_test test = {
56 .test_all = verify_nice,
57 .needs_root = 1,
58 };
59