HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/calendarspec.c

45,313 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 6a56438c1c7ba619a54b7cb8918918812082e13d

Lines 1001–1513 of 1,513

        } else if (strcaseeq(p, "weekly")) {
 
                c->weekdays_bits = 1;
 
                r = const_chain(0, &c->hour);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->minute);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->microsecond);
                if (r < 0)
                        return r;
 
        } else if (strcaseeq(p, "quarterly")) {
 
                r = const_chain(1, &c->month);
                if (r < 0)
                        return r;
                r = const_chain(4, &c->month);
                if (r < 0)
                        return r;
                r = const_chain(7, &c->month);
                if (r < 0)
                        return r;
                r = const_chain(10, &c->month);
                if (r < 0)
                        return r;
                r = const_chain(1, &c->day);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->hour);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->minute);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->microsecond);
                if (r < 0)
                        return r;
 
        } else if (STRCASE_IN_SET(p,
                                  "biannually",
                                  "bi-annually",
                                  "semiannually",
                                  "semi-annually")) {
 
                r = const_chain(1, &c->month);
                if (r < 0)
                        return r;
                r = const_chain(7, &c->month);
                if (r < 0)
                        return r;
                r = const_chain(1, &c->day);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->hour);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->minute);
                if (r < 0)
                        return r;
                r = const_chain(0, &c->microsecond);
                if (r < 0)
                        return r;
 
        } else {
                r = parse_weekdays(&p, c);
                if (r < 0)
                        return r;
 
                r = parse_date(&p, c);
                if (r < 0)
                        return r;
 
                if (r == 0) {
                        r = parse_calendar_time(&p, c);
                        if (r < 0)
                                return r;
                }
 
                if (*p != 0)
                        return -EINVAL;
        }
 
        calendar_spec_normalize(c);
 
        if (!calendar_spec_valid(c))
                return -EINVAL;
 
        if (warn_on_weekday_mismatch) {
                int wday;
                if (calendar_spec_weekday_conflicts(c, &wday))
                        log_warning("Weekday constraint does not match the fixed date %04d-%02d-%02d "
                                    "(which is a %s), so this timer will never elapse.",
                                    c->year->start, c->month->start, c->day->start,
                                    weekday_to_string(wday));
        }
 
        if (ret)
                *ret = TAKE_PTR(c);
        return 0;
}
 
static int find_end_of_month(const struct tm *tm, bool utc, int day) {
        struct tm t = *ASSERT_PTR(tm);
 
        t.tm_mon++;
        t.tm_mday = 1 - day;
 
        if (mktime_or_timegm_usec(&t, utc, /* ret= */ NULL) < 0 ||
            t.tm_mon != tm->tm_mon)
                return -1;
 
        return t.tm_mday;
}
 
static int find_matching_component(
                const CalendarSpec *spec,
                const CalendarComponent *c,
                const struct tm *tm,           /* tm is only used for end-of-month calculations */
                int *val) {
 
        int d = -1, r;
        bool d_set = false;
 
        assert(val);
 
        /* Finds the *earliest* matching time specified by one of the CalendarComponent items in chain c.
         * If no matches can be found, returns -ENOENT.
         * Otherwise, updates *val to the matching time. 1 is returned if *val was changed, 0 otherwise.
         */
 
        if (!c)
                return 0;
 
        bool end_of_month = spec->end_of_month && c == spec->day;
 
        while (c) {
                int start, stop;
 
                if (end_of_month) {
                        start = find_end_of_month(tm, spec->utc, c->start);
                        stop = find_end_of_month(tm, spec->utc, c->stop);
 
                        if (stop > 0)
                                SWAP_TWO(start, stop);
                } else {
                        start = c->start;
                        stop = c->stop;
                }
 
                if (start >= *val) {
 
                        if (!d_set || start < d) {
                                d = start;
                                d_set = true;
                        }
 
                } else if (c->repeat > 0) {
                        int k;
 
                        if (ADD_SAFE(&k, start, ROUND_UP(*val - start, c->repeat)) &&
                            (!d_set || k < d) && (stop < 0 || k <= stop)) {
                                d = k;
                                d_set = true;
                        }
                }
 
                c = c->next;
        }
 
        if (!d_set)
                return -ENOENT;
 
        r = *val != d;
        *val = d;
        return r;
}
 
