• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
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>
61 #include "test.h"
62 
63 #define	FAILED		1
64 #define	VAL_SEC		100
65 #define	VAL_MSEC	100
66 #define ACCEPTABLE_DELTA	500	/* in milli-seconds */
67 #define USEC_PER_SEC    1000000L
68 
69 char *TCID = "settimeofday01";
70 int TST_TOTAL = 1;
71 time_t save_tv_sec, save_tv_usec;
72 struct timeval tp, tp1, tp2;
73 
74 void setup(void);
75 void cleanup(void);
76 
77 #if !defined(UCLINUX)
78 
main(int argc,char ** argv)79 int main(int argc, char **argv)
80 {
81 	int lc;
82 	suseconds_t delta;
83 
84 	tst_parse_opts(argc, argv, NULL, NULL);
85 
86 	setup();
87 
88 	for (lc = 0; TEST_LOOPING(lc); lc++) {
89 		int condition_number = 1;
90 		/* reset tst_count in case we are looping */
91 		tst_count = 0;
92 
93 		gettimeofday(&tp, NULL);
94 		tp.tv_sec += VAL_SEC;
95 		tp.tv_usec += VAL_MSEC;
96 		if (tp.tv_usec >= USEC_PER_SEC)
97 			tp.tv_usec = VAL_MSEC;
98 
99 		TEST(settimeofday(&tp, NULL));
100 		if (TEST_RETURN == -1) {
101 			tst_resm(TFAIL, "Error Setting Time, errno=%d",
102 				 TEST_ERRNO);
103 		}
104 
105 		if ((gettimeofday(&tp2, (struct timezone *)&tp1)) == -1) {
106 			tst_resm(TBROK, "Error Getting Time, errno=%d", errno);
107 		}
108 
109 		if (tp2.tv_sec > tp.tv_sec) {
110 			delta =
111 			    (suseconds_t) (tp2.tv_sec - tp.tv_sec) * 1000 +
112 			    (tp2.tv_usec - tp.tv_usec) / 1000;
113 		} else {
114 			delta =
115 			    (suseconds_t) (tp.tv_sec - tp2.tv_sec) * 1000 +
116 			    (tp.tv_usec - tp2.tv_usec) / 1000;
117 		}
118 
119 		if (delta > -ACCEPTABLE_DELTA && delta < ACCEPTABLE_DELTA) {
120 			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) {
130 			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();
139 	tst_exit();
140 
141 }
142 
143 #else
144 
main(void)145 int main(void)
146 {
147 	tst_resm(TINFO, "test is not available on uClinux");
148 	tst_exit();
149 }
150 
151 #endif /* if !defined(UCLINUX) */
152 
153 /*
154  * setup()
155  *	performs all ONE TIME setup for this test
156  */
setup(void)157 void setup(void)
158 {
159 	tst_require_root();
160 
161 	tst_sig(FORK, DEF_HANDLER, cleanup);
162 
163 	/* 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. "
171 			 "errno=%d", errno);
172 	}
173 	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  */
cleanup(void)182 void cleanup(void)
183 {
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");
189 		tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
190 	}
191 
192 }
193