X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Flogind.c;h=6438631b594c15ce61e8d05947f542c5e48f99a6;hp=e22f68d23761ba31adac1eeef23b5c8105a2ca04;hb=23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0;hpb=beaafb2ea6be591882aef21fe19b88e3b2461087 diff --git a/src/login/logind.c b/src/login/logind.c index e22f68d23..6438631b5 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -56,10 +57,16 @@ Manager *manager_new(void) { m->reserve_vt = 6; m->inhibit_delay_max = 5 * USEC_PER_SEC; m->handle_power_key = HANDLE_POWEROFF; - m->handle_sleep_key = HANDLE_SUSPEND; + m->handle_suspend_key = HANDLE_SUSPEND; + m->handle_hibernate_key = HANDLE_HIBERNATE; m->handle_lid_switch = HANDLE_SUSPEND; m->lid_switch_ignore_inhibited = true; + m->idle_action_fd = -1; + m->idle_action_usec = 30 * USEC_PER_MINUTE; + m->idle_action = HANDLE_IGNORE; + m->idle_action_not_before_usec = now(CLOCK_MONOTONIC); + m->devices = hashmap_new(string_hash_func, string_compare_func); m->seats = hashmap_new(string_hash_func, string_compare_func); m->sessions = hashmap_new(string_hash_func, string_compare_func); @@ -172,6 +179,9 @@ void manager_free(Manager *m) { if (m->reserve_vt_fd >= 0) close_nointr_nofail(m->reserve_vt_fd); + if (m->idle_action_fd >= 0) + close_nointr_nofail(m->idle_action_fd); + strv_free(m->controllers); strv_free(m->reset_controllers); strv_free(m->kill_only_users); @@ -496,7 +506,8 @@ int manager_enumerate_buttons(Manager *m) { /* Loads buttons from udev */ if (m->handle_power_key == HANDLE_IGNORE && - m->handle_sleep_key == HANDLE_IGNORE && + m->handle_suspend_key == HANDLE_IGNORE && + m->handle_hibernate_key == HANDLE_IGNORE && m->handle_lid_switch == HANDLE_IGNORE) return 0; @@ -1306,7 +1317,8 @@ static int manager_connect_udev(Manager *m) { /* Don't watch keys if nobody cares */ if (m->handle_power_key != HANDLE_IGNORE || - m->handle_sleep_key != HANDLE_IGNORE || + m->handle_suspend_key != HANDLE_IGNORE || + m->handle_hibernate_key != HANDLE_IGNORE || m->handle_lid_switch != HANDLE_IGNORE) { m->udev_button_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); @@ -1407,7 +1419,7 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t) { assert(m); - idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false); + idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0); HASHMAP_FOREACH(s, m->sessions, i) { dual_timestamp k; @@ -1438,6 +1450,79 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t) { return idle_hint; } +int manager_dispatch_idle_action(Manager *m) { + struct dual_timestamp since; + struct itimerspec its; + int r; + usec_t n; + + assert(m); + + if (m->idle_action == HANDLE_IGNORE || + m->idle_action_usec <= 0) { + r = 0; + goto finish; + } + + zero(its); + n = now(CLOCK_MONOTONIC); + + r = manager_get_idle_hint(m, &since); + if (r <= 0) + /* Not idle. Let's check if after a timeout it it might be idle then. */ + timespec_store(&its.it_value, 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; + } + + timespec_store(&its.it_value, MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec); + } + + if (m->idle_action_fd < 0) { + struct epoll_event ev; + + m->idle_action_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC); + if (m->idle_action_fd < 0) { + log_error("Failed to create idle action timer: %m"); + r = -errno; + goto finish; + } + + zero(ev); + ev.events = EPOLLIN; + ev.data.u32 = FD_IDLE_ACTION; + + if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->idle_action_fd, &ev) < 0) { + log_error("Failed to add idle action timer to epoll: %m"); + r = -errno; + goto finish; + } + } + + if (timerfd_settime(m->idle_action_fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) { + log_error("Failed to reset timerfd: %m"); + r = -errno; + goto finish; + } + + return 0; + +finish: + if (m->idle_action_fd >= 0) { + close_nointr_nofail(m->idle_action_fd); + m->idle_action_fd = -1; + } + + return r; +} int manager_startup(Manager *m) { int r; Seat *seat; @@ -1503,9 +1588,31 @@ int manager_startup(Manager *m) { HASHMAP_FOREACH(inhibitor, m->inhibitors, i) inhibitor_start(inhibitor); + manager_dispatch_idle_action(m); + return 0; } +static int manager_recheck_buttons(Manager *m) { + Iterator i; + Button *b; + int r = 0; + + assert(m); + + HASHMAP_FOREACH(b, m->buttons, i) { + int q; + + q = button_recheck(b); + if (q > 0) + return 1; + if (q < 0) + r = q; + } + + return r; +} + int manager_run(Manager *m) { assert(m); @@ -1519,6 +1626,9 @@ int manager_run(Manager *m) { if (manager_dispatch_delayed(m) > 0) continue; + if (manager_recheck_buttons(m) > 0) + continue; + if (dbus_connection_dispatch(m->bus) != DBUS_DISPATCH_COMPLETE) continue; @@ -1563,6 +1673,10 @@ int manager_run(Manager *m) { manager_dispatch_console(m); break; + case FD_IDLE_ACTION: + manager_dispatch_idle_action(m); + break; + case FD_BUS: bus_loop_dispatch(m->bus_fd); break;