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 #154

Signed-off-by: Peter Jones <pjones@redhat.com>
  • Loading branch information
vathpela committed Oct 15, 2020
1 parent 57f30dc commit ff6db32
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 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,39 @@
#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);

if (!efivarfs_path)
err(1, "couldn't allocate memory");

return efivarfs_path;
}

path = getenv("EFIVARFS_PATH");
if (!path)
path = default_efivarfs_path;
return path;
static void CONSTRUCTOR
init_efivarfs_path(void)
{
get_efivarfs_path();
}

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

static int
Expand Down

0 comments on commit ff6db32

Please sign in to comment.