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