From ae983e6fd72b55ababce42f0020bdde1e4927722 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 28 Apr 2024 14:22:18 +0100 Subject: [PATCH] Improve monitoring of seat state If the user is reported as active then check that they have at least one active seat and downgrade the status to online if not. This ensures that a remote login session won't be interpreted as the user being active on a local seat. --- modules/module-logind.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/modules/module-logind.c b/modules/module-logind.c index 43952536..1660ea73 100644 --- a/modules/module-logind.c +++ b/modules/module-logind.c @@ -48,6 +48,20 @@ wp_logind_get_state (WpLogind *self) return g_strdup (self->state); } +static int +wp_logind_get_user_state (uid_t uid, char **state) +{ + int res; + if ((res = sd_uid_get_state (uid, state)) >= 0) { + if (g_strcmp0 (*state, "active") == 0 && + sd_uid_get_seats (uid, 1, NULL) == 0) { + free (*state); + *state = strdup ("online"); + } + } + return res; +} + static gboolean wp_logind_source_ready (gint fd, GIOCondition condition, gpointer user_data) { @@ -55,14 +69,15 @@ wp_logind_source_ready (gint fd, GIOCondition condition, gpointer user_data) sd_login_monitor_flush (self->monitor); { char *state = NULL; - sd_uid_get_state (getuid(), &state); - if (g_strcmp0 (state, self->state) != 0) { - char *tmp = state; - state = self->state; - self->state = tmp; - g_signal_emit (self, signals[SIGNAL_STATE_CHANGED], 0, self->state); + if (wp_logind_get_user_state (getuid(), &state) >= 0) { + if (g_strcmp0 (state, self->state) != 0) { + char *tmp = state; + state = self->state; + self->state = tmp; + g_signal_emit (self, signals[SIGNAL_STATE_CHANGED], 0, self->state); + } + free (state); } - free (state); } return G_SOURCE_CONTINUE; } @@ -81,7 +96,7 @@ wp_logind_enable (WpPlugin * plugin, WpTransition * transition) return; } - if ((res = sd_uid_get_state (getuid(), &self->state)) < 0) { + if ((res = wp_logind_get_user_state (getuid(), &self->state)) < 0) { wp_transition_return_error (transition, g_error_new (G_IO_ERROR, g_io_error_from_errno (-res), "failed to get systemd login state: %d (%s)",