From 0cb66c0c5aafd48b63a755860746e70afb332c8d Mon Sep 17 00:00:00 2001
From: Tanu Kaskinen <tanuk@iki.fi>
Date: Wed, 24 Jan 2018 03:51:49 +0200
Subject: [PATCH] memfd-wrappers: only define memfd_create() if not already
 defined

glibc 2.27 is to be released soon, and it will provide memfd_create().
If glibc provides the function, we must not define it ourselves,
otherwise building fails due to conflict between the two implementations
of the same function.

BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=104733
---
Upstream-Status: Submitted [https://bugs.freedesktop.org/attachment.cgi?id=136927]
Signed-off-by: Khem Raj <raj.khem@gmail.com>

 configure.ac                   | 3 +++
 src/pulsecore/memfd-wrappers.h | 7 ++++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 77b5ff5..3a71fd8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -607,6 +607,9 @@ AS_IF([test "x$enable_memfd" = "xyes" && test "x$HAVE_MEMFD" = "x0"],
     [AC_MSG_ERROR([*** Your Linux kernel does not support memfd shared memory.
                   *** Use linux v3.17 or higher for such a feature.])])
 
+AS_IF([test "x$HAVE_MEMFD" = "x1"],
+    AC_CHECK_FUNCS([memfd_create]))
+
 AC_SUBST(HAVE_MEMFD)
 AM_CONDITIONAL([HAVE_MEMFD], [test "x$HAVE_MEMFD" = x1])
 AS_IF([test "x$HAVE_MEMFD" = "x1"], AC_DEFINE([HAVE_MEMFD], 1, [Have memfd shared memory.]))
diff --git a/src/pulsecore/memfd-wrappers.h b/src/pulsecore/memfd-wrappers.h
index 3bed9b2..c7aadfd 100644
--- a/src/pulsecore/memfd-wrappers.h
+++ b/src/pulsecore/memfd-wrappers.h
@@ -20,13 +20,14 @@
   License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#ifdef HAVE_MEMFD
+#if defined(HAVE_MEMFD) && !defined(HAVE_MEMFD_CREATE)
 
 #include <sys/syscall.h>
 #include <fcntl.h>
 
 /*
- * No glibc wrappers exist for memfd_create(2), so provide our own.
+ * Before glibc version 2.27 there was no wrapper for memfd_create(2),
+ * so we have to provide our own.
  *
  * Also define memfd fcntl sealing macros. While they are already
  * defined in the kernel header file <linux/fcntl.h>, that file as
@@ -63,6 +64,6 @@ static inline int memfd_create(const char *name, unsigned int flags) {
 #define F_SEAL_WRITE    0x0008  /* prevent writes */
 #endif
 
-#endif /* HAVE_MEMFD */
+#endif /* HAVE_MEMFD && !HAVE_MEMFD_CREATE */
 
 #endif
-- 
2.16.1

