summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-10 14:25:43 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-10 14:25:43 +0000
commit4bc7ba1e4b95ac9f1501401fee97bdab7fbf475c (patch)
tree972e6125a1b4d0745bf935cb1c2e970ef60d9544
parentc953228bb002059faa92b9e000303099d80b9091 (diff)
util/nvmutil: use O_NONBLOCK on /dev/[u]random
on some systems, it is otherwise blocking, but blocking can be disabled, making access more reliable. Signed-off-by: Leah Rowe <leah@libreboot.org>
-rw-r--r--util/nvmutil/nvmutil.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 0101888e..d5f0c2b1 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -101,6 +101,10 @@ typedef char static_assert_off_t_is_32[(sizeof(off_t) >= 4) ? 1 : -1];
#define O_BINARY 0
#endif
+#ifndef O_NONBLOCK
+#define O_NONBLOCK 0
+#endif
+
/*
* Sanitize command tables.
*/
@@ -708,11 +712,15 @@ static void
open_dev_urandom(void)
{
rname = newrandom;
- if ((urandom_fd = open(rname, O_RDONLY | O_BINARY)) != -1)
+ urandom_fd = open(rname, O_RDONLY | O_BINARY | O_NONBLOCK);
+ if (urandom_fd != -1)
return;
/*
* Fall back to /dev/random on very old Unix.
+ *
+ * We must reset errno, to remove stale state
+ * set by reading /dev/urandom
*/
fprintf(stderr, "Can't open %s (will use %s instead)\n",
@@ -721,7 +729,7 @@ open_dev_urandom(void)
errno = 0;
rname = oldrandom;
- xopen(&urandom_fd, rname, O_RDONLY | O_BINARY, &gbe_st);
+ xopen(&urandom_fd, rname, O_RDONLY | O_BINARY | O_NONBLOCK, &gbe_st);
}
static void