static int tm_within_bounds(struct tm *tm, bool utc) {
        int r;
 
        assert(tm);
 
        /*
         * Set an upper bound on the year so impossible dates like "*-02-31"
         * don't cause find_next() to loop forever. tm_year contains years
         * since 1900, so adjust it accordingly.
         */
        if (tm->tm_year + 1900 > MAX_YEAR)
                return -ERANGE;
 
        struct tm t = *tm;
        r = mktime_or_timegm_usec(&t, utc, /* ret= */ NULL);
        if (r < 0)
                return r;
 
        /*
         * Did any normalization take place? If so, it was out of bounds before.
         * Normalization could skip next elapse, e.g. result of normalizing 3-33
         * is 4-2. This skips 4-1. So reset the sub time unit if upper unit was
         * out of bounds. Normalization has occurred implies find_matching_component() > 0,
         * other sub time units are already reset in find_next().
         */
        int cmp;
        if ((cmp = CMP(t.tm_year, tm->tm_year)) != 0) {
                t.tm_mon = 0;
                t.tm_mday = 1;
        } else if ((cmp = CMP(t.tm_mon, tm->tm_mon)) != 0)
                t.tm_mday = 1;
        else if ((cmp = CMP(t.tm_mday, tm->tm_mday)) != 0)
                t.tm_hour = 0;
        else if ((cmp = CMP(t.tm_hour, tm->tm_hour)) != 0)
                t.tm_min = 0;
        else if ((cmp = CMP(t.tm_min, tm->tm_min)) != 0)
                t.tm_sec = 0;
        else
                cmp = CMP(t.tm_sec, tm->tm_sec);
 
        if (cmp < 0)
                return -EDEADLK; /* Refuse to go backward */
        if (cmp > 0)
                *tm = t;
        return cmp == 0;
}
 
static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
        struct tm t;
        usec_t usec;
        int k;
 
        assert(tm);
 
        if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
                return true;
 
        t = *tm;
        if (mktime_or_timegm_usec(&t, utc, &usec) < 0)
                return false;
        if (localtime_or_gmtime_usec(usec, utc, &t) < 0)
                return false;
 
        k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
        return (weekdays_bits & (1 << k));
}
 
static bool component_is_single_fixed(const CalendarComponent *c) {
        /* Returns true if the component is a single fixed value: no range, no repeat, no alternatives. */
        return c && !c->next && c->repeat == 0 && (c->stop < 0 || c->stop == c->start);
}
 
bool calendar_spec_weekday_conflicts(const CalendarSpec *spec, int *ret_actual_wday) {
        assert(spec);
 
        if (spec->weekdays_bits <= 0 || spec->weekdays_bits >= BITS_WEEKDAYS)
                return false;
 
        /* Skip when end_of_month (~) is used: day->start holds an offset, not an
         * absolute day-of-month, so the real firing date cannot be determined here. */
        if (spec->end_of_month)
                return false;
 
        /* Only detectable when year, month and day are each a single fixed value */
        if (!component_is_single_fixed(spec->year) ||
            !component_is_single_fixed(spec->month) ||
            !component_is_single_fixed(spec->day))
                return false;
 
        /* CalendarSpec stores month as 1-12; struct tm uses 0-11 */
        struct tm tm = {
                .tm_year = spec->year->start - 1900,
                .tm_mon  = spec->month->start - 1,
                .tm_mday = spec->day->start,
        };
        struct tm orig = tm;
 
        /* Compute weekday in UTC to avoid TZ/DST skew; reject dates that would be
         * silently normalised (e.g. 2027-02-31 → 2027-03-03).
         *
         * Do not rely on timegm() to fill tm_wday, since musl does not do that. */
        usec_t usec;
        if (mktime_or_timegm_usec(&tm, /* utc= */ true, &usec) < 0 ||
            localtime_or_gmtime_usec(usec, /* utc= */ true, &tm) < 0 ||
            tm.tm_year != orig.tm_year || tm.tm_mon != orig.tm_mon ||
            tm.tm_mday != orig.tm_mday)
                return false;
 
        if (matches_weekday(spec->weekdays_bits, &tm, /* utc= */ true))
                return false; /* no conflict */
 
        if (ret_actual_wday)
                *ret_actual_wday = tm.tm_wday == 0 ? 6 : tm.tm_wday - 1;
        return true;
}
 
static int tm_compare(const struct tm *t1, const struct tm *t2) {
        int r;
 
        assert(t1);
        assert(t2);
 
        r = CMP(t1->tm_year, t2->tm_year);
        if (r != 0)
                return r;
 
        r = CMP(t1->tm_mon, t2->tm_mon);
        if (r != 0)
                return r;
 
        r = CMP(t1->tm_mday, t2->tm_mday);
        if (r != 0)
                return r;
 
        r = CMP(t1->tm_hour, t2->tm_hour);
        if (r != 0)
                return r;
 
        r = CMP(t1->tm_min, t2->tm_min);
        if (r != 0)
                return r;
 
        return CMP(t1->tm_sec, t2->tm_sec);
}
 
/* A safety valve: if we get stuck in the calculation, return an error.
 * C.f. https://bugzilla.redhat.com/show_bug.cgi?id=1941335. */
#define MAX_CALENDAR_ITERATIONS 1000
 
