summaryrefslogtreecommitdiff
path: root/src/login/logind.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/login/logind.c')
-rw-r--r--src/login/logind.c235
1 files changed, 178 insertions, 57 deletions
diff --git a/src/login/logind.c b/src/login/logind.c
index bae9a95f38..6776229ee7 100644
--- a/src/login/logind.c
+++ b/src/login/logind.c
@@ -28,6 +28,7 @@
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
+#include <sys/timerfd.h>
#include <systemd/sd-daemon.h>
@@ -50,12 +51,21 @@ Manager *manager_new(void) {
m->udev_vcsa_fd = -1;
m->udev_button_fd = -1;
m->epoll_fd = -1;
+ m->reserve_vt_fd = -1;
m->n_autovts = 6;
+ m->reserve_vt = 6;
m->inhibit_delay_max = 5 * USEC_PER_SEC;
- m->handle_power_key = HANDLE_NO_SESSION;
- m->handle_sleep_key = HANDLE_TTY_SESSION;
- m->handle_lid_switch = HANDLE_OFF;
+ m->handle_power_key = HANDLE_POWEROFF;
+ 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);
@@ -166,6 +176,12 @@ void manager_free(Manager *m) {
if (m->epoll_fd >= 0)
close_nointr_nofail(m->epoll_fd);
+ 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);
@@ -443,11 +459,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;
@@ -489,9 +501,10 @@ int manager_enumerate_buttons(Manager *m) {
/* Loads buttons from udev */
- if (m->handle_power_key == HANDLE_OFF &&
- m->handle_sleep_key == HANDLE_OFF &&
- m->handle_lid_switch == HANDLE_OFF)
+ if (m->handle_power_key == HANDLE_IGNORE &&
+ m->handle_suspend_key == HANDLE_IGNORE &&
+ m->handle_hibernate_key == HANDLE_IGNORE &&
+ m->handle_lid_switch == HANDLE_IGNORE)
return 0;
e = udev_enumerate_new(m->udev);
@@ -942,30 +955,26 @@ static int vt_is_busy(int vtnr) {
int manager_spawn_autovt(Manager *m, int vtnr) {
int r;
- DBusMessage *message = NULL, *reply = NULL;
char *name = NULL;
const char *mode = "fail";
- DBusError error;
assert(m);
assert(vtnr >= 1);
- dbus_error_init(&error);
-
- if ((unsigned) vtnr > m->n_autovts)
+ if ((unsigned) vtnr > m->n_autovts &&
+ (unsigned) vtnr != m->reserve_vt)
return 0;
- r = vt_is_busy(vtnr);
- if (r < 0)
- return r;
- else if (r > 0)
- return -EBUSY;
+ if ((unsigned) vtnr != m->reserve_vt) {
+ /* If this is the reserved TTY, we'll start the getty
+ * on it in any case, but otherwise only if it is not
+ * busy. */
- message = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit");
- if (!message) {
- log_error("Could not allocate message.");
- r = -ENOMEM;
- goto finish;
+ r = vt_is_busy(vtnr);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ return -EBUSY;
}
if (asprintf(&name, "autovt@tty%i.service", vtnr) < 0) {
@@ -974,35 +983,45 @@ int manager_spawn_autovt(Manager *m, int vtnr) {
goto finish;
}
- if (!dbus_message_append_args(message,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_STRING, &mode,
- DBUS_TYPE_INVALID)) {
- log_error("Could not attach target and flag information to message.");
- r = -ENOMEM;
- goto finish;
- }
-
- reply = dbus_connection_send_with_reply_and_block(m->bus, message, -1, &error);
- if (!reply) {
- log_error("Failed to start unit: %s", bus_error_message(&error));
- goto finish;
- }
-
- r = 0;
+ r = bus_method_call_with_reply (
+ m->bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartUnit",
+ NULL,
+ NULL,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_STRING, &mode,
+ DBUS_TYPE_INVALID);
finish:
free(name);
- if (message)
- dbus_message_unref(message);
+ return r;
+}
- if (reply)
- dbus_message_unref(reply);
+static int manager_reserve_vt(Manager *m) {
+ _cleanup_free_ char *p = NULL;
- dbus_error_free(&error);
+ assert(m);
- return r;
+ if (m->reserve_vt <= 0)
+ return 0;
+
+ if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0)
+ return log_oom();
+
+ m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+ if (m->reserve_vt_fd < 0) {
+
+ /* Don't complain on VT-less systems */
+ if (errno != ENOENT)
+ log_warning("Failed to pin reserved VT: %m");
+ return -errno;
+ }
+
+ return 0;
}
int manager_get_session_by_cgroup(Manager *m, const char *cgroup, Session **session) {
@@ -1272,11 +1291,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;
@@ -1293,9 +1308,10 @@ static int manager_connect_udev(Manager *m) {
return -errno;
/* Don't watch keys if nobody cares */
- if (m->handle_power_key != HANDLE_OFF ||
- m->handle_sleep_key != HANDLE_OFF ||
- m->handle_lid_switch != HANDLE_OFF) {
+ if (m->handle_power_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");
if (!m->udev_button_monitor)
@@ -1395,7 +1411,7 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
assert(m);
- idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t);
+ idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0);
HASHMAP_FOREACH(s, m->sessions, i) {
dual_timestamp k;
@@ -1426,6 +1442,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;
@@ -1475,6 +1564,9 @@ int manager_startup(Manager *m) {
/* Remove stale objects before we start them */
manager_gc(m, false);
+ /* Reserve the special reserved VT */
+ manager_reserve_vt(m);
+
/* And start everything */
HASHMAP_FOREACH(seat, m->seats, i)
seat_start(seat);
@@ -1488,9 +1580,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);
@@ -1504,6 +1618,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;
@@ -1548,6 +1665,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;