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 */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Verify that any user can successfully increase the nice value of
12*49cdfc7eSAndroid Build Coastguard Worker * the process by passing an increment value (< max. applicable limits) to
13*49cdfc7eSAndroid Build Coastguard Worker * nice() system call.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.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 NICEINC 2
22*49cdfc7eSAndroid Build Coastguard Worker #define MAX_PRIO 19
23*49cdfc7eSAndroid Build Coastguard Worker
nice_test(void)24*49cdfc7eSAndroid Build Coastguard Worker static void nice_test(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker int new_nice;
27*49cdfc7eSAndroid Build Coastguard Worker int orig_nice;
28*49cdfc7eSAndroid Build Coastguard Worker int exp_nice;
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker orig_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker TEST(nice(NICEINC));
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
35*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "nice(%d) returned -1", NICEINC);
36*49cdfc7eSAndroid Build Coastguard Worker return;
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR) {
40*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "nice(%d) failed", NICEINC);
41*49cdfc7eSAndroid Build Coastguard Worker return;
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker new_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
45*49cdfc7eSAndroid Build Coastguard Worker exp_nice = MIN(MAX_PRIO, (orig_nice + NICEINC));
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker if (new_nice != exp_nice) {
48*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Process priority %i, expected %i",
49*49cdfc7eSAndroid Build Coastguard Worker new_nice, exp_nice);
50*49cdfc7eSAndroid Build Coastguard Worker return;
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "nice(%d) passed", NICEINC);
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker exit(0);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
verify_nice(void)58*49cdfc7eSAndroid Build Coastguard Worker static void verify_nice(void)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_FORK())
61*49cdfc7eSAndroid Build Coastguard Worker nice_test();
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker tst_reap_children();
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 .forks_child = 1,
68*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_nice,
69*49cdfc7eSAndroid Build Coastguard Worker };
70