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