blob: f15f6f3eb808ddf1f9602f26591b185fe5e9fe01 [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 * NAME
22 * settimeofday01.c
23 *
24 * DESCRIPTION
25 * Testcase to check the basic functionality of settimeofday().
26 *
27 * ALGORITHM
28 * Setup:
29 * Setup signal handling.
30 * Check that we are the proper user.
31 * Setup expected errnos.
32 * Pause for SIGUSER1 if option specified.
33 * Save the current time values.
34 * Loop if the proper options are given.
35 * Call settimeofday and verify the time was changed.
36 * Call settimeofday with invalid Args and verify that the call fails.
37 * Cleanup:
38 * Restore the original time values.
39 * Print errno log and/or timing stats if options given.
40 *
41 * USAGE: <for command-line>
42 * settimeofday01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
43 * where, -c n : Run n copies concurrently.
44 * -e : Turn on errno logging.
45 * -i n : Execute test n times.
46 * -I x : Execute test for x seconds.
47 * -P x : Pause for x seconds between iterations.
48 * -t : Turn on syscall timing.
49 *
50 * History
51 * 07/2001 John George
52 * -Ported
53 *
54 * Restrictions
55 * Must be run as root.
56 */
57
58#include <sys/time.h>
59#include <errno.h>
60#include <unistd.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080061#include "test.h"
plars865695b2001-08-27 22:15:12 +000062
63#define FAILED 1
64#define VAL_SEC 100
65#define VAL_MSEC 100
robbiew4e8556e2004-11-22 17:08:16 +000066#define ACCEPTABLE_DELTA 500 /* in milli-seconds */
plars865695b2001-08-27 22:15:12 +000067
68char *TCID = "settimeofday01";
69int TST_TOTAL = 1;
70time_t save_tv_sec, save_tv_usec;
robbiew4e8556e2004-11-22 17:08:16 +000071struct timeval tp, tp1, tp2;
plars865695b2001-08-27 22:15:12 +000072
73void setup(void);
74void cleanup(void);
75
vapiercc0d4502006-02-27 04:36:28 +000076#if !defined(UCLINUX)
77
plars74948ad2002-11-14 16:16:14 +000078int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000079{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020080 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020081 const char *msg;
robbiew4e8556e2004-11-22 17:08:16 +000082 suseconds_t delta;
plars865695b2001-08-27 22:15:12 +000083
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000085 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080086
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 }
plars865695b2001-08-27 22:15:12 +000088
89 setup();
90
plars865695b2001-08-27 22:15:12 +000091 for (lc = 0; TEST_LOOPING(lc); lc++) {
92 int condition_number = 1;
Caspar Zhangd59a6592013-03-07 14:59:12 +080093 /* reset tst_count in case we are looping */
94 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000095
subrata_modak56207ce2009-03-23 13:35:39 +000096 tp.tv_sec = VAL_SEC;
97 tp.tv_usec = VAL_MSEC;
plars865695b2001-08-27 22:15:12 +000098
99 TEST(settimeofday(&tp, NULL));
100 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000101 tst_resm(TFAIL, "Error Setting Time, errno=%d",
102 TEST_ERRNO);
103 }
104
robbiew4e8556e2004-11-22 17:08:16 +0000105 if ((gettimeofday(&tp2, (struct timezone *)&tp1)) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000106 tst_resm(TBROK, "Error Getting Time, errno=%d", errno);
plars865695b2001-08-27 22:15:12 +0000107 }
108
robbiew4e8556e2004-11-22 17:08:16 +0000109 if (tp2.tv_sec > tp.tv_sec) {
subrata_modak56207ce2009-03-23 13:35:39 +0000110 delta =
111 (suseconds_t) (tp2.tv_sec - tp.tv_sec) * 1000 +
112 (tp2.tv_usec - tp.tv_usec) / 1000;
robbiew4e8556e2004-11-22 17:08:16 +0000113 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000114 delta =
115 (suseconds_t) (tp.tv_sec - tp2.tv_sec) * 1000 +
116 (tp.tv_usec - tp2.tv_usec) / 1000;
robbiew4e8556e2004-11-22 17:08:16 +0000117 }
subrata_modak56207ce2009-03-23 13:35:39 +0000118
119 if (delta > -ACCEPTABLE_DELTA && delta < ACCEPTABLE_DELTA) {
plars865695b2001-08-27 22:15:12 +0000120 tst_resm(TPASS, "Test condition %d successful",
121 condition_number++);
122 } else {
123 tst_resm(TFAIL, "Test condition %d failed",
124 condition_number++);
125 }
126
127 /* Invalid Args : Error Condition where tp = NULL */
128 TEST(settimeofday((struct timeval *)-1, NULL));
129 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000130 tst_resm(TPASS, "Test condition %d successful",
131 condition_number++);
132 } else {
133 tst_resm(TFAIL, "Test condition %d failed",
134 condition_number);
135 }
136
137 }
138 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800139 tst_exit();
robbiewfa451a12003-03-27 20:52:36 +0000140
plars865695b2001-08-27 22:15:12 +0000141}
142
vapiercc0d4502006-02-27 04:36:28 +0000143#else
144
Mike Frysingerc57fba52014-04-09 18:56:30 -0400145int main(void)
vapiercc0d4502006-02-27 04:36:28 +0000146{
147 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800148 tst_exit();
vapiercc0d4502006-02-27 04:36:28 +0000149}
150
151#endif /* if !defined(UCLINUX) */
152
plars865695b2001-08-27 22:15:12 +0000153/*
154 * setup()
155 * performs all ONE TIME setup for this test
156 */
subrata_modak56207ce2009-03-23 13:35:39 +0000157void setup(void)
plars865695b2001-08-27 22:15:12 +0000158{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200159 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800160
plars865695b2001-08-27 22:15:12 +0000161 tst_sig(FORK, DEF_HANDLER, cleanup);
162
plars865695b2001-08-27 22:15:12 +0000163 /* Pause if that option was specified
164 * TEST_PAUSE contains the code to fork the test with the -c option.
165 */
166 TEST_PAUSE;
167
168 /* Save the current time values */
169 if ((gettimeofday(&tp, (struct timezone *)&tp1)) == -1) {
170 tst_brkm(TBROK, cleanup, "gettimeofday failed. "
subrata_modak56207ce2009-03-23 13:35:39 +0000171 "errno=%d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 }
plars865695b2001-08-27 22:15:12 +0000173 save_tv_sec = tp.tv_sec;
174 save_tv_usec = tp.tv_usec;
175}
176
177/*
178 * cleanup()
179 * performs all ONE TIME cleanup for this test at
180 * completion or premature exit
181 */
subrata_modak56207ce2009-03-23 13:35:39 +0000182void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000183{
184 /* restore the original time values. */
185 tp.tv_sec = save_tv_sec;
186 tp.tv_usec = save_tv_usec;
187 if ((settimeofday(&tp, NULL)) == -1) {
188 tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK");
subrata_modak56207ce2009-03-23 13:35:39 +0000189 tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
plars865695b2001-08-27 22:15:12 +0000190 }
191
Wanlong Gao354ebb42012-12-07 10:10:04 +0800192}