Skip to content

Commit

Permalink
efi: cache test results of boolean EFI state functions
Browse files Browse the repository at this point in the history
EFI variable access is nowadays subject to rate limiting by the kernel.
Thus, let's cache the results of checking them, in order to minimize how
often we access them.

Fixes: systemd#14828
  • Loading branch information
poettering committed Apr 30, 2020
1 parent d47df15 commit f46ba93
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/basic/efivars.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,16 @@ int efi_set_variable_string(sd_id128_t vendor, const char *name, const char *v)
}

bool is_efi_boot(void) {
if (detect_container() > 0)
return false;
static int cache = -1;

return access("/sys/firmware/efi/", F_OK) >= 0;
if (cache < 0) {
if (detect_container() > 0)
cache = false;
else
cache = access("/sys/firmware/efi/", F_OK) >= 0;
}

return cache;
}

static int read_flag(const char *varname) {
Expand All @@ -271,11 +277,21 @@ static int read_flag(const char *varname) {
}

bool is_efi_secure_boot(void) {
return read_flag("SecureBoot") > 0;
static int cache = -1;

if (cache < 0)
cache = read_flag("SecureBoot");

return cache > 0;
}

bool is_efi_secure_boot_setup_mode(void) {
return read_flag("SetupMode") > 0;
static int cache = -1;

if (cache < 0)
cache = read_flag("SetupMode");

return cache > 0;
}

int systemd_efi_options_variable(char **line) {
Expand Down

0 comments on commit f46ba93

Please sign in to comment.