-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime2posix.c
433 lines (371 loc) · 10.2 KB
/
time2posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* 2014 by Marek Behun <[email protected]>
This file is in public domain */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <endian.h>
#include "time2posix.h"
#define RIGHT_TZ "zoneinfo-leaps/UTC"
#define LEAP_TZFILE ("/usr/share/" RIGHT_TZ)
struct leapsecond *t2p_leapsecs = NULL;
size_t t2p_leapsecs_num = 0;
time_t t2p_last_read = 0;
/* read the leap seconds table */
int
t2p_leaps_read (void)
{
int fd, prev_change;
const char *buf;
const u_int32_t *lbuf;
u_int32_t n, skip, i;
struct leapsecond *ptr, *leapsecs;
time_t now;
/* we don't need to re-read every time
(keep the same table at least for 30 days) */
now = t2p_orig_time (NULL);
if (now < (t2p_last_read + 86400*30))
return 0;
/* does O_NONBLOCK need to be here if we are mmapping? */
fd = open (LEAP_TZFILE, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (fd < 0)
{
fprintf (stderr, "time2posix error: Cannot open %s !\n", LEAP_TZFILE);
return -1;
}
/* right/UTC shouldn't be bigger than 4096B */
buf = mmap (NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
close (fd);
if (buf == MAP_FAILED)
{
fprintf (stderr, "time2posix error: Cannot mmap %s !\n", LEAP_TZFILE);
return (-1);
}
/* check the header */
if (memcmp (buf, (const void *) "TZif", 4))
{
fprintf (stderr, "time2posix error: Invalid %s header !\n", LEAP_TZFILE);
goto err;
}
lbuf = (const u_int32_t *) buf;
/* number of leap seconds in database */
n = be32toh (lbuf[7]);
if (n == 0)
{
fprintf (stderr, "time2posix warning: No leap seconds in %s !%s\n", LEAP_TZFILE,
t2p_leapsecs != NULL ? " Using old table." : "");
goto end;
}
leapsecs = malloc (n * sizeof (struct leapsecond));
if (leapsecs == NULL)
{
fprintf (stderr, "time2posix error: Cannot allocate space for leap seconds table!\n");
goto err;
}
/* skip unimportant stuff */
skip = 5*be32toh (lbuf[8]) + 6*be32toh (lbuf[9]) + be32toh (lbuf[10]);
lbuf = (const u_int32_t *) (buf + 44 + skip);
prev_change = 0;
for (i = 0, ptr = leapsecs; i < n; i++, ptr++)
{
time_t transition, posix_transition, daystart, posix_daystart;
int change, type;
transition = (time_t) be32toh (*lbuf++);
change = (int) be32toh (*lbuf++);
type = change > prev_change;
/* inserted is always 86400th second of the day,
but deleted should be 86399th */
daystart = transition - 86400 + !type;
posix_daystart = daystart - prev_change;
ptr->type = type;
/* and we want to prolong the 23:59:59, not 00:00:00, if inserting
(therefore subtracting one second if type == 1) */
ptr->transition = transition - type;
ptr->posix_transition = ptr->transition - prev_change;
ptr->daystart = daystart;
ptr->posix_daystart = posix_daystart;
ptr->prev_change = prev_change;
ptr->change = change;
prev_change = change;
}
/* this should be protected by some rw lock */
if (t2p_leapsecs != NULL)
free (t2p_leapsecs);
t2p_leapsecs = leapsecs;
t2p_leapsecs_num = n;
t2p_last_read = now;
end:
munmap ((void *) buf, 4096);
return 0;
err:
munmap ((void *) buf, 4096);
return -1;
}
/* Convert right timestamp to a posix timestamp.
If a leap second is being inserted at t+1, *state is set to 1.
If a leap second is being inesrted at t, *state is set to 2.
If a leap second is being deleted at t+1, *state is set to -1.
Otherwise, *state is set to 0.
Example:
time time2posix state
1341100821 1341100797 0
1341100822 1341100798 0
1341100823 1341100799 1
1341100824 1341100799 2
1341100825 1341100800 0
1341100826 1341100801 0
*/
time_t
t2p_time2posix (time_t t, int *state)
{
time_t res = t;
size_t i = t2p_leapsecs_num;
struct leapsecond *ptr;
/* reread leap seconds table sometimes */
t2p_leaps_read ();
do
if (i-- == 0)
return res;
while (t < t2p_leapsecs[i].transition);
ptr = t2p_leapsecs + i;
res = t - ptr->change;
if (ptr->type && t-1 == ptr->transition)
*state = 2;
else if (ptr->type && t == ptr->transition)
{
*state = 1;
res++;
}
else if (!ptr->type && t == ptr->transition)
{
*state = -1;
res--;
}
else
*state = 0;
return res;
}
/* Convert a posix timestamp to a right timestamp.
If a leap second is being inserted at t+1, *state is set to 1.
If a leap second is being deleted at t+1, *state is set to -1.
If a leap second is being deleted at t, *state is set to -2.
Otherwise, *state is set to 0.
Example:
time posix2time state
1341100797 1341100821 0
1341100798 1341100822 0
1341100799 1341100823 1
1341100800 1341100825 0
1341100801 1341100826 0
*/
time_t
t2p_posix2time (time_t t, int *state)
{
time_t res = t;
size_t i = t2p_leapsecs_num;
struct leapsecond *ptr;
/* reread leap seconds table sometimes */
t2p_leaps_read ();
do
if (i-- == 0)
return t;
while (t < t2p_leapsecs[i].posix_transition);
ptr = t2p_leapsecs + i;
res = t + ptr->change;
if (ptr->type && t == ptr->posix_transition)
{
*state = 1;
res--;
}
else if (!ptr->type && t == ptr->posix_transition)
{
*state = -1;
res++;
}
else if (!ptr->type && t-1 == ptr->posix_transition)
*state = -2;
else
*state = 0;
return res;
}
/* normalize struct timeval */
inline struct timeval *
t2p_normalize_timeval (struct timeval *tv)
{
while (tv->tv_usec >= 1000000)
{
tv->tv_usec -= 1000000;
tv->tv_sec ++;
}
return tv;
}
/* Convert a right timeval to a posix timeval.
If leap second is being inserted, simulates slowdown of the 23:59:59 second.
(That means that 2 second will pass from 23:59:59 to 00:00:00).
If a leap second is being deleted, simulates speedup of the 23:59:58 second.
(That means that 1 second will pass from 23:59:58 to 00:00:00). */
struct timeval *
t2p_time2posix_timeval (struct timeval *tv)
{
int state;
tv->tv_sec = t2p_time2posix (tv->tv_sec, &state);
if (state == 1)
tv->tv_usec >>= 1;
else if (state == 2)
tv->tv_usec = (tv->tv_usec + 1000000) >> 1;
else if (state == -1)
{
tv->tv_usec <<= 1;
t2p_normalize_timeval (tv);
}
return tv;
}
/* Inverse to the previous function. */
struct timeval *
t2p_posix2time_timeval (struct timeval *tv)
{
int state;
tv->tv_sec = t2p_posix2time (tv->tv_sec, &state);
if (state == 1)
{
tv->tv_usec <<= 1;
t2p_normalize_timeval (tv);
}
else if (state == -1)
tv->tv_usec >>= 1;
else if (state == -2)
tv->tv_usec = (tv->tv_usec + 1000000) >> 1;
return tv;
}
/* normalize struct timespec */
inline struct timespec *
t2p_normalize_timespec (struct timespec *ts)
{
while (ts->tv_nsec >= 1000000000)
{
ts->tv_nsec -= 1000000000;
ts->tv_sec ++;
}
return ts;
}
/* struct timespec version of t2p_time2posix_timeval */
struct timespec *
t2p_time2posix_timespec (struct timespec *ts)
{
int state;
ts->tv_sec = t2p_time2posix (ts->tv_sec, &state);
if (state == 1)
ts->tv_nsec >>= 1;
else if (state == 2)
ts->tv_nsec = (ts->tv_nsec + 1000000000) >> 1;
else if (state == -1)
{
ts->tv_nsec <<= 1;
t2p_normalize_timespec (ts);
}
return ts;
}
/* struct timespec version of t2p_posix2time_timeval */
struct timespec *
t2p_posix2time_timespec (struct timespec *ts)
{
int state;
ts->tv_sec = t2p_posix2time (ts->tv_sec, &state);
if (state == 1)
{
ts->tv_nsec <<= 1;
t2p_normalize_timespec (ts);
}
else if (state == -1)
ts->tv_nsec >>= 1;
else if (state == -2)
ts->tv_nsec = (ts->tv_nsec + 1000000000) >> 1;
return ts;
}
/* simulates adjtimex() return value when inserting or deleting a leap second.
If a leap second will be inserted at the end of the day, return TIME_INS.
If a leap second is being inserted now, return TIME_OOP.
If a leap second was inserted in the previous second, return TIME_WAIT.
If a leap second will be deleted at the end of the day, return TIME_DEL.
If a leap second was deleted in the previous second, return TIME_WAIT.
Otherwise return TIME_OK. */
int
t2p_timestatus (time_t t)
{
struct leapsecond *ptr;
for (ptr = t2p_leapsecs + t2p_leapsecs_num - 1; ptr >= t2p_leapsecs; ptr--)
{
if (ptr->type)
{
if (t > ptr->transition + 2)
return TIME_OK;
else if (t > ptr->transition + 1)
return TIME_WAIT;
else if (t >= ptr->transition)
return TIME_OOP;
else if (t >= ptr->daystart)
return TIME_INS;
}
else
{
if (t > ptr->transition + 1)
return TIME_OK;
else if (t > ptr->transition)
return TIME_WAIT;
else if (t >= ptr->daystart)
return TIME_DEL;
}
}
return TIME_OK;
}
static void *t2p_libc_handle = NULL;
static void t2p_init (void) __attribute__((constructor));
static void t2p_fini (void) __attribute__((destructor));
/* initialize leap seconds table and original function pointers */
static void
t2p_init (void)
{
t2p_libc_handle = dlopen ("libc.so.6", RTLD_LAZY);
if (!t2p_libc_handle)
exit (255);
#define fetchsymbol(name) \
t2p_orig_##name = dlsym (t2p_libc_handle, #name); \
if (t2p_orig_##name == NULL) \
fprintf (stderr, "time2posix error: Cannot load symbol " #name " from libc.so.6!\n")
fetchsymbol(getutent);
fetchsymbol(getutid);
fetchsymbol(getutline);
fetchsymbol(pututline);
fetchsymbol(updwtmp);
fetchsymbol(getutent_r);
fetchsymbol(getutid_r);
fetchsymbol(getutline_r);
fetchsymbol(getutxent);
fetchsymbol(getutxid);
fetchsymbol(getutxline);
fetchsymbol(pututxline);
fetchsymbol(updwtmpx);
fetchsymbol(time);
fetchsymbol(stime);
fetchsymbol(clock_gettime);
fetchsymbol(clock_settime);
fetchsymbol(clock_adjtime);
fetchsymbol(gettimeofday);
fetchsymbol(settimeofday);
fetchsymbol(adjtimex);
fetchsymbol(ntp_adjtime);
fetchsymbol(ntp_gettime);
fetchsymbol(recvmsg);
if (t2p_leaps_read ())
exit (255);
}
static void
t2p_fini (void)
{
if (t2p_libc_handle != NULL)
dlclose (t2p_libc_handle);
}