util/nvmutil: fix faulty fd check

i screwed up in an earlier commit

this change fixes a bug where on rhex(), each
call would re-open /dev/urandom, resetting rfd

Signed-off-by: Leah Rowe <leah@libreboot.org>
fsdg20230625
Leah Rowe 2023-06-01 12:58:33 +01:00
parent b49da12dad
commit adf3aece6f
2 changed files with 6 additions and 5 deletions

View File

@ -141,9 +141,12 @@ rhex(void)
{ {
static int rfd = -1, n = 0; static int rfd = -1, n = 0;
static uint8_t rnum[16]; static uint8_t rnum[16];
if (!n) {
if (rfd == -1) {
xopen(rfd, "/dev/urandom", O_RDONLY); xopen(rfd, "/dev/urandom", O_RDONLY);
if (!n) }
xpread(rfd, (uint8_t *) &rnum, (n = 15) + 1, 0, "/dev/urandom"); xpread(rfd, (uint8_t *) &rnum, (n = 15) + 1, 0, "/dev/urandom");
}
return rnum[n--] & 0xf; return rnum[n--] & 0xf;
} }

View File

@ -51,9 +51,7 @@ int big_endian;
#define ERR() errno = errno ? errno : ECANCELED #define ERR() errno = errno ? errno : ECANCELED
#define err_if(x) if (x) err(ERR(), NULL) #define err_if(x) if (x) err(ERR(), NULL)
#define xopen(f,l,p) \ #define xopen(f,l,p) if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
if (f == -1) \
if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \ if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \
struct stat st; if (fstat(f, &st) == -1) err(ERR(), "%s", l) struct stat st; if (fstat(f, &st) == -1) err(ERR(), "%s", l)
#define xpread(f, b, n, o, l) if (pread(f, b, n, o) == -1) err(ERR(), "%s", l) #define xpread(f, b, n, o, l) if (pread(f, b, n, o) == -1) err(ERR(), "%s", l)