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: stime01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that the system call stime() successfully sets the system's idea |
plars | 85da7aa | 2002-09-25 17:45:49 +0000 | [diff] [blame] | 25 | * of date and time if invoked by "root" user. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * Expected Result: |
| 28 | * stime() should succeed to set the system data/time to the specified time. |
| 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 | * Log the errno and Issue a FAIL message. |
| 40 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 41 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * if successful, |
| 43 | * Issue Functionality-Pass message. |
| 44 | * Otherwise, |
| 45 | * Issue Functionality-Fail message. |
| 46 | * Cleanup: |
| 47 | * Print errno log and/or timing stats if options given |
| 48 | * |
| 49 | * Usage: <for command-line> |
| 50 | * stime01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 51 | * where, -c n : Run n copies concurrently. |
| 52 | * -e : Turn on errno logging. |
| 53 | * -f : Turn off functionality Testing. |
| 54 | * -i n : Execute test n times. |
| 55 | * -I x : Execute test for x seconds. |
| 56 | * -P x : Pause for x seconds between iterations. |
| 57 | * -t : Turn on syscall timing. |
| 58 | * |
| 59 | * History |
| 60 | * 07/2001 John George |
| 61 | * -Ported |
| 62 | * |
| 63 | * Restrictions: |
| 64 | * This test should be run by 'super-user' (root) only. |
| 65 | * |
| 66 | */ |
| 67 | |
| 68 | #include <stdio.h> |
| 69 | #include <unistd.h> |
| 70 | #include <sys/types.h> |
| 71 | #include <errno.h> |
| 72 | #include <fcntl.h> |
| 73 | #include <time.h> |
| 74 | #include <string.h> |
| 75 | #include <sys/stat.h> |
| 76 | #include <signal.h> |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 77 | #include <sys/time.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 81 | #define INCR_TIME 30 /* increment in the system's current time */ |
| 82 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 83 | #define BASH_CLOCK |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 85 | char *TCID = "stime01"; |
| 86 | int TST_TOTAL = 1; |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 87 | struct timeval real_time_tv, pres_time_tv; |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 88 | time_t new_time; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 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 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 95 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 96 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 98 | msg = parse_opts(ac, av, NULL, NULL); |
| 99 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 101 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | setup(); |
| 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * ``Break`` the clock. |
| 110 | * |
| 111 | * This is being done inline here so that the offset is |
| 112 | * automatically reset based on the elapsed time, and not a |
| 113 | * fixed time sampled once in setup. |
| 114 | * |
| 115 | * The big assumption here is the clock state isn't super |
| 116 | * fubared if so, the executing party needs to go fix their |
| 117 | * RTC's battery, or they have more pressing issues to attend |
| 118 | * to as far as clock skew is concerned :P. |
| 119 | */ |
| 120 | if (gettimeofday(&real_time_tv, NULL) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | tst_brkm(TBROK | TERRNO, NULL, |
| 122 | "failed to get current time via gettimeofday(2)"); |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Get the system's new time */ |
| 126 | new_time = real_time_tv.tv_sec + INCR_TIME; |
| 127 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 128 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 130 | /* |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 131 | * Invoke stime(2) to set the system's time to the specified |
| 132 | * new_time. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | */ |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 134 | if (stime(&new_time) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | tst_resm(TFAIL | TERRNO, "stime(%ld) failed", new_time); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | } else { |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 139 | * Get the system's current time after call |
| 140 | * to stime(). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 142 | if (gettimeofday(&pres_time_tv, NULL) < 0) { |
| 143 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 144 | "time() failed to get " |
| 145 | "system's time after stime"); |
| 146 | } |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 147 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 148 | /* Now do the actual verification */ |
| 149 | switch (pres_time_tv.tv_sec - new_time) { |
| 150 | case 0: |
| 151 | case 1: |
| 152 | tst_resm(TINFO, "pt.tv_sec: %ld", |
| 153 | pres_time_tv.tv_sec); |
| 154 | tst_resm(TPASS, "system time was set " |
| 155 | "to %ld", new_time); |
| 156 | break; |
| 157 | default: |
| 158 | tst_resm(TFAIL, "system time was not " |
| 159 | "set to %ld (time is " |
| 160 | "actually: %ld)", |
| 161 | new_time, pres_time_tv.tv_sec); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 162 | } |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 163 | |
| 164 | if (settimeofday(&real_time_tv, NULL) < 0) { |
| 165 | tst_resm(TBROK | TERRNO, |
| 166 | "failed to restore time to original " |
| 167 | "value; system clock may need to be " |
| 168 | "fixed manually"); |
| 169 | } |
| 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | } |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 172 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 173 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | |
Garrett Cooper | e9cca93 | 2010-02-24 18:42:58 -0800 | [diff] [blame] | 175 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 176 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 177 | |
| 178 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * void |
| 182 | * setup() - performs all ONE TIME setup for this test. |
| 183 | * Get the current time and system's new time to be set in the test. |
| 184 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 185 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 187 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | TEST_PAUSE; |
| 190 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 191 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * void |
| 195 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 196 | * completion or premature exit. |
| 197 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 198 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 201 | /* Restore the original system time. */ |
| 202 | if (settimeofday(&real_time_tv, NULL) != 0) { |
| 203 | tst_resm(TBROK | TERRNO, "failed to restore time to original " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 204 | "value; system clock may need to be " |
| 205 | "fixed manually"); |
Garrett Cooper | ee8d486 | 2010-02-24 18:35:08 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 208 | } |