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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | time_t curr_time; /* system's current time in seconds */ |
| 85 | time_t new_time; /* system's new time */ |
| 86 | time_t tloc; /* argument var. for time() */ |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 87 | char nobody_uid[] = "nobody"; |
| 88 | struct passwd *ltpuser; |
| 89 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | void setup(); /* Main setup function of test */ |
| 91 | void cleanup(); /* cleanup function for the test */ |
| 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | { |
| 95 | int lc; /* loop counter */ |
| 96 | char *msg; /* message returned from parse_opts */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 99 | msg = parse_opts(ac, av, NULL, NULL); |
| 100 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | setup(); |
| 106 | |
| 107 | /* set the expected errnos... */ |
| 108 | TEST_EXP_ENOS(exp_enos); |
| 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 111 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 114 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | * Invoke stime(2) to set the system's time |
| 116 | * to the specified new_time as non-root user. |
| 117 | */ |
| 118 | TEST(stime(&new_time)); |
| 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | if (TEST_RETURN == -1) { |
| 121 | TEST_ERROR_LOG(TEST_ERRNO); |
| 122 | if (TEST_ERRNO == EPERM) { |
| 123 | tst_resm(TPASS, "stime(2) fails, Caller not " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | "root, errno:%d", TEST_ERRNO); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | } else { |
| 126 | tst_resm(TFAIL, "stime(2) fails, Caller not " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | "root, errno:%d, expected errno:%d", |
| 128 | TEST_ERRNO, EPERM); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | } |
| 130 | } else { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 131 | tst_resm(TFAIL, "stime(2) returned %ld, expected -1, " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | "errno:%d", TEST_RETURN, EPERM); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | Tst_count++; /* incr TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 139 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * void |
| 143 | * setup() - performs all ONE TIME setup for this test. |
| 144 | * Get the current time and system's new time. |
| 145 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 150 | |
robbiew | b1c11a1 | 2001-08-28 15:47:02 +0000 | [diff] [blame] | 151 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 153 | tst_brkm(TBROK, NULL, "Test must be run as root"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | } |
| 155 | ltpuser = getpwnam(nobody_uid); |
| 156 | if (setuid(ltpuser->pw_uid) == -1) { |
| 157 | tst_resm(TINFO, "setuid failed to " |
| 158 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 159 | perror("setuid"); |
| 160 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | TEST_PAUSE; |
| 163 | |
| 164 | /* Get the current time */ |
| 165 | if ((curr_time = time(&tloc)) < 0) { |
| 166 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | "time() failed to get current time, errno=%d", errno); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 168 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
| 170 | /* Get the system's new time */ |
| 171 | new_time = curr_time + INCR_TIME; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * void |
| 176 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 177 | * completion or premature exit. |
| 178 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | { |
| 181 | /* |
| 182 | * print timing stats if that option was specified. |
| 183 | * print errno log if that option was specified. |
| 184 | */ |
| 185 | TEST_CLEANUP; |
| 186 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 187 | } |