#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
+#include <sys/timerfd.h>
#include <systemd/sd-daemon.h>
#include "dbus-loop.h"
#include "strv.h"
#include "conf-parser.h"
+#include "mkdir.h"
Manager *manager_new(void) {
Manager *m;
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);
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);
}
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, "master-of-seat");
if (r < 0)
goto finish;
/* 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;
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, "master-of-seat");
if (r < 0)
return r;
/* 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");
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;
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 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;
HASHMAP_FOREACH(inhibitor, m->inhibitors, i)
inhibitor_start(inhibitor);
+ manager_dispatch_idle_action(m);
+
return 0;
}
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);
}
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;
goto finish;
}
+ /* Always create the directories people can create inotify
+ * watches in. Note that some applications might check for the
+ * existance of /run/systemd/seats/ to determine whether
+ * logind is available, so please always make sure this check
+ * stays in. */
+ mkdir_label("/run/systemd/seats", 0755);
+ mkdir_label("/run/systemd/users", 0755);
+ mkdir_label("/run/systemd/sessions", 0755);
+
m = manager_new();
if (!m) {
r = log_oom();