[PATCH libxshmfence 2/2] alloc: close-on-exec without O_TMPFILE as well

Jan Beich jbeich at FreeBSD.org
Fri Jul 7 14:31:58 UTC 2017


Signed-off-by: Jan Beich <jbeich at FreeBSD.org>
---
 configure.ac          |  2 +-
 src/xshmfence_alloc.c | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 0c98875..2d55890 100644
--- a/configure.ac
+++ b/configure.ac
@@ -89,7 +89,7 @@ AC_SUBST([XPROTO_CFLAGS])
 
 CFLAGS="$CFLAGS $XPROTO_CFLAGS"
 
-AC_CHECK_FUNCS(memfd_create)
+AC_CHECK_FUNCS(memfd_create mkostemp)
 
 AC_CHECK_DECLS([__NR_memfd_create], [], [], [[#include <asm/unistd.h>]])
 
diff --git a/src/xshmfence_alloc.c b/src/xshmfence_alloc.c
index 05cf953..427335f 100644
--- a/src/xshmfence_alloc.c
+++ b/src/xshmfence_alloc.c
@@ -26,6 +26,8 @@
 
 #include "xshmfenceint.h"
 
+#include <fcntl.h>
+
 #if !HAVE_MEMFD_CREATE
 #if HAVE_DECL___NR_MEMFD_CREATE
 #include <asm/unistd.h>
@@ -79,10 +81,21 @@ xshmfence_alloc_shm(void)
 		if (fd < 0)
 #endif
 		{
+#ifdef HAVE_MKOSTEMP
+			fd = mkostemp(template, O_CLOEXEC);
+#else
 			fd = mkstemp(template);
+#endif
 			if (fd < 0)
 				return fd;
 			unlink(template);
+#ifndef HAVE_MKOSTEMP
+			int flags = fcntl(fd, F_GETFD);
+			if (flags != -1) {
+				flags |= FD_CLOEXEC;
+				(void) fcntl(fd, F_SETFD, &flags);
+			}
+#endif
 		}
 	}
 	if (ftruncate(fd, sizeof (struct xshmfence)) < 0) {


More information about the xorg-devel mailing list