From 768b507adc08b47e0dfebb30e6dd0cb30c9a517d Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 24 Mar 2026 15:40:40 +0100 Subject: [PATCH] logind: log peer ID when shutdown is called The io.systemd.Manager.{PowerOff,SoftReboot,Halt,Kexec} manager varlink and bus methods log the peer ID when calling shutdown. The logind code is missing this, so this commit adds a similar logging now. The code is quite similar to the one in existing in src/core/manager.c but its hard to share code so this adds a bit of duplication. --- src/login/logind-dbus.c | 7 +++++++ src/login/logind-shutdown.c | 23 +++++++++++++++++++++++ src/login/logind-shutdown.h | 2 ++ src/login/logind-varlink.c | 7 +++++++ 4 files changed, 39 insertions(+) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index ac5da453d64..98c651896d2 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -2310,6 +2310,13 @@ static int method_do_shutdown_or_sleep( if (r != 0) return r; + { + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; + + (void) bus_query_sender_pidref(message, &pidref); + log_shutdown_caller(&pidref, handle_action_to_string(a->handle)); + } + if (m->delayed_action) return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "Action %s already in progress, refusing requested %s operation.", diff --git a/src/login/logind-shutdown.c b/src/login/logind-shutdown.c index d79843a287f..064ebf8e2ff 100644 --- a/src/login/logind-shutdown.c +++ b/src/login/logind-shutdown.c @@ -8,8 +8,11 @@ #include "bus-common-errors.h" #include "bus-polkit.h" +#include "cgroup-util.h" +#include "format-util.h" #include "fs-util.h" #include "hashmap.h" +#include "log.h" #include "login-util.h" #include "logind.h" #include "logind-dbus.h" @@ -17,6 +20,8 @@ #include "logind-session.h" #include "logind-shutdown.h" #include "logind-user.h" +#include "pidref.h" +#include "process-util.h" #include "user-record.h" int manager_have_multiple_sessions( @@ -37,6 +42,24 @@ int manager_have_multiple_sessions( return false; } +void log_shutdown_caller(const PidRef *caller, const char *method) { + _cleanup_free_ char *comm = NULL, *unit = NULL; + + assert(method); + + if (!pidref_is_set(caller)) { + return log_notice("%s requested from unknown client PID...", method); + } + + (void) pidref_get_comm(caller, &comm); + (void) cg_pidref_get_unit(caller, &unit); + + log_notice("%s requested from client PID " PID_FMT "%s%s%s%s%s%s...", + method, caller->pid, + comm ? " ('" : "", strempty(comm), comm ? "')" : "", + unit ? " (unit " : "", strempty(unit), unit ? ")" : ""); +} + int manager_verify_shutdown_creds( Manager *m, sd_bus_message *message, diff --git a/src/login/logind-shutdown.h b/src/login/logind-shutdown.h index 38b8b7a9c08..e6bcc8c4f5d 100644 --- a/src/login/logind-shutdown.h +++ b/src/login/logind-shutdown.h @@ -7,6 +7,8 @@ int manager_have_multiple_sessions(Manager *m, uid_t uid); +void log_shutdown_caller(const PidRef *caller, const char *method); + /* manager_verify_shutdown_creds() takes *either* a "message" or "link" depending on if it is used * to validate a D-Bus or Varlink shutdown request. When varlink is used the sd_bus_error *error * must be NULL */ diff --git a/src/login/logind-varlink.c b/src/login/logind-varlink.c index f9ba74c6320..40dee113c42 100644 --- a/src/login/logind-varlink.c +++ b/src/login/logind-varlink.c @@ -385,6 +385,13 @@ static int manager_do_shutdown_action(sd_varlink *link, sd_json_variant *paramet if (r != 0) return r; + { + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; + + (void) varlink_get_peer_pidref(link, &pidref); + log_shutdown_caller(&pidref, handle_action_to_string(action)); + } + if (m->delayed_action) return sd_varlink_error(link, "io.systemd.Shutdown.AlreadyInProgress", /* parameters= */ NULL); -- 2.47.3