static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
        struct tm c;
        int tm_usec, r;
        bool invalidate_dst = false;
 
        /* Returns -ENOENT if the expression is not going to elapse anymore */
 
        assert(spec);
        assert(tm);
        assert(usec);
 
        c = *tm;
        tm_usec = *usec;
 
        for (unsigned iteration = 0; iteration < MAX_CALENDAR_ITERATIONS; iteration++) {
                /* Normalize the current date */
                (void) mktime_or_timegm_usec(&c, spec->utc, /* ret= */ NULL);
                if (!invalidate_dst)
                        c.tm_isdst = spec->dst;
 
                c.tm_year += 1900;
                r = find_matching_component(spec, spec->year, &c, &c.tm_year);
                c.tm_year -= 1900;
 
                if (r > 0) {
                        c.tm_mon = 0;
                        c.tm_mday = 1;
                        c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
                }
                if (r < 0)
                        return r;
                if (tm_within_bounds(&c, spec->utc) <= 0)
                        return -ENOENT;
 
                c.tm_mon += 1;
                r = find_matching_component(spec, spec->month, &c, &c.tm_mon);
                c.tm_mon -= 1;
 
                if (r > 0) {
                        c.tm_mday = 1;
                        c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
                }
                if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) {
                        c.tm_year++;
                        c.tm_mon = 0;
                        c.tm_mday = 1;
                        c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
                        continue;
                }
                if (r == 0)
                        continue;
 
                r = find_matching_component(spec, spec->day, &c, &c.tm_mday);
                if (r > 0)
                        c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
                if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) {
                        c.tm_mon++;
                        c.tm_mday = 1;
                        c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
                        continue;
                }
                if (r == 0)
                        continue;
 
                if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) {
                        c.tm_mday++;
                        c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
                        continue;
                }
 
                r = find_matching_component(spec, spec->hour, &c, &c.tm_hour);
                if (r > 0)
                        c.tm_min = c.tm_sec = tm_usec = 0;
                if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) {
                        c.tm_mday++;
                        c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
                        continue;
                }
                if (r == 0)
                        /* The next hour we set might be missing if there
                         * are time zone changes. Let's try again starting at
                         * normalized time. */
                        continue;
 
                r = find_matching_component(spec, spec->minute, &c, &c.tm_min);
                if (r > 0)
                        c.tm_sec = tm_usec = 0;
                if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) {
                        c.tm_hour++;
                        c.tm_min = c.tm_sec = tm_usec = 0;
                        continue;
                }
                if (r == 0)
                        continue;
 
                c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec;
                r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec);
                tm_usec = c.tm_sec % USEC_PER_SEC;
                c.tm_sec /= USEC_PER_SEC;
 
                if (r < 0 || (r = tm_within_bounds(&c, spec->utc)) < 0) {
                        c.tm_min++;
                        c.tm_sec = tm_usec = 0;
                        continue;
                }
                if (r == 0)
                        continue;
 
                r = tm_compare(tm, &c);
                if (r == 0) {
                        assert(tm_usec + 1 <= 1000000);
                        r = CMP(*usec, (usec_t) tm_usec + 1);
                }
                if (r >= 0) {
                        /* We're stuck - advance, let mktime determine DST transition and try again. */
                        invalidate_dst = true;
                        c.tm_hour++;
                        continue;
                }
 
                *tm = c;
                *usec = tm_usec;
                return 0;
        }
 
        /* It seems we entered an infinite loop. Let's gracefully return an error instead of hanging or
         * aborting. This code is also exercised when timers.target is brought up during early boot, so
         * aborting here is problematic and hard to diagnose for users. */
        _cleanup_free_ char *s = NULL;
        (void) calendar_spec_to_string(spec, &s);
        return log_warning_errno(SYNTHETIC_ERRNO(EDEADLK),
                                 "Infinite loop in calendar calculation: %s", strna(s));
}
 
static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *ret_next) {
        usec_t tm_usec;
        struct tm tm;
        int r;
 
        assert(spec);
 
        if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
                return -EINVAL;
 
        usec++;
        r = localtime_or_gmtime_usec(usec, spec->utc, &tm);
        if (r < 0)
                return r;
        tm_usec = usec % USEC_PER_SEC;
 
        r = find_next(spec, &tm, &tm_usec);
        if (r < 0)
                return r;
 
        usec_t t;
        r = mktime_or_timegm_usec(&tm, spec->utc, &t);
        if (r < 0)
                return r;
 
        if (ret_next)
                *ret_next = t + tm_usec;
 
        return 0;
}
 
int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *ret_next) {
        int r;
 
        assert(spec);
 
        if (isempty(spec->timezone))
                return calendar_spec_next_usec_impl(spec, usec, ret_next);
 
        SAVE_TIMEZONE;
 
        r = RET_NERRNO(setenv("TZ", spec->timezone, /* overwrite= */ true));
        if (r < 0)
                return r;
 
        tzset();
 
        return calendar_spec_next_usec_impl(spec, usec, ret_next);
}