Blame src/unix/poll.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 <unistd.h>
Packit Service 7c31a4
#include <assert.h>
Packit Service 7c31a4
#include <errno.h>
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
static void uv__poll_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
Packit Service 7c31a4
  uv_poll_t* handle;
Packit Service 7c31a4
  int pevents;
Packit Service 7c31a4
Packit Service 7c31a4
  handle = container_of(w, uv_poll_t, io_watcher);
Packit Service 7c31a4
Packit Service 7c31a4
  /*
Packit Service 7c31a4
   * As documented in the kernel source fs/kernfs/file.c #780
Packit Service 7c31a4
   * poll will return POLLERR|POLLPRI in case of sysfs
Packit Service 7c31a4
   * polling. This does not happen in case of out-of-band
Packit Service 7c31a4
   * TCP messages.
Packit Service 7c31a4
   *
Packit Service 7c31a4
   * The above is the case on (at least) FreeBSD and Linux.
Packit Service 7c31a4
   *
Packit Service 7c31a4
   * So to properly determine a POLLPRI or a POLLERR we need
Packit Service 7c31a4
   * to check for both.
Packit Service 7c31a4
   */
Packit Service 7c31a4
  if ((events & POLLERR) && !(events & UV__POLLPRI)) {
Packit Service 7c31a4
    uv__io_stop(loop, w, POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
Packit Service 7c31a4
    uv__handle_stop(handle);
Packit Service 7c31a4
    handle->poll_cb(handle, UV_EBADF, 0);
Packit Service 7c31a4
    return;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  pevents = 0;
Packit Service 7c31a4
  if (events & POLLIN)
Packit Service 7c31a4
    pevents |= UV_READABLE;
Packit Service 7c31a4
  if (events & UV__POLLPRI)
Packit Service 7c31a4
    pevents |= UV_PRIORITIZED;
Packit Service 7c31a4
  if (events & POLLOUT)
Packit Service 7c31a4
    pevents |= UV_WRITABLE;
Packit Service 7c31a4
  if (events & UV__POLLRDHUP)
Packit Service 7c31a4
    pevents |= UV_DISCONNECT;
Packit Service 7c31a4
Packit Service 7c31a4
  handle->poll_cb(handle, 0, pevents);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) {
Packit Service 7c31a4
  int err;
Packit Service 7c31a4
Packit Service 7c31a4
  if (uv__fd_exists(loop, fd))
Packit Service 7c31a4
    return UV_EEXIST;
Packit Service 7c31a4
Packit Service 7c31a4
  err = uv__io_check_fd(loop, fd);
Packit Service 7c31a4
  if (err)
Packit Service 7c31a4
    return err;
Packit Service 7c31a4
Packit Service 7c31a4
  /* If ioctl(FIONBIO) reports ENOTTY, try fcntl(F_GETFL) + fcntl(F_SETFL).
Packit Service 7c31a4
   * Workaround for e.g. kqueue fds not supporting ioctls.
Packit Service 7c31a4
   */
Packit Service 7c31a4
  err = uv__nonblock(fd, 1);
Packit Service 7c31a4
  if (err == UV_ENOTTY)
Packit Service 7c31a4
    if (uv__nonblock == uv__nonblock_ioctl)
Packit Service 7c31a4
      err = uv__nonblock_fcntl(fd, 1);
Packit Service 7c31a4
Packit Service 7c31a4
  if (err)
Packit Service 7c31a4
    return err;
Packit Service 7c31a4
Packit Service 7c31a4
  uv__handle_init(loop, (uv_handle_t*) handle, UV_POLL);
Packit Service 7c31a4
  uv__io_init(&handle->io_watcher, uv__poll_io, fd);
Packit Service 7c31a4
  handle->poll_cb = NULL;
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
Packit Service 7c31a4
    uv_os_sock_t socket) {
Packit Service 7c31a4
  return uv_poll_init(loop, handle, socket);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
static void uv__poll_stop(uv_poll_t* handle) {
Packit Service 7c31a4
  uv__io_stop(handle->loop,
Packit Service 7c31a4
              &handle->io_watcher,
Packit Service 7c31a4
              POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
Packit Service 7c31a4
  uv__handle_stop(handle);
Packit Service 7c31a4
  uv__platform_invalidate_fd(handle->loop, handle->io_watcher.fd);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_poll_stop(uv_poll_t* handle) {
Packit Service 7c31a4
  assert(!uv__is_closing(handle));
Packit Service 7c31a4
  uv__poll_stop(handle);
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
int uv_poll_start(uv_poll_t* handle, int pevents, uv_poll_cb poll_cb) {
Packit Service 7c31a4
  int events;
Packit Service 7c31a4
Packit Service 7c31a4
  assert((pevents & ~(UV_READABLE | UV_WRITABLE | UV_DISCONNECT |
Packit Service 7c31a4
                      UV_PRIORITIZED)) == 0);
Packit Service 7c31a4
  assert(!uv__is_closing(handle));
Packit Service 7c31a4
Packit Service 7c31a4
  uv__poll_stop(handle);
Packit Service 7c31a4
Packit Service 7c31a4
  if (pevents == 0)
Packit Service 7c31a4
    return 0;
Packit Service 7c31a4
Packit Service 7c31a4
  events = 0;
Packit Service 7c31a4
  if (pevents & UV_READABLE)
Packit Service 7c31a4
    events |= POLLIN;
Packit Service 7c31a4
  if (pevents & UV_PRIORITIZED)
Packit Service 7c31a4
    events |= UV__POLLPRI;
Packit Service 7c31a4
  if (pevents & UV_WRITABLE)
Packit Service 7c31a4
    events |= POLLOUT;
Packit Service 7c31a4
  if (pevents & UV_DISCONNECT)
Packit Service 7c31a4
    events |= UV__POLLRDHUP;
Packit Service 7c31a4
Packit Service 7c31a4
  uv__io_start(handle->loop, &handle->io_watcher, events);
Packit Service 7c31a4
  uv__handle_start(handle);
Packit Service 7c31a4
  handle->poll_cb = poll_cb;
Packit Service 7c31a4
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
void uv__poll_close(uv_poll_t* handle) {
Packit Service 7c31a4
  uv__poll_stop(handle);
Packit Service 7c31a4
}