fd_util: added function pipe_cloexec()

Same as pipe_cloexec_nonblock(), but doesn't set non-blocking mode.
This commit is contained in:
Max Kellermann 2009-11-10 16:53:20 +01:00
parent 6975c087e0
commit 3d2a9d3545
2 changed files with 31 additions and 2 deletions

View File

@ -139,6 +139,30 @@ creat_cloexec(const char *path_fs, int mode)
return fd; return fd;
} }
int
pipe_cloexec(int fd[2])
{
#ifdef WIN32
return _pipe(event_pipe, 512, _O_BINARY);
#else
int ret;
#ifdef HAVE_PIPE2
ret = pipe2(fd, O_CLOEXEC);
if (ret >= 0 || errno != ENOSYS)
return ret;
#endif
ret = pipe(fd);
if (ret >= 0) {
fd_set_cloexec(fd[0], true);
fd_set_cloexec(fd[1], true);
}
return ret;
#endif
}
int int
pipe_cloexec_nonblock(int fd[2]) pipe_cloexec_nonblock(int fd[2])
{ {
@ -158,10 +182,8 @@ pipe_cloexec_nonblock(int fd[2])
fd_set_cloexec(fd[0], true); fd_set_cloexec(fd[0], true);
fd_set_cloexec(fd[1], true); fd_set_cloexec(fd[1], true);
#ifndef WIN32
fd_set_nonblock(fd[0]); fd_set_nonblock(fd[0]);
fd_set_nonblock(fd[1]); fd_set_nonblock(fd[1]);
#endif
} }
return ret; return ret;

View File

@ -55,6 +55,13 @@ open_cloexec(const char *path_fs, int flags);
int int
creat_cloexec(const char *path_fs, int mode); creat_cloexec(const char *path_fs, int mode);
/**
* Wrapper for pipe(), which sets the CLOEXEC flag (atomically if
* supported by the OS).
*/
int
pipe_cloexec(int fd[2]);
/** /**
* Wrapper for pipe(), which sets the CLOEXEC flag (atomically if * Wrapper for pipe(), which sets the CLOEXEC flag (atomically if
* supported by the OS). * supported by the OS).