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