From 49cac232d8e3cc6147a530d4f7971832f6b869b5 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 28 Mar 2026 08:09:14 +0000 Subject: libreboot-utils: much stricter open() handling abort on error, and do EINTR looping Signed-off-by: Leah Rowe --- util/libreboot-utils/lib/file.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'util/libreboot-utils/lib/file.c') diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c index 3c7a3ba2..5d656e0e 100644 --- a/util/libreboot-utils/lib/file.c +++ b/util/libreboot-utils/lib/file.c @@ -589,6 +589,36 @@ free_and_set_null(char **buf) *buf = NULL; } +void +open_on_eintr(char *path, + int *fd, int flags, mode_t mode) +{ + int r = -1; + int saved_errno = errno; + + if (path == NULL) + err_exit(EINVAL, "open_on_eintr: null path"); + + if (fd == NULL) + err_exit(EFAULT, "%s: open_on_eintr: null fd ptr", path); + + if (*fd >= 0) + err_exit(EBADF, "%s: open_on_eintr: file already open", path); + + do { + r = open(path, flags, mode); + } while (r == -1 && ( + errno == EINTR || errno == EAGAIN || + errno == EWOULDBLOCK || errno == ETXTBSY)); + + if (r < 0) + err_exit(errno, "%s: open_on_eintr: could not close", path); + + *fd = r; + + errno = saved_errno; +} + void close_on_eintr(int *fd) { @@ -672,8 +702,10 @@ rootfs(void) if (!fs_initialised) { - global_fs.rootfd = - open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC); + global_fs.rootfd = -1; + + open_on_eintr("/", &global_fs.rootfd, + O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0400); if (global_fs.rootfd < 0) return NULL; -- cgit v1.2.1