• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 13665439908

04 Mar 2025 09:54PM UTC coverage: 71.855% (+0.04%) from 71.819%
13665439908

push

github

poettering
dirent-util: add several assertions in posix_getdents()

Follow-up for e86a492ff.

4 of 4 new or added lines in 1 file covered. (100.0%)

8029 existing lines in 84 files now uncovered.

294783 of 410245 relevant lines covered (71.86%)

717882.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.29
/src/basic/time-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <ctype.h>
4
#include <errno.h>
5
#include <limits.h>
6
#include <stdlib.h>
7
#include <sys/mman.h>
8
#include <sys/time.h>
9
#include <sys/timerfd.h>
10
#include <sys/types.h>
11
#include <threads.h>
12
#include <unistd.h>
13

14
#include "alloc-util.h"
15
#include "fd-util.h"
16
#include "fileio.h"
17
#include "fs-util.h"
18
#include "io-util.h"
19
#include "log.h"
20
#include "macro.h"
21
#include "parse-util.h"
22
#include "path-util.h"
23
#include "process-util.h"
24
#include "stat-util.h"
25
#include "string-table.h"
26
#include "string-util.h"
27
#include "strv.h"
28
#include "time-util.h"
29

30
static clockid_t map_clock_id(clockid_t c) {
26,420,797✔
31

32
        /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus,
33
         * clock_gettime() will fail for them. Since they are essentially the same as their non-ALARM
34
         * pendants (their only difference is when timers are set on them), let's just map them
35
         * accordingly. This way, we can get the correct time even on those archs. */
36

37
        switch (c) {
26,420,797✔
38

39
        case CLOCK_BOOTTIME_ALARM:
40
                return CLOCK_BOOTTIME;
41

42
        case CLOCK_REALTIME_ALARM:
8✔
43
                return CLOCK_REALTIME;
8✔
44

45
        default:
26,420,788✔
46
                return c;
26,420,788✔
47
        }
48
}
49

50
usec_t now(clockid_t clock_id) {
26,417,595✔
51
        struct timespec ts;
26,417,595✔
52

53
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
26,417,595✔
54

55
        return timespec_load(&ts);
26,417,595✔
56
}
57

58
nsec_t now_nsec(clockid_t clock_id) {
622✔
59
        struct timespec ts;
622✔
60

61
        assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
622✔
62

63
        return timespec_load_nsec(&ts);
622✔
64
}
65

66
dual_timestamp* dual_timestamp_now(dual_timestamp *ts) {
1,929,230✔
67
        assert(ts);
1,929,230✔
68

69
        ts->realtime = now(CLOCK_REALTIME);
1,929,230✔
70
        ts->monotonic = now(CLOCK_MONOTONIC);
1,929,230✔
71

72
        return ts;
1,929,230✔
73
}
74

75
triple_timestamp* triple_timestamp_now(triple_timestamp *ts) {
2,793,243✔
76
        assert(ts);
2,793,243✔
77

78
        ts->realtime = now(CLOCK_REALTIME);
2,793,243✔
79
        ts->monotonic = now(CLOCK_MONOTONIC);
2,793,243✔
80
        ts->boottime = now(CLOCK_BOOTTIME);
2,793,243✔
81

82
        return ts;
2,793,243✔
83
}
84

85
usec_t map_clock_usec_raw(usec_t from, usec_t from_base, usec_t to_base) {
8,107✔
86

87
        /* Maps the time 'from' between two clocks, based on a common reference point where the first clock
88
         * is at 'from_base' and the second clock at 'to_base'. Basically calculates:
89
         *
90
         *         from - from_base + to_base
91
         *
92
         * But takes care of overflows/underflows and avoids signed operations. */
93

94
        if (from >= from_base) { /* In the future */
8,107✔
95
                usec_t delta = from - from_base;
20✔
96

97
                if (to_base >= USEC_INFINITY - delta) /* overflow? */
20✔
98
                        return USEC_INFINITY;
99

100
                return to_base + delta;
20✔
101

102
        } else { /* In the past */
103
                usec_t delta = from_base - from;
8,087✔
104

105
                if (to_base <= delta) /* underflow? */
8,087✔
106
                        return 0;
107

108
                return to_base - delta;
8,081✔
109
        }
110
}
111

112
usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock) {
616✔
113

114
        /* Try to avoid any inaccuracy needlessly added in case we convert from effectively the same clock
115
         * onto itself */
116
        if (map_clock_id(from_clock) == map_clock_id(to_clock))
616✔
117
                return from;
118

119
        /* Keep infinity as is */
120
        if (from == USEC_INFINITY)
616✔
121
                return from;
122

123
        return map_clock_usec_raw(from, now(from_clock), now(to_clock));
582✔
124
}
125

126
dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
5✔
127
        assert(ts);
5✔
128

129
        if (!timestamp_is_set(u)) {
5✔
UNCOV
130
                ts->realtime = ts->monotonic = u;
×
131
                return ts;
×
132
        }
133

134
        ts->realtime = u;
5✔
135
        ts->monotonic = map_clock_usec(u, CLOCK_REALTIME, CLOCK_MONOTONIC);
5✔
136
        return ts;
5✔
137
}
138

139
triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
299✔
140
        usec_t nowr;
299✔
141

142
        assert(ts);
299✔
143

144
        if (!timestamp_is_set(u)) {
299✔
UNCOV
145
                ts->realtime = ts->monotonic = ts->boottime = u;
×
146
                return ts;
×
147
        }
148

149
        nowr = now(CLOCK_REALTIME);
299✔
150

151
        ts->realtime = u;
299✔
152
        ts->monotonic = map_clock_usec_raw(u, nowr, now(CLOCK_MONOTONIC));
299✔
153
        ts->boottime = map_clock_usec_raw(u, nowr, now(CLOCK_BOOTTIME));
299✔
154

155
        return ts;
299✔
156
}
157

UNCOV
158
triple_timestamp* triple_timestamp_from_boottime(triple_timestamp *ts, usec_t u) {
×
159
        usec_t nowb;
×
160

UNCOV
161
        assert(ts);
×
162

UNCOV
163
        if (u == USEC_INFINITY) {
×
164
                ts->realtime = ts->monotonic = ts->boottime = u;
×
165
                return ts;
×
166
        }
167

UNCOV
168
        nowb = now(CLOCK_BOOTTIME);
×
169

UNCOV
170
        ts->boottime = u;
×
171
        ts->monotonic = map_clock_usec_raw(u, nowb, now(CLOCK_MONOTONIC));
×
172
        ts->realtime = map_clock_usec_raw(u, nowb, now(CLOCK_REALTIME));
×
173

UNCOV
174
        return ts;
×
175
}
176

177
dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
250✔
178
        assert(ts);
250✔
179

180
        if (u == USEC_INFINITY) {
250✔
UNCOV
181
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
182
                return ts;
×
183
        }
184

185
        ts->monotonic = u;
250✔
186
        ts->realtime = map_clock_usec(u, CLOCK_MONOTONIC, CLOCK_REALTIME);
250✔
187
        return ts;
250✔
188
}
189

190
dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u) {
632✔
191
        usec_t nowm;
632✔
192

193
        assert(ts);
632✔
194

195
        if (u == USEC_INFINITY) {
632✔
UNCOV
196
                ts->realtime = ts->monotonic = USEC_INFINITY;
×
197
                return ts;
×
198
        }
199

200
        nowm = now(CLOCK_BOOTTIME);
632✔
201
        ts->monotonic = map_clock_usec_raw(u, nowm, now(CLOCK_MONOTONIC));
632✔
202
        ts->realtime = map_clock_usec_raw(u, nowm, now(CLOCK_REALTIME));
632✔
203
        return ts;
632✔
204
}
205

206
usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
3,214,026✔
207
        assert(ts);
3,214,026✔
208

209
        switch (clock) {
3,214,026✔
210

211
        case CLOCK_REALTIME:
1,675,272✔
212
        case CLOCK_REALTIME_ALARM:
213
                return ts->realtime;
1,675,272✔
214

215
        case CLOCK_MONOTONIC:
1,495,765✔
216
                return ts->monotonic;
1,495,765✔
217

218
        case CLOCK_BOOTTIME:
42,989✔
219
        case CLOCK_BOOTTIME_ALARM:
220
                return ts->boottime;
42,989✔
221

222
        default:
223
                return USEC_INFINITY;
224
        }
225
}
226

227
usec_t timespec_load(const struct timespec *ts) {
26,771,608✔
228
        assert(ts);
26,771,608✔
229

230
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
26,771,608✔
231
                return USEC_INFINITY;
232

233
        if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
26,771,608✔
234
                return USEC_INFINITY;
235

236
        return
26,771,608✔
237
                (usec_t) ts->tv_sec * USEC_PER_SEC +
26,771,608✔
238
                (usec_t) ts->tv_nsec / NSEC_PER_USEC;
239
}
240

