]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/calendarspec.c
basic: add log_level argument to timezone_is_valid
[thirdparty/systemd.git] / src / basic / calendarspec.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
36697dc0
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
36697dc0
LP
6***/
7
11c3a366 8#include <alloca.h>
9dfa81a0 9#include <ctype.h>
11c3a366 10#include <errno.h>
c0aebb4b 11#include <limits.h>
11c3a366
TA
12#include <stddef.h>
13#include <stdio.h>
0d536673 14#include <stdio_ext.h>
36697dc0
LP
15#include <stdlib.h>
16#include <string.h>
48d26c01 17#include <sys/mman.h>
11c3a366 18#include <time.h>
36697dc0 19
b5efdb8a 20#include "alloc-util.h"
36697dc0 21#include "calendarspec.h"
0d39fa9c 22#include "fileio.h"
11c3a366 23#include "macro.h"
93cc7779 24#include "parse-util.h"
dccca82b 25#include "process-util.h"
cf0fbc49 26#include "string-util.h"
48d26c01 27#include "time-util.h"
36697dc0 28
a2eb5ea7
DC
29#define BITS_WEEKDAYS 127
30#define MIN_YEAR 1970
31#define MAX_YEAR 2199
489464d0 32
74353158
ZJS
33/* An arbitrary limit on the length of the chains of components. We don't want to
34 * build a very long linked list, which would be slow to iterate over and might cause
35 * our stack to overflow. It's unlikely that legitimate uses require more than a few
36 * linked compenents anyway. */
37#define CALENDARSPEC_COMPONENTS_MAX 240
38
36697dc0
LP
39static void free_chain(CalendarComponent *c) {
40 CalendarComponent *n;
41
42 while (c) {
43 n = c->next;
44 free(c);
45 c = n;
46 }
47}
48
7c123d49 49CalendarSpec* calendar_spec_free(CalendarSpec *c) {
0b76b4d8
LP
50
51 if (!c)
7c123d49 52 return NULL;
36697dc0
LP
53
54 free_chain(c->year);
55 free_chain(c->month);
56 free_chain(c->day);
57 free_chain(c->hour);
58 free_chain(c->minute);
436dd70f 59 free_chain(c->microsecond);
48d26c01 60 free(c->timezone);
36697dc0 61
7c123d49 62 return mfree(c);
36697dc0
LP
63}
64
65static int component_compare(const void *_a, const void *_b) {
66 CalendarComponent * const *a = _a, * const *b = _b;
67
e78fce48 68 if ((*a)->start < (*b)->start)
36697dc0 69 return -1;
e78fce48 70 if ((*a)->start > (*b)->start)
36697dc0
LP
71 return 1;
72
e78fce48 73 if ((*a)->stop < (*b)->stop)
a2eb5ea7 74 return -1;
e78fce48 75 if ((*a)->stop > (*b)->stop)
a2eb5ea7
DC
76 return 1;
77
36697dc0
LP
78 if ((*a)->repeat < (*b)->repeat)
79 return -1;
80 if ((*a)->repeat > (*b)->repeat)
81 return 1;
82
83 return 0;
84}
85
a2eb5ea7 86static void normalize_chain(CalendarComponent **c) {
36697dc0 87 CalendarComponent **b, *i, **j, *next;
da6053d0 88 size_t n = 0, k;
36697dc0
LP
89
90 assert(c);
91
a2eb5ea7 92 for (i = *c; i; i = i->next) {
36697dc0
LP
93 n++;
94
a2eb5ea7 95 /*
e78fce48 96 * While we're counting the chain, also normalize `stop`
a2eb5ea7
DC
97 * so the length of the range is a multiple of `repeat`
98 */
c0aebb4b 99 if (i->stop > i->start && i->repeat > 0)
e78fce48 100 i->stop -= (i->stop - i->start) % i->repeat;
a2eb5ea7
DC
101
102 }
103
36697dc0
LP
104 if (n <= 1)
105 return;
106
cf409d15 107 j = b = newa(CalendarComponent*, n);
36697dc0
LP
108 for (i = *c; i; i = i->next)
109 *(j++) = i;
110
111 qsort(b, n, sizeof(CalendarComponent*), component_compare);
112
113 b[n-1]->next = NULL;
114 next = b[n-1];
115
116 /* Drop non-unique entries */
117 for (k = n-1; k > 0; k--) {
963e3d83 118 if (component_compare(&b[k-1], &next) == 0) {
36697dc0
LP
119 free(b[k-1]);
120 continue;
121 }
122
123 b[k-1]->next = next;
124 next = b[k-1];
125 }
126
127 *c = next;
128}
129
130static void fix_year(CalendarComponent *c) {
131 /* Turns 12 → 2012, 89 → 1989 */
132
9ed794a3 133 while (c) {
e78fce48
DC
134 if (c->start >= 0 && c->start < 70)
135 c->start += 2000;
36697dc0 136
e78fce48
DC
137 if (c->stop >= 0 && c->stop < 70)
138 c->stop += 2000;
a2eb5ea7 139
e78fce48
DC
140 if (c->start >= 70 && c->start < 100)
141 c->start += 1900;
36697dc0 142
e78fce48
DC
143 if (c->stop >= 70 && c->stop < 100)
144 c->stop += 1900;
a2eb5ea7 145
482f3b54 146 c = c->next;
36697dc0
LP
147 }
148}
149
150int calendar_spec_normalize(CalendarSpec *c) {
151 assert(c);
152
10e859a2
LP
153 if (streq_ptr(c->timezone, "UTC")) {
154 c->utc = true;
155 c->timezone = mfree(c->timezone);
156 }
157
489464d0 158 if (c->weekdays_bits <= 0 || c->weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
159 c->weekdays_bits = -1;
160
8ea80351
DC
161 if (c->end_of_month && !c->day)
162 c->end_of_month = false;
163
36697dc0
LP
164 fix_year(c->year);
165
a2eb5ea7
DC
166 normalize_chain(&c->year);
167 normalize_chain(&c->month);
168 normalize_chain(&c->day);
169 normalize_chain(&c->hour);
170 normalize_chain(&c->minute);
171 normalize_chain(&c->microsecond);
36697dc0
LP
172
173 return 0;
174}
175
c58b1b3a 176_pure_ static bool chain_valid(CalendarComponent *c, int from, int to, bool end_of_month) {
e127f26b
ZJS
177 assert(to >= from);
178
36697dc0
LP
179 if (!c)
180 return true;
181
a2eb5ea7
DC
182 /* Forbid dates more than 28 days from the end of the month */
183 if (end_of_month)
184 to -= 3;
185
e78fce48 186 if (c->start < from || c->start > to)
36697dc0
LP
187 return false;
188
e127f26b
ZJS
189 /* Avoid overly large values that could cause overflow */
190 if (c->repeat > to - from)
191 return false;
192
8ea80351
DC
193 /*
194 * c->repeat must be short enough so at least one repetition may
195 * occur before the end of the interval. For dates scheduled
e78fce48 196 * relative to the end of the month, c->start and c->stop
a2eb5ea7 197 * correspond to the Nth last day of the month.
8ea80351 198 */
e78fce48
DC
199 if (c->stop >= 0) {
200 if (c->stop < from || c ->stop > to)
a2eb5ea7 201 return false;
8ea80351 202
e78fce48 203 if (c->start + c->repeat > c->stop)
a2eb5ea7
DC
204 return false;
205 } else {
e78fce48 206 if (end_of_month && c->start - c->repeat < from)
a2eb5ea7
DC
207 return false;
208
e78fce48 209 if (!end_of_month && c->start + c->repeat > to)
a2eb5ea7
DC
210 return false;
211 }
36697dc0
LP
212
213 if (c->next)
c58b1b3a 214 return chain_valid(c->next, from, to, end_of_month);
36697dc0
LP
215
216 return true;
217}
218
44a6b1b6 219_pure_ bool calendar_spec_valid(CalendarSpec *c) {
36697dc0
LP
220 assert(c);
221
489464d0 222 if (c->weekdays_bits > BITS_WEEKDAYS)
36697dc0
LP
223 return false;
224
8ea80351 225 if (!chain_valid(c->year, MIN_YEAR, MAX_YEAR, false))
36697dc0
LP
226 return false;
227
8ea80351 228 if (!chain_valid(c->month, 1, 12, false))
36697dc0
LP
229 return false;
230
8ea80351 231 if (!chain_valid(c->day, 1, 31, c->end_of_month))
36697dc0
LP
232 return false;
233
8ea80351 234 if (!chain_valid(c->hour, 0, 23, false))
36697dc0
LP
235 return false;
236
8ea80351 237 if (!chain_valid(c->minute, 0, 59, false))
36697dc0
LP
238 return false;
239
8ea80351 240 if (!chain_valid(c->microsecond, 0, 60*USEC_PER_SEC-1, false))
36697dc0
LP
241 return false;
242
243 return true;
244}
245
246static void format_weekdays(FILE *f, const CalendarSpec *c) {
247 static const char *const days[] = {
248 "Mon",
249 "Tue",
250 "Wed",
251 "Thu",
252 "Fri",
253 "Sat",
254 "Sun"
255 };
256
257 int l, x;
e638d050 258 bool need_comma = false;
36697dc0
LP
259
260 assert(f);
261 assert(c);
489464d0 262 assert(c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS);
36697dc0
LP
263
264 for (x = 0, l = -1; x < (int) ELEMENTSOF(days); x++) {
265
266 if (c->weekdays_bits & (1 << x)) {
267
268 if (l < 0) {
e638d050 269 if (need_comma)
0d536673 270 fputc(',', f);
36697dc0 271 else
e638d050 272 need_comma = true;
36697dc0 273
0d536673 274 fputs(days[x], f);
36697dc0
LP
275 l = x;
276 }
277
278 } else if (l >= 0) {
279
280 if (x > l + 1) {
0d536673
LP
281 fputs(x > l + 2 ? ".." : ",", f);
282 fputs(days[x-1], f);
36697dc0
LP
283 }
284
285 l = -1;
286 }
287 }
288
289 if (l >= 0 && x > l + 1) {
0d536673
LP
290 fputs(x > l + 2 ? ".." : ",", f);
291 fputs(days[x-1], f);
36697dc0
LP
292 }
293}
294
436dd70f 295static void format_chain(FILE *f, int space, const CalendarComponent *c, bool usec) {
7c250321 296 int d = usec ? (int) USEC_PER_SEC : 1;
9904dc00 297
36697dc0
LP
298 assert(f);
299
300 if (!c) {
0d536673 301 fputc('*', f);
36697dc0
LP
302 return;
303 }
304
482f3b54 305 if (usec && c->start == 0 && c->repeat == USEC_PER_SEC && !c->next) {
0d536673 306 fputc('*', f);
482f3b54
DC
307 return;
308 }
309
e78fce48 310 assert(c->start >= 0);
7c250321 311
e78fce48
DC
312 fprintf(f, "%0*i", space, c->start / d);
313 if (c->start % d > 0)
314 fprintf(f, ".%06i", c->start % d);
7c250321 315
e78fce48
DC
316 if (c->stop > 0)
317 fprintf(f, "..%0*i", space, c->stop / d);
318 if (c->stop % d > 0)
319 fprintf(f, ".%06i", c->stop % d);
a2eb5ea7 320
e78fce48 321 if (c->repeat > 0 && !(c->stop > 0 && c->repeat == d))
7c250321 322 fprintf(f, "/%i", c->repeat / d);
a2eb5ea7 323 if (c->repeat % d > 0)
7c250321 324 fprintf(f, ".%06i", c->repeat % d);
36697dc0 325
a2eb5ea7 326 if (c->next) {
0d536673 327 fputc(',', f);
436dd70f 328 format_chain(f, space, c->next, usec);
36697dc0
LP
329 }
330}
331
332int calendar_spec_to_string(const CalendarSpec *c, char **p) {
333 char *buf = NULL;
334 size_t sz = 0;
335 FILE *f;
dacd6cee 336 int r;
36697dc0
LP
337
338 assert(c);
339 assert(p);
340
341 f = open_memstream(&buf, &sz);
342 if (!f)
343 return -ENOMEM;
344
0d536673
LP
345 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
346
489464d0 347 if (c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS) {
36697dc0 348 format_weekdays(f, c);
0d536673 349 fputc(' ', f);
36697dc0
LP
350 }
351
436dd70f 352 format_chain(f, 4, c->year, false);
0d536673 353 fputc('-', f);
436dd70f 354 format_chain(f, 2, c->month, false);
0d536673 355 fputc(c->end_of_month ? '~' : '-', f);
436dd70f 356 format_chain(f, 2, c->day, false);
0d536673 357 fputc(' ', f);
436dd70f 358 format_chain(f, 2, c->hour, false);
0d536673 359 fputc(':', f);
436dd70f 360 format_chain(f, 2, c->minute, false);
0d536673 361 fputc(':', f);
482f3b54 362 format_chain(f, 2, c->microsecond, true);
36697dc0 363
51ffa239 364 if (c->utc)
0d536673 365 fputs(" UTC", f);
48d26c01 366 else if (c->timezone != NULL) {
0d536673
LP
367 fputc(' ', f);
368 fputs(c->timezone, f);
48d26c01 369 } else if (IN_SET(c->dst, 0, 1)) {
21b3a0fc
LP
370
371 /* If daylight saving is explicitly on or off, let's show the used timezone. */
372
373 tzset();
374
375 if (!isempty(tzname[c->dst])) {
0d536673
LP
376 fputc(' ', f);
377 fputs(tzname[c->dst], f);
21b3a0fc
LP
378 }
379 }
51ffa239 380
dacd6cee
LP
381 r = fflush_and_check(f);
382 if (r < 0) {
36697dc0
LP
383 free(buf);
384 fclose(f);
dacd6cee 385 return r;
36697dc0
LP
386 }
387
388 fclose(f);
389
390 *p = buf;
391 return 0;
392}
393
394static int parse_weekdays(const char **p, CalendarSpec *c) {
395 static const struct {
396 const char *name;
397 const int nr;
398 } day_nr[] = {
399 { "Monday", 0 },
400 { "Mon", 0 },
401 { "Tuesday", 1 },
402 { "Tue", 1 },
403 { "Wednesday", 2 },
404 { "Wed", 2 },
405 { "Thursday", 3 },
406 { "Thu", 3 },
407 { "Friday", 4 },
408 { "Fri", 4 },
409 { "Saturday", 5 },
410 { "Sat", 5 },
411 { "Sunday", 6 },
412 { "Sun", 6 }
413 };
414
415 int l = -1;
416 bool first = true;
417
418 assert(p);
419 assert(*p);
420 assert(c);
421
422 for (;;) {
da6053d0 423 size_t i;
36697dc0 424
36697dc0
LP
425 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
426 size_t skip;
427
428 if (!startswith_no_case(*p, day_nr[i].name))
429 continue;
430
431 skip = strlen(day_nr[i].name);
432
4c701096 433 if (!IN_SET((*p)[skip], 0, '-', '.', ',', ' '))
36697dc0
LP
434 return -EINVAL;
435
436 c->weekdays_bits |= 1 << day_nr[i].nr;
437
438 if (l >= 0) {
439 int j;
440
441 if (l > day_nr[i].nr)
442 return -EINVAL;
443
444 for (j = l + 1; j < day_nr[i].nr; j++)
445 c->weekdays_bits |= 1 << j;
446 }
447
448 *p += skip;
449 break;
450 }
451
452 /* Couldn't find this prefix, so let's assume the
453 weekday was not specified and let's continue with
454 the date */
455 if (i >= ELEMENTSOF(day_nr))
456 return first ? 0 : -EINVAL;
457
458 /* We reached the end of the string */
459 if (**p == 0)
460 return 0;
461
462 /* We reached the end of the weekday spec part */
463 if (**p == ' ') {
464 *p += strspn(*p, " ");
465 return 0;
466 }
467
e638d050
DC
468 if (**p == '.') {
469 if (l >= 0)
470 return -EINVAL;
471
472 if ((*p)[1] != '.')
473 return -EINVAL;
474
475 l = day_nr[i].nr;
6bae2fd4 476 *p += 2;
e638d050
DC
477
478 /* Support ranges with "-" for backwards compatibility */
479 } else if (**p == '-') {
36697dc0
LP
480 if (l >= 0)
481 return -EINVAL;
482
483 l = day_nr[i].nr;
6bae2fd4
DC
484 *p += 1;
485
486 } else if (**p == ',') {
36697dc0 487 l = -1;
6bae2fd4
DC
488 *p += 1;
489 }
490
482f3b54 491 /* Allow a trailing comma but not an open range */
4c701096 492 if (IN_SET(**p, 0, ' ')) {
6bae2fd4
DC
493 *p += strspn(*p, " ");
494 return l < 0 ? 0 : -EINVAL;
495 }
36697dc0 496
36697dc0
LP
497 first = false;
498 }
499}
500
d80e5b74
ZJS
501static int parse_one_number(const char *p, const char **e, unsigned long *ret) {
502 char *ee = NULL;
503 unsigned long value;
504
505 errno = 0;
506 value = strtoul(p, &ee, 10);
507 if (errno > 0)
508 return -errno;
509 if (ee == p)
510 return -EINVAL;
511
512 *ret = value;
513 *e = ee;
514 return 0;
515}
516
c0aebb4b 517static int parse_component_decimal(const char **p, bool usec, int *res) {
436dd70f
HV
518 unsigned long value;
519 const char *e = NULL;
436dd70f 520 int r;
36697dc0 521
9dfa81a0
DC
522 if (!isdigit(**p))
523 return -EINVAL;
524
d80e5b74
ZJS
525 r = parse_one_number(*p, &e, &value);
526 if (r < 0)
527 return r;
36697dc0 528
436dd70f
HV
529 if (usec) {
530 if (value * USEC_PER_SEC / USEC_PER_SEC != value)
36697dc0
LP
531 return -ERANGE;
532
436dd70f 533 value *= USEC_PER_SEC;
436dd70f 534
60bf5836
DC
535 /* One "." is a decimal point, but ".." is a range separator */
536 if (e[0] == '.' && e[1] != '.') {
537 unsigned add;
36ff0c97 538
436dd70f
HV
539 e++;
540 r = parse_fractional_part_u(&e, 6, &add);
541 if (r < 0)
542 return r;
543
544 if (add + value < value)
545 return -ERANGE;
546 value += add;
547 }
548 }
549
c0aebb4b
DC
550 if (value > INT_MAX)
551 return -ERANGE;
552
436dd70f
HV
553 *p = e;
554 *res = value;
555
556 return 0;
557}
558
32b52369
DC
559static int const_chain(int value, CalendarComponent **c) {
560 CalendarComponent *cc = NULL;
561
562 assert(c);
563
564 cc = new0(CalendarComponent, 1);
565 if (!cc)
566 return -ENOMEM;
567
e78fce48
DC
568 cc->start = value;
569 cc->stop = -1;
32b52369
DC
570 cc->repeat = 0;
571 cc->next = *c;
572
573 *c = cc;
574
575 return 0;
576}
577
d80e5b74
ZJS
578static int calendarspec_from_time_t(CalendarSpec *c, time_t time) {
579 struct tm tm;
580 CalendarComponent *year = NULL, *month = NULL, *day = NULL, *hour = NULL, *minute = NULL, *us = NULL;
581 int r;
582
55a30fd4
ZJS
583 if (!gmtime_r(&time, &tm))
584 return -ERANGE;
d80e5b74
ZJS
585
586 r = const_chain(tm.tm_year + 1900, &year);
587 if (r < 0)
588 return r;
589
590 r = const_chain(tm.tm_mon + 1, &month);
591 if (r < 0)
592 return r;
593
594 r = const_chain(tm.tm_mday, &day);
595 if (r < 0)
596 return r;
597
598 r = const_chain(tm.tm_hour, &hour);
599 if (r < 0)
600 return r;
601
602 r = const_chain(tm.tm_min, &minute);
603 if (r < 0)
604 return r;
605
606 r = const_chain(tm.tm_sec * USEC_PER_SEC, &us);
607 if (r < 0)
608 return r;
609
610 c->utc = true;
611 c->year = year;
612 c->month = month;
613 c->day = day;
614 c->hour = hour;
615 c->minute = minute;
616 c->microsecond = us;
617 return 0;
618}
619
74353158 620static int prepend_component(const char **p, bool usec, unsigned nesting, CalendarComponent **c) {
c0aebb4b 621 int r, start, stop = -1, repeat = 0;
436dd70f 622 CalendarComponent *cc;
74353158 623 const char *e = *p;
436dd70f
HV
624
625 assert(p);
626 assert(c);
627
74353158
ZJS
628 if (nesting > CALENDARSPEC_COMPONENTS_MAX)
629 return -ENOBUFS;
436dd70f 630
e78fce48 631 r = parse_component_decimal(&e, usec, &start);
436dd70f
HV
632 if (r < 0)
633 return r;
634
a2eb5ea7
DC
635 if (e[0] == '.' && e[1] == '.') {
636 e += 2;
e78fce48 637 r = parse_component_decimal(&e, usec, &stop);
a2eb5ea7
DC
638 if (r < 0)
639 return r;
640
641 repeat = usec ? USEC_PER_SEC : 1;
642 }
643
436dd70f
HV
644 if (*e == '/') {
645 e++;
646 r = parse_component_decimal(&e, usec, &repeat);
647 if (r < 0)
648 return r;
649
650 if (repeat == 0)
651 return -ERANGE;
36697dc0
LP
652 }
653
4c701096 654 if (!IN_SET(*e, 0, ' ', ',', '-', '~', ':'))
36697dc0
LP
655 return -EINVAL;
656
657 cc = new0(CalendarComponent, 1);
658 if (!cc)
659 return -ENOMEM;
660
e78fce48
DC
661 cc->start = start;
662 cc->stop = stop;
36697dc0
LP
663 cc->repeat = repeat;
664 cc->next = *c;
665
666 *p = e;
667 *c = cc;
668
669 if (*e ==',') {
670 *p += 1;
74353158 671 return prepend_component(p, usec, nesting + 1, c);
36697dc0
LP
672 }
673
674 return 0;
675}
676
436dd70f 677static int parse_chain(const char **p, bool usec, CalendarComponent **c) {
36697dc0
LP
678 const char *t;
679 CalendarComponent *cc = NULL;
680 int r;
681
682 assert(p);
683 assert(c);
684
685 t = *p;
686
687 if (t[0] == '*') {
436dd70f
HV
688 if (usec) {
689 r = const_chain(0, c);
690 if (r < 0)
691 return r;
692 (*c)->repeat = USEC_PER_SEC;
693 } else
694 *c = NULL;
695
36697dc0 696 *p = t + 1;
36697dc0
LP
697 return 0;
698 }
699
74353158 700 r = prepend_component(&t, usec, 0, &cc);
36697dc0
LP
701 if (r < 0) {
702 free_chain(cc);
703 return r;
704 }
705
706 *p = t;
707 *c = cc;
708 return 0;
709}
710
36697dc0
LP
711static int parse_date(const char **p, CalendarSpec *c) {
712 const char *t;
713 int r;
714 CalendarComponent *first, *second, *third;
715
716 assert(p);
717 assert(*p);
718 assert(c);
719
720 t = *p;
721
722 if (*t == 0)
723 return 0;
724
d80e5b74
ZJS
725 /* @TIMESTAMP — UNIX time in seconds since the epoch */
726 if (*t == '@') {
727 unsigned long value;
728 time_t time;
729
730 r = parse_one_number(t + 1, &t, &value);
731 if (r < 0)
732 return r;
733
734 time = value;
735 if ((unsigned long) time != value)
736 return -ERANGE;
737
738 r = calendarspec_from_time_t(c, time);
739 if (r < 0)
740 return r;
741
742 *p = t;
743 return 1; /* finito, don't parse H:M:S after that */
744 }
745
436dd70f 746 r = parse_chain(&t, false, &first);
36697dc0
LP
747 if (r < 0)
748 return r;
749
750 /* Already the end? A ':' as separator? In that case this was a time, not a date */
4c701096 751 if (IN_SET(*t, 0, ':')) {
36697dc0
LP
752 free_chain(first);
753 return 0;
754 }
755
8ea80351
DC
756 if (*t == '~')
757 c->end_of_month = true;
758 else if (*t != '-') {
36697dc0
LP
759 free_chain(first);
760 return -EINVAL;
761 }
762
763 t++;
436dd70f 764 r = parse_chain(&t, false, &second);
36697dc0
LP
765 if (r < 0) {
766 free_chain(first);
767 return r;
768 }
769
770 /* Got two parts, hence it's month and day */
4c701096 771 if (IN_SET(*t, 0, ' ')) {
36697dc0
LP
772 *p = t + strspn(t, " ");
773 c->month = first;
774 c->day = second;
775 return 0;
fc2371c7
DC
776 } else if (c->end_of_month) {
777 free_chain(first);
778 free_chain(second);
8ea80351 779 return -EINVAL;
fc2371c7 780 }
36697dc0 781
8ea80351
DC
782 if (*t == '~')
783 c->end_of_month = true;
784 else if (*t != '-') {
36697dc0
LP
785 free_chain(first);
786 free_chain(second);
787 return -EINVAL;
788 }
789
790 t++;
436dd70f 791 r = parse_chain(&t, false, &third);
36697dc0
LP
792 if (r < 0) {
793 free_chain(first);
794 free_chain(second);
795 return r;
796 }
797
8ea80351 798 /* Got three parts, hence it is year, month and day */
4c701096 799 if (IN_SET(*t, 0, ' ')) {
36697dc0
LP
800 *p = t + strspn(t, " ");
801 c->year = first;
802 c->month = second;
803 c->day = third;
804 return 0;
805 }
806
807 free_chain(first);
808 free_chain(second);
809 free_chain(third);
810 return -EINVAL;
811}
812
519cffec 813static int parse_calendar_time(const char **p, CalendarSpec *c) {
36697dc0
LP
814 CalendarComponent *h = NULL, *m = NULL, *s = NULL;
815 const char *t;
816 int r;
817
818 assert(p);
819 assert(*p);
820 assert(c);
821
822 t = *p;
823
408a51e1
DC
824 /* If no time is specified at all, then this means 00:00:00 */
825 if (*t == 0)
826 goto null_hour;
36697dc0 827
436dd70f 828 r = parse_chain(&t, false, &h);
36697dc0
LP
829 if (r < 0)
830 goto fail;
831
832 if (*t != ':') {
833 r = -EINVAL;
834 goto fail;
835 }
836
837 t++;
436dd70f 838 r = parse_chain(&t, false, &m);
36697dc0
LP
839 if (r < 0)
840 goto fail;
841
842 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
c0df71fa
DC
843 if (*t == 0)
844 goto null_second;
36697dc0
LP
845
846 if (*t != ':') {
847 r = -EINVAL;
848 goto fail;
849 }
850
851 t++;
436dd70f 852 r = parse_chain(&t, true, &s);
36697dc0
LP
853 if (r < 0)
854 goto fail;
855
856 /* At the end? Then it's hours, minutes and seconds */
857 if (*t == 0)
858 goto finish;
859
860 r = -EINVAL;
861 goto fail;
862
863null_hour:
864 r = const_chain(0, &h);
865 if (r < 0)
866 goto fail;
867
868 r = const_chain(0, &m);
869 if (r < 0)
870 goto fail;
871
872null_second:
873 r = const_chain(0, &s);
874 if (r < 0)
875 goto fail;
876
877finish:
878 *p = t;
879 c->hour = h;
880 c->minute = m;
436dd70f
HV
881 c->microsecond = s;
882
36697dc0
LP
883 return 0;
884
885fail:
886 free_chain(h);
887 free_chain(m);
888 free_chain(s);
889 return r;
890}
891
892int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
21b3a0fc 893 const char *utc;
921b5987 894 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
36697dc0
LP
895 int r;
896
897 assert(p);
898 assert(spec);
899
36697dc0
LP
900 c = new0(CalendarSpec, 1);
901 if (!c)
902 return -ENOMEM;
21b3a0fc 903 c->dst = -1;
48d26c01 904 c->timezone = NULL;
36697dc0 905
078efddd
HV
906 utc = endswith_no_case(p, " UTC");
907 if (utc) {
908 c->utc = true;
909 p = strndupa(p, utc - p);
21b3a0fc
LP
910 } else {
911 const char *e = NULL;
912 int j;
913
914 tzset();
915
916 /* Check if the local timezone was specified? */
917 for (j = 0; j <= 1; j++) {
918 if (isempty(tzname[j]))
919 continue;
920
921 e = endswith_no_case(p, tzname[j]);
d80e5b74 922 if (!e)
21b3a0fc
LP
923 continue;
924 if (e == p)
925 continue;
926 if (e[-1] != ' ')
927 continue;
928
929 break;
930 }
931
932 /* Found one of the two timezones specified? */
933 if (IN_SET(j, 0, 1)) {
934 p = strndupa(p, e - p - 1);
935 c->dst = j;
48d26c01
IK
936 } else {
937 const char *last_space;
48d26c01 938
a2932a0d 939 last_space = strrchr(p, ' ');
089fb865 940 if (last_space != NULL && timezone_is_valid(last_space + 1, LOG_DEBUG)) {
a2932a0d 941 c->timezone = strdup(last_space + 1);
921b5987
DT
942 if (!c->timezone)
943 return -ENOMEM;
a2932a0d
ZJS
944
945 p = strndupa(p, last_space - p);
48d26c01 946 }
21b3a0fc 947 }
078efddd 948 }
51ffa239 949
921b5987
DT
950 if (isempty(p))
951 return -EINVAL;
04773cb5 952
272ac205 953 if (strcaseeq(p, "minutely")) {
436dd70f 954 r = const_chain(0, &c->microsecond);
272ac205 955 if (r < 0)
921b5987 956 return r;
272ac205
DM
957
958 } else if (strcaseeq(p, "hourly")) {
36697dc0
LP
959 r = const_chain(0, &c->minute);
960 if (r < 0)
921b5987 961 return r;
436dd70f 962 r = const_chain(0, &c->microsecond);
36697dc0 963 if (r < 0)
921b5987 964 return r;
36697dc0 965
b43d1d01 966 } else if (strcaseeq(p, "daily")) {
36697dc0
LP
967 r = const_chain(0, &c->hour);
968 if (r < 0)
921b5987 969 return r;
36697dc0
LP
970 r = const_chain(0, &c->minute);
971 if (r < 0)
921b5987 972 return r;
436dd70f 973 r = const_chain(0, &c->microsecond);
36697dc0 974 if (r < 0)
921b5987 975 return r;
36697dc0 976
b43d1d01 977 } else if (strcaseeq(p, "monthly")) {
36697dc0
LP
978 r = const_chain(1, &c->day);
979 if (r < 0)
921b5987 980 return r;
36697dc0
LP
981 r = const_chain(0, &c->hour);
982 if (r < 0)
921b5987 983 return r;
36697dc0
LP
984 r = const_chain(0, &c->minute);
985 if (r < 0)
921b5987 986 return r;
436dd70f 987 r = const_chain(0, &c->microsecond);
36697dc0 988 if (r < 0)
921b5987 989 return r;
36697dc0 990
dbfd41e2
LP
991 } else if (strcaseeq(p, "annually") ||
992 strcaseeq(p, "yearly") ||
993 strcaseeq(p, "anually") /* backwards compatibility */ ) {
994
13516818
LP
995 r = const_chain(1, &c->month);
996 if (r < 0)
921b5987 997 return r;
13516818
LP
998 r = const_chain(1, &c->day);
999 if (r < 0)
921b5987 1000 return r;
13516818
LP
1001 r = const_chain(0, &c->hour);
1002 if (r < 0)
921b5987 1003 return r;
13516818
LP
1004 r = const_chain(0, &c->minute);
1005 if (r < 0)
921b5987 1006 return r;
436dd70f 1007 r = const_chain(0, &c->microsecond);
13516818 1008 if (r < 0)
921b5987 1009 return r;
13516818 1010
b43d1d01 1011 } else if (strcaseeq(p, "weekly")) {
36697dc0
LP
1012
1013 c->weekdays_bits = 1;
1014
1015 r = const_chain(0, &c->hour);
1016 if (r < 0)
921b5987 1017 return r;
36697dc0
LP
1018 r = const_chain(0, &c->minute);
1019 if (r < 0)
921b5987 1020 return r;
436dd70f 1021 r = const_chain(0, &c->microsecond);
dbfd41e2 1022 if (r < 0)
921b5987 1023 return r;
dbfd41e2
LP
1024
1025 } else if (strcaseeq(p, "quarterly")) {
1026
1027 r = const_chain(1, &c->month);
1028 if (r < 0)
921b5987 1029 return r;
dbfd41e2
LP
1030 r = const_chain(4, &c->month);
1031 if (r < 0)
921b5987 1032 return r;
dbfd41e2
LP
1033 r = const_chain(7, &c->month);
1034 if (r < 0)
921b5987 1035 return r;
dbfd41e2
LP
1036 r = const_chain(10, &c->month);
1037 if (r < 0)
921b5987 1038 return r;
dbfd41e2
LP
1039 r = const_chain(1, &c->day);
1040 if (r < 0)
921b5987 1041 return r;
dbfd41e2
LP
1042 r = const_chain(0, &c->hour);
1043 if (r < 0)
921b5987 1044 return r;
dbfd41e2
LP
1045 r = const_chain(0, &c->minute);
1046 if (r < 0)
921b5987 1047 return r;
436dd70f 1048 r = const_chain(0, &c->microsecond);
dbfd41e2 1049 if (r < 0)
921b5987 1050 return r;
dbfd41e2
LP
1051
1052 } else if (strcaseeq(p, "biannually") ||
1053 strcaseeq(p, "bi-annually") ||
1054 strcaseeq(p, "semiannually") ||
1055 strcaseeq(p, "semi-annually")) {
1056
1057 r = const_chain(1, &c->month);
1058 if (r < 0)
921b5987 1059 return r;
dbfd41e2
LP
1060 r = const_chain(7, &c->month);
1061 if (r < 0)
921b5987 1062 return r;
dbfd41e2
LP
1063 r = const_chain(1, &c->day);
1064 if (r < 0)
921b5987 1065 return r;
dbfd41e2
LP
1066 r = const_chain(0, &c->hour);
1067 if (r < 0)
921b5987 1068 return r;
dbfd41e2
LP
1069 r = const_chain(0, &c->minute);
1070 if (r < 0)
921b5987 1071 return r;
436dd70f 1072 r = const_chain(0, &c->microsecond);
36697dc0 1073 if (r < 0)
921b5987 1074 return r;
36697dc0
LP
1075
1076 } else {
1077 r = parse_weekdays(&p, c);
1078 if (r < 0)
921b5987 1079 return r;
36697dc0
LP
1080
1081 r = parse_date(&p, c);
1082 if (r < 0)
921b5987 1083 return r;
36697dc0 1084
d80e5b74
ZJS
1085 if (r == 0) {
1086 r = parse_calendar_time(&p, c);
1087 if (r < 0)
921b5987 1088 return r;
d80e5b74 1089 }
36697dc0 1090
921b5987
DT
1091 if (*p != 0)
1092 return -EINVAL;
36697dc0
LP
1093 }
1094
1095 r = calendar_spec_normalize(c);
1096 if (r < 0)
921b5987 1097 return r;
36697dc0 1098
921b5987
DT
1099 if (!calendar_spec_valid(c))
1100 return -EINVAL;
36697dc0 1101
921b5987 1102 *spec = TAKE_PTR(c);
36697dc0 1103 return 0;
36697dc0
LP
1104}
1105
60bf5836 1106static int find_end_of_month(struct tm *tm, bool utc, int day) {
a2eb5ea7
DC
1107 struct tm t = *tm;
1108
1109 t.tm_mon++;
1110 t.tm_mday = 1 - day;
1111
c477ff14 1112 if (mktime_or_timegm(&t, utc) < 0 ||
a2eb5ea7
DC
1113 t.tm_mon != tm->tm_mon)
1114 return -1;
1115
1116 return t.tm_mday;
1117}
1118
8ea80351
DC
1119static int find_matching_component(const CalendarSpec *spec, const CalendarComponent *c,
1120 struct tm *tm, int *val) {
482f3b54 1121 const CalendarComponent *p = c;
e78fce48 1122 int start, stop, d = -1;
36697dc0
LP
1123 bool d_set = false;
1124 int r;
1125
1126 assert(val);
1127
1128 if (!c)
1129 return 0;
1130
1131 while (c) {
e78fce48
DC
1132 start = c->start;
1133 stop = c->stop;
a2eb5ea7 1134
8ea80351 1135 if (spec->end_of_month && p == spec->day) {
e78fce48
DC
1136 start = find_end_of_month(tm, spec->utc, start);
1137 stop = find_end_of_month(tm, spec->utc, stop);
a2eb5ea7 1138
e78fce48
DC
1139 if (stop > 0)
1140 SWAP_TWO(start, stop);
a2eb5ea7 1141 }
8ea80351 1142
e78fce48 1143 if (start >= *val) {
36697dc0 1144
e78fce48
DC
1145 if (!d_set || start < d) {
1146 d = start;
36697dc0
LP
1147 d_set = true;
1148 }
1149
1150 } else if (c->repeat > 0) {
1151 int k;
1152
be6b0c21 1153 k = start + c->repeat * DIV_ROUND_UP(*val - start, c->repeat);
36697dc0 1154
e78fce48 1155 if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
36697dc0
LP
1156 d = k;
1157 d_set = true;
1158 }
1159 }
1160
482f3b54 1161 c = c->next;
36697dc0
LP
1162 }
1163
1164 if (!d_set)
1165 return -ENOENT;
1166
1167 r = *val != d;
1168 *val = d;
1169 return r;
1170}
1171
51ffa239 1172static bool tm_out_of_bounds(const struct tm *tm, bool utc) {
36697dc0
LP
1173 struct tm t;
1174 assert(tm);
1175
1176 t = *tm;
1177
c477ff14 1178 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1179 return true;
1180
f6e7d66b
DC
1181 /*
1182 * Set an upper bound on the year so impossible dates like "*-02-31"
1183 * don't cause find_next() to loop forever. tm_year contains years
1184 * since 1900, so adjust it accordingly.
1185 */
1186 if (tm->tm_year + 1900 > MAX_YEAR)
1187 return true;
1188
36697dc0
LP
1189 /* Did any normalization take place? If so, it was out of bounds before */
1190 return
1191 t.tm_year != tm->tm_year ||
1192 t.tm_mon != tm->tm_mon ||
1193 t.tm_mday != tm->tm_mday ||
1194 t.tm_hour != tm->tm_hour ||
1195 t.tm_min != tm->tm_min ||
1196 t.tm_sec != tm->tm_sec;
1197}
1198
51ffa239 1199static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
36697dc0
LP
1200 struct tm t;
1201 int k;
1202
489464d0 1203 if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
1204 return true;
1205
1206 t = *tm;
c477ff14 1207 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1208 return false;
1209
1210 k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
1211 return (weekdays_bits & (1 << k));
1212}
1213
436dd70f 1214static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
36697dc0 1215 struct tm c;
436dd70f 1216 int tm_usec;
36697dc0
LP
1217 int r;
1218
1219 assert(spec);
1220 assert(tm);
1221
1222 c = *tm;
436dd70f 1223 tm_usec = *usec;
36697dc0
LP
1224
1225 for (;;) {
1226 /* Normalize the current date */
ea3894c1 1227 (void) mktime_or_timegm(&c, spec->utc);
21b3a0fc 1228 c.tm_isdst = spec->dst;
36697dc0
LP
1229
1230 c.tm_year += 1900;
8ea80351 1231 r = find_matching_component(spec, spec->year, &c, &c.tm_year);
36697dc0
LP
1232 c.tm_year -= 1900;
1233
1234 if (r > 0) {
1235 c.tm_mon = 0;
1236 c.tm_mday = 1;
436dd70f 1237 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1238 }
e308ddca 1239 if (r < 0)
36697dc0 1240 return r;
e308ddca
LP
1241 if (tm_out_of_bounds(&c, spec->utc))
1242 return -ENOENT;
36697dc0
LP
1243
1244 c.tm_mon += 1;
8ea80351 1245 r = find_matching_component(spec, spec->month, &c, &c.tm_mon);
36697dc0
LP
1246 c.tm_mon -= 1;
1247
1248 if (r > 0) {
1249 c.tm_mday = 1;
436dd70f 1250 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1251 }
51ffa239 1252 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1253 c.tm_year++;
36697dc0
LP
1254 c.tm_mon = 0;
1255 c.tm_mday = 1;
436dd70f 1256 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1257 continue;
1258 }
1259
8ea80351 1260 r = find_matching_component(spec, spec->day, &c, &c.tm_mday);
36697dc0 1261 if (r > 0)
436dd70f 1262 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1263 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1264 c.tm_mon++;
36697dc0 1265 c.tm_mday = 1;
436dd70f 1266 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1267 continue;
1268 }
1269
51ffa239 1270 if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) {
36697dc0 1271 c.tm_mday++;
436dd70f 1272 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1273 continue;
1274 }
1275
8ea80351 1276 r = find_matching_component(spec, spec->hour, &c, &c.tm_hour);
36697dc0 1277 if (r > 0)
a022d76e 1278 c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1279 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1280 c.tm_mday++;
436dd70f 1281 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1282 continue;
1283 }
1284
8ea80351 1285 r = find_matching_component(spec, spec->minute, &c, &c.tm_min);
36697dc0 1286 if (r > 0)
a022d76e 1287 c.tm_sec = tm_usec = 0;
51ffa239 1288 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1289 c.tm_hour++;
436dd70f 1290 c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1291 continue;
1292 }
1293
436dd70f 1294 c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec;
8ea80351 1295 r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec);
436dd70f
HV
1296 tm_usec = c.tm_sec % USEC_PER_SEC;
1297 c.tm_sec /= USEC_PER_SEC;
1298
51ffa239 1299 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1300 c.tm_min++;
436dd70f 1301 c.tm_sec = tm_usec = 0;
36697dc0
LP
1302 continue;
1303 }
1304
36697dc0 1305 *tm = c;
436dd70f 1306 *usec = tm_usec;
36697dc0
LP
1307 return 0;
1308 }
1309}
1310
48d26c01 1311static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *next) {
36697dc0
LP
1312 struct tm tm;
1313 time_t t;
1314 int r;
436dd70f 1315 usec_t tm_usec;
36697dc0
LP
1316
1317 assert(spec);
1318 assert(next);
1319
1bb4b028
LP
1320 if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1321 return -EINVAL;
1322
436dd70f
HV
1323 usec++;
1324 t = (time_t) (usec / USEC_PER_SEC);
51ffa239 1325 assert_se(localtime_or_gmtime_r(&t, &tm, spec->utc));
436dd70f 1326 tm_usec = usec % USEC_PER_SEC;
36697dc0 1327
436dd70f 1328 r = find_next(spec, &tm, &tm_usec);
36697dc0
LP
1329 if (r < 0)
1330 return r;
1331
51ffa239 1332 t = mktime_or_timegm(&tm, spec->utc);
c477ff14 1333 if (t < 0)
36697dc0
LP
1334 return -EINVAL;
1335
436dd70f 1336 *next = (usec_t) t * USEC_PER_SEC + tm_usec;
36697dc0
LP
1337 return 0;
1338}
48d26c01
IK
1339
1340typedef struct SpecNextResult {
1341 usec_t next;
1342 int return_value;
1343} SpecNextResult;
1344
1345int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
4c253ed1 1346 SpecNextResult *shared, tmp;
48d26c01
IK
1347 int r;
1348
1349 if (isempty(spec->timezone))
1350 return calendar_spec_next_usec_impl(spec, usec, next);
1351
1352 shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
1353 if (shared == MAP_FAILED)
1354 return negative_errno();
1355
1f5d1e02 1356 r = safe_fork("(sd-calendar)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL);
4c253ed1 1357 if (r < 0) {
48d26c01 1358 (void) munmap(shared, sizeof *shared);
4c253ed1 1359 return r;
48d26c01 1360 }
4c253ed1 1361 if (r == 0) {
48d26c01
IK
1362 if (setenv("TZ", spec->timezone, 1) != 0) {
1363 shared->return_value = negative_errno();
1364 _exit(EXIT_FAILURE);
1365 }
1366
1367 tzset();
1368
1369 shared->return_value = calendar_spec_next_usec_impl(spec, usec, &shared->next);
1370
1371 _exit(EXIT_SUCCESS);
1372 }
1373
48d26c01 1374 tmp = *shared;
1f5d1e02 1375 if (munmap(shared, sizeof *shared) < 0)
48d26c01
IK
1376 return negative_errno();
1377
1378 if (tmp.return_value == 0)
1379 *next = tmp.next;
1380
1381 return tmp.return_value;
1382}