Merge pull request #12394 from poettering/oncalendar-tweak

systemd-run --on-calendar= follow-up
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-04-26 08:48:28 +02:00 committed by GitHub
commit a6d04b1a17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 18 deletions

View file

@ -382,21 +382,22 @@ static int parse_argv(int argc, char *argv[]) {
case ARG_ON_CALENDAR: {
_cleanup_(calendar_spec_freep) CalendarSpec *cs = NULL;
usec_t next, curr;
/* Let's make sure the given calendar event is not in the past */
curr = now(CLOCK_REALTIME);
r = calendar_spec_from_string(optarg, &cs);
if (r < 0)
return log_error_errno(r, "Failed to parse calendar event specification");
r = calendar_spec_next_usec(cs, curr, &next);
if (r < 0) {
/* The calendar event is in the past - in such case
* don't add an OnCalendar property and execute
* the command immediately instead */
log_warning("Specified calendar event is in the past, executing immediately");
break;
}
return log_error_errno(r, "Failed to parse calendar event specification: %m");
r = calendar_spec_next_usec(cs, now(CLOCK_REALTIME), NULL);
if (r == -ENOENT)
/* The calendar event is in the past - let's warn about this, but install it
* anyway as it is. The service manager will trigger the service right-away
* then, but everything is discoverable as usual. Moreover, the server side
* might have a different clock or timezone than we do, hence it should
* decide when or whether to run something. */
log_warning("Specified calendar expression is in the past, proceeding anyway.");
else if (r < 0)
return log_error_errno(r, "Failed to calculate next time calendar expression elapses: %m");
r = add_timer_property("OnCalendar", optarg);
if (r < 0)

View file

@ -1193,6 +1193,8 @@ static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
int tm_usec;
int r;
/* Returns -ENOENT if the expression is not going to elapse anymore */
assert(spec);
assert(tm);
@ -1285,14 +1287,13 @@ static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
}
}
static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *next) {
static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *ret_next) {
struct tm tm;
time_t t;
int r;
usec_t tm_usec;
assert(spec);
assert(next);
if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
return -EINVAL;
@ -1310,7 +1311,9 @@ static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, u
if (t < 0)
return -EINVAL;
*next = (usec_t) t * USEC_PER_SEC + tm_usec;
if (ret_next)
*ret_next = (usec_t) t * USEC_PER_SEC + tm_usec;
return 0;
}
@ -1319,12 +1322,14 @@ typedef struct SpecNextResult {
int return_value;
} SpecNextResult;
int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *ret_next) {
SpecNextResult *shared, tmp;
int r;
assert(spec);
if (isempty(spec->timezone))
return calendar_spec_next_usec_impl(spec, usec, next);
return calendar_spec_next_usec_impl(spec, usec, ret_next);
shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (shared == MAP_FAILED)
@ -1352,8 +1357,8 @@ int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next)
if (munmap(shared, sizeof *shared) < 0)
return negative_errno();
if (tmp.return_value == 0)
*next = tmp.next;
if (tmp.return_value == 0 && ret_next)
*ret_next = tmp.next;
return tmp.return_value;
}