xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/reboot/reboot02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2009-2021
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Aniruddha Marathe <[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  * Test whether libc wrapper of reboot(2) system call returns appropriate
11*49cdfc7eSAndroid Build Coastguard Worker  * error number for invalid cmd parameter or invalid user.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/reboot.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <linux/reboot.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define INVALID_CMD 100
21*49cdfc7eSAndroid Build Coastguard Worker #define CMD_DESC(x) .cmd = x, .desc = #x
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker char nobody_uid[] = "nobody";
24*49cdfc7eSAndroid Build Coastguard Worker struct passwd *ltpuser;
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
27*49cdfc7eSAndroid Build Coastguard Worker 	int cmd;
28*49cdfc7eSAndroid Build Coastguard Worker 	const char *desc;
29*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
30*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
31*49cdfc7eSAndroid Build Coastguard Worker 	{CMD_DESC(INVALID_CMD), EINVAL},
32*49cdfc7eSAndroid Build Coastguard Worker 	{CMD_DESC(LINUX_REBOOT_CMD_CAD_ON), EPERM},
33*49cdfc7eSAndroid Build Coastguard Worker };
34*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)35*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	if (n == 0)
40*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL(reboot(tc->cmd),
41*49cdfc7eSAndroid Build Coastguard Worker 			tc->exp_errno, "%s", tc->desc);
42*49cdfc7eSAndroid Build Coastguard Worker 	else {
43*49cdfc7eSAndroid Build Coastguard Worker 		ltpuser = SAFE_GETPWNAM(nobody_uid);
44*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(ltpuser->pw_uid);
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL(reboot(tc->cmd),
47*49cdfc7eSAndroid Build Coastguard Worker 			tc->exp_errno, "%s", tc->desc);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(0);
50*49cdfc7eSAndroid Build Coastguard Worker 	}
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
54*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
55*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
56*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
57*49cdfc7eSAndroid Build Coastguard Worker };
58