]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hibernate-resume-generator: don't initiate resume if soft-rebooted 34755/head
authorMike Yuan <me@yhndnzj.com>
Mon, 14 Oct 2024 16:31:14 +0000 (18:31 +0200)
committerMike Yuan <me@yhndnzj.com>
Mon, 14 Oct 2024 23:18:26 +0000 (01:18 +0200)
This is just paranoia, to ensure that we don't accidentally
initiate resume if the initrd is entered through soft-reboot
rather than the initial one for booting up.

src/hibernate-resume/hibernate-resume-generator.c
src/shared/generator.c
src/shared/generator.h

index 01684283942f902cf074b0e0b30a8ac814d21d5b..2b175342c149f505e0a9bbef144a36f713bf3fd1 100644 (file)
@@ -107,6 +107,11 @@ static int run(const char *dest, const char *dest_early, const char *dest_late)
                 return 0;
         }
 
+        if (generator_soft_rebooted()) {
+                log_debug("Running in an initrd entered through soft-reboot, not initiating resume.");
+                return 0;
+        }
+
         r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
         if (r < 0)
                 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
index bff44cfc15d82cd13dfcd2b9a470e10597e508e9..b3e57770aa7f6c19084f49c0dcab6e5efffdf936 100644 (file)
@@ -16,6 +16,7 @@
 #include "macro.h"
 #include "mkdir-label.h"
 #include "mountpoint-util.h"
+#include "parse-util.h"
 #include "path-util.h"
 #include "process-util.h"
 #include "special.h"
@@ -974,3 +975,25 @@ void log_setup_generator(void) {
         log_parse_environment();
         log_open();
 }
+
+bool generator_soft_rebooted(void) {
+        static int cached = -1;
+        int r;
+
+        if (cached >= 0)
+                return cached;
+
+        const char *e = secure_getenv("SYSTEMD_SOFT_REBOOTS_COUNT");
+        if (!e)
+                return (cached = false);
+
+        unsigned u;
+
+        r = safe_atou(e, &u);
+        if (r < 0) {
+                log_debug_errno(r, "Failed to parse $SYSTEMD_SOFT_REBOOTS_COUNT, assuming the system hasn't soft-rebooted: %m");
+                return (cached = false);
+        }
+
+        return (cached = u > 0);
+}
index baf1dafca3fb791fc0758e9d88dda5e3177a39dc..f07d7d6b3ae5d7f076d97305e31dbcd0e893c8ee 100644 (file)
@@ -100,6 +100,8 @@ int generator_enable_remount_fs_service(const char *dir);
 
 void log_setup_generator(void);
 
+bool generator_soft_rebooted(void);
+
 /* Similar to DEFINE_MAIN_FUNCTION, but initializes logging and assigns positional arguments. */
 #define DEFINE_MAIN_GENERATOR_FUNCTION(impl)                            \
         _DEFINE_MAIN_FUNCTION(                                          \