summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/file.c
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-28 07:30:55 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-28 07:30:55 +0000
commit63984a4a6abb7a65098f27196fcb6395fc0ada22 (patch)
tree3c7e6e562d82f735f2b27201a0b23abc9afb367e /util/libreboot-utils/lib/file.c
parentfd26c6e63163d9811a89c3a5ca27a9aa2f61b09f (diff)
libreboot-utils: much stricter close() handling
remove close_warn and close_no_err make close_on_eintr a void, and abort on error instead of returning -1. a failed file closure is a world-ending event. burn accordingly. Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/file.c')
-rw-r--r--util/libreboot-utils/lib/file.c76
1 files changed, 18 insertions, 58 deletions
diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c
index fbb4e53f..2b1adca8 100644
--- a/util/libreboot-utils/lib/file.c
+++ b/util/libreboot-utils/lib/file.c
@@ -146,11 +146,7 @@ fsync_dir(const char *path)
if_err_sys(fsync_on_eintr(dirfd) == -1))
goto err_fsync_dir;
- if (close_on_eintr(dirfd) == -1) {
-
- dirfd = -1;
- goto err_fsync_dir;
- }
+ close_on_eintr(&dirfd);
free_and_set_null(&dirbuf);
@@ -163,7 +159,7 @@ err_fsync_dir:
errno = EIO;
free_and_set_null(&dirbuf);
- close_no_err(&dirfd);
+ close_on_eintr(&dirfd);
return -1;
}
@@ -589,66 +585,30 @@ free_and_set_null(char **buf)
*buf = NULL;
}
-/* also returns error code */
-int
-close_warn(int *fd, char *s)
-{
- int saved_errno = errno;
-
- if (fd == NULL) {
- if (s != NULL)
- fprintf(stderr, "FAIL: %s: bad fd ptr\n", s);
- return -1;
- }
-
- if (*fd < 0 && s != NULL) {
- fprintf(stderr, "WARN: %s: already closed\n", s);
- } else if (close(*fd) < 0) {
- if (s != NULL)
- fprintf(stderr, "FAIL: %s: close\n", s);
- return -1;
- }
-
- *fd = -1;
- errno = saved_errno;
-
- return 0;
-}
-
-/* TODO: remove this, and just check
- * err on every close. */
void
-close_no_err(int *fd)
+close_on_eintr(int *fd)
{
+ int r;
int saved_errno = errno;
- if (fd == NULL || *fd < 0)
- return;
+ if (fd == NULL)
+ err_exit(EINVAL, "close_on_eintr: null pointer");
- (void) close_on_eintr(*fd);
- *fd = -1;
-
- errno = saved_errno;
-}
-
-/* TODO: make fd a pointer insttead
- and automatically reset -1 here */
-int
-close_on_eintr(int fd)
-{
- int r;
- int saved_errno = errno;
+ if (*fd < 0)
+ return;
do {
- r = close(fd);
+ r = close(*fd);
} while (r == -1 && (
errno == EINTR || errno == EAGAIN ||
errno == EWOULDBLOCK || errno == ETXTBSY));
- if (r >= 0)
- errno = saved_errno;
+ if (r < 0)
+ err_exit(errno, "close_on_eintr: could not close");
- return r;
+ *fd = -1;
+
+ errno = saved_errno;
}
int
@@ -761,7 +721,7 @@ fs_resolve_at(int dirfd, const char *path, int flags)
/* close previous fd if not the original input */
if (curfd != dirfd)
- (void) close_on_eintr(curfd);
+ close_on_eintr(&curfd);
curfd = nextfd;
nextfd = -1;
@@ -774,11 +734,11 @@ err:
saved_errno = errno;
if (nextfd >= 0)
- (void) close_on_eintr(nextfd);
+ close_on_eintr(&nextfd);
/* close curfd only if it's not the original */
if (curfd != dirfd && curfd >= 0)
- (void) close_on_eintr(curfd);
+ close_on_eintr(&curfd);
errno = saved_errno;
return -1;
@@ -847,7 +807,7 @@ fs_open_component(int dirfd, const char *name,
if (!S_ISDIR(st.st_mode)) {
- (void) close_on_eintr(fd);
+ close_on_eintr(&fd);
errno = ENOTDIR;
return -1;
}