- HASHMAP_FOREACH(s, m->sessions, i) {
- dual_timestamp k;
- int ih;
-
- ih = session_get_idle_hint(s, &k);
- if (ih < 0)
- return ih;
-
- if (!ih) {
- if (!idle_hint) {
- if (k.monotonic < ts.monotonic)
- ts = k;
- } else {
- idle_hint = false;
- ts = k;
- }
- } else if (idle_hint) {
-
- if (k.monotonic > ts.monotonic)
- ts = k;
+ if (m->idle_action == HANDLE_IGNORE ||
+ m->idle_action_usec <= 0)
+ return 0;
+
+ n = now(CLOCK_MONOTONIC);
+
+ r = manager_get_idle_hint(m, &since);
+ if (r <= 0)
+ /* Not idle. Let's check if after a timeout it might be idle then. */
+ elapse = n + m->idle_action_usec;
+ else {
+ /* Idle! Let's see if it's time to do something, or if
+ * we shall sleep for longer. */
+
+ if (n >= since.monotonic + m->idle_action_usec &&
+ (m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) {
+ log_info("System idle. Taking action.");
+
+ manager_handle_action(m, 0, m->idle_action, false, false);
+ m->idle_action_not_before_usec = n;
+ }
+
+ elapse = MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec;
+ }
+
+ if (!m->idle_action_event_source) {
+
+ r = sd_event_add_monotonic(m->event, &m->idle_action_event_source, elapse, USEC_PER_SEC*30, manager_dispatch_idle_action, m);
+ if (r < 0) {
+ log_error("Failed to add idle event source: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_event_source_set_priority(m->idle_action_event_source, SD_EVENT_PRIORITY_IDLE+10);
+ if (r < 0) {
+ log_error("Failed to set idle event source priority: %s", strerror(-r));
+ return r;
+ }
+ } else {
+ r = sd_event_source_set_time(m->idle_action_event_source, elapse);
+ if (r < 0) {
+ log_error("Failed to set idle event timer: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_event_source_set_enabled(m->idle_action_event_source, SD_EVENT_ONESHOT);
+ if (r < 0) {
+ log_error("Failed to enable idle event timer: %s", strerror(-r));
+ return r;