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/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
#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.");