]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shutdown: send an sd_notify() message on shutdown with the shutdown reason and boot...
authorLennart Poettering <lennart@poettering.net>
Mon, 22 Apr 2024 15:32:12 +0000 (17:32 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 23 Apr 2024 09:04:08 +0000 (11:04 +0200)
This is kinda nice in containers, to exfiltrate a string from the
container on shutdown.

man/systemd.xml
src/shutdown/shutdown.c

index abfcc499f088fa619d18b3a55b4fe748064ad5aa..df0027886c3b8817aa75821dd3e0ad0fe15c6adb 100644 (file)
       details.</para>
 
       <xi:include href="version-info.xml" xpointer="v256"/></listitem>
+
+      <listitem><para>An <varname>X_SYSTEMD_SHUTDOWN=…</varname> message will be sent out very shortly before
+      the system shuts down. The value is one of the strings <literal>reboot</literal>,
+      <literal>halt</literal>, <literal>poweroff</literal>, <literal>kexec</literal> and indicates which kind
+      of shutdown is being executed.</para>
+
+      <xi:include href="version-info.xml" xpointer="v256"/></listitem>
+
+      <listitem><para>An <varname>X_SYSTEMD_REBOOT_PARAMETER=…</varname> message will also be sent out very
+      shortly before the system shuts down. Its value is the reboot argument as configured with
+      <command>systemctl --reboot-argument=…</command>.</para>
+
+      <xi:include href="version-info.xml" xpointer="v256"/></listitem>
     </itemizedlist>
 
     <para>Note that these extension fields are sent in addition to the regular <literal>READY=1</literal> and
index b709078afed190056bb3b47951d87a4cb415ef08..1ddda009410f12306180c853190a1fc9b7fcedb6 100644 (file)
@@ -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.");