From 623a00020f116d8e9c70608a9e4f7cc978342441 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 12 Apr 2023 11:56:00 +0200 Subject: [PATCH 1/3] notify: Add EXIT_STATUS field Whenever one of our tools or daemons exits, let's send the exit status via sd-notify in the EXIT_STATUS field. --- man/sd_notify.xml | 7 +++++++ src/shared/main-func.h | 1 + 2 files changed, 8 insertions(+) diff --git a/man/sd_notify.xml b/man/sd_notify.xml index 39bddc9d15..f93542e329 100644 --- a/man/sd_notify.xml +++ b/man/sd_notify.xml @@ -170,6 +170,13 @@ BUSERROR=org.freedesktop.DBus.Error.TimedOut + + EXIT_STATUS=… + + If a service exits, the return value of its main() function. + + + MAINPID=… diff --git a/src/shared/main-func.h b/src/shared/main-func.h index 3dc4992282..3f6b6a849d 100644 --- a/src/shared/main-func.h +++ b/src/shared/main-func.h @@ -21,6 +21,7 @@ r = impl; \ if (r < 0) \ (void) sd_notifyf(0, "ERRNO=%i", -r); \ + (void) sd_notifyf(0, "EXIT_STATUS=%i", ret); \ ask_password_agent_close(); \ polkit_agent_close(); \ pager_close(); \ From 3a89cb84a62314c3c4807016dbff4359d6988d79 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 11 Apr 2023 13:17:13 +0200 Subject: [PATCH 2/3] core: Propagate exit status via notify socket when running in VM When running in a container, we can propagate the exit status of pid1 as usual via the process exit status. This is not possible when running in a VM. Instead, let's send EXIT_STATUS=%i via the notify socket if one is configured. The user running the VM can then pick up the exit status from the notify socket after the VM has shut down. --- src/core/main.c | 4 ++++ src/shutdown/shutdown.c | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/core/main.c b/src/core/main.c index 9588c625b2..e49f6ab094 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -3105,6 +3105,10 @@ finish: error_message = "Failed to execute shutdown binary"; } + /* This is primarily useful when running systemd in a VM, as it provides the user running the VM with + * a mechanism to pick up systemd's exit status in the VM. */ + (void) sd_notifyf(0, "EXIT_STATUS=%i", retval); + watchdog_free_device(); arg_watchdog_device = mfree(arg_watchdog_device); diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c index a8248901ce..b1dac20c69 100644 --- a/src/shutdown/shutdown.c +++ b/src/shutdown/shutdown.c @@ -14,6 +14,8 @@ #include #include +#include "sd-daemon.h" + #include "alloc-util.h" #include "async.h" #include "binfmt-util.h" @@ -570,6 +572,10 @@ int main(int argc, char *argv[]) { if (!in_container) sync_with_progress(); + /* This is primarily useful when running systemd in a VM, as it provides the user running the VM with + * a mechanism to pick up systemd's exit status in the VM. */ + (void) sd_notifyf(0, "EXIT_STATUS=%i", arg_exit_code); + if (streq(arg_verb, "exit")) { if (in_container) { log_info("Exiting container."); From 88eec29d188f2ed18bff6b06ecd61d8c09db48f4 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 12 Apr 2023 12:08:27 +0200 Subject: [PATCH 3/3] core: Send ERRNO= via notify socket on exit --- src/core/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/main.c b/src/core/main.c index e49f6ab094..6e3796f9dc 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -3096,6 +3096,9 @@ finish: __lsan_do_leak_check(); #endif + if (r < 0) + (void) sd_notifyf(0, "ERRNO=%i", -r); + /* Try to invoke the shutdown binary unless we already failed. * If we failed above, we want to freeze after finishing cleanup. */ if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM &&