Debian Codegraph @

ulib/clock.c

raw
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4
5 #include "ulib/clock.h"
6 #include "ulib/chacha.h"
7 #include "ulib/gettime.h"
8
9 #ifdef __cplusplus
10 }
11 #endif
12
13 #ifdef USE_WIN32_NATIVE
14 #define ftruncate(a,b) _chsize(a,b)
15 #endif
16 #undef open
17
18 #define state_fd UCXT.clock_state_fd
19 #define state_f UCXT.clock_state_f
20 #define thread_persist UCXT.clock_persist
21 #define adjustment SMEM->clock_adj
22 #define last SMEM->clock_last
23 #define clock_seq SMEM->clock_seq
24 #define prev_reg SMEM->clock_prev_reg
25 #define defer_100ns SMEM->clock_defer_100ns
26 #define global_persist SMEM->clock_persist
27
28 /* state_fd:
29 * -4 cannot create
30 * -3 untried
31 * -2 symlink
32 * -1 can create
33 * >=0 open fd
34 */
35 #define STATEFD_NOCREATE -4
36 #define STATEFD_UNTRIED -3
37 #define STATEFD_SYMLINK -2
38 #define STATEFD_CREATEOK -1
39
40
41 /* compare paths, return 0 if same or longer length if not */
42 static IV persistNE(persist_t *a, persist_t *b) {
43 STRLEN longer = a->len >= b->len ? a->len : b->len;
44 UV i;
45 if (a->len != b->len)
46 return longer;
47 for (i=0 ; i<longer ; ++i)
48 if(a->path[i] != b->path[i])
49 return longer;
50 return 0;
51 }
52
53 /* called at boot */
54 void uu_clock_init(pUCXT) {
55 UV ptod[2];
56
57 /* gettimeofday(&tv, 0); */
58 (*uu_gettime_U2time)(aTHX_ ptod);
59 last.tv_sec = (long)ptod[0];
60 last.tv_usec = (long)ptod[1];
61
62 uu_chacha_rand16(aUCXT, &clock_seq);
63 clock_seq &= 0x3fff;
64
65 state_fd = STATEFD_UNTRIED;
66 state_f = NULL;
67 adjustment = 0;
68 prev_reg = 0;
69 defer_100ns = 0;
70 Zero(&global_persist, 1, persist_t);
71 Zero(&thread_persist, 1, persist_t);
72 }
73
74 void uu_clock_getpath(pUCXT, persist_t *persist) {
75 Copy(&global_persist, &thread_persist, 1, persist_t);
76 Copy(&thread_persist, persist, 1, persist_t);
77 }
78
79 void uu_clock_setpath(pUCXT, persist_t *persist) {
80 Copy(persist, &thread_persist, 1, persist_t);
81 Copy(persist, &global_persist, 1, persist_t);
82 if (state_fd >= 0)
83 fclose(state_f);
84 state_fd = STATEFD_UNTRIED;
85 /* doing this would break inter-process deferrals */
86 /* prev_reg = 0; */
87 }
88
89 /* returns 100ns intervals since unix epoch.
90 * since gettimeofday() is in 1usec intervals,
91 * last digit is simulated via adjustment.
92 */
93 IV uu_clock(pUCXT, U64 *ret_clock_reg, U16 *ret_clock_seq) {
94 struct timeval tv;
95 mode_t save_umask;
96 U64 clock_reg;
97 STRLEN longer;
98 UV ptod[2];
99 #ifdef HAVE_LSTAT
100 struct stat statbuf;
101 #endif
102
103 if ((longer = persistNE(&global_persist, &thread_persist))) {
104 Copy(&global_persist, &thread_persist, longer, UCHAR);
105 if (state_fd >= 0)
106 fclose(state_f);
107 state_fd = STATEFD_UNTRIED;
108 }
109
110 if (state_fd == STATEFD_UNTRIED) {
111 #ifdef HAVE_LSTAT
112 if (lstat((char*)thread_persist.path, &statbuf) < 0) { /* this covers EINTR too.. ugh */
113 if (errno == ENOENT)
114 state_fd = STATEFD_CREATEOK;
115 else
116 state_fd = STATEFD_NOCREATE;
117 }
118 else if ((statbuf.st_mode & S_IFMT) == S_IFLNK)
119 state_fd = STATEFD_SYMLINK;
120 else {
121 #endif
122 state_fd = open((char*)thread_persist.path, O_RDWR);
123 if (state_fd < 0 && errno == ENOENT)
124 state_fd = STATEFD_CREATEOK;
125 else if (state_fd >= 0) {
126 #ifdef HAVE_LSTAT
127 state_f = NULL;
128 if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK))
129 #endif
130 state_f = fdopen(state_fd, "r+");
131 if (!state_f) {
132 close(state_fd);
133 state_fd = STATEFD_SYMLINK;
134 }
135 }
136 #ifdef HAVE_LSTAT
137 }
138 #endif
139 }
140
141 if (state_fd >= 0) {
142 unsigned int cl;
143 unsigned long tv1, tv2;
144 int a;
145
146 rewind(state_f);
147
148 if (fscanf(state_f, "clock: %04x tv: %lu %lu adj: %d\n", &cl, &tv1, &tv2, &a) == 4) {
149 clock_seq = cl & 0x3fff;
150 last.tv_sec = tv1;
151 last.tv_usec = tv2;
152 adjustment = a;
153 }
154 }
155
156 /* gettimeofday(&tv, 0); */
157 (*uu_gettime_U2time)(aTHX_ ptod);
158 tv.tv_sec = (long)ptod[0];
159 tv.tv_usec = (long)ptod[1];
160
161 if ((tv.tv_sec == last.tv_sec) && (tv.tv_usec == last.tv_usec)) {
162 /* same time as last */
163 if (++adjustment >= MAX_ADJUSTMENT) {
164 clock_seq = (clock_seq+1) & 0x3fff;
165 adjustment = 0;
166 }
167 }
168 else {
169 /* time changed */
170 if (((tv.tv_sec == last.tv_sec) && (tv.tv_usec < last.tv_usec)) || (tv.tv_sec < last.tv_sec))
171 clock_seq = (clock_seq+1) & 0x3fff; /* time moved backward */
172 adjustment = 0;
173 last = tv;
174 }
175
176 clock_reg = tv.tv_usec*10 + adjustment;
177 clock_reg += ((U64)tv.tv_sec)*10000000;
178 /* *clock_reg += (((U64)0x01b21dd2) << 32) + 0x13814000; */
179
180 if ((clock_reg - prev_reg) >= defer_100ns) {
181 if (state_fd == STATEFD_CREATEOK) {
182 #ifdef HAVE_LSTAT
183 if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) == S_IFLNK))
184 state_fd = STATEFD_SYMLINK;
185 else {
186 #endif
187 save_umask = umask(0);
188 state_fd = open((char*)thread_persist.path, O_RDWR|O_CREAT, 0660);
189 if (state_fd < 0)
190 state_fd = STATEFD_NOCREATE;
191 (void) umask(save_umask);
192 if (state_fd >= 0) {
193 #ifdef HAVE_LSTAT
194 state_f = NULL;
195 if ((lstat((char*)thread_persist.path, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFLNK))
196 #endif
197 state_f = fdopen(state_fd, "r+");
198 if (!state_f) {
199 close(state_fd);
200 state_fd = STATEFD_SYMLINK;
201 }
202 }
203 #ifdef HAVE_LSTAT
204 }
205 #endif
206 }
207
208 if (state_fd > 0) {
209 rewind(state_f);
210 long len = fprintf(state_f,
211 "clock: %04x tv: %016lu %08lu adj: %08d\n",
212 clock_seq, (unsigned long)last.tv_sec,
213 (unsigned long)last.tv_usec, adjustment);
214 fflush(state_f);
215 if (ftruncate(state_fd, len) < 0) {
216 fprintf(state_f, " \n");
217 fflush(state_f);
218 }
219 rewind(state_f);
220 }
221 }
222
223 prev_reg = clock_reg;
224
225 /* *clock_high = clock_reg >> 32; */
226 /* *clock_low = (U32)clock_reg; */
227 *ret_clock_reg = clock_reg;
228 *ret_clock_seq = clock_seq;
229 return 0;
230 }
231
232 /* ex:set ts=2 sw=2 itab=spaces: */