diff --git a/osdep/io.c b/osdep/io.c index f7e6927187..0ebf102d8c 100644 --- a/osdep/io.c +++ b/osdep/io.c @@ -63,6 +63,11 @@ bool mp_set_cloexec(int fd) } #ifndef _WIN32 +int mp_dup_cloexec(int fd) +{ + return fcntl(fd, F_DUPFD_CLOEXEC); +} + int mp_make_cloexec_pipe(int pipes[2]) { if (pipe(pipes) != 0) { @@ -263,6 +268,24 @@ static int hstat(HANDLE h, struct mp_stat *buf) return 0; } +int mp_dup_cloexec(int fd) +{ + HANDLE proc = GetCurrentProcess(); + HANDLE src = (HANDLE)_get_osfhandle(fd), dup = INVALID_HANDLE_VALUE; + if (src == INVALID_HANDLE_VALUE) + return -1; + BOOL ok = DuplicateHandle(proc, src, proc, &dup, 0, FALSE, DUPLICATE_SAME_ACCESS); + if (!ok) { + set_errno_from_lasterror(); + return -1; + } + int oflag = _O_RDWR | _O_NOINHERIT; // FIXME: find out the proper flags from the HANDLE + int dupfd = _open_osfhandle((intptr_t)dup, oflag); + if (dupfd < 0) + CloseHandle(dup); + return dupfd; +} + int mp_stat(const char *path, struct mp_stat *buf) { wchar_t *wpath = mp_from_utf8(NULL, path); diff --git a/osdep/io.h b/osdep/io.h index ab1e298f7f..cc72e22473 100644 --- a/osdep/io.h +++ b/osdep/io.h @@ -75,6 +75,7 @@ static inline int mp_fseeko(FILE* fp, off64_t offset, int whence) { #endif bool mp_set_cloexec(int fd); +int mp_dup_cloexec(int fd); int mp_make_cloexec_pipe(int pipes[2]); int mp_make_wakeup_pipe(int pipes[2]); void mp_flush_wakeup_pipe(int pipe_end);