]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #32428 from poettering/sd-notify-reboot-param
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 23 Apr 2024 11:31:40 +0000 (13:31 +0200)
committerGitHub <noreply@github.com>
Tue, 23 Apr 2024 11:31:40 +0000 (13:31 +0200)
pid1: send shutdown type and reboot argument to supervisor via sd_notify()

TODO
man/systemd.xml
src/shutdown/shutdown.c
src/systemctl/systemctl-start-special.c

diff --git a/TODO b/TODO
index 8c52f65b5507ab85321f593eaaba80fa21ebbd34..163f66f1527b93465b62f2d3f9f2b2fb35fe852f 100644 (file)
--- a/TODO
+++ b/TODO
@@ -130,6 +130,9 @@ Deprecations and removals:
 
 Features:
 
+* Clean up "reboot argument" handling, i.e. set it through some IPC service
+  instead of directly via /run/, so that it can be sensible set remotely.
+
 * userdb: add concept for user "aliases", to cover for cases where you can log
   in under the name lennart@somenetworkfsserver, and it would automatically
   generate a local user, and from the one both names can be used to allow
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.");
index d486b406da556cd8e9bd156dd994f454082645d7..4b99d0c62940d6d1518297668312482cfe4f3af6 100644 (file)
@@ -155,14 +155,16 @@ int verb_start_special(int argc, char *argv[], void *userdata) {
                         return r;
         }
 
-        if (a == ACTION_REBOOT) {
-                if (arg_reboot_argument) {
-                        r = update_reboot_parameter_and_warn(arg_reboot_argument, false);
-                        if (r < 0)
-                                return r;
-                }
+        if (arg_reboot_argument && IN_SET(a, ACTION_HALT, ACTION_POWEROFF, ACTION_REBOOT, ACTION_KEXEC)) {
+                /* If we are going through an action that involves systemd-shutdown, let's set the reboot
+                 * parameter, even if it's not a regular reboot. After all we nowadays send the string to
+                 * our supervisor via sd_notify() too. */
+                r = update_reboot_parameter_and_warn(arg_reboot_argument, /* keep= */ false);
+                if (r < 0)
+                        return r;
+        }
 
-        } else if (a == ACTION_KEXEC) {
+        if (a == ACTION_KEXEC) {
                 r = load_kexec_kernel();
                 if (r < 0 && arg_force >= 1)
                         log_notice("Failed to load kexec kernel, continuing without.");