241
nsec_t timespec_load_nsec(const struct timespec *ts) {
1,688✔
242
        assert(ts);
1,688✔
243

244
        if (ts->tv_sec < 0 || ts->tv_nsec < 0)
1,688✔
245
                return NSEC_INFINITY;
246

247
        if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
1,688✔
248
                return NSEC_INFINITY;
249

250
        return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
1,688✔
251
}
252

253
struct timespec *timespec_store(struct timespec *ts, usec_t u) {
1,941,004✔
254
        assert(ts);
1,941,004✔
255

256
        if (u == USEC_INFINITY ||
1,941,004✔
257
            u / USEC_PER_SEC >= TIME_T_MAX) {
UNCOV
258
                ts->tv_sec = (time_t) -1;
×
259
                ts->tv_nsec = -1L;
×
260
                return ts;
×
261
        }
262

263
        ts->tv_sec = (time_t) (u / USEC_PER_SEC);
1,941,004✔
264
        ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
1,941,004✔
265

266
        return ts;
1,941,004✔
267
}
268

269
struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n) {
56✔
270
        assert(ts);
56✔
271

272
        if (n == NSEC_INFINITY ||
56✔
273
            n / NSEC_PER_SEC >= TIME_T_MAX) {
UNCOV
274
                ts->tv_sec = (time_t) -1;
×
275
                ts->tv_nsec = -1L;
×
276
                return ts;
×
277
        }
278

279
        ts->tv_sec = (time_t) (n / NSEC_PER_SEC);
56✔
280
        ts->tv_nsec = (long) (n % NSEC_PER_SEC);
56✔
281

282
        return ts;
56✔
283
}
284

285
usec_t timeval_load(const struct timeval *tv) {
335,566✔
286
        assert(tv);
335,566✔
287

288
        if (tv->tv_sec < 0 || tv->tv_usec < 0)
335,566✔
289
                return USEC_INFINITY;
290

291
        if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
335,566✔
292
                return USEC_INFINITY;
293

294
        return
335,566✔
295
                (usec_t) tv->tv_sec * USEC_PER_SEC +
335,566✔
296
                (usec_t) tv->tv_usec;
297
}
298

299
struct timeval *timeval_store(struct timeval *tv, usec_t u) {
283,683✔
300
        assert(tv);
283,683✔
301

302
        if (u == USEC_INFINITY ||
283,683✔
303
            u / USEC_PER_SEC > TIME_T_MAX) {
UNCOV
304
                tv->tv_sec = (time_t) -1;
×
305
                tv->tv_usec = (suseconds_t) -1;
×
306
        } else {
307
                tv->tv_sec = (time_t) (u / USEC_PER_SEC);
283,683✔
308
                tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
283,683✔
309
        }
310

311
        return tv;
283,683✔
312
}
313

314
char* format_timestamp_style(
5,749✔
315
                char *buf,
316
                size_t l,
317
                usec_t t,
318
                TimestampStyle style) {
319

320
        /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that
321
         * our generated timestamps may be parsed with parse_timestamp(), and always read the same. */
322
        static const char * const weekdays[] = {
5,749✔
323
                [0] = "Sun",
324
                [1] = "Mon",
325
                [2] = "Tue",
326
                [3] = "Wed",
327
                [4] = "Thu",
328
                [5] = "Fri",
329
                [6] = "Sat",
330
        };
331

332
        struct tm tm;
5,749✔
333
        bool utc, us;
5,749✔
334
        size_t n;
5,749✔
335

336
        assert(buf);
5,749✔
337
        assert(style >= 0);
5,749✔
338
        assert(style < _TIMESTAMP_STYLE_MAX);
5,749✔
339

340
        if (!timestamp_is_set(t))
5,749✔
341
                return NULL; /* Timestamp is unset */
5,749✔
342

343
        if (style == TIMESTAMP_UNIX) {
4,199✔
344
                if (l < (size_t) (1 + 1 + 1))
100✔
345
                        return NULL; /* not enough space for even the shortest of forms */
346

347
                return snprintf_ok(buf, l, "@" USEC_FMT, t / USEC_PER_SEC);  /* round down μs → s */
100✔
348
        }
349

350
        utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC, TIMESTAMP_DATE);
4,099✔
351
        us = IN_SET(style, TIMESTAMP_US, TIMESTAMP_US_UTC);
4,099✔
352

353
        if (l < (size_t) (3 +                   /* week day */
3,619✔
354
                          1 + 10 +              /* space and date */
355
                          style == TIMESTAMP_DATE ? 0 :
356
                          (1 + 8 +              /* space and time */
357
                           (us ? 1 + 6 : 0) +   /* "." and microsecond part */
4,099✔
358
                           1 + (utc ? 3 : 1)) + /* space and shortest possible zone */
7,861✔
359
                          1))
360
                return NULL; /* Not enough space even for the shortest form. */
361

362
        /* Let's not format times with years > 9999 */
363
        if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) {
4,099✔
364
                static const char* const xxx[_TIMESTAMP_STYLE_MAX] = {
4✔
365
                        [TIMESTAMP_PRETTY] = "--- XXXX-XX-XX XX:XX:XX",
366
                        [TIMESTAMP_US]     = "--- XXXX-XX-XX XX:XX:XX.XXXXXX",
367
                        [TIMESTAMP_UTC]    = "--- XXXX-XX-XX XX:XX:XX UTC",
368
                        [TIMESTAMP_US_UTC] = "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC",
369
                        [TIMESTAMP_DATE]   = "--- XXXX-XX-XX",
370
                };
371

372
                assert(l >= strlen(xxx[style]) + 1);
4✔
373
                return strcpy(buf, xxx[style]);
4✔
374
        }
375

376
        if (localtime_or_gmtime_usec(t, utc, &tm) < 0)
4,095✔
377
                return NULL;
378

379
        /* Start with the week day */
380
        assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
4,095✔
381
        memcpy(buf, weekdays[tm.tm_wday], 4);
4,095✔
382

383
        if (style == TIMESTAMP_DATE) {
4,095✔
384
                /* Special format string if only date should be shown. */
385
                if (strftime(buf + 3, l - 3, " %Y-%m-%d", &tm) <= 0)
104✔
386
                        return NULL; /* Doesn't fit */
387

388
                return buf;
104✔
389
        }
390

391
        /* Add the main components */
392
        if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
3,991✔
393
                return NULL; /* Doesn't fit */
394

395
        /* Append the microseconds part, if that's requested */
396
        if (us) {
3,991✔
397
                n = strlen(buf);
479✔
398
                if (n + 8 > l)
479✔
399
                        return NULL; /* Microseconds part doesn't fit. */
400

401
                sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
479✔
402
        }
403

404
        /* Append the timezone */
405
        n = strlen(buf);
3,991✔
406
        if (utc) {
3,991✔
407
                /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r()
408
                 * normally uses the obsolete "GMT" instead. */
409
                if (n + 5 > l)
230✔
410
                        return NULL; /* "UTC" doesn't fit. */
411

412
                strcpy(buf + n, " UTC");
230✔
413

414
        } else if (!isempty(tm.tm_zone)) {
3,761✔
415
                size_t tn;
3,761✔
416

417
                /* An explicit timezone is specified, let's use it, if it fits */
418
                tn = strlen(tm.tm_zone);
3,761✔
419
                if (n + 1 + tn + 1 > l) {
3,761✔
420
                        /* The full time zone does not fit in. Yuck. */
421

UNCOV
422
                        if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
×
423
                                return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that
×
424
                                              * case, complain that it doesn't fit. */
425

426
                        /* So the time zone doesn't fit in fully, but the caller passed enough space for the
427
                         * POSIX minimum time zone length. In this case suppress the timezone entirely, in
428
                         * order not to dump an overly long, hard to read string on the user. This should be
429
                         * safe, because the user will assume the local timezone anyway if none is shown. And
430
                         * so does parse_timestamp(). */
431
                } else {
432
                        buf[n++] = ' ';
3,761✔
433
                        strcpy(buf + n, tm.tm_zone);
3,761✔
434
                }
435
        }
436

437
        return buf;
438
}
439

440
char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, clockid_t clock, bool implicit_left) {
1,358✔
441
        const char *s;
1,358✔
442
        usec_t n, d;
1,358✔
443

444
        assert(buf);
1,358✔
445

446
        if (!timestamp_is_set(t))
1,358✔
447
                return NULL;
448

449
        n = now(clock);
1,352✔
450
        if (n > t) {
1,352✔
451
                d = n - t;
1,159✔
452
                s = " ago";
1,159✔
453
        } else {
454
                d = t - n;
193✔
455
                s = implicit_left ? "" : " left";
193✔
456
        }
457

458
        if (d >= USEC_PER_YEAR) {
1,352✔
459
                usec_t years = d / USEC_PER_YEAR;
733✔
460
                usec_t months = (d % USEC_PER_YEAR) / USEC_PER_MONTH;
733✔
461

462
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
2,178✔
463
                                years,
464
                                years == 1 ? "year" : "years",
465
                                months,
466
                                months == 1 ? "month" : "months",
467
                                s);
468
        } else if (d >= USEC_PER_MONTH) {
619✔
469
                usec_t months = d / USEC_PER_MONTH;
10✔
470
                usec_t days = (d % USEC_PER_MONTH) / USEC_PER_DAY;
10✔
471

472
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
22✔
473
                                months,
474
                                months == 1 ? "month" : "months",
475
                                days,
476
                                days == 1 ? "day" : "days",
477
                                s);
