tempfile(): use same fallback also when O_TMPFILE is missing

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-10-06 18:05:45 +02:00
parent fae83c9be3
commit a712364d49
+16 -8
View File
@@ -29,6 +29,18 @@
#include <stdlib.h> /* mkostemp() */
#include <sys/stat.h> /* umask() */
static FILE *fallback(void)
{
char nm[15] = _PATH_TMP "XXXXXXXX";
int fd;
fd = mkostemp(nm, O_CLOEXEC);
if (-1 == fd)
return NULL;
return fdopen(fd, "w+");
}
/**
* A secure tmpfile() replacement
*
@@ -54,19 +66,15 @@ FILE *tempfile(void)
fd = open(_PATH_TMP, O_TMPFILE | O_RDWR | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
umask(oldmask);
if (-1 == fd) {
if (errno == EOPNOTSUPP) {
char nm[15] = _PATH_TMP "XXXXXXXX";
if (errno == EOPNOTSUPP)
return fallback();
fd = mkostemp(nm, O_CLOEXEC);
if (-1 == fd)
return NULL;
} else
return NULL;
return NULL;
}
return fdopen(fd, "w+");
#else
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
return fallback();
#endif
}