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