static int sntp_arm_timer(SNTPContext *sntp, usec_t next) { sd_event *e; int r; assert(sntp); assert(sntp->event_receive); if (next == 0) { sntp->event_timer = sd_event_source_unref(sntp->event_timer); return 0; } if (sntp->event_timer) { r = sd_event_source_set_time(sntp->event_timer, now(CLOCK_MONOTONIC) + next); if (r < 0) return r; return sd_event_source_set_enabled(sntp->event_timer, SD_EVENT_ONESHOT); } e = sd_event_source_get_event(sntp->event_receive); r = sd_event_add_time( e, &sntp->event_timer, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + next, 0, sntp_timer, sntp); if (r < 0) return r; return 0; }
static int scope_arm_timer(Scope *s) { int r; assert(s); if (s->timeout_stop_usec <= 0) { s->timer_event_source = sd_event_source_unref(s->timer_event_source); return 0; } if (s->timer_event_source) { r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_stop_usec); if (r < 0) return r; return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); } r = sd_event_add_time( UNIT(s)->manager->event, &s->timer_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + s->timeout_stop_usec, 0, scope_dispatch_timer, s); if (r < 0) return r; (void) sd_event_source_set_description(s->timer_event_source, "scope-timer"); return 0; }
static int scope_arm_timer(Scope *s, usec_t usec) { int r; assert(s); if (s->timer_event_source) { r = sd_event_source_set_time(s->timer_event_source, usec); if (r < 0) return r; return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); } if (usec == USEC_INFINITY) return 0; r = sd_event_add_time( UNIT(s)->manager->event, &s->timer_event_source, CLOCK_MONOTONIC, usec, 0, scope_dispatch_timer, s); if (r < 0) return r; (void) sd_event_source_set_description(s->timer_event_source, "scope-timer"); return 0; }
static int busname_arm_timer(BusName *n) { int r; assert(n); if (n->timeout_usec <= 0) { n->timer_event_source = sd_event_source_unref(n->timer_event_source); return 0; } if (n->timer_event_source) { r = sd_event_source_set_time(n->timer_event_source, now(CLOCK_MONOTONIC) + n->timeout_usec); if (r < 0) return r; return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT); } return sd_event_add_time( UNIT(n)->manager->event, &n->timer_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + n->timeout_usec, 0, busname_dispatch_timer, n); }
static int wall_message_timeout_handler( sd_event_source *s, uint64_t usec, void *userdata) { Manager *m = userdata; usec_t n, next; int r; assert(m); assert(s == m->wall_message_timeout_source); n = now(CLOCK_REALTIME); r = warn_wall(m, n); if (r == 0) return 0; next = when_wall(n, m->scheduled_shutdown_timeout); if (next > 0) { r = sd_event_source_set_time(s, n + next); if (r < 0) return log_error_errno(r, "sd_event_source_set_time() failed. %m"); r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); if (r < 0) return log_error_errno(r, "sd_event_source_set_enabled() failed. %m"); } return 0; }
static int automount_start_expire(Automount *a) { int r; usec_t timeout; assert(a); if (a->timeout_idle_usec == 0) return 0; timeout = now(CLOCK_MONOTONIC) + MAX(a->timeout_idle_usec/3, USEC_PER_SEC); if (a->expire_event_source) { r = sd_event_source_set_time(a->expire_event_source, timeout); if (r < 0) return r; return sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_ONESHOT); } r = sd_event_add_time( UNIT(a)->manager->event, &a->expire_event_source, CLOCK_MONOTONIC, timeout, 0, automount_dispatch_expire, a); if (r < 0) return r; (void) sd_event_source_set_description(a->expire_event_source, "automount-expire"); return 0; }
static int curl_glue_timer_callback(CURLM *curl, long timeout_ms, void *userdata) { CurlGlue *g = userdata; usec_t usec; assert(curl); assert(g); if (timeout_ms < 0) { if (g->timer) { if (sd_event_source_set_enabled(g->timer, SD_EVENT_OFF) < 0) return -1; } return 0; } usec = now(clock_boottime_or_monotonic()) + (usec_t) timeout_ms * USEC_PER_MSEC + USEC_PER_MSEC - 1; if (g->timer) { if (sd_event_source_set_time(g->timer, usec) < 0) return -1; if (sd_event_source_set_enabled(g->timer, SD_EVENT_ONESHOT) < 0) return -1; } else { if (sd_event_add_time(g->event, &g->timer, clock_boottime_or_monotonic(), usec, 0, curl_glue_on_timer, g) < 0) return -1; (void) sd_event_source_set_description(g->timer, "curl-timer"); } return 0; }
static int on_lldp_timer(sd_event_source *s, usec_t t, void *userdata) { Link *link = userdata; usec_t current, delay, next; int r; assert(s); assert(userdata); log_link_debug(link, "Sending LLDP packet..."); r = link_send_lldp(link); if (r < 0) log_link_debug_errno(link, r, "Failed to send LLDP packet, ignoring: %m"); if (link->lldp_tx_fast > 0) link->lldp_tx_fast--; assert_se(sd_event_now(sd_event_source_get_event(s), clock_boottime_or_monotonic(), ¤t) >= 0); delay = link->lldp_tx_fast > 0 ? LLDP_FAST_TX_USEC : LLDP_TX_INTERVAL_USEC; next = usec_add(usec_add(current, delay), (usec_t) random_u64() % LLDP_JITTER_USEC); r = sd_event_source_set_time(s, next); if (r < 0) return log_link_error_errno(link, r, "Failed to restart LLDP timer: %m"); r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); if (r < 0) return log_link_error_errno(link, r, "Failed to enable LLDP timer: %m"); return 0; }
static int busname_arm_timer(BusName *n, usec_t usec) { int r; assert(n); if (n->timer_event_source) { r = sd_event_source_set_time(n->timer_event_source, usec); if (r < 0) return r; return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT); } if (usec == USEC_INFINITY) return 0; r = sd_event_add_time( UNIT(n)->manager->event, &n->timer_event_source, CLOCK_MONOTONIC, usec, 0, busname_dispatch_timer, n); if (r < 0) return r; (void) sd_event_source_set_description(n->timer_event_source, "busname-timer"); return 0; }
static int manager_arm_timer(Manager *m, usec_t next) { int r; assert(m); assert(m->event_receive); if (next == 0) { m->event_timer = sd_event_source_unref(m->event_timer); return 0; } if (m->event_timer) { r = sd_event_source_set_time(m->event_timer, now(clock_boottime_or_monotonic()) + next); if (r < 0) return r; return sd_event_source_set_enabled(m->event_timer, SD_EVENT_ONESHOT); } return sd_event_add_time( m->event, &m->event_timer, clock_boottime_or_monotonic(), now(clock_boottime_or_monotonic()) + next, 0, manager_timer, m); }
static void keyboard_arm(idev_keyboard *k, usec_t usecs) { int r; if (usecs != 0) { usecs += now(CLOCK_MONOTONIC); r = sd_event_source_set_time(k->repeat_timer, usecs); if (r >= 0) sd_event_source_set_enabled(k->repeat_timer, SD_EVENT_ONESHOT); } else { sd_event_source_set_enabled(k->repeat_timer, SD_EVENT_OFF); } }
int manager_setup_wall_message_timer(Manager *m) { usec_t n, elapse; int r; assert(m); n = now(CLOCK_REALTIME); elapse = m->scheduled_shutdown_timeout; /* wall message handling */ if (isempty(m->scheduled_shutdown_type)) { warn_wall(m, n); return 0; } if (elapse < n) return 0; /* Warn immediately if less than 15 minutes are left */ if (elapse - n < 15 * USEC_PER_MINUTE) { r = warn_wall(m, n); if (r == 0) return 0; } elapse = when_wall(n, elapse); if (elapse == 0) return 0; if (m->wall_message_timeout_source) { r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse); if (r < 0) return log_error_errno(r, "sd_event_source_set_time() failed. %m"); r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT); if (r < 0) return log_error_errno(r, "sd_event_source_set_enabled() failed. %m"); } else { r = sd_event_add_time(m->event, &m->wall_message_timeout_source, CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m); if (r < 0) return log_error_errno(r, "sd_event_add_time() failed. %m"); } return 0; }
int link_lldp_emit_start(Link *link) { usec_t next; int r; assert(link); if (!link->network || link->network->lldp_emit == LLDP_EMIT_NO) { link_lldp_emit_stop(link); return 0; } /* Starts the LLDP transmission in "fast" mode. If it is already started, turns "fast" mode back on again. */ link->lldp_tx_fast = LLDP_TX_FAST_INIT; next = usec_add(usec_add(now(clock_boottime_or_monotonic()), LLDP_FAST_TX_USEC), (usec_t) random_u64() % LLDP_JITTER_USEC); if (link->lldp_emit_event_source) { usec_t old; /* Lower the timeout, maybe */ r = sd_event_source_get_time(link->lldp_emit_event_source, &old); if (r < 0) return r; if (old <= next) return 0; return sd_event_source_set_time(link->lldp_emit_event_source, next); } else { r = sd_event_add_time( link->manager->event, &link->lldp_emit_event_source, clock_boottime_or_monotonic(), next, 0, on_lldp_timer, link); if (r < 0) return r; (void) sd_event_source_set_description(link->lldp_emit_event_source, "lldp-tx"); } return 0; }
static int lldp_start_timer(sd_lldp *lldp, sd_lldp_neighbor *neighbor) { sd_lldp_neighbor *n; int r; assert(lldp); if (neighbor) lldp_neighbor_start_ttl(neighbor); n = prioq_peek(lldp->neighbor_by_expiry); if (!n) { if (lldp->timer_event_source) return sd_event_source_set_enabled(lldp->timer_event_source, SD_EVENT_OFF); return 0; } if (lldp->timer_event_source) { r = sd_event_source_set_time(lldp->timer_event_source, n->until); if (r < 0) return r; return sd_event_source_set_enabled(lldp->timer_event_source, SD_EVENT_ONESHOT); } if (!lldp->event) return 0; r = sd_event_add_time(lldp->event, &lldp->timer_event_source, clock_boottime_or_monotonic(), n->until, 0, on_timer_event, lldp); if (r < 0) return r; r = sd_event_source_set_priority(lldp->timer_event_source, lldp->event_priority); if (r < 0) return r; (void) sd_event_source_set_description(lldp->timer_event_source, "lldp-timer"); return 0; }
static int PmonPing(sd_bus_message *m, void *userdata, sd_bus_error *retError) { int id = 0; sd_bus_message_read(m, "u", &id); char *sid = (char*)sd_event_source_get_userdata(clients[id]); if (sid == NULL) { return sd_bus_reply_method_return(m, "b", false); } if (strcmp((char*)sd_event_source_get_userdata(clients[id]), sd_bus_message_get_sender(m)) != 0) { return sd_bus_reply_method_return(m, "b", false); } uint64_t usec = 0; sd_event_now(event, CLOCK_MONOTONIC, &usec); sd_event_source_set_time(clients[id], clientTimeout[id]+usec); return sd_bus_reply_method_return(m, "b", true); }
int main(int argc, char *argv[]) { sd_event *e = NULL; sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL; static const char ch = 'x'; int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1}, k[2] = { -1, -1 }; assert_se(pipe(a) >= 0); assert_se(pipe(b) >= 0); assert_se(pipe(d) >= 0); assert_se(pipe(k) >= 0); assert_se(sd_event_default(&e) >= 0); assert_se(sd_event_set_watchdog(e, true) >= 0); /* Test whether we cleanly can destroy an io event source from its own handler */ got_unref = false; assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0); assert_se(write(k[1], &ch, 1) == 1); assert_se(sd_event_run(e, (uint64_t) -1) >= 1); assert_se(got_unref); got_a = false, got_b = false, got_c = false, got_d = 0; /* Add a oneshot handler, trigger it, re-enable it, and trigger * it again. */ assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0); assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0); assert_se(write(d[1], &ch, 1) >= 0); assert_se(sd_event_run(e, (uint64_t) -1) >= 1); assert_se(got_d == 1); assert_se(write(d[1], &ch, 1) >= 0); assert_se(sd_event_run(e, (uint64_t) -1) >= 1); assert_se(got_d == 2); assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0); assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0); assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0); assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0); assert_se(sd_event_source_set_priority(x, 99) >= 0); assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0); assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0); assert_se(sd_event_source_set_priority(z, 50) >= 0); assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0); /* Test for floating event sources */ assert_se(sigprocmask_many(SIG_BLOCK, SIGRTMIN+1, -1) == 0); assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0); assert_se(write(a[1], &ch, 1) >= 0); assert_se(write(b[1], &ch, 1) >= 0); assert_se(!got_a && !got_b && !got_c); assert_se(sd_event_run(e, (uint64_t) -1) >= 1); assert_se(!got_a && got_b && !got_c); assert_se(sd_event_run(e, (uint64_t) -1) >= 1); assert_se(!got_a && got_b && got_c); assert_se(sd_event_run(e, (uint64_t) -1) >= 1); assert_se(got_a && got_b && got_c); sd_event_source_unref(x); sd_event_source_unref(y); do_quit = true; assert_se(sd_event_source_set_time(z, now(CLOCK_MONOTONIC) + 200 * USEC_PER_MSEC) >= 0); assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); assert_se(sd_event_loop(e) >= 0); sd_event_source_unref(z); sd_event_source_unref(q); sd_event_source_unref(w); sd_event_unref(e); safe_close_pair(a); safe_close_pair(b); safe_close_pair(d); safe_close_pair(k); return 0; }