diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-28 09:03:18 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-28 09:03:18 +0000 |
| commit | 6643d9c1fa4d62e648d93ffc2841465d6422da22 (patch) | |
| tree | 197ca3e8add5b89609618d9938b0fc4cfe2f4ab5 /util/libreboot-utils/lib | |
| parent | 4ecdadb7a601b0613e65bf8547d17b39ed87153f (diff) | |
lbutils: unify xopen and open_on_eintr
use open_on_eintr for gbe files
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib')
| -rw-r--r-- | util/libreboot-utils/lib/file.c | 34 | ||||
| -rw-r--r-- | util/libreboot-utils/lib/io.c | 12 | ||||
| -rw-r--r-- | util/libreboot-utils/lib/rand.c | 2 |
3 files changed, 25 insertions, 23 deletions
diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c index 96654640..4623748c 100644 --- a/util/libreboot-utils/lib/file.c +++ b/util/libreboot-utils/lib/file.c @@ -65,22 +65,6 @@ err_same_file: return set_errno(saved_errno, ESTALE); } -void -xopen(int *fd_ptr, const char *path, int flags, struct stat *st) -{ - if ((*fd_ptr = open(path, flags)) < 0) - err_exit(errno, "%s", path); - - if (fstat(*fd_ptr, st) < 0) - err_exit(errno, "%s: stat", path); - - if (!S_ISREG(st->st_mode)) - err_exit(errno, "%s: not a regular file", path); - - if (lseek_on_eintr(*fd_ptr, 0, SEEK_CUR, 1, 1) == (off_t)-1) - err_exit(errno, "%s: file not seekable", path); -} - int fsync_dir(const char *path) { @@ -568,8 +552,9 @@ free_and_set_null(char **buf) } void -open_on_eintr(char *path, - int *fd, int flags, mode_t mode) +open_on_eintr(const char *path, + int *fd, int flags, mode_t mode, + struct stat *st) { int r = -1; int saved_errno = errno; @@ -594,6 +579,17 @@ open_on_eintr(char *path, *fd = r; + if (st != NULL) { + if (fstat(*fd, st) < 0) + err_exit(errno, "%s: stat", path); + + if (!S_ISREG(st->st_mode)) + err_exit(errno, "%s: not a regular file", path); + } + + if (lseek_on_eintr(*fd, 0, SEEK_CUR, 1, 1) == (off_t)-1) + err_exit(errno, "%s: file not seekable", path); + errno = saved_errno; } @@ -683,7 +679,7 @@ rootfs(void) global_fs.rootfd = -1; open_on_eintr("/", &global_fs.rootfd, - O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0400); + O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0400, NULL); if (global_fs.rootfd < 0) return NULL; diff --git a/util/libreboot-utils/lib/io.c b/util/libreboot-utils/lib/io.c index d3f5733d..a09e3992 100644 --- a/util/libreboot-utils/lib/io.c +++ b/util/libreboot-utils/lib/io.c @@ -27,9 +27,12 @@ open_gbe_file(void) int _flags; - xopen(&f->gbe_fd, f->fname, - cmd->flags | O_BINARY | - O_NOFOLLOW | O_CLOEXEC | O_NOCTTY, &f->gbe_st); + f->gbe_fd = -1; + + open_on_eintr(f->fname, &f->gbe_fd, + O_NOFOLLOW | O_CLOEXEC | O_NOCTTY, + ((cmd->flags & O_ACCMODE) == O_RDONLY) ? 0400 : 0600, + &f->gbe_st); if (f->gbe_st.st_nlink > 1) err_exit(EINVAL, @@ -62,8 +65,11 @@ open_gbe_file(void) err_exit(EINVAL, "File size must be 8KB, 16KB or 128KB"); } +/* currently fails (EBADF), locks are advisory anyway: */ +/* if (lock_file(f->gbe_fd, cmd->flags) == -1) err_exit(errno, "%s: can't lock", f->fname); +*/ } void diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index 030ca5ec..9edc8a5b 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -142,7 +142,7 @@ rset(void *buf, size_t n) #if defined(USE_URANDOM) && \ ((USE_URANDOM) > 0) int fd = -1; - open_on_eintr("/dev/urandom", &fd, O_RDONLY, 0400); + open_on_eintr("/dev/urandom", &fd, O_RDONLY, 0400, NULL); retry_rand: if ((rc = read(fd, (unsigned char *)buf + off, n - off)) < 0) { #elif defined(__linux__) |
