1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Created by: julie.n.fleischer REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license. For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7 *
8 * Test having two processes attempt to set the clock at the same time.
9 * Ensure that both actually set the clock, and it is the later one that
10 * takes effect. [Note: It would be hard to test that they both set
11 * the clock without setting up atomic operations. Will just test that
12 * at least one set took place.]
13 * The two processes will attempt to set the clock to TESTTIME+DELTA
14 * and TESTTIME-DELTA.
15 *
16 * The clock_id chosen for this test is CLOCK_REALTIME.
17 * The date chosen is Nov 12, 2002 ~11:13am.
18 */
19 #include <stdio.h>
20 #include <time.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include "posixtest.h"
25
26 #define TESTTIME 1037128358
27 #define DELTA 5
28 #define ACCEPTABLEDELTA 1
29 #define LONGTIME 3 //== long enough for both clocks to be set
30
main(int argc,char * argv[])31 int main(int argc, char *argv[])
32 {
33 struct timespec tpget, tsreset;
34 int pid, delta;
35
36 if (clock_gettime(CLOCK_REALTIME, &tsreset) != 0) {
37 perror("clock_getime() did not return success\n");
38 return PTS_UNRESOLVED;
39 }
40
41 if ((pid = fork()) == 0) {
42 /*child */
43 struct timespec tschild;
44
45 tschild.tv_sec = TESTTIME + DELTA;
46 tschild.tv_nsec = 0;
47 if (clock_settime(CLOCK_REALTIME, &tschild) != 0) {
48 printf("Note: clock_settime() failed\n");
49 }
50 if (clock_gettime(CLOCK_REALTIME, &tpget) == -1) {
51 printf("Note: Error in clock_gettime()\n");
52 }
53
54 } else {
55 /*parent */
56 struct timespec tsparent;
57 int pass = 0;
58
59 tsparent.tv_sec = TESTTIME - DELTA;
60 tsparent.tv_nsec = 0;
61 if (clock_settime(CLOCK_REALTIME, &tsparent) != 0) {
62 printf("Note: clock_settime() failed\n");
63 }
64
65 sleep(LONGTIME);
66
67 /*
68 * Ensure we set clock to TESTTIME-DELTA or TESTTIME+DELTA.
69 * Assume that clock increased monotonically and clock_gettime,
70 * clock_settime return correct values.
71 */
72
73 if (clock_gettime(CLOCK_REALTIME, &tpget) == -1) {
74 printf("Note: Error in clock_gettime()\n");
75 }
76
77 delta = (tpget.tv_sec - LONGTIME) - TESTTIME;
78
79 if ((delta <= ACCEPTABLEDELTA - DELTA) ||
80 (delta <= ACCEPTABLEDELTA + DELTA)) {
81 pass = 1;
82 }
83
84 if (clock_settime(CLOCK_REALTIME, &tsreset) != 0) {
85 printf("Need to manually reset time\n");
86 }
87
88 if (pass) {
89 printf("Test PASSED\n");
90 return PTS_PASS;
91 } else {
92 printf("Test FAILED\n");
93 return PTS_FAIL;
94 }
95 }
96 return PTS_UNRESOLVED;
97 }
98