]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
reboot-util: cache the result of shall_restore_state 28809/head
authorMike Yuan <me@yhndnzj.com>
Thu, 10 Aug 2023 15:25:59 +0000 (23:25 +0800)
committerMike Yuan <me@yhndnzj.com>
Sun, 13 Aug 2023 04:54:56 +0000 (12:54 +0800)
src/backlight/backlight.c
src/rfkill/rfkill.c
src/shared/reboot-util.c
src/shared/reboot-util.h

index a3842088799af6deebddf351af6d4c693a84d0ca..24459628faf694316c11885bd049cebca52b81aa 100644 (file)
@@ -528,7 +528,7 @@ static int run(int argc, char *argv[]) {
                 _cleanup_free_ char *value = NULL;
                 bool clamp;
 
-                if (shall_restore_state() == 0)
+                if (!shall_restore_state())
                         return 0;
 
                 if (validate_device(device) == 0)
index 96315136286b15df224e825ae5b4f39364d38d80..c4e93737f54f307bd9e4387912529080f7e73a66 100644 (file)
@@ -140,7 +140,7 @@ static int load_state(Context *c, const struct rfkill_event *event) {
         assert(c->rfkill_fd >= 0);
         assert(event);
 
-        if (shall_restore_state() == 0)
+        if (!shall_restore_state())
                 return 0;
 
         r = determine_state_file(event, &state_file);
index 75910ede4413b85f45c2035cc63fe597d8a63fb0..62ff697fe26c4567cd0ee07bc413d21f5f67759f 100644 (file)
@@ -109,15 +109,19 @@ int reboot_with_parameter(RebootFlags flags) {
         return log_full_errno(flags & REBOOT_LOG ? LOG_ERR : LOG_DEBUG, errno, "Failed to reboot: %m");
 }
 
-int shall_restore_state(void) {
-        bool ret;
+bool shall_restore_state(void) {
+        static int cached = -1;
+        bool b = true; /* If nothing specified or the check fails, then defaults to true. */
         int r;
 
-        r = proc_cmdline_get_bool("systemd.restore_state", /* flags = */ 0, &ret);
+        if (cached >= 0)
+                return cached;
+
+        r = proc_cmdline_get_bool("systemd.restore_state", PROC_CMDLINE_TRUE_WHEN_MISSING, &b);
         if (r < 0)
-                return r;
+                log_debug_errno(r, "Failed to parse systemd.restore_state= kernel command line option, ignoring: %m");
 
-        return r > 0 ? ret : true;
+        return (cached = b);
 }
 
 static int xen_kexec_loaded(void) {
index 9e6366206e56030b4e447a7d0c02b88c32578d5e..ccd15c7b354ade540a3652e1aa945b91cefdfdd1 100644 (file)
@@ -12,6 +12,6 @@ typedef enum RebootFlags {
 int read_reboot_parameter(char **parameter);
 int reboot_with_parameter(RebootFlags flags);
 
-int shall_restore_state(void);
+bool shall_restore_state(void);
 
 bool kexec_loaded(void);