1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to
4 * deal in the Software without restriction, including without limitation the
5 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 * sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include <stddef.h> /* NULL */
25 #include <stdio.h> /* printf */
27 #include <string.h> /* strerror */
31 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <limits.h> /* INT_MAX, PATH_MAX */
39 #include <sys/uio.h> /* writev */
42 # include <sys/ioctl.h>
46 # include <sys/types.h>
47 # include <sys/wait.h>
51 # include <mach-o/dyld.h> /* _NSGetExecutablePath */
52 # include <sys/filio.h>
53 # include <sys/ioctl.h>
57 # include <sys/sysctl.h>
58 # include <sys/filio.h>
59 # include <sys/ioctl.h>
60 # include <sys/wait.h>
63 static void uv__run_pending(uv_loop_t* loop);
65 static uv_loop_t default_loop_struct;
66 static uv_loop_t* default_loop_ptr;
69 void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
70 assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
72 handle->flags |= UV_CLOSING;
73 handle->close_cb = close_cb;
75 switch (handle->type) {
77 uv__pipe_close((uv_pipe_t*)handle);
81 uv__stream_close((uv_stream_t*)handle);
85 uv__tcp_close((uv_tcp_t*)handle);
89 uv__udp_close((uv_udp_t*)handle);
93 uv__prepare_close((uv_prepare_t*)handle);
97 uv__check_close((uv_check_t*)handle);
101 uv__idle_close((uv_idle_t*)handle);
105 uv__async_close((uv_async_t*)handle);
109 uv__timer_close((uv_timer_t*)handle);
113 uv__process_close((uv_process_t*)handle);
117 uv__fs_event_close((uv_fs_event_t*)handle);
121 uv__poll_close((uv_poll_t*)handle);
125 uv__fs_poll_close((uv_fs_poll_t*)handle);
129 uv__signal_close((uv_signal_t*) handle);
130 /* Signal handles may not be closed immediately. The signal code will */
131 /* itself close uv__make_close_pending whenever appropriate. */
138 uv__make_close_pending(handle);
142 void uv__make_close_pending(uv_handle_t* handle) {
143 assert(handle->flags & UV_CLOSING);
144 assert(!(handle->flags & UV_CLOSED));
145 handle->next_closing = handle->loop->closing_handles;
146 handle->loop->closing_handles = handle;
150 static void uv__finish_close(uv_handle_t* handle) {
151 assert(!uv__is_active(handle));
152 assert(handle->flags & UV_CLOSING);
153 assert(!(handle->flags & UV_CLOSED));
154 handle->flags |= UV_CLOSED;
156 switch (handle->type) {
172 uv__stream_destroy((uv_stream_t*)handle);
176 uv__udp_finish_close((uv_udp_t*)handle);
184 uv__handle_unref(handle);
185 ngx_queue_remove(&handle->handle_queue);
187 if (handle->close_cb) {
188 handle->close_cb(handle);
193 static void uv__run_closing_handles(uv_loop_t* loop) {
197 p = loop->closing_handles;
198 loop->closing_handles = NULL;
208 int uv_is_closing(const uv_handle_t* handle) {
209 return handle->flags & (UV_CLOSING | UV_CLOSED);
213 uv_loop_t* uv_default_loop(void) {
214 if (default_loop_ptr)
215 return default_loop_ptr;
217 if (uv__loop_init(&default_loop_struct, /* default_loop? */ 1))
220 return (default_loop_ptr = &default_loop_struct);
224 uv_loop_t* uv_loop_new(void) {
227 if ((loop = malloc(sizeof(*loop))) == NULL)
230 if (uv__loop_init(loop, /* default_loop? */ 0)) {
239 void uv_loop_delete(uv_loop_t* loop) {
240 uv__loop_delete(loop);
242 memset(loop, -1, sizeof *loop);
244 if (loop == default_loop_ptr)
245 default_loop_ptr = NULL;
251 int uv_backend_fd(const uv_loop_t* loop) {
252 return loop->backend_fd;
256 int uv_backend_timeout(const uv_loop_t* loop) {
257 if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
260 if (!ngx_queue_empty(&loop->idle_handles))
263 if (loop->closing_handles)
266 return uv__next_timeout(loop);
270 static int uv__run(uv_loop_t* loop) {
271 uv_update_time(loop);
272 uv__run_timers(loop);
274 uv__run_prepare(loop);
275 uv__run_pending(loop);
276 uv__io_poll(loop, uv_backend_timeout(loop));
278 uv__run_closing_handles(loop);
279 return uv__has_active_handles(loop) || uv__has_active_reqs(loop);
283 int uv_run(uv_loop_t* loop) {
284 while (uv__run(loop));
289 int uv_run_once(uv_loop_t* loop) {
290 return uv__run(loop);
294 void uv_update_time(uv_loop_t* loop) {
295 loop->time = uv_hrtime() / 1000000;
299 int64_t uv_now(uv_loop_t* loop) {
304 int uv_is_active(const uv_handle_t* handle) {
305 return uv__is_active(handle);
309 /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
310 int uv__socket(int domain, int type, int protocol) {
313 #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
314 sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
323 sockfd = socket(domain, type, protocol);
328 if (uv__nonblock(sockfd, 1) || uv__cloexec(sockfd, 1)) {
333 #if defined(SO_NOSIGPIPE)
336 setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
345 int uv__accept(int sockfd) {
352 static int no_accept4;
357 peerfd = uv__accept4(sockfd,
360 UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
375 peerfd = accept(sockfd, NULL, NULL);
384 if (uv__cloexec(peerfd, 1) || uv__nonblock(peerfd, 1)) {
396 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
398 int uv__nonblock(int fd, int set) {
402 r = ioctl(fd, FIONBIO, &set);
403 while (r == -1 && errno == EINTR);
409 int uv__cloexec(int fd, int set) {
413 r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
414 while (r == -1 && errno == EINTR);
419 #else /* !(defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) */
421 int uv__nonblock(int fd, int set) {
426 r = fcntl(fd, F_GETFL);
427 while (r == -1 && errno == EINTR);
433 flags = r | O_NONBLOCK;
435 flags = r & ~O_NONBLOCK;
438 r = fcntl(fd, F_SETFL, flags);
439 while (r == -1 && errno == EINTR);
445 int uv__cloexec(int fd, int set) {
450 r = fcntl(fd, F_GETFD);
451 while (r == -1 && errno == EINTR);
457 flags = r | FD_CLOEXEC;
459 flags = r & ~FD_CLOEXEC;
462 r = fcntl(fd, F_SETFD, flags);
463 while (r == -1 && errno == EINTR);
468 #endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */
471 /* This function is not execve-safe, there is a race window
472 * between the call to dup() and fcntl(FD_CLOEXEC).
474 int uv__dup(int fd) {
480 if (uv__cloexec(fd, 1)) {
481 SAVE_ERRNO(close(fd));
489 /* TODO move to uv-common.c? */
490 size_t uv__strlcpy(char* dst, const char* src, size_t size) {
498 while (--size && *src) {
507 uv_err_t uv_cwd(char* buffer, size_t size) {
508 if (!buffer || !size) {
509 return uv__new_artificial_error(UV_EINVAL);
512 if (getcwd(buffer, size)) {
515 return uv__new_sys_error(errno);
520 uv_err_t uv_chdir(const char* dir) {
521 if (chdir(dir) == 0) {
524 return uv__new_sys_error(errno);
529 void uv_disable_stdio_inheritance(void) {
532 /* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
533 * first 16 file descriptors. After that, bail out after the first error.
536 if (uv__cloexec(fd, 1) && fd > 15)
541 static void uv__run_pending(uv_loop_t* loop) {
545 while (!ngx_queue_empty(&loop->pending_queue)) {
546 q = ngx_queue_head(&loop->pending_queue);
550 w = ngx_queue_data(q, uv__io_t, pending_queue);
551 w->cb(loop, w, UV__POLLOUT);
556 static unsigned int next_power_of_two(unsigned int val) {
567 static void maybe_resize(uv_loop_t* loop, unsigned int len) {
569 unsigned int nwatchers;
572 if (len <= loop->nwatchers)
575 nwatchers = next_power_of_two(len);
576 watchers = realloc(loop->watchers, nwatchers * sizeof(loop->watchers[0]));
578 if (watchers == NULL)
581 for (i = loop->nwatchers; i < nwatchers; i++)
584 loop->watchers = watchers;
585 loop->nwatchers = nwatchers;
589 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd) {
592 ngx_queue_init(&w->pending_queue);
593 ngx_queue_init(&w->watcher_queue);
601 /* Note that uv__io_start() and uv__io_stop() can't simply remove the watcher
602 * from the queue when the new event mask equals the old one. The event ports
603 * backend operates exclusively in single-shot mode and needs to rearm all fds
604 * before each call to port_getn(). It's up to the individual backends to
605 * filter out superfluous event mask modifications.
609 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
610 assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
613 assert(w->fd < INT_MAX);
615 w->pevents |= events;
616 maybe_resize(loop, w->fd + 1);
618 if (ngx_queue_empty(&w->watcher_queue))
619 ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
621 if (loop->watchers[w->fd] == NULL) {
622 loop->watchers[w->fd] = w;
628 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
629 assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
637 /* Happens when uv__io_stop() is called on a handle that was never started. */
638 if ((unsigned) w->fd >= loop->nwatchers)
641 w->pevents &= ~events;
643 if (w->pevents == 0) {
644 ngx_queue_remove(&w->watcher_queue);
645 ngx_queue_init(&w->watcher_queue);
647 if (loop->watchers[w->fd] != NULL) {
648 assert(loop->watchers[w->fd] == w);
649 assert(loop->nfds > 0);
650 loop->watchers[w->fd] = NULL;
655 else if (ngx_queue_empty(&w->watcher_queue))
656 ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
660 void uv__io_close(uv_loop_t* loop, uv__io_t* w) {
661 uv__io_stop(loop, w, UV__POLLIN | UV__POLLOUT);
662 ngx_queue_remove(&w->pending_queue);
666 void uv__io_feed(uv_loop_t* loop, uv__io_t* w) {
667 if (ngx_queue_empty(&w->pending_queue))
668 ngx_queue_insert_tail(&loop->pending_queue, &w->pending_queue);
672 int uv__io_active(const uv__io_t* w, unsigned int events) {
673 assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
675 return 0 != (w->pevents & events);