Permalink
Browse files

unix: clean up code in process.c

  • Loading branch information...
1 parent 6f679a4 commit 2ec2a5194fd65addbf1995992e91712719c532c9 @bnoordhuis bnoordhuis committed Jan 11, 2013
Showing with 41 additions and 52 deletions.
  1. +41 −52 src/unix/process.c
View
@@ -185,77 +185,66 @@ int uv__make_pipe(int fds[2], int flags) {
* zero on success.
*/
static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2]) {
- int fd = -1;
- switch (container->flags & (UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD |
- UV_INHERIT_STREAM)) {
- 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;
- }
+ int mask;
+ int fd;
- 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);
- }
+ mask = UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD | UV_INHERIT_STREAM;
- if (fd == -1) {
- errno = EINVAL;
- return -1;
- }
+ switch (container->flags & mask) {
+ case UV_IGNORE:
+ return 0;
- fds[1] = fd;
+ 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_INHERIT_FD:
+ case UV_INHERIT_STREAM:
+ if (container->flags & UV_INHERIT_FD)
+ fd = container->data.fd;
+ else
+ fd = uv__stream_fd(container->data.stream);
- return 0;
- default:
- assert(0 && "Unexpected flags");
+ if (fd == -1) {
+ errno = EINVAL;
return -1;
- }
-}
+ }
+ fds[1] = fd;
+ return 0;
-static int uv__process_stdio_flags(uv_stdio_container_t* container,
- int writable) {
- if (container->data.stream->type == UV_NAMED_PIPE &&
- ((uv_pipe_t*)container->data.stream)->ipc) {
- return UV_STREAM_READABLE | UV_STREAM_WRITABLE;
- } else if (writable) {
- return UV_STREAM_WRITABLE;
- } else {
- return UV_STREAM_READABLE;
+ default:
+ assert(0 && "Unexpected flags");
+ return -1;
}
}
static int uv__process_open_stream(uv_stdio_container_t* container,
- int fds[2],
+ int pipefds[2],
int writable) {
- int child_fd;
int flags;
- int fd;
-
- fd = fds[0];
- child_fd = fds[1];
- /* No need to create stream */
- if (!(container->flags & UV_CREATE_PIPE) || fd < 0)
+ if (!(container->flags & UV_CREATE_PIPE) || pipefds[0] < 0)
return 0;
- assert(child_fd >= 0);
- close(child_fd);
+ assert(pipefds[1] >= 0);
+ close(pipefds[1]);
+ uv__nonblock(pipefds[0], 1);
- uv__nonblock(fd, 1);
- flags = uv__process_stdio_flags(container, writable);
+ if (container->data.stream->type == UV_NAMED_PIPE &&
+ ((uv_pipe_t*)container->data.stream)->ipc)
+ flags = UV_STREAM_READABLE | UV_STREAM_WRITABLE;
+ else if (writable)
+ flags = UV_STREAM_WRITABLE;
+ else
+ flags = UV_STREAM_READABLE;
- return uv__stream_open((uv_stream_t*)container->data.stream, fd, flags);
+ return uv__stream_open(container->data.stream, pipefds[0], flags);
}

0 comments on commit 2ec2a51

Please sign in to comment.