diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-10 09:04:20 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-10 09:04:20 +0000 |
| commit | 21f8d323f436f315650abdb862b28c72695f61b2 (patch) | |
| tree | e2b9c5af9a67173c5e31d1845b8c604bf4b986a0 /util/nvmutil | |
| parent | 5ae5d537511c5bea40346bd47d33c885dabc00d0 (diff) | |
util/nvmutil: portable pread/pwrite
not thread-safe
lucky we're single-threaded!
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/nvmutil')
| -rw-r--r-- | util/nvmutil/nvmutil.c | 26 |
1 files changed, 20 insertions, 6 deletions
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, ...) { |
