diff options
author | Mike Yuan <me@yhndnzj.com> | 2023-04-13 01:12:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-13 01:12:03 +0800 |
commit | 93ba4c1bc095ddeda608c0a7b1af2b51c8319a8b (patch) | |
tree | 07110af764407b1dfa38fc7f3f6df7407b583b78 | |
parent | 7581da99a104f31e49ea679eb352cb46a3a19383 (diff) | |
parent | 88eec29d188f2ed18bff6b06ecd61d8c09db48f4 (diff) | |
download | systemd-93ba4c1bc095ddeda608c0a7b1af2b51c8319a8b.tar.gz |
Merge pull request #27212 from DaanDeMeyer/notify-exit
core: Propagate exit status via notify socket when running in VM
-rw-r--r-- | man/sd_notify.xml | 7 | ||||
-rw-r--r-- | src/core/main.c | 7 | ||||
-rw-r--r-- | src/shared/main-func.h | 1 | ||||
-rw-r--r-- | src/shutdown/shutdown.c | 6 |
4 files changed, 21 insertions, 0 deletions
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 @@ -171,6 +171,13 @@ </varlistentry> <varlistentry> + <term>EXIT_STATUS=…</term> + + <listitem><para>If a service exits, the return value of its <function>main()</function> function. + </para></listitem> + </varlistentry> + + <varlistentry> <term>MAINPID=…</term> <listitem><para>The main process ID (PID) of the service, in case the service manager did not fork diff --git a/src/core/main.c b/src/core/main.c index 9588c625b2..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 && @@ -3105,6 +3108,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/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(); \ 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 <sys/stat.h> #include <unistd.h> +#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."); |