1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "calendarspec.h"
13 #include "errno-util.h"
16 #include "parse-util.h"
17 #include "process-util.h"
18 #include "sort-util.h"
19 #include "string-util.h"
20 #include "time-util.h"
22 #define BITS_WEEKDAYS 127
26 /* An arbitrary limit on the length of the chains of components. We don't want to
27 * build a very long linked list, which would be slow to iterate over and might cause
28 * our stack to overflow. It's unlikely that legitimate uses require more than a few
29 * linked compenents anyway. */
30 #define CALENDARSPEC_COMPONENTS_MAX 240
32 static void chain_free(CalendarComponent
*c
) {
42 DEFINE_TRIVIAL_CLEANUP_FUNC(CalendarComponent
*, chain_free
);
44 CalendarSpec
* calendar_spec_free(CalendarSpec
*c
) {
53 chain_free(c
->minute
);
54 chain_free(c
->microsecond
);
60 static int component_compare(CalendarComponent
* const *a
, CalendarComponent
* const *b
) {
63 r
= CMP((*a
)->start
, (*b
)->start
);
67 r
= CMP((*a
)->stop
, (*b
)->stop
);
71 return CMP((*a
)->repeat
, (*b
)->repeat
);
74 static void normalize_chain(CalendarComponent
**c
) {
75 CalendarComponent
**b
, *i
, **j
, *next
;
80 for (i
= *c
; i
; i
= i
->next
) {
84 * While we're counting the chain, also normalize `stop`
85 * so the length of the range is a multiple of `repeat`
87 if (i
->stop
> i
->start
&& i
->repeat
> 0)
88 i
->stop
-= (i
->stop
- i
->start
) % i
->repeat
;
95 j
= b
= newa(CalendarComponent
*, n
);
96 for (i
= *c
; i
; i
= i
->next
)
99 typesafe_qsort(b
, n
, component_compare
);
104 /* Drop non-unique entries */
105 for (k
= n
-1; k
> 0; k
--) {
106 if (component_compare(&b
[k
-1], &next
) == 0) {
118 static void fix_year(CalendarComponent
*c
) {
119 /* Turns 12 → 2012, 89 → 1989 */
122 if (c
->start
>= 0 && c
->start
< 70)
125 if (c
->stop
>= 0 && c
->stop
< 70)
128 if (c
->start
>= 70 && c
->start
< 100)
131 if (c
->stop
>= 70 && c
->stop
< 100)
138 int calendar_spec_normalize(CalendarSpec
*c
) {
141 if (streq_ptr(c
->timezone
, "UTC")) {
143 c
->timezone
= mfree(c
->timezone
);
146 if (c
->weekdays_bits
<= 0 || c
->weekdays_bits
>= BITS_WEEKDAYS
)
147 c
->weekdays_bits
= -1;
149 if (c
->end_of_month
&& !c
->day
)
150 c
->end_of_month
= false;
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
);
164 _pure_
static bool chain_valid(CalendarComponent
*c
, int from
, int to
, bool end_of_month
) {
170 /* Forbid dates more than 28 days from the end of the month */
174 if (c
->start
< from
|| c
->start
> to
)
177 /* Avoid overly large values that could cause overflow */
178 if (c
->repeat
> to
- from
)
182 * c->repeat must be short enough so at least one repetition may
183 * occur before the end of the interval. For dates scheduled
184 * relative to the end of the month, c->start and c->stop
185 * correspond to the Nth last day of the month.
188 if (c
->stop
< from
|| c
->stop
> to
)
191 if (c
->start
+ c
->repeat
> c
->stop
)
194 if (end_of_month
&& c
->start
- c
->repeat
< from
)
197 if (!end_of_month
&& c
->start
+ c
->repeat
> to
)
202 return chain_valid(c
->next
, from
, to
, end_of_month
);
207 _pure_
bool calendar_spec_valid(CalendarSpec
*c
) {
210 if (c
->weekdays_bits
> BITS_WEEKDAYS
)
213 if (!chain_valid(c
->year
, MIN_YEAR
, MAX_YEAR
, false))
216 if (!chain_valid(c
->month
, 1, 12, false))
219 if (!chain_valid(c
->day
, 1, 31, c
->end_of_month
))
222 if (!chain_valid(c
->hour
, 0, 23, false))
225 if (!chain_valid(c
->minute
, 0, 59, false))
228 if (!chain_valid(c
->microsecond
, 0, 60*USEC_PER_SEC
-1, false))
234 static void format_weekdays(FILE *f
, const CalendarSpec
*c
) {
235 static const char *const days
[] = {
246 bool need_comma
= false;
250 assert(c
->weekdays_bits
> 0 && c
->weekdays_bits
<= BITS_WEEKDAYS
);
252 for (x
= 0, l
= -1; x
< (int) ELEMENTSOF(days
); x
++) {
254 if (c
->weekdays_bits
& (1 << x
)) {
269 fputs(x
> l
+ 2 ? ".." : ",", f
);
277 if (l
>= 0 && x
> l
+ 1) {
278 fputs(x
> l
+ 2 ? ".." : ",", f
);
283 static void format_chain(FILE *f
, int space
, const CalendarComponent
*c
, bool usec
) {
284 int d
= usec
? (int) USEC_PER_SEC
: 1;
293 if (usec
&& c
->start
== 0 && c
->repeat
== USEC_PER_SEC
&& !c
->next
) {
298 assert(c
->start
>= 0);
300 fprintf(f
, "%0*i", space
, c
->start
/ d
);
301 if (c
->start
% d
> 0)
302 fprintf(f
, ".%06i", c
->start
% d
);
305 fprintf(f
, "..%0*i", space
, c
->stop
/ d
);
307 fprintf(f
, ".%06i", c
->stop
% d
);
309 if (c
->repeat
> 0 && !(c
->stop
> 0 && c
->repeat
== d
))
310 fprintf(f
, "/%i", c
->repeat
/ d
);
311 if (c
->repeat
% d
> 0)
312 fprintf(f
, ".%06i", c
->repeat
% d
);
316 format_chain(f
, space
, c
->next
, usec
);
320 int calendar_spec_to_string(const CalendarSpec
*c
, char **p
) {
329 f
= open_memstream_unlocked(&buf
, &sz
);
333 if (c
->weekdays_bits
> 0 && c
->weekdays_bits
<= BITS_WEEKDAYS
) {
334 format_weekdays(f
, c
);
338 format_chain(f
, 4, c
->year
, false);
340 format_chain(f
, 2, c
->month
, false);
341 fputc(c
->end_of_month
? '~' : '-', f
);
342 format_chain(f
, 2, c
->day
, false);
344 format_chain(f
, 2, c
->hour
, false);
346 format_chain(f
, 2, c
->minute
, false);
348 format_chain(f
, 2, c
->microsecond
, true);
352 else if (c
->timezone
!= NULL
) {
354 fputs(c
->timezone
, f
);
355 } else if (IN_SET(c
->dst
, 0, 1)) {
357 /* If daylight saving is explicitly on or off, let's show the used timezone. */
361 if (!isempty(tzname
[c
->dst
])) {
363 fputs(tzname
[c
->dst
], f
);
367 r
= fflush_and_check(f
);
380 static int parse_weekdays(const char **p
, CalendarSpec
*c
) {
381 static const struct {
411 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
414 if (!startswith_no_case(*p
, day_nr
[i
].name
))
417 skip
= strlen(day_nr
[i
].name
);
419 if (!IN_SET((*p
)[skip
], 0, '-', '.', ',', ' '))
422 c
->weekdays_bits
|= 1 << day_nr
[i
].nr
;
427 if (l
> day_nr
[i
].nr
)
430 for (j
= l
+ 1; j
< day_nr
[i
].nr
; j
++)
431 c
->weekdays_bits
|= 1 << j
;
438 /* Couldn't find this prefix, so let's assume the
439 weekday was not specified and let's continue with
441 if (i
>= ELEMENTSOF(day_nr
))
442 return first
? 0 : -EINVAL
;
444 /* We reached the end of the string */
448 /* We reached the end of the weekday spec part */
450 *p
+= strspn(*p
, " ");
464 /* Support ranges with "-" for backwards compatibility */
465 } else if (**p
== '-') {
472 } else if (**p
== ',') {
477 /* Allow a trailing comma but not an open range */
478 if (IN_SET(**p
, 0, ' ')) {
479 *p
+= strspn(*p
, " ");
480 return l
< 0 ? 0 : -EINVAL
;
487 static int parse_one_number(const char *p
, const char **e
, unsigned long *ret
) {
492 value
= strtoul(p
, &ee
, 10);
503 static int parse_component_decimal(const char **p
, bool usec
, int *res
) {
505 const char *e
= NULL
;
511 r
= parse_one_number(*p
, &e
, &value
);
516 if (value
* USEC_PER_SEC
/ USEC_PER_SEC
!= value
)
519 value
*= USEC_PER_SEC
;
521 /* One "." is a decimal point, but ".." is a range separator */
522 if (e
[0] == '.' && e
[1] != '.') {
526 r
= parse_fractional_part_u(&e
, 6, &add
);
530 if (add
+ value
< value
)
545 static int const_chain(int value
, CalendarComponent
**c
) {
546 CalendarComponent
*cc
= NULL
;
550 cc
= new(CalendarComponent
, 1);
554 *cc
= (CalendarComponent
) {
566 static int calendarspec_from_time_t(CalendarSpec
*c
, time_t time
) {
567 _cleanup_(chain_freep
) CalendarComponent
568 *year
= NULL
, *month
= NULL
, *day
= NULL
,
569 *hour
= NULL
, *minute
= NULL
, *us
= NULL
;
573 if (!gmtime_r(&time
, &tm
))
576 if (tm
.tm_year
> INT_MAX
- 1900)
579 r
= const_chain(tm
.tm_year
+ 1900, &year
);
583 r
= const_chain(tm
.tm_mon
+ 1, &month
);
587 r
= const_chain(tm
.tm_mday
, &day
);
591 r
= const_chain(tm
.tm_hour
, &hour
);
595 r
= const_chain(tm
.tm_min
, &minute
);
599 r
= const_chain(tm
.tm_sec
* USEC_PER_SEC
, &us
);
604 c
->year
= TAKE_PTR(year
);
605 c
->month
= TAKE_PTR(month
);
606 c
->day
= TAKE_PTR(day
);
607 c
->hour
= TAKE_PTR(hour
);
608 c
->minute
= TAKE_PTR(minute
);
609 c
->microsecond
= TAKE_PTR(us
);
613 static int prepend_component(const char **p
, bool usec
, unsigned nesting
, CalendarComponent
**c
) {
614 int r
, start
, stop
= -1, repeat
= 0;
615 CalendarComponent
*cc
;
621 if (nesting
> CALENDARSPEC_COMPONENTS_MAX
)
624 r
= parse_component_decimal(&e
, usec
, &start
);
628 if (e
[0] == '.' && e
[1] == '.') {
630 r
= parse_component_decimal(&e
, usec
, &stop
);
634 repeat
= usec
? USEC_PER_SEC
: 1;
639 r
= parse_component_decimal(&e
, usec
, &repeat
);
647 if (!IN_SET(*e
, 0, ' ', ',', '-', '~', ':'))
650 cc
= new(CalendarComponent
, 1);
654 *cc
= (CalendarComponent
) {
666 return prepend_component(p
, usec
, nesting
+ 1, c
);
672 static int parse_chain(const char **p
, bool usec
, CalendarComponent
**c
) {
673 _cleanup_(chain_freep
) CalendarComponent
*cc
= NULL
;
684 r
= const_chain(0, c
);
687 (*c
)->repeat
= USEC_PER_SEC
;
695 r
= prepend_component(&t
, usec
, 0, &cc
);
704 static int parse_date(const char **p
, CalendarSpec
*c
) {
705 _cleanup_(chain_freep
) CalendarComponent
*first
= NULL
, *second
= NULL
, *third
= NULL
;
718 /* @TIMESTAMP — UNIX time in seconds since the epoch */
723 r
= parse_one_number(t
+ 1, &t
, &value
);
728 if ((unsigned long) time
!= value
)
731 r
= calendarspec_from_time_t(c
, time
);
736 return 1; /* finito, don't parse H:M:S after that */
739 r
= parse_chain(&t
, false, &first
);
743 /* Already the end? A ':' as separator? In that case this was a time, not a date */
744 if (IN_SET(*t
, 0, ':'))
748 c
->end_of_month
= true;
753 r
= parse_chain(&t
, false, &second
);
757 /* Got two parts, hence it's month and day */
758 if (IN_SET(*t
, 0, ' ')) {
759 *p
= t
+ strspn(t
, " ");
760 c
->month
= TAKE_PTR(first
);
761 c
->day
= TAKE_PTR(second
);
763 } else if (c
->end_of_month
)
767 c
->end_of_month
= true;
772 r
= parse_chain(&t
, false, &third
);
776 if (!IN_SET(*t
, 0, ' '))
779 /* Got three parts, hence it is year, month and day */
780 *p
= t
+ strspn(t
, " ");
781 c
->year
= TAKE_PTR(first
);
782 c
->month
= TAKE_PTR(second
);
783 c
->day
= TAKE_PTR(third
);
787 static int parse_calendar_time(const char **p
, CalendarSpec
*c
) {
788 _cleanup_(chain_freep
) CalendarComponent
*h
= NULL
, *m
= NULL
, *s
= NULL
;
798 /* If no time is specified at all, then this means 00:00:00 */
802 r
= parse_chain(&t
, false, &h
);
810 r
= parse_chain(&t
, false, &m
);
814 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
822 r
= parse_chain(&t
, true, &s
);
826 /* At the end? Then it's hours, minutes and seconds */
833 r
= const_chain(0, &h
);
837 r
= const_chain(0, &m
);
842 r
= const_chain(0, &s
);
848 c
->hour
= TAKE_PTR(h
);
849 c
->minute
= TAKE_PTR(m
);
850 c
->microsecond
= TAKE_PTR(s
);
855 int calendar_spec_from_string(const char *p
, CalendarSpec
**spec
) {
857 _cleanup_(calendar_spec_freep
) CalendarSpec
*c
= NULL
;
858 _cleanup_free_
char *p_tmp
= NULL
;
863 c
= new(CalendarSpec
, 1);
867 *c
= (CalendarSpec
) {
872 utc
= endswith_no_case(p
, " UTC");
875 p
= p_tmp
= strndup(p
, utc
- p
);
879 const char *e
= NULL
;
884 /* Check if the local timezone was specified? */
885 for (j
= 0; j
<= 1; j
++) {
886 if (isempty(tzname
[j
]))
889 e
= endswith_no_case(p
, tzname
[j
]);
900 /* Found one of the two timezones specified? */
901 if (IN_SET(j
, 0, 1)) {
902 p
= p_tmp
= strndup(p
, e
- p
- 1);
908 const char *last_space
;
910 last_space
= strrchr(p
, ' ');
911 if (last_space
!= NULL
&& timezone_is_valid(last_space
+ 1, LOG_DEBUG
)) {
912 c
->timezone
= strdup(last_space
+ 1);
916 p
= p_tmp
= strndup(p
, last_space
- p
);
926 if (strcaseeq(p
, "minutely")) {
927 r
= const_chain(0, &c
->microsecond
);
931 } else if (strcaseeq(p
, "hourly")) {
932 r
= const_chain(0, &c
->minute
);
935 r
= const_chain(0, &c
->microsecond
);
939 } else if (strcaseeq(p
, "daily")) {
940 r
= const_chain(0, &c
->hour
);
943 r
= const_chain(0, &c
->minute
);
946 r
= const_chain(0, &c
->microsecond
);
950 } else if (strcaseeq(p
, "monthly")) {
951 r
= const_chain(1, &c
->day
);
954 r
= const_chain(0, &c
->hour
);
957 r
= const_chain(0, &c
->minute
);
960 r
= const_chain(0, &c
->microsecond
);
964 } else if (strcaseeq(p
, "annually") ||
965 strcaseeq(p
, "yearly") ||
966 strcaseeq(p
, "anually") /* backwards compatibility */ ) {
968 r
= const_chain(1, &c
->month
);
971 r
= const_chain(1, &c
->day
);
974 r
= const_chain(0, &c
->hour
);
977 r
= const_chain(0, &c
->minute
);
980 r
= const_chain(0, &c
->microsecond
);
984 } else if (strcaseeq(p
, "weekly")) {
986 c
->weekdays_bits
= 1;
988 r
= const_chain(0, &c
->hour
);
991 r
= const_chain(0, &c
->minute
);
994 r
= const_chain(0, &c
->microsecond
);
998 } else if (strcaseeq(p
, "quarterly")) {
1000 r
= const_chain(1, &c
->month
);
1003 r
= const_chain(4, &c
->month
);
1006 r
= const_chain(7, &c
->month
);
1009 r
= const_chain(10, &c
->month
);
1012 r
= const_chain(1, &c
->day
);
1015 r
= const_chain(0, &c
->hour
);
1018 r
= const_chain(0, &c
->minute
);
1021 r
= const_chain(0, &c
->microsecond
);
1025 } else if (strcaseeq(p
, "biannually") ||
1026 strcaseeq(p
, "bi-annually") ||
1027 strcaseeq(p
, "semiannually") ||
1028 strcaseeq(p
, "semi-annually")) {
1030 r
= const_chain(1, &c
->month
);
1033 r
= const_chain(7, &c
->month
);
1036 r
= const_chain(1, &c
->day
);
1039 r
= const_chain(0, &c
->hour
);
1042 r
= const_chain(0, &c
->minute
);
1045 r
= const_chain(0, &c
->microsecond
);
1050 r
= parse_weekdays(&p
, c
);
1054 r
= parse_date(&p
, c
);
1059 r
= parse_calendar_time(&p
, c
);
1068 r
= calendar_spec_normalize(c
);
1072 if (!calendar_spec_valid(c
))
1076 *spec
= TAKE_PTR(c
);
1080 static int find_end_of_month(struct tm
*tm
, bool utc
, int day
) {
1084 t
.tm_mday
= 1 - day
;
1086 if (mktime_or_timegm(&t
, utc
) < 0 ||
1087 t
.tm_mon
!= tm
->tm_mon
)
1093 static int find_matching_component(const CalendarSpec
*spec
, const CalendarComponent
*c
,
1094 struct tm
*tm
, int *val
) {
1095 const CalendarComponent
*p
= c
;
1096 int start
, stop
, d
= -1;
1109 if (spec
->end_of_month
&& p
== spec
->day
) {
1110 start
= find_end_of_month(tm
, spec
->utc
, start
);
1111 stop
= find_end_of_month(tm
, spec
->utc
, stop
);
1114 SWAP_TWO(start
, stop
);
1117 if (start
>= *val
) {
1119 if (!d_set
|| start
< d
) {
1124 } else if (c
->repeat
> 0) {
1127 k
= start
+ c
->repeat
* DIV_ROUND_UP(*val
- start
, c
->repeat
);
1129 if ((!d_set
|| k
< d
) && (stop
< 0 || k
<= stop
)) {
1146 static int tm_within_bounds(struct tm
*tm
, bool utc
) {
1151 * Set an upper bound on the year so impossible dates like "*-02-31"
1152 * don't cause find_next() to loop forever. tm_year contains years
1153 * since 1900, so adjust it accordingly.
1155 if (tm
->tm_year
+ 1900 > MAX_YEAR
)
1159 if (mktime_or_timegm(&t
, utc
) < 0)
1160 return negative_errno();
1162 /* Did any normalization take place? If so, it was out of bounds before */
1163 bool good
= t
.tm_year
== tm
->tm_year
&&
1164 t
.tm_mon
== tm
->tm_mon
&&
1165 t
.tm_mday
== tm
->tm_mday
&&
1166 t
.tm_hour
== tm
->tm_hour
&&
1167 t
.tm_min
== tm
->tm_min
&&
1168 t
.tm_sec
== tm
->tm_sec
;
1174 static bool matches_weekday(int weekdays_bits
, const struct tm
*tm
, bool utc
) {
1178 if (weekdays_bits
< 0 || weekdays_bits
>= BITS_WEEKDAYS
)
1182 if (mktime_or_timegm(&t
, utc
) < 0)
1185 k
= t
.tm_wday
== 0 ? 6 : t
.tm_wday
- 1;
1186 return (weekdays_bits
& (1 << k
));
1189 static int find_next(const CalendarSpec
*spec
, struct tm
*tm
, usec_t
*usec
) {
1194 /* Returns -ENOENT if the expression is not going to elapse anymore */
1203 /* Normalize the current date */
1204 (void) mktime_or_timegm(&c
, spec
->utc
);
1205 c
.tm_isdst
= spec
->dst
;
1208 r
= find_matching_component(spec
, spec
->year
, &c
, &c
.tm_year
);
1214 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1218 if (tm_within_bounds(&c
, spec
->utc
) <= 0)
1222 r
= find_matching_component(spec
, spec
->month
, &c
, &c
.tm_mon
);
1227 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1229 if (r
< 0 || (r
= tm_within_bounds(&c
, spec
->utc
)) < 0) {
1233 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1239 r
= find_matching_component(spec
, spec
->day
, &c
, &c
.tm_mday
);
1241 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1242 if (r
< 0 || (r
= tm_within_bounds(&c
, spec
->utc
)) < 0) {
1245 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1251 if (!matches_weekday(spec
->weekdays_bits
, &c
, spec
->utc
)) {
1253 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1257 r
= find_matching_component(spec
, spec
->hour
, &c
, &c
.tm_hour
);
1259 c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1260 if (r
< 0 || (r
= tm_within_bounds(&c
, spec
->utc
)) < 0) {
1262 c
.tm_hour
= c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1266 /* The next hour we set might be missing if there
1267 * are time zone changes. Let's try again starting at
1268 * normalized time. */
1271 r
= find_matching_component(spec
, spec
->minute
, &c
, &c
.tm_min
);
1273 c
.tm_sec
= tm_usec
= 0;
1274 if (r
< 0 || (r
= tm_within_bounds(&c
, spec
->utc
)) < 0) {
1276 c
.tm_min
= c
.tm_sec
= tm_usec
= 0;
1282 c
.tm_sec
= c
.tm_sec
* USEC_PER_SEC
+ tm_usec
;
1283 r
= find_matching_component(spec
, spec
->microsecond
, &c
, &c
.tm_sec
);
1284 tm_usec
= c
.tm_sec
% USEC_PER_SEC
;
1285 c
.tm_sec
/= USEC_PER_SEC
;
1287 if (r
< 0 || (r
= tm_within_bounds(&c
, spec
->utc
)) < 0) {
1289 c
.tm_sec
= tm_usec
= 0;
1301 static int calendar_spec_next_usec_impl(const CalendarSpec
*spec
, usec_t usec
, usec_t
*ret_next
) {
1309 if (usec
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
1313 t
= (time_t) (usec
/ USEC_PER_SEC
);
1314 assert_se(localtime_or_gmtime_r(&t
, &tm
, spec
->utc
));
1315 tm_usec
= usec
% USEC_PER_SEC
;
1317 r
= find_next(spec
, &tm
, &tm_usec
);
1321 t
= mktime_or_timegm(&tm
, spec
->utc
);
1326 *ret_next
= (usec_t
) t
* USEC_PER_SEC
+ tm_usec
;
1331 typedef struct SpecNextResult
{
1336 int calendar_spec_next_usec(const CalendarSpec
*spec
, usec_t usec
, usec_t
*ret_next
) {
1337 SpecNextResult
*shared
, tmp
;
1342 if (isempty(spec
->timezone
))
1343 return calendar_spec_next_usec_impl(spec
, usec
, ret_next
);
1345 shared
= mmap(NULL
, sizeof *shared
, PROT_READ
|PROT_WRITE
, MAP_SHARED
|MAP_ANONYMOUS
, -1, 0);
1346 if (shared
== MAP_FAILED
)
1347 return negative_errno();
1349 r
= safe_fork("(sd-calendar)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_WAIT
, NULL
);
1351 (void) munmap(shared
, sizeof *shared
);
1355 if (setenv("TZ", spec
->timezone
, 1) != 0) {
1356 shared
->return_value
= negative_errno();
1357 _exit(EXIT_FAILURE
);
1362 shared
->return_value
= calendar_spec_next_usec_impl(spec
, usec
, &shared
->next
);
1364 _exit(EXIT_SUCCESS
);
1368 if (munmap(shared
, sizeof *shared
) < 0)
1369 return negative_errno();
1371 if (tmp
.return_value
== 0 && ret_next
)
1372 *ret_next
= tmp
.next
;
1374 return tmp
.return_value
;