Skip to content

Commit

Permalink
fallback to tmpfile() when O_TMPFILE not supported (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpvdm authored Jun 19, 2024
1 parent b315d06 commit 74b1f7f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/tempfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
#include <stdio.h> /* fdopen() */
#include <sys/stat.h> /* umask() */
#include <errno.h>

/**
* tempfile - A secure tmpfile() replacement
Expand All @@ -42,8 +43,13 @@ FILE *tempfile(void)
oldmask = umask(0077);
fd = open(_PATH_TMP, O_TMPFILE | O_RDWR | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
umask(oldmask);
if (-1 == fd)
return NULL;
if (fd == -1) {
/* Fall back to tmpfile() if O_TMPFILE is not supported */
if (errno == EOPNOTSUPP)
return tmpfile();

return NULL;
}

return fdopen(fd, "w+");
#else
Expand Down

0 comments on commit 74b1f7f

Please sign in to comment.