]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/calendarspec.c
basic: add log_level argument to timezone_is_valid
[thirdparty/systemd.git] / src / basic / calendarspec.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6 ***/
7
8 #include <alloca.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <limits.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdio_ext.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include <time.h>
19
20 #include "alloc-util.h"
21 #include "calendarspec.h"
22 #include "fileio.h"
23 #include "macro.h"
24 #include "parse-util.h"
25 #include "process-util.h"
26 #include "string-util.h"
27 #include "time-util.h"
28
29 #define BITS_WEEKDAYS 127
30 #define MIN_YEAR 1970
31 #define MAX_YEAR 2199
32
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
39 static 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
49 CalendarSpec* calendar_spec_free(CalendarSpec *c) {
50
51 if (!c)
52 return NULL;
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);
59 free_chain(c->microsecond);
60 free(c->timezone);
61
62 return mfree(c);
63 }
64
65 static int component_compare(const void *_a, const void *_b) {
66 CalendarComponent * const *a = _a, * const *b = _b;
67
68 if ((*a)->start < (*b)->start)
69 return -1;
70 if ((*a)->start > (*b)->start)
71 return 1;
72
73 if ((*a)->stop < (*b)->stop)
74 return -1;
75 if ((*a)->stop > (*b)->stop)
76 return 1;
77
78 if ((*a)->repeat < (*b)->repeat)
79 return -1;
80 if ((*a)->repeat > (*b)->repeat)
81 return 1;
82
83 return 0;
84 }
85
86 static void normalize_chain(CalendarComponent **c) {
87 CalendarComponent **b, *i, **j, *next;
88 size_t n = 0, k;
89
90 assert(c);
91
92 for (i = *c; i; i = i->next) {
93 n++;
94
95 /*
96 * While we're counting the chain, also normalize `stop`
97 * so the length of the range is a multiple of `repeat`
98 */
99 if (i->stop > i->start && i->repeat > 0)
100 i->stop -= (i->stop - i->start) % i->repeat;
101
102 }
103
104 if (n <= 1)
105 return;
106
107 j = b = newa(CalendarComponent*, n);
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--) {
118 if (component_compare(&b[k-1], &next) == 0) {
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
130 static void fix_year(CalendarComponent *c) {
131 /* Turns 12 → 2012, 89 → 1989 */
132
133 while (c) {
134 if (c->start >= 0 && c->start < 70)
135 c->start += 2000;
136
137 if (c->stop >= 0 && c->stop < 70)
138 c->stop += 2000;
139
140 if (c->start >= 70 && c->start < 100)
141 c->start += 1900;
142
143 if (c->stop >= 70 && c->stop < 100)
144 c->stop += 1900;
145
146 c = c->next;
147 }
148 }
149
150 int calendar_spec_normalize(CalendarSpec *c) {
151 assert(c);
152
153 if (streq_ptr(c->timezone, "UTC")) {
154 c->utc = true;
155 c->timezone = mfree(c->timezone);
156 }
157
158 if (c->weekdays_bits <= 0 || c->weekdays_bits >= BITS_WEEKDAYS)
159 c->weekdays_bits = -1;
160
161 if (c->end_of_month && !c->day)
162 c->end_of_month = false;
163
164 fix_year(c->year);
165
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);
172
173 return 0;
174 }
175
176 _pure_ static bool chain_valid(CalendarComponent *c, int from, int to, bool end_of_month) {
177 assert(to >= from);
178
179 if (!c)
180 return true;
181
182 /* Forbid dates more than 28 days from the end of the month */
183 if (end_of_month)
184 to -= 3;
185
186 if (c->start < from || c->start > to)
187 return false;
188
189 /* Avoid overly large values that could cause overflow */
190 if (c->repeat > to - from)
191 return false;
192
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
196 * relative to the end of the month, c->start and c->stop
197 * correspond to the Nth last day of the month.
198 */
199 if (c->stop >= 0) {
200 if (c->stop < from || c ->stop > to)
201 return false;
202
203 if (c->start + c->repeat > c->stop)
204 return false;
205 } else {
206 if (end_of_month && c->start - c->repeat < from)
207 return false;
208
209 if (!end_of_month && c->start + c->repeat > to)
210 return false;
211 }
212
213 if (c->next)
214 return chain_valid(c->next, from, to, end_of_month);
215
216 return true;
217 }
218
219 _pure_ bool calendar_spec_valid(CalendarSpec *c) {
220 assert(c);
221
222 if (c->weekdays_bits > BITS_WEEKDAYS)
223 return false;
224
225 if (!chain_valid(c->year, MIN_YEAR, MAX_YEAR, false))
226 return false;
227
228 if (!chain_valid(c->month, 1, 12, false))
229 return false;
230
231 if (!chain_valid(c->day, 1, 31, c->end_of_month))
232 return false;
233
234 if (!chain_valid(c->hour, 0, 23, false))
235 return false;
236
237 if (!chain_valid(c->minute, 0, 59, false))
238 return false;
239
240 if (!chain_valid(c->microsecond, 0, 60*USEC_PER_SEC-1, false))
241 return false;
242
243 return true;
244 }
245
246 static 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;
258 bool need_comma = false;
259
260 assert(f);
261 assert(c);
262 assert(c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS);
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) {
269 if (need_comma)
270 fputc(',', f);
271 else
272 need_comma = true;
273
274 fputs(days[x], f);
275 l = x;
276 }
277
278 } else if (l >= 0) {
279
280 if (x > l + 1) {
281 fputs(x > l + 2 ? ".." : ",", f);
282 fputs(days[x-1], f);
283 }
284
285 l = -1;
286 }
287 }
288
289 if (l >= 0 && x > l + 1) {
290 fputs(x > l + 2 ? ".." : ",", f);
291 fputs(days[x-1], f);
292 }
293 }
294
295 static void format_chain(FILE *f, int space, const CalendarComponent *c, bool usec) {
296 int d = usec ? (int) USEC_PER_SEC : 1;
297
298 assert(f);
299
300 if (!c) {
301 fputc('*', f);
302 return;
303 }
304
305 if (usec && c->start == 0 && c->repeat == USEC_PER_SEC && !c->next) {
306 fputc('*', f);
307 return;
308 }
309
310 assert(c->start >= 0);
311
312 fprintf(f, "%0*i", space, c->start / d);
313 if (c->start % d > 0)
314 fprintf(f, ".%06i", c->start % d);
315
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);
320
321 if (c->repeat > 0 && !(c->stop > 0 && c->repeat == d))
322 fprintf(f, "/%i", c->repeat / d);
323 if (c->repeat % d > 0)
324 fprintf(f, ".%06i", c->repeat % d);
325
326 if (c->next) {
327 fputc(',', f);
328 format_chain(f, space, c->next, usec);
329 }
330 }
331
332 int calendar_spec_to_string(const CalendarSpec *c, char **p) {
333 char *buf = NULL;
334 size_t sz = 0;
335 FILE *f;
336 int r;
337
338 assert(c);
339 assert(p);
340
341 f = open_memstream(&buf, &sz);
342 if (!f)
343 return -ENOMEM;
344
345 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
346
347 if (c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS) {
348 format_weekdays(f, c);
349 fputc(' ', f);
350 }
351
352 format_chain(f, 4, c->year, false);
353 fputc('-', f);
354 format_chain(f, 2, c->month, false);
355 fputc(c->end_of_month ? '~' : '-', f);
356 format_chain(f, 2, c->day, false);
357 fputc(' ', f);
358 format_chain(f, 2, c->hour, false);
359 fputc(':', f);
360 format_chain(f, 2, c->minute, false);
361 fputc(':', f);
362 format_chain(f, 2, c->microsecond, true);
363
364 if (c->utc)
365 fputs(" UTC", f);
366 else if (c->timezone != NULL) {
367 fputc(' ', f);
368 fputs(c->timezone, f);
369 } else if (IN_SET(c->dst, 0, 1)) {
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])) {
376 fputc(' ', f);
377 fputs(tzname[c->dst], f);
378 }
379 }
380
381 r = fflush_and_check(f);
382 if (r < 0) {
383 free(buf);
384 fclose(f);
385 return r;
386 }
387
388 fclose(f);
389
390 *p = buf;
391 return 0;
392 }
393
394 static 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 (;;) {
423 size_t i;
424
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
433 if (!IN_SET((*p)[skip], 0, '-', '.', ',', ' '))
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
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;
476 *p += 2;
477
478 /* Support ranges with "-" for backwards compatibility */
479 } else if (**p == '-') {
480 if (l >= 0)
481 return -EINVAL;
482
483 l = day_nr[i].nr;
484 *p += 1;
485
486 } else if (**p == ',') {
487 l = -1;
488 *p += 1;
489 }
490
491 /* Allow a trailing comma but not an open range */
492 if (IN_SET(**p, 0, ' ')) {
493 *p += strspn(*p, " ");
494 return l < 0 ? 0 : -EINVAL;
495 }
496
497 first = false;
498 }
499 }
500
501 static 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
517 static int parse_component_decimal(const char **p, bool usec, int *res) {
518 unsigned long value;
519 const char *e = NULL;
520 int r;
521
522 if (!isdigit(**p))
523 return -EINVAL;
524
525 r = parse_one_number(*p, &e, &value);
526 if (r < 0)
527 return r;
528
529 if (usec) {
530 if (value * USEC_PER_SEC / USEC_PER_SEC != value)
531 return -ERANGE;
532
533 value *= USEC_PER_SEC;
534
535 /* One "." is a decimal point, but ".." is a range separator */
536 if (e[0] == '.' && e[1] != '.') {
537 unsigned add;
538
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
550 if (value > INT_MAX)
551 return -ERANGE;
552
553 *p = e;
554 *res = value;
555
556 return 0;
557 }
558
559 static 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
568 cc->start = value;
569 cc->stop = -1;
570 cc->repeat = 0;
571 cc->next = *c;
572
573 *c = cc;
574
575 return 0;
576 }
577
578 static 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
583 if (!gmtime_r(&time, &tm))
584 return -ERANGE;
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
620 static int prepend_component(const char **p, bool usec, unsigned nesting, CalendarComponent **c) {
621 int r, start, stop = -1, repeat = 0;
622 CalendarComponent *cc;
623 const char *e = *p;
624
625 assert(p);
626 assert(c);
627
628 if (nesting > CALENDARSPEC_COMPONENTS_MAX)
629 return -ENOBUFS;
630
631 r = parse_component_decimal(&e, usec, &start);
632 if (r < 0)
633 return r;
634
635 if (e[0] == '.' && e[1] == '.') {
636 e += 2;
637 r = parse_component_decimal(&e, usec, &stop);
638 if (r < 0)
639 return r;
640
641 repeat = usec ? USEC_PER_SEC : 1;
642 }
643
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;
652 }
653
654 if (!IN_SET(*e, 0, ' ', ',', '-', '~', ':'))
655 return -EINVAL;
656
657 cc = new0(CalendarComponent, 1);
658 if (!cc)
659 return -ENOMEM;
660
661 cc->start = start;
662 cc->stop = stop;
663 cc->repeat = repeat;
664 cc->next = *c;
665
666 *p = e;
667 *c = cc;
668
669 if (*e ==',') {
670 *p += 1;
671 return prepend_component(p, usec, nesting + 1, c);
672 }
673
674 return 0;
675 }
676
677 static int parse_chain(const char **p, bool usec, CalendarComponent **c) {
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] == '*') {
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
696 *p = t + 1;
697 return 0;
698 }
699
700 r = prepend_component(&t, usec, 0, &cc);
701 if (r < 0) {
702 free_chain(cc);
703 return r;
704 }
705
706 *p = t;
707 *c = cc;
708 return 0;
709 }
710
711 static 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
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
746 r = parse_chain(&t, false, &first);
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 */
751 if (IN_SET(*t, 0, ':')) {
752 free_chain(first);
753 return 0;
754 }
755
756 if (*t == '~')
757 c->end_of_month = true;
758 else if (*t != '-') {
759 free_chain(first);
760 return -EINVAL;
761 }
762
763 t++;
764 r = parse_chain(&t, false, &second);
765 if (r < 0) {
766 free_chain(first);
767 return r;
768 }
769
770 /* Got two parts, hence it's month and day */
771 if (IN_SET(*t, 0, ' ')) {
772 *p = t + strspn(t, " ");
773 c->month = first;
774 c->day = second;
775 return 0;
776 } else if (c->end_of_month) {
777 free_chain(first);
778 free_chain(second);
779 return -EINVAL;
780 }
781
782 if (*t == '~')
783 c->end_of_month = true;
784 else if (*t != '-') {
785 free_chain(first);
786 free_chain(second);
787 return -EINVAL;
788 }
789
790 t++;
791 r = parse_chain(&t, false, &third);
792 if (r < 0) {
793 free_chain(first);
794 free_chain(second);
795 return r;
796 }
797
798 /* Got three parts, hence it is year, month and day */
799 if (IN_SET(*t, 0, ' ')) {
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
813 static int parse_calendar_time(const char **p, CalendarSpec *c) {
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
824 /* If no time is specified at all, then this means 00:00:00 */
825 if (*t == 0)
826 goto null_hour;
827
828 r = parse_chain(&t, false, &h);
829 if (r < 0)
830 goto fail;
831
832 if (*t != ':') {
833 r = -EINVAL;
834 goto fail;
835 }
836
837 t++;
838 r = parse_chain(&t, false, &m);
839 if (r < 0)
840 goto fail;
841
842 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
843 if (*t == 0)
844 goto null_second;
845
846 if (*t != ':') {
847 r = -EINVAL;
848 goto fail;
849 }
850
851 t++;
852 r = parse_chain(&t, true, &s);
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
863 null_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
872 null_second:
873 r = const_chain(0, &s);
874 if (r < 0)
875 goto fail;
876
877 finish:
878 *p = t;
879 c->hour = h;
880 c->minute = m;
881 c->microsecond = s;
882
883 return 0;
884
885 fail:
886 free_chain(h);
887 free_chain(m);
888 free_chain(s);
889 return r;
890 }
891
892 int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
893 const char *utc;
894 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
895 int r;
896
897 assert(p);
898 assert(spec);
899
900 c = new0(CalendarSpec, 1);
901 if (!c)
902 return -ENOMEM;
903 c->dst = -1;
904 c->timezone = NULL;
905
906 utc = endswith_no_case(p, " UTC");
907 if (utc) {
908 c->utc = true;
909 p = strndupa(p, utc - p);
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]);
922 if (!e)
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;
936 } else {
937 const char *last_space;
938
939 last_space = strrchr(p, ' ');
940 if (last_space != NULL && timezone_is_valid(last_space + 1, LOG_DEBUG)) {
941 c->timezone = strdup(last_space + 1);
942 if (!c->timezone)
943 return -ENOMEM;
944
945 p = strndupa(p, last_space - p);
946 }
947 }
948 }
949
950 if (isempty(p))
951 return -EINVAL;
952
953 if (strcaseeq(p, "minutely")) {
954 r = const_chain(0, &c->microsecond);
955 if (r < 0)
956 return r;
957
958 } else if (strcaseeq(p, "hourly")) {
959 r = const_chain(0, &c->minute);
960 if (r < 0)
961 return r;
962 r = const_chain(0, &c->microsecond);
963 if (r < 0)
964 return r;
965
966 } else if (strcaseeq(p, "daily")) {
967 r = const_chain(0, &c->hour);
968 if (r < 0)
969 return r;
970 r = const_chain(0, &c->minute);
971 if (r < 0)
972 return r;
973 r = const_chain(0, &c->microsecond);
974 if (r < 0)
975 return r;
976
977 } else if (strcaseeq(p, "monthly")) {
978 r = const_chain(1, &c->day);
979 if (r < 0)
980 return r;
981 r = const_chain(0, &c->hour);
982 if (r < 0)
983 return r;
984 r = const_chain(0, &c->minute);
985 if (r < 0)
986 return r;
987 r = const_chain(0, &c->microsecond);
988 if (r < 0)
989 return r;
990
991 } else if (strcaseeq(p, "annually") ||
992 strcaseeq(p, "yearly") ||
993 strcaseeq(p, "anually") /* backwards compatibility */ ) {
994
995 r = const_chain(1, &c->month);
996 if (r < 0)
997 return r;
998 r = const_chain(1, &c->day);
999 if (r < 0)
1000 return r;
1001 r = const_chain(0, &c->hour);
1002 if (r < 0)
1003 return r;
1004 r = const_chain(0, &c->minute);
1005 if (r < 0)
1006 return r;
1007 r = const_chain(0, &c->microsecond);
1008 if (r < 0)
1009 return r;
1010
1011 } else if (strcaseeq(p, "weekly")) {
1012
1013 c->weekdays_bits = 1;
1014
1015 r = const_chain(0, &c->hour);
1016 if (r < 0)
1017 return r;
1018 r = const_chain(0, &c->minute);
1019 if (r < 0)
1020 return r;
1021 r = const_chain(0, &c->microsecond);
1022 if (r < 0)
1023 return r;
1024
1025 } else if (strcaseeq(p, "quarterly")) {
1026
1027 r = const_chain(1, &c->month);
1028 if (r < 0)
1029 return r;
1030 r = const_chain(4, &c->month);
1031 if (r < 0)
1032 return r;
1033 r = const_chain(7, &c->month);
1034 if (r < 0)
1035 return r;
1036 r = const_chain(10, &c->month);
1037 if (r < 0)
1038 return r;
1039 r = const_chain(1, &c->day);
1040 if (r < 0)
1041 return r;
1042 r = const_chain(0, &c->hour);
1043 if (r < 0)
1044 return r;
1045 r = const_chain(0, &c->minute);
1046 if (r < 0)
1047 return r;
1048 r = const_chain(0, &c->microsecond);
1049 if (r < 0)
1050 return r;
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)
1059 return r;
1060 r = const_chain(7, &c->month);
1061 if (r < 0)
1062 return r;
1063 r = const_chain(1, &c->day);
1064 if (r < 0)
1065 return r;
1066 r = const_chain(0, &c->hour);
1067 if (r < 0)
1068 return r;
1069 r = const_chain(0, &c->minute);
1070 if (r < 0)
1071 return r;
1072 r = const_chain(0, &c->microsecond);
1073 if (r < 0)
1074 return r;
1075
1076 } else {
1077 r = parse_weekdays(&p, c);
1078 if (r < 0)
1079 return r;
1080
1081 r = parse_date(&p, c);
1082 if (r < 0)
1083 return r;
1084
1085 if (r == 0) {
1086 r = parse_calendar_time(&p, c);
1087 if (r < 0)
1088 return r;
1089 }
1090
1091 if (*p != 0)
1092 return -EINVAL;
1093 }
1094
1095 r = calendar_spec_normalize(c);
1096 if (r < 0)
1097 return r;
1098
1099 if (!calendar_spec_valid(c))
1100 return -EINVAL;
1101
1102 *spec = TAKE_PTR(c);
1103 return 0;
1104 }
1105
1106 static int find_end_of_month(struct tm *tm, bool utc, int day) {
1107 struct tm t = *tm;
1108
1109 t.tm_mon++;
1110 t.tm_mday = 1 - day;
1111
1112 if (mktime_or_timegm(&t, utc) < 0 ||
1113 t.tm_mon != tm->tm_mon)
1114 return -1;
1115
1116 return t.tm_mday;
1117 }
1118
1119 static int find_matching_component(const CalendarSpec *spec, const CalendarComponent *c,
1120 struct tm *tm, int *val) {
1121 const CalendarComponent *p = c;
1122 int start, stop, d = -1;
1123 bool d_set = false;
1124 int r;
1125
1126 assert(val);
1127
1128 if (!c)
1129 return 0;
1130
1131 while (c) {
1132 start = c->start;
1133 stop = c->stop;
1134
1135 if (spec->end_of_month && p == spec->day) {
1136 start = find_end_of_month(tm, spec->utc, start);
1137 stop = find_end_of_month(tm, spec->utc, stop);
1138
1139 if (stop > 0)
1140 SWAP_TWO(start, stop);
1141 }
1142
1143 if (start >= *val) {
1144
1145 if (!d_set || start < d) {
1146 d = start;
1147 d_set = true;
1148 }
1149
1150 } else if (c->repeat > 0) {
1151 int k;
1152
1153 k = start + c->repeat * DIV_ROUND_UP(*val - start, c->repeat);
1154
1155 if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
1156 d = k;
1157 d_set = true;
1158 }
1159 }
1160
1161 c = c->next;
1162 }
1163
1164 if (!d_set)
1165 return -ENOENT;
1166
1167 r = *val != d;
1168 *val = d;
1169 return r;
1170 }
1171
1172 static bool tm_out_of_bounds(const struct tm *tm, bool utc) {
1173 struct tm t;
1174 assert(tm);
1175
1176 t = *tm;
1177
1178 if (mktime_or_timegm(&t, utc) < 0)
1179 return true;
1180
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
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
1199 static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
1200 struct tm t;
1201 int k;
1202
1203 if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
1204 return true;
1205
1206 t = *tm;
1207 if (mktime_or_timegm(&t, utc) < 0)
1208 return false;
1209
1210 k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
1211 return (weekdays_bits & (1 << k));
1212 }
1213
1214 static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
1215 struct tm c;
1216 int tm_usec;
1217 int r;
1218
1219 assert(spec);
1220 assert(tm);
1221
1222 c = *tm;
1223 tm_usec = *usec;
1224
1225 for (;;) {
1226 /* Normalize the current date */
1227 (void) mktime_or_timegm(&c, spec->utc);
1228 c.tm_isdst = spec->dst;
1229
1230 c.tm_year += 1900;
1231 r = find_matching_component(spec, spec->year, &c, &c.tm_year);
1232 c.tm_year -= 1900;
1233
1234 if (r > 0) {
1235 c.tm_mon = 0;
1236 c.tm_mday = 1;
1237 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
1238 }
1239 if (r < 0)
1240 return r;
1241 if (tm_out_of_bounds(&c, spec->utc))
1242 return -ENOENT;
1243
1244 c.tm_mon += 1;
1245 r = find_matching_component(spec, spec->month, &c, &c.tm_mon);
1246 c.tm_mon -= 1;
1247
1248 if (r > 0) {
1249 c.tm_mday = 1;
1250 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
1251 }
1252 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
1253 c.tm_year++;
1254 c.tm_mon = 0;
1255 c.tm_mday = 1;
1256 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
1257 continue;
1258 }
1259
1260 r = find_matching_component(spec, spec->day, &c, &c.tm_mday);
1261 if (r > 0)
1262 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
1263 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
1264 c.tm_mon++;
1265 c.tm_mday = 1;
1266 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
1267 continue;
1268 }
1269
1270 if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) {
1271 c.tm_mday++;
1272 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
1273 continue;
1274 }
1275
1276 r = find_matching_component(spec, spec->hour, &c, &c.tm_hour);
1277 if (r > 0)
1278 c.tm_min = c.tm_sec = tm_usec = 0;
1279 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
1280 c.tm_mday++;
1281 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
1282 continue;
1283 }
1284
1285 r = find_matching_component(spec, spec->minute, &c, &c.tm_min);
1286 if (r > 0)
1287 c.tm_sec = tm_usec = 0;
1288 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
1289 c.tm_hour++;
1290 c.tm_min = c.tm_sec = tm_usec = 0;
1291 continue;
1292 }
1293
1294 c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec;
1295 r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec);
1296 tm_usec = c.tm_sec % USEC_PER_SEC;
1297 c.tm_sec /= USEC_PER_SEC;
1298
1299 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
1300 c.tm_min++;
1301 c.tm_sec = tm_usec = 0;
1302 continue;
1303 }
1304
1305 *tm = c;
1306 *usec = tm_usec;
1307 return 0;
1308 }
1309 }
1310
1311 static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *next) {
1312 struct tm tm;
1313 time_t t;
1314 int r;
1315 usec_t tm_usec;
1316
1317 assert(spec);
1318 assert(next);
1319
1320 if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1321 return -EINVAL;
1322
1323 usec++;
1324 t = (time_t) (usec / USEC_PER_SEC);
1325 assert_se(localtime_or_gmtime_r(&t, &tm, spec->utc));
1326 tm_usec = usec % USEC_PER_SEC;
1327
1328 r = find_next(spec, &tm, &tm_usec);
1329 if (r < 0)
1330 return r;
1331
1332 t = mktime_or_timegm(&tm, spec->utc);
1333 if (t < 0)
1334 return -EINVAL;
1335
1336 *next = (usec_t) t * USEC_PER_SEC + tm_usec;
1337 return 0;
1338 }
1339
1340 typedef struct SpecNextResult {
1341 usec_t next;
1342 int return_value;
1343 } SpecNextResult;
1344
1345 int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
1346 SpecNextResult *shared, tmp;
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
1356 r = safe_fork("(sd-calendar)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL);
1357 if (r < 0) {
1358 (void) munmap(shared, sizeof *shared);
1359 return r;
1360 }
1361 if (r == 0) {
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
1374 tmp = *shared;
1375 if (munmap(shared, sizeof *shared) < 0)
1376 return negative_errno();
1377
1378 if (tmp.return_value == 0)
1379 *next = tmp.next;
1380
1381 return tmp.return_value;
1382 }