]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/calendarspec.c
Merge pull request #8575 from keszybz/non-absolute-paths
[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
LP
87 unsigned n = 0, k;
88 CalendarComponent **b, *i, **j, *next;
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
107 j = b = alloca(sizeof(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--) {
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 (;;) {
423 unsigned i;
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;
36697dc0
LP
894 CalendarSpec *c;
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
ZJS
939 last_space = strrchr(p, ' ');
940 if (last_space != NULL && timezone_is_valid(last_space + 1)) {
941 c->timezone = strdup(last_space + 1);
942 if (!c->timezone) {
943 r = -ENOMEM;
944 goto fail;
48d26c01 945 }
a2932a0d
ZJS
946
947 p = strndupa(p, last_space - p);
48d26c01 948 }
21b3a0fc 949 }
078efddd 950 }
51ffa239 951
04773cb5
DC
952 if (isempty(p)) {
953 r = -EINVAL;
954 goto fail;
955 }
956
272ac205 957 if (strcaseeq(p, "minutely")) {
436dd70f 958 r = const_chain(0, &c->microsecond);
272ac205
DM
959 if (r < 0)
960 goto fail;
961
962 } else if (strcaseeq(p, "hourly")) {
36697dc0
LP
963 r = const_chain(0, &c->minute);
964 if (r < 0)
965 goto fail;
436dd70f 966 r = const_chain(0, &c->microsecond);
36697dc0
LP
967 if (r < 0)
968 goto fail;
969
b43d1d01 970 } else if (strcaseeq(p, "daily")) {
36697dc0
LP
971 r = const_chain(0, &c->hour);
972 if (r < 0)
973 goto fail;
974 r = const_chain(0, &c->minute);
975 if (r < 0)
976 goto fail;
436dd70f 977 r = const_chain(0, &c->microsecond);
36697dc0
LP
978 if (r < 0)
979 goto fail;
980
b43d1d01 981 } else if (strcaseeq(p, "monthly")) {
36697dc0
LP
982 r = const_chain(1, &c->day);
983 if (r < 0)
984 goto fail;
985 r = const_chain(0, &c->hour);
986 if (r < 0)
987 goto fail;
988 r = const_chain(0, &c->minute);
989 if (r < 0)
990 goto fail;
436dd70f 991 r = const_chain(0, &c->microsecond);
36697dc0
LP
992 if (r < 0)
993 goto fail;
994
dbfd41e2
LP
995 } else if (strcaseeq(p, "annually") ||
996 strcaseeq(p, "yearly") ||
997 strcaseeq(p, "anually") /* backwards compatibility */ ) {
998
13516818
LP
999 r = const_chain(1, &c->month);
1000 if (r < 0)
1001 goto fail;
1002 r = const_chain(1, &c->day);
1003 if (r < 0)
1004 goto fail;
1005 r = const_chain(0, &c->hour);
1006 if (r < 0)
1007 goto fail;
1008 r = const_chain(0, &c->minute);
1009 if (r < 0)
1010 goto fail;
436dd70f 1011 r = const_chain(0, &c->microsecond);
13516818
LP
1012 if (r < 0)
1013 goto fail;
1014
b43d1d01 1015 } else if (strcaseeq(p, "weekly")) {
36697dc0
LP
1016
1017 c->weekdays_bits = 1;
1018
1019 r = const_chain(0, &c->hour);
1020 if (r < 0)
1021 goto fail;
1022 r = const_chain(0, &c->minute);
1023 if (r < 0)
1024 goto fail;
436dd70f 1025 r = const_chain(0, &c->microsecond);
dbfd41e2
LP
1026 if (r < 0)
1027 goto fail;
1028
1029 } else if (strcaseeq(p, "quarterly")) {
1030
1031 r = const_chain(1, &c->month);
1032 if (r < 0)
1033 goto fail;
1034 r = const_chain(4, &c->month);
1035 if (r < 0)
1036 goto fail;
1037 r = const_chain(7, &c->month);
1038 if (r < 0)
1039 goto fail;
1040 r = const_chain(10, &c->month);
1041 if (r < 0)
1042 goto fail;
1043 r = const_chain(1, &c->day);
1044 if (r < 0)
1045 goto fail;
1046 r = const_chain(0, &c->hour);
1047 if (r < 0)
1048 goto fail;
1049 r = const_chain(0, &c->minute);
1050 if (r < 0)
1051 goto fail;
436dd70f 1052 r = const_chain(0, &c->microsecond);
dbfd41e2
LP
1053 if (r < 0)
1054 goto fail;
1055
1056 } else if (strcaseeq(p, "biannually") ||
1057 strcaseeq(p, "bi-annually") ||
1058 strcaseeq(p, "semiannually") ||
1059 strcaseeq(p, "semi-annually")) {
1060
1061 r = const_chain(1, &c->month);
1062 if (r < 0)
1063 goto fail;
1064 r = const_chain(7, &c->month);
1065 if (r < 0)
1066 goto fail;
1067 r = const_chain(1, &c->day);
1068 if (r < 0)
1069 goto fail;
1070 r = const_chain(0, &c->hour);
1071 if (r < 0)
1072 goto fail;
1073 r = const_chain(0, &c->minute);
1074 if (r < 0)
1075 goto fail;
436dd70f 1076 r = const_chain(0, &c->microsecond);
36697dc0
LP
1077 if (r < 0)
1078 goto fail;
1079
1080 } else {
1081 r = parse_weekdays(&p, c);
1082 if (r < 0)
1083 goto fail;
1084
1085 r = parse_date(&p, c);
1086 if (r < 0)
1087 goto fail;
1088
d80e5b74
ZJS
1089 if (r == 0) {
1090 r = parse_calendar_time(&p, c);
1091 if (r < 0)
1092 goto fail;
1093 }
36697dc0
LP
1094
1095 if (*p != 0) {
1096 r = -EINVAL;
1097 goto fail;
1098 }
1099 }
1100
1101 r = calendar_spec_normalize(c);
1102 if (r < 0)
1103 goto fail;
1104
1105 if (!calendar_spec_valid(c)) {
1106 r = -EINVAL;
1107 goto fail;
1108 }
1109
1110 *spec = c;
1111 return 0;
1112
1113fail:
1114 calendar_spec_free(c);
1115 return r;
1116}
1117
60bf5836 1118static int find_end_of_month(struct tm *tm, bool utc, int day) {
a2eb5ea7
DC
1119 struct tm t = *tm;
1120
1121 t.tm_mon++;
1122 t.tm_mday = 1 - day;
1123
c477ff14 1124 if (mktime_or_timegm(&t, utc) < 0 ||
a2eb5ea7
DC
1125 t.tm_mon != tm->tm_mon)
1126 return -1;
1127
1128 return t.tm_mday;
1129}
1130
8ea80351
DC
1131static int find_matching_component(const CalendarSpec *spec, const CalendarComponent *c,
1132 struct tm *tm, int *val) {
482f3b54 1133 const CalendarComponent *p = c;
e78fce48 1134 int start, stop, d = -1;
36697dc0
LP
1135 bool d_set = false;
1136 int r;
1137
1138 assert(val);
1139
1140 if (!c)
1141 return 0;
1142
1143 while (c) {
e78fce48
DC
1144 start = c->start;
1145 stop = c->stop;
a2eb5ea7 1146
8ea80351 1147 if (spec->end_of_month && p == spec->day) {
e78fce48
DC
1148 start = find_end_of_month(tm, spec->utc, start);
1149 stop = find_end_of_month(tm, spec->utc, stop);
a2eb5ea7 1150
e78fce48
DC
1151 if (stop > 0)
1152 SWAP_TWO(start, stop);
a2eb5ea7 1153 }
8ea80351 1154
e78fce48 1155 if (start >= *val) {
36697dc0 1156
e78fce48
DC
1157 if (!d_set || start < d) {
1158 d = start;
36697dc0
LP
1159 d_set = true;
1160 }
1161
1162 } else if (c->repeat > 0) {
1163 int k;
1164
be6b0c21 1165 k = start + c->repeat * DIV_ROUND_UP(*val - start, c->repeat);
36697dc0 1166
e78fce48 1167 if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
36697dc0
LP
1168 d = k;
1169 d_set = true;
1170 }
1171 }
1172
482f3b54 1173 c = c->next;
36697dc0
LP
1174 }
1175
1176 if (!d_set)
1177 return -ENOENT;
1178
1179 r = *val != d;
1180 *val = d;
1181 return r;
1182}
1183
51ffa239 1184static bool tm_out_of_bounds(const struct tm *tm, bool utc) {
36697dc0
LP
1185 struct tm t;
1186 assert(tm);
1187
1188 t = *tm;
1189
c477ff14 1190 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1191 return true;
1192
f6e7d66b
DC
1193 /*
1194 * Set an upper bound on the year so impossible dates like "*-02-31"
1195 * don't cause find_next() to loop forever. tm_year contains years
1196 * since 1900, so adjust it accordingly.
1197 */
1198 if (tm->tm_year + 1900 > MAX_YEAR)
1199 return true;
1200
36697dc0
LP
1201 /* Did any normalization take place? If so, it was out of bounds before */
1202 return
1203 t.tm_year != tm->tm_year ||
1204 t.tm_mon != tm->tm_mon ||
1205 t.tm_mday != tm->tm_mday ||
1206 t.tm_hour != tm->tm_hour ||
1207 t.tm_min != tm->tm_min ||
1208 t.tm_sec != tm->tm_sec;
1209}
1210
51ffa239 1211static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
36697dc0
LP
1212 struct tm t;
1213 int k;
1214
489464d0 1215 if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
1216 return true;
1217
1218 t = *tm;
c477ff14 1219 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1220 return false;
1221
1222 k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
1223 return (weekdays_bits & (1 << k));
1224}
1225
436dd70f 1226static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
36697dc0 1227 struct tm c;
436dd70f 1228 int tm_usec;
36697dc0
LP
1229 int r;
1230
1231 assert(spec);
1232 assert(tm);
1233
1234 c = *tm;
436dd70f 1235 tm_usec = *usec;
36697dc0
LP
1236
1237 for (;;) {
1238 /* Normalize the current date */
ea3894c1 1239 (void) mktime_or_timegm(&c, spec->utc);
21b3a0fc 1240 c.tm_isdst = spec->dst;
36697dc0
LP
1241
1242 c.tm_year += 1900;
8ea80351 1243 r = find_matching_component(spec, spec->year, &c, &c.tm_year);
36697dc0
LP
1244 c.tm_year -= 1900;
1245
1246 if (r > 0) {
1247 c.tm_mon = 0;
1248 c.tm_mday = 1;
436dd70f 1249 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1250 }
e308ddca 1251 if (r < 0)
36697dc0 1252 return r;
e308ddca
LP
1253 if (tm_out_of_bounds(&c, spec->utc))
1254 return -ENOENT;
36697dc0
LP
1255
1256 c.tm_mon += 1;
8ea80351 1257 r = find_matching_component(spec, spec->month, &c, &c.tm_mon);
36697dc0
LP
1258 c.tm_mon -= 1;
1259
1260 if (r > 0) {
1261 c.tm_mday = 1;
436dd70f 1262 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1263 }
51ffa239 1264 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1265 c.tm_year++;
36697dc0
LP
1266 c.tm_mon = 0;
1267 c.tm_mday = 1;
436dd70f 1268 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1269 continue;
1270 }
1271
8ea80351 1272 r = find_matching_component(spec, spec->day, &c, &c.tm_mday);
36697dc0 1273 if (r > 0)
436dd70f 1274 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1275 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1276 c.tm_mon++;
36697dc0 1277 c.tm_mday = 1;
436dd70f 1278 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1279 continue;
1280 }
1281
51ffa239 1282 if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) {
36697dc0 1283 c.tm_mday++;
436dd70f 1284 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1285 continue;
1286 }
1287
8ea80351 1288 r = find_matching_component(spec, spec->hour, &c, &c.tm_hour);
36697dc0 1289 if (r > 0)
a022d76e 1290 c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1291 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1292 c.tm_mday++;
436dd70f 1293 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1294 continue;
1295 }
1296
8ea80351 1297 r = find_matching_component(spec, spec->minute, &c, &c.tm_min);
36697dc0 1298 if (r > 0)
a022d76e 1299 c.tm_sec = tm_usec = 0;
51ffa239 1300 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1301 c.tm_hour++;
436dd70f 1302 c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1303 continue;
1304 }
1305
436dd70f 1306 c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec;
8ea80351 1307 r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec);
436dd70f
HV
1308 tm_usec = c.tm_sec % USEC_PER_SEC;
1309 c.tm_sec /= USEC_PER_SEC;
1310
51ffa239 1311 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1312 c.tm_min++;
436dd70f 1313 c.tm_sec = tm_usec = 0;
36697dc0
LP
1314 continue;
1315 }
1316
36697dc0 1317 *tm = c;
436dd70f 1318 *usec = tm_usec;
36697dc0
LP
1319 return 0;
1320 }
1321}
1322
48d26c01 1323static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *next) {
36697dc0
LP
1324 struct tm tm;
1325 time_t t;
1326 int r;
436dd70f 1327 usec_t tm_usec;
36697dc0
LP
1328
1329 assert(spec);
1330 assert(next);
1331
1bb4b028
LP
1332 if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1333 return -EINVAL;
1334
436dd70f
HV
1335 usec++;
1336 t = (time_t) (usec / USEC_PER_SEC);
51ffa239 1337 assert_se(localtime_or_gmtime_r(&t, &tm, spec->utc));
436dd70f 1338 tm_usec = usec % USEC_PER_SEC;
36697dc0 1339
436dd70f 1340 r = find_next(spec, &tm, &tm_usec);
36697dc0
LP
1341 if (r < 0)
1342 return r;
1343
51ffa239 1344 t = mktime_or_timegm(&tm, spec->utc);
c477ff14 1345 if (t < 0)
36697dc0
LP
1346 return -EINVAL;
1347
436dd70f 1348 *next = (usec_t) t * USEC_PER_SEC + tm_usec;
36697dc0
LP
1349 return 0;
1350}
48d26c01
IK
1351
1352typedef struct SpecNextResult {
1353 usec_t next;
1354 int return_value;
1355} SpecNextResult;
1356
1357int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
4c253ed1 1358 SpecNextResult *shared, tmp;
48d26c01
IK
1359 int r;
1360
1361 if (isempty(spec->timezone))
1362 return calendar_spec_next_usec_impl(spec, usec, next);
1363
1364 shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
1365 if (shared == MAP_FAILED)
1366 return negative_errno();
1367
1f5d1e02 1368 r = safe_fork("(sd-calendar)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL);
4c253ed1 1369 if (r < 0) {
48d26c01 1370 (void) munmap(shared, sizeof *shared);
4c253ed1 1371 return r;
48d26c01 1372 }
4c253ed1 1373 if (r == 0) {
48d26c01
IK
1374 if (setenv("TZ", spec->timezone, 1) != 0) {
1375 shared->return_value = negative_errno();
1376 _exit(EXIT_FAILURE);
1377 }
1378
1379 tzset();
1380
1381 shared->return_value = calendar_spec_next_usec_impl(spec, usec, &shared->next);
1382
1383 _exit(EXIT_SUCCESS);
1384 }
1385
48d26c01 1386 tmp = *shared;
1f5d1e02 1387 if (munmap(shared, sizeof *shared) < 0)
48d26c01
IK
1388 return negative_errno();
1389
1390 if (tmp.return_value == 0)
1391 *next = tmp.next;
1392
1393 return tmp.return_value;
1394}