Blame src/unix/pipe.c

Packit Service 7c31a4
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service 7c31a4
 * of this software and associated documentation files (the "Software"), to
Packit Service 7c31a4
 * deal in the Software without restriction, including without limitation the
Packit Service 7c31a4
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit Service 7c31a4
 * sell copies of the Software, and to permit persons to whom the Software is
Packit Service 7c31a4
 * furnished to do so, subject to the following conditions:
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * The above copyright notice and this permission notice shall be included in
Packit Service 7c31a4
 * all copies or substantial portions of the Software.
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 7c31a4
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 7c31a4
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit Service 7c31a4
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit Service 7c31a4
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit Service 7c31a4
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit Service 7c31a4
 * IN THE SOFTWARE.
Packit Service 7c31a4
 */
Packit Service 7c31a4
Packit Service 7c31a4
#include "uv.h"
Packit Service 7c31a4
#include "internal.h"
Packit Service 7c31a4
Packit Service 7c31a4
#include <assert.h>
Packit Service 7c31a4
#include <errno.h>
Packit Service 7c31a4
#include <string.h>
Packit Service 7c31a4
#include <sys/un.h>
Packit Service 7c31a4
#include <unistd.h>
Packit Service 7c31a4
#include <stdlib.h>
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc) {
Packit Service 7c31a4
  uv__stream_init(loop, (uv_stream_t*)handle, UV_NAMED_PIPE);
Packit Service 7c31a4
  handle->shutdown_req = NULL;
Packit Service 7c31a4
  handle->connect_req = NULL;
Packit Service 7c31a4
  handle->pipe_fname = NULL;
Packit Service 7c31a4
  handle->ipc = ipc;
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
Packit Service 7c31a4
  struct sockaddr_un saddr;
Packit Service 7c31a4
  const char* pipe_fname;
Packit Service 7c31a4
  int sockfd;
Packit Service 7c31a4
  int err;
Packit Service 7c31a4
Packit Service 7c31a4
  pipe_fname = NULL;
Packit Service 7c31a4
Packit Service 7c31a4
  /* Already bound? */
Packit Service 7c31a4
  if (uv__stream_fd(handle) >= 0)
Packit Service 7c31a4
    return UV_EINVAL;
Packit Service 7c31a4
Packit Service 7c31a4
  /* Make a copy of the file name, it outlives this function's scope. */
Packit Service 7c31a4
  pipe_fname = uv__strdup(name);
Packit Service 7c31a4
  if (pipe_fname == NULL)
Packit Service 7c31a4
    return UV_ENOMEM;
Packit Service 7c31a4
Packit Service 7c31a4
  /* We've got a copy, don't touch the original any more. */
Packit Service 7c31a4
  name = NULL;
Packit Service 7c31a4
Packit Service 7c31a4
  err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
Packit Service 7c31a4
  if (err < 0)
Packit Service 7c31a4
    goto err_socket;
Packit Service 7c31a4
  sockfd = err;
Packit Service 7c31a4
Packit Service 7c31a4
  memset(&saddr, 0, sizeof saddr);
Packit Service 7c31a4
  uv__strscpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
Packit Service 7c31a4
  saddr.sun_family = AF_UNIX;
Packit Service 7c31a4
Packit Service 7c31a4
  if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr)) {
Packit Service 7c31a4
    err = UV__ERR(errno);
Packit Service 7c31a4
    /* Convert ENOENT to EACCES for compatibility with Windows. */
Packit Service 7c31a4
    if (err == UV_ENOENT)
Packit Service 7c31a4
      err = UV_EACCES;
Packit Service 7c31a4
Packit Service 7c31a4
    uv__close(sockfd);
Packit Service 7c31a4
    goto err_socket;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  /* Success. */
Packit Service 7c31a4
  handle->flags |= UV_HANDLE_BOUND;
Packit Service 7c31a4
  handle->pipe_fname = pipe_fname; /* Is a strdup'ed copy. */
Packit Service 7c31a4
  handle->io_watcher.fd = sockfd;
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
Packit Service 7c31a4
err_socket:
Packit Service 7c31a4
  uv__free((void*)pipe_fname);
Packit Service 7c31a4
  return err;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
Packit Service 7c31a4
  if (uv__stream_fd(handle) == -1)
Packit Service 7c31a4
    return UV_EINVAL;
Packit Service 7c31a4
Packit Service 7c31a4
  if (handle->ipc)
Packit Service 7c31a4
    return UV_EINVAL;
Packit Service 7c31a4
Packit Service 7c31a4
#if defined(__MVS__) || defined(__PASE__)
Packit Service 7c31a4
  /* On zOS, backlog=0 has undefined behaviour */
Packit Service 7c31a4
  /* On IBMi PASE, backlog=0 leads to "Connection refused" error */
Packit Service 7c31a4
  if (backlog == 0)
Packit Service 7c31a4
    backlog = 1;
Packit Service 7c31a4
  else if (backlog < 0)
Packit Service 7c31a4
    backlog = SOMAXCONN;
Packit Service 7c31a4
#endif
Packit Service 7c31a4
Packit Service 7c31a4
  if (listen(uv__stream_fd(handle), backlog))
Packit Service 7c31a4
    return UV__ERR(errno);
Packit Service 7c31a4
Packit Service 7c31a4
  handle->connection_cb = cb;
Packit Service 7c31a4
  handle->io_watcher.cb = uv__server_io;
Packit Service 7c31a4
  uv__io_start(handle->loop, &handle->io_watcher, POLLIN);
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
void uv__pipe_close(uv_pipe_t* handle) {
Packit Service 7c31a4
  if (handle->pipe_fname) {
Packit Service 7c31a4
    /*
Packit Service 7c31a4
     * Unlink the file system entity before closing the file descriptor.
Packit Service 7c31a4
     * Doing it the other way around introduces a race where our process
Packit Service 7c31a4
     * unlinks a socket with the same name that's just been created by
Packit Service 7c31a4
     * another thread or process.
Packit Service 7c31a4
     */
Packit Service 7c31a4
    unlink(handle->pipe_fname);
Packit Service 7c31a4
    uv__free((void*)handle->pipe_fname);
Packit Service 7c31a4
    handle->pipe_fname = NULL;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  uv__stream_close((uv_stream_t*)handle);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
Packit Service 7c31a4
  int flags;
Packit Service 7c31a4
  int mode;
Packit Service 7c31a4
  int err;
Packit Service 7c31a4
  flags = 0;
Packit Service 7c31a4
Packit Service 7c31a4
  if (uv__fd_exists(handle->loop, fd))
Packit Service 7c31a4
    return UV_EEXIST;
Packit Service 7c31a4
Packit Service 7c31a4
  do
Packit Service 7c31a4
    mode = fcntl(fd, F_GETFL);
Packit Service 7c31a4
  while (mode == -1 && errno == EINTR);
Packit Service 7c31a4
Packit Service 7c31a4
  if (mode == -1)
Packit Service 7c31a4
    return UV__ERR(errno); /* according to docs, must be EBADF */
Packit Service 7c31a4
Packit Service 7c31a4
  err = uv__nonblock(fd, 1);
Packit Service 7c31a4
  if (err)
Packit Service 7c31a4
    return err;
Packit Service 7c31a4
Packit Service 7c31a4
#if defined(__APPLE__)
Packit Service 7c31a4
  err = uv__stream_try_select((uv_stream_t*) handle, &fd;;
Packit Service 7c31a4
  if (err)
Packit Service 7c31a4
    return err;
Packit Service 7c31a4
#endif /* defined(__APPLE__) */
Packit Service 7c31a4
Packit Service 7c31a4
  mode &= O_ACCMODE;
Packit Service 7c31a4
  if (mode != O_WRONLY)
Packit Service 7c31a4
    flags |= UV_HANDLE_READABLE;
Packit Service 7c31a4
  if (mode != O_RDONLY)
Packit Service 7c31a4
    flags |= UV_HANDLE_WRITABLE;
Packit Service 7c31a4
Packit Service 7c31a4
  return uv__stream_open((uv_stream_t*)handle, fd, flags);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
void uv_pipe_connect(uv_connect_t* req,
Packit Service 7c31a4
                    uv_pipe_t* handle,
Packit Service 7c31a4
                    const char* name,
Packit Service 7c31a4
                    uv_connect_cb cb) {
Packit Service 7c31a4
  struct sockaddr_un saddr;
Packit Service 7c31a4
  int new_sock;
Packit Service 7c31a4
  int err;
Packit Service 7c31a4
  int r;
Packit Service 7c31a4
Packit Service 7c31a4
  new_sock = (uv__stream_fd(handle) == -1);
Packit Service 7c31a4
Packit Service 7c31a4
  if (new_sock) {
Packit Service 7c31a4
    err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
Packit Service 7c31a4
    if (err < 0)
Packit Service 7c31a4
      goto out;
Packit Service 7c31a4
    handle->io_watcher.fd = err;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  memset(&saddr, 0, sizeof saddr);
Packit Service 7c31a4
  uv__strscpy(saddr.sun_path, name, sizeof(saddr.sun_path));
Packit Service 7c31a4
  saddr.sun_family = AF_UNIX;
Packit Service 7c31a4
Packit Service 7c31a4
  do {
Packit Service 7c31a4
    r = connect(uv__stream_fd(handle),
Packit Service 7c31a4
                (struct sockaddr*)&saddr, sizeof saddr);
Packit Service 7c31a4
  }
Packit Service 7c31a4
  while (r == -1 && errno == EINTR);
Packit Service 7c31a4
Packit Service 7c31a4
  if (r == -1 && errno != EINPROGRESS) {
Packit Service 7c31a4
    err = UV__ERR(errno);
Packit Service 7c31a4
#if defined(__CYGWIN__) || defined(__MSYS__)
Packit Service 7c31a4
    /* EBADF is supposed to mean that the socket fd is bad, but
Packit Service 7c31a4
       Cygwin reports EBADF instead of ENOTSOCK when the file is
Packit Service 7c31a4
       not a socket.  We do not expect to see a bad fd here
Packit Service 7c31a4
       (e.g. due to new_sock), so translate the error.  */
Packit Service 7c31a4
    if (err == UV_EBADF)
Packit Service 7c31a4
      err = UV_ENOTSOCK;
Packit Service 7c31a4
#endif
Packit Service 7c31a4
    goto out;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  err = 0;
Packit Service 7c31a4
  if (new_sock) {
Packit Service 7c31a4
    err = uv__stream_open((uv_stream_t*)handle,
Packit Service 7c31a4
                          uv__stream_fd(handle),
Packit Service 7c31a4
                          UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  if (err == 0)
Packit Service 7c31a4
    uv__io_start(handle->loop, &handle->io_watcher, POLLOUT);
Packit Service 7c31a4
Packit Service 7c31a4
out:
Packit Service 7c31a4
  handle->delayed_error = err;
Packit Service 7c31a4
  handle->connect_req = req;
Packit Service 7c31a4
Packit Service 7c31a4
  uv__req_init(handle->loop, req, UV_CONNECT);
Packit Service 7c31a4
  req->handle = (uv_stream_t*)handle;
Packit Service 7c31a4
  req->cb = cb;
Packit Service 7c31a4
  QUEUE_INIT(&req->queue);
Packit Service 7c31a4
Packit Service 7c31a4
  /* Force callback to run on next tick in case of error. */
Packit Service 7c31a4
  if (err)
Packit Service 7c31a4
    uv__io_feed(handle->loop, &handle->io_watcher);
Packit Service 7c31a4
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
static int uv__pipe_getsockpeername(const uv_pipe_t* handle,
Packit Service 7c31a4
                                    uv__peersockfunc func,
Packit Service 7c31a4
                                    char* buffer,
Packit Service 7c31a4
                                    size_t* size) {
Packit Service 7c31a4
  struct sockaddr_un sa;
Packit Service 7c31a4
  socklen_t addrlen;
Packit Service 7c31a4
  int err;
Packit Service 7c31a4
Packit Service 7c31a4
  addrlen = sizeof(sa);
Packit Service 7c31a4
  memset(&sa, 0, addrlen);
Packit Service 7c31a4
  err = uv__getsockpeername((const uv_handle_t*) handle,
Packit Service 7c31a4
                            func,
Packit Service 7c31a4
                            (struct sockaddr*) &sa,
Packit Service 7c31a4
                            (int*) &addrlen);
Packit Service 7c31a4
  if (err < 0) {
Packit Service 7c31a4
    *size = 0;
Packit Service 7c31a4
    return err;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
#if defined(__linux__)
Packit Service 7c31a4
  if (sa.sun_path[0] == 0)
Packit Service 7c31a4
    /* Linux abstract namespace */
Packit Service 7c31a4
    addrlen -= offsetof(struct sockaddr_un, sun_path);
Packit Service 7c31a4
  else
Packit Service 7c31a4
#endif
Packit Service 7c31a4
    addrlen = strlen(sa.sun_path);
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
  if ((size_t)addrlen >= *size) {
Packit Service 7c31a4
    *size = addrlen + 1;
Packit Service 7c31a4
    return UV_ENOBUFS;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  memcpy(buffer, sa.sun_path, addrlen);
Packit Service 7c31a4
  *size = addrlen;
Packit Service 7c31a4
Packit Service 7c31a4
  /* only null-terminate if it's not an abstract socket */
Packit Service 7c31a4
  if (buffer[0] != '\0')
Packit Service 7c31a4
    buffer[addrlen] = '\0';
Packit Service 7c31a4
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_getsockname(const uv_pipe_t* handle, char* buffer, size_t* size) {
Packit Service 7c31a4
  return uv__pipe_getsockpeername(handle, getsockname, buffer, size);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_getpeername(const uv_pipe_t* handle, char* buffer, size_t* size) {
Packit Service 7c31a4
  return uv__pipe_getsockpeername(handle, getpeername, buffer, size);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
void uv_pipe_pending_instances(uv_pipe_t* handle, int count) {
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_pending_count(uv_pipe_t* handle) {
Packit Service 7c31a4
  uv__stream_queued_fds_t* queued_fds;
Packit Service 7c31a4
Packit Service 7c31a4
  if (!handle->ipc)
Packit Service 7c31a4
    return 0;
Packit Service 7c31a4
Packit Service 7c31a4
  if (handle->accepted_fd == -1)
Packit Service 7c31a4
    return 0;
Packit Service 7c31a4
Packit Service 7c31a4
  if (handle->queued_fds == NULL)
Packit Service 7c31a4
    return 1;
Packit Service 7c31a4
Packit Service 7c31a4
  queued_fds = handle->queued_fds;
Packit Service 7c31a4
  return queued_fds->offset + 1;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle) {
Packit Service 7c31a4
  if (!handle->ipc)
Packit Service 7c31a4
    return UV_UNKNOWN_HANDLE;
Packit Service 7c31a4
Packit Service 7c31a4
  if (handle->accepted_fd == -1)
Packit Service 7c31a4
    return UV_UNKNOWN_HANDLE;
Packit Service 7c31a4
  else
Packit Service 7c31a4
    return uv__handle_type(handle->accepted_fd);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_pipe_chmod(uv_pipe_t* handle, int mode) {
Packit Service 7c31a4
  unsigned desired_mode;
Packit Service 7c31a4
  struct stat pipe_stat;
Packit Service 7c31a4
  char* name_buffer;
Packit Service 7c31a4
  size_t name_len;
Packit Service 7c31a4
  int r;
Packit Service 7c31a4
Packit Service 7c31a4
  if (handle == NULL || uv__stream_fd(handle) == -1)
Packit Service 7c31a4
    return UV_EBADF;
Packit Service 7c31a4
Packit Service 7c31a4
  if (mode != UV_READABLE &&
Packit Service 7c31a4
      mode != UV_WRITABLE &&
Packit Service 7c31a4
      mode != (UV_WRITABLE | UV_READABLE))
Packit Service 7c31a4
    return UV_EINVAL;
Packit Service 7c31a4
Packit Service 7c31a4
  /* Unfortunately fchmod does not work on all platforms, we will use chmod. */
Packit Service 7c31a4
  name_len = 0;
Packit Service 7c31a4
  r = uv_pipe_getsockname(handle, NULL, &name_len);
Packit Service 7c31a4
  if (r != UV_ENOBUFS)
Packit Service 7c31a4
    return r;
Packit Service 7c31a4
Packit Service 7c31a4
  name_buffer = uv__malloc(name_len);
Packit Service 7c31a4
  if (name_buffer == NULL)
Packit Service 7c31a4
    return UV_ENOMEM;
Packit Service 7c31a4
Packit Service 7c31a4
  r = uv_pipe_getsockname(handle, name_buffer, &name_len);
Packit Service 7c31a4
  if (r != 0) {
Packit Service 7c31a4
    uv__free(name_buffer);
Packit Service 7c31a4
    return r;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  /* stat must be used as fstat has a bug on Darwin */
Packit Service 7c31a4
  if (stat(name_buffer, &pipe_stat) == -1) {
Packit Service 7c31a4
    uv__free(name_buffer);
Packit Service 7c31a4
    return -errno;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  desired_mode = 0;
Packit Service 7c31a4
  if (mode & UV_READABLE)
Packit Service 7c31a4
    desired_mode |= S_IRUSR | S_IRGRP | S_IROTH;
Packit Service 7c31a4
  if (mode & UV_WRITABLE)
Packit Service 7c31a4
    desired_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Packit Service 7c31a4
Packit Service 7c31a4
  /* Exit early if pipe already has desired mode. */
Packit Service 7c31a4
  if ((pipe_stat.st_mode & desired_mode) == desired_mode) {
Packit Service 7c31a4
    uv__free(name_buffer);
Packit Service 7c31a4
    return 0;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  pipe_stat.st_mode |= desired_mode;
Packit Service 7c31a4
Packit Service 7c31a4
  r = chmod(name_buffer, pipe_stat.st_mode);
Packit Service 7c31a4
  uv__free(name_buffer);
Packit Service 7c31a4
Packit Service 7c31a4
  return r != -1 ? 0 : UV__ERR(errno);
Packit Service 7c31a4
}