osdep/io: add mp_dup_cloexec()

will be used in the next commit
This commit is contained in:
NRK authored and sfan5 committed 2026-03-12 15:58:53 +01:00
1 parent 7e9fa0dee4
commit 1aee4637f2
2 files changed
+24

No files matched your search

+23
View File
@@ -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);
+1
View File
@@ -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);