blob: 43e6f7fe791190b5e5a8d7e5de22fe4088e71184 [file] [log] [blame]
robbiew07eb41b2002-12-24 17:13:36 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew07eb41b2002-12-24 17:13:36 +000015 *
16 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiew07eb41b2002-12-24 17:13:36 +000019 * TEST IDENTIFIER : reboot02
20 *
subrata_modak4bb656a2009-02-26 12:02:09 +000021 *
robbiew07eb41b2002-12-24 17:13:36 +000022 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
robbiew07eb41b2002-12-24 17:13:36 +000024 * TEST TITLE : Test checking for basic error conditions
25 * for reboot(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
27 * TEST CASE TOTAL : 2
28 *
robbiew07eb41b2002-12-24 17:13:36 +000029 * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000030 *
robbiew07eb41b2002-12-24 17:13:36 +000031 * SIGNALS
32 * Uses SIGUSR1 to pause before test if option set.
33 * (See the parse_opts(3) man page).
34 *
35 * DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000036 * This test case checks whether reboot(2) system call returns
robbiew07eb41b2002-12-24 17:13:36 +000037 * appropriate error number for invalid
38 * flag parameter or invalid user.
subrata_modak4bb656a2009-02-26 12:02:09 +000039 *
robbiew07eb41b2002-12-24 17:13:36 +000040 * Setup:
41 * Setup signal handling.
42 * Pause for SIGUSR1 if option specified.
43 * For testing error on invalid user, change the effective uid
subrata_modak4bb656a2009-02-26 12:02:09 +000044 *
robbiew07eb41b2002-12-24 17:13:36 +000045 * Test:
46 * Loop if the proper options are given.
47 * Execute system call with invaid flag parameter
48 * and then for invalid user
49 * Check return code, if system call fails with errno == expected errno
50 * Issue syscall passed with expected errno
subrata_modak4bb656a2009-02-26 12:02:09 +000051 * Otherwise,
robbiew07eb41b2002-12-24 17:13:36 +000052 * Issue syscall failed to produce expected errno
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
robbiew07eb41b2002-12-24 17:13:36 +000054 * Cleanup:
55 * Do cleanup for the test.
subrata_modak56207ce2009-03-23 13:35:39 +000056 * $
robbiew07eb41b2002-12-24 17:13:36 +000057 * USAGE: <for command-line>
58 * reboot02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p]
subrata_modak4bb656a2009-02-26 12:02:09 +000059 * where
60 * -c n: run n copies simultaneously
robbiew07eb41b2002-12-24 17:13:36 +000061 * -e : Turn on errno logging.
62 * -i n : Execute test n times.
63 * -I x : Execute test for x seconds.
64 * -p : Pause for SIGUSR1 before starting
65 * -P x : Pause for x seconds between iterations.
66 * -t : Turn on syscall timing.
Garrett Cooper2c282152010-12-16 00:55:50 -080067 *
robbiew07eb41b2002-12-24 17:13:36 +000068 *RESTRICTIONS:
69 *for lib4 and lib5 reboot(2) system call is implemented as
70 *int reboot(int magic, int magic2, int flag, void *arg); This test case
71 *is written for int reboot(int flag); which is implemented under glibc
72 *Therefore this testcase may not work under libc4 and libc5 libraries
73 *****************************************************************************/
74
75#include <unistd.h>
76#include <sys/reboot.h>
77#include <errno.h>
78#include <linux/reboot.h>
79#include <pwd.h>
80#include "test.h"
robbiew07eb41b2002-12-24 17:13:36 +000081
82#define INVALID_PARAMETER 100
83
84static void setup();
85static void cleanup();
86static int setup_test();
87
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020088char *TCID = "reboot02";
89int TST_TOTAL = 2;
robbiew07eb41b2002-12-24 17:13:36 +000090char nobody_uid[] = "nobody";
91struct passwd *ltpuser;
robbiew07eb41b2002-12-24 17:13:36 +000092
93static struct test_case_t {
94 char *err_desc; /*error description */
subrata_modak56207ce2009-03-23 13:35:39 +000095 int exp_errno; /* expected error number */
96 char *exp_errval; /*Expected errorvalue string */
robbiew07eb41b2002-12-24 17:13:36 +000097} testcase[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000098 {
99 "Invalid flag", EINVAL, "EINVAL"}, {
100 "Permission denied", EPERM, "EPERM "}
robbiew07eb41b2002-12-24 17:13:36 +0000101};
102
subrata_modak56207ce2009-03-23 13:35:39 +0000103int main(int ac, char **av)
robbiew07eb41b2002-12-24 17:13:36 +0000104{
105
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200106 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200107 const char *msg;
robbiew07eb41b2002-12-24 17:13:36 +0000108
Garrett Coopera9e49f12010-12-16 10:54:03 -0800109 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800110 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew07eb41b2002-12-24 17:13:36 +0000111
robbiew07eb41b2002-12-24 17:13:36 +0000112 setup();
113
robbiew07eb41b2002-12-24 17:13:36 +0000114 for (lc = 0; TEST_LOOPING(lc); lc++) {
115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 for (i = 0; i < TST_TOTAL; i++) {
robbiew07eb41b2002-12-24 17:13:36 +0000117
Caspar Zhangd59a6592013-03-07 14:59:12 +0800118 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000119 if (i == 0) {
robbiew07eb41b2002-12-24 17:13:36 +0000120 TEST(reboot(INVALID_PARAMETER));
121 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000122 /*change the user to nobody */
123 if (setup_test() == 0) {
robbiew07eb41b2002-12-24 17:13:36 +0000124 TEST(reboot(LINUX_REBOOT_CMD_CAD_ON));
125 /* Set effective user id back to root */
126 if (seteuid(0) == -1) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000127 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000128 "seteuid failed to "
129 "set the effective uid"
130 " to root");
robbiew07eb41b2002-12-24 17:13:36 +0000131 perror("seteuid");
132 }
133 } else {
134 tst_resm(TWARN, "skipping the test");
135 continue;
136 }
137 }
138 /* check return code */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 if ((TEST_RETURN == -1)
140 && (TEST_ERRNO == testcase[i].exp_errno)) {
141 tst_resm(TPASS,
142 "reboot(2) expected failure;"
subrata_modak56207ce2009-03-23 13:35:39 +0000143 " Got errno - %s : %s",
144 testcase[i].exp_errval,
145 testcase[i].err_desc);
robbiew07eb41b2002-12-24 17:13:36 +0000146 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000147 tst_resm(TFAIL, "reboot(2) failed to produce"
148 " expected error; %d, errno"
149 ": %s and got %d",
150 testcase[i].exp_errno,
151 testcase[i].exp_errval, TEST_ERRNO);
robbiew07eb41b2002-12-24 17:13:36 +0000152 }
subrata_modak56207ce2009-03-23 13:35:39 +0000153 } /*End of TEST LOOPS */
Garrett Cooper2c282152010-12-16 00:55:50 -0800154 }
robbiew07eb41b2002-12-24 17:13:36 +0000155
subrata_modak56207ce2009-03-23 13:35:39 +0000156 /*Clean up and exit */
robbiew07eb41b2002-12-24 17:13:36 +0000157 cleanup();
158
Garrett Cooper53740502010-12-16 00:04:01 -0800159 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000160} /*End of main */
robbiew07eb41b2002-12-24 17:13:36 +0000161
162/*
163 * setup_test() - This function sets the user as nobdy
164 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400165int setup_test(void)
robbiew07eb41b2002-12-24 17:13:36 +0000166{
subrata_modak56207ce2009-03-23 13:35:39 +0000167 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
robbiew07eb41b2002-12-24 17:13:36 +0000168 tst_resm(TWARN, "\"nobody\" user not present. skipping test");
169 return -1;
170 }
171 if (seteuid(ltpuser->pw_uid) == -1) {
172 tst_resm(TWARN, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000173 "to set the effective uid to %d", ltpuser->pw_uid);
robbiew07eb41b2002-12-24 17:13:36 +0000174 perror("seteuid");
175 return -1;
176 }
177 return 0;
178}
179
180/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400181void setup(void)
robbiew07eb41b2002-12-24 17:13:36 +0000182{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200183 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800184
robbiew07eb41b2002-12-24 17:13:36 +0000185 tst_sig(NOFORK, DEF_HANDLER, cleanup);
186
robbiew07eb41b2002-12-24 17:13:36 +0000187 TEST_PAUSE;
188
Garrett Cooper2c282152010-12-16 00:55:50 -0800189}
robbiew07eb41b2002-12-24 17:13:36 +0000190
191/*
192* cleanup() - Performs one time cleanup for this test at
193* completion or premature exit
194*/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400195void cleanup(void)
robbiew07eb41b2002-12-24 17:13:36 +0000196{
robbiew07eb41b2002-12-24 17:13:36 +0000197
Chris Dearmanec6edca2012-10-17 19:54:01 -0700198}