summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/mkhtemp.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/libreboot-utils/lib/mkhtemp.c')
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index c1574634..4d7ad0bd 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -58,10 +58,8 @@ new_tmp_common(int *fd, char **path, int type,
struct stat st;
const char *templatestr;
- size_t templatestr_len;
size_t dirlen;
- size_t destlen;
char *dest = NULL; /* final path (will be written into "path") */
int saved_errno = errno;
int dirfd = -1;
@@ -114,9 +112,8 @@ new_tmp_common(int *fd, char **path, int type,
else
templatestr = "tmp.XXXXXXXXXX";
- /* may as well calculate in advance */
- destlen = slen(tmpdir, PATH_MAX, &dirlen) + 1
- + slen(templatestr, PATH_MAX, &templatestr_len);
+ /* may as well calculate in advance */
+ dirlen = slen(tmpdir, PATH_MAX, &dirlen);
/* full path: */
dest = scatn(3, (const char *[]) { tmpdir, "/", templatestr },
PATH_MAX, &dest);
@@ -441,7 +438,6 @@ mkhtemp(int *fd,
size_t retries;
- int close_errno;
int saved_errno = errno;
int r;
@@ -699,6 +695,10 @@ mkhtemp_tmpfile_linux(int dirfd,
linked = 1; /* file created */
+ /* TODO: potential fd leak here.
+ * probably should only set *fd on successful
+ * return from this function (see below)
+ */
if (fd_verify_dir_identity(dirfd, st_dir_first) < 0 ||
fstat(*fd = tmpfd, st) < 0 ||
secure_file(fd, st, st, O_APPEND, 1, 1, 0600) < 0)
@@ -740,15 +740,21 @@ int secure_file(int *fd,
int do_lock,
mode_t mode)
{
- int flags;
+ int flags = -1;
struct stat st_now;
int saved_errno = errno;
errno = 0;
if (if_err(fd == NULL || st == NULL, EFAULT) ||
- if_err(*fd < 0, EBADF) ||
- if_err_sys((flags = fcntl(*fd, F_GETFL)) == -1) ||
- if_err(bad_flags > 0 && (flags & bad_flags), EPERM))
+ if_err(*fd < 0, EBADF))
+ goto err_demons;
+
+ while (fs_retry(saved_errno,
+ flags = fcntl(*fd, F_GETFL)));
+ if (flags == -1)
+ goto err_demons;
+
+ if (if_err(bad_flags > 0 && (flags & bad_flags), EPERM))
goto err_demons;
if (expected != NULL) {
@@ -874,6 +880,7 @@ lock_file(int fd, int flags)
{
struct flock fl;
int saved_errno = errno;
+ int fcntl_rval = -1;
errno = 0;
if (if_err(fd < 0, EBADF) ||
@@ -889,7 +896,9 @@ lock_file(int fd, int flags)
fl.l_whence = SEEK_SET;
- if (fcntl(fd, F_SETLK, &fl) == -1)
+ while (fs_retry(saved_errno,
+ fcntl_rval = fcntl(fd, F_SETLK, &fl)));
+ if (fcntl_rval == -1)
goto err_lock_file;
reset_caller_errno(0);