X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Flogind.c;h=be793e2a93ca8a8f93cf059a220850d4e30e5f34;hp=14c83551b9a4cc3be9ec37d78200b22bf48c5397;hb=314b4b0a68d9ab35de981923a088fc8c8820caa5;hpb=65b5116220a8ebf8a260716152409aa05377aacc diff --git a/src/login/logind.c b/src/login/logind.c index 14c83551b..be793e2a9 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,11 +179,16 @@ 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); strv_free(m->kill_exclude_users); + free(m->action_job); + free(m->cgroup_path); free(m); } @@ -449,11 +461,7 @@ int manager_enumerate_devices(Manager *m) { goto finish; } - r = udev_enumerate_add_match_subsystem(e, "graphics"); - if (r < 0) - goto finish; - - r = udev_enumerate_add_match_tag(e, "seat"); + r = udev_enumerate_add_match_tag(e, "seat-master"); if (r < 0) goto finish; @@ -496,7 +504,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; @@ -1284,11 +1293,7 @@ static int manager_connect_udev(Manager *m) { if (!m->udev_seat_monitor) return -ENOMEM; - r = udev_monitor_filter_add_match_tag(m->udev_seat_monitor, "seat"); - if (r < 0) - return r; - - r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_seat_monitor, "graphics", NULL); + r = udev_monitor_filter_add_match_tag(m->udev_seat_monitor, "seat-master"); if (r < 0) return r; @@ -1306,7 +1311,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 +1413,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 +1444,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,6 +1582,8 @@ int manager_startup(Manager *m) { HASHMAP_FOREACH(inhibitor, m->inhibitors, i) inhibitor_start(inhibitor); + manager_dispatch_idle_action(m); + return 0; } @@ -1547,11 +1628,11 @@ int manager_run(Manager *m) { manager_gc(m, true); - if (m->delayed_unit) { + if (m->action_what != 0) { usec_t x, y; x = now(CLOCK_MONOTONIC); - y = m->delayed_timestamp + m->inhibit_delay_max; + y = m->action_timestamp + m->inhibit_delay_max; msec = x >= y ? 0 : (int) ((y - x) / USEC_PER_MSEC); } @@ -1586,6 +1667,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;