/* * Used for initializing stdio streams like options.stdin_stream. Returns * zero on success. See also the cleanup section in uv_spawn(). */ static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2]) { int mask; int fd; mask = UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD | UV_INHERIT_STREAM; switch (container->flags & mask) { case UV_IGNORE: return 0; case UV_CREATE_PIPE: assert(container->data.stream != NULL); if (container->data.stream->type != UV_NAMED_PIPE) return -EINVAL; else return uv__make_socketpair(fds, 0); case UV_INHERIT_FD: case UV_INHERIT_STREAM: if (container->flags & UV_INHERIT_FD) fd = container->data.fd; else fd = uv__stream_fd(container->data.stream); if (fd == -1) return -EINVAL; fds[1] = fd; return 0; default: assert(0 && "Unexpected flags"); return -EINVAL; } }
/* * Used for initializing stdio streams like options.stdin_stream. Returns * zero on success. */ static int uv__process_init_pipe(uv_pipe_t* handle, int fds[2], int flags) { if (handle->type != UV_NAMED_PIPE) { errno = EINVAL; return -1; } if (handle->ipc) return uv__make_socketpair(fds, flags); else return uv__make_pipe(fds, flags); }
/* * Used for initializing stdio streams like options.stdin_stream. Returns * zero on success. */ static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2], int writable) { int fd = -1; switch (container->flags & (UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD | UV_INHERIT_STREAM | UV_NATIVE_PIPE)) { case UV_IGNORE: return 0; case UV_CREATE_PIPE: assert(container->data.stream != NULL); if (container->data.stream->type != UV_NAMED_PIPE) { errno = EINVAL; return -1; } return uv__make_socketpair(fds, 0); case UV_NATIVE_PIPE: assert(container->data.stream != NULL); if(uv__make_pipe(*(int**)container->data.pipe, 0) == -1) return -1; if (writable) fds[1] = container->data.pipe->write; else fds[0] = container->data.pipe->read; return 0; case UV_INHERIT_FD: case UV_INHERIT_STREAM: if (container->flags & UV_INHERIT_FD) { fd = container->data.fd; } else { fd = container->data.stream->fd; } if (fd == -1) { errno = EINVAL; return -1; } fds[writable ? 1 : 0] = fd; return 0; default: assert(0 && "Unexpected flags"); return -1; } }
uv_err_t uv_pipe_pair(uv_pipe_t* a, uv_pipe_t* b) { int fds[2]; int r; uv_err_t err; /* Make sure that the mutex is only initialized once. */ uv_once(&uv__pipe_pair_lock_guard, uv__pipe_pair_lock_init); uv_mutex_lock(&uv__pipe_pair_lock); r = uv__make_socketpair(fds, UV__F_NONBLOCK | UV__F_IPC); if (r) { err = uv__new_sys_error(errno); } else { uv_pipe_open(a, fds[0]); uv_pipe_open(b, fds[1]); err = uv_ok_; } uv_mutex_unlock(&uv__pipe_pair_lock); return err; }