478
        } else if (d >= USEC_PER_WEEK) {
609✔
479
                usec_t weeks = d / USEC_PER_WEEK;
12✔
480
                usec_t days = (d % USEC_PER_WEEK) / USEC_PER_DAY;
12✔
481

482
                (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
24✔
483
                                weeks,
484
                                weeks == 1 ? "week" : "weeks",
485
                                days,
486
                                days == 1 ? "day" : "days",
487
                                s);
488
        } else if (d >= 2*USEC_PER_DAY)
597✔
UNCOV
489
                (void) snprintf(buf, l, USEC_FMT " days%s", d / USEC_PER_DAY,s);
×
490
        else if (d >= 25*USEC_PER_HOUR)
597✔
491
                (void) snprintf(buf, l, "1 day " USEC_FMT "h%s",
13✔
492
                                (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
13✔
493
        else if (d >= 6*USEC_PER_HOUR)
584✔
494
                (void) snprintf(buf, l, USEC_FMT "h%s",
18✔
495
                                d / USEC_PER_HOUR, s);
496
        else if (d >= USEC_PER_HOUR)
566✔
497
                (void) snprintf(buf, l, USEC_FMT "h " USEC_FMT "min%s",
2✔
498
                                d / USEC_PER_HOUR,
499
                                (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
2✔
500
        else if (d >= 5*USEC_PER_MINUTE)
564✔
501
                (void) snprintf(buf, l, USEC_FMT "min%s",
6✔
502
                                d / USEC_PER_MINUTE, s);
503
        else if (d >= USEC_PER_MINUTE)
558✔
504
                (void) snprintf(buf, l, USEC_FMT "min " USEC_FMT "s%s",
6✔
505
                                d / USEC_PER_MINUTE,
506
                                (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
6✔
507
        else if (d >= USEC_PER_SEC)
552✔
508
                (void) snprintf(buf, l, USEC_FMT "s%s",
469✔
509
                                d / USEC_PER_SEC, s);
510
        else if (d >= USEC_PER_MSEC)
83✔
511
                (void) snprintf(buf, l, USEC_FMT "ms%s",
80✔
512
                                d / USEC_PER_MSEC, s);
513
        else if (d > 0)
3✔
514
                (void) snprintf(buf, l, USEC_FMT"us%s",
3✔
515
                                d, s);
516
        else
UNCOV
517
                (void) snprintf(buf, l, "now");
×
518

519
        buf[l-1] = 0;
1,352✔
520
        return buf;
1,352✔
521
}
522

523
char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
11,664✔
524
        static const struct {
11,664✔
525
                const char *suffix;
526
                usec_t usec;
527
        } table[] = {
528
                { "y",     USEC_PER_YEAR   },
529
                { "month", USEC_PER_MONTH  },
530
                { "w",     USEC_PER_WEEK   },
531
                { "d",     USEC_PER_DAY    },
532
                { "h",     USEC_PER_HOUR   },
533
                { "min",   USEC_PER_MINUTE },
534
                { "s",     USEC_PER_SEC    },
535
                { "ms",    USEC_PER_MSEC   },
536
                { "us",    1               },
537
        };
538

539
        char *p = ASSERT_PTR(buf);
11,664✔
540
        bool something = false;
11,664✔
541

542
        assert(l > 0);
11,664✔
543

544
        if (t == USEC_INFINITY) {
11,664✔
545
                strncpy(p, "infinity", l-1);
910✔
546
                p[l-1] = 0;
910✔
547
                return p;
910✔
548
        }
549

550
        if (t <= 0) {
10,754✔
551
                strncpy(p, "0", l-1);
529✔
552
                p[l-1] = 0;
529✔
553
                return p;
529✔
554
        }
555

556
        /* The result of this function can be parsed with parse_sec */
557

558
        FOREACH_ELEMENT(i, table) {
85,382✔
559
                int k = 0;
85,310✔
560
                size_t n;
85,310✔
561
                bool done = false;
85,310✔
562
                usec_t a, b;
85,310✔
563

564
                if (t <= 0)
85,310✔
565
                        break;
566

567
                if (t < accuracy && something)
75,206✔
568
                        break;
569

570
                if (t < i->usec)
75,157✔
571
                        continue;
60,102✔
572

573
                if (l <= 1)
15,055✔
574
                        break;
575

576
                a = t / i->usec;
15,055✔
577
                b = t % i->usec;
15,055✔
578

579
                /* Let's see if we should shows this in dot notation */
580
                if (t < USEC_PER_MINUTE && b > 0) {
15,055✔
581
                        signed char j = 0;
582

583
                        for (usec_t cc = i->usec; cc > 1; cc /= 10)
44,192✔
584
                                j++;
36,207✔
585

586
                        for (usec_t cc = accuracy; cc > 1; cc /= 10) {
39,390✔
587
                                b /= 10;
31,405✔
588
                                j--;
31,405✔
589
                        }
590

591
                        if (j > 0) {
7,985✔
592
                                k = snprintf(p, l,
2,664✔
593
                                             "%s"USEC_FMT".%0*"PRI_USEC"%s",
594
                                             p > buf ? " " : "",
595
                                             a,
596
                                             j,
597
                                             b,
598
                                             i->suffix);
1,332✔
599

600
                                t = 0;
1,332✔
601
                                done = true;
1,332✔
602
                        }
603
                }
604

605
                /* No? Then let's show it normally */
606
                if (!done) {
1,332✔
607
                        k = snprintf(p, l,
27,446✔
608
                                     "%s"USEC_FMT"%s",
609
                                     p > buf ? " " : "",
610
                                     a,
611
                                     i->suffix);
13,723✔
612

613
                        t = b;
13,723✔
614
                }
615

616
                n = MIN((size_t) k, l-1);
15,055✔
617

618
                l -= n;
15,055✔
619
                p += n;
15,055✔
620

621
                something = true;
15,055✔
622
        }
623

624
        *p = 0;
10,225✔
625

626
        return buf;
10,225✔
627
}
628

629
static int parse_timestamp_impl(
2,121✔
630
                const char *t,
631
                size_t max_len,
632
                bool utc,
633
                int isdst,
634
                long gmtoff,
635
                usec_t *ret) {
636

637
        static const struct {
2,121✔
638
                const char *name;
639
                const int nr;
640
        } day_nr[] = {
641
                { "Sunday",    0 },
642
                { "Sun",       0 },
643
                { "Monday",    1 },
644
                { "Mon",       1 },
645
                { "Tuesday",   2 },
646
                { "Tue",       2 },
647
                { "Wednesday", 3 },
648
                { "Wed",       3 },
649
                { "Thursday",  4 },
650
                { "Thu",       4 },
651
                { "Friday",    5 },
652
                { "Fri",       5 },
653
                { "Saturday",  6 },
654
                { "Sat",       6 },
655
        };
656

657
        _cleanup_free_ char *t_alloc = NULL;
2,121✔
658
        usec_t usec, plus = 0, minus = 0;
2,121✔
659
        bool with_tz = false;
2,121✔
660
        int r, weekday = -1;
2,121✔
661
        unsigned fractional = 0;
2,121✔
662
        const char *k;
2,121✔
663
        struct tm tm, copy;
2,121✔
664

665
        /* Allowed syntaxes:
666
         *
667
         *   2012-09-22 16:34:22.1[2[3[4[5[6]]]]]
668
         *   2012-09-22 16:34:22  (µsec will be set to 0)
669
         *   2012-09-22 16:34     (seconds will be set to 0)
670
         *   2012-09-22T16:34:22.1[2[3[4[5[6]]]]]
671
         *   2012-09-22T16:34:22  (µsec will be set to 0)
672
         *   2012-09-22T16:34     (seconds will be set to 0)
673
         *   2012-09-22           (time will be set to 00:00:00)
674
         *   16:34:22             (date will be set to today)
675
         *   16:34                (date will be set to today, seconds to 0)
676
         *   now
677
         *   yesterday            (time is set to 00:00:00)
678
         *   today                (time is set to 00:00:00)
679
         *   tomorrow             (time is set to 00:00:00)
680
         *   +5min
681
         *   -5days
682
         *   @2147483647          (seconds since epoch)
683
         *
684
         * Note, on DST change, 00:00:00 may not exist and in that case the time part may be shifted.
685
         * E.g. "Sun 2023-03-13 America/Havana" is parsed as "Sun 2023-03-13 01:00:00 CDT".
686
         *
687
         * A simplified strptime-spelled RFC3339 ABNF looks like
688
         *   "%Y-%m-%d" "T" "%H" ":" "%M" ":" "%S" [".%N"] ("Z" / (("+" / "-") "%H:%M"))
689
         * We additionally allow no seconds and inherited timezone
690
         * for symmetry with our other syntaxes and improved interactive usability:
691
         *   "%Y-%m-%d" "T" "%H" ":" "%M" ":" ["%S" [".%N"]] ["Z" / (("+" / "-") "%H:%M")]
692
         * RFC3339 defines time-secfrac to as "." 1*DIGIT, but we limit to 6 digits,
693
         * since we're limited to 1µs resolution.
694
         * We also accept "Sat 2012-09-22T16:34:22", RFC3339 warns against it.
695
         */
696

697
        assert(t);
2,121✔
698

699
        if (max_len != SIZE_MAX) {
2,121✔
700
                /* If the input string contains timezone, then cut it here. */
701

702
                if (max_len == 0) /* Can't be the only field */
1,689✔
703
                        return -EINVAL;
704

705
                t_alloc = strndup(t, max_len);
1,689✔
706
                if (!t_alloc)
1,689✔
707
                        return -ENOMEM;
708

709
                t = t_alloc;
710
                with_tz = true;
711
        }
712

713
        if (utc) {
2,121✔
714
                /* glibc accepts gmtoff more than 24 hours, but we refuse it. */
715
                if ((usec_t) labs(gmtoff) * USEC_PER_SEC > USEC_PER_DAY)
991✔
716
                        return -EINVAL;
717
        } else {
718
                if (gmtoff != 0)
1,130✔
719
                        return -EINVAL;
720
        }
721

722
        if (t[0] == '@' && !with_tz)
2,121✔
723
                return parse_sec(t + 1, ret);
103✔
724

725
        usec = now(CLOCK_REALTIME);
2,018✔
726

727
        if (!with_tz) {
2,018✔
728
                if (streq(t, "now"))
329✔
729
                        goto finish;
5✔
730

731
                if (t[0] == '+') {
324✔
732
                        r = parse_sec(t+1, &plus);
6✔
733
                        if (r < 0)
6✔
734
                                return r;
735

736
                        goto finish;
6✔
737
                }
738

739
                if (t[0] == '-') {
318✔
740
                        r = parse_sec(t+1, &minus);
6✔
741
                        if (r < 0)
6✔
742
                                return r;
743

744
                        goto finish;
6✔
745
                }
746

747
                if ((k = endswith(t, " ago"))) {
312✔
748
                        _cleanup_free_ char *buf = NULL;
5✔
749

750
                        buf = strndup(t, k - t);
5✔
751
                        if (!buf)
5✔
752
                                return -ENOMEM;
753

754
                        r = parse_sec(buf, &minus);
5✔
755
                        if (r < 0)
5✔
756
                                return r;
757

758
                        goto finish;
5✔
759
                }
760

761
                if ((k = endswith(t, " left"))) {
307✔
762
                        _cleanup_free_ char *buf = NULL;
100✔
763

764
                        buf = strndup(t, k - t);
100✔
765
                        if (!buf)
100✔
766
                                return -ENOMEM;
767

768
                        r = parse_sec(buf, &plus);
100✔
769
                        if (r < 0)
100✔
770
                                return r;
771

772
                        goto finish;
100✔
773
                }
774
        }
775

776
        r = localtime_or_gmtime_usec(usec, utc, &tm);
1,896✔
777
        if (r < 0)
1,896✔
778
                return r;
779

780
        tm.tm_isdst = isdst;
1,896✔
781

782
        if (streq(t, "today")) {
1,896✔
783
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
13✔
784
                goto from_tm;
13✔
785

786
        } else if (streq(t, "yesterday")) {
1,883✔
787
                tm.tm_mday--;
10✔
788
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
10✔
789
                goto from_tm;
10✔
790

791
        } else if (streq(t, "tomorrow")) {
1,873✔
792
                tm.tm_mday++;
8✔
793
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
8✔
794
                goto from_tm;
8✔
795
        }
796

797
        FOREACH_ELEMENT(day, day_nr) {
19,436✔
798
                k = startswith_no_case(t, day->name);
19,122✔
799
                if (!k || *k != ' ')
19,122✔
800
                        continue;
17,571✔
801

802
                weekday = day->nr;
1,551✔
803
                t = k + 1;
1,551✔
804
                break;
1,551✔
805
        }
806

807
        copy = tm;
1,865✔
808
        k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
1,865✔
809
        if (k) {
1,865✔
810
                if (*k == '.')
100✔
811
                        goto parse_usec;
64✔
812
                else if (*k == 0)
36✔
813
                        goto from_tm;
32✔
814
        }
815

816
        /* Our "canonical" RFC3339 syntax variant */
817
        tm = copy;
1,769✔
818
        k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
1,769✔
819
        if (k) {
1,769✔
820
                if (*k == '.')
1,471✔
821
                        goto parse_usec;
343✔
822
                else if (*k == 0)
1,128✔
823
                        goto from_tm;
1,119✔
824
        }
825

826
        /* RFC3339 syntax */
827
        tm = copy;
307✔
828
        k = strptime(t, "%Y-%m-%dT%H:%M:%S", &tm);
307✔
829
        if (k) {
307✔
830
                if (*k == '.')
42✔
831
                        goto parse_usec;
20✔
832
                else if (*k == 0)
22✔
833
                        goto from_tm;
22✔
834
        }
835

836
        /* Support OUTPUT_SHORT and OUTPUT_SHORT_PRECISE formats */
837
        tm = copy;
265✔
838
        k = strptime(t, "%b %d %H:%M:%S", &tm);
265✔
839
        if (k) {
265✔
840
                if (*k == '.')
4✔
841
                        goto parse_usec;
2✔
842
                else if (*k == 0)
2✔
843
                        goto from_tm;
2✔
844
        }
845

846
        tm = copy;
261✔
847
        k = strptime(t, "%y-%m-%d %H:%M", &tm);
261✔
848
        if (k && *k == 0) {
261✔
849
                tm.tm_sec = 0;
30✔
850
                goto from_tm;
30✔
851
        }
852

853
        /* Our "canonical" RFC3339 syntax variant without seconds */
854
        tm = copy;
231✔
855
        k = strptime(t, "%Y-%m-%d %H:%M", &tm);
231✔
856
        if (k && *k == 0) {
231✔
857
                tm.tm_sec = 0;
31✔
858
                goto from_tm;
31✔
859
        }
860

861
        /* RFC3339 syntax without seconds */
862
        tm = copy;
200✔
863
        k = strptime(t, "%Y-%m-%dT%H:%M", &tm);
200✔
864
        if (k && *k == 0) {
200✔
865
                tm.tm_sec = 0;
8✔
866
                goto from_tm;
8✔
867
        }
868

869
        tm = copy;
192✔
870
        k = strptime(t, "%y-%m-%d", &tm);
192✔
871
        if (k && *k == 0) {
192✔
UNCOV
872
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
×
873
                goto from_tm;
×
874
        }
875

876
        tm = copy;
192✔
877
        k = strptime(t, "%Y-%m-%d", &tm);
192✔
878
        if (k && *k == 0) {
192✔
879
                tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
106✔
880
                goto from_tm;
106✔
881
        }
882

883
        tm = copy;
86✔
884
        k = strptime(t, "%H:%M:%S", &tm);
86✔
885
        if (k) {
86✔
886
                if (*k == '.')
28✔
887
                        goto parse_usec;
8✔
888
                else if (*k == 0)
20✔
889
                        goto from_tm;
20✔
890
        }
891

892
        tm = copy;
58✔
893
        k = strptime(t, "%H:%M", &tm);
58✔
894
        if (k && *k == 0) {
58✔
895
                tm.tm_sec = 0;
6✔
896
                goto from_tm;
6✔
897
        }
898

899
        return -EINVAL;
900

901
parse_usec:
437✔
902
        k++;
437✔
903
        r = parse_fractional_part_u(&k, 6, &fractional);
437✔
904
        if (r < 0)
437✔
905
                return -EINVAL;
906
        if (*k != '\0')
437✔
907
                return -EINVAL;
908

909
from_tm:
421✔
910
        assert(plus == 0);
1,828✔
911
        assert(minus == 0);
1,828✔
912

913
        if (weekday >= 0 && tm.tm_wday != weekday)
1,828✔
914
                return -EINVAL;
915

916
        if (gmtoff < 0) {
1,828✔
917
                plus = -gmtoff * USEC_PER_SEC;
110✔
918

919
                /* If gmtoff is negative, the string may be too old to be parsed as UTC.
920
                 * E.g. 1969-12-31 23:00:00 -06 == 1970-01-01 05:00:00 UTC
921
                 * We assumed that gmtoff is in the range of -24:00…+24:00, hence the only date we need to
922
                 * handle here is 1969-12-31. So, let's shift the date with one day, then subtract the shift
923
                 * later. */
924
                if (tm.tm_year == 69 && tm.tm_mon == 11 && tm.tm_mday == 31) {
110✔
925
                        /* Thu 1970-01-01-00:00:00 */
926
                        tm.tm_year = 70;
96✔
927
                        tm.tm_mon = 0;
96✔
928
                        tm.tm_mday = 1;
96✔
929
                        tm.tm_wday = 4;
96✔
930
                        tm.tm_yday = 0;
96✔
931
                        minus = USEC_PER_DAY;
96✔
932
                }
933
        } else
934
                minus = gmtoff * USEC_PER_SEC;
1,718✔
935

936
        r = mktime_or_timegm_usec(&tm, utc, &usec);
1,828✔
937
        if (r < 0)
1,828✔
938
                return r;
939

940
        usec = usec_add(usec, fractional);
3,650✔
941

942
finish:
1,947✔
943
        usec = usec_add(usec, plus);
1,947✔
944

945
        if (usec < minus)
1,947✔
946
                return -EINVAL;
947

948
        usec = usec_sub_unsigned(usec, minus);
1,946✔
949

950
        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1,946✔
951
                return -EINVAL;
952

953
        if (ret)
1,945✔
954
                *ret = usec;
1,943✔
955
        return 0;
956
}
957

958
static int parse_timestamp_maybe_with_tz(const char *t, size_t tz_offset, bool valid_tz, usec_t *ret) {
945✔
959
        assert(t);
945✔
960

961
        tzset();
945✔
962

963
        for (int j = 0; j <= 1; j++) {
2,821✔
964
                if (isempty(tzname[j]))
1,883✔
UNCOV
965
                        continue;
×
966

967
                if (!streq(t + tz_offset, tzname[j]))
1,883✔
968
                        continue;
1,876✔
969

970
                /* The specified timezone matches tzname[] of the local timezone. */
971
                return parse_timestamp_impl(t, tz_offset - 1, /* utc = */ false, /* isdst = */ j, /* gmtoff = */ 0, ret);
7✔
972
        }
973

974
        /* If we know that the last word is a valid timezone (e.g. Asia/Tokyo), then simply drop the timezone
975
         * and parse the remaining string as a local time. If we know that the last word is not a timezone,
976
         * then assume that it is a part of the time and try to parse the whole string as a local time. */
977
        return parse_timestamp_impl(t, valid_tz ? tz_offset - 1 : SIZE_MAX,
938✔
978
                                    /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
979
}
980

981
typedef struct ParseTimestampResult {
982
        usec_t usec;
983
        int return_value;
984
} ParseTimestampResult;
985

986
int parse_timestamp(const char *t, usec_t *ret) {
2,088✔
987
        ParseTimestampResult *shared, tmp;
2,088✔
988
        const char *k, *tz, *current_tz;
2,088✔
989
        size_t max_len, t_len;
2,088✔
990
        struct tm tm;
2,088✔
991
        int r;
2,088✔
992

993
        assert(t);
2,088✔
994

995
        t_len = strlen(t);
2,088✔
996
        if (t_len > 2 && t[t_len - 1] == 'Z') {
2,088✔
997
                /* Try to parse as RFC3339-style welded UTC: "1985-04-12T23:20:50.52Z" */
998
                r = parse_timestamp_impl(t, t_len - 1, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
67✔
999
                if (r >= 0)
67✔
1000
                        return r;
2,088✔
1001
        }
1002

1003
        if (t_len > 7 && IN_SET(t[t_len - 6], '+', '-') && t[t_len - 7] != ' ') {  /* RFC3339-style welded offset: "1990-12-31T15:59:60-08:00" */
2,040✔
1004
                k = strptime(&t[t_len - 6], "%z", &tm);
143✔
1005
                if (k && *k == '\0')
143✔
1006
                        return parse_timestamp_impl(t, t_len - 6, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ tm.tm_gmtoff, ret);
40✔
1007
        }
1008

1009
        tz = strrchr(t, ' ');
2,014✔
1010
        if (!tz)
2,014✔
1011
                return parse_timestamp_impl(t, /* max_len = */ SIZE_MAX, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);
185✔
1012

1013
        max_len = tz - t;
1,829✔
1014
        tz++;
1,829✔
1015

1016
        /* Shortcut, parse the string as UTC. */
1017
        if (streq(tz, "UTC"))
1,829✔
1018
                return parse_timestamp_impl(t, max_len, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ 0, ret);
758✔
1019

1020
        /* If the timezone is compatible with RFC-822/ISO 8601 (e.g. +06, or -03:00) then parse the string as
1021
         * UTC and shift the result. Note, this must be earlier than the timezone check with tzname[], as
1022
         * tzname[] may be in the same format. */
1023
        k = strptime(tz, "%z", &tm);
1,071✔
1024
        if (k && *k == '\0')
1,071✔
1025
                return parse_timestamp_impl(t, max_len, /* utc = */ true, /* isdst = */ -1, /* gmtoff = */ tm.tm_gmtoff, ret);
126✔
1026

1027
        /* If the last word is not a timezone file (e.g. Asia/Tokyo), then let's check if it matches
1028
         * tzname[] of the local timezone, e.g. JST or CEST. */
1029
        if (!timezone_is_valid(tz, LOG_DEBUG))
945✔
1030
                return parse_timestamp_maybe_with_tz(t, tz - t, /* valid_tz = */ false, ret);
247✔
1031

1032
        /* Shortcut. If the current $TZ is equivalent to the specified timezone, it is not necessary to fork
1033
         * the process. */
1034
        current_tz = getenv("TZ");
698✔
1035
        if (current_tz && *current_tz == ':' && streq(current_tz + 1, tz))
698✔
UNCOV
1036
                return parse_timestamp_maybe_with_tz(t, tz - t, /* valid_tz = */ true, ret);
×
1037

1038
        /* Otherwise, to avoid polluting the current environment variables, let's fork the process and set
1039
         * the specified timezone in the child process. */
1040

1041
        shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
698✔
1042
        if (shared == MAP_FAILED)
698✔
UNCOV
1043
                return negative_errno();
×
1044

1045
        /* The input string may be in argv. Let's copy it. */
1046
        _cleanup_free_ char *t_copy = strdup(t);
698✔
1047
        if (!t_copy)
698✔
1048
                return -ENOMEM;
1049

1050
        t = t_copy;
698✔
1051
        assert_se(tz = endswith(t_copy, tz));
698✔
1052

1053
        r = safe_fork("(sd-timestamp)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_WAIT, NULL);
698✔
1054
        if (r < 0) {
1,396✔
UNCOV
1055
                (void) munmap(shared, sizeof *shared);
×
1056
                return r;
×
1057
        }
1058
        if (r == 0) {
1,396✔
1059
                const char *colon_tz;
698✔
1060

1061
                /* tzset(3) says $TZ should be prefixed with ":" if we reference timezone files */
1062
                colon_tz = strjoina(":", tz);
3,490✔
1063

1064
                if (setenv("TZ", colon_tz, 1) != 0) {
698✔
UNCOV
1065
                        shared->return_value = negative_errno();
×
1066
                        _exit(EXIT_FAILURE);
×
1067
                }
1068

1069
                shared->return_value = parse_timestamp_maybe_with_tz(t, tz - t, /* valid_tz = */ true, &shared->usec);
698✔
1070

1071
                _exit(EXIT_SUCCESS);
698✔
1072
        }
1073

1074
        tmp = *shared;
698✔
1075
        if (munmap(shared, sizeof *shared) != 0)
698✔
UNCOV
1076
                return negative_errno();
×
1077

1078
        if (tmp.return_value == 0 && ret)
698✔
1079
                *ret = tmp.usec;
695✔
1080

1081
        return tmp.return_value;
1082
}
1083

1084
static const char* extract_multiplier(const char *p, usec_t *ret) {
11,209✔
1085
        static const struct {
11,209✔
1086
                const char *suffix;
1087
                usec_t usec;
1088
        } table[] = {
1089
                { "seconds", USEC_PER_SEC    },
1090
                { "second",  USEC_PER_SEC    },
1091
                { "sec",     USEC_PER_SEC    },
1092
                { "s",       USEC_PER_SEC    },
1093
                { "minutes", USEC_PER_MINUTE },
1094
                { "minute",  USEC_PER_MINUTE },
1095
                { "min",     USEC_PER_MINUTE },
1096
                { "months",  USEC_PER_MONTH  },
1097
                { "month",   USEC_PER_MONTH  },
1098
                { "M",       USEC_PER_MONTH  },
1099
                { "msec",    USEC_PER_MSEC   },
1100
                { "ms",      USEC_PER_MSEC   },
1101
                { "m",       USEC_PER_MINUTE },
1102
                { "hours",   USEC_PER_HOUR   },
1103
                { "hour",    USEC_PER_HOUR   },
1104
                { "hr",      USEC_PER_HOUR   },
1105
                { "h",       USEC_PER_HOUR   },
1106
                { "days",    USEC_PER_DAY    },
1107
                { "day",     USEC_PER_DAY    },
1108
                { "d",       USEC_PER_DAY    },
1109
                { "weeks",   USEC_PER_WEEK   },
1110
                { "week",    USEC_PER_WEEK   },
1111
                { "w",       USEC_PER_WEEK   },
1112
                { "years",   USEC_PER_YEAR   },
1113
                { "year",    USEC_PER_YEAR   },
1114
                { "y",       USEC_PER_YEAR   },
1115
                { "usec",    1ULL            },
1116
                { "us",      1ULL            },
1117
                { "μs",      1ULL            }, /* U+03bc (aka GREEK SMALL LETTER MU) */
1118
                { "µs",      1ULL            }, /* U+b5 (aka MICRO SIGN) */
1119
        };
1120

1121
        assert(p);
11,209✔
1122
        assert(ret);
11,209✔
1123

1124
        FOREACH_ELEMENT(i, table) {
179,311✔
1125
                char *e;
176,104✔
1126

1127
                e = startswith(p, i->suffix);
176,104✔
1128
                if (e) {
176,104✔
1129
                        *ret = i->usec;
8,002✔
1130
                        return e;
8,002✔
1131
                }
1132
        }
1133

1134
        return p;
1135
}
1136

1137
int parse_time(const char *t, usec_t *ret, usec_t default_unit) {
11,390✔
1138
        const char *p, *s;
11,390✔
1139

1140
        assert(t);
11,390✔
1141
        assert(default_unit > 0);
11,390✔
1142

1143
        p = skip_leading_chars(t, /* bad = */ NULL);
11,390✔
1144
        s = startswith(p, "infinity");
11,390✔
1145
        if (s) {
11,390✔
1146
                if (!in_charset(s, WHITESPACE))
376✔
1147
                        return -EINVAL;
1148

1149
                if (ret)
375✔
1150
                        *ret = USEC_INFINITY;
375✔
1151
                return 0;
375✔
1152
        }
1153

1154
        usec_t usec = 0;
1155

1156
        for (bool something = false;;) {
11,193✔
1157
                usec_t multiplier = default_unit, k;
22,207✔
1158
                long long l;
22,207✔
1159
                char *e;
22,207✔
1160

1161
                p = skip_leading_chars(p, /* bad = */ NULL);
22,207✔
1162
                if (*p == 0) {
22,207✔
1163
                        if (!something)
10,976✔
1164
                                return -EINVAL;
43✔
1165

1166
                        break;
10,971✔
1167
                }
1168

1169
                if (*p == '-') /* Don't allow "-0" */
11,231✔
1170
                        return -ERANGE;
1171

1172
                errno = 0;
11,223✔
1173
                l = strtoll(p, &e, 10);
11,223✔
1174
                if (errno > 0)
11,223✔
UNCOV
1175
                        return -errno;
×
1176
                if (l < 0)
11,223✔
1177
                        return -ERANGE;
1178

1179
                if (*e == '.') {
11,223✔
1180
                        p = e + 1;
55✔
1181
                        p += strspn(p, DIGITS);
55✔
1182
                } else if (e == p)
11,168✔
1183
                        return -EINVAL;
1184
                else
1185
                        p = e;
1186

1187
                s = extract_multiplier(p + strspn(p, WHITESPACE), &multiplier);
11,209✔
1188
                if (s == p && *s != '\0')
11,209✔
1189
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1190
                        return -EINVAL;
1191

1192
                p = s;
11,200✔
1193

1194
                if ((usec_t) l >= USEC_INFINITY / multiplier)
11,200✔
1195
                        return -ERANGE;
1196

1197
                k = (usec_t) l * multiplier;
11,198✔
1198
                if (k >= USEC_INFINITY - usec)
11,198✔
1199
                        return -ERANGE;
1200

1201
                usec += k;
11,198✔
1202

1203
                something = true;
11,198✔
1204

1205
                if (*e == '.') {
11,198✔
1206
                        usec_t m = multiplier / 10;
48✔
1207
                        const char *b;
48✔
1208

1209
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
188✔
1210
                                k = (usec_t) (*b - '0') * m;
140✔
1211
                                if (k >= USEC_INFINITY - usec)
140✔
1212
                                        return -ERANGE;
1213

1214
                                usec += k;
140✔
1215
                        }
1216

1217
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1218
                        if (b == e + 1)
48✔
1219
                                return -EINVAL;
1220
                }
1221
        }
1222

1223
        if (ret)
10,971✔
1224
                *ret = usec;
10,969✔
1225
        return 0;
1226
}
1227

1228
int parse_sec(const char *t, usec_t *ret) {
11,356✔
1229
        return parse_time(t, ret, USEC_PER_SEC);
11,356✔
1230
}
1231

1232
int parse_sec_fix_0(const char *t, usec_t *ret) {
2,531✔
1233
        usec_t k;
2,531✔
1234
        int r;
2,531✔
1235

1236
        assert(t);
2,531✔
1237
        assert(ret);
2,531✔
1238

1239
        r = parse_sec(t, &k);
2,531✔
1240
        if (r < 0)
2,531✔
1241
                return r;
2,531✔
1242

1243
        *ret = k == 0 ? USEC_INFINITY : k;
2,531✔
1244
        return r;
2,531✔
1245
}
1246

1247
int parse_sec_def_infinity(const char *t, usec_t *ret) {
7✔
1248
        assert(t);
7✔
1249
        assert(ret);
7✔
1250

1251
        t += strspn(t, WHITESPACE);
7✔
1252
        if (isempty(t)) {
7✔
1253
                *ret = USEC_INFINITY;
2✔
1254
                return 0;
2✔
1255
        }
1256
        return parse_sec(t, ret);
5✔
1257
}
1258

1259
static const char* extract_nsec_multiplier(const char *p, nsec_t *ret) {
43✔
1260
        static const struct {
43✔
1261
                const char *suffix;
1262
                nsec_t nsec;
1263
        } table[] = {
1264
                { "seconds", NSEC_PER_SEC    },
1265
                { "second",  NSEC_PER_SEC    },
1266
                { "sec",     NSEC_PER_SEC    },
1267
                { "s",       NSEC_PER_SEC    },
1268
                { "minutes", NSEC_PER_MINUTE },
1269
                { "minute",  NSEC_PER_MINUTE },
1270
                { "min",     NSEC_PER_MINUTE },
1271
                { "months",  NSEC_PER_MONTH  },
1272
                { "month",   NSEC_PER_MONTH  },
1273
                { "M",       NSEC_PER_MONTH  },
1274
                { "msec",    NSEC_PER_MSEC   },
1275
                { "ms",      NSEC_PER_MSEC   },
1276
                { "m",       NSEC_PER_MINUTE },
1277
                { "hours",   NSEC_PER_HOUR   },
1278
                { "hour",    NSEC_PER_HOUR   },
1279
                { "hr",      NSEC_PER_HOUR   },
1280
                { "h",       NSEC_PER_HOUR   },
1281
                { "days",    NSEC_PER_DAY    },
1282
                { "day",     NSEC_PER_DAY    },
1283
                { "d",       NSEC_PER_DAY    },
1284
                { "weeks",   NSEC_PER_WEEK   },
1285
                { "week",    NSEC_PER_WEEK   },
1286
                { "w",       NSEC_PER_WEEK   },
1287
                { "years",   NSEC_PER_YEAR   },
1288
                { "year",    NSEC_PER_YEAR   },
1289
                { "y",       NSEC_PER_YEAR   },
1290
                { "usec",    NSEC_PER_USEC   },
1291
                { "us",      NSEC_PER_USEC   },
1292
                { "μs",      NSEC_PER_USEC   }, /* U+03bc (aka GREEK LETTER MU) */
1293
                { "µs",      NSEC_PER_USEC   }, /* U+b5 (aka MICRO SIGN) */
1294
                { "nsec",    1ULL            },
1295
                { "ns",      1ULL            },
1296
                { "",        1ULL            }, /* default is nsec */
1297
        };
1298

1299
        assert(p);
43✔
1300
        assert(ret);
43✔
1301

1302
        FOREACH_ELEMENT(i, table) {
791✔
1303
                char *e;
791✔
1304

1305
                e = startswith(p, i->suffix);
791✔
1306
                if (e) {
791✔
1307
                        *ret = i->nsec;
43✔
1308
                        return e;
43✔
1309
                }
1310
        }
1311

1312
        return p;
1313
}
1314

1315
int parse_nsec(const char *t, nsec_t *ret) {
47✔
1316
        const char *p, *s;
47✔
1317
        nsec_t nsec = 0;
47✔
1318
        bool something = false;
47✔
1319

1320
        assert(t);
47✔
1321
        assert(ret);
47✔
1322

1323
        p = t;
47✔
1324

1325
        p += strspn(p, WHITESPACE);
47✔
1326
        s = startswith(p, "infinity");
47✔
1327
        if (s) {
47✔
1328
                s += strspn(s, WHITESPACE);
4✔
1329
                if (*s != 0)
4✔
1330
                        return -EINVAL;
1331

1332
                *ret = NSEC_INFINITY;
2✔
1333
                return 0;
2✔
1334
        }
1335

1336
        for (;;) {
29✔
1337
                nsec_t multiplier = 1, k;
72✔
1338
                long long l;
72✔
1339
                char *e;
72✔
1340

1341
                p += strspn(p, WHITESPACE);
72✔
1342

1343
                if (*p == 0) {
72✔
1344
                        if (!something)
20✔
1345
                                return -EINVAL;
24✔
1346

1347
                        break;
19✔
1348
                }
1349

1350
                if (*p == '-') /* Don't allow "-0" */
52✔
1351
                        return -ERANGE;
1352

1353
                errno = 0;
47✔
1354
                l = strtoll(p, &e, 10);
47✔
1355
                if (errno > 0)
47✔
UNCOV
1356
                        return -errno;
×
1357
                if (l < 0)
47✔
1358
                        return -ERANGE;
1359

1360
                if (*e == '.') {
47✔
1361
                        p = e + 1;
31✔
1362
                        p += strspn(p, DIGITS);
31✔
1363
                } else if (e == p)
16✔
1364
                        return -EINVAL;
1365
                else
1366
                        p = e;
1367

1368
                s = extract_nsec_multiplier(p + strspn(p, WHITESPACE), &multiplier);
43✔
1369
                if (s == p && *s != '\0')
43✔
1370
                        /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1371
                        return -EINVAL;
1372

1373
                p = s;
35✔
1374

1375
                if ((nsec_t) l >= NSEC_INFINITY / multiplier)
35✔
1376
                        return -ERANGE;
1377

1378
                k = (nsec_t) l * multiplier;
34✔
1379
                if (k >= NSEC_INFINITY - nsec)
34✔
1380
                        return -ERANGE;
1381

1382
                nsec += k;
34✔
1383

1384
                something = true;
34✔
1385

1386
                if (*e == '.') {
34✔
1387
                        nsec_t m = multiplier / 10;
24✔
1388
                        const char *b;
24✔
1389

1390
                        for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
56✔
1391
                                k = (nsec_t) (*b - '0') * m;
32✔
1392
                                if (k >= NSEC_INFINITY - nsec)
32✔
1393
                                        return -ERANGE;
1394

1395
                                nsec += k;
32✔
1396
                        }
1397

1398
                        /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1399
                        if (b == e + 1)
24✔
1400
                                return -EINVAL;
1401
                }
1402
        }
1403

1404
        *ret = nsec;
19✔
1405

1406
        return 0;
19✔
1407
}
1408

UNCOV
1409
static int get_timezones_from_zone1970_tab(char ***ret) {
×
1410
        _cleanup_fclose_ FILE *f = NULL;
×
1411
        _cleanup_strv_free_ char **zones = NULL;
×
1412
        int r;
×
1413

UNCOV
1414
        assert(ret);
×
1415

UNCOV
1416
        f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
×
1417
        if (!f)
×
1418
                return -errno;
×
1419

UNCOV
1420
        for (;;) {
×
1421
                _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
×
1422

UNCOV
1423
                r = read_line(f, LONG_LINE_MAX, &line);
×
1424
                if (r < 0)
×
1425
                        return r;
UNCOV
1426
                if (r == 0)
×
1427
                        break;
1428

UNCOV
1429
                const char *p = line;
×
1430

1431
                /* Line format is:
1432
                 * 'country codes' 'coordinates' 'timezone' 'comments' */
UNCOV
1433
                r = extract_many_words(&p, NULL, 0, &cc, &co, &tz);
×
1434
                if (r < 0)
×
1435
                        continue;
×
1436

1437
                /* Lines that start with # are comments. */
UNCOV
1438
                if (*cc == '#')
×
1439
                        continue;
×
1440

UNCOV
1441
                r = strv_extend(&zones, tz);
×
1442
                if (r < 0)
×
1443
                        return r;
1444
        }
1445

UNCOV
1446
        *ret = TAKE_PTR(zones);
×
1447
        return 0;
×
1448
}
1449

1450
static int get_timezones_from_tzdata_zi(char ***ret) {
4✔
1451
        _cleanup_fclose_ FILE *f = NULL;
4✔
1452
        _cleanup_strv_free_ char **zones = NULL;
4✔
1453
        int r;
4✔
1454

1455
        assert(ret);
4✔
1456

1457
        f = fopen("/usr/share/zoneinfo/tzdata.zi", "re");
4✔
1458
        if (!f)
4✔
UNCOV
1459
                return -errno;
×
1460

1461
        for (;;) {
17,148✔
1462
                _cleanup_free_ char *line = NULL, *type = NULL, *f1 = NULL, *f2 = NULL;
17,144✔
1463

1464
                r = read_line(f, LONG_LINE_MAX, &line);
17,148✔
1465
                if (r < 0)
17,148✔
1466
                        return r;
1467
                if (r == 0)
17,148✔
1468
                        break;
1469

1470
                const char *p = line;
17,144✔
1471

1472
                /* The only lines we care about are Zone and Link lines.
1473
                 * Zone line format is:
1474
                 * 'Zone' 'timezone' ...
1475
                 * Link line format is:
1476
                 * 'Link' 'target' 'alias'
1477
                 * See 'man zic' for more detail. */
1478
                r = extract_many_words(&p, NULL, 0, &type, &f1, &f2);
17,144✔
1479
                if (r < 0)
17,144✔
UNCOV
1480
                        continue;
×
1481

1482
                char *tz;
17,144✔
1483
                if (IN_SET(*type, 'Z', 'z'))
17,144✔
1484
                        /* Zone lines have timezone in field 1. */
1485
                        tz = f1;
1,360✔
1486
                else if (IN_SET(*type, 'L', 'l'))
15,784✔
1487
                        /* Link lines have timezone in field 2. */
1488
                        tz = f2;
1,028✔
1489
                else
1490
                        /* Not a line we care about. */
1491
                        continue;
14,756✔
1492

1493
                r = strv_extend(&zones, tz);
2,388✔
1494
                if (r < 0)
2,388✔
1495
                        return r;
1496
        }
1497

1498
        *ret = TAKE_PTR(zones);
4✔
1499
        return 0;
4✔
1500
}
1501

1502
int get_timezones(char ***ret) {
4✔
1503
        _cleanup_strv_free_ char **zones = NULL;
4✔
1504
        int r;
4✔
1505

1506
        assert(ret);
4✔
1507

1508
        r = get_timezones_from_tzdata_zi(&zones);
4✔
1509
        if (r == -ENOENT) {
4✔
UNCOV
1510
                log_debug_errno(r, "Could not get timezone data from tzdata.zi, using zone1970.tab: %m");
×
1511
                r = get_timezones_from_zone1970_tab(&zones);
×
1512
                if (r == -ENOENT)
×
1513
                        log_debug_errno(r, "Could not get timezone data from zone1970.tab, using UTC: %m");
×
1514
        }
1515
        if (r < 0 && r != -ENOENT)
4✔
1516
                return r;
1517

1518
        /* Always include UTC */
1519
        r = strv_extend(&zones, "UTC");
4✔
1520
        if (r < 0)
4✔
1521
                return r;
1522

1523
        strv_sort_uniq(zones);
4✔
1524

1525
        *ret = TAKE_PTR(zones);
4✔
1526
        return 0;
4✔
1527
}
1528

1529
int verify_timezone(const char *name, int log_level) {
1,804✔
1530
        bool slash = false;
1,804✔
1531
        const char *p, *t;
1,804✔
1532
        _cleanup_close_ int fd = -EBADF;
1,804✔
1533
        char buf[4];
1,804✔
1534
        int r;
1,804✔
1535

1536
        if (isempty(name))
3,608✔
1537
                return -EINVAL;
1538

1539
        /* Always accept "UTC" as valid timezone, since it's the fallback, even if user has no timezones installed. */
1540
        if (streq(name, "UTC"))
1,802✔
1541
                return 0;
1542

1543
        if (name[0] == '/')
1,799✔
1544
                return -EINVAL;
1545

1546
        for (p = name; *p; p++) {
22,709✔
1547
                if (!ascii_isdigit(*p) &&
21,056✔
1548
                    !ascii_isalpha(*p) &&
19,936✔
1549
                    !IN_SET(*p, '-', '_', '+', '/'))
1,990✔
1550
                        return -EINVAL;
1551

1552
                if (*p == '/') {
20,911✔
1553

1554
                        if (slash)
1,377✔
1555
                                return -EINVAL;
1556

1557
                        slash = true;
1558
                } else
1559
                        slash = false;
1560
        }
1561

1562
        if (slash)
1,653✔
1563
                return -EINVAL;
1564

1565
        if (p - name >= PATH_MAX)
1,652✔
1566
                return -ENAMETOOLONG;
1567

1568
        t = strjoina("/usr/share/zoneinfo/", name);
8,260✔
1569

1570
        fd = open(t, O_RDONLY|O_CLOEXEC);
1,652✔
1571
        if (fd < 0)
1,652✔
1572
                return log_full_errno(log_level, errno, "Failed to open timezone file '%s': %m", t);
210✔
1573

1574
        r = fd_verify_regular(fd);
1,442✔
1575
        if (r < 0)
1,442✔
UNCOV
1576
                return log_full_errno(log_level, r, "Timezone file '%s' is not a regular file: %m", t);
×
1577

1578
        r = loop_read_exact(fd, buf, 4, false);
1,442✔
1579
        if (r < 0)
1,442✔
UNCOV
1580
                return log_full_errno(log_level, r, "Failed to read from timezone file '%s': %m", t);
×
1581

1582
        /* Magic from tzfile(5) */
1583
        if (memcmp(buf, "TZif", 4) != 0)
1,442✔
UNCOV
1584
                return log_full_errno(log_level, SYNTHETIC_ERRNO(EBADMSG),
×
1585
                                      "Timezone file '%s' has wrong magic bytes", t);
1586

1587
        return 0;
1588
}
1589

1590
bool clock_supported(clockid_t clock) {
375,426✔
1591
        struct timespec ts;
375,426✔
1592

1593
        switch (clock) {
375,426✔
1594

1595
        case CLOCK_MONOTONIC:
1596
        case CLOCK_REALTIME:
1597
        case CLOCK_BOOTTIME:
1598
                /* These three are always available in our baseline, and work in timerfd, as of kernel 3.15 */
1599
                return true;
1600

1601
        default:
1✔
1602
                /* For everything else, check properly */
1603
                return clock_gettime(clock, &ts) >= 0;
1✔
1604
        }
1605
}
1606

1607
int get_timezone(char **ret) {
54✔
1608
        _cleanup_free_ char *t = NULL;
54✔
1609
        int r;
54✔
1610

1611
        assert(ret);
54✔
1612

1613
        r = readlink_malloc("/etc/localtime", &t);
54✔
1614
        if (r == -ENOENT)
54✔
1615
                /* If the symlink does not exist, assume "UTC", like glibc does */
UNCOV
1616
                return strdup_to(ret, "UTC");
×
1617
        if (r < 0)
54✔
1618
                return r; /* Return EINVAL if not a symlink */
1619

1620
        const char *e = PATH_STARTSWITH_SET(t, "/usr/share/zoneinfo/", "../usr/share/zoneinfo/");
54✔
1621
        if (!e)
54✔
1622
                return -EINVAL;
1623
        if (!timezone_is_valid(e, LOG_DEBUG))
54✔
1624
                return -EINVAL;
1625

1626
        return strdup_to(ret, e);
54✔
1627
}
1628

1629
int mktime_or_timegm_usec(
8,073✔
1630
                struct tm *tm, /* input + normalized output */
1631
                bool utc,
1632
                usec_t *ret) {
1633

1634
        time_t t;
8,073✔
1635

1636
        assert(tm);
8,073✔
1637

1638
        if (tm->tm_year < 69) /* early check for negative (i.e. before 1970) time_t (Note that in some timezones the epoch is in the year 1969!) */
8,073✔
1639
                return -ERANGE;
1640
        if ((usec_t) tm->tm_year > CONST_MIN(USEC_INFINITY / USEC_PER_YEAR, (usec_t) TIME_T_MAX / (365U * 24U * 60U * 60U)) - 1900) /* early check for possible overrun of usec_t or time_t */
8,071✔
1641
                return -ERANGE;
1642

1643
        /* timegm()/mktime() is a bit weird to use, since it returns -1 in two cases: on error as well as a
1644
         * valid time indicating one second before the UNIX epoch. Let's treat both cases the same here, and
1645
         * return -ERANGE for anything negative, since usec_t is unsigned, and we can thus not express
1646
         * negative times anyway. */
1647

1648
        t = utc ? timegm(tm) : mktime(tm);
8,071✔
1649
        if (t < 0) /* Refuse negative times and errors */
8,071✔
1650
                return -ERANGE;
1651
        if ((usec_t) t >= USEC_INFINITY / USEC_PER_SEC) /* Never return USEC_INFINITY by accident (or overflow) */
8,069✔
1652
                return -ERANGE;
1653

1654
        if (ret)
8,069✔
1655
                *ret = (usec_t) t * USEC_PER_SEC;
2,207✔
1656
        return 0;
1657
}
1658

1659
int localtime_or_gmtime_usec(
92,106✔
1660
                usec_t t,
1661
                bool utc,
1662
                struct tm *ret) {
1663

1664
        t /= USEC_PER_SEC; /* Round down */
92,106✔
1665
        if (t > (usec_t) TIME_T_MAX)
92,106✔
1666
                return -ERANGE;
1667
        time_t sec = (time_t) t;
92,106✔
1668

1669
        struct tm buf = {};
92,106✔
1670
        if (!(utc ? gmtime_r(&sec, &buf) : localtime_r(&sec, &buf)))
92,106✔
1671
                return -EINVAL;
1672

1673
        if (ret)
92,106✔
1674
                *ret = buf;
92,106✔
1675

1676
        return 0;
1677
}
1678

1679
static uint32_t sysconf_clock_ticks_cached(void) {
781✔
1680
        static thread_local uint32_t hz = 0;
781✔
1681
        long r;
781✔
1682

1683
        if (hz == 0) {
781✔
1684
                r = sysconf(_SC_CLK_TCK);
80✔
1685

1686
                assert(r > 0);
80✔
1687
                hz = r;
80✔
1688
        }
1689

1690
        return hz;
781✔
1691
}
1692

1693
uint32_t usec_to_jiffies(usec_t u) {
9✔
1694
        uint32_t hz = sysconf_clock_ticks_cached();
9✔
1695
        return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
9✔
1696
}
1697

1698
usec_t jiffies_to_usec(uint32_t j) {
772✔
1699
        uint32_t hz = sysconf_clock_ticks_cached();
772✔
1700
        return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
772✔
1701
}
1702

1703
usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
924✔
1704
        usec_t a, b;
924✔
1705

1706
        if (x == USEC_INFINITY)
924✔
1707
                return USEC_INFINITY;
1708
        if (map_clock_id(from) == map_clock_id(to))
674✔
1709
                return x;
1710

1711
        a = now(from);
8✔
1712
        b = now(to);
8✔
1713

1714
        if (x > a)
8✔
1715
                /* x lies in the future */
1716
                return usec_add(b, usec_sub_unsigned(x, a));
12✔
1717
        else
1718
                /* x lies in the past */
1719
                return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
6✔
1720
}
1721

1722
bool in_utc_timezone(void) {
675✔
1723
        tzset();
675✔
1724

1725
        return timezone == 0 && daylight == 0;
675✔
1726
}
1727

1728
int time_change_fd(void) {
496✔
1729

1730
        /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
1731
        static const struct itimerspec its = {
496✔
1732
                .it_value.tv_sec = TIME_T_MAX,
1733
        };
1734

1735
        _cleanup_close_ int fd = -EBADF;
992✔
1736

1737
        assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX));
496✔
1738

1739
        /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
1740
         * CLOCK_MONOTONIC. */
1741

1742
        fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
496✔
1743
        if (fd < 0)
496✔
UNCOV
1744
                return -errno;
×
1745

1746
        if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) >= 0)
496✔
1747
                return TAKE_FD(fd);
496✔
1748

1749
        /* So apparently there are systems where time_t is 64-bit, but the kernel actually doesn't support
1750
         * 64-bit time_t. In that case configuring a timer to TIME_T_MAX will fail with EOPNOTSUPP or a
1751
         * similar error. If that's the case let's try with INT32_MAX instead, maybe that works. It's a bit
1752
         * of a black magic thing though, but what can we do?
1753
         *
1754
         * We don't want this code on x86-64, hence let's conditionalize this for systems with 64-bit time_t
1755
         * but where "long" is shorter than 64-bit, i.e. 32-bit archs.
1756
         *
1757
         * See: https://github.com/systemd/systemd/issues/14362 */
1758

1759
#if SIZEOF_TIME_T == 8 && ULONG_MAX < UINT64_MAX
1760
        if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EOVERFLOW) {
1761
                static const struct itimerspec its32 = {
1762
                        .it_value.tv_sec = INT32_MAX,
1763
                };
1764

1765
                if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its32, NULL) >= 0)
