robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /************************************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : reboot02 |
| 20 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 22 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 24 | * TEST TITLE : Test checking for basic error conditions |
| 25 | * for reboot(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
| 27 | * TEST CASE TOTAL : 2 |
| 28 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 29 | * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 31 | * SIGNALS |
| 32 | * Uses SIGUSR1 to pause before test if option set. |
| 33 | * (See the parse_opts(3) man page). |
| 34 | * |
| 35 | * DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 36 | * This test case checks whether reboot(2) system call returns |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 37 | * appropriate error number for invalid |
| 38 | * flag parameter or invalid user. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 39 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 40 | * Setup: |
| 41 | * Setup signal handling. |
| 42 | * Pause for SIGUSR1 if option specified. |
| 43 | * For testing error on invalid user, change the effective uid |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 44 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 45 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 51 | * Otherwise, |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 52 | * Issue syscall failed to produce expected errno |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 53 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 54 | * Cleanup: |
| 55 | * Do cleanup for the test. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 56 | * $ |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 57 | * USAGE: <for command-line> |
| 58 | * reboot02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p] |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 59 | * where |
| 60 | * -c n: run n copies simultaneously |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 61 | * -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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 67 | * |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 68 | *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" |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 81 | |
| 82 | #define INVALID_PARAMETER 100 |
| 83 | |
| 84 | static void setup(); |
| 85 | static void cleanup(); |
| 86 | static int setup_test(); |
| 87 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 88 | char *TCID = "reboot02"; |
| 89 | int TST_TOTAL = 2; |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 90 | char nobody_uid[] = "nobody"; |
| 91 | struct passwd *ltpuser; |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 92 | |
| 93 | static struct test_case_t { |
| 94 | char *err_desc; /*error description */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | int exp_errno; /* expected error number */ |
| 96 | char *exp_errval; /*Expected errorvalue string */ |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 97 | } testcase[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | { |
| 99 | "Invalid flag", EINVAL, "EINVAL"}, { |
| 100 | "Permission denied", EPERM, "EPERM "} |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | int main(int ac, char **av) |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 104 | { |
| 105 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 106 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 107 | const char *msg; |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 108 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 109 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 110 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 111 | |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 112 | setup(); |
| 113 | |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 114 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 115 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | for (i = 0; i < TST_TOTAL; i++) { |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 117 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 118 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | if (i == 0) { |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 120 | TEST(reboot(INVALID_PARAMETER)); |
| 121 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | /*change the user to nobody */ |
| 123 | if (setup_test() == 0) { |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 124 | TEST(reboot(LINUX_REBOOT_CMD_CAD_ON)); |
| 125 | /* Set effective user id back to root */ |
| 126 | if (seteuid(0) == -1) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 127 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | "seteuid failed to " |
| 129 | "set the effective uid" |
| 130 | " to root"); |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 131 | perror("seteuid"); |
| 132 | } |
| 133 | } else { |
| 134 | tst_resm(TWARN, "skipping the test"); |
| 135 | continue; |
| 136 | } |
| 137 | } |
| 138 | /* check return code */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | if ((TEST_RETURN == -1) |
| 140 | && (TEST_ERRNO == testcase[i].exp_errno)) { |
| 141 | tst_resm(TPASS, |
| 142 | "reboot(2) expected failure;" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | " Got errno - %s : %s", |
| 144 | testcase[i].exp_errval, |
| 145 | testcase[i].err_desc); |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 146 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | 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); |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 152 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | } /*End of TEST LOOPS */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | } |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 155 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | /*Clean up and exit */ |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 157 | cleanup(); |
| 158 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 159 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | } /*End of main */ |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * setup_test() - This function sets the user as nobdy |
| 164 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 165 | int setup_test(void) |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 166 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 168 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | "to set the effective uid to %d", ltpuser->pw_uid); |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 174 | perror("seteuid"); |
| 175 | return -1; |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* setup() - performs all ONE TIME setup for this test */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 181 | void setup(void) |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 182 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 183 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 184 | |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 185 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 186 | |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 187 | TEST_PAUSE; |
| 188 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 189 | } |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * cleanup() - Performs one time cleanup for this test at |
| 193 | * completion or premature exit |
| 194 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 195 | void cleanup(void) |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 196 | { |
robbiew | 07eb41b | 2002-12-24 17:13:36 +0000 | [diff] [blame] | 197 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 198 | } |