From 51d4b7eafef091bd77c59777537e762fe3e586a8 Mon Sep 17 00:00:00 2001 From: Jan Beich Date: Fri, 7 Jul 2017 13:57:56 +0000 Subject: alloc: prefer atomic close-on-exec without O_TMPFILE as well Signed-off-by: Jan Beich --- configure.ac | 2 +- src/xshmfence_alloc.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 94c27b3..5636cb8 100644 --- a/configure.ac +++ b/configure.ac @@ -91,7 +91,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 ]]) diff --git a/src/xshmfence_alloc.c b/src/xshmfence_alloc.c index 05cf953..cba99a3 100644 --- a/src/xshmfence_alloc.c +++ b/src/xshmfence_alloc.c @@ -26,6 +26,8 @@ #include "xshmfenceint.h" +#include + #if !HAVE_MEMFD_CREATE #if HAVE_DECL___NR_MEMFD_CREATE #include @@ -68,6 +70,9 @@ xshmfence_alloc_shm(void) { char template[] = SHMDIR "/shmfd-XXXXXX"; int fd; +#ifndef HAVE_MKOSTEMP + int flags; +#endif #if HAVE_MEMFD_CREATE fd = memfd_create("xshmfence", MFD_CLOEXEC|MFD_ALLOW_SEALING); @@ -79,10 +84,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 + flags = fcntl(fd, F_GETFD); + if (flags != -1) { + flags |= FD_CLOEXEC; + fcntl(fd, F_SETFD, &flags); + } +#endif } } if (ftruncate(fd, sizeof (struct xshmfence)) < 0) { -- cgit v1.2.3