From 8c081ae84b5a98df1a18c542a34431c99f59e3ed Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 22 Apr 2024 17:32:12 +0200 Subject: [PATCH] shutdown: send an sd_notify() message on shutdown with the shutdown reason and boot param This is kinda nice in containers, to exfiltrate a string from the container on shutdown. --- man/systemd.xml | 13 +++++++++++++ src/shutdown/shutdown.c | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/man/systemd.xml b/man/systemd.xml index abfcc499f08..df0027886c3 100644 --- a/man/systemd.xml +++ b/man/systemd.xml @@ -1257,6 +1257,19 @@ details. + + An X_SYSTEMD_SHUTDOWN=… message will be sent out very shortly before + the system shuts down. The value is one of the strings reboot, + halt, poweroff, kexec and indicates which kind + of shutdown is being executed. + + + + An X_SYSTEMD_REBOOT_PARAMETER=… message will also be sent out very + shortly before the system shuts down. Its value is the reboot argument as configured with + systemctl --reboot-argument=…. + + Note that these extension fields are sent in addition to the regular READY=1 and diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c index b709078afed..1ddda009410 100644 --- a/src/shutdown/shutdown.c +++ b/src/shutdown/shutdown.c @@ -333,6 +333,26 @@ static void init_watchdog(void) { } } +static void notify_supervisor(void) { + /* Notify VMM/container manager of the desired mode of reboot and the boot parameter */ + _cleanup_free_ char *reboot_parameter = NULL; + int r; + + r = read_reboot_parameter(&reboot_parameter); + if (r < 0 && r != -ENOENT) + log_debug_errno(r, "Failed to read reboot parameter, ignoring: %m"); + + if (reboot_parameter) + (void) sd_notifyf(/* unset_environment= */ false, + "X_SYSTEMD_SHUTDOWN=%s\n" + "X_SYSTEMD_REBOOT_PARAMETER=%s", + arg_verb, reboot_parameter); + else + (void) sd_notifyf(/* unset_environment= */ false, + "X_SYSTEMD_SHUTDOWN=%s", + arg_verb); +} + int main(int argc, char *argv[]) { static const char* const dirs[] = { SYSTEM_SHUTDOWN_PATH, @@ -589,6 +609,8 @@ int main(int argc, char *argv[]) { if (!in_container) sync_with_progress(); + notify_supervisor(); + if (streq(arg_verb, "exit")) { if (in_container) { log_info("Exiting container."); -- 2.47.3