Skip to content

Commit

Permalink
efivarfs.c: Call get_efivarfs_path() from a constructor
Browse files Browse the repository at this point in the history
To avoid having problems when the environment changes (due to setenv()
or whatever), ensure that there's an initial call to
get_efivarfs_path(), and that it duplicates the result into a freshly
allocated string.

Fixes github issue rhboot#154

Signed-off-by: Peter Jones <pjones@redhat.com>
  • Loading branch information
vathpela committed Oct 1, 2020
1 parent 65a3ad0 commit a5b825a
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/efivarfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "fix_coverity.h"

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/magic.h>
Expand All @@ -28,18 +29,36 @@
#endif

static char const default_efivarfs_path[] = "/sys/firmware/efi/efivars/";
static char *efivarfs_path;

static char const *
get_efivarfs_path(void)
{
static const char *path;
if (path)
return path;
if (efivarfs_path)
return efivarfs_path;

efivarfs_path = secure_getenv("EFIVARFS_PATH");
if (efivarfs_path)
efivarfs_path = strdup(efivarfs_path);
else
efivarfs_path = strdup(default_efivarfs_path);
return efivarfs_path;
}

path = getenv("EFIVARFS_PATH");
if (!path)
path = default_efivarfs_path;
return path;
static void CONSTRUCTOR
init_efivarfs_path(void)
{
if (!get_efivarfs_path())
err(1, "couldn't allocate memory");
}

static void DESTRUCTOR
fini_efivarfs_path(void)
{
if (efivarfs_path) {
free(efivarfs_path);
efivarfs_path = NULL;
}
}

static int
Expand Down

0 comments on commit a5b825a

Please sign in to comment.