summaryrefslogtreecommitdiffstats
path: root/osdep
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-11-30 22:40:51 +0100
committerwm4 <wm4@nowhere>2013-11-30 22:40:51 +0100
commit95cfe58e3db9d939abe7a9a26116c1d576eed60b (patch)
tree98a4738f2f989c900702d84ef8a257f9413389af /osdep
parenteea69682a6a874d540f9fc576c937466970713f6 (diff)
downloadmpv-95cfe58e3db9d939abe7a9a26116c1d576eed60b.tar.bz2
mpv-95cfe58e3db9d939abe7a9a26116c1d576eed60b.tar.xz
Use O_CLOEXEC when creating FDs
This is needed so that new processes (created with fork+exec) don't inherit open files, which can be important for a number of reasons. Since O_CLOEXEC is relatively new (POSIX.1-2008, before that Linux specific), we #define it to 0 in io.h to prevent compilation errors on older/crappy systems. At least this is the plan. input.c creates a pipe. For that, add a mp_set_cloexec() function (which is based on Weston's code in vo_wayland.c, but more correct). We could use pipe2() instead, but that is Linux specific. Technically, we have a race condition, but it won't matter.
Diffstat (limited to 'osdep')
-rw-r--r--osdep/io.c18
-rw-r--r--osdep/io.h11
2 files changed, 28 insertions, 1 deletions
diff --git a/osdep/io.c b/osdep/io.c
index 94bd90a676..4ad00d8df3 100644
--- a/osdep/io.c
+++ b/osdep/io.c
@@ -19,6 +19,23 @@
*/
#include "config.h"
+#include "osdep/io.h"
+
+// Set the CLOEXEC flag on the given fd.
+// On error, false is returned (and errno set).
+bool mp_set_cloexec(int fd)
+{
+#if defined(FD_CLOEXEC) && defined(F_SETFD)
+ if (fd >= 0) {
+ int flags = fcntl(fd, F_GETFD);
+ if (flags == -1)
+ return false;
+ if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
+ return false;
+ }
+#endif
+ return true;
+}
#ifdef _WIN32
@@ -27,7 +44,6 @@
#include <stdio.h>
#include <stddef.h>
-#include "osdep/io.h"
#include "talloc.h"
//copied and modified from libav
diff --git a/osdep/io.h b/osdep/io.h
index d855e852eb..aa20446210 100644
--- a/osdep/io.h
+++ b/osdep/io.h
@@ -20,7 +20,18 @@
#ifndef MPLAYER_OSDEP_IO
#define MPLAYER_OSDEP_IO
+#include <stdbool.h>
#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+// This is in POSIX.1-2008, but support outside of Linux is scarce.
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
+bool mp_set_cloexec(int fd);
#ifdef _WIN32
#include <wchar.h>