From 1573ea1485e809557bd9c8777a794d394fbc79c7 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Sat, 7 Nov 2009 19:02:53 +0100
Subject: inotify: set close-on-exec flag

Added wrapper for inotify_init1() to fd_util.c.
---
 src/fd_util.c        | 26 ++++++++++++++++++++++++++
 src/fd_util.h        |  3 +++
 src/inotify_source.c |  3 ++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/fd_util.c b/src/fd_util.c
index b6593d919..9137e6e3b 100644
--- a/src/fd_util.c
+++ b/src/fd_util.c
@@ -35,6 +35,10 @@
 #include <sys/socket.h>
 #endif
 
+#ifdef HAVE_INOTIFY_INIT
+#include <sys/inotify.h>
+#endif
+
 #ifndef WIN32
 
 static int
@@ -169,3 +173,25 @@ accept_cloexec(int fd, struct sockaddr *address, size_t *address_length_r)
 
 	return ret;
 }
+
+#ifdef HAVE_INOTIFY_INIT
+
+int
+inotify_init_cloexec(void)
+{
+	int fd;
+
+#ifdef HAVE_INOTIFY_INIT1
+	fd = inotify_init1(IN_CLOEXEC);
+	if (fd >= 0 || errno != ENOSYS)
+		return fd;
+#endif
+
+	fd = inotify_init();
+	if (fd >= 0)
+		fd_set_cloexec(fd, true);
+
+	return fd;
+}
+
+#endif
diff --git a/src/fd_util.h b/src/fd_util.h
index 805d1cc3d..99479a0cf 100644
--- a/src/fd_util.h
+++ b/src/fd_util.h
@@ -50,4 +50,7 @@ socket_cloexec(int domain, int type, int protocol);
 int
 accept_cloexec(int fd, struct sockaddr *address, size_t *address_length_r);
 
+int
+inotify_init_cloexec(void);
+
 #endif
diff --git a/src/inotify_source.c b/src/inotify_source.c
index d5feec3e0..fd39f8481 100644
--- a/src/inotify_source.c
+++ b/src/inotify_source.c
@@ -19,6 +19,7 @@
 
 #include "inotify_source.h"
 #include "fifo_buffer.h"
+#include "fd_util.h"
 
 #include <sys/inotify.h>
 #include <unistd.h>
@@ -105,7 +106,7 @@ mpd_inotify_source_new(mpd_inotify_callback_t callback, void *callback_ctx,
 	struct mpd_inotify_source *source =
 		g_new(struct mpd_inotify_source, 1);
 
-	source->fd = inotify_init();
+	source->fd = inotify_init_cloexec();
 	if (source->fd < 0) {
 		g_set_error(error_r, mpd_inotify_quark(), errno,
 			    "inotify_init() has failed: %s",
-- 
cgit v1.2.3