1766
                        return TAKE_FD(fd);
1767
        }
1768
#endif
1769

UNCOV
1770
        return -errno;
×
1771
}
1772

1773
static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
1774
        [TIMESTAMP_PRETTY] = "pretty",
1775
        [TIMESTAMP_US]     = "us",
1776
        [TIMESTAMP_UTC]    = "utc",
1777
        [TIMESTAMP_US_UTC] = "us+utc",
1778
        [TIMESTAMP_UNIX]   = "unix",
1779
};
1780

1781
/* Use the macro for enum → string to allow for aliases */
UNCOV
1782
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle);
×
1783

1784
/* For the string → enum mapping we use the generic implementation, but also support two aliases */
1785
TimestampStyle timestamp_style_from_string(const char *s) {
6✔
1786
        TimestampStyle t;
6✔
1787

1788
        t = (TimestampStyle) string_table_lookup(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
6✔
1789
        if (t >= 0)
6✔
1790
                return t;
1791
        if (STRPTR_IN_SET(s, "µs", "μs")) /* accept both µ symbols in unicode, i.e. micro symbol + Greek small letter mu. */
2✔
1792
                return TIMESTAMP_US;
1✔
1793
        if (STRPTR_IN_SET(s, "µs+utc", "μs+utc"))
1✔
1794
                return TIMESTAMP_US_UTC;
1✔
1795
        return t;
1796
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc