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> /* 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 */
55 # include <sys/sysctl.h>
56 # include <sys/wait.h>
59 static uv_loop_t default_loop_struct;
60 static uv_loop_t* default_loop_ptr;
63 void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
64 handle->close_cb = close_cb;
66 switch (handle->type) {
68 uv__pipe_close((uv_pipe_t*)handle);
73 uv__stream_close((uv_stream_t*)handle);
77 uv__udp_close((uv_udp_t*)handle);
81 uv__prepare_close((uv_prepare_t*)handle);
85 uv__check_close((uv_check_t*)handle);
89 uv__idle_close((uv_idle_t*)handle);
93 uv__async_close((uv_async_t*)handle);
97 uv__timer_close((uv_timer_t*)handle);
101 uv__process_close((uv_process_t*)handle);
105 uv__fs_event_close((uv_fs_event_t*)handle);
109 uv__poll_close((uv_poll_t*)handle);
113 uv__fs_poll_close((uv_fs_poll_t*)handle);
120 handle->flags |= UV_CLOSING;
122 handle->next_closing = handle->loop->closing_handles;
123 handle->loop->closing_handles = handle;
127 static void uv__finish_close(uv_handle_t* handle) {
128 assert(!uv__is_active(handle));
129 assert(handle->flags & UV_CLOSING);
130 assert(!(handle->flags & UV_CLOSED));
131 handle->flags |= UV_CLOSED;
133 switch (handle->type) {
148 assert(!uv__io_active(&((uv_stream_t*)handle)->read_watcher));
149 assert(!uv__io_active(&((uv_stream_t*)handle)->write_watcher));
150 assert(((uv_stream_t*)handle)->fd == -1);
151 uv__stream_destroy((uv_stream_t*)handle);
155 uv__udp_finish_close((uv_udp_t*)handle);
163 uv__handle_unref(handle);
164 ngx_queue_remove(&handle->handle_queue);
166 if (handle->close_cb) {
167 handle->close_cb(handle);
172 static void uv__run_closing_handles(uv_loop_t* loop) {
176 p = loop->closing_handles;
177 loop->closing_handles = NULL;
187 int uv_is_closing(const uv_handle_t* handle) {
188 return handle->flags & (UV_CLOSING | UV_CLOSED);
192 uv_loop_t* uv_default_loop(void) {
193 if (default_loop_ptr)
194 return default_loop_ptr;
196 if (uv__loop_init(&default_loop_struct, /* default_loop? */ 1))
199 return (default_loop_ptr = &default_loop_struct);
203 uv_loop_t* uv_loop_new(void) {
206 if ((loop = malloc(sizeof(*loop))) == NULL)
209 if (uv__loop_init(loop, /* default_loop? */ 0)) {
218 void uv_loop_delete(uv_loop_t* loop) {
219 uv__loop_delete(loop);
221 memset(loop, -1, sizeof *loop);
223 if (loop == default_loop_ptr)
224 default_loop_ptr = NULL;
230 static unsigned int uv__poll_timeout(uv_loop_t* loop) {
231 if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
234 if (!ngx_queue_empty(&loop->idle_handles))
237 if (loop->closing_handles)
240 return uv__next_timeout(loop);
244 static void uv__poll(uv_loop_t* loop) {
245 void ev__run(EV_P_ ev_tstamp waittime);
246 ev_invoke_pending(loop->ev);
247 ev__run(loop->ev, uv__poll_timeout(loop) / 1000.);
248 ev_invoke_pending(loop->ev);
252 static int uv__run(uv_loop_t* loop) {
253 uv_update_time(loop);
254 uv__run_timers(loop);
256 uv__run_prepare(loop);
259 uv__run_closing_handles(loop);
260 return uv__has_active_handles(loop) || uv__has_active_reqs(loop);
264 int uv_run(uv_loop_t* loop) {
265 while (uv__run(loop));
270 int uv_run_once(uv_loop_t* loop) {
271 return uv__run(loop);
275 void uv_update_time(uv_loop_t* loop) {
276 loop->time = uv_hrtime() / 1000000;
280 int64_t uv_now(uv_loop_t* loop) {
285 int uv_is_active(const uv_handle_t* handle) {
286 return uv__is_active(handle);
290 static int uv_getaddrinfo_done(eio_req* req_) {
291 uv_getaddrinfo_t* req = req_->data;
292 struct addrinfo *res = req->res;
297 hostlen = strlen(req->hostname);
304 uv__req_unregister(req->loop, req);
310 if (req->retcode == 0) {
312 #if EAI_NODATA /* FreeBSD deprecated EAI_NODATA */
313 } else if (req->retcode == EAI_NONAME || req->retcode == EAI_NODATA) {
315 } else if (req->retcode == EAI_NONAME) {
317 uv__set_sys_error(req->loop, ENOENT); /* FIXME compatibility hack */
319 } else if (req->retcode == EAI_MEMORY && hostlen >= MAXHOSTNAMELEN) {
320 uv__set_sys_error(req->loop, ENOENT);
323 req->loop->last_err.code = UV_EADDRINFO;
324 req->loop->last_err.sys_errno_ = req->retcode;
327 req->cb(req, req->retcode, res);
333 static void getaddrinfo_thread_proc(eio_req *req) {
334 uv_getaddrinfo_t* handle = req->data;
336 handle->retcode = getaddrinfo(handle->hostname,
343 /* stub implementation of uv_getaddrinfo */
344 int uv_getaddrinfo(uv_loop_t* loop,
345 uv_getaddrinfo_t* handle,
346 uv_getaddrinfo_cb cb,
347 const char* hostname,
349 const struct addrinfo* hints) {
353 if (handle == NULL || cb == NULL ||
354 (hostname == NULL && service == NULL)) {
355 uv__set_artificial_error(loop, UV_EINVAL);
359 uv__req_init(loop, handle, UV_GETADDRINFO);
363 /* TODO don't alloc so much. */
366 handle->hints = malloc(sizeof(struct addrinfo));
367 memcpy(handle->hints, hints, sizeof(struct addrinfo));
370 handle->hints = NULL;
373 /* TODO security! check lengths, check return values. */
375 handle->hostname = hostname ? strdup(hostname) : NULL;
376 handle->service = service ? strdup(service) : NULL;
380 /* TODO check handle->hostname == NULL */
381 /* TODO check handle->service == NULL */
383 req = eio_custom(getaddrinfo_thread_proc, EIO_PRI_DEFAULT,
384 uv_getaddrinfo_done, handle, &loop->uv_eio_channel);
386 assert(req->data == handle);
392 void uv_freeaddrinfo(struct addrinfo* ai) {
398 /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
399 int uv__socket(int domain, int type, int protocol) {
402 #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
403 sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
412 sockfd = socket(domain, type, protocol);
417 if (uv__nonblock(sockfd, 1) || uv__cloexec(sockfd, 1)) {
427 int uv__accept(int sockfd) {
434 static int no_accept4;
439 peerfd = uv__accept4(sockfd,
442 UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
457 peerfd = accept(sockfd, NULL, NULL);
466 if (uv__cloexec(peerfd, 1) || uv__nonblock(peerfd, 1)) {
478 int uv__nonblock(int fd, int set) {
483 r = ioctl(fd, FIONBIO, &set);
484 while (r == -1 && errno == EINTR);
491 r = fcntl(fd, F_GETFL);
492 while (r == -1 && errno == EINTR);
498 flags = r | O_NONBLOCK;
500 flags = r & ~O_NONBLOCK;
503 r = fcntl(fd, F_SETFL, flags);
504 while (r == -1 && errno == EINTR);
511 int uv__cloexec(int fd, int set) {
516 /* Linux knows only FD_CLOEXEC so we can safely omit the fcntl(F_GETFD)
517 * syscall. CHECKME: That's probably true for other Unices as well.
525 r = fcntl(fd, F_GETFD);
526 while (r == -1 && errno == EINTR);
532 flags = r | FD_CLOEXEC;
534 flags = r & ~FD_CLOEXEC;
538 r = fcntl(fd, F_SETFD, flags);
539 while (r == -1 && errno == EINTR);
545 /* This function is not execve-safe, there is a race window
546 * between the call to dup() and fcntl(FD_CLOEXEC).
548 int uv__dup(int fd) {
554 if (uv__cloexec(fd, 1)) {
555 SAVE_ERRNO(close(fd));
563 /* TODO move to uv-common.c? */
564 size_t uv__strlcpy(char* dst, const char* src, size_t size) {
572 while (--size && *src) {
581 uv_err_t uv_cwd(char* buffer, size_t size) {
582 if (!buffer || !size) {
583 return uv__new_artificial_error(UV_EINVAL);
586 if (getcwd(buffer, size)) {
589 return uv__new_sys_error(errno);
594 uv_err_t uv_chdir(const char* dir) {
595 if (chdir(dir) == 0) {
598 return uv__new_sys_error(errno);
603 void uv_disable_stdio_inheritance(void) {
606 /* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
607 * first 16 file descriptors. After that, bail out after the first error.
610 if (uv__cloexec(fd, 1) && fd > 15)
615 static void uv__io_set_cb(uv__io_t* handle, uv__io_cb cb) {
616 union { void* data; uv__io_cb cb; } u;
618 handle->io_watcher.data = u.data;
622 static void uv__io_rw(struct ev_loop* ev, ev_io* w, int events) {
623 union { void* data; uv__io_cb cb; } u;
624 uv_loop_t* loop = ev_userdata(ev);
625 uv__io_t* handle = container_of(w, uv__io_t, io_watcher);
626 u.data = handle->io_watcher.data;
627 u.cb(loop, handle, events & (EV_READ|EV_WRITE|EV_ERROR));
631 void uv__io_init(uv__io_t* handle, uv__io_cb cb, int fd, int events) {
632 ev_io_init(&handle->io_watcher, uv__io_rw, fd, events & (EV_READ|EV_WRITE));
633 uv__io_set_cb(handle, cb);
637 void uv__io_set(uv__io_t* handle, uv__io_cb cb, int fd, int events) {
638 ev_io_set(&handle->io_watcher, fd, events);
639 uv__io_set_cb(handle, cb);
643 void uv__io_start(uv_loop_t* loop, uv__io_t* handle) {
644 ev_io_start(loop->ev, &handle->io_watcher);
648 void uv__io_stop(uv_loop_t* loop, uv__io_t* handle) {
649 ev_io_stop(loop->ev, &handle->io_watcher);
653 void uv__io_feed(uv_loop_t* loop, uv__io_t* handle, int event) {
654 ev_feed_event(loop->ev, &handle->io_watcher, event);
658 int uv__io_active(uv__io_t* handle) {
659 return ev_is_active(&handle->io_watcher);