xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/nice/nice04.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, nice(2) fails when, a non-root user attempts to increase
12*49cdfc7eSAndroid Build Coastguard Worker  * the priority of a process by specifying a negative increment value.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define NICEINC -10
20*49cdfc7eSAndroid Build Coastguard Worker 
verify_nice(void)21*49cdfc7eSAndroid Build Coastguard Worker static void verify_nice(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	TEST(nice(NICEINC));
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
26*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "nice(%i) succeded unexpectedly (returned %li)",
27*49cdfc7eSAndroid Build Coastguard Worker 			NICEINC, TST_RET);
28*49cdfc7eSAndroid Build Coastguard Worker 		return;
29*49cdfc7eSAndroid Build Coastguard Worker 	}
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != EPERM) {
32*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "nice(%i) should fail with EPERM",
33*49cdfc7eSAndroid Build Coastguard Worker 			NICEINC);
34*49cdfc7eSAndroid Build Coastguard Worker 		return;
35*49cdfc7eSAndroid Build Coastguard Worker 	}
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "nice(%i) failed with EPERM", NICEINC);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)40*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETUID(ltpuser->pw_uid);
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
49*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
50*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_nice,
51*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
52*49cdfc7eSAndroid Build Coastguard Worker };
53