]> git.sesse.net Git - ffmpeg/commitdiff
libavutil: add avpriv_open() to open files with close-on-exec flag
authorRémi Denis-Courmont <remi@remlab.net>
Tue, 6 Aug 2013 18:19:24 +0000 (21:19 +0300)
committerAnton Khirnov <anton@khirnov.net>
Wed, 7 Aug 2013 19:12:20 +0000 (21:12 +0200)
Signed-off-by: Anton Khirnov <anton@khirnov.net>
libavutil/file.c
libavutil/internal.h

index ce02487f35d6e86477b2c0356052092e867c57fc..add049d24bdc7378a7a32a6a224eab28e1d77f68 100644 (file)
 
 #include "config.h"
 #include "file.h"
+#include "internal.h"
 #include "log.h"
 #include "mem.h"
+#include <stdarg.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #if HAVE_UNISTD_H
 #include <windows.h>
 #endif
 
+int avpriv_open(const char *filename, int flags, ...)
+{
+    int fd;
+    unsigned int mode = 0;
+    va_list ap;
+
+    va_start(ap, flags);
+    if (flags & O_CREAT)
+        mode = va_arg(ap, unsigned int);
+    va_end(ap);
+
+#ifdef O_CLOEXEC
+    flags |= O_CLOEXEC;
+#endif
+
+    fd = open(filename, flags, mode);
+    if (fd != -1)
+        fcntl(fd, F_SETFD, FD_CLOEXEC);
+    return fd;
+}
+
 typedef struct {
     const AVClass *class;
     int   log_offset;
@@ -49,7 +72,7 @@ int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
                 int log_offset, void *log_ctx)
 {
     FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
-    int err, fd = open(filename, O_RDONLY);
+    int err, fd = avpriv_open(filename, O_RDONLY);
     struct stat st;
     av_unused void *ptr;
     off_t off_size;
index 5a721f3a714643db470a8be4f16725e3896bbe6e..cb3a8f5f1202c19b86ed614eef92e9e8178f540e 100644 (file)
@@ -196,4 +196,9 @@ void avpriv_report_missing_feature(void *avc,
 void avpriv_request_sample(void *avc,
                            const char *msg, ...) av_printf_format(2, 3);
 
+/**
+ * A wrapper for open() setting O_CLOEXEC.
+ */
+int avpriv_open(const char *filename, int flags, ...);
+
 #endif /* AVUTIL_INTERNAL_H */