]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9a98c7a1 | 2 | |
279f52a1 | 3 | #include <ctype.h> |
11c3a366 TA |
4 | #include <errno.h> |
5 | #include <limits.h> | |
6 | #include <stdlib.h> | |
9a98c7a1 | 7 | #include <string.h> |
48d26c01 | 8 | #include <sys/mman.h> |
11c3a366 TA |
9 | #include <sys/stat.h> |
10 | #include <sys/time.h> | |
77ff2de9 | 11 | #include <sys/timerfd.h> |
07630cea | 12 | #include <sys/timex.h> |
11c3a366 TA |
13 | #include <sys/types.h> |
14 | #include <unistd.h> | |
9a98c7a1 | 15 | |
b5efdb8a | 16 | #include "alloc-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
0d39fa9c | 18 | #include "fileio.h" |
f4f15635 | 19 | #include "fs-util.h" |
a2932d51 | 20 | #include "io-util.h" |
11c3a366 TA |
21 | #include "log.h" |
22 | #include "macro.h" | |
36dd5ffd | 23 | #include "missing.h" |
93cc7779 TA |
24 | #include "parse-util.h" |
25 | #include "path-util.h" | |
dccca82b | 26 | #include "process-util.h" |
d68c645b | 27 | #include "serialize.h" |
a2932d51 | 28 | #include "stat-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 | 282 | /* Let's not format times with years > 9999 */ |
d3d28024 | 283 | if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) { |
490c5a37 | 284 | assert(l >= STRLEN("--- XXXX-XX-XX XX:XX:XX") + 1); |
d3d28024 ZJS |
285 | strcpy(buf, "--- XXXX-XX-XX XX:XX:XX"); |
286 | return buf; | |
287 | } | |
1bb4b028 | 288 | |
21b3a0fc | 289 | sec = (time_t) (t / USEC_PER_SEC); /* Round down */ |
21b3a0fc LP |
290 | |
291 | if (!localtime_or_gmtime_r(&sec, &tm, utc)) | |
9a98c7a1 LP |
292 | return NULL; |
293 | ||
21b3a0fc LP |
294 | /* Start with the week day */ |
295 | assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays)); | |
296 | memcpy(buf, weekdays[tm.tm_wday], 4); | |
9a98c7a1 | 297 | |
21b3a0fc LP |
298 | /* Add the main components */ |
299 | if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0) | |
300 | return NULL; /* Doesn't fit */ | |
0056086a | 301 | |
21b3a0fc | 302 | /* Append the microseconds part, if that's requested */ |
0056086a | 303 | if (us) { |
21b3a0fc LP |
304 | n = strlen(buf); |
305 | if (n + 8 > l) | |
306 | return NULL; /* Microseconds part doesn't fit. */ | |
307 | ||
70887c5f | 308 | sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC); |
21b3a0fc LP |
309 | } |
310 | ||
311 | /* Append the timezone */ | |
312 | n = strlen(buf); | |
313 | if (utc) { | |
314 | /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the | |
315 | * obsolete "GMT" instead. */ | |
316 | if (n + 5 > l) | |
317 | return NULL; /* "UTC" doesn't fit. */ | |
318 | ||
319 | strcpy(buf + n, " UTC"); | |
320 | ||
321 | } else if (!isempty(tm.tm_zone)) { | |
322 | size_t tn; | |
323 | ||
324 | /* An explicit timezone is specified, let's use it, if it fits */ | |
325 | tn = strlen(tm.tm_zone); | |
326 | if (n + 1 + tn + 1 > l) { | |
327 | /* The full time zone does not fit in. Yuck. */ | |
328 | ||
329 | if (n + 1 + _POSIX_TZNAME_MAX + 1 > l) | |
330 | return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */ | |
331 | ||
332 | /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX | |
333 | * minimum time zone length. In this case suppress the timezone entirely, in order not to dump | |
334 | * an overly long, hard to read string on the user. This should be safe, because the user will | |
335 | * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */ | |
336 | } else { | |
337 | buf[n++] = ' '; | |
338 | strcpy(buf + n, tm.tm_zone); | |
339 | } | |
0056086a | 340 | } |
9a98c7a1 LP |
341 | |
342 | return buf; | |
343 | } | |
344 | ||
a62e83b4 | 345 | char *format_timestamp(char *buf, size_t l, usec_t t) { |
0056086a | 346 | return format_timestamp_internal(buf, l, t, false, false); |
a62e83b4 JS |
347 | } |
348 | ||
5ab99e07 | 349 | char *format_timestamp_utc(char *buf, size_t l, usec_t t) { |
0056086a | 350 | return format_timestamp_internal(buf, l, t, true, false); |
f02d8367 ZJS |
351 | } |
352 | ||
5ab99e07 | 353 | char *format_timestamp_us(char *buf, size_t l, usec_t t) { |
0056086a | 354 | return format_timestamp_internal(buf, l, t, false, true); |
5ab99e07 LP |
355 | } |
356 | ||
357 | char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) { | |
0056086a | 358 | return format_timestamp_internal(buf, l, t, true, true); |
5ab99e07 LP |
359 | } |
360 | ||
bbb8486e | 361 | char *format_timestamp_relative(char *buf, size_t l, usec_t t) { |
1fcf71f5 | 362 | const char *s; |
9a98c7a1 LP |
363 | usec_t n, d; |
364 | ||
65de0395 | 365 | if (t <= 0 || t == USEC_INFINITY) |
9a98c7a1 LP |
366 | return NULL; |
367 | ||
65de0395 | 368 | n = now(CLOCK_REALTIME); |
1fcf71f5 LP |
369 | if (n > t) { |
370 | d = n - t; | |
371 | s = "ago"; | |
372 | } else { | |
373 | d = t - n; | |
374 | s = "left"; | |
375 | } | |
9a98c7a1 LP |
376 | |
377 | if (d >= USEC_PER_YEAR) | |
609e002e | 378 | snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s", |
de0671ee ZJS |
379 | d / USEC_PER_YEAR, |
380 | (d % USEC_PER_YEAR) / USEC_PER_MONTH, s); | |
9a98c7a1 | 381 | else if (d >= USEC_PER_MONTH) |
609e002e | 382 | snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s", |
de0671ee ZJS |
383 | d / USEC_PER_MONTH, |
384 | (d % USEC_PER_MONTH) / USEC_PER_DAY, s); | |
9a98c7a1 | 385 | else if (d >= USEC_PER_WEEK) |
609e002e | 386 | snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s", |
de0671ee ZJS |
387 | d / USEC_PER_WEEK, |
388 | (d % USEC_PER_WEEK) / USEC_PER_DAY, s); | |
9a98c7a1 | 389 | else if (d >= 2*USEC_PER_DAY) |
609e002e | 390 | snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s); |
9a98c7a1 | 391 | else if (d >= 25*USEC_PER_HOUR) |
609e002e | 392 | snprintf(buf, l, "1 day " USEC_FMT "h %s", |
de0671ee | 393 | (d - USEC_PER_DAY) / USEC_PER_HOUR, s); |
9a98c7a1 | 394 | else if (d >= 6*USEC_PER_HOUR) |
609e002e | 395 | snprintf(buf, l, USEC_FMT "h %s", |
de0671ee | 396 | d / USEC_PER_HOUR, s); |
9a98c7a1 | 397 | else if (d >= USEC_PER_HOUR) |
609e002e | 398 | snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s", |
de0671ee ZJS |
399 | d / USEC_PER_HOUR, |
400 | (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s); | |
9a98c7a1 | 401 | else if (d >= 5*USEC_PER_MINUTE) |
609e002e | 402 | snprintf(buf, l, USEC_FMT "min %s", |
de0671ee | 403 | d / USEC_PER_MINUTE, s); |
9a98c7a1 | 404 | else if (d >= USEC_PER_MINUTE) |
609e002e | 405 | snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s", |
de0671ee ZJS |
406 | d / USEC_PER_MINUTE, |
407 | (d % USEC_PER_MINUTE) / USEC_PER_SEC, s); | |
9a98c7a1 | 408 | else if (d >= USEC_PER_SEC) |
609e002e | 409 | snprintf(buf, l, USEC_FMT "s %s", |
de0671ee | 410 | d / USEC_PER_SEC, s); |
9a98c7a1 | 411 | else if (d >= USEC_PER_MSEC) |
609e002e | 412 | snprintf(buf, l, USEC_FMT "ms %s", |
de0671ee | 413 | d / USEC_PER_MSEC, s); |
9a98c7a1 | 414 | else if (d > 0) |
de0671ee ZJS |
415 | snprintf(buf, l, USEC_FMT"us %s", |
416 | d, s); | |
9a98c7a1 LP |
417 | else |
418 | snprintf(buf, l, "now"); | |
419 | ||
420 | buf[l-1] = 0; | |
421 | return buf; | |
422 | } | |
423 | ||
2fa4092c | 424 | char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { |
9a98c7a1 LP |
425 | static const struct { |
426 | const char *suffix; | |
427 | usec_t usec; | |
428 | } table[] = { | |
eb55ec9f LP |
429 | { "y", USEC_PER_YEAR }, |
430 | { "month", USEC_PER_MONTH }, | |
431 | { "w", USEC_PER_WEEK }, | |
432 | { "d", USEC_PER_DAY }, | |
433 | { "h", USEC_PER_HOUR }, | |
434 | { "min", USEC_PER_MINUTE }, | |
435 | { "s", USEC_PER_SEC }, | |
436 | { "ms", USEC_PER_MSEC }, | |
437 | { "us", 1 }, | |
9a98c7a1 LP |
438 | }; |
439 | ||
da6053d0 | 440 | size_t i; |
9a98c7a1 | 441 | char *p = buf; |
2fa4092c | 442 | bool something = false; |
9a98c7a1 LP |
443 | |
444 | assert(buf); | |
445 | assert(l > 0); | |
446 | ||
bb1fada8 LP |
447 | if (t == USEC_INFINITY) { |
448 | strncpy(p, "infinity", l-1); | |
449 | p[l-1] = 0; | |
450 | return p; | |
451 | } | |
452 | ||
453 | if (t <= 0) { | |
454 | strncpy(p, "0", l-1); | |
7c537b2e LP |
455 | p[l-1] = 0; |
456 | return p; | |
457 | } | |
458 | ||
7f602784 | 459 | /* The result of this function can be parsed with parse_sec */ |
9a98c7a1 LP |
460 | |
461 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
7759ecb2 | 462 | int k = 0; |
9a98c7a1 | 463 | size_t n; |
2fa4092c LP |
464 | bool done = false; |
465 | usec_t a, b; | |
466 | ||
7c537b2e LP |
467 | if (t <= 0) |
468 | break; | |
2fa4092c | 469 | |
7c537b2e | 470 | if (t < accuracy && something) |
2fa4092c | 471 | break; |
9a98c7a1 LP |
472 | |
473 | if (t < table[i].usec) | |
474 | continue; | |
475 | ||
476 | if (l <= 1) | |
477 | break; | |
478 | ||
2fa4092c LP |
479 | a = t / table[i].usec; |
480 | b = t % table[i].usec; | |
481 | ||
482 | /* Let's see if we should shows this in dot notation */ | |
483 | if (t < USEC_PER_MINUTE && b > 0) { | |
484 | usec_t cc; | |
5fd8d5be | 485 | signed char j; |
2fa4092c LP |
486 | |
487 | j = 0; | |
488 | for (cc = table[i].usec; cc > 1; cc /= 10) | |
489 | j++; | |
490 | ||
491 | for (cc = accuracy; cc > 1; cc /= 10) { | |
492 | b /= 10; | |
493 | j--; | |
494 | } | |
495 | ||
496 | if (j > 0) { | |
497 | k = snprintf(p, l, | |
70887c5f | 498 | "%s"USEC_FMT".%0*"PRI_USEC"%s", |
2fa4092c | 499 | p > buf ? " " : "", |
de0671ee | 500 | a, |
2fa4092c | 501 | j, |
70887c5f | 502 | b, |
2fa4092c LP |
503 | table[i].suffix); |
504 | ||
505 | t = 0; | |
506 | done = true; | |
507 | } | |
508 | } | |
509 | ||
510 | /* No? Then let's show it normally */ | |
511 | if (!done) { | |
512 | k = snprintf(p, l, | |
de0671ee | 513 | "%s"USEC_FMT"%s", |
2fa4092c | 514 | p > buf ? " " : "", |
de0671ee | 515 | a, |
2fa4092c LP |
516 | table[i].suffix); |
517 | ||
518 | t = b; | |
519 | } | |
520 | ||
9a98c7a1 LP |
521 | n = MIN((size_t) k, l); |
522 | ||
523 | l -= n; | |
524 | p += n; | |
525 | ||
2fa4092c | 526 | something = true; |
9a98c7a1 LP |
527 | } |
528 | ||
529 | *p = 0; | |
530 | ||
531 | return buf; | |
532 | } | |
533 | ||
48d26c01 | 534 | static int parse_timestamp_impl(const char *t, usec_t *usec, bool with_tz) { |
92134489 LP |
535 | static const struct { |
536 | const char *name; | |
537 | const int nr; | |
538 | } day_nr[] = { | |
539 | { "Sunday", 0 }, | |
540 | { "Sun", 0 }, | |
541 | { "Monday", 1 }, | |
542 | { "Mon", 1 }, | |
543 | { "Tuesday", 2 }, | |
544 | { "Tue", 2 }, | |
545 | { "Wednesday", 3 }, | |
546 | { "Wed", 3 }, | |
547 | { "Thursday", 4 }, | |
548 | { "Thu", 4 }, | |
549 | { "Friday", 5 }, | |
550 | { "Fri", 5 }, | |
551 | { "Saturday", 6 }, | |
552 | { "Sat", 6 }, | |
553 | }; | |
554 | ||
48d26c01 | 555 | const char *k, *utc = NULL, *tzn = NULL; |
9a98c7a1 LP |
556 | struct tm tm, copy; |
557 | time_t x; | |
e4eaf99a | 558 | usec_t x_usec, plus = 0, minus = 0, ret; |
21b3a0fc | 559 | int r, weekday = -1, dst = -1; |
da6053d0 | 560 | size_t i; |
9a98c7a1 | 561 | |
947f9f01 | 562 | /* Allowed syntaxes: |
9a98c7a1 LP |
563 | * |
564 | * 2012-09-22 16:34:22 | |
565 | * 2012-09-22 16:34 (seconds will be set to 0) | |
566 | * 2012-09-22 (time will be set to 00:00:00) | |
567 | * 16:34:22 (date will be set to today) | |
568 | * 16:34 (date will be set to today, seconds to 0) | |
569 | * now | |
570 | * yesterday (time is set to 00:00:00) | |
571 | * today (time is set to 00:00:00) | |
572 | * tomorrow (time is set to 00:00:00) | |
573 | * +5min | |
574 | * -5days | |
5ba6e094 | 575 | * @2147483647 (seconds since epoch) |
9a98c7a1 LP |
576 | */ |
577 | ||
578 | assert(t); | |
579 | assert(usec); | |
580 | ||
48d26c01 | 581 | if (t[0] == '@' && !with_tz) |
e4eaf99a | 582 | return parse_sec(t + 1, usec); |
9a98c7a1 | 583 | |
e4eaf99a | 584 | ret = now(CLOCK_REALTIME); |
9a98c7a1 | 585 | |
48d26c01 IK |
586 | if (!with_tz) { |
587 | if (streq(t, "now")) | |
588 | goto finish; | |
9a98c7a1 | 589 | |
48d26c01 IK |
590 | else if (t[0] == '+') { |
591 | r = parse_sec(t+1, &plus); | |
592 | if (r < 0) | |
593 | return r; | |
9a98c7a1 | 594 | |
48d26c01 | 595 | goto finish; |
9a98c7a1 | 596 | |
48d26c01 IK |
597 | } else if (t[0] == '-') { |
598 | r = parse_sec(t+1, &minus); | |
599 | if (r < 0) | |
600 | return r; | |
9a98c7a1 | 601 | |
48d26c01 | 602 | goto finish; |
decad910 | 603 | |
48d26c01 IK |
604 | } else if ((k = endswith(t, " ago"))) { |
605 | t = strndupa(t, k - t); | |
decad910 | 606 | |
48d26c01 IK |
607 | r = parse_sec(t, &minus); |
608 | if (r < 0) | |
609 | return r; | |
decad910 | 610 | |
48d26c01 | 611 | goto finish; |
1fcf71f5 | 612 | |
48d26c01 IK |
613 | } else if ((k = endswith(t, " left"))) { |
614 | t = strndupa(t, k - t); | |
1fcf71f5 | 615 | |
48d26c01 IK |
616 | r = parse_sec(t, &plus); |
617 | if (r < 0) | |
618 | return r; | |
1fcf71f5 | 619 | |
48d26c01 IK |
620 | goto finish; |
621 | } | |
9a98c7a1 | 622 | |
48d26c01 IK |
623 | /* See if the timestamp is suffixed with UTC */ |
624 | utc = endswith_no_case(t, " UTC"); | |
625 | if (utc) | |
626 | t = strndupa(t, utc - t); | |
627 | else { | |
628 | const char *e = NULL; | |
629 | int j; | |
21b3a0fc | 630 | |
48d26c01 | 631 | tzset(); |
21b3a0fc | 632 | |
48d26c01 | 633 | /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only |
947f9f01 YW |
634 | * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because |
635 | * there are no nice APIs available to cover this. By accepting the local time zone strings, we make | |
636 | * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't | |
637 | * support arbitrary timezone specifications. */ | |
e4eaf99a | 638 | |
48d26c01 | 639 | for (j = 0; j <= 1; j++) { |
21b3a0fc | 640 | |
48d26c01 IK |
641 | if (isempty(tzname[j])) |
642 | continue; | |
21b3a0fc | 643 | |
48d26c01 IK |
644 | e = endswith_no_case(t, tzname[j]); |
645 | if (!e) | |
646 | continue; | |
647 | if (e == t) | |
648 | continue; | |
649 | if (e[-1] != ' ') | |
650 | continue; | |
21b3a0fc | 651 | |
48d26c01 IK |
652 | break; |
653 | } | |
21b3a0fc | 654 | |
48d26c01 IK |
655 | if (IN_SET(j, 0, 1)) { |
656 | /* Found one of the two timezones specified. */ | |
657 | t = strndupa(t, e - t - 1); | |
658 | dst = j; | |
659 | tzn = tzname[j]; | |
660 | } | |
21b3a0fc LP |
661 | } |
662 | } | |
663 | ||
664 | x = (time_t) (ret / USEC_PER_SEC); | |
e4eaf99a HV |
665 | x_usec = 0; |
666 | ||
21b3a0fc LP |
667 | if (!localtime_or_gmtime_r(&x, &tm, utc)) |
668 | return -EINVAL; | |
669 | ||
2e72b794 IK |
670 | tm.tm_isdst = dst; |
671 | if (!with_tz && tzn) | |
672 | tm.tm_zone = tzn; | |
e4eaf99a HV |
673 | |
674 | if (streq(t, "today")) { | |
675 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; | |
676 | goto from_tm; | |
677 | ||
678 | } else if (streq(t, "yesterday")) { | |
313cefa1 | 679 | tm.tm_mday--; |
e4eaf99a HV |
680 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; |
681 | goto from_tm; | |
682 | ||
683 | } else if (streq(t, "tomorrow")) { | |
313cefa1 | 684 | tm.tm_mday++; |
e4eaf99a HV |
685 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; |
686 | goto from_tm; | |
687 | } | |
688 | ||
92134489 LP |
689 | for (i = 0; i < ELEMENTSOF(day_nr); i++) { |
690 | size_t skip; | |
691 | ||
692 | if (!startswith_no_case(t, day_nr[i].name)) | |
693 | continue; | |
694 | ||
695 | skip = strlen(day_nr[i].name); | |
696 | if (t[skip] != ' ') | |
697 | continue; | |
698 | ||
699 | weekday = day_nr[i].nr; | |
700 | t += skip + 1; | |
701 | break; | |
702 | } | |
703 | ||
9a98c7a1 LP |
704 | copy = tm; |
705 | k = strptime(t, "%y-%m-%d %H:%M:%S", &tm); | |
e4eaf99a HV |
706 | if (k) { |
707 | if (*k == '.') | |
708 | goto parse_usec; | |
709 | else if (*k == 0) | |
710 | goto from_tm; | |
711 | } | |
9a98c7a1 LP |
712 | |
713 | tm = copy; | |
714 | k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm); | |
e4eaf99a HV |
715 | if (k) { |
716 | if (*k == '.') | |
717 | goto parse_usec; | |
718 | else if (*k == 0) | |
719 | goto from_tm; | |
720 | } | |
9a98c7a1 LP |
721 | |
722 | tm = copy; | |
723 | k = strptime(t, "%y-%m-%d %H:%M", &tm); | |
724 | if (k && *k == 0) { | |
725 | tm.tm_sec = 0; | |
e4eaf99a | 726 | goto from_tm; |
9a98c7a1 LP |
727 | } |
728 | ||
729 | tm = copy; | |
730 | k = strptime(t, "%Y-%m-%d %H:%M", &tm); | |
731 | if (k && *k == 0) { | |
732 | tm.tm_sec = 0; | |
e4eaf99a | 733 | goto from_tm; |
9a98c7a1 LP |
734 | } |
735 | ||
736 | tm = copy; | |
737 | k = strptime(t, "%y-%m-%d", &tm); | |
738 | if (k && *k == 0) { | |
739 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; | |
e4eaf99a | 740 | goto from_tm; |
9a98c7a1 LP |
741 | } |
742 | ||
743 | tm = copy; | |
744 | k = strptime(t, "%Y-%m-%d", &tm); | |
745 | if (k && *k == 0) { | |
746 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; | |
e4eaf99a | 747 | goto from_tm; |
9a98c7a1 LP |
748 | } |
749 | ||
750 | tm = copy; | |
751 | k = strptime(t, "%H:%M:%S", &tm); | |
e4eaf99a HV |
752 | if (k) { |
753 | if (*k == '.') | |
754 | goto parse_usec; | |
755 | else if (*k == 0) | |
756 | goto from_tm; | |
757 | } | |
9a98c7a1 LP |
758 | |
759 | tm = copy; | |
760 | k = strptime(t, "%H:%M", &tm); | |
761 | if (k && *k == 0) { | |
762 | tm.tm_sec = 0; | |
e4eaf99a | 763 | goto from_tm; |
9a98c7a1 LP |
764 | } |
765 | ||
766 | return -EINVAL; | |
767 | ||
e4eaf99a HV |
768 | parse_usec: |
769 | { | |
436dd70f | 770 | unsigned add; |
e4eaf99a HV |
771 | |
772 | k++; | |
436dd70f HV |
773 | r = parse_fractional_part_u(&k, 6, &add); |
774 | if (r < 0) | |
e4eaf99a HV |
775 | return -EINVAL; |
776 | ||
436dd70f | 777 | if (*k) |
e4eaf99a HV |
778 | return -EINVAL; |
779 | ||
436dd70f | 780 | x_usec = add; |
e4eaf99a HV |
781 | } |
782 | ||
783 | from_tm: | |
214cc95d | 784 | if (weekday >= 0 && tm.tm_wday != weekday) |
68bdd2d2 | 785 | return -EINVAL; |
9a98c7a1 | 786 | |
214cc95d MH |
787 | x = mktime_or_timegm(&tm, utc); |
788 | if (x < 0) | |
92134489 LP |
789 | return -EINVAL; |
790 | ||
68bdd2d2 | 791 | ret = (usec_t) x * USEC_PER_SEC + x_usec; |
1bb4b028 LP |
792 | if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX) |
793 | return -EINVAL; | |
9a98c7a1 | 794 | |
e4eaf99a | 795 | finish: |
315782db | 796 | if (ret + plus < ret) /* overflow? */ |
68bdd2d2 | 797 | return -EINVAL; |
9a98c7a1 | 798 | ret += plus; |
1bb4b028 LP |
799 | if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX) |
800 | return -EINVAL; | |
801 | ||
68bdd2d2 | 802 | if (ret >= minus) |
9a98c7a1 LP |
803 | ret -= minus; |
804 | else | |
68bdd2d2 | 805 | return -EINVAL; |
9a98c7a1 LP |
806 | |
807 | *usec = ret; | |
808 | ||
809 | return 0; | |
810 | } | |
811 | ||
48d26c01 IK |
812 | typedef struct ParseTimestampResult { |
813 | usec_t usec; | |
814 | int return_value; | |
815 | } ParseTimestampResult; | |
816 | ||
817 | int parse_timestamp(const char *t, usec_t *usec) { | |
ff69484a | 818 | char *last_space, *tz = NULL; |
48d26c01 | 819 | ParseTimestampResult *shared, tmp; |
4c253ed1 | 820 | int r; |
48d26c01 IK |
821 | |
822 | last_space = strrchr(t, ' '); | |
089fb865 | 823 | if (last_space != NULL && timezone_is_valid(last_space + 1, LOG_DEBUG)) |
ff69484a | 824 | tz = last_space + 1; |
48d26c01 | 825 | |
234519ae | 826 | if (!tz || endswith_no_case(t, " UTC")) |
48d26c01 IK |
827 | return parse_timestamp_impl(t, usec, false); |
828 | ||
48d26c01 IK |
829 | shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
830 | if (shared == MAP_FAILED) | |
831 | return negative_errno(); | |
832 | ||
1f5d1e02 | 833 | r = safe_fork("(sd-timestamp)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL); |
4c253ed1 | 834 | if (r < 0) { |
48d26c01 | 835 | (void) munmap(shared, sizeof *shared); |
4c253ed1 | 836 | return r; |
48d26c01 | 837 | } |
4c253ed1 | 838 | if (r == 0) { |
3fd4929b MH |
839 | bool with_tz = true; |
840 | ||
ff69484a | 841 | if (setenv("TZ", tz, 1) != 0) { |
48d26c01 IK |
842 | shared->return_value = negative_errno(); |
843 | _exit(EXIT_FAILURE); | |
844 | } | |
845 | ||
846 | tzset(); | |
847 | ||
3fd4929b | 848 | /* If there is a timezone that matches the tzname fields, leave the parsing to the implementation. |
947f9f01 | 849 | * Otherwise just cut it off. */ |
3fd4929b MH |
850 | with_tz = !STR_IN_SET(tz, tzname[0], tzname[1]); |
851 | ||
f21f31b2 | 852 | /* Cut off the timezone if we don't need it. */ |
3fd4929b MH |
853 | if (with_tz) |
854 | t = strndupa(t, last_space - t); | |
855 | ||
856 | shared->return_value = parse_timestamp_impl(t, &shared->usec, with_tz); | |
48d26c01 IK |
857 | |
858 | _exit(EXIT_SUCCESS); | |
859 | } | |
860 | ||
48d26c01 IK |
861 | tmp = *shared; |
862 | if (munmap(shared, sizeof *shared) != 0) | |
863 | return negative_errno(); | |
864 | ||
865 | if (tmp.return_value == 0) | |
866 | *usec = tmp.usec; | |
867 | ||
868 | return tmp.return_value; | |
869 | } | |
870 | ||
ed2e7967 | 871 | static const char* extract_multiplier(const char *p, usec_t *multiplier) { |
9a98c7a1 LP |
872 | static const struct { |
873 | const char *suffix; | |
874 | usec_t usec; | |
875 | } table[] = { | |
eb55ec9f LP |
876 | { "seconds", USEC_PER_SEC }, |
877 | { "second", USEC_PER_SEC }, | |
878 | { "sec", USEC_PER_SEC }, | |
879 | { "s", USEC_PER_SEC }, | |
9a98c7a1 | 880 | { "minutes", USEC_PER_MINUTE }, |
eb55ec9f LP |
881 | { "minute", USEC_PER_MINUTE }, |
882 | { "min", USEC_PER_MINUTE }, | |
883 | { "months", USEC_PER_MONTH }, | |
884 | { "month", USEC_PER_MONTH }, | |
885 | { "M", USEC_PER_MONTH }, | |
886 | { "msec", USEC_PER_MSEC }, | |
887 | { "ms", USEC_PER_MSEC }, | |
888 | { "m", USEC_PER_MINUTE }, | |
889 | { "hours", USEC_PER_HOUR }, | |
890 | { "hour", USEC_PER_HOUR }, | |
891 | { "hr", USEC_PER_HOUR }, | |
892 | { "h", USEC_PER_HOUR }, | |
893 | { "days", USEC_PER_DAY }, | |
894 | { "day", USEC_PER_DAY }, | |
895 | { "d", USEC_PER_DAY }, | |
896 | { "weeks", USEC_PER_WEEK }, | |
897 | { "week", USEC_PER_WEEK }, | |
898 | { "w", USEC_PER_WEEK }, | |
899 | { "years", USEC_PER_YEAR }, | |
900 | { "year", USEC_PER_YEAR }, | |
901 | { "y", USEC_PER_YEAR }, | |
902 | { "usec", 1ULL }, | |
903 | { "us", 1ULL }, | |
5efdbf11 | 904 | { "µs", 1ULL }, |
9a98c7a1 | 905 | }; |
da6053d0 | 906 | size_t i; |
240a7ba9 ZJS |
907 | |
908 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
909 | char *e; | |
910 | ||
911 | e = startswith(p, table[i].suffix); | |
912 | if (e) { | |
913 | *multiplier = table[i].usec; | |
914 | return e; | |
915 | } | |
916 | } | |
9a98c7a1 | 917 | |
240a7ba9 ZJS |
918 | return p; |
919 | } | |
920 | ||
921 | int parse_time(const char *t, usec_t *usec, usec_t default_unit) { | |
b1d6dcf5 | 922 | const char *p, *s; |
9a98c7a1 | 923 | usec_t r = 0; |
cb0dac05 | 924 | bool something = false; |
9a98c7a1 LP |
925 | |
926 | assert(t); | |
927 | assert(usec); | |
519cffec | 928 | assert(default_unit > 0); |
9a98c7a1 LP |
929 | |
930 | p = t; | |
b1d6dcf5 ZJS |
931 | |
932 | p += strspn(p, WHITESPACE); | |
933 | s = startswith(p, "infinity"); | |
934 | if (s) { | |
935 | s += strspn(s, WHITESPACE); | |
936 | if (*s != 0) | |
937 | return -EINVAL; | |
938 | ||
939 | *usec = USEC_INFINITY; | |
940 | return 0; | |
941 | } | |
942 | ||
cb0dac05 | 943 | for (;;) { |
5a9fb358 | 944 | usec_t multiplier = default_unit, k; |
ed2e7967 | 945 | long long l; |
5a9fb358 | 946 | char *e; |
cb0dac05 LP |
947 | |
948 | p += strspn(p, WHITESPACE); | |
949 | ||
950 | if (*p == 0) { | |
951 | if (!something) | |
952 | return -EINVAL; | |
953 | ||
954 | break; | |
955 | } | |
9a98c7a1 | 956 | |
5a9fb358 LP |
957 | if (*p == '-') /* Don't allow "-0" */ |
958 | return -ERANGE; | |
959 | ||
9a98c7a1 LP |
960 | errno = 0; |
961 | l = strtoll(p, &e, 10); | |
8333c77e | 962 | if (errno > 0) |
9a98c7a1 | 963 | return -errno; |
9a98c7a1 LP |
964 | if (l < 0) |
965 | return -ERANGE; | |
966 | ||
cb0dac05 | 967 | if (*e == '.') { |
ed2e7967 YW |
968 | p = e + 1; |
969 | p += strspn(p, DIGITS); | |
cb0dac05 | 970 | } else if (e == p) |
9a98c7a1 | 971 | return -EINVAL; |
ed2e7967 YW |
972 | else |
973 | p = e; | |
9a98c7a1 | 974 | |
ed2e7967 YW |
975 | s = extract_multiplier(p + strspn(p, WHITESPACE), &multiplier); |
976 | if (s == p && *s != '\0') | |
977 | /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56'*/ | |
978 | return -EINVAL; | |
9a98c7a1 | 979 | |
ed2e7967 | 980 | p = s; |
519cffec | 981 | |
ed2e7967 YW |
982 | if ((usec_t) l >= USEC_INFINITY / multiplier) |
983 | return -ERANGE; | |
8079c903 | 984 | |
ed2e7967 YW |
985 | k = (usec_t) l * multiplier; |
986 | if (k >= USEC_INFINITY - r) | |
8079c903 YW |
987 | return -ERANGE; |
988 | ||
ed2e7967 | 989 | r += k; |
519cffec | 990 | |
ed2e7967 | 991 | something = true; |
519cffec | 992 | |
ed2e7967 YW |
993 | if (*e == '.') { |
994 | usec_t m = multiplier / 10; | |
995 | const char *b; | |
8079c903 | 996 | |
ed2e7967 YW |
997 | for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) { |
998 | k = (usec_t) (*b - '0') * m; | |
999 | if (k >= USEC_INFINITY - r) | |
1000 | return -ERANGE; | |
1001 | ||
1002 | r += k; | |
1003 | } | |
1004 | ||
1005 | /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge"*/ | |
1006 | if (b == e + 1) | |
1007 | return -EINVAL; | |
1008 | } | |
cb0dac05 | 1009 | } |
9a98c7a1 LP |
1010 | |
1011 | *usec = r; | |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
519cffec LP |
1016 | int parse_sec(const char *t, usec_t *usec) { |
1017 | return parse_time(t, usec, USEC_PER_SEC); | |
1018 | } | |
1019 | ||
def34f63 LP |
1020 | int parse_sec_fix_0(const char *t, usec_t *ret) { |
1021 | usec_t k; | |
1022 | int r; | |
eae51da3 | 1023 | |
def34f63 LP |
1024 | assert(t); |
1025 | assert(ret); | |
eae51da3 | 1026 | |
def34f63 LP |
1027 | r = parse_sec(t, &k); |
1028 | if (r < 0) | |
1029 | return r; | |
0004f698 | 1030 | |
def34f63 LP |
1031 | *ret = k == 0 ? USEC_INFINITY : k; |
1032 | return r; | |
0004f698 ZJS |
1033 | } |
1034 | ||
ed2e7967 | 1035 | static const char* extract_nsec_multiplier(const char *p, nsec_t *multiplier) { |
9a98c7a1 LP |
1036 | static const struct { |
1037 | const char *suffix; | |
1038 | nsec_t nsec; | |
1039 | } table[] = { | |
ed2e7967 YW |
1040 | { "seconds", NSEC_PER_SEC }, |
1041 | { "second", NSEC_PER_SEC }, | |
1042 | { "sec", NSEC_PER_SEC }, | |
1043 | { "s", NSEC_PER_SEC }, | |
9a98c7a1 | 1044 | { "minutes", NSEC_PER_MINUTE }, |
ed2e7967 YW |
1045 | { "minute", NSEC_PER_MINUTE }, |
1046 | { "min", NSEC_PER_MINUTE }, | |
1047 | { "months", NSEC_PER_MONTH }, | |
1048 | { "month", NSEC_PER_MONTH }, | |
1049 | { "M", NSEC_PER_MONTH }, | |
1050 | { "msec", NSEC_PER_MSEC }, | |
1051 | { "ms", NSEC_PER_MSEC }, | |
1052 | { "m", NSEC_PER_MINUTE }, | |
1053 | { "hours", NSEC_PER_HOUR }, | |
1054 | { "hour", NSEC_PER_HOUR }, | |
1055 | { "hr", NSEC_PER_HOUR }, | |
1056 | { "h", NSEC_PER_HOUR }, | |
1057 | { "days", NSEC_PER_DAY }, | |
1058 | { "day", NSEC_PER_DAY }, | |
1059 | { "d", NSEC_PER_DAY }, | |
1060 | { "weeks", NSEC_PER_WEEK }, | |
1061 | { "week", NSEC_PER_WEEK }, | |
1062 | { "w", NSEC_PER_WEEK }, | |
1063 | { "years", NSEC_PER_YEAR }, | |
1064 | { "year", NSEC_PER_YEAR }, | |
1065 | { "y", NSEC_PER_YEAR }, | |
1066 | { "usec", NSEC_PER_USEC }, | |
1067 | { "us", NSEC_PER_USEC }, | |
1068 | { "µs", NSEC_PER_USEC }, | |
1069 | { "nsec", 1ULL }, | |
1070 | { "ns", 1ULL }, | |
1071 | { "", 1ULL }, /* default is nsec */ | |
9a98c7a1 | 1072 | }; |
ed2e7967 YW |
1073 | size_t i; |
1074 | ||
1075 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
1076 | char *e; | |
1077 | ||
1078 | e = startswith(p, table[i].suffix); | |
1079 | if (e) { | |
1080 | *multiplier = table[i].nsec; | |
1081 | return e; | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | return p; | |
1086 | } | |
9a98c7a1 | 1087 | |
ed2e7967 | 1088 | int parse_nsec(const char *t, nsec_t *nsec) { |
e73c78c2 | 1089 | const char *p, *s; |
9a98c7a1 | 1090 | nsec_t r = 0; |
cb0dac05 | 1091 | bool something = false; |
9a98c7a1 LP |
1092 | |
1093 | assert(t); | |
1094 | assert(nsec); | |
1095 | ||
1096 | p = t; | |
e73c78c2 LP |
1097 | |
1098 | p += strspn(p, WHITESPACE); | |
1099 | s = startswith(p, "infinity"); | |
1100 | if (s) { | |
1101 | s += strspn(s, WHITESPACE); | |
8e8933ca | 1102 | if (*s != 0) |
e73c78c2 LP |
1103 | return -EINVAL; |
1104 | ||
1105 | *nsec = NSEC_INFINITY; | |
1106 | return 0; | |
1107 | } | |
1108 | ||
cb0dac05 | 1109 | for (;;) { |
ed2e7967 YW |
1110 | nsec_t multiplier = 1, k; |
1111 | long long l; | |
9a98c7a1 | 1112 | char *e; |
cb0dac05 LP |
1113 | |
1114 | p += strspn(p, WHITESPACE); | |
1115 | ||
1116 | if (*p == 0) { | |
1117 | if (!something) | |
1118 | return -EINVAL; | |
1119 | ||
1120 | break; | |
1121 | } | |
9a98c7a1 | 1122 | |
ed2e7967 | 1123 | if (*p == '-') /* Don't allow "-0" */ |
5a9fb358 LP |
1124 | return -ERANGE; |
1125 | ||
9a98c7a1 LP |
1126 | errno = 0; |
1127 | l = strtoll(p, &e, 10); | |
8333c77e | 1128 | if (errno > 0) |
9a98c7a1 | 1129 | return -errno; |
9a98c7a1 LP |
1130 | if (l < 0) |
1131 | return -ERANGE; | |
1132 | ||
cb0dac05 | 1133 | if (*e == '.') { |
ed2e7967 YW |
1134 | p = e + 1; |
1135 | p += strspn(p, DIGITS); | |
cb0dac05 | 1136 | } else if (e == p) |
9a98c7a1 | 1137 | return -EINVAL; |
ed2e7967 YW |
1138 | else |
1139 | p = e; | |
9a98c7a1 | 1140 | |
ed2e7967 YW |
1141 | s = extract_nsec_multiplier(p + strspn(p, WHITESPACE), &multiplier); |
1142 | if (s == p && *s != '\0') | |
1143 | /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56'*/ | |
1144 | return -EINVAL; | |
9a98c7a1 | 1145 | |
ed2e7967 | 1146 | p = s; |
f6a178e9 | 1147 | |
ed2e7967 YW |
1148 | if ((nsec_t) l >= NSEC_INFINITY / multiplier) |
1149 | return -ERANGE; | |
1150 | ||
1151 | k = (nsec_t) l * multiplier; | |
1152 | if (k >= NSEC_INFINITY - r) | |
1153 | return -ERANGE; | |
f6a178e9 | 1154 | |
ed2e7967 YW |
1155 | r += k; |
1156 | ||
1157 | something = true; | |
cb0dac05 | 1158 | |
ed2e7967 YW |
1159 | if (*e == '.') { |
1160 | nsec_t m = multiplier / 10; | |
1161 | const char *b; | |
cb0dac05 | 1162 | |
ed2e7967 YW |
1163 | for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) { |
1164 | k = (nsec_t) (*b - '0') * m; | |
1165 | if (k >= NSEC_INFINITY - r) | |
f6a178e9 YW |
1166 | return -ERANGE; |
1167 | ||
1168 | r += k; | |
9a98c7a1 LP |
1169 | } |
1170 | ||
ed2e7967 YW |
1171 | /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge"*/ |
1172 | if (b == e + 1) | |
1173 | return -EINVAL; | |
1174 | } | |
cb0dac05 | 1175 | } |
9a98c7a1 LP |
1176 | |
1177 | *nsec = r; | |
1178 | ||
1179 | return 0; | |
1180 | } | |
03cc26dd LP |
1181 | |
1182 | bool ntp_synced(void) { | |
1183 | struct timex txc = {}; | |
1184 | ||
1185 | if (adjtimex(&txc) < 0) | |
1186 | return false; | |
1187 | ||
1188 | if (txc.status & STA_UNSYNC) | |
1189 | return false; | |
1190 | ||
1191 | return true; | |
1192 | } | |
75683450 LP |
1193 | |
1194 | int get_timezones(char ***ret) { | |
1195 | _cleanup_fclose_ FILE *f = NULL; | |
1196 | _cleanup_strv_free_ char **zones = NULL; | |
1197 | size_t n_zones = 0, n_allocated = 0; | |
8d2b9d14 | 1198 | int r; |
75683450 LP |
1199 | |
1200 | assert(ret); | |
1201 | ||
bea1a013 | 1202 | zones = strv_new("UTC"); |
75683450 LP |
1203 | if (!zones) |
1204 | return -ENOMEM; | |
1205 | ||
1206 | n_allocated = 2; | |
1207 | n_zones = 1; | |
1208 | ||
1209 | f = fopen("/usr/share/zoneinfo/zone.tab", "re"); | |
1210 | if (f) { | |
8d2b9d14 LP |
1211 | for (;;) { |
1212 | _cleanup_free_ char *line = NULL; | |
75683450 LP |
1213 | char *p, *w; |
1214 | size_t k; | |
1215 | ||
8d2b9d14 LP |
1216 | r = read_line(f, LONG_LINE_MAX, &line); |
1217 | if (r < 0) | |
1218 | return r; | |
1219 | if (r == 0) | |
1220 | break; | |
1221 | ||
1222 | p = strstrip(line); | |
75683450 LP |
1223 | |
1224 | if (isempty(p) || *p == '#') | |
1225 | continue; | |
1226 | ||
1227 | /* Skip over country code */ | |
1228 | p += strcspn(p, WHITESPACE); | |
1229 | p += strspn(p, WHITESPACE); | |
1230 | ||
1231 | /* Skip over coordinates */ | |
1232 | p += strcspn(p, WHITESPACE); | |
1233 | p += strspn(p, WHITESPACE); | |
1234 | ||
1235 | /* Found timezone name */ | |
1236 | k = strcspn(p, WHITESPACE); | |
1237 | if (k <= 0) | |
1238 | continue; | |
1239 | ||
1240 | w = strndup(p, k); | |
1241 | if (!w) | |
1242 | return -ENOMEM; | |
1243 | ||
1244 | if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) { | |
1245 | free(w); | |
1246 | return -ENOMEM; | |
1247 | } | |
1248 | ||
1249 | zones[n_zones++] = w; | |
1250 | zones[n_zones] = NULL; | |
1251 | } | |
1252 | ||
1253 | strv_sort(zones); | |
1254 | ||
1255 | } else if (errno != ENOENT) | |
1256 | return -errno; | |
1257 | ||
1cc6c93a | 1258 | *ret = TAKE_PTR(zones); |
75683450 LP |
1259 | |
1260 | return 0; | |
1261 | } | |
1262 | ||
089fb865 | 1263 | bool timezone_is_valid(const char *name, int log_level) { |
75683450 LP |
1264 | bool slash = false; |
1265 | const char *p, *t; | |
a2932d51 MG |
1266 | _cleanup_close_ int fd = -1; |
1267 | char buf[4]; | |
1268 | int r; | |
75683450 | 1269 | |
5c904ba5 LP |
1270 | if (isempty(name)) |
1271 | return false; | |
1272 | ||
1273 | if (name[0] == '/') | |
75683450 LP |
1274 | return false; |
1275 | ||
1276 | for (p = name; *p; p++) { | |
1277 | if (!(*p >= '0' && *p <= '9') && | |
1278 | !(*p >= 'a' && *p <= 'z') && | |
1279 | !(*p >= 'A' && *p <= 'Z') && | |
4c701096 | 1280 | !IN_SET(*p, '-', '_', '+', '/')) |
75683450 LP |
1281 | return false; |
1282 | ||
1283 | if (*p == '/') { | |
1284 | ||
1285 | if (slash) | |
1286 | return false; | |
1287 | ||
1288 | slash = true; | |
1289 | } else | |
1290 | slash = false; | |
1291 | } | |
1292 | ||
1293 | if (slash) | |
1294 | return false; | |
1295 | ||
1c73b60b YW |
1296 | if (p - name >= PATH_MAX) |
1297 | return false; | |
1298 | ||
63c372cb | 1299 | t = strjoina("/usr/share/zoneinfo/", name); |
a2932d51 MG |
1300 | |
1301 | fd = open(t, O_RDONLY|O_CLOEXEC); | |
1302 | if (fd < 0) { | |
089fb865 | 1303 | log_full_errno(log_level, errno, "Failed to open timezone file '%s': %m", t); |
a2932d51 MG |
1304 | return false; |
1305 | } | |
1306 | ||
1307 | r = fd_verify_regular(fd); | |
1308 | if (r < 0) { | |
089fb865 | 1309 | log_full_errno(log_level, r, "Timezone file '%s' is not a regular file: %m", t); |
75683450 | 1310 | return false; |
a2932d51 MG |
1311 | } |
1312 | ||
1313 | r = loop_read_exact(fd, buf, 4, false); | |
1314 | if (r < 0) { | |
089fb865 | 1315 | log_full_errno(log_level, r, "Failed to read from timezone file '%s': %m", t); |
a2932d51 MG |
1316 | return false; |
1317 | } | |
75683450 | 1318 | |
a2932d51 MG |
1319 | /* Magic from tzfile(5) */ |
1320 | if (memcmp(buf, "TZif", 4) != 0) { | |
089fb865 | 1321 | log_full(log_level, "Timezone file '%s' has wrong magic bytes", t); |
75683450 | 1322 | return false; |
a2932d51 | 1323 | } |
75683450 LP |
1324 | |
1325 | return true; | |
1326 | } | |
77ff2de9 | 1327 | |
3411372e LP |
1328 | bool clock_boottime_supported(void) { |
1329 | static int supported = -1; | |
1330 | ||
1331 | /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */ | |
1332 | ||
1333 | if (supported < 0) { | |
1334 | int fd; | |
1335 | ||
1336 | fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC); | |
1337 | if (fd < 0) | |
1338 | supported = false; | |
1339 | else { | |
1340 | safe_close(fd); | |
1341 | supported = true; | |
1342 | } | |
77ff2de9 TG |
1343 | } |
1344 | ||
3411372e LP |
1345 | return supported; |
1346 | } | |
1347 | ||
1348 | clockid_t clock_boottime_or_monotonic(void) { | |
1349 | if (clock_boottime_supported()) | |
1350 | return CLOCK_BOOTTIME; | |
1351 | else | |
1352 | return CLOCK_MONOTONIC; | |
77ff2de9 | 1353 | } |
5c904ba5 | 1354 | |
fe624c4c LP |
1355 | bool clock_supported(clockid_t clock) { |
1356 | struct timespec ts; | |
1357 | ||
1358 | switch (clock) { | |
1359 | ||
1360 | case CLOCK_MONOTONIC: | |
1361 | case CLOCK_REALTIME: | |
1362 | return true; | |
1363 | ||
1364 | case CLOCK_BOOTTIME: | |
1365 | return clock_boottime_supported(); | |
1366 | ||
1367 | case CLOCK_BOOTTIME_ALARM: | |
1368 | if (!clock_boottime_supported()) | |
1369 | return false; | |
1370 | ||
4831981d | 1371 | _fallthrough_; |
fe624c4c LP |
1372 | default: |
1373 | /* For everything else, check properly */ | |
1374 | return clock_gettime(clock, &ts) >= 0; | |
1375 | } | |
1376 | } | |
1377 | ||
64d6c229 | 1378 | int get_timezone(char **tz) { |
5c904ba5 LP |
1379 | _cleanup_free_ char *t = NULL; |
1380 | const char *e; | |
1381 | char *z; | |
1382 | int r; | |
1383 | ||
1384 | r = readlink_malloc("/etc/localtime", &t); | |
1385 | if (r < 0) | |
1386 | return r; /* returns EINVAL if not a symlink */ | |
1387 | ||
da9fc98d | 1388 | e = PATH_STARTSWITH_SET(t, "/usr/share/zoneinfo/", "../usr/share/zoneinfo/"); |
5c904ba5 LP |
1389 | if (!e) |
1390 | return -EINVAL; | |
1391 | ||
089fb865 | 1392 | if (!timezone_is_valid(e, LOG_DEBUG)) |
5c904ba5 LP |
1393 | return -EINVAL; |
1394 | ||
1395 | z = strdup(e); | |
1396 | if (!z) | |
1397 | return -ENOMEM; | |
1398 | ||
64d6c229 | 1399 | *tz = z; |
5c904ba5 LP |
1400 | return 0; |
1401 | } | |
7c67c79c HV |
1402 | |
1403 | time_t mktime_or_timegm(struct tm *tm, bool utc) { | |
1404 | return utc ? timegm(tm) : mktime(tm); | |
1405 | } | |
1406 | ||
1407 | struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) { | |
1408 | return utc ? gmtime_r(t, tm) : localtime_r(t, tm); | |
1409 | } | |
87b8ce69 SS |
1410 | |
1411 | unsigned long usec_to_jiffies(usec_t u) { | |
1412 | static thread_local unsigned long hz = 0; | |
1413 | long r; | |
1414 | ||
1415 | if (hz == 0) { | |
1416 | r = sysconf(_SC_CLK_TCK); | |
1417 | ||
1418 | assert(r > 0); | |
70887c5f | 1419 | hz = r; |
87b8ce69 SS |
1420 | } |
1421 | ||
1422 | return DIV_ROUND_UP(u , USEC_PER_SEC / hz); | |
1423 | } | |
1007ec60 LP |
1424 | |
1425 | usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) { | |
1426 | usec_t a, b; | |
1427 | ||
1428 | if (x == USEC_INFINITY) | |
1429 | return USEC_INFINITY; | |
1430 | if (map_clock_id(from) == map_clock_id(to)) | |
1431 | return x; | |
1432 | ||
1433 | a = now(from); | |
1434 | b = now(to); | |
1435 | ||
1436 | if (x > a) | |
1437 | /* x lies in the future */ | |
1438 | return usec_add(b, usec_sub_unsigned(x, a)); | |
1439 | else | |
1440 | /* x lies in the past */ | |
1441 | return usec_sub_unsigned(b, usec_sub_unsigned(a, x)); | |
1442 | } | |
9a9a4f10 LP |
1443 | |
1444 | bool in_utc_timezone(void) { | |
1445 | tzset(); | |
1446 | ||
1447 | return timezone == 0 && daylight == 0; | |
1448 | } | |
4f811d27 LP |
1449 | |
1450 | int time_change_fd(void) { | |
1451 | ||
1452 | /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */ | |
1453 | static const struct itimerspec its = { | |
1454 | .it_value.tv_sec = TIME_T_MAX, | |
1455 | }; | |
1456 | ||
1457 | _cleanup_close_ int fd; | |
1458 | ||
1459 | assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX)); | |
1460 | ||
1461 | /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to | |
1462 | * CLOCK_MONOTONIC. */ | |
1463 | ||
1464 | fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC); | |
1465 | if (fd < 0) | |
1466 | return -errno; | |
1467 | ||
1468 | if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) | |
1469 | return -errno; | |
1470 | ||
1471 | return TAKE_FD(fd); | |
1472 | } |