summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-13 15:48:18 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-13 15:52:28 +0000
commitd894240574ce969b58a51e54926a7e62b680bcd2 (patch)
treeb183055ba06485a8f5b082373f039bbaacb67b38
parent8ca953ec79a80fe7c3a853aff65c4f4cc822bb9d (diff)
util/nvmutil: lock gbe file
Signed-off-by: Leah Rowe <leah@libreboot.org>
-rw-r--r--util/nvmutil/nvmutil.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index b6a8b600..efedb07d 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -253,6 +253,7 @@ static int xstrxcmp(const char *a, const char *b, size_t maxlen);
*/
static void open_dev_urandom(void);
static void open_gbe_file(void);
+static void lock_gbe_file(void);
static void xopen(int *fd, const char *path, int flags, struct stat *st);
/*
@@ -652,6 +653,7 @@ main(int argc, char *argv[])
open_dev_urandom();
open_gbe_file();
+ lock_gbe_file();
#ifdef NVMUTIL_PLEDGE
if (pledge("stdio", NULL) == -1)
@@ -881,6 +883,24 @@ open_gbe_file(void)
}
static void
+lock_gbe_file(void)
+{
+ struct flock fl;
+
+ memset(&fl, 0, sizeof(fl));
+
+ if (command[cmd_index].flags == O_RDONLY)
+ fl.l_type = F_RDLCK;
+ else
+ fl.l_type = F_WRLCK;
+
+ fl.l_whence = SEEK_SET;
+
+ if (fcntl(gbe_fd, F_SETLK, &fl) == -1)
+ err(errno, "file is locked by another process");
+}
+
+static void
xopen(int *fd_ptr, const char *path, int flags, struct stat *st)
{
if ((*fd_ptr = open(path, flags)) == -1)