summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-24 07:06:39 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-24 07:06:39 +0000
commite7ede0c75570ddd57b9d55e33764c3bdd74274b1 (patch)
treeba484c1ad22bf6c3e531b9a7f4966cd16a6075c6
parent616099edade1d43893564177cbd31ff5f1c7eea0 (diff)
mkhtemp: unified non-error close handling
Signed-off-by: Leah Rowe <leah@libreboot.org>
-rw-r--r--util/libreboot-utils/include/common.h1
-rw-r--r--util/libreboot-utils/lib/file.c27
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c60
3 files changed, 41 insertions, 47 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h
index fa07da7f..bdd625f8 100644
--- a/util/libreboot-utils/include/common.h
+++ b/util/libreboot-utils/include/common.h
@@ -530,6 +530,7 @@ int fsync_on_eintr(int fd);
int fs_rename_at(int olddirfd, const char *old,
int newdirfd, const char *new);
int fs_open(const char *path, int flags);
+void close_no_err(int *fd);
struct filesystem *rootfs(void);
int fs_resolve_at(int dirfd, const char *path, int flags);
int fs_next_component(const char **p,
diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c
index ea2bcd0b..00cab2e9 100644
--- a/util/libreboot-utils/lib/file.c
+++ b/util/libreboot-utils/lib/file.c
@@ -732,6 +732,22 @@ try_err(int loop_err, int errval)
return -1;
}
+void
+close_no_err(int *fd)
+{
+ int saved_errno = errno;
+
+ if (fd == NULL)
+ return;
+ if (*fd < 0)
+ return;
+
+ (void) close_on_eintr(*fd);
+ *fd = -1;
+
+ errno = saved_errno;
+}
+
int
close_on_eintr(int fd)
{
@@ -1134,3 +1150,14 @@ retry:
return rval;
}
+
+
+
+
+
+
+
+
+
+
+
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index 84723a08..8f54b045 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -68,7 +68,6 @@ new_tmp_common(int *fd, char **path, int type)
char suffix[] = "tmp.XXXXXXXXXX";
char *tmpdir = NULL;
- int close_errno;
size_t dirlen;
size_t destlen;
char *dest = NULL; /* final path (will be written into "path") */
@@ -155,12 +154,7 @@ new_tmp_common(int *fd, char **path, int type)
if (*fd < 0)
goto err;
- if (dirfd >= 0) {
- close_errno = errno;
- (void) close_on_eintr(dirfd);
- errno = close_errno;
- dirfd = -1;
- }
+ close_no_err(&dirfd);
errno = saved_errno;
*path = dest;
@@ -179,19 +173,8 @@ err:
dest = NULL;
}
- if (dirfd >= 0) {
- close_errno = errno;
- (void) close_on_eintr(dirfd);
- errno = close_errno;
- dirfd = -1;
- }
-
- if (*fd >= 0) {
- close_errno = errno;
- (void) close_on_eintr(*fd);
- errno = close_errno;
- *fd = -1;
- }
+ close_no_err(&dirfd);
+ close_no_err(fd);
/* where a TMPDIR isn't found, and we err,
* we pass this back through for the
@@ -353,8 +336,8 @@ same_dir(const char *a, const char *b)
if (st_a.st_dev == st_b.st_dev &&
st_a.st_ino == st_b.st_ino) {
- (void) close_on_eintr(fd_a);
- (void) close_on_eintr(fd_b);
+ close_no_err(&fd_a);
+ close_no_err(&fd_b);
success_same_dir:
@@ -365,8 +348,8 @@ success_same_dir:
return 1;
}
- (void) close_on_eintr(fd_a);
- (void) close_on_eintr(fd_b);
+ close_no_err(&fd_a);
+ close_no_err(&fd_b);
/* FAILURE (logical)
*/
@@ -379,10 +362,8 @@ err_same_dir:
/* FAILURE (probably syscall)
*/
- if (fd_a >= 0)
- (void) close_on_eintr(fd_a);
- if (fd_b >= 0)
- (void) close_on_eintr(fd_b);
+ close_no_err(&fd_a);
+ close_no_err(&fd_b);
if (errno == saved_errno)
errno = EIO;
@@ -472,9 +453,7 @@ world_writeable_and_sticky(
sticky_heaven:
/* i like the one in hamburg better */
- if (dirfd >= 0)
- (void) close_on_eintr(dirfd);
-
+ close_no_err(&dirfd);
errno = saved_errno;
return 1;
@@ -486,8 +465,7 @@ sticky_hell:
saved_errno = errno;
- if (dirfd >= 0)
- (void) close_on_eintr(dirfd);
+ close_no_err(&dirfd);
errno = saved_errno;
@@ -677,12 +655,7 @@ mkhtemp(int *fd,
errno = EEXIST;
err:
- if (*fd >= 0) {
- close_errno = errno;
- (void)close_on_eintr(*fd);
- errno = close_errno;
- *fd = -1;
- }
+ close_no_err(fd);
success:
@@ -704,7 +677,6 @@ mkhtemp_try_create(int dirfd,
{
struct stat st_open;
int saved_errno = errno;
- int close_errno;
int rval = -1;
int file_created = 0;
@@ -824,12 +796,7 @@ mkhtemp_try_create(int dirfd,
goto out;
err:
- close_errno = errno;
-
- if (fd != NULL && *fd >= 0) {
- (void) close_on_eintr(*fd);
- *fd = -1;
- }
+ close_no_err(fd);
if (file_created)
(void) unlinkat(dirfd, fname_copy, 0);
@@ -837,7 +804,6 @@ err:
if (dir_created)
(void) unlinkat(dirfd, fname_copy, AT_REMOVEDIR);
- errno = close_errno;
rval = -1;
out:
return rval;