1433d6423SLionel Sambuc /* Test 69. clock_getres(), clock_gettime(), clock_settime(), adjtime().
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Note, any type of ntpd or software that calls adjtime() or settimeofday()
4433d6423SLionel Sambuc * should be disabled while running this test. This test takes ~40s to run.
5433d6423SLionel Sambuc */
6433d6423SLionel Sambuc
7433d6423SLionel Sambuc #include <time.h>
8433d6423SLionel Sambuc #include <sys/types.h>
9433d6423SLionel Sambuc #include <errno.h>
10433d6423SLionel Sambuc #include <fcntl.h>
11433d6423SLionel Sambuc #include <stdlib.h>
12433d6423SLionel Sambuc #include <unistd.h>
13433d6423SLionel Sambuc #include <stdio.h>
14433d6423SLionel Sambuc
15433d6423SLionel Sambuc #define TRIALS 100
16433d6423SLionel Sambuc int max_error = 4;
17433d6423SLionel Sambuc #include "common.h"
18433d6423SLionel Sambuc
19433d6423SLionel Sambuc #ifndef DEBUG
20433d6423SLionel Sambuc #define DEBUG 0
21433d6423SLionel Sambuc #endif
22433d6423SLionel Sambuc
23433d6423SLionel Sambuc int subtest = 1;
24433d6423SLionel Sambuc
25433d6423SLionel Sambuc
26433d6423SLionel Sambuc int main(void);
27433d6423SLionel Sambuc void quit(void);
28*7c48de6cSDavid van Moolenbroek static void test_clock_getres(void);
29*7c48de6cSDavid van Moolenbroek static void test_clock_gettime(void);
30*7c48de6cSDavid van Moolenbroek static void test_clock_settime(void);
31*7c48de6cSDavid van Moolenbroek static void test_adjtime(void);
32433d6423SLionel Sambuc static void show_timespec(char *msg, struct timespec *ts);
33433d6423SLionel Sambuc
test_clock_getres(void)34*7c48de6cSDavid van Moolenbroek static void test_clock_getres(void)
35433d6423SLionel Sambuc {
36433d6423SLionel Sambuc struct timespec res;
37433d6423SLionel Sambuc
38433d6423SLionel Sambuc /* valid clock_id should succeed, invalid clock_id should fail */
39433d6423SLionel Sambuc if (clock_getres(CLOCK_REALTIME, &res) == -1) e(10);
40433d6423SLionel Sambuc if (res.tv_sec < 0 || res.tv_nsec < 0) e(11);
41433d6423SLionel Sambuc show_timespec("res(CLOCK_REALTIME)", &res);
42433d6423SLionel Sambuc
43433d6423SLionel Sambuc if (clock_getres(CLOCK_MONOTONIC, &res) == -1) e(12);
44433d6423SLionel Sambuc if (res.tv_sec < 0 || res.tv_nsec < 0) e(13);
45433d6423SLionel Sambuc show_timespec("res(CLOCK_MONOTONIC)", &res);
46433d6423SLionel Sambuc
47433d6423SLionel Sambuc if (clock_getres(-1, &res) == 0) e(14);
48433d6423SLionel Sambuc }
49433d6423SLionel Sambuc
test_clock_gettime(void)50*7c48de6cSDavid van Moolenbroek static void test_clock_gettime(void)
51433d6423SLionel Sambuc {
52433d6423SLionel Sambuc struct timespec ts, ts2;
53433d6423SLionel Sambuc
54433d6423SLionel Sambuc /* valid clock_id should succeed, invalid clock_id should fail */
55433d6423SLionel Sambuc if (clock_gettime(CLOCK_REALTIME, &ts) == -1) e(21);
56433d6423SLionel Sambuc if (ts.tv_sec < 0 || ts.tv_nsec < 0) e(22);
57433d6423SLionel Sambuc show_timespec("time(CLOCK_REALTIME)", &ts);
58433d6423SLionel Sambuc sleep(2);
59433d6423SLionel Sambuc if (clock_gettime(CLOCK_REALTIME, &ts2) == -1) e(23);
60433d6423SLionel Sambuc if (ts2.tv_sec < 0 || ts2.tv_nsec < 0) e(24);
61433d6423SLionel Sambuc if (ts2.tv_sec <= ts.tv_sec) e(25);
62433d6423SLionel Sambuc
63433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) e(26);
64433d6423SLionel Sambuc if (ts.tv_sec < 0 || ts.tv_nsec < 0) e(27);
65433d6423SLionel Sambuc show_timespec("time(CLOCK_MONOTONIC)", &ts);
66433d6423SLionel Sambuc sleep(2);
67433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &ts2) == -1) e(28);
68433d6423SLionel Sambuc if (ts2.tv_sec < 0 || ts2.tv_nsec < 0) e(29);
69433d6423SLionel Sambuc if (ts2.tv_sec <= ts.tv_sec) e(30);
70433d6423SLionel Sambuc
71433d6423SLionel Sambuc if (clock_gettime(-1, &ts) == 0) e(31);
72433d6423SLionel Sambuc }
73433d6423SLionel Sambuc
test_clock_settime(void)74433d6423SLionel Sambuc static void test_clock_settime(void)
75433d6423SLionel Sambuc {
76433d6423SLionel Sambuc struct timespec ts;
77433d6423SLionel Sambuc struct timespec ts2;
78433d6423SLionel Sambuc
79433d6423SLionel Sambuc /* shouldn't be able to set MONOTONIC */
80433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) e(50);
81433d6423SLionel Sambuc if (clock_settime(CLOCK_MONOTONIC, &ts) == 0) e(51);
82433d6423SLionel Sambuc if (errno != EINVAL) e(52); /* reponse should be EINVAL */
83433d6423SLionel Sambuc
84433d6423SLionel Sambuc /* set the time of REALTIME to that of MONOTONIC */
85433d6423SLionel Sambuc if (clock_settime(CLOCK_REALTIME, &ts) == -1) e(53);
86433d6423SLionel Sambuc
87433d6423SLionel Sambuc ts.tv_sec += 600; /* time travel 10 minutes into the future */
88433d6423SLionel Sambuc if (clock_gettime(CLOCK_REALTIME, &ts2) == -1) e(54);
89433d6423SLionel Sambuc if (clock_settime(CLOCK_REALTIME, &ts) == -1) e(55);
90433d6423SLionel Sambuc if (clock_gettime(CLOCK_REALTIME, &ts) == -1) e(56);
91433d6423SLionel Sambuc
92433d6423SLionel Sambuc /* get the value we set, if it's not about 10 minutes ahead, it failed */
93433d6423SLionel Sambuc if (ts.tv_sec - ts2.tv_sec < 500 ||
94433d6423SLionel Sambuc ts.tv_sec - ts2.tv_sec > 700) e(57);
95433d6423SLionel Sambuc
96433d6423SLionel Sambuc /* back to current time - don't leave the system time 10 ahead */
97433d6423SLionel Sambuc if (clock_gettime(CLOCK_REALTIME, &ts) == -1) e(58);
98433d6423SLionel Sambuc ts.tv_sec -= 600;
99433d6423SLionel Sambuc if (clock_settime(CLOCK_REALTIME, &ts) == -1) e(59);
100433d6423SLionel Sambuc
101433d6423SLionel Sambuc /* Test with an invalid clock */
102433d6423SLionel Sambuc if (clock_settime(-1, &ts) == 0) e(60);
103433d6423SLionel Sambuc }
104433d6423SLionel Sambuc
test_adjtime(void)105433d6423SLionel Sambuc static void test_adjtime(void)
106433d6423SLionel Sambuc {
107433d6423SLionel Sambuc struct timeval delta, olddelta;
108433d6423SLionel Sambuc struct timespec rt, mt;
109433d6423SLionel Sambuc
110433d6423SLionel Sambuc /* set the realtime clock to the same value as the monotonic clock */
111433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &mt) == -1) e(65);
112433d6423SLionel Sambuc if (clock_settime(CLOCK_REALTIME, &mt) == -1) e(66);
113433d6423SLionel Sambuc
114433d6423SLionel Sambuc delta.tv_sec = 7;
115433d6423SLionel Sambuc delta.tv_usec = 0;
116433d6423SLionel Sambuc
117433d6423SLionel Sambuc if (adjtime(&delta, &olddelta) != 0) e(70); /* adjust +7 seconds */
118433d6423SLionel Sambuc sleep(15); /* should take 14 seconds to adjust the clock */
119433d6423SLionel Sambuc
120433d6423SLionel Sambuc /* check that the 7 second adjustment puts us between 5 and 10 seconds
121433d6423SLionel Sambuc * ahead of the monotonic clock.
122433d6423SLionel Sambuc */
123433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &mt) == -1) e(71);
124433d6423SLionel Sambuc if (clock_gettime(CLOCK_REALTIME, &rt) == -1) e(72);
125433d6423SLionel Sambuc show_timespec("Monotonic", &mt);
126433d6423SLionel Sambuc show_timespec("Realtime (+7)", &rt);
127433d6423SLionel Sambuc if (rt.tv_sec - 5 < mt.tv_sec || rt.tv_sec - 10 > mt.tv_sec) e(73);
128433d6423SLionel Sambuc
129433d6423SLionel Sambuc delta.tv_sec = -7;
130433d6423SLionel Sambuc if (adjtime(&delta, &olddelta) != 0) e(73); /* adjust -7 seconds */
131433d6423SLionel Sambuc sleep(15); /* should take 14 seconds to adjust the clock */
132433d6423SLionel Sambuc
133433d6423SLionel Sambuc /* check that the 7 second adjustment puts us close to even with
134433d6423SLionel Sambuc * the monotonic clock.
135433d6423SLionel Sambuc */
136433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &mt) == -1) e(74);
137433d6423SLionel Sambuc if (clock_gettime(CLOCK_REALTIME, &rt) == -1) e(75);
138433d6423SLionel Sambuc show_timespec("Monotonic", &mt);
139433d6423SLionel Sambuc show_timespec("Realtime (-7)", &rt);
140433d6423SLionel Sambuc if (abs(rt.tv_sec - mt.tv_sec) > 5) e(76);
141433d6423SLionel Sambuc
142433d6423SLionel Sambuc }
143433d6423SLionel Sambuc
show_timespec(char * msg,struct timespec * ts)144433d6423SLionel Sambuc static void show_timespec(char *msg, struct timespec *ts)
145433d6423SLionel Sambuc {
146433d6423SLionel Sambuc #if DEBUG == 1
147433d6423SLionel Sambuc printf("[%s] tv_sec=%d tv_nsec=%ld\n", msg, ts->tv_sec, ts->tv_nsec);
148433d6423SLionel Sambuc #endif /* DEBUG == 1 */
149433d6423SLionel Sambuc }
150433d6423SLionel Sambuc
main(void)151*7c48de6cSDavid van Moolenbroek int main(void)
152433d6423SLionel Sambuc {
153433d6423SLionel Sambuc start(69);
154433d6423SLionel Sambuc struct timespec starttime, endtime;
155433d6423SLionel Sambuc
156433d6423SLionel Sambuc /* get test start time */
157433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &starttime) == -1) e(1);
158433d6423SLionel Sambuc
159433d6423SLionel Sambuc test_clock_getres();
160433d6423SLionel Sambuc test_clock_gettime();
161433d6423SLionel Sambuc test_clock_settime();
162433d6423SLionel Sambuc test_adjtime();
163433d6423SLionel Sambuc
164433d6423SLionel Sambuc /* get test end time */
165433d6423SLionel Sambuc if (clock_gettime(CLOCK_MONOTONIC, &endtime) == -1) e(2);
166433d6423SLionel Sambuc
167433d6423SLionel Sambuc /* we shouldn't have gone backwards in time during this test */
168433d6423SLionel Sambuc if ((starttime.tv_sec > endtime.tv_sec) ||
169433d6423SLionel Sambuc (starttime.tv_sec == endtime.tv_sec &&
170433d6423SLionel Sambuc starttime.tv_nsec > endtime.tv_nsec)) e(3);
171433d6423SLionel Sambuc
172433d6423SLionel Sambuc quit();
173433d6423SLionel Sambuc return(-1); /* impossible */
174433d6423SLionel Sambuc }
175433d6423SLionel Sambuc
176