blob: 64b684268954155ca83ec734c88b92fc42e72a8c [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: stime01
22 *
23 * Test Description:
24 * Verify that the system call stime() successfully sets the system's idea
plars85da7aa2002-09-25 17:45:49 +000025 * of date and time if invoked by "root" user.
plars865695b2001-08-27 22:15:12 +000026 *
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_modakbdbaec52009-02-26 12:14:51 +000041 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000042 * 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 Cooperee8d4862010-02-24 18:35:08 -080077#include <sys/time.h>
plars865695b2001-08-27 22:15:12 +000078
79#include "test.h"
plars865695b2001-08-27 22:15:12 +000080
Garrett Cooperee8d4862010-02-24 18:35:08 -080081#define INCR_TIME 30 /* increment in the system's current time */
82
Garrett Cooper2c282152010-12-16 00:55:50 -080083#define BASH_CLOCK
plars865695b2001-08-27 22:15:12 +000084
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020085char *TCID = "stime01";
86int TST_TOTAL = 1;
Garrett Cooperee8d4862010-02-24 18:35:08 -080087struct timeval real_time_tv, pres_time_tv;
Cyril Hrubis605fa332015-02-04 13:11:20 +010088time_t new_time;
plars865695b2001-08-27 22:15:12 +000089
90void setup(); /* Main setup function of test */
91void cleanup(); /* cleanup function for the test */
92
subrata_modak56207ce2009-03-23 13:35:39 +000093int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000094{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020095 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020096 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000097
Garrett Cooper45e285d2010-11-22 12:19:25 -080098 msg = parse_opts(ac, av, NULL, NULL);
99 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000100 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800101
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 }
plars865695b2001-08-27 22:15:12 +0000103
plars865695b2001-08-27 22:15:12 +0000104 setup();
105
plars865695b2001-08-27 22:15:12 +0000106 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooperee8d4862010-02-24 18:35:08 -0800107
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 Gao354ebb42012-12-07 10:10:04 +0800121 tst_brkm(TBROK | TERRNO, NULL,
122 "failed to get current time via gettimeofday(2)");
Garrett Cooperee8d4862010-02-24 18:35:08 -0800123 }
124
125 /* Get the system's new time */
126 new_time = real_time_tv.tv_sec + INCR_TIME;
127
Caspar Zhangd59a6592013-03-07 14:59:12 +0800128 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000129
subrata_modak4bb656a2009-02-26 12:02:09 +0000130 /*
Garrett Cooperee8d4862010-02-24 18:35:08 -0800131 * Invoke stime(2) to set the system's time to the specified
132 * new_time.
plars865695b2001-08-27 22:15:12 +0000133 */
Garrett Cooperee8d4862010-02-24 18:35:08 -0800134 if (stime(&new_time) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 tst_resm(TFAIL | TERRNO, "stime(%ld) failed", new_time);
plars865695b2001-08-27 22:15:12 +0000136 } else {
Garrett Cooperee8d4862010-02-24 18:35:08 -0800137
plars865695b2001-08-27 22:15:12 +0000138 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200139 * Get the system's current time after call
140 * to stime().
plars865695b2001-08-27 22:15:12 +0000141 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200142 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 Cooperee8d4862010-02-24 18:35:08 -0800147
Cyril Hrubise38b9612014-06-02 17:20:57 +0200148 /* 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_modak4bb656a2009-02-26 12:02:09 +0000162 }
Garrett Cooperee8d4862010-02-24 18:35:08 -0800163
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
plars865695b2001-08-27 22:15:12 +0000171 }
Garrett Cooperee8d4862010-02-24 18:35:08 -0800172
Garrett Cooper2c282152010-12-16 00:55:50 -0800173 }
plars865695b2001-08-27 22:15:12 +0000174
Garrett Coopere9cca932010-02-24 18:42:58 -0800175 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800176 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800177
178}
plars865695b2001-08-27 22:15:12 +0000179
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 Frysingerc57fba52014-04-09 18:56:30 -0400185void setup(void)
plars865695b2001-08-27 22:15:12 +0000186{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200187 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000188
plars865695b2001-08-27 22:15:12 +0000189 TEST_PAUSE;
190
Garrett Cooper2c282152010-12-16 00:55:50 -0800191}
plars865695b2001-08-27 22:15:12 +0000192
193/*
194 * void
195 * cleanup() - performs all ONE TIME cleanup for this test at
196 * completion or premature exit.
197 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400198void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000199{
plars865695b2001-08-27 22:15:12 +0000200
Garrett Cooperee8d4862010-02-24 18:35:08 -0800201 /* 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 Gao354ebb42012-12-07 10:10:04 +0800204 "value; system clock may need to be "
205 "fixed manually");
Garrett Cooperee8d4862010-02-24 18:35:08 -0800206 }
207
Chris Dearmanec6edca2012-10-17 19:54:01 -0700208}