plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: stime02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that the system call stime() fails to set the system's idea |
| 25 | * of data and time if invoked by "non-root" user. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * stime() should fail with return value -1 and set errno to EPERM. |
| 29 | * |
| 30 | * Algorithm: |
| 31 | * Setup: |
| 32 | * Setup signal handling. |
| 33 | * Pause for SIGUSR1 if option specified. |
| 34 | * |
| 35 | * Test: |
| 36 | * Loop if the proper options are given. |
| 37 | * Execute system call |
| 38 | * Check return code, if system call failed (return=-1) |
| 39 | * if errno set == expected errno |
| 40 | * Issue sys call fails with expected return value and errno. |
| 41 | * Otherwise, |
| 42 | * Issue sys call fails with unexpected errno. |
| 43 | * Otherwise, |
| 44 | * Issue sys call returns unexpected value. |
| 45 | * |
| 46 | * Cleanup: |
| 47 | * Print errno log and/or timing stats if options given |
| 48 | * |
| 49 | * Usage: <for command-line> |
| 50 | * stime02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] |
| 51 | * where, -c n : Run n copies concurrently. |
| 52 | * -e : Turn on errno logging. |
| 53 | * -i n : Execute test n times. |
| 54 | * -I x : Execute test for x seconds. |
| 55 | * -P x : Pause for x seconds between iterations. |
| 56 | * -t : Turn on syscall timing. |
| 57 | * |
| 58 | * History |
| 59 | * 07/2001 John George |
| 60 | * -Ported |
| 61 | * |
| 62 | * Restrictions: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | */ |
| 64 | |
| 65 | #include <stdio.h> |
| 66 | #include <unistd.h> |
| 67 | #include <sys/types.h> |
| 68 | #include <errno.h> |
| 69 | #include <fcntl.h> |
| 70 | #include <time.h> |
| 71 | #include <string.h> |
| 72 | #include <sys/stat.h> |
| 73 | #include <signal.h> |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 74 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | #include "test.h" |
| 77 | #include "usctest.h" |
| 78 | |
| 79 | #define INCR_TIME 10 /* increment in the system's current time */ |
| 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | char *TCID = "stime02"; /* Test program identifier. */ |
| 82 | int TST_TOTAL = 1; /* Total number of test cases. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int exp_enos[] = { EPERM, 0 }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | time_t curr_time; /* system's current time in seconds */ |
| 86 | time_t new_time; /* system's new time */ |
| 87 | time_t tloc; /* argument var. for time() */ |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 88 | char nobody_uid[] = "nobody"; |
| 89 | struct passwd *ltpuser; |
| 90 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | void setup(); /* Main setup function of test */ |
| 92 | void cleanup(); /* cleanup function for the test */ |
| 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 96 | int lc; |
| 97 | char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 100 | msg = parse_opts(ac, av, NULL, NULL); |
| 101 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | setup(); |
| 107 | |
| 108 | /* set the expected errnos... */ |
| 109 | TEST_EXP_ENOS(exp_enos); |
| 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame^] | 113 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 115 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | * Invoke stime(2) to set the system's time |
| 117 | * to the specified new_time as non-root user. |
| 118 | */ |
| 119 | TEST(stime(&new_time)); |
| 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | if (TEST_RETURN == -1) { |
| 122 | TEST_ERROR_LOG(TEST_ERRNO); |
| 123 | if (TEST_ERRNO == EPERM) { |
| 124 | tst_resm(TPASS, "stime(2) fails, Caller not " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | "root, errno:%d", TEST_ERRNO); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | } else { |
| 127 | tst_resm(TFAIL, "stime(2) fails, Caller not " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | "root, errno:%d, expected errno:%d", |
| 129 | TEST_ERRNO, EPERM); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | } else { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 132 | tst_resm(TFAIL, "stime(2) returned %ld, expected -1, " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | "errno:%d", TEST_RETURN, EPERM); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame^] | 135 | tst_count++; /* incr TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 136 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 139 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 141 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * void |
| 145 | * setup() - performs all ONE TIME setup for this test. |
| 146 | * Get the current time and system's new time. |
| 147 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 152 | |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 153 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 155 | tst_brkm(TBROK, NULL, "Test must be run as root"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | } |
| 157 | ltpuser = getpwnam(nobody_uid); |
| 158 | if (setuid(ltpuser->pw_uid) == -1) { |
| 159 | tst_resm(TINFO, "setuid failed to " |
| 160 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 161 | perror("setuid"); |
| 162 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | TEST_PAUSE; |
| 165 | |
| 166 | /* Get the current time */ |
| 167 | if ((curr_time = time(&tloc)) < 0) { |
| 168 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | "time() failed to get current time, errno=%d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 170 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
| 172 | /* Get the system's new time */ |
| 173 | new_time = curr_time + INCR_TIME; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 174 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * void |
| 178 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 179 | * completion or premature exit. |
| 180 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | { |
| 183 | /* |
| 184 | * print timing stats if that option was specified. |
| 185 | * print errno log if that option was specified. |
| 186 | */ |
| 187 | TEST_CLEANUP; |
| 188 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 189 | } |