]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9a98c7a1 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
9a98c7a1 LP |
6 | ***/ |
7 | ||
11c3a366 TA |
8 | #include <errno.h> |
9 | #include <limits.h> | |
10 | #include <stdlib.h> | |
9a98c7a1 | 11 | #include <string.h> |
48d26c01 | 12 | #include <sys/mman.h> |
11c3a366 TA |
13 | #include <sys/stat.h> |
14 | #include <sys/time.h> | |
77ff2de9 | 15 | #include <sys/timerfd.h> |
07630cea | 16 | #include <sys/timex.h> |
11c3a366 TA |
17 | #include <sys/types.h> |
18 | #include <unistd.h> | |
9a98c7a1 | 19 | |
b5efdb8a | 20 | #include "alloc-util.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
0d39fa9c | 22 | #include "fileio.h" |
f4f15635 | 23 | #include "fs-util.h" |
11c3a366 TA |
24 | #include "log.h" |
25 | #include "macro.h" | |
93cc7779 TA |
26 | #include "parse-util.h" |
27 | #include "path-util.h" | |
dccca82b | 28 | #include "process-util.h" |
07630cea | 29 | #include "string-util.h" |
75683450 | 30 | #include "strv.h" |
07630cea | 31 | #include "time-util.h" |
9a98c7a1 | 32 | |
32c1f5a5 LP |
33 | static clockid_t map_clock_id(clockid_t c) { |
34 | ||
35 | /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will | |
36 | * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is | |
37 | * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on | |
bdf19f8f | 38 | * those archs. */ |
32c1f5a5 LP |
39 | |
40 | switch (c) { | |
41 | ||
42 | case CLOCK_BOOTTIME_ALARM: | |
bdf19f8f | 43 | return CLOCK_BOOTTIME; |
32c1f5a5 LP |
44 | |
45 | case CLOCK_REALTIME_ALARM: | |
46 | return CLOCK_REALTIME; | |
47 | ||
48 | default: | |
49 | return c; | |
50 | } | |
51 | } | |
52 | ||
9a98c7a1 LP |
53 | usec_t now(clockid_t clock_id) { |
54 | struct timespec ts; | |
55 | ||
32c1f5a5 | 56 | assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0); |
9a98c7a1 LP |
57 | |
58 | return timespec_load(&ts); | |
59 | } | |
60 | ||
45d7a8bb LP |
61 | nsec_t now_nsec(clockid_t clock_id) { |
62 | struct timespec ts; | |
63 | ||
32c1f5a5 | 64 | assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0); |
45d7a8bb LP |
65 | |
66 | return timespec_load_nsec(&ts); | |
67 | } | |
68 | ||
9a98c7a1 LP |
69 | dual_timestamp* dual_timestamp_get(dual_timestamp *ts) { |
70 | assert(ts); | |
71 | ||
72 | ts->realtime = now(CLOCK_REALTIME); | |
73 | ts->monotonic = now(CLOCK_MONOTONIC); | |
74 | ||
75 | return ts; | |
76 | } | |
77 | ||
fe624c4c LP |
78 | triple_timestamp* triple_timestamp_get(triple_timestamp *ts) { |
79 | assert(ts); | |
80 | ||
81 | ts->realtime = now(CLOCK_REALTIME); | |
82 | ts->monotonic = now(CLOCK_MONOTONIC); | |
83 | ts->boottime = clock_boottime_supported() ? now(CLOCK_BOOTTIME) : USEC_INFINITY; | |
84 | ||
85 | return ts; | |
86 | } | |
87 | ||
9a98c7a1 LP |
88 | dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { |
89 | int64_t delta; | |
90 | assert(ts); | |
91 | ||
75a5f1d8 LP |
92 | if (u == USEC_INFINITY || u <= 0) { |
93 | ts->realtime = ts->monotonic = u; | |
cae0c5e0 LP |
94 | return ts; |
95 | } | |
96 | ||
9a98c7a1 LP |
97 | ts->realtime = u; |
98 | ||
75a5f1d8 | 99 | delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u; |
54d8ef14 | 100 | ts->monotonic = usec_sub_signed(now(CLOCK_MONOTONIC), delta); |
9a98c7a1 LP |
101 | |
102 | return ts; | |
103 | } | |
104 | ||
fe624c4c LP |
105 | triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) { |
106 | int64_t delta; | |
107 | ||
108 | assert(ts); | |
109 | ||
110 | if (u == USEC_INFINITY || u <= 0) { | |
111 | ts->realtime = ts->monotonic = ts->boottime = u; | |
112 | return ts; | |
113 | } | |
114 | ||
115 | ts->realtime = u; | |
116 | delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u; | |
54d8ef14 LP |
117 | ts->monotonic = usec_sub_signed(now(CLOCK_MONOTONIC), delta); |
118 | ts->boottime = clock_boottime_supported() ? usec_sub_signed(now(CLOCK_BOOTTIME), delta) : USEC_INFINITY; | |
fe624c4c LP |
119 | |
120 | return ts; | |
121 | } | |
122 | ||
cae0c5e0 LP |
123 | dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) { |
124 | int64_t delta; | |
125 | assert(ts); | |
126 | ||
3a43da28 KS |
127 | if (u == USEC_INFINITY) { |
128 | ts->realtime = ts->monotonic = USEC_INFINITY; | |
cae0c5e0 LP |
129 | return ts; |
130 | } | |
131 | ||
132 | ts->monotonic = u; | |
133 | delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u; | |
54d8ef14 | 134 | ts->realtime = usec_sub_signed(now(CLOCK_REALTIME), delta); |
cae0c5e0 LP |
135 | |
136 | return ts; | |
137 | } | |
138 | ||
fbe55073 LP |
139 | dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) { |
140 | int64_t delta; | |
141 | ||
142 | if (u == USEC_INFINITY) { | |
143 | ts->realtime = ts->monotonic = USEC_INFINITY; | |
144 | return ts; | |
145 | } | |
fbe55073 | 146 | |
0345d252 | 147 | dual_timestamp_get(ts); |
fbe55073 | 148 | delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u; |
54d8ef14 LP |
149 | ts->realtime = usec_sub_signed(ts->realtime, delta); |
150 | ts->monotonic = usec_sub_signed(ts->monotonic, delta); | |
fbe55073 LP |
151 | |
152 | return ts; | |
153 | } | |
154 | ||
fe624c4c LP |
155 | usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) { |
156 | ||
157 | switch (clock) { | |
158 | ||
159 | case CLOCK_REALTIME: | |
160 | case CLOCK_REALTIME_ALARM: | |
161 | return ts->realtime; | |
162 | ||
163 | case CLOCK_MONOTONIC: | |
164 | return ts->monotonic; | |
165 | ||
166 | case CLOCK_BOOTTIME: | |
167 | case CLOCK_BOOTTIME_ALARM: | |
168 | return ts->boottime; | |
169 | ||
170 | default: | |
171 | return USEC_INFINITY; | |
172 | } | |
173 | } | |
174 | ||
9a98c7a1 LP |
175 | usec_t timespec_load(const struct timespec *ts) { |
176 | assert(ts); | |
177 | ||
c477ff14 | 178 | if (ts->tv_sec < 0 || ts->tv_nsec < 0) |
3a43da28 | 179 | return USEC_INFINITY; |
9a98c7a1 LP |
180 | |
181 | if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) | |
3a43da28 | 182 | return USEC_INFINITY; |
9a98c7a1 LP |
183 | |
184 | return | |
185 | (usec_t) ts->tv_sec * USEC_PER_SEC + | |
186 | (usec_t) ts->tv_nsec / NSEC_PER_USEC; | |
187 | } | |
188 | ||
3a730176 | 189 | nsec_t timespec_load_nsec(const struct timespec *ts) { |
45d7a8bb LP |
190 | assert(ts); |
191 | ||
c477ff14 | 192 | if (ts->tv_sec < 0 || ts->tv_nsec < 0) |
45d7a8bb LP |
193 | return NSEC_INFINITY; |
194 | ||
a2daa2f0 ZJS |
195 | if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC) |
196 | return NSEC_INFINITY; | |
197 | ||
198 | return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec; | |
45d7a8bb LP |
199 | } |
200 | ||
9a98c7a1 LP |
201 | struct timespec *timespec_store(struct timespec *ts, usec_t u) { |
202 | assert(ts); | |
203 | ||
f977849c | 204 | if (u == USEC_INFINITY || |
d201d908 | 205 | u / USEC_PER_SEC >= TIME_T_MAX) { |
9a98c7a1 LP |
206 | ts->tv_sec = (time_t) -1; |
207 | ts->tv_nsec = (long) -1; | |
208 | return ts; | |
209 | } | |
210 | ||
211 | ts->tv_sec = (time_t) (u / USEC_PER_SEC); | |
212 | ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); | |
213 | ||
214 | return ts; | |
215 | } | |
216 | ||
217 | usec_t timeval_load(const struct timeval *tv) { | |
218 | assert(tv); | |
219 | ||
c477ff14 | 220 | if (tv->tv_sec < 0 || tv->tv_usec < 0) |
3a43da28 | 221 | return USEC_INFINITY; |
9a98c7a1 LP |
222 | |
223 | if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC) | |
3a43da28 | 224 | return USEC_INFINITY; |
9a98c7a1 LP |
225 | |
226 | return | |
227 | (usec_t) tv->tv_sec * USEC_PER_SEC + | |
228 | (usec_t) tv->tv_usec; | |
229 | } | |
230 | ||
231 | struct timeval *timeval_store(struct timeval *tv, usec_t u) { | |
232 | assert(tv); | |
233 | ||
68bdd2d2 | 234 | if (u == USEC_INFINITY || |
f977849c | 235 | u / USEC_PER_SEC > TIME_T_MAX) { |
9a98c7a1 LP |
236 | tv->tv_sec = (time_t) -1; |
237 | tv->tv_usec = (suseconds_t) -1; | |
4d89874a ZJS |
238 | } else { |
239 | tv->tv_sec = (time_t) (u / USEC_PER_SEC); | |
240 | tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); | |
9a98c7a1 LP |
241 | } |
242 | ||
9a98c7a1 LP |
243 | return tv; |
244 | } | |
245 | ||
21b3a0fc LP |
246 | static char *format_timestamp_internal( |
247 | char *buf, | |
248 | size_t l, | |
249 | usec_t t, | |
250 | bool utc, | |
251 | bool us) { | |
252 | ||
253 | /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our | |
254 | * generated timestamps may be parsed with parse_timestamp(), and always read the same. */ | |
255 | static const char * const weekdays[] = { | |
256 | [0] = "Sun", | |
257 | [1] = "Mon", | |
258 | [2] = "Tue", | |
259 | [3] = "Wed", | |
260 | [4] = "Thu", | |
261 | [5] = "Fri", | |
262 | [6] = "Sat", | |
263 | }; | |
264 | ||
9a98c7a1 LP |
265 | struct tm tm; |
266 | time_t sec; | |
21b3a0fc | 267 | size_t n; |
9a98c7a1 LP |
268 | |
269 | assert(buf); | |
9a98c7a1 | 270 | |
21b3a0fc LP |
271 | if (l < |
272 | 3 + /* week day */ | |
273 | 1 + 10 + /* space and date */ | |
274 | 1 + 8 + /* space and time */ | |
275 | (us ? 1 + 6 : 0) + /* "." and microsecond part */ | |
276 | 1 + 1 + /* space and shortest possible zone */ | |
277 | 1) | |
278 | return NULL; /* Not enough space even for the shortest form. */ | |
3a43da28 | 279 | if (t <= 0 || t == USEC_INFINITY) |
21b3a0fc LP |
280 | return NULL; /* Timestamp is unset */ |
281 | ||
1bb4b028 LP |
282 | /* Let's not format times with years > 9999 */ |
283 | if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) | |
284 | return NULL; | |
285 | ||
21b3a0fc | 286 | sec = (time_t) (t / USEC_PER_SEC); /* Round down */ |
21b3a0fc LP |
287 | |
288 | if (!localtime_or_gmtime_r(&sec, &tm, utc)) | |
9a98c7a1 LP |
289 | return NULL; |
290 | ||
21b3a0fc LP |
291 | /* Start with the week day */ |
292 | assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays)); | |
293 | memcpy(buf, weekdays[tm.tm_wday], 4); | |
9a98c7a1 | 294 | |
21b3a0fc LP |
295 | /* Add the main components */ |
296 | if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0) | |
297 | return NULL; /* Doesn't fit */ | |
0056086a | 298 | |
21b3a0fc | 299 | /* Append the microseconds part, if that's requested */ |
0056086a | 300 | if (us) { |
21b3a0fc LP |
301 | n = strlen(buf); |
302 | if (n + 8 > l) | |
303 | return NULL; /* Microseconds part doesn't fit. */ | |
304 | ||
70887c5f | 305 | sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC); |
21b3a0fc LP |
306 | } |
307 | ||
308 | /* Append the timezone */ | |
309 | n = strlen(buf); | |
310 | if (utc) { | |
311 | /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the | |
312 | * obsolete "GMT" instead. */ | |
313 | if (n + 5 > l) | |
314 | return NULL; /* "UTC" doesn't fit. */ | |
315 | ||
316 | strcpy(buf + n, " UTC"); | |
317 | ||
318 | } else if (!isempty(tm.tm_zone)) { | |
319 | size_t tn; | |
320 | ||
321 | /* An explicit timezone is specified, let's use it, if it fits */ | |
322 | tn = strlen(tm.tm_zone); | |
323 | if (n + 1 + tn + 1 > l) { | |
324 | /* The full time zone does not fit in. Yuck. */ | |
325 | ||
326 | if (n + 1 + _POSIX_TZNAME_MAX + 1 > l) | |
327 | return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */ | |
328 | ||
329 | /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX | |
330 | * minimum time zone length. In this case suppress the timezone entirely, in order not to dump | |
331 | * an overly long, hard to read string on the user. This should be safe, because the user will | |
332 | * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */ | |
333 | } else { | |
334 | buf[n++] = ' '; | |
335 | strcpy(buf + n, tm.tm_zone); | |
336 | } | |
0056086a | 337 | } |
9a98c7a1 LP |
338 | |
339 | return buf; | |
340 | } | |
341 | ||
a62e83b4 | 342 | char *format_timestamp(char *buf, size_t l, usec_t t) { |
0056086a | 343 | return format_timestamp_internal(buf, l, t, false, false); |
a62e83b4 JS |
344 | } |
345 | ||
5ab99e07 | 346 | char *format_timestamp_utc(char *buf, size_t l, usec_t t) { |
0056086a | 347 | return format_timestamp_internal(buf, l, t, true, false); |
f02d8367 ZJS |
348 | } |
349 | ||
5ab99e07 | 350 | char *format_timestamp_us(char *buf, size_t l, usec_t t) { |
0056086a | 351 | return format_timestamp_internal(buf, l, t, false, true); |
5ab99e07 LP |
352 | } |
353 | ||
354 | char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) { | |
0056086a | 355 | return format_timestamp_internal(buf, l, t, true, true); |
5ab99e07 LP |
356 | } |
357 | ||
bbb8486e | 358 | char *format_timestamp_relative(char *buf, size_t l, usec_t t) { |
1fcf71f5 | 359 | const char *s; |
9a98c7a1 LP |
360 | usec_t n, d; |
361 | ||
65de0395 | 362 | if (t <= 0 || t == USEC_INFINITY) |
9a98c7a1 LP |
363 | return NULL; |
364 | ||
65de0395 | 365 | n = now(CLOCK_REALTIME); |
1fcf71f5 LP |
366 | if (n > t) { |
367 | d = n - t; | |
368 | s = "ago"; | |
369 | } else { | |
370 | d = t - n; | |
371 | s = "left"; | |
372 | } | |
9a98c7a1 LP |
373 | |
374 | if (d >= USEC_PER_YEAR) | |
609e002e | 375 | snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s", |
de0671ee ZJS |
376 | d / USEC_PER_YEAR, |
377 | (d % USEC_PER_YEAR) / USEC_PER_MONTH, s); | |
9a98c7a1 | 378 | else if (d >= USEC_PER_MONTH) |
609e002e | 379 | snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s", |
de0671ee ZJS |
380 | d / USEC_PER_MONTH, |
381 | (d % USEC_PER_MONTH) / USEC_PER_DAY, s); | |
9a98c7a1 | 382 | else if (d >= USEC_PER_WEEK) |
609e002e | 383 | snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s", |
de0671ee ZJS |
384 | d / USEC_PER_WEEK, |
385 | (d % USEC_PER_WEEK) / USEC_PER_DAY, s); | |
9a98c7a1 | 386 | else if (d >= 2*USEC_PER_DAY) |
609e002e | 387 | snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s); |
9a98c7a1 | 388 | else if (d >= 25*USEC_PER_HOUR) |
609e002e | 389 | snprintf(buf, l, "1 day " USEC_FMT "h %s", |
de0671ee | 390 | (d - USEC_PER_DAY) / USEC_PER_HOUR, s); |
9a98c7a1 | 391 | else if (d >= 6*USEC_PER_HOUR) |
609e002e | 392 | snprintf(buf, l, USEC_FMT "h %s", |
de0671ee | 393 | d / USEC_PER_HOUR, s); |
9a98c7a1 | 394 | else if (d >= USEC_PER_HOUR) |
609e002e | 395 | snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s", |
de0671ee ZJS |
396 | d / USEC_PER_HOUR, |
397 | (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s); | |
9a98c7a1 | 398 | else if (d >= 5*USEC_PER_MINUTE) |
609e002e | 399 | snprintf(buf, l, USEC_FMT "min %s", |
de0671ee | 400 | d / USEC_PER_MINUTE, s); |
9a98c7a1 | 401 | else if (d >= USEC_PER_MINUTE) |
609e002e | 402 | snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s", |
de0671ee ZJS |
403 | d / USEC_PER_MINUTE, |
404 | (d % USEC_PER_MINUTE) / USEC_PER_SEC, s); | |
9a98c7a1 | 405 | else if (d >= USEC_PER_SEC) |
609e002e | 406 | snprintf(buf, l, USEC_FMT "s %s", |
de0671ee | 407 | d / USEC_PER_SEC, s); |
9a98c7a1 | 408 | else if (d >= USEC_PER_MSEC) |
609e002e | 409 | snprintf(buf, l, USEC_FMT "ms %s", |
de0671ee | 410 | d / USEC_PER_MSEC, s); |
9a98c7a1 | 411 | else if (d > 0) |
de0671ee ZJS |
412 | snprintf(buf, l, USEC_FMT"us %s", |
413 | d, s); | |
9a98c7a1 LP |
414 | else |
415 | snprintf(buf, l, "now"); | |
416 | ||
417 | buf[l-1] = 0; | |
418 | return buf; | |
419 | } | |
420 | ||
2fa4092c | 421 | char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { |
9a98c7a1 LP |
422 | static const struct { |
423 | const char *suffix; | |
424 | usec_t usec; | |
425 | } table[] = { | |
eb55ec9f LP |
426 | { "y", USEC_PER_YEAR }, |
427 | { "month", USEC_PER_MONTH }, | |
428 | { "w", USEC_PER_WEEK }, | |
429 | { "d", USEC_PER_DAY }, | |
430 | { "h", USEC_PER_HOUR }, | |
431 | { "min", USEC_PER_MINUTE }, | |
432 | { "s", USEC_PER_SEC }, | |
433 | { "ms", USEC_PER_MSEC }, | |
434 | { "us", 1 }, | |
9a98c7a1 LP |
435 | }; |
436 | ||
437 | unsigned i; | |
438 | char *p = buf; | |
2fa4092c | 439 | bool something = false; |
9a98c7a1 LP |
440 | |
441 | assert(buf); | |
442 | assert(l > 0); | |
443 | ||
bb1fada8 LP |
444 | if (t == USEC_INFINITY) { |
445 | strncpy(p, "infinity", l-1); | |
446 | p[l-1] = 0; | |
447 | return p; | |
448 | } | |
449 | ||
450 | if (t <= 0) { | |
451 | strncpy(p, "0", l-1); | |
7c537b2e LP |
452 | p[l-1] = 0; |
453 | return p; | |
454 | } | |
455 | ||
7f602784 | 456 | /* The result of this function can be parsed with parse_sec */ |
9a98c7a1 LP |
457 | |
458 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
7759ecb2 | 459 | int k = 0; |
9a98c7a1 | 460 | size_t n; |
2fa4092c LP |
461 | bool done = false; |
462 | usec_t a, b; | |
463 | ||
7c537b2e LP |
464 | if (t <= 0) |
465 | break; | |
2fa4092c | 466 | |
7c537b2e | 467 | if (t < accuracy && something) |
2fa4092c | 468 | break; |
9a98c7a1 LP |
469 | |
470 | if (t < table[i].usec) | |
471 | continue; | |
472 | ||
473 | if (l <= 1) | |
474 | break; | |
475 | ||
2fa4092c LP |
476 | a = t / table[i].usec; |
477 | b = t % table[i].usec; | |
478 | ||
479 | /* Let's see if we should shows this in dot notation */ | |
480 | if (t < USEC_PER_MINUTE && b > 0) { | |
481 | usec_t cc; | |
482 | int j; | |
483 | ||
484 | j = 0; | |
485 | for (cc = table[i].usec; cc > 1; cc /= 10) | |
486 | j++; | |
487 | ||
488 | for (cc = accuracy; cc > 1; cc /= 10) { | |
489 | b /= 10; | |
490 | j--; | |
491 | } | |
492 | ||
493 | if (j > 0) { | |
494 | k = snprintf(p, l, | |
70887c5f | 495 | "%s"USEC_FMT".%0*"PRI_USEC"%s", |
2fa4092c | 496 | p > buf ? " " : "", |
de0671ee | 497 | a, |
2fa4092c | 498 | j, |
70887c5f | 499 | b, |
2fa4092c LP |
500 | table[i].suffix); |
501 | ||
502 | t = 0; | |
503 | done = true; | |
504 | } | |
505 | } | |
506 | ||
507 | /* No? Then let's show it normally */ | |
508 | if (!done) { | |
509 | k = snprintf(p, l, | |
de0671ee | 510 | "%s"USEC_FMT"%s", |
2fa4092c | 511 | p > buf ? " " : "", |
de0671ee | 512 | a, |
2fa4092c LP |
513 | table[i].suffix); |
514 | ||
515 | t = b; | |
516 | } | |
517 | ||
9a98c7a1 LP |
518 | n = MIN((size_t) k, l); |
519 | ||
520 | l -= n; | |
521 | p += n; | |
522 | ||
2fa4092c | 523 | something = true; |
9a98c7a1 LP |
524 | } |
525 | ||
526 | *p = 0; | |
527 | ||
528 | return buf; | |
529 | } | |
530 | ||
531 | void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) { | |
532 | ||
533 | assert(f); | |
534 | assert(name); | |
535 | assert(t); | |
536 | ||
537 | if (!dual_timestamp_is_set(t)) | |
538 | return; | |
539 | ||
de0671ee | 540 | fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n", |
9a98c7a1 | 541 | name, |
de0671ee ZJS |
542 | t->realtime, |
543 | t->monotonic); | |
9a98c7a1 LP |
544 | } |
545 | ||
e911de99 | 546 | int dual_timestamp_deserialize(const char *value, dual_timestamp *t) { |
74c5b33b | 547 | uint64_t a, b; |
9c0565b2 | 548 | int r, pos; |
9a98c7a1 LP |
549 | |
550 | assert(value); | |
551 | assert(t); | |
552 | ||
9c0565b2 ZJS |
553 | pos = strspn(value, WHITESPACE); |
554 | if (value[pos] == '-') | |
555 | return -EINVAL; | |
556 | pos += strspn(value + pos, DIGITS); | |
557 | pos += strspn(value + pos, WHITESPACE); | |
558 | if (value[pos] == '-') | |
559 | return -EINVAL; | |
560 | ||
561 | r = sscanf(value, "%" PRIu64 "%" PRIu64 "%n", &a, &b, &pos); | |
562 | if (r != 2) { | |
563 | log_debug("Failed to parse dual timestamp value \"%s\".", value); | |
e911de99 | 564 | return -EINVAL; |
9a98c7a1 | 565 | } |
e911de99 | 566 | |
9c0565b2 ZJS |
567 | if (value[pos] != '\0') |
568 | /* trailing garbage */ | |
569 | return -EINVAL; | |
570 | ||
e911de99 LP |
571 | t->realtime = a; |
572 | t->monotonic = b; | |
573 | ||
574 | return 0; | |
9a98c7a1 LP |
575 | } |
576 | ||
b895a735 | 577 | int timestamp_deserialize(const char *value, usec_t *timestamp) { |
ebf30a08 AK |
578 | int r; |
579 | ||
580 | assert(value); | |
581 | ||
582 | r = safe_atou64(value, timestamp); | |
ebf30a08 | 583 | if (r < 0) |
b895a735 | 584 | return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value); |
ebf30a08 AK |
585 | |
586 | return r; | |
587 | } | |
588 | ||
48d26c01 | 589 | static int parse_timestamp_impl(const char *t, usec_t *usec, bool with_tz) { |
92134489 LP |
590 | static const struct { |
591 | const char *name; | |
592 | const int nr; | |
593 | } day_nr[] = { | |
594 | { "Sunday", 0 }, | |
595 | { "Sun", 0 }, | |
596 | { "Monday", 1 }, | |
597 | { "Mon", 1 }, | |
598 | { "Tuesday", 2 }, | |
599 | { "Tue", 2 }, | |
600 | { "Wednesday", 3 }, | |
601 | { "Wed", 3 }, | |
602 | { "Thursday", 4 }, | |
603 | { "Thu", 4 }, | |
604 | { "Friday", 5 }, | |
605 | { "Fri", 5 }, | |
606 | { "Saturday", 6 }, | |
607 | { "Sat", 6 }, | |
608 | }; | |
609 | ||
48d26c01 | 610 | const char *k, *utc = NULL, *tzn = NULL; |
9a98c7a1 LP |
611 | struct tm tm, copy; |
612 | time_t x; | |
e4eaf99a | 613 | usec_t x_usec, plus = 0, minus = 0, ret; |
21b3a0fc | 614 | int r, weekday = -1, dst = -1; |
92134489 | 615 | unsigned i; |
9a98c7a1 LP |
616 | |
617 | /* | |
618 | * Allowed syntaxes: | |
619 | * | |
620 | * 2012-09-22 16:34:22 | |
621 | * 2012-09-22 16:34 (seconds will be set to 0) | |
622 | * 2012-09-22 (time will be set to 00:00:00) | |
623 | * 16:34:22 (date will be set to today) | |
624 | * 16:34 (date will be set to today, seconds to 0) | |
625 | * now | |
626 | * yesterday (time is set to 00:00:00) | |
627 | * today (time is set to 00:00:00) | |
628 | * tomorrow (time is set to 00:00:00) | |
629 | * +5min | |
630 | * -5days | |
5ba6e094 | 631 | * @2147483647 (seconds since epoch) |
9a98c7a1 LP |
632 | * |
633 | */ | |
634 | ||
635 | assert(t); | |
636 | assert(usec); | |
637 | ||
48d26c01 | 638 | if (t[0] == '@' && !with_tz) |
e4eaf99a | 639 | return parse_sec(t + 1, usec); |
9a98c7a1 | 640 | |
e4eaf99a | 641 | ret = now(CLOCK_REALTIME); |
9a98c7a1 | 642 | |
48d26c01 IK |
643 | if (!with_tz) { |
644 | if (streq(t, "now")) | |
645 | goto finish; | |
9a98c7a1 | 646 | |
48d26c01 IK |
647 | else if (t[0] == '+') { |
648 | r = parse_sec(t+1, &plus); | |
649 | if (r < 0) | |
650 | return r; | |
9a98c7a1 | 651 | |
48d26c01 | 652 | goto finish; |
9a98c7a1 | 653 | |
48d26c01 IK |
654 | } else if (t[0] == '-') { |
655 | r = parse_sec(t+1, &minus); | |
656 | if (r < 0) | |
657 | return r; | |
9a98c7a1 | 658 | |
48d26c01 | 659 | goto finish; |
decad910 | 660 | |
48d26c01 IK |
661 | } else if ((k = endswith(t, " ago"))) { |
662 | t = strndupa(t, k - t); | |
decad910 | 663 | |
48d26c01 IK |
664 | r = parse_sec(t, &minus); |
665 | if (r < 0) | |
666 | return r; | |
decad910 | 667 | |
48d26c01 | 668 | goto finish; |
1fcf71f5 | 669 | |
48d26c01 IK |
670 | } else if ((k = endswith(t, " left"))) { |
671 | t = strndupa(t, k - t); | |
1fcf71f5 | 672 | |
48d26c01 IK |
673 | r = parse_sec(t, &plus); |
674 | if (r < 0) | |
675 | return r; | |
1fcf71f5 | 676 | |
48d26c01 IK |
677 | goto finish; |
678 | } | |
9a98c7a1 | 679 | |
48d26c01 IK |
680 | /* See if the timestamp is suffixed with UTC */ |
681 | utc = endswith_no_case(t, " UTC"); | |
682 | if (utc) | |
683 | t = strndupa(t, utc - t); | |
684 | else { | |
685 | const char *e = NULL; | |
686 | int j; | |
21b3a0fc | 687 | |
48d26c01 | 688 | tzset(); |
21b3a0fc | 689 | |
48d26c01 IK |
690 | /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only |
691 | * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because | |
692 | * there are no nice APIs available to cover this. By accepting the local time zone strings, we make | |
693 | * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't | |
694 | * support arbitrary timezone specifications. */ | |
e4eaf99a | 695 | |
48d26c01 | 696 | for (j = 0; j <= 1; j++) { |
21b3a0fc | 697 | |
48d26c01 IK |
698 | if (isempty(tzname[j])) |
699 | continue; | |
21b3a0fc | 700 | |
48d26c01 IK |
701 | e = endswith_no_case(t, tzname[j]); |
702 | if (!e) | |
703 | continue; | |
704 | if (e == t) | |
705 | continue; | |
706 | if (e[-1] != ' ') | |
707 | continue; | |
21b3a0fc | 708 | |
48d26c01 IK |
709 | break; |
710 | } | |
21b3a0fc | 711 | |
48d26c01 IK |
712 | if (IN_SET(j, 0, 1)) { |
713 | /* Found one of the two timezones specified. */ | |
714 | t = strndupa(t, e - t - 1); | |
715 | dst = j; | |
716 | tzn = tzname[j]; | |
717 | } | |
21b3a0fc LP |
718 | } |
719 | } | |
720 | ||
721 | x = (time_t) (ret / USEC_PER_SEC); | |
e4eaf99a HV |
722 | x_usec = 0; |
723 | ||
21b3a0fc LP |
724 | if (!localtime_or_gmtime_r(&x, &tm, utc)) |
725 | return -EINVAL; | |
726 | ||
2e72b794 IK |
727 | tm.tm_isdst = dst; |
728 | if (!with_tz && tzn) | |
729 | tm.tm_zone = tzn; | |
e4eaf99a HV |
730 | |
731 | if (streq(t, "today")) { | |
732 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; | |
733 | goto from_tm; | |
734 | ||
735 | } else if (streq(t, "yesterday")) { | |
313cefa1 | 736 | tm.tm_mday--; |
e4eaf99a HV |
737 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; |
738 | goto from_tm; | |
739 | ||
740 | } else if (streq(t, "tomorrow")) { | |
313cefa1 | 741 | tm.tm_mday++; |
e4eaf99a HV |
742 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; |
743 | goto from_tm; | |
744 | } | |
745 | ||
92134489 LP |
746 | for (i = 0; i < ELEMENTSOF(day_nr); i++) { |
747 | size_t skip; | |
748 | ||
749 | if (!startswith_no_case(t, day_nr[i].name)) | |
750 | continue; | |
751 | ||
752 | skip = strlen(day_nr[i].name); | |
753 | if (t[skip] != ' ') | |
754 | continue; | |
755 | ||
756 | weekday = day_nr[i].nr; | |
757 | t += skip + 1; | |
758 | break; | |
759 | } | |
760 | ||
9a98c7a1 LP |
761 | copy = tm; |
762 | k = strptime(t, "%y-%m-%d %H:%M:%S", &tm); | |
e4eaf99a HV |
763 | if (k) { |
764 | if (*k == '.') | |
765 | goto parse_usec; | |
766 | else if (*k == 0) | |
767 | goto from_tm; | |
768 | } | |
9a98c7a1 LP |
769 | |
770 | tm = copy; | |
771 | k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm); | |
e4eaf99a HV |
772 | if (k) { |
773 | if (*k == '.') | |
774 | goto parse_usec; | |
775 | else if (*k == 0) | |
776 | goto from_tm; | |
777 | } | |
9a98c7a1 LP |
778 | |
779 | tm = copy; | |
780 | k = strptime(t, "%y-%m-%d %H:%M", &tm); | |
781 | if (k && *k == 0) { | |
782 | tm.tm_sec = 0; | |
e4eaf99a | 783 | goto from_tm; |
9a98c7a1 LP |
784 | } |
785 | ||
786 | tm = copy; | |
787 | k = strptime(t, "%Y-%m-%d %H:%M", &tm); | |
788 | if (k && *k == 0) { | |
789 | tm.tm_sec = 0; | |
e4eaf99a | 790 | goto from_tm; |
9a98c7a1 LP |
791 | } |
792 | ||
793 | tm = copy; | |
794 | k = strptime(t, "%y-%m-%d", &tm); | |
795 | if (k && *k == 0) { | |
796 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; | |
e4eaf99a | 797 | goto from_tm; |
9a98c7a1 LP |
798 | } |
799 | ||
800 | tm = copy; | |
801 | k = strptime(t, "%Y-%m-%d", &tm); | |
802 | if (k && *k == 0) { | |
803 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; | |
e4eaf99a | 804 | goto from_tm; |
9a98c7a1 LP |
805 | } |
806 | ||
807 | tm = copy; | |
808 | k = strptime(t, "%H:%M:%S", &tm); | |
e4eaf99a HV |
809 | if (k) { |
810 | if (*k == '.') | |
811 | goto parse_usec; | |
812 | else if (*k == 0) | |
813 | goto from_tm; | |
814 | } | |
9a98c7a1 LP |
815 | |
816 | tm = copy; | |
817 | k = strptime(t, "%H:%M", &tm); | |
818 | if (k && *k == 0) { | |
819 | tm.tm_sec = 0; | |
e4eaf99a | 820 | goto from_tm; |
9a98c7a1 LP |
821 | } |
822 | ||
823 | return -EINVAL; | |
824 | ||
e4eaf99a HV |
825 | parse_usec: |
826 | { | |
436dd70f | 827 | unsigned add; |
e4eaf99a HV |
828 | |
829 | k++; | |
436dd70f HV |
830 | r = parse_fractional_part_u(&k, 6, &add); |
831 | if (r < 0) | |
e4eaf99a HV |
832 | return -EINVAL; |
833 | ||
436dd70f | 834 | if (*k) |
e4eaf99a HV |
835 | return -EINVAL; |
836 | ||
436dd70f | 837 | x_usec = add; |
e4eaf99a HV |
838 | } |
839 | ||
840 | from_tm: | |
214cc95d | 841 | if (weekday >= 0 && tm.tm_wday != weekday) |
68bdd2d2 | 842 | return -EINVAL; |
9a98c7a1 | 843 | |
214cc95d MH |
844 | x = mktime_or_timegm(&tm, utc); |
845 | if (x < 0) | |
92134489 LP |
846 | return -EINVAL; |
847 | ||
68bdd2d2 | 848 | ret = (usec_t) x * USEC_PER_SEC + x_usec; |
1bb4b028 LP |
849 | if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX) |
850 | return -EINVAL; | |
9a98c7a1 | 851 | |
e4eaf99a | 852 | finish: |
315782db | 853 | if (ret + plus < ret) /* overflow? */ |
68bdd2d2 | 854 | return -EINVAL; |
9a98c7a1 | 855 | ret += plus; |
1bb4b028 LP |
856 | if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX) |
857 | return -EINVAL; | |
858 | ||
68bdd2d2 | 859 | if (ret >= minus) |
9a98c7a1 LP |
860 | ret -= minus; |
861 | else | |
68bdd2d2 | 862 | return -EINVAL; |
9a98c7a1 LP |
863 | |
864 | *usec = ret; | |
865 | ||
866 | return 0; | |
867 | } | |
868 | ||
48d26c01 IK |
869 | typedef struct ParseTimestampResult { |
870 | usec_t usec; | |
871 | int return_value; | |
872 | } ParseTimestampResult; | |
873 | ||
874 | int parse_timestamp(const char *t, usec_t *usec) { | |
ff69484a | 875 | char *last_space, *tz = NULL; |
48d26c01 | 876 | ParseTimestampResult *shared, tmp; |
4c253ed1 | 877 | int r; |
48d26c01 IK |
878 | |
879 | last_space = strrchr(t, ' '); | |
a2932a0d | 880 | if (last_space != NULL && timezone_is_valid(last_space + 1)) |
ff69484a | 881 | tz = last_space + 1; |
48d26c01 | 882 | |
234519ae | 883 | if (!tz || endswith_no_case(t, " UTC")) |
48d26c01 IK |
884 | return parse_timestamp_impl(t, usec, false); |
885 | ||
48d26c01 IK |
886 | shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
887 | if (shared == MAP_FAILED) | |
888 | return negative_errno(); | |
889 | ||
1f5d1e02 | 890 | r = safe_fork("(sd-timestamp)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL); |
4c253ed1 | 891 | if (r < 0) { |
48d26c01 | 892 | (void) munmap(shared, sizeof *shared); |
4c253ed1 | 893 | return r; |
48d26c01 | 894 | } |
4c253ed1 | 895 | if (r == 0) { |
3fd4929b MH |
896 | bool with_tz = true; |
897 | ||
ff69484a | 898 | if (setenv("TZ", tz, 1) != 0) { |
48d26c01 IK |
899 | shared->return_value = negative_errno(); |
900 | _exit(EXIT_FAILURE); | |
901 | } | |
902 | ||
903 | tzset(); | |
904 | ||
3fd4929b MH |
905 | /* If there is a timezone that matches the tzname fields, leave the parsing to the implementation. |
906 | * Otherwise just cut it off */ | |
907 | with_tz = !STR_IN_SET(tz, tzname[0], tzname[1]); | |
908 | ||
909 | /*cut off the timezone if we dont need it*/ | |
910 | if (with_tz) | |
911 | t = strndupa(t, last_space - t); | |
912 | ||
913 | shared->return_value = parse_timestamp_impl(t, &shared->usec, with_tz); | |
48d26c01 IK |
914 | |
915 | _exit(EXIT_SUCCESS); | |
916 | } | |
917 | ||
48d26c01 IK |
918 | tmp = *shared; |
919 | if (munmap(shared, sizeof *shared) != 0) | |
920 | return negative_errno(); | |
921 | ||
922 | if (tmp.return_value == 0) | |
923 | *usec = tmp.usec; | |
924 | ||
925 | return tmp.return_value; | |
926 | } | |
927 | ||
240a7ba9 | 928 | static char* extract_multiplier(char *p, usec_t *multiplier) { |
9a98c7a1 LP |
929 | static const struct { |
930 | const char *suffix; | |
931 | usec_t usec; | |
932 | } table[] = { | |
eb55ec9f LP |
933 | { "seconds", USEC_PER_SEC }, |
934 | { "second", USEC_PER_SEC }, | |
935 | { "sec", USEC_PER_SEC }, | |
936 | { "s", USEC_PER_SEC }, | |
9a98c7a1 | 937 | { "minutes", USEC_PER_MINUTE }, |
eb55ec9f LP |
938 | { "minute", USEC_PER_MINUTE }, |
939 | { "min", USEC_PER_MINUTE }, | |
940 | { "months", USEC_PER_MONTH }, | |
941 | { "month", USEC_PER_MONTH }, | |
942 | { "M", USEC_PER_MONTH }, | |
943 | { "msec", USEC_PER_MSEC }, | |
944 | { "ms", USEC_PER_MSEC }, | |
945 | { "m", USEC_PER_MINUTE }, | |
946 | { "hours", USEC_PER_HOUR }, | |
947 | { "hour", USEC_PER_HOUR }, | |
948 | { "hr", USEC_PER_HOUR }, | |
949 | { "h", USEC_PER_HOUR }, | |
950 | { "days", USEC_PER_DAY }, | |
951 | { "day", USEC_PER_DAY }, | |
952 | { "d", USEC_PER_DAY }, | |
953 | { "weeks", USEC_PER_WEEK }, | |
954 | { "week", USEC_PER_WEEK }, | |
955 | { "w", USEC_PER_WEEK }, | |
956 | { "years", USEC_PER_YEAR }, | |
957 | { "year", USEC_PER_YEAR }, | |
958 | { "y", USEC_PER_YEAR }, | |
959 | { "usec", 1ULL }, | |
960 | { "us", 1ULL }, | |
5efdbf11 | 961 | { "µs", 1ULL }, |
9a98c7a1 | 962 | }; |
240a7ba9 ZJS |
963 | unsigned i; |
964 | ||
965 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
966 | char *e; | |
967 | ||
968 | e = startswith(p, table[i].suffix); | |
969 | if (e) { | |
970 | *multiplier = table[i].usec; | |
971 | return e; | |
972 | } | |
973 | } | |
9a98c7a1 | 974 | |
240a7ba9 ZJS |
975 | return p; |
976 | } | |
977 | ||
978 | int parse_time(const char *t, usec_t *usec, usec_t default_unit) { | |
b1d6dcf5 | 979 | const char *p, *s; |
9a98c7a1 | 980 | usec_t r = 0; |
cb0dac05 | 981 | bool something = false; |
9a98c7a1 LP |
982 | |
983 | assert(t); | |
984 | assert(usec); | |
519cffec | 985 | assert(default_unit > 0); |
9a98c7a1 LP |
986 | |
987 | p = t; | |
b1d6dcf5 ZJS |
988 | |
989 | p += strspn(p, WHITESPACE); | |
990 | s = startswith(p, "infinity"); | |
991 | if (s) { | |
992 | s += strspn(s, WHITESPACE); | |
993 | if (*s != 0) | |
994 | return -EINVAL; | |
995 | ||
996 | *usec = USEC_INFINITY; | |
997 | return 0; | |
998 | } | |
999 | ||
cb0dac05 LP |
1000 | for (;;) { |
1001 | long long l, z = 0; | |
9a98c7a1 | 1002 | char *e; |
240a7ba9 ZJS |
1003 | unsigned n = 0; |
1004 | usec_t multiplier = default_unit, k; | |
cb0dac05 LP |
1005 | |
1006 | p += strspn(p, WHITESPACE); | |
1007 | ||
1008 | if (*p == 0) { | |
1009 | if (!something) | |
1010 | return -EINVAL; | |
1011 | ||
1012 | break; | |
1013 | } | |
9a98c7a1 LP |
1014 | |
1015 | errno = 0; | |
1016 | l = strtoll(p, &e, 10); | |
8333c77e | 1017 | if (errno > 0) |
9a98c7a1 | 1018 | return -errno; |
9a98c7a1 LP |
1019 | if (l < 0) |
1020 | return -ERANGE; | |
1021 | ||
cb0dac05 LP |
1022 | if (*e == '.') { |
1023 | char *b = e + 1; | |
1024 | ||
1025 | errno = 0; | |
1026 | z = strtoll(b, &e, 10); | |
1027 | if (errno > 0) | |
1028 | return -errno; | |
1029 | ||
1030 | if (z < 0) | |
1031 | return -ERANGE; | |
1032 | ||
1033 | if (e == b) | |
1034 | return -EINVAL; | |
1035 | ||
1036 | n = e - b; | |
1037 | ||
1038 | } else if (e == p) | |
9a98c7a1 LP |
1039 | return -EINVAL; |
1040 | ||
1041 | e += strspn(e, WHITESPACE); | |
240a7ba9 | 1042 | p = extract_multiplier(e, &multiplier); |
9a98c7a1 | 1043 | |
519cffec LP |
1044 | something = true; |
1045 | ||
1046 | k = (usec_t) z * multiplier; | |
1047 | ||
1048 | for (; n > 0; n--) | |
1049 | k /= 10; | |
1050 | ||
1051 | r += (usec_t) l * multiplier + k; | |
cb0dac05 | 1052 | } |
9a98c7a1 LP |
1053 | |
1054 | *usec = r; | |
1055 | ||
1056 | return 0; | |
1057 | } | |
1058 | ||
519cffec LP |
1059 | int parse_sec(const char *t, usec_t *usec) { |
1060 | return parse_time(t, usec, USEC_PER_SEC); | |
1061 | } | |
1062 | ||
0004f698 | 1063 | int parse_sec_fix_0(const char *t, usec_t *usec) { |
eae51da3 LP |
1064 | assert(t); |
1065 | assert(usec); | |
1066 | ||
0004f698 | 1067 | t += strspn(t, WHITESPACE); |
eae51da3 | 1068 | |
0004f698 ZJS |
1069 | if (streq(t, "0")) { |
1070 | *usec = USEC_INFINITY; | |
1071 | return 0; | |
1072 | } | |
1073 | ||
1074 | return parse_sec(t, usec); | |
1075 | } | |
1076 | ||
9a98c7a1 LP |
1077 | int parse_nsec(const char *t, nsec_t *nsec) { |
1078 | static const struct { | |
1079 | const char *suffix; | |
1080 | nsec_t nsec; | |
1081 | } table[] = { | |
1082 | { "seconds", NSEC_PER_SEC }, | |
1083 | { "second", NSEC_PER_SEC }, | |
1084 | { "sec", NSEC_PER_SEC }, | |
1085 | { "s", NSEC_PER_SEC }, | |
1086 | { "minutes", NSEC_PER_MINUTE }, | |
1087 | { "minute", NSEC_PER_MINUTE }, | |
1088 | { "min", NSEC_PER_MINUTE }, | |
1089 | { "months", NSEC_PER_MONTH }, | |
1090 | { "month", NSEC_PER_MONTH }, | |
1091 | { "msec", NSEC_PER_MSEC }, | |
1092 | { "ms", NSEC_PER_MSEC }, | |
1093 | { "m", NSEC_PER_MINUTE }, | |
1094 | { "hours", NSEC_PER_HOUR }, | |
1095 | { "hour", NSEC_PER_HOUR }, | |
1096 | { "hr", NSEC_PER_HOUR }, | |
1097 | { "h", NSEC_PER_HOUR }, | |
1098 | { "days", NSEC_PER_DAY }, | |
1099 | { "day", NSEC_PER_DAY }, | |
1100 | { "d", NSEC_PER_DAY }, | |
1101 | { "weeks", NSEC_PER_WEEK }, | |
1102 | { "week", NSEC_PER_WEEK }, | |
1103 | { "w", NSEC_PER_WEEK }, | |
1104 | { "years", NSEC_PER_YEAR }, | |
1105 | { "year", NSEC_PER_YEAR }, | |
1106 | { "y", NSEC_PER_YEAR }, | |
1107 | { "usec", NSEC_PER_USEC }, | |
1108 | { "us", NSEC_PER_USEC }, | |
5efdbf11 | 1109 | { "µs", NSEC_PER_USEC }, |
9a98c7a1 LP |
1110 | { "nsec", 1ULL }, |
1111 | { "ns", 1ULL }, | |
1112 | { "", 1ULL }, /* default is nsec */ | |
1113 | }; | |
1114 | ||
e73c78c2 | 1115 | const char *p, *s; |
9a98c7a1 | 1116 | nsec_t r = 0; |
cb0dac05 | 1117 | bool something = false; |
9a98c7a1 LP |
1118 | |
1119 | assert(t); | |
1120 | assert(nsec); | |
1121 | ||
1122 | p = t; | |
e73c78c2 LP |
1123 | |
1124 | p += strspn(p, WHITESPACE); | |
1125 | s = startswith(p, "infinity"); | |
1126 | if (s) { | |
1127 | s += strspn(s, WHITESPACE); | |
8e8933ca | 1128 | if (*s != 0) |
e73c78c2 LP |
1129 | return -EINVAL; |
1130 | ||
1131 | *nsec = NSEC_INFINITY; | |
1132 | return 0; | |
1133 | } | |
1134 | ||
cb0dac05 LP |
1135 | for (;;) { |
1136 | long long l, z = 0; | |
9a98c7a1 | 1137 | char *e; |
cb0dac05 LP |
1138 | unsigned i, n = 0; |
1139 | ||
1140 | p += strspn(p, WHITESPACE); | |
1141 | ||
1142 | if (*p == 0) { | |
1143 | if (!something) | |
1144 | return -EINVAL; | |
1145 | ||
1146 | break; | |
1147 | } | |
9a98c7a1 LP |
1148 | |
1149 | errno = 0; | |
1150 | l = strtoll(p, &e, 10); | |
1151 | ||
8333c77e | 1152 | if (errno > 0) |
9a98c7a1 LP |
1153 | return -errno; |
1154 | ||
1155 | if (l < 0) | |
1156 | return -ERANGE; | |
1157 | ||
cb0dac05 LP |
1158 | if (*e == '.') { |
1159 | char *b = e + 1; | |
1160 | ||
1161 | errno = 0; | |
1162 | z = strtoll(b, &e, 10); | |
1163 | if (errno > 0) | |
1164 | return -errno; | |
1165 | ||
1166 | if (z < 0) | |
1167 | return -ERANGE; | |
1168 | ||
1169 | if (e == b) | |
1170 | return -EINVAL; | |
1171 | ||
1172 | n = e - b; | |
1173 | ||
1174 | } else if (e == p) | |
9a98c7a1 LP |
1175 | return -EINVAL; |
1176 | ||
1177 | e += strspn(e, WHITESPACE); | |
1178 | ||
1179 | for (i = 0; i < ELEMENTSOF(table); i++) | |
1180 | if (startswith(e, table[i].suffix)) { | |
cb0dac05 LP |
1181 | nsec_t k = (nsec_t) z * table[i].nsec; |
1182 | ||
1183 | for (; n > 0; n--) | |
1184 | k /= 10; | |
1185 | ||
1186 | r += (nsec_t) l * table[i].nsec + k; | |
9a98c7a1 | 1187 | p = e + strlen(table[i].suffix); |
cb0dac05 LP |
1188 | |
1189 | something = true; | |
9a98c7a1 LP |
1190 | break; |
1191 | } | |
1192 | ||
1193 | if (i >= ELEMENTSOF(table)) | |
1194 | return -EINVAL; | |
1195 | ||
cb0dac05 | 1196 | } |
9a98c7a1 LP |
1197 | |
1198 | *nsec = r; | |
1199 | ||
1200 | return 0; | |
1201 | } | |
03cc26dd LP |
1202 | |
1203 | bool ntp_synced(void) { | |
1204 | struct timex txc = {}; | |
1205 | ||
1206 | if (adjtimex(&txc) < 0) | |
1207 | return false; | |
1208 | ||
1209 | if (txc.status & STA_UNSYNC) | |
1210 | return false; | |
1211 | ||
1212 | return true; | |
1213 | } | |
75683450 LP |
1214 | |
1215 | int get_timezones(char ***ret) { | |
1216 | _cleanup_fclose_ FILE *f = NULL; | |
1217 | _cleanup_strv_free_ char **zones = NULL; | |
1218 | size_t n_zones = 0, n_allocated = 0; | |
1219 | ||
1220 | assert(ret); | |
1221 | ||
1222 | zones = strv_new("UTC", NULL); | |
1223 | if (!zones) | |
1224 | return -ENOMEM; | |
1225 | ||
1226 | n_allocated = 2; | |
1227 | n_zones = 1; | |
1228 | ||
1229 | f = fopen("/usr/share/zoneinfo/zone.tab", "re"); | |
1230 | if (f) { | |
1231 | char l[LINE_MAX]; | |
1232 | ||
1233 | FOREACH_LINE(l, f, return -errno) { | |
1234 | char *p, *w; | |
1235 | size_t k; | |
1236 | ||
1237 | p = strstrip(l); | |
1238 | ||
1239 | if (isempty(p) || *p == '#') | |
1240 | continue; | |
1241 | ||
1242 | /* Skip over country code */ | |
1243 | p += strcspn(p, WHITESPACE); | |
1244 | p += strspn(p, WHITESPACE); | |
1245 | ||
1246 | /* Skip over coordinates */ | |
1247 | p += strcspn(p, WHITESPACE); | |
1248 | p += strspn(p, WHITESPACE); | |
1249 | ||
1250 | /* Found timezone name */ | |
1251 | k = strcspn(p, WHITESPACE); | |
1252 | if (k <= 0) | |
1253 | continue; | |
1254 | ||
1255 | w = strndup(p, k); | |
1256 | if (!w) | |
1257 | return -ENOMEM; | |
1258 | ||
1259 | if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) { | |
1260 | free(w); | |
1261 | return -ENOMEM; | |
1262 | } | |
1263 | ||
1264 | zones[n_zones++] = w; | |
1265 | zones[n_zones] = NULL; | |
1266 | } | |
1267 | ||
1268 | strv_sort(zones); | |
1269 | ||
1270 | } else if (errno != ENOENT) | |
1271 | return -errno; | |
1272 | ||
1cc6c93a | 1273 | *ret = TAKE_PTR(zones); |
75683450 LP |
1274 | |
1275 | return 0; | |
1276 | } | |
1277 | ||
1278 | bool timezone_is_valid(const char *name) { | |
1279 | bool slash = false; | |
1280 | const char *p, *t; | |
1281 | struct stat st; | |
1282 | ||
5c904ba5 LP |
1283 | if (isempty(name)) |
1284 | return false; | |
1285 | ||
1286 | if (name[0] == '/') | |
75683450 LP |
1287 | return false; |
1288 | ||
1289 | for (p = name; *p; p++) { | |
1290 | if (!(*p >= '0' && *p <= '9') && | |
1291 | !(*p >= 'a' && *p <= 'z') && | |
1292 | !(*p >= 'A' && *p <= 'Z') && | |
4c701096 | 1293 | !IN_SET(*p, '-', '_', '+', '/')) |
75683450 LP |
1294 | return false; |
1295 | ||
1296 | if (*p == '/') { | |
1297 | ||
1298 | if (slash) | |
1299 | return false; | |
1300 | ||
1301 | slash = true; | |
1302 | } else | |
1303 | slash = false; | |
1304 | } | |
1305 | ||
1306 | if (slash) | |
1307 | return false; | |
1308 | ||
63c372cb | 1309 | t = strjoina("/usr/share/zoneinfo/", name); |
75683450 LP |
1310 | if (stat(t, &st) < 0) |
1311 | return false; | |
1312 | ||
1313 | if (!S_ISREG(st.st_mode)) | |
1314 | return false; | |
1315 | ||
1316 | return true; | |
1317 | } | |
77ff2de9 | 1318 | |
3411372e LP |
1319 | bool clock_boottime_supported(void) { |
1320 | static int supported = -1; | |
1321 | ||
1322 | /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */ | |
1323 | ||
1324 | if (supported < 0) { | |
1325 | int fd; | |
1326 | ||
1327 | fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC); | |
1328 | if (fd < 0) | |
1329 | supported = false; | |
1330 | else { | |
1331 | safe_close(fd); | |
1332 | supported = true; | |
1333 | } | |
77ff2de9 TG |
1334 | } |
1335 | ||
3411372e LP |
1336 | return supported; |
1337 | } | |
1338 | ||
1339 | clockid_t clock_boottime_or_monotonic(void) { | |
1340 | if (clock_boottime_supported()) | |
1341 | return CLOCK_BOOTTIME; | |
1342 | else | |
1343 | return CLOCK_MONOTONIC; | |
77ff2de9 | 1344 | } |
5c904ba5 | 1345 | |
fe624c4c LP |
1346 | bool clock_supported(clockid_t clock) { |
1347 | struct timespec ts; | |
1348 | ||
1349 | switch (clock) { | |
1350 | ||
1351 | case CLOCK_MONOTONIC: | |
1352 | case CLOCK_REALTIME: | |
1353 | return true; | |
1354 | ||
1355 | case CLOCK_BOOTTIME: | |
1356 | return clock_boottime_supported(); | |
1357 | ||
1358 | case CLOCK_BOOTTIME_ALARM: | |
1359 | if (!clock_boottime_supported()) | |
1360 | return false; | |
1361 | ||
4831981d | 1362 | _fallthrough_; |
fe624c4c LP |
1363 | default: |
1364 | /* For everything else, check properly */ | |
1365 | return clock_gettime(clock, &ts) >= 0; | |
1366 | } | |
1367 | } | |
1368 | ||
64d6c229 | 1369 | int get_timezone(char **tz) { |
5c904ba5 LP |
1370 | _cleanup_free_ char *t = NULL; |
1371 | const char *e; | |
1372 | char *z; | |
1373 | int r; | |
1374 | ||
1375 | r = readlink_malloc("/etc/localtime", &t); | |
1376 | if (r < 0) | |
1377 | return r; /* returns EINVAL if not a symlink */ | |
1378 | ||
1379 | e = path_startswith(t, "/usr/share/zoneinfo/"); | |
1380 | if (!e) | |
1381 | e = path_startswith(t, "../usr/share/zoneinfo/"); | |
1382 | if (!e) | |
1383 | return -EINVAL; | |
1384 | ||
1385 | if (!timezone_is_valid(e)) | |
1386 | return -EINVAL; | |
1387 | ||
1388 | z = strdup(e); | |
1389 | if (!z) | |
1390 | return -ENOMEM; | |
1391 | ||
64d6c229 | 1392 | *tz = z; |
5c904ba5 LP |
1393 | return 0; |
1394 | } | |
7c67c79c HV |
1395 | |
1396 | time_t mktime_or_timegm(struct tm *tm, bool utc) { | |
1397 | return utc ? timegm(tm) : mktime(tm); | |
1398 | } | |
1399 | ||
1400 | struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) { | |
1401 | return utc ? gmtime_r(t, tm) : localtime_r(t, tm); | |
1402 | } | |
87b8ce69 SS |
1403 | |
1404 | unsigned long usec_to_jiffies(usec_t u) { | |
1405 | static thread_local unsigned long hz = 0; | |
1406 | long r; | |
1407 | ||
1408 | if (hz == 0) { | |
1409 | r = sysconf(_SC_CLK_TCK); | |
1410 | ||
1411 | assert(r > 0); | |
70887c5f | 1412 | hz = r; |
87b8ce69 SS |
1413 | } |
1414 | ||
1415 | return DIV_ROUND_UP(u , USEC_PER_SEC / hz); | |
1416 | } | |
1007ec60 LP |
1417 | |
1418 | usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) { | |
1419 | usec_t a, b; | |
1420 | ||
1421 | if (x == USEC_INFINITY) | |
1422 | return USEC_INFINITY; | |
1423 | if (map_clock_id(from) == map_clock_id(to)) | |
1424 | return x; | |
1425 | ||
1426 | a = now(from); | |
1427 | b = now(to); | |
1428 | ||
1429 | if (x > a) | |
1430 | /* x lies in the future */ | |
1431 | return usec_add(b, usec_sub_unsigned(x, a)); | |
1432 | else | |
1433 | /* x lies in the past */ | |
1434 | return usec_sub_unsigned(b, usec_sub_unsigned(a, x)); | |
1435 | } | |
9a9a4f10 LP |
1436 | |
1437 | bool in_utc_timezone(void) { | |
1438 | tzset(); | |
1439 | ||
1440 | return timezone == 0 && daylight == 0; | |
1441 | } |