mkhtemp: unified non-error close handling

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-24 07:06:39 +00:00
parent 616099edad
commit e7ede0c755
3 changed files with 41 additions and 47 deletions

View File

@@ -530,6 +530,7 @@ int fsync_on_eintr(int fd);
int fs_rename_at(int olddirfd, const char *old, int fs_rename_at(int olddirfd, const char *old,
int newdirfd, const char *new); int newdirfd, const char *new);
int fs_open(const char *path, int flags); int fs_open(const char *path, int flags);
void close_no_err(int *fd);
struct filesystem *rootfs(void); struct filesystem *rootfs(void);
int fs_resolve_at(int dirfd, const char *path, int flags); int fs_resolve_at(int dirfd, const char *path, int flags);
int fs_next_component(const char **p, int fs_next_component(const char **p,

View File

@@ -732,6 +732,22 @@ try_err(int loop_err, int errval)
return -1; 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 int
close_on_eintr(int fd) close_on_eintr(int fd)
{ {
@@ -1134,3 +1150,14 @@ retry:
return rval; return rval;
} }

View File

@@ -68,7 +68,6 @@ new_tmp_common(int *fd, char **path, int type)
char suffix[] = "tmp.XXXXXXXXXX"; char suffix[] = "tmp.XXXXXXXXXX";
char *tmpdir = NULL; char *tmpdir = NULL;
int close_errno;
size_t dirlen; size_t dirlen;
size_t destlen; size_t destlen;
char *dest = NULL; /* final path (will be written into "path") */ 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) if (*fd < 0)
goto err; goto err;
if (dirfd >= 0) { close_no_err(&dirfd);
close_errno = errno;
(void) close_on_eintr(dirfd);
errno = close_errno;
dirfd = -1;
}
errno = saved_errno; errno = saved_errno;
*path = dest; *path = dest;
@@ -179,19 +173,8 @@ err:
dest = NULL; dest = NULL;
} }
if (dirfd >= 0) { close_no_err(&dirfd);
close_errno = errno; close_no_err(fd);
(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;
}
/* where a TMPDIR isn't found, and we err, /* where a TMPDIR isn't found, and we err,
* we pass this back through for the * 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 && if (st_a.st_dev == st_b.st_dev &&
st_a.st_ino == st_b.st_ino) { st_a.st_ino == st_b.st_ino) {
(void) close_on_eintr(fd_a); close_no_err(&fd_a);
(void) close_on_eintr(fd_b); close_no_err(&fd_b);
success_same_dir: success_same_dir:
@@ -365,8 +348,8 @@ success_same_dir:
return 1; return 1;
} }
(void) close_on_eintr(fd_a); close_no_err(&fd_a);
(void) close_on_eintr(fd_b); close_no_err(&fd_b);
/* FAILURE (logical) /* FAILURE (logical)
*/ */
@@ -379,10 +362,8 @@ err_same_dir:
/* FAILURE (probably syscall) /* FAILURE (probably syscall)
*/ */
if (fd_a >= 0) close_no_err(&fd_a);
(void) close_on_eintr(fd_a); close_no_err(&fd_b);
if (fd_b >= 0)
(void) close_on_eintr(fd_b);
if (errno == saved_errno) if (errno == saved_errno)
errno = EIO; errno = EIO;
@@ -472,9 +453,7 @@ world_writeable_and_sticky(
sticky_heaven: sticky_heaven:
/* i like the one in hamburg better */ /* i like the one in hamburg better */
if (dirfd >= 0) close_no_err(&dirfd);
(void) close_on_eintr(dirfd);
errno = saved_errno; errno = saved_errno;
return 1; return 1;
@@ -486,8 +465,7 @@ sticky_hell:
saved_errno = errno; saved_errno = errno;
if (dirfd >= 0) close_no_err(&dirfd);
(void) close_on_eintr(dirfd);
errno = saved_errno; errno = saved_errno;
@@ -677,12 +655,7 @@ mkhtemp(int *fd,
errno = EEXIST; errno = EEXIST;
err: err:
if (*fd >= 0) { close_no_err(fd);
close_errno = errno;
(void)close_on_eintr(*fd);
errno = close_errno;
*fd = -1;
}
success: success:
@@ -704,7 +677,6 @@ mkhtemp_try_create(int dirfd,
{ {
struct stat st_open; struct stat st_open;
int saved_errno = errno; int saved_errno = errno;
int close_errno;
int rval = -1; int rval = -1;
int file_created = 0; int file_created = 0;
@@ -824,12 +796,7 @@ mkhtemp_try_create(int dirfd,
goto out; goto out;
err: err:
close_errno = errno; close_no_err(fd);
if (fd != NULL && *fd >= 0) {
(void) close_on_eintr(*fd);
*fd = -1;
}
if (file_created) if (file_created)
(void) unlinkat(dirfd, fname_copy, 0); (void) unlinkat(dirfd, fname_copy, 0);
@@ -837,7 +804,6 @@ err:
if (dir_created) if (dir_created)
(void) unlinkat(dirfd, fname_copy, AT_REMOVEDIR); (void) unlinkat(dirfd, fname_copy, AT_REMOVEDIR);
errno = close_errno;
rval = -1; rval = -1;
out: out:
return rval; return rval;