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