mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-11 05:52:36 +02:00
lbutils/file: don't loop EINTR on close()
state is undefined after EINTR. just abort universally. Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
@@ -525,7 +525,7 @@ int secure_file(int *fd,
|
|||||||
int check_seek,
|
int check_seek,
|
||||||
int do_lock,
|
int do_lock,
|
||||||
mode_t mode);
|
mode_t mode);
|
||||||
void close_on_eintr(int *fd);
|
void xclose(int *fd);
|
||||||
int fsync_on_eintr(int fd);
|
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);
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ fsync_dir(const char *path)
|
|||||||
if_err_sys((rval = fsync_on_eintr(dirfd)) == -1))
|
if_err_sys((rval = fsync_on_eintr(dirfd)) == -1))
|
||||||
goto err_fsync_dir;
|
goto err_fsync_dir;
|
||||||
|
|
||||||
close_on_eintr(&dirfd);
|
xclose(&dirfd);
|
||||||
free_and_set_null(&dirbuf);
|
free_and_set_null(&dirbuf);
|
||||||
|
|
||||||
reset_caller_errno(rval);
|
reset_caller_errno(rval);
|
||||||
@@ -118,7 +118,7 @@ fsync_dir(const char *path)
|
|||||||
|
|
||||||
err_fsync_dir:
|
err_fsync_dir:
|
||||||
free_and_set_null(&dirbuf);
|
free_and_set_null(&dirbuf);
|
||||||
close_on_eintr(&dirfd);
|
xclose(&dirfd);
|
||||||
|
|
||||||
return with_fallback_errno(EIO);
|
return with_fallback_errno(EIO);
|
||||||
}
|
}
|
||||||
@@ -419,7 +419,7 @@ fs_resolve_at(int dirfd, const char *path, int flags)
|
|||||||
|
|
||||||
/* close previous fd if not the original input */
|
/* close previous fd if not the original input */
|
||||||
if (curfd != dirfd)
|
if (curfd != dirfd)
|
||||||
close_on_eintr(&curfd);
|
xclose(&curfd);
|
||||||
|
|
||||||
curfd = nextfd;
|
curfd = nextfd;
|
||||||
nextfd = -1;
|
nextfd = -1;
|
||||||
@@ -432,11 +432,11 @@ err:
|
|||||||
saved_errno = errno;
|
saved_errno = errno;
|
||||||
|
|
||||||
if (nextfd >= 0)
|
if (nextfd >= 0)
|
||||||
close_on_eintr(&nextfd);
|
xclose(&nextfd);
|
||||||
|
|
||||||
/* close curfd only if it's not the original */
|
/* close curfd only if it's not the original */
|
||||||
if (curfd != dirfd && curfd >= 0)
|
if (curfd != dirfd && curfd >= 0)
|
||||||
close_on_eintr(&curfd);
|
xclose(&curfd);
|
||||||
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
return with_fallback_errno(EIO);
|
return with_fallback_errno(EIO);
|
||||||
@@ -721,22 +721,19 @@ fsync_on_eintr(int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
close_on_eintr(int *fd)
|
xclose(int *fd)
|
||||||
{
|
{
|
||||||
int saved_errno = errno;
|
int saved_errno = errno;
|
||||||
int rval = 0;
|
int rval = 0;
|
||||||
|
|
||||||
if (fd == NULL)
|
if (fd == NULL)
|
||||||
exitf("close_on_eintr: null pointer");
|
exitf("xclose: null pointer");
|
||||||
if (*fd < 0)
|
if (*fd < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
while (fs_retry(saved_errno,
|
if ((rval = close(*fd)) < 0)
|
||||||
rval = close(*fd)));
|
exitf("xclose: could not close");
|
||||||
|
|
||||||
if (rval < 0)
|
|
||||||
exitf("close_on_eintr: could not close");
|
|
||||||
|
|
||||||
*fd = -1;
|
*fd = -1;
|
||||||
|
|
||||||
|
|||||||
@@ -251,8 +251,8 @@ write_to_gbe_bin(void)
|
|||||||
|
|
||||||
saved_errno = errno;
|
saved_errno = errno;
|
||||||
|
|
||||||
close_on_eintr(&f->tmp_fd);
|
xclose(&f->tmp_fd);
|
||||||
close_on_eintr(&f->gbe_fd);
|
xclose(&f->gbe_fd);
|
||||||
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
|
|
||||||
@@ -437,7 +437,7 @@ gbe_mv(void)
|
|||||||
tmp_gbe_bin_exists = 0;
|
tmp_gbe_bin_exists = 0;
|
||||||
|
|
||||||
if (f->gbe_fd > -1) {
|
if (f->gbe_fd > -1) {
|
||||||
close_on_eintr(&f->gbe_fd);
|
xclose(&f->gbe_fd);
|
||||||
|
|
||||||
if (fsync_dir(f->fname) < 0) {
|
if (fsync_dir(f->fname) < 0) {
|
||||||
f->io_err_gbe_bin = 1;
|
f->io_err_gbe_bin = 1;
|
||||||
@@ -445,7 +445,7 @@ gbe_mv(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close_on_eintr(&f->tmp_fd);
|
xclose(&f->tmp_fd);
|
||||||
|
|
||||||
/* before this function is called,
|
/* before this function is called,
|
||||||
* tmp_fd may have been moved
|
* tmp_fd may have been moved
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ new_tmp_common(int *fd, char **path, int type,
|
|||||||
if (*fd < 0)
|
if (*fd < 0)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
close_on_eintr(&dirfd);
|
xclose(&dirfd);
|
||||||
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
*path = dest;
|
*path = dest;
|
||||||
@@ -145,8 +145,8 @@ new_tmp_common(int *fd, char **path, int type,
|
|||||||
err:
|
err:
|
||||||
free_and_set_null(&dest);
|
free_and_set_null(&dest);
|
||||||
|
|
||||||
close_on_eintr(&dirfd);
|
xclose(&dirfd);
|
||||||
close_on_eintr(fd);
|
xclose(fd);
|
||||||
|
|
||||||
/* 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
|
||||||
@@ -290,8 +290,8 @@ success_same_dir:
|
|||||||
rval = 1; /* SUCCESS */
|
rval = 1; /* SUCCESS */
|
||||||
}
|
}
|
||||||
|
|
||||||
close_on_eintr(&fd_a);
|
xclose(&fd_a);
|
||||||
close_on_eintr(&fd_b);
|
xclose(&fd_b);
|
||||||
|
|
||||||
/* we reset caller errno regardless
|
/* we reset caller errno regardless
|
||||||
* of success, so long as it's not
|
* of success, so long as it's not
|
||||||
@@ -303,8 +303,8 @@ success_same_dir:
|
|||||||
err_same_dir:
|
err_same_dir:
|
||||||
/* FAILURE (probably syscall) - returns -1
|
/* FAILURE (probably syscall) - returns -1
|
||||||
*/
|
*/
|
||||||
close_on_eintr(&fd_a);
|
xclose(&fd_a);
|
||||||
close_on_eintr(&fd_b);
|
xclose(&fd_b);
|
||||||
|
|
||||||
return with_fallback_errno(EIO); /* -1 */
|
return with_fallback_errno(EIO); /* -1 */
|
||||||
}
|
}
|
||||||
@@ -359,12 +359,12 @@ sticky_heaven:
|
|||||||
if (faccessat(dirfd, ".", X_OK, AT_EACCESS) < 0)
|
if (faccessat(dirfd, ".", X_OK, AT_EACCESS) < 0)
|
||||||
goto sticky_hell; /* down you go! */
|
goto sticky_hell; /* down you go! */
|
||||||
|
|
||||||
close_on_eintr(&dirfd);
|
xclose(&dirfd);
|
||||||
reset_caller_errno(0);
|
reset_caller_errno(0);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
sticky_hell:
|
sticky_hell:
|
||||||
close_on_eintr(&dirfd);
|
xclose(&dirfd);
|
||||||
(void) with_fallback_errno(EPERM);
|
(void) with_fallback_errno(EPERM);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -495,7 +495,7 @@ mkhtemp(int *fd,
|
|||||||
|
|
||||||
errno = EEXIST;
|
errno = EEXIST;
|
||||||
err:
|
err:
|
||||||
close_on_eintr(fd);
|
xclose(fd);
|
||||||
free_and_set_null(&fname_copy);
|
free_and_set_null(&fname_copy);
|
||||||
|
|
||||||
return with_fallback_errno(EIO);
|
return with_fallback_errno(EIO);
|
||||||
@@ -635,7 +635,7 @@ out:
|
|||||||
reset_caller_errno(0);
|
reset_caller_errno(0);
|
||||||
return rval;
|
return rval;
|
||||||
err:
|
err:
|
||||||
close_on_eintr(fd);
|
xclose(fd);
|
||||||
|
|
||||||
if (file_created)
|
if (file_created)
|
||||||
(void) unlinkat(dirfd, fname_copy, 0);
|
(void) unlinkat(dirfd, fname_copy, 0);
|
||||||
@@ -721,7 +721,7 @@ err:
|
|||||||
if (linked)
|
if (linked)
|
||||||
(void) unlinkat(dirfd, fname_copy, 0);
|
(void) unlinkat(dirfd, fname_copy, 0);
|
||||||
|
|
||||||
close_on_eintr(&tmpfd);
|
xclose(&tmpfd);
|
||||||
return with_fallback_errno(EIO);
|
return with_fallback_errno(EIO);
|
||||||
out:
|
out:
|
||||||
reset_caller_errno(0);
|
reset_caller_errno(0);
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ retry_rand: {
|
|||||||
rc == 0) { /* prevent infinite loop on fatal err */
|
rc == 0) { /* prevent infinite loop on fatal err */
|
||||||
#if defined(USE_URANDOM) && \
|
#if defined(USE_URANDOM) && \
|
||||||
((USE_URANDOM) > 0)
|
((USE_URANDOM) > 0)
|
||||||
close_on_eintr(&fd);
|
xclose(&fd);
|
||||||
#endif
|
#endif
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
@@ -186,7 +186,7 @@ retry_rand: {
|
|||||||
|
|
||||||
#if defined(USE_URANDOM) && \
|
#if defined(USE_URANDOM) && \
|
||||||
((USE_URANDOM) > 0)
|
((USE_URANDOM) > 0)
|
||||||
close_on_eintr(&fd);
|
xclose(&fd);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,8 +108,8 @@ exit_cleanup(void)
|
|||||||
f = &x->f;
|
f = &x->f;
|
||||||
|
|
||||||
/* close fds if still open */
|
/* close fds if still open */
|
||||||
close_on_eintr(&f->tmp_fd);
|
xclose(&f->tmp_fd);
|
||||||
close_on_eintr(&f->gbe_fd);
|
xclose(&f->gbe_fd);
|
||||||
|
|
||||||
/* unlink tmpfile if it exists */
|
/* unlink tmpfile if it exists */
|
||||||
if (f->tname != NULL) {
|
if (f->tname != NULL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user