From 21f8d323f436f315650abdb862b28c72695f61b2 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Tue, 10 Mar 2026 09:04:20 +0000 Subject: util/nvmutil: portable pread/pwrite not thread-safe lucky we're single-threaded! Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'util/nvmutil/nvmutil.c') diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 4ab6018a..9e8a4a1c 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -216,6 +216,7 @@ static void rw_file_exact(int fd, uint8_t *mem, size_t len, off_t off, int rw_type, const char *path, const char *rw_type_str); static ssize_t prw(int fd, void *mem, size_t count, off_t offset, int rw_type, const char *path); +static off_t lseek_eintr(int fd, off_t offset, int whence); /* * Error handling and cleanup @@ -1397,14 +1398,13 @@ static ssize_t prw(int fd, void *mem, size_t count, off_t offset, int rw_type, const char *path) { + off_t rs; off_t old; ssize_t r; - old = lseek(fd, 0, SEEK_CUR); - if (old == (off_t)-1) + if ((old = lseek_eintr(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) return -1; - - if (lseek(fd, offset, SEEK_SET) == (off_t)-1) + if ((r = lseek_eintr(fd, offset, SEEK_SET)) == (off_t)-1) return -1; do { @@ -1416,12 +1416,26 @@ prw(int fd, void *mem, size_t count, err(EIO, "%s: Invalid rw_type", path); } while (r < 0 && errno == EINTR); - if (lseek(fd, old, SEEK_SET) == (off_t)-1 && r >= 0) - errno = EIO; + if (r >= 0) { + if ((rs = lseek_eintr(fd, old, SEEK_SET)) == (off_t)-1) + errno = EIO; + } return r; } +static off_t +lseek_eintr(int fd, off_t offset, int whence) +{ + off_t old; + + do { + old = lseek(fd, offset, whence); + } while (old == (off_t)-1 && errno == EINTR); + + return old; +} + static void err(int nvm_errval, const char *msg, ...) { -- cgit v1.2.1