xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/nice/nice02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 a higher increment value (> max. applicable limits)
13*49cdfc7eSAndroid Build Coastguard Worker  * to nice() system call.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #define	NICEINC 50
22*49cdfc7eSAndroid Build Coastguard Worker #define MAX_PRIO 19
23*49cdfc7eSAndroid Build Coastguard Worker #define DEFAULT_PRIO 0
24*49cdfc7eSAndroid Build Coastguard Worker 
verify_nice(void)25*49cdfc7eSAndroid Build Coastguard Worker static void verify_nice(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	int new_nice;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	TEST(nice(NICEINC));
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
32*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "nice(%d) returned -1", NICEINC);
33*49cdfc7eSAndroid Build Coastguard Worker 		return;
34*49cdfc7eSAndroid Build Coastguard Worker 	}
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR) {
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "nice(%d) failed", NICEINC);
38*49cdfc7eSAndroid Build Coastguard Worker 		return;
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	new_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	if (new_nice != MAX_PRIO) {
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Process priority %i, expected %i",
45*49cdfc7eSAndroid Build Coastguard Worker 			new_nice, MAX_PRIO);
46*49cdfc7eSAndroid Build Coastguard Worker 		return;
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "nice(%d) passed", NICEINC);
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	TEST(nice(DEFAULT_PRIO));
52*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR)
53*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "nice(%d) failed", DEFAULT_PRIO);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
57*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_nice,
58*49cdfc7eSAndroid Build Coastguard Worker };
59