deps: upgrade libuv to e079a99
[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> /* INT_MAX, 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 # include <sys/filio.h>
53 # include <sys/ioctl.h>
54 #endif
55
56 #ifdef __FreeBSD__
57 # include <sys/sysctl.h>
58 # include <sys/filio.h>
59 # include <sys/ioctl.h>
60 # include <sys/wait.h>
61 #endif
62
63 static void uv__run_pending(uv_loop_t* loop);
64
65 static uv_loop_t default_loop_struct;
66 static uv_loop_t* default_loop_ptr;
67
68
69 void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
70   assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
71
72   handle->flags |= UV_CLOSING;
73   handle->close_cb = close_cb;
74
75   switch (handle->type) {
76   case UV_NAMED_PIPE:
77     uv__pipe_close((uv_pipe_t*)handle);
78     break;
79
80   case UV_TTY:
81     uv__stream_close((uv_stream_t*)handle);
82     break;
83
84   case UV_TCP:
85     uv__tcp_close((uv_tcp_t*)handle);
86     break;
87
88   case UV_UDP:
89     uv__udp_close((uv_udp_t*)handle);
90     break;
91
92   case UV_PREPARE:
93     uv__prepare_close((uv_prepare_t*)handle);
94     break;
95
96   case UV_CHECK:
97     uv__check_close((uv_check_t*)handle);
98     break;
99
100   case UV_IDLE:
101     uv__idle_close((uv_idle_t*)handle);
102     break;
103
104   case UV_ASYNC:
105     uv__async_close((uv_async_t*)handle);
106     break;
107
108   case UV_TIMER:
109     uv__timer_close((uv_timer_t*)handle);
110     break;
111
112   case UV_PROCESS:
113     uv__process_close((uv_process_t*)handle);
114     break;
115
116   case UV_FS_EVENT:
117     uv__fs_event_close((uv_fs_event_t*)handle);
118     break;
119
120   case UV_POLL:
121     uv__poll_close((uv_poll_t*)handle);
122     break;
123
124   case UV_FS_POLL:
125     uv__fs_poll_close((uv_fs_poll_t*)handle);
126     break;
127
128   case UV_SIGNAL:
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. */
132     return;
133
134   default:
135     assert(0);
136   }
137
138   uv__make_close_pending(handle);
139 }
140
141
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;
147 }
148
149
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;
155
156   switch (handle->type) {
157     case UV_PREPARE:
158     case UV_CHECK:
159     case UV_IDLE:
160     case UV_ASYNC:
161     case UV_TIMER:
162     case UV_PROCESS:
163     case UV_FS_EVENT:
164     case UV_FS_POLL:
165     case UV_POLL:
166     case UV_SIGNAL:
167       break;
168
169     case UV_NAMED_PIPE:
170     case UV_TCP:
171     case UV_TTY:
172       uv__stream_destroy((uv_stream_t*)handle);
173       break;
174
175     case UV_UDP:
176       uv__udp_finish_close((uv_udp_t*)handle);
177       break;
178
179     default:
180       assert(0);
181       break;
182   }
183
184   uv__handle_unref(handle);
185   ngx_queue_remove(&handle->handle_queue);
186
187   if (handle->close_cb) {
188     handle->close_cb(handle);
189   }
190 }
191
192
193 static void uv__run_closing_handles(uv_loop_t* loop) {
194   uv_handle_t* p;
195   uv_handle_t* q;
196
197   p = loop->closing_handles;
198   loop->closing_handles = NULL;
199
200   while (p) {
201     q = p->next_closing;
202     uv__finish_close(p);
203     p = q;
204   }
205 }
206
207
208 int uv_is_closing(const uv_handle_t* handle) {
209   return handle->flags & (UV_CLOSING | UV_CLOSED);
210 }
211
212
213 uv_loop_t* uv_default_loop(void) {
214   if (default_loop_ptr)
215     return default_loop_ptr;
216
217   if (uv__loop_init(&default_loop_struct, /* default_loop? */ 1))
218     return NULL;
219
220   return (default_loop_ptr = &default_loop_struct);
221 }
222
223
224 uv_loop_t* uv_loop_new(void) {
225   uv_loop_t* loop;
226
227   if ((loop = malloc(sizeof(*loop))) == NULL)
228     return NULL;
229
230   if (uv__loop_init(loop, /* default_loop? */ 0)) {
231     free(loop);
232     return NULL;
233   }
234
235   return loop;
236 }
237
238
239 void uv_loop_delete(uv_loop_t* loop) {
240   uv__loop_delete(loop);
241 #ifndef NDEBUG
242   memset(loop, -1, sizeof *loop);
243 #endif
244   if (loop == default_loop_ptr)
245     default_loop_ptr = NULL;
246   else
247     free(loop);
248 }
249
250
251 int uv_backend_fd(const uv_loop_t* loop) {
252   return loop->backend_fd;
253 }
254
255
256 int uv_backend_timeout(const uv_loop_t* loop) {
257   if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
258     return 0;
259
260   if (!ngx_queue_empty(&loop->idle_handles))
261     return 0;
262
263   if (loop->closing_handles)
264     return 0;
265
266   return uv__next_timeout(loop);
267 }
268
269
270 static int uv__run(uv_loop_t* loop) {
271   uv_update_time(loop);
272   uv__run_timers(loop);
273   uv__run_idle(loop);
274   uv__run_prepare(loop);
275   uv__run_pending(loop);
276   uv__io_poll(loop, uv_backend_timeout(loop));
277   uv__run_check(loop);
278   uv__run_closing_handles(loop);
279   return uv__has_active_handles(loop) || uv__has_active_reqs(loop);
280 }
281
282
283 int uv_run(uv_loop_t* loop) {
284   while (uv__run(loop));
285   return 0;
286 }
287
288
289 int uv_run_once(uv_loop_t* loop) {
290   return uv__run(loop);
291 }
292
293
294 void uv_update_time(uv_loop_t* loop) {
295   loop->time = uv_hrtime() / 1000000;
296 }
297
298
299 int64_t uv_now(uv_loop_t* loop) {
300   return loop->time;
301 }
302
303
304 int uv_is_active(const uv_handle_t* handle) {
305   return uv__is_active(handle);
306 }
307
308
309 /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
310 int uv__socket(int domain, int type, int protocol) {
311   int sockfd;
312
313 #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
314   sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
315
316   if (sockfd != -1)
317     goto out;
318
319   if (errno != EINVAL)
320     goto out;
321 #endif
322
323   sockfd = socket(domain, type, protocol);
324
325   if (sockfd == -1)
326     goto out;
327
328   if (uv__nonblock(sockfd, 1) || uv__cloexec(sockfd, 1)) {
329     close(sockfd);
330     sockfd = -1;
331   }
332
333 #if defined(SO_NOSIGPIPE)
334   {
335     int on = 1;
336     setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
337   }
338 #endif
339
340 out:
341   return sockfd;
342 }
343
344
345 int uv__accept(int sockfd) {
346   int peerfd;
347
348   assert(sockfd >= 0);
349
350   while (1) {
351 #if __linux__
352     static int no_accept4;
353
354     if (no_accept4)
355       goto skip;
356
357     peerfd = uv__accept4(sockfd,
358                          NULL,
359                          NULL,
360                          UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
361
362     if (peerfd != -1)
363       break;
364
365     if (errno == EINTR)
366       continue;
367
368     if (errno != ENOSYS)
369       break;
370
371     no_accept4 = 1;
372 skip:
373 #endif
374
375     peerfd = accept(sockfd, NULL, NULL);
376
377     if (peerfd == -1) {
378       if (errno == EINTR)
379         continue;
380       else
381         break;
382     }
383
384     if (uv__cloexec(peerfd, 1) || uv__nonblock(peerfd, 1)) {
385       close(peerfd);
386       peerfd = -1;
387     }
388
389     break;
390   }
391
392   return peerfd;
393 }
394
395
396 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
397
398 int uv__nonblock(int fd, int set) {
399   int r;
400
401   do
402     r = ioctl(fd, FIONBIO, &set);
403   while (r == -1 && errno == EINTR);
404
405   return r;
406 }
407
408
409 int uv__cloexec(int fd, int set) {
410   int r;
411
412   do
413     r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
414   while (r == -1 && errno == EINTR);
415
416   return r;
417 }
418
419 #else /* !(defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) */
420
421 int uv__nonblock(int fd, int set) {
422   int flags;
423   int r;
424
425   do
426     r = fcntl(fd, F_GETFL);
427   while (r == -1 && errno == EINTR);
428
429   if (r == -1)
430     return -1;
431
432   if (set)
433     flags = r | O_NONBLOCK;
434   else
435     flags = r & ~O_NONBLOCK;
436
437   do
438     r = fcntl(fd, F_SETFL, flags);
439   while (r == -1 && errno == EINTR);
440
441   return r;
442 }
443
444
445 int uv__cloexec(int fd, int set) {
446   int flags;
447   int r;
448
449   do
450     r = fcntl(fd, F_GETFD);
451   while (r == -1 && errno == EINTR);
452
453   if (r == -1)
454     return -1;
455
456   if (set)
457     flags = r | FD_CLOEXEC;
458   else
459     flags = r & ~FD_CLOEXEC;
460
461   do
462     r = fcntl(fd, F_SETFD, flags);
463   while (r == -1 && errno == EINTR);
464
465   return r;
466 }
467
468 #endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */
469
470
471 /* This function is not execve-safe, there is a race window
472  * between the call to dup() and fcntl(FD_CLOEXEC).
473  */
474 int uv__dup(int fd) {
475   fd = dup(fd);
476
477   if (fd == -1)
478     return -1;
479
480   if (uv__cloexec(fd, 1)) {
481     SAVE_ERRNO(close(fd));
482     return -1;
483   }
484
485   return fd;
486 }
487
488
489 /* TODO move to uv-common.c? */
490 size_t uv__strlcpy(char* dst, const char* src, size_t size) {
491   const char *org;
492
493   if (size == 0) {
494     return 0;
495   }
496
497   org = src;
498   while (--size && *src) {
499     *dst++ = *src++;
500   }
501   *dst = '\0';
502
503   return src - org;
504 }
505
506
507 uv_err_t uv_cwd(char* buffer, size_t size) {
508   if (!buffer || !size) {
509     return uv__new_artificial_error(UV_EINVAL);
510   }
511
512   if (getcwd(buffer, size)) {
513     return uv_ok_;
514   } else {
515     return uv__new_sys_error(errno);
516   }
517 }
518
519
520 uv_err_t uv_chdir(const char* dir) {
521   if (chdir(dir) == 0) {
522     return uv_ok_;
523   } else {
524     return uv__new_sys_error(errno);
525   }
526 }
527
528
529 void uv_disable_stdio_inheritance(void) {
530   int fd;
531
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.
534    */
535   for (fd = 0; ; fd++)
536     if (uv__cloexec(fd, 1) && fd > 15)
537       break;
538 }
539
540
541 static void uv__run_pending(uv_loop_t* loop) {
542   ngx_queue_t* q;
543   uv__io_t* w;
544
545   while (!ngx_queue_empty(&loop->pending_queue)) {
546     q = ngx_queue_head(&loop->pending_queue);
547     ngx_queue_remove(q);
548     ngx_queue_init(q);
549
550     w = ngx_queue_data(q, uv__io_t, pending_queue);
551     w->cb(loop, w, UV__POLLOUT);
552   }
553 }
554
555
556 static unsigned int next_power_of_two(unsigned int val) {
557   val -= 1;
558   val |= val >> 1;
559   val |= val >> 2;
560   val |= val >> 4;
561   val |= val >> 8;
562   val |= val >> 16;
563   val += 1;
564   return val;
565 }
566
567 static void maybe_resize(uv_loop_t* loop, unsigned int len) {
568   uv__io_t** watchers;
569   unsigned int nwatchers;
570   unsigned int i;
571
572   if (len <= loop->nwatchers)
573     return;
574
575   nwatchers = next_power_of_two(len);
576   watchers = realloc(loop->watchers, nwatchers * sizeof(loop->watchers[0]));
577
578   if (watchers == NULL)
579     abort();
580
581   for (i = loop->nwatchers; i < nwatchers; i++)
582     watchers[i] = NULL;
583
584   loop->watchers = watchers;
585   loop->nwatchers = nwatchers;
586 }
587
588
589 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd) {
590   assert(cb != NULL);
591   assert(fd >= -1);
592   ngx_queue_init(&w->pending_queue);
593   ngx_queue_init(&w->watcher_queue);
594   w->cb = cb;
595   w->fd = fd;
596   w->events = 0;
597   w->pevents = 0;
598 }
599
600
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.
606  */
607
608
609 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
610   assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
611   assert(0 != events);
612   assert(w->fd >= 0);
613   assert(w->fd < INT_MAX);
614
615   w->pevents |= events;
616   maybe_resize(loop, w->fd + 1);
617
618   if (ngx_queue_empty(&w->watcher_queue))
619     ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
620
621   if (loop->watchers[w->fd] == NULL) {
622     loop->watchers[w->fd] = w;
623     loop->nfds++;
624   }
625 }
626
627
628 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
629   assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
630   assert(0 != events);
631
632   if (w->fd == -1)
633     return;
634
635   assert(w->fd >= 0);
636
637   /* Happens when uv__io_stop() is called on a handle that was never started. */
638   if ((unsigned) w->fd >= loop->nwatchers)
639     return;
640
641   w->pevents &= ~events;
642
643   if (w->pevents == 0) {
644     ngx_queue_remove(&w->watcher_queue);
645     ngx_queue_init(&w->watcher_queue);
646
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;
651       loop->nfds--;
652       w->events = 0;
653     }
654   }
655   else if (ngx_queue_empty(&w->watcher_queue))
656     ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
657 }
658
659
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);
663 }
664
665
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);
669 }
670
671
672 int uv__io_active(const uv__io_t* w, unsigned int events) {
673   assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
674   assert(0 != events);
675   return 0 != (w->pevents & events);
676 }