deps: upgrade libuv to 4997738
[platform/upstream/nodejs.git] / deps / uv / src / unix / core.c
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:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
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
18  * IN THE SOFTWARE.
19  */
20
21 #include "uv.h"
22 #include "internal.h"
23
24 #include <stddef.h> /* NULL */
25 #include <stdio.h> /* printf */
26 #include <stdlib.h>
27 #include <string.h> /* strerror */
28 #include <errno.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <limits.h> /* PATH_MAX */
39 #include <sys/uio.h> /* writev */
40
41 #ifdef __linux__
42 # include <sys/ioctl.h>
43 #endif
44
45 #ifdef __sun
46 # include <sys/types.h>
47 # include <sys/wait.h>
48 #endif
49
50 #ifdef __APPLE__
51 # include <mach-o/dyld.h> /* _NSGetExecutablePath */
52 #endif
53
54 #ifdef __FreeBSD__
55 # include <sys/sysctl.h>
56 # include <sys/wait.h>
57 #endif
58
59 static uv_loop_t default_loop_struct;
60 static uv_loop_t* default_loop_ptr;
61
62
63 void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
64   handle->close_cb = close_cb;
65
66   switch (handle->type) {
67   case UV_NAMED_PIPE:
68     uv__pipe_close((uv_pipe_t*)handle);
69     break;
70
71   case UV_TTY:
72   case UV_TCP:
73     uv__stream_close((uv_stream_t*)handle);
74     break;
75
76   case UV_UDP:
77     uv__udp_close((uv_udp_t*)handle);
78     break;
79
80   case UV_PREPARE:
81     uv__prepare_close((uv_prepare_t*)handle);
82     break;
83
84   case UV_CHECK:
85     uv__check_close((uv_check_t*)handle);
86     break;
87
88   case UV_IDLE:
89     uv__idle_close((uv_idle_t*)handle);
90     break;
91
92   case UV_ASYNC:
93     uv__async_close((uv_async_t*)handle);
94     break;
95
96   case UV_TIMER:
97     uv__timer_close((uv_timer_t*)handle);
98     break;
99
100   case UV_PROCESS:
101     uv__process_close((uv_process_t*)handle);
102     break;
103
104   case UV_FS_EVENT:
105     uv__fs_event_close((uv_fs_event_t*)handle);
106     break;
107
108   case UV_POLL:
109     uv__poll_close((uv_poll_t*)handle);
110     break;
111
112   case UV_FS_POLL:
113     uv__fs_poll_close((uv_fs_poll_t*)handle);
114     break;
115
116   default:
117     assert(0);
118   }
119
120   handle->flags |= UV_CLOSING;
121
122   handle->next_closing = handle->loop->closing_handles;
123   handle->loop->closing_handles = handle;
124 }
125
126
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;
132
133   switch (handle->type) {
134     case UV_PREPARE:
135     case UV_CHECK:
136     case UV_IDLE:
137     case UV_ASYNC:
138     case UV_TIMER:
139     case UV_PROCESS:
140     case UV_FS_EVENT:
141     case UV_FS_POLL:
142     case UV_POLL:
143       break;
144
145     case UV_NAMED_PIPE:
146     case UV_TCP:
147     case UV_TTY:
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);
152       break;
153
154     case UV_UDP:
155       uv__udp_finish_close((uv_udp_t*)handle);
156       break;
157
158     default:
159       assert(0);
160       break;
161   }
162
163   uv__handle_unref(handle);
164   ngx_queue_remove(&handle->handle_queue);
165
166   if (handle->close_cb) {
167     handle->close_cb(handle);
168   }
169 }
170
171
172 static void uv__run_closing_handles(uv_loop_t* loop) {
173   uv_handle_t* p;
174   uv_handle_t* q;
175
176   p = loop->closing_handles;
177   loop->closing_handles = NULL;
178
179   while (p) {
180     q = p->next_closing;
181     uv__finish_close(p);
182     p = q;
183   }
184 }
185
186
187 int uv_is_closing(const uv_handle_t* handle) {
188   return handle->flags & (UV_CLOSING | UV_CLOSED);
189 }
190
191
192 uv_loop_t* uv_default_loop(void) {
193   if (default_loop_ptr)
194     return default_loop_ptr;
195
196   if (uv__loop_init(&default_loop_struct, /* default_loop? */ 1))
197     return NULL;
198
199   return (default_loop_ptr = &default_loop_struct);
200 }
201
202
203 uv_loop_t* uv_loop_new(void) {
204   uv_loop_t* loop;
205
206   if ((loop = malloc(sizeof(*loop))) == NULL)
207     return NULL;
208
209   if (uv__loop_init(loop, /* default_loop? */ 0)) {
210     free(loop);
211     return NULL;
212   }
213
214   return loop;
215 }
216
217
218 void uv_loop_delete(uv_loop_t* loop) {
219   uv__loop_delete(loop);
220 #ifndef NDEBUG
221   memset(loop, -1, sizeof *loop);
222 #endif
223   if (loop == default_loop_ptr)
224     default_loop_ptr = NULL;
225   else
226     free(loop);
227 }
228
229
230 static unsigned int uv__poll_timeout(uv_loop_t* loop) {
231   if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
232     return 0;
233
234   if (!ngx_queue_empty(&loop->idle_handles))
235     return 0;
236
237   if (loop->closing_handles)
238     return 0;
239
240   return uv__next_timeout(loop);
241 }
242
243
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);
249 }
250
251
252 static int uv__run(uv_loop_t* loop) {
253   uv_update_time(loop);
254   uv__run_timers(loop);
255   uv__run_idle(loop);
256   uv__run_prepare(loop);
257   uv__poll(loop);
258   uv__run_check(loop);
259   uv__run_closing_handles(loop);
260   return uv__has_active_handles(loop) || uv__has_active_reqs(loop);
261 }
262
263
264 int uv_run(uv_loop_t* loop) {
265   while (uv__run(loop));
266   return 0;
267 }
268
269
270 int uv_run_once(uv_loop_t* loop) {
271   return uv__run(loop);
272 }
273
274
275 void uv_update_time(uv_loop_t* loop) {
276   loop->time = uv_hrtime() / 1000000;
277 }
278
279
280 int64_t uv_now(uv_loop_t* loop) {
281   return loop->time;
282 }
283
284
285 int uv_is_active(const uv_handle_t* handle) {
286   return uv__is_active(handle);
287 }
288
289
290 static int uv_getaddrinfo_done(eio_req* req_) {
291   uv_getaddrinfo_t* req = req_->data;
292   struct addrinfo *res = req->res;
293 #if __sun
294   size_t hostlen;
295
296   if (req->hostname)
297     hostlen = strlen(req->hostname);
298   else
299     hostlen = 0;
300 #endif
301
302   req->res = NULL;
303
304   uv__req_unregister(req->loop, req);
305
306   free(req->hints);
307   free(req->service);
308   free(req->hostname);
309
310   if (req->retcode == 0) {
311     /* OK */
312 #if EAI_NODATA /* FreeBSD deprecated EAI_NODATA */
313   } else if (req->retcode == EAI_NONAME || req->retcode == EAI_NODATA) {
314 #else
315   } else if (req->retcode == EAI_NONAME) {
316 #endif
317     uv__set_sys_error(req->loop, ENOENT); /* FIXME compatibility hack */
318 #if __sun
319   } else if (req->retcode == EAI_MEMORY && hostlen >= MAXHOSTNAMELEN) {
320     uv__set_sys_error(req->loop, ENOENT);
321 #endif
322   } else {
323     req->loop->last_err.code = UV_EADDRINFO;
324     req->loop->last_err.sys_errno_ = req->retcode;
325   }
326
327   req->cb(req, req->retcode, res);
328
329   return 0;
330 }
331
332
333 static void getaddrinfo_thread_proc(eio_req *req) {
334   uv_getaddrinfo_t* handle = req->data;
335
336   handle->retcode = getaddrinfo(handle->hostname,
337                                 handle->service,
338                                 handle->hints,
339                                 &handle->res);
340 }
341
342
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,
348                    const char* service,
349                    const struct addrinfo* hints) {
350   eio_req* req;
351   uv_eio_init(loop);
352
353   if (handle == NULL || cb == NULL ||
354       (hostname == NULL && service == NULL)) {
355     uv__set_artificial_error(loop, UV_EINVAL);
356     return -1;
357   }
358
359   uv__req_init(loop, handle, UV_GETADDRINFO);
360   handle->loop = loop;
361   handle->cb = cb;
362
363   /* TODO don't alloc so much. */
364
365   if (hints) {
366     handle->hints = malloc(sizeof(struct addrinfo));
367     memcpy(handle->hints, hints, sizeof(struct addrinfo));
368   }
369   else {
370     handle->hints = NULL;
371   }
372
373   /* TODO security! check lengths, check return values. */
374
375   handle->hostname = hostname ? strdup(hostname) : NULL;
376   handle->service = service ? strdup(service) : NULL;
377   handle->res = NULL;
378   handle->retcode = 0;
379
380   /* TODO check handle->hostname == NULL */
381   /* TODO check handle->service == NULL */
382
383   req = eio_custom(getaddrinfo_thread_proc, EIO_PRI_DEFAULT,
384       uv_getaddrinfo_done, handle, &loop->uv_eio_channel);
385   assert(req);
386   assert(req->data == handle);
387
388   return 0;
389 }
390
391
392 void uv_freeaddrinfo(struct addrinfo* ai) {
393   if (ai)
394     freeaddrinfo(ai);
395 }
396
397
398 /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
399 int uv__socket(int domain, int type, int protocol) {
400   int sockfd;
401
402 #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
403   sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
404
405   if (sockfd != -1)
406     goto out;
407
408   if (errno != EINVAL)
409     goto out;
410 #endif
411
412   sockfd = socket(domain, type, protocol);
413
414   if (sockfd == -1)
415     goto out;
416
417   if (uv__nonblock(sockfd, 1) || uv__cloexec(sockfd, 1)) {
418     close(sockfd);
419     sockfd = -1;
420   }
421
422 out:
423   return sockfd;
424 }
425
426
427 int uv__accept(int sockfd) {
428   int peerfd;
429
430   assert(sockfd >= 0);
431
432   while (1) {
433 #if __linux__
434     static int no_accept4;
435
436     if (no_accept4)
437       goto skip;
438
439     peerfd = uv__accept4(sockfd,
440                          NULL,
441                          NULL,
442                          UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
443
444     if (peerfd != -1)
445       break;
446
447     if (errno == EINTR)
448       continue;
449
450     if (errno != ENOSYS)
451       break;
452
453     no_accept4 = 1;
454 skip:
455 #endif
456
457     peerfd = accept(sockfd, NULL, NULL);
458
459     if (peerfd == -1) {
460       if (errno == EINTR)
461         continue;
462       else
463         break;
464     }
465
466     if (uv__cloexec(peerfd, 1) || uv__nonblock(peerfd, 1)) {
467       close(peerfd);
468       peerfd = -1;
469     }
470
471     break;
472   }
473
474   return peerfd;
475 }
476
477
478 int uv__nonblock(int fd, int set) {
479   int r;
480
481 #if FIONBIO
482   do
483     r = ioctl(fd, FIONBIO, &set);
484   while (r == -1 && errno == EINTR);
485
486   return r;
487 #else
488   int flags;
489
490   do
491     r = fcntl(fd, F_GETFL);
492   while (r == -1 && errno == EINTR);
493
494   if (r == -1)
495     return -1;
496
497   if (set)
498     flags = r | O_NONBLOCK;
499   else
500     flags = r & ~O_NONBLOCK;
501
502   do
503     r = fcntl(fd, F_SETFL, flags);
504   while (r == -1 && errno == EINTR);
505
506   return r;
507 #endif
508 }
509
510
511 int uv__cloexec(int fd, int set) {
512   int flags;
513   int r;
514
515 #if __linux__
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.
518    */
519   if (set)
520     flags = FD_CLOEXEC;
521   else
522     flags = 0;
523 #else
524   do
525     r = fcntl(fd, F_GETFD);
526   while (r == -1 && errno == EINTR);
527
528   if (r == -1)
529     return -1;
530
531   if (set)
532     flags = r | FD_CLOEXEC;
533   else
534     flags = r & ~FD_CLOEXEC;
535 #endif
536
537   do
538     r = fcntl(fd, F_SETFD, flags);
539   while (r == -1 && errno == EINTR);
540
541   return r;
542 }
543
544
545 /* This function is not execve-safe, there is a race window
546  * between the call to dup() and fcntl(FD_CLOEXEC).
547  */
548 int uv__dup(int fd) {
549   fd = dup(fd);
550
551   if (fd == -1)
552     return -1;
553
554   if (uv__cloexec(fd, 1)) {
555     SAVE_ERRNO(close(fd));
556     return -1;
557   }
558
559   return fd;
560 }
561
562
563 /* TODO move to uv-common.c? */
564 size_t uv__strlcpy(char* dst, const char* src, size_t size) {
565   const char *org;
566
567   if (size == 0) {
568     return 0;
569   }
570
571   org = src;
572   while (--size && *src) {
573     *dst++ = *src++;
574   }
575   *dst = '\0';
576
577   return src - org;
578 }
579
580
581 uv_err_t uv_cwd(char* buffer, size_t size) {
582   if (!buffer || !size) {
583     return uv__new_artificial_error(UV_EINVAL);
584   }
585
586   if (getcwd(buffer, size)) {
587     return uv_ok_;
588   } else {
589     return uv__new_sys_error(errno);
590   }
591 }
592
593
594 uv_err_t uv_chdir(const char* dir) {
595   if (chdir(dir) == 0) {
596     return uv_ok_;
597   } else {
598     return uv__new_sys_error(errno);
599   }
600 }
601
602
603 void uv_disable_stdio_inheritance(void) {
604   int fd;
605
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.
608    */
609   for (fd = 0; ; fd++)
610     if (uv__cloexec(fd, 1) && fd > 15)
611       break;
612 }
613
614
615 static void uv__io_set_cb(uv__io_t* handle, uv__io_cb cb) {
616   union { void* data; uv__io_cb cb; } u;
617   u.cb = cb;
618   handle->io_watcher.data = u.data;
619 }
620
621
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));
628 }
629
630
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);
634 }
635
636
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);
640 }
641
642
643 void uv__io_start(uv_loop_t* loop, uv__io_t* handle) {
644   ev_io_start(loop->ev, &handle->io_watcher);
645 }
646
647
648 void uv__io_stop(uv_loop_t* loop, uv__io_t* handle) {
649   ev_io_stop(loop->ev, &handle->io_watcher);
650 }
651
652
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);
655 }
656
657
658 int uv__io_active(uv__io_t* handle) {
659   return ev_is_active(&handle->io_watcher);
660 }