1e66f31c5Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2e66f31c5Sopenharmony_ci *
3e66f31c5Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
4e66f31c5Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
5e66f31c5Sopenharmony_ci * deal in the Software without restriction, including without limitation the
6e66f31c5Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7e66f31c5Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
8e66f31c5Sopenharmony_ci * furnished to do so, subject to the following conditions:
9e66f31c5Sopenharmony_ci *
10e66f31c5Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
11e66f31c5Sopenharmony_ci * all copies or substantial portions of the Software.
12e66f31c5Sopenharmony_ci *
13e66f31c5Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14e66f31c5Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15e66f31c5Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16e66f31c5Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17e66f31c5Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18e66f31c5Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19e66f31c5Sopenharmony_ci * IN THE SOFTWARE.
20e66f31c5Sopenharmony_ci */
21e66f31c5Sopenharmony_ci
22e66f31c5Sopenharmony_ci#include "uv.h"
23e66f31c5Sopenharmony_ci#include "internal.h"
24e66f31c5Sopenharmony_ci
25e66f31c5Sopenharmony_ci#include <unistd.h>
26e66f31c5Sopenharmony_ci#include <assert.h>
27e66f31c5Sopenharmony_ci#include <errno.h>
28e66f31c5Sopenharmony_ci
29e66f31c5Sopenharmony_ci
30e66f31c5Sopenharmony_cistatic void uv__poll_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
31e66f31c5Sopenharmony_ci  uv_poll_t* handle;
32e66f31c5Sopenharmony_ci  int pevents;
33e66f31c5Sopenharmony_ci
34e66f31c5Sopenharmony_ci  handle = container_of(w, uv_poll_t, io_watcher);
35e66f31c5Sopenharmony_ci
36e66f31c5Sopenharmony_ci  /*
37e66f31c5Sopenharmony_ci   * As documented in the kernel source fs/kernfs/file.c #780
38e66f31c5Sopenharmony_ci   * poll will return POLLERR|POLLPRI in case of sysfs
39e66f31c5Sopenharmony_ci   * polling. This does not happen in case of out-of-band
40e66f31c5Sopenharmony_ci   * TCP messages.
41e66f31c5Sopenharmony_ci   *
42e66f31c5Sopenharmony_ci   * The above is the case on (at least) FreeBSD and Linux.
43e66f31c5Sopenharmony_ci   *
44e66f31c5Sopenharmony_ci   * So to properly determine a POLLPRI or a POLLERR we need
45e66f31c5Sopenharmony_ci   * to check for both.
46e66f31c5Sopenharmony_ci   */
47e66f31c5Sopenharmony_ci  if ((events & POLLERR) && !(events & UV__POLLPRI)) {
48e66f31c5Sopenharmony_ci    uv__io_stop(loop, w, POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
49e66f31c5Sopenharmony_ci    uv__handle_stop(handle);
50e66f31c5Sopenharmony_ci    handle->poll_cb(handle, UV_EBADF, 0);
51e66f31c5Sopenharmony_ci    return;
52e66f31c5Sopenharmony_ci  }
53e66f31c5Sopenharmony_ci
54e66f31c5Sopenharmony_ci  pevents = 0;
55e66f31c5Sopenharmony_ci  if (events & POLLIN)
56e66f31c5Sopenharmony_ci    pevents |= UV_READABLE;
57e66f31c5Sopenharmony_ci  if (events & UV__POLLPRI)
58e66f31c5Sopenharmony_ci    pevents |= UV_PRIORITIZED;
59e66f31c5Sopenharmony_ci  if (events & POLLOUT)
60e66f31c5Sopenharmony_ci    pevents |= UV_WRITABLE;
61e66f31c5Sopenharmony_ci  if (events & UV__POLLRDHUP)
62e66f31c5Sopenharmony_ci    pevents |= UV_DISCONNECT;
63e66f31c5Sopenharmony_ci
64e66f31c5Sopenharmony_ci  handle->poll_cb(handle, 0, pevents);
65e66f31c5Sopenharmony_ci}
66e66f31c5Sopenharmony_ci
67e66f31c5Sopenharmony_ci
68e66f31c5Sopenharmony_ciint uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) {
69e66f31c5Sopenharmony_ci  int err;
70e66f31c5Sopenharmony_ci
71e66f31c5Sopenharmony_ci  if (uv__fd_exists(loop, fd))
72e66f31c5Sopenharmony_ci    return UV_EEXIST;
73e66f31c5Sopenharmony_ci
74e66f31c5Sopenharmony_ci  err = uv__io_check_fd(loop, fd);
75e66f31c5Sopenharmony_ci  if (err)
76e66f31c5Sopenharmony_ci    return err;
77e66f31c5Sopenharmony_ci
78e66f31c5Sopenharmony_ci  /* If ioctl(FIONBIO) reports ENOTTY, try fcntl(F_GETFL) + fcntl(F_SETFL).
79e66f31c5Sopenharmony_ci   * Workaround for e.g. kqueue fds not supporting ioctls.
80e66f31c5Sopenharmony_ci   */
81e66f31c5Sopenharmony_ci  err = uv__nonblock(fd, 1);
82e66f31c5Sopenharmony_ci#if UV__NONBLOCK_IS_IOCTL
83e66f31c5Sopenharmony_ci  if (err == UV_ENOTTY)
84e66f31c5Sopenharmony_ci    err = uv__nonblock_fcntl(fd, 1);
85e66f31c5Sopenharmony_ci#endif
86e66f31c5Sopenharmony_ci
87e66f31c5Sopenharmony_ci  if (err)
88e66f31c5Sopenharmony_ci    return err;
89e66f31c5Sopenharmony_ci
90e66f31c5Sopenharmony_ci  uv__handle_init(loop, (uv_handle_t*) handle, UV_POLL);
91e66f31c5Sopenharmony_ci  uv__io_init(&handle->io_watcher, uv__poll_io, fd);
92e66f31c5Sopenharmony_ci  handle->poll_cb = NULL;
93e66f31c5Sopenharmony_ci  return 0;
94e66f31c5Sopenharmony_ci}
95e66f31c5Sopenharmony_ci
96e66f31c5Sopenharmony_ci
97e66f31c5Sopenharmony_ciint uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
98e66f31c5Sopenharmony_ci    uv_os_sock_t socket) {
99e66f31c5Sopenharmony_ci  return uv_poll_init(loop, handle, socket);
100e66f31c5Sopenharmony_ci}
101e66f31c5Sopenharmony_ci
102e66f31c5Sopenharmony_ci
103e66f31c5Sopenharmony_cistatic void uv__poll_stop(uv_poll_t* handle) {
104e66f31c5Sopenharmony_ci  uv__io_stop(handle->loop,
105e66f31c5Sopenharmony_ci              &handle->io_watcher,
106e66f31c5Sopenharmony_ci              POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
107e66f31c5Sopenharmony_ci  uv__handle_stop(handle);
108e66f31c5Sopenharmony_ci  uv__platform_invalidate_fd(handle->loop, handle->io_watcher.fd);
109e66f31c5Sopenharmony_ci}
110e66f31c5Sopenharmony_ci
111e66f31c5Sopenharmony_ci
112e66f31c5Sopenharmony_ciint uv_poll_stop(uv_poll_t* handle) {
113e66f31c5Sopenharmony_ci  assert(!uv__is_closing(handle));
114e66f31c5Sopenharmony_ci  uv__poll_stop(handle);
115e66f31c5Sopenharmony_ci  return 0;
116e66f31c5Sopenharmony_ci}
117e66f31c5Sopenharmony_ci
118e66f31c5Sopenharmony_ci
119e66f31c5Sopenharmony_ciint uv_poll_start(uv_poll_t* handle, int pevents, uv_poll_cb poll_cb) {
120e66f31c5Sopenharmony_ci  uv__io_t** watchers;
121e66f31c5Sopenharmony_ci  uv__io_t* w;
122e66f31c5Sopenharmony_ci  int events;
123e66f31c5Sopenharmony_ci
124e66f31c5Sopenharmony_ci  assert((pevents & ~(UV_READABLE | UV_WRITABLE | UV_DISCONNECT |
125e66f31c5Sopenharmony_ci                      UV_PRIORITIZED)) == 0);
126e66f31c5Sopenharmony_ci  assert(!uv__is_closing(handle));
127e66f31c5Sopenharmony_ci
128e66f31c5Sopenharmony_ci  watchers = handle->loop->watchers;
129e66f31c5Sopenharmony_ci  w = &handle->io_watcher;
130e66f31c5Sopenharmony_ci
131e66f31c5Sopenharmony_ci  if (uv__fd_exists(handle->loop, w->fd))
132e66f31c5Sopenharmony_ci    if (watchers[w->fd] != w)
133e66f31c5Sopenharmony_ci      return UV_EEXIST;
134e66f31c5Sopenharmony_ci
135e66f31c5Sopenharmony_ci  uv__poll_stop(handle);
136e66f31c5Sopenharmony_ci
137e66f31c5Sopenharmony_ci  if (pevents == 0)
138e66f31c5Sopenharmony_ci    return 0;
139e66f31c5Sopenharmony_ci
140e66f31c5Sopenharmony_ci  events = 0;
141e66f31c5Sopenharmony_ci  if (pevents & UV_READABLE)
142e66f31c5Sopenharmony_ci    events |= POLLIN;
143e66f31c5Sopenharmony_ci  if (pevents & UV_PRIORITIZED)
144e66f31c5Sopenharmony_ci    events |= UV__POLLPRI;
145e66f31c5Sopenharmony_ci  if (pevents & UV_WRITABLE)
146e66f31c5Sopenharmony_ci    events |= POLLOUT;
147e66f31c5Sopenharmony_ci  if (pevents & UV_DISCONNECT)
148e66f31c5Sopenharmony_ci    events |= UV__POLLRDHUP;
149e66f31c5Sopenharmony_ci
150e66f31c5Sopenharmony_ci  uv__io_start(handle->loop, &handle->io_watcher, events);
151e66f31c5Sopenharmony_ci  uv__handle_start(handle);
152e66f31c5Sopenharmony_ci  handle->poll_cb = poll_cb;
153e66f31c5Sopenharmony_ci
154e66f31c5Sopenharmony_ci  return 0;
155e66f31c5Sopenharmony_ci}
156e66f31c5Sopenharmony_ci
157e66f31c5Sopenharmony_ci
158e66f31c5Sopenharmony_civoid uv__poll_close(uv_poll_t* handle) {
159e66f31c5Sopenharmony_ci  uv__poll_stop(handle);
160e66f31c5Sopenharmony_ci}
161