From 90b1ec03b2ce939f589239133a32f4429f2ad6a6 Mon Sep 17 00:00:00 2001 From: Ludwig Nussel Date: Tue, 14 Dec 2021 15:26:43 +0100 Subject: [PATCH] logind: fix wall message for immediate shutdowns An elapse time of zero means NOW which should trigger a wall message. --- src/login/logind-dbus.c | 46 ++++++++++++++++++++++++++--------------- src/login/logind-utmp.c | 4 ++-- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index c05c0d02cc..41b48bcab1 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -1854,6 +1854,29 @@ static int verify_shutdown_creds( return 0; } +static int setup_wall_message_timer(Manager *m, sd_bus_message* message) { + _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; + int r; + + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); + if (r >= 0) { + const char *tty = NULL; + + (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid); + (void) sd_bus_creds_get_tty(creds, &tty); + + r = free_and_strdup(&m->scheduled_shutdown_tty, tty); + if (r < 0) + return log_oom(); + } + + r = manager_setup_wall_message_timer(m); + if (r < 0) + return r; + + return 0; +} + static int method_do_shutdown_or_sleep( Manager *m, sd_bus_message *message, @@ -1922,6 +1945,8 @@ static int method_do_shutdown_or_sleep( if (r != 0) return r; + (void) setup_wall_message_timer(m, message); + r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error); if (r < 0) return r; @@ -2189,7 +2214,6 @@ static int manager_scheduled_shutdown_handler( static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { Manager *m = userdata; - _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; const char *action_multiple_sessions = NULL; const char *action_ignore_inhibit = NULL; const char *action = NULL; @@ -2270,23 +2294,11 @@ static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_ m->scheduled_shutdown_timeout = elapse; - r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); - if (r >= 0) { - const char *tty = NULL; - - (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid); - (void) sd_bus_creds_get_tty(creds, &tty); - - r = free_and_strdup(&m->scheduled_shutdown_tty, tty); - if (r < 0) { - m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); - return log_oom(); - } - } - - r = manager_setup_wall_message_timer(m); - if (r < 0) + r = setup_wall_message_timer(m, message); + if (r < 0) { + m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); return r; + } r = update_schedule_file(m); if (r < 0) diff --git a/src/login/logind-utmp.c b/src/login/logind-utmp.c index 5533836473..d2b2f8b193 100644 --- a/src/login/logind-utmp.c +++ b/src/login/logind-utmp.c @@ -135,11 +135,11 @@ int manager_setup_wall_message_timer(Manager *m) { return 0; } - if (elapse < n) + if (elapse > 0 && elapse < n) return 0; /* Warn immediately if less than 15 minutes are left */ - if (elapse - n < 15 * USEC_PER_MINUTE) { + if (elapse == 0 || elapse - n < 15 * USEC_PER_MINUTE) { r = warn_wall(m, n); if (r == 0) return 0;