From: Mike Yuan Date: Thu, 10 Aug 2023 15:25:59 +0000 (+0800) Subject: reboot-util: cache the result of shall_restore_state X-Git-Tag: v255-rc1~735^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F28809%2Fhead;p=thirdparty%2Fsystemd.git reboot-util: cache the result of shall_restore_state --- diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c index a3842088799..24459628faf 100644 --- a/src/backlight/backlight.c +++ b/src/backlight/backlight.c @@ -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) diff --git a/src/rfkill/rfkill.c b/src/rfkill/rfkill.c index 96315136286..c4e93737f54 100644 --- a/src/rfkill/rfkill.c +++ b/src/rfkill/rfkill.c @@ -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); diff --git a/src/shared/reboot-util.c b/src/shared/reboot-util.c index 75910ede441..62ff697fe26 100644 --- a/src/shared/reboot-util.c +++ b/src/shared/reboot-util.c @@ -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) { diff --git a/src/shared/reboot-util.h b/src/shared/reboot-util.h index 9e6366206e5..ccd15c7b354 100644 --- a/src/shared/reboot-util.h +++ b/src/shared/reboot-util.h @@ -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);