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 committed 2021-10-06 18:05:45 +02:00
1 parent fae83c9be3
commit a712364d49
1 file changed
+16 -8
+16 -8
View File
@@ -29,6 +29,18 @@
#include <stdlib.h> /* mkostemp() */ #include <stdlib.h> /* mkostemp() */
#include <sys/stat.h> /* umask() */ #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 * 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); fd = open(_PATH_TMP, O_TMPFILE | O_RDWR | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
umask(oldmask); umask(oldmask);
if (-1 == fd) { if (-1 == fd) {
if (errno == EOPNOTSUPP) { if (errno == EOPNOTSUPP)
char nm[15] = _PATH_TMP "XXXXXXXX"; return fallback();
fd = mkostemp(nm, O_CLOEXEC); return NULL;
if (-1 == fd)
return NULL;
} else
return NULL;
} }
return fdopen(fd, "w+"); return fdopen(fd, "w+");
#else #else
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */ return fallback();
#endif #endif
} }