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 /* Verify that uv_buf_t is ABI-compatible with struct iovec. */
66 STATIC_ASSERT(sizeof(uv_buf_t) == sizeof(struct iovec));
67 STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->base) ==
68 sizeof(((struct iovec*) 0)->iov_base));
69 STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->len) ==
70 sizeof(((struct iovec*) 0)->iov_len));
71 STATIC_ASSERT(offsetof(uv_buf_t, base) == offsetof(struct iovec, iov_base));
72 STATIC_ASSERT(offsetof(uv_buf_t, len) == offsetof(struct iovec, iov_len));
75 uint64_t uv_hrtime(void) {
80 void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
81 assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
83 handle->flags |= UV_CLOSING;
84 handle->close_cb = close_cb;
86 switch (handle->type) {
88 uv__pipe_close((uv_pipe_t*)handle);
92 uv__stream_close((uv_stream_t*)handle);
96 uv__tcp_close((uv_tcp_t*)handle);
100 uv__udp_close((uv_udp_t*)handle);
104 uv__prepare_close((uv_prepare_t*)handle);
108 uv__check_close((uv_check_t*)handle);
112 uv__idle_close((uv_idle_t*)handle);
116 uv__async_close((uv_async_t*)handle);
120 uv__timer_close((uv_timer_t*)handle);
124 uv__process_close((uv_process_t*)handle);
128 uv__fs_event_close((uv_fs_event_t*)handle);
132 uv__poll_close((uv_poll_t*)handle);
136 uv__fs_poll_close((uv_fs_poll_t*)handle);
140 uv__signal_close((uv_signal_t*) handle);
141 /* Signal handles may not be closed immediately. The signal code will */
142 /* itself close uv__make_close_pending whenever appropriate. */
149 uv__make_close_pending(handle);
153 void uv__make_close_pending(uv_handle_t* handle) {
154 assert(handle->flags & UV_CLOSING);
155 assert(!(handle->flags & UV_CLOSED));
156 handle->next_closing = handle->loop->closing_handles;
157 handle->loop->closing_handles = handle;
161 static void uv__finish_close(uv_handle_t* handle) {
162 /* Note: while the handle is in the UV_CLOSING state now, it's still possible
163 * for it to be active in the sense that uv__is_active() returns true.
164 * A good example is when the user calls uv_shutdown(), immediately followed
165 * by uv_close(). The handle is considered active at this point because the
166 * completion of the shutdown req is still pending.
168 assert(handle->flags & UV_CLOSING);
169 assert(!(handle->flags & UV_CLOSED));
170 handle->flags |= UV_CLOSED;
172 switch (handle->type) {
188 uv__stream_destroy((uv_stream_t*)handle);
192 uv__udp_finish_close((uv_udp_t*)handle);
200 uv__handle_unref(handle);
201 QUEUE_REMOVE(&handle->handle_queue);
203 if (handle->close_cb) {
204 handle->close_cb(handle);
209 static void uv__run_closing_handles(uv_loop_t* loop) {
213 p = loop->closing_handles;
214 loop->closing_handles = NULL;
224 int uv_is_closing(const uv_handle_t* handle) {
225 return uv__is_closing(handle);
229 int uv_backend_fd(const uv_loop_t* loop) {
230 return loop->backend_fd;
234 int uv_backend_timeout(const uv_loop_t* loop) {
235 if (loop->stop_flag != 0)
238 if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
241 if (!QUEUE_EMPTY(&loop->idle_handles))
244 if (loop->closing_handles)
247 return uv__next_timeout(loop);
251 static int uv__loop_alive(uv_loop_t* loop) {
252 return uv__has_active_handles(loop) ||
253 uv__has_active_reqs(loop) ||
254 loop->closing_handles != NULL;
258 int uv_run(uv_loop_t* loop, uv_run_mode mode) {
262 r = uv__loop_alive(loop);
263 while (r != 0 && loop->stop_flag == 0) {
264 UV_TICK_START(loop, mode);
266 uv__update_time(loop);
267 uv__run_timers(loop);
269 uv__run_prepare(loop);
270 uv__run_pending(loop);
273 if ((mode & UV_RUN_NOWAIT) == 0)
274 timeout = uv_backend_timeout(loop);
276 uv__io_poll(loop, timeout);
278 uv__run_closing_handles(loop);
280 if (mode == UV_RUN_ONCE) {
281 /* UV_RUN_ONCE implies forward progess: at least one callback must have
282 * been invoked when it returns. uv__io_poll() can return without doing
283 * I/O (meaning: no callbacks) when its timeout expires - which means we
284 * have pending timers that satisfy the forward progress constraint.
286 * UV_RUN_NOWAIT makes no guarantees about progress so it's omitted from
289 uv__update_time(loop);
290 uv__run_timers(loop);
293 r = uv__loop_alive(loop);
294 UV_TICK_STOP(loop, mode);
296 if (mode & (UV_RUN_ONCE | UV_RUN_NOWAIT))
300 /* The if statement lets gcc compile it to a conditional store. Avoids
301 * dirtying a cache line.
303 if (loop->stop_flag != 0)
310 void uv_update_time(uv_loop_t* loop) {
311 uv__update_time(loop);
315 int uv_is_active(const uv_handle_t* handle) {
316 return uv__is_active(handle);
320 /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
321 int uv__socket(int domain, int type, int protocol) {
325 #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
326 sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
334 sockfd = socket(domain, type, protocol);
338 err = uv__nonblock(sockfd, 1);
340 err = uv__cloexec(sockfd, 1);
347 #if defined(SO_NOSIGPIPE)
350 setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
358 int uv__accept(int sockfd) {
365 #if defined(__linux__)
366 static int no_accept4;
371 peerfd = uv__accept4(sockfd,
374 UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
388 peerfd = accept(sockfd, NULL, NULL);
395 err = uv__cloexec(peerfd, 1);
397 err = uv__nonblock(peerfd, 1);
409 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
411 int uv__nonblock(int fd, int set) {
415 r = ioctl(fd, FIONBIO, &set);
416 while (r == -1 && errno == EINTR);
425 int uv__cloexec(int fd, int set) {
429 r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
430 while (r == -1 && errno == EINTR);
438 #else /* !(defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) */
440 int uv__nonblock(int fd, int set) {
445 r = fcntl(fd, F_GETFL);
446 while (r == -1 && errno == EINTR);
451 /* Bail out now if already set/clear. */
452 if (!!(r & O_NONBLOCK) == !!set)
456 flags = r | O_NONBLOCK;
458 flags = r & ~O_NONBLOCK;
461 r = fcntl(fd, F_SETFL, flags);
462 while (r == -1 && errno == EINTR);
471 int uv__cloexec(int fd, int set) {
476 r = fcntl(fd, F_GETFD);
477 while (r == -1 && errno == EINTR);
482 /* Bail out now if already set/clear. */
483 if (!!(r & FD_CLOEXEC) == !!set)
487 flags = r | FD_CLOEXEC;
489 flags = r & ~FD_CLOEXEC;
492 r = fcntl(fd, F_SETFD, flags);
493 while (r == -1 && errno == EINTR);
501 #endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */
504 /* This function is not execve-safe, there is a race window
505 * between the call to dup() and fcntl(FD_CLOEXEC).
507 int uv__dup(int fd) {
515 err = uv__cloexec(fd, 1);
525 int uv_cwd(char* buffer, size_t size) {
532 if (getcwd(buffer, size) == NULL)
539 int uv_chdir(const char* dir) {
547 void uv_disable_stdio_inheritance(void) {
550 /* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
551 * first 16 file descriptors. After that, bail out after the first error.
554 if (uv__cloexec(fd, 1) && fd > 15)
559 static void uv__run_pending(uv_loop_t* loop) {
563 while (!QUEUE_EMPTY(&loop->pending_queue)) {
564 q = QUEUE_HEAD(&loop->pending_queue);
568 w = QUEUE_DATA(q, uv__io_t, pending_queue);
569 w->cb(loop, w, UV__POLLOUT);
574 static unsigned int next_power_of_two(unsigned int val) {
585 static void maybe_resize(uv_loop_t* loop, unsigned int len) {
587 unsigned int nwatchers;
590 if (len <= loop->nwatchers)
593 nwatchers = next_power_of_two(len);
594 watchers = realloc(loop->watchers, nwatchers * sizeof(loop->watchers[0]));
596 if (watchers == NULL)
599 for (i = loop->nwatchers; i < nwatchers; i++)
602 loop->watchers = watchers;
603 loop->nwatchers = nwatchers;
607 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd) {
610 QUEUE_INIT(&w->pending_queue);
611 QUEUE_INIT(&w->watcher_queue);
617 #if defined(UV_HAVE_KQUEUE)
620 #endif /* defined(UV_HAVE_KQUEUE) */
624 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
625 assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
628 assert(w->fd < INT_MAX);
630 w->pevents |= events;
631 maybe_resize(loop, w->fd + 1);
634 /* The event ports backend needs to rearm all file descriptors on each and
635 * every tick of the event loop but the other backends allow us to
636 * short-circuit here if the event mask is unchanged.
638 if (w->events == w->pevents) {
639 if (w->events == 0 && !QUEUE_EMPTY(&w->watcher_queue)) {
640 QUEUE_REMOVE(&w->watcher_queue);
641 QUEUE_INIT(&w->watcher_queue);
647 if (QUEUE_EMPTY(&w->watcher_queue))
648 QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
650 if (loop->watchers[w->fd] == NULL) {
651 loop->watchers[w->fd] = w;
657 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
658 assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
666 /* Happens when uv__io_stop() is called on a handle that was never started. */
667 if ((unsigned) w->fd >= loop->nwatchers)
670 w->pevents &= ~events;
672 if (w->pevents == 0) {
673 QUEUE_REMOVE(&w->watcher_queue);
674 QUEUE_INIT(&w->watcher_queue);
676 if (loop->watchers[w->fd] != NULL) {
677 assert(loop->watchers[w->fd] == w);
678 assert(loop->nfds > 0);
679 loop->watchers[w->fd] = NULL;
684 else if (QUEUE_EMPTY(&w->watcher_queue))
685 QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
689 void uv__io_close(uv_loop_t* loop, uv__io_t* w) {
690 uv__io_stop(loop, w, UV__POLLIN | UV__POLLOUT);
691 QUEUE_REMOVE(&w->pending_queue);
695 void uv__io_feed(uv_loop_t* loop, uv__io_t* w) {
696 if (QUEUE_EMPTY(&w->pending_queue))
697 QUEUE_INSERT_TAIL(&loop->pending_queue, &w->pending_queue);
701 int uv__io_active(const uv__io_t* w, unsigned int events) {
702 assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
704 return 0 != (w->pevents & events);