xref: /netbsd-src/external/mit/libuv/dist/src/unix/poll.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos  *
30e552da7Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos  * of this software and associated documentation files (the "Software"), to
50e552da7Schristos  * deal in the Software without restriction, including without limitation the
60e552da7Schristos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos  * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos  * furnished to do so, subject to the following conditions:
90e552da7Schristos  *
100e552da7Schristos  * The above copyright notice and this permission notice shall be included in
110e552da7Schristos  * all copies or substantial portions of the Software.
120e552da7Schristos  *
130e552da7Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos  * IN THE SOFTWARE.
200e552da7Schristos  */
210e552da7Schristos 
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "internal.h"
240e552da7Schristos 
250e552da7Schristos #include <unistd.h>
260e552da7Schristos #include <assert.h>
270e552da7Schristos #include <errno.h>
280e552da7Schristos 
290e552da7Schristos 
uv__poll_io(uv_loop_t * loop,uv__io_t * w,unsigned int events)300e552da7Schristos static void uv__poll_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
310e552da7Schristos   uv_poll_t* handle;
320e552da7Schristos   int pevents;
330e552da7Schristos 
340e552da7Schristos   handle = container_of(w, uv_poll_t, io_watcher);
350e552da7Schristos 
360e552da7Schristos   /*
370e552da7Schristos    * As documented in the kernel source fs/kernfs/file.c #780
380e552da7Schristos    * poll will return POLLERR|POLLPRI in case of sysfs
390e552da7Schristos    * polling. This does not happen in case of out-of-band
400e552da7Schristos    * TCP messages.
410e552da7Schristos    *
420e552da7Schristos    * The above is the case on (at least) FreeBSD and Linux.
430e552da7Schristos    *
440e552da7Schristos    * So to properly determine a POLLPRI or a POLLERR we need
450e552da7Schristos    * to check for both.
460e552da7Schristos    */
470e552da7Schristos   if ((events & POLLERR) && !(events & UV__POLLPRI)) {
480e552da7Schristos     uv__io_stop(loop, w, POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
490e552da7Schristos     uv__handle_stop(handle);
500e552da7Schristos     handle->poll_cb(handle, UV_EBADF, 0);
510e552da7Schristos     return;
520e552da7Schristos   }
530e552da7Schristos 
540e552da7Schristos   pevents = 0;
550e552da7Schristos   if (events & POLLIN)
560e552da7Schristos     pevents |= UV_READABLE;
570e552da7Schristos   if (events & UV__POLLPRI)
580e552da7Schristos     pevents |= UV_PRIORITIZED;
590e552da7Schristos   if (events & POLLOUT)
600e552da7Schristos     pevents |= UV_WRITABLE;
610e552da7Schristos   if (events & UV__POLLRDHUP)
620e552da7Schristos     pevents |= UV_DISCONNECT;
630e552da7Schristos 
640e552da7Schristos   handle->poll_cb(handle, 0, pevents);
650e552da7Schristos }
660e552da7Schristos 
670e552da7Schristos 
uv_poll_init(uv_loop_t * loop,uv_poll_t * handle,int fd)680e552da7Schristos int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) {
690e552da7Schristos   int err;
700e552da7Schristos 
710e552da7Schristos   if (uv__fd_exists(loop, fd))
720e552da7Schristos     return UV_EEXIST;
730e552da7Schristos 
740e552da7Schristos   err = uv__io_check_fd(loop, fd);
750e552da7Schristos   if (err)
760e552da7Schristos     return err;
770e552da7Schristos 
780e552da7Schristos   /* If ioctl(FIONBIO) reports ENOTTY, try fcntl(F_GETFL) + fcntl(F_SETFL).
790e552da7Schristos    * Workaround for e.g. kqueue fds not supporting ioctls.
800e552da7Schristos    */
810e552da7Schristos   err = uv__nonblock(fd, 1);
82*5f2f4271Schristos #if UV__NONBLOCK_IS_IOCTL
830e552da7Schristos   if (err == UV_ENOTTY)
840e552da7Schristos     err = uv__nonblock_fcntl(fd, 1);
85*5f2f4271Schristos #endif
860e552da7Schristos 
870e552da7Schristos   if (err)
880e552da7Schristos     return err;
890e552da7Schristos 
900e552da7Schristos   uv__handle_init(loop, (uv_handle_t*) handle, UV_POLL);
910e552da7Schristos   uv__io_init(&handle->io_watcher, uv__poll_io, fd);
920e552da7Schristos   handle->poll_cb = NULL;
930e552da7Schristos   return 0;
940e552da7Schristos }
950e552da7Schristos 
960e552da7Schristos 
uv_poll_init_socket(uv_loop_t * loop,uv_poll_t * handle,uv_os_sock_t socket)970e552da7Schristos int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
980e552da7Schristos     uv_os_sock_t socket) {
990e552da7Schristos   return uv_poll_init(loop, handle, socket);
1000e552da7Schristos }
1010e552da7Schristos 
1020e552da7Schristos 
uv__poll_stop(uv_poll_t * handle)1030e552da7Schristos static void uv__poll_stop(uv_poll_t* handle) {
1040e552da7Schristos   uv__io_stop(handle->loop,
1050e552da7Schristos               &handle->io_watcher,
1060e552da7Schristos               POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
1070e552da7Schristos   uv__handle_stop(handle);
1080e552da7Schristos   uv__platform_invalidate_fd(handle->loop, handle->io_watcher.fd);
1090e552da7Schristos }
1100e552da7Schristos 
1110e552da7Schristos 
uv_poll_stop(uv_poll_t * handle)1120e552da7Schristos int uv_poll_stop(uv_poll_t* handle) {
1130e552da7Schristos   assert(!uv__is_closing(handle));
1140e552da7Schristos   uv__poll_stop(handle);
1150e552da7Schristos   return 0;
1160e552da7Schristos }
1170e552da7Schristos 
1180e552da7Schristos 
uv_poll_start(uv_poll_t * handle,int pevents,uv_poll_cb poll_cb)1190e552da7Schristos int uv_poll_start(uv_poll_t* handle, int pevents, uv_poll_cb poll_cb) {
120*5f2f4271Schristos   uv__io_t** watchers;
121*5f2f4271Schristos   uv__io_t* w;
1220e552da7Schristos   int events;
1230e552da7Schristos 
1240e552da7Schristos   assert((pevents & ~(UV_READABLE | UV_WRITABLE | UV_DISCONNECT |
1250e552da7Schristos                       UV_PRIORITIZED)) == 0);
1260e552da7Schristos   assert(!uv__is_closing(handle));
1270e552da7Schristos 
128*5f2f4271Schristos   watchers = handle->loop->watchers;
129*5f2f4271Schristos   w = &handle->io_watcher;
130*5f2f4271Schristos 
131*5f2f4271Schristos   if (uv__fd_exists(handle->loop, w->fd))
132*5f2f4271Schristos     if (watchers[w->fd] != w)
133*5f2f4271Schristos       return UV_EEXIST;
134*5f2f4271Schristos 
1350e552da7Schristos   uv__poll_stop(handle);
1360e552da7Schristos 
1370e552da7Schristos   if (pevents == 0)
1380e552da7Schristos     return 0;
1390e552da7Schristos 
1400e552da7Schristos   events = 0;
1410e552da7Schristos   if (pevents & UV_READABLE)
1420e552da7Schristos     events |= POLLIN;
1430e552da7Schristos   if (pevents & UV_PRIORITIZED)
1440e552da7Schristos     events |= UV__POLLPRI;
1450e552da7Schristos   if (pevents & UV_WRITABLE)
1460e552da7Schristos     events |= POLLOUT;
1470e552da7Schristos   if (pevents & UV_DISCONNECT)
1480e552da7Schristos     events |= UV__POLLRDHUP;
1490e552da7Schristos 
1500e552da7Schristos   uv__io_start(handle->loop, &handle->io_watcher, events);
1510e552da7Schristos   uv__handle_start(handle);
1520e552da7Schristos   handle->poll_cb = poll_cb;
1530e552da7Schristos 
1540e552da7Schristos   return 0;
1550e552da7Schristos }
1560e552da7Schristos 
1570e552da7Schristos 
uv__poll_close(uv_poll_t * handle)1580e552da7Schristos void uv__poll_close(uv_poll_t* handle) {
1590e552da7Schristos   uv__poll_stop(handle);
1600e552da7Schristos }
161