Handle old Linux/(G)LIBC and UNIX systems the same

Linux systems pre Linux 3.11, and UNIX do not have the O_TMPFILE flag,
so let's treat them the same, by wrapping tmpfile().

Signed-off-by: David Brownlee <abs@absd.org>
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
David Brownlee
2016-05-30 21:05:21 +02:00
committed by Joachim Nilsson
parent 613d397161
commit f3f6947604
+2 -11
View File
@@ -17,17 +17,9 @@
#include <paths.h>
#include <fcntl.h> /* O_TMPFILE requires -D_GNU_SOURCE */
#include <linux/version.h>
#include <stdlib.h> /* mkstemp() */
#include <stdio.h> /* fdopen() */
#include <sys/stat.h> /* umask() */
#ifndef O_TMPFILE /* Too old GLIBC or kernel */
#warning O_TMPFILE missing on your system, tempfile() may not work!
#define __O_TMPFILE 020000000
#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY) /* Define and let it fail at runtime */
#endif
/**
* tempfile - A secure tmpfile() replacement
*
@@ -43,7 +35,7 @@
*/
FILE *tempfile(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
#ifdef O_TMPFILE /* Only on Linux, with fairly recent (G)LIBC */
int fd;
mode_t oldmask;
@@ -55,8 +47,7 @@ FILE *tempfile(void)
return fdopen(fd, "rw");
#else
#warning Too old kernel, reverting to wrap unsafe tmpfile() ...
return tmpfile();
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
#endif
}