xref: /netbsd-src/external/mit/libuv/dist/src/unix/core.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
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>  /* O_CLOEXEC */
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <limits.h> /* INT_MAX, PATH_MAX, IOV_MAX */
40 #include <sys/uio.h> /* writev */
41 #include <sys/resource.h> /* getrusage */
42 #include <pwd.h>
43 #include <sys/utsname.h>
44 #include <sys/time.h>
45 
46 #ifdef __sun
47 # include <sys/filio.h>
48 # include <sys/types.h>
49 # include <sys/wait.h>
50 #endif
51 
52 #if defined(__APPLE__)
53 # include <sys/filio.h>
54 # endif /* defined(__APPLE__) */
55 
56 
57 #if defined(__APPLE__) && !TARGET_OS_IPHONE
58 # include <crt_externs.h>
59 # include <mach-o/dyld.h> /* _NSGetExecutablePath */
60 # define environ (*_NSGetEnviron())
61 #else /* defined(__APPLE__) && !TARGET_OS_IPHONE */
62 extern char** environ;
63 #endif /* !(defined(__APPLE__) && !TARGET_OS_IPHONE) */
64 
65 
66 #if defined(__DragonFly__)      || \
67     defined(__FreeBSD__)        || \
68     defined(__FreeBSD_kernel__) || \
69     defined(__NetBSD__)         || \
70     defined(__OpenBSD__)
71 # include <sys/sysctl.h>
72 # include <sys/filio.h>
73 # include <sys/wait.h>
74 # if defined(__FreeBSD__)
75 #  define uv__accept4 accept4
76 # endif
77 # if defined(__NetBSD__)
78 #  define uv__accept4(a, b, c, d) paccept((a), (b), (c), NULL, (d))
79 # endif
80 #endif
81 
82 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
83 # include <dlfcn.h>  /* for dlsym */
84 #endif
85 
86 #if defined(__MVS__)
87 #include <sys/ioctl.h>
88 #endif
89 
90 #if defined(__linux__)
91 # include <sys/syscall.h>
92 # define uv__accept4 accept4
93 #endif
94 
95 static int uv__run_pending(uv_loop_t* loop);
96 
97 /* Verify that uv_buf_t is ABI-compatible with struct iovec. */
98 STATIC_ASSERT(sizeof(uv_buf_t) == sizeof(struct iovec));
99 STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->base) ==
100               sizeof(((struct iovec*) 0)->iov_base));
101 STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->len) ==
102               sizeof(((struct iovec*) 0)->iov_len));
103 STATIC_ASSERT(offsetof(uv_buf_t, base) == offsetof(struct iovec, iov_base));
104 STATIC_ASSERT(offsetof(uv_buf_t, len) == offsetof(struct iovec, iov_len));
105 
106 
107 uint64_t uv_hrtime(void) {
108   return uv__hrtime(UV_CLOCK_PRECISE);
109 }
110 
111 
112 void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
113   assert(!uv__is_closing(handle));
114 
115   handle->flags |= UV_HANDLE_CLOSING;
116   handle->close_cb = close_cb;
117 
118   switch (handle->type) {
119   case UV_NAMED_PIPE:
120     uv__pipe_close((uv_pipe_t*)handle);
121     break;
122 
123   case UV_TTY:
124     uv__stream_close((uv_stream_t*)handle);
125     break;
126 
127   case UV_TCP:
128     uv__tcp_close((uv_tcp_t*)handle);
129     break;
130 
131   case UV_UDP:
132     uv__udp_close((uv_udp_t*)handle);
133     break;
134 
135   case UV_PREPARE:
136     uv__prepare_close((uv_prepare_t*)handle);
137     break;
138 
139   case UV_CHECK:
140     uv__check_close((uv_check_t*)handle);
141     break;
142 
143   case UV_IDLE:
144     uv__idle_close((uv_idle_t*)handle);
145     break;
146 
147   case UV_ASYNC:
148     uv__async_close((uv_async_t*)handle);
149     break;
150 
151   case UV_TIMER:
152     uv__timer_close((uv_timer_t*)handle);
153     break;
154 
155   case UV_PROCESS:
156     uv__process_close((uv_process_t*)handle);
157     break;
158 
159   case UV_FS_EVENT:
160     uv__fs_event_close((uv_fs_event_t*)handle);
161     break;
162 
163   case UV_POLL:
164     uv__poll_close((uv_poll_t*)handle);
165     break;
166 
167   case UV_FS_POLL:
168     uv__fs_poll_close((uv_fs_poll_t*)handle);
169     /* Poll handles use file system requests, and one of them may still be
170      * running. The poll code will call uv__make_close_pending() for us. */
171     return;
172 
173   case UV_SIGNAL:
174     uv__signal_close((uv_signal_t*) handle);
175     break;
176 
177   default:
178     assert(0);
179   }
180 
181   uv__make_close_pending(handle);
182 }
183 
184 int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value) {
185   int r;
186   int fd;
187   socklen_t len;
188 
189   if (handle == NULL || value == NULL)
190     return UV_EINVAL;
191 
192   if (handle->type == UV_TCP || handle->type == UV_NAMED_PIPE)
193     fd = uv__stream_fd((uv_stream_t*) handle);
194   else if (handle->type == UV_UDP)
195     fd = ((uv_udp_t *) handle)->io_watcher.fd;
196   else
197     return UV_ENOTSUP;
198 
199   len = sizeof(*value);
200 
201   if (*value == 0)
202     r = getsockopt(fd, SOL_SOCKET, optname, value, &len);
203   else
204     r = setsockopt(fd, SOL_SOCKET, optname, (const void*) value, len);
205 
206   if (r < 0)
207     return UV__ERR(errno);
208 
209   return 0;
210 }
211 
212 void uv__make_close_pending(uv_handle_t* handle) {
213   assert(handle->flags & UV_HANDLE_CLOSING);
214   assert(!(handle->flags & UV_HANDLE_CLOSED));
215   handle->next_closing = handle->loop->closing_handles;
216   handle->loop->closing_handles = handle;
217 }
218 
219 int uv__getiovmax(void) {
220 #if defined(IOV_MAX)
221   return IOV_MAX;
222 #elif defined(_SC_IOV_MAX)
223   static int iovmax = -1;
224   if (iovmax == -1) {
225     iovmax = sysconf(_SC_IOV_MAX);
226     /* On some embedded devices (arm-linux-uclibc based ip camera),
227      * sysconf(_SC_IOV_MAX) can not get the correct value. The return
228      * value is -1 and the errno is EINPROGRESS. Degrade the value to 1.
229      */
230     if (iovmax == -1) iovmax = 1;
231   }
232   return iovmax;
233 #else
234   return 1024;
235 #endif
236 }
237 
238 
239 static void uv__finish_close(uv_handle_t* handle) {
240   uv_signal_t* sh;
241 
242   /* Note: while the handle is in the UV_HANDLE_CLOSING state now, it's still
243    * possible for it to be active in the sense that uv__is_active() returns
244    * true.
245    *
246    * A good example is when the user calls uv_shutdown(), immediately followed
247    * by uv_close(). The handle is considered active at this point because the
248    * completion of the shutdown req is still pending.
249    */
250   assert(handle->flags & UV_HANDLE_CLOSING);
251   assert(!(handle->flags & UV_HANDLE_CLOSED));
252   handle->flags |= UV_HANDLE_CLOSED;
253 
254   switch (handle->type) {
255     case UV_PREPARE:
256     case UV_CHECK:
257     case UV_IDLE:
258     case UV_ASYNC:
259     case UV_TIMER:
260     case UV_PROCESS:
261     case UV_FS_EVENT:
262     case UV_FS_POLL:
263     case UV_POLL:
264       break;
265 
266     case UV_SIGNAL:
267       /* If there are any caught signals "trapped" in the signal pipe,
268        * we can't call the close callback yet. Reinserting the handle
269        * into the closing queue makes the event loop spin but that's
270        * okay because we only need to deliver the pending events.
271        */
272       sh = (uv_signal_t*) handle;
273       if (sh->caught_signals > sh->dispatched_signals) {
274         handle->flags ^= UV_HANDLE_CLOSED;
275         uv__make_close_pending(handle);  /* Back into the queue. */
276         return;
277       }
278       break;
279 
280     case UV_NAMED_PIPE:
281     case UV_TCP:
282     case UV_TTY:
283       uv__stream_destroy((uv_stream_t*)handle);
284       break;
285 
286     case UV_UDP:
287       uv__udp_finish_close((uv_udp_t*)handle);
288       break;
289 
290     default:
291       assert(0);
292       break;
293   }
294 
295   uv__handle_unref(handle);
296   QUEUE_REMOVE(&handle->handle_queue);
297 
298   if (handle->close_cb) {
299     handle->close_cb(handle);
300   }
301 }
302 
303 
304 static void uv__run_closing_handles(uv_loop_t* loop) {
305   uv_handle_t* p;
306   uv_handle_t* q;
307 
308   p = loop->closing_handles;
309   loop->closing_handles = NULL;
310 
311   while (p) {
312     q = p->next_closing;
313     uv__finish_close(p);
314     p = q;
315   }
316 }
317 
318 
319 int uv_is_closing(const uv_handle_t* handle) {
320   return uv__is_closing(handle);
321 }
322 
323 
324 int uv_backend_fd(const uv_loop_t* loop) {
325   return loop->backend_fd;
326 }
327 
328 
329 int uv_backend_timeout(const uv_loop_t* loop) {
330   if (loop->stop_flag != 0)
331     return 0;
332 
333   if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
334     return 0;
335 
336   if (!QUEUE_EMPTY(&loop->idle_handles))
337     return 0;
338 
339   if (!QUEUE_EMPTY(&loop->pending_queue))
340     return 0;
341 
342   if (loop->closing_handles)
343     return 0;
344 
345   return uv__next_timeout(loop);
346 }
347 
348 
349 static int uv__loop_alive(const uv_loop_t* loop) {
350   return uv__has_active_handles(loop) ||
351          uv__has_active_reqs(loop) ||
352          loop->closing_handles != NULL;
353 }
354 
355 
356 int uv_loop_alive(const uv_loop_t* loop) {
357     return uv__loop_alive(loop);
358 }
359 
360 
361 int uv_run(uv_loop_t* loop, uv_run_mode mode) {
362   int timeout;
363   int r;
364   int ran_pending;
365 
366   r = uv__loop_alive(loop);
367   if (!r)
368     uv__update_time(loop);
369 
370   while (r != 0 && loop->stop_flag == 0) {
371     uv__update_time(loop);
372     uv__run_timers(loop);
373     ran_pending = uv__run_pending(loop);
374     uv__run_idle(loop);
375     uv__run_prepare(loop);
376 
377     timeout = 0;
378     if ((mode == UV_RUN_ONCE && !ran_pending) || mode == UV_RUN_DEFAULT)
379       timeout = uv_backend_timeout(loop);
380 
381     uv__io_poll(loop, timeout);
382     uv__run_check(loop);
383     uv__run_closing_handles(loop);
384 
385     if (mode == UV_RUN_ONCE) {
386       /* UV_RUN_ONCE implies forward progress: at least one callback must have
387        * been invoked when it returns. uv__io_poll() can return without doing
388        * I/O (meaning: no callbacks) when its timeout expires - which means we
389        * have pending timers that satisfy the forward progress constraint.
390        *
391        * UV_RUN_NOWAIT makes no guarantees about progress so it's omitted from
392        * the check.
393        */
394       uv__update_time(loop);
395       uv__run_timers(loop);
396     }
397 
398     r = uv__loop_alive(loop);
399     if (mode == UV_RUN_ONCE || mode == UV_RUN_NOWAIT)
400       break;
401   }
402 
403   /* The if statement lets gcc compile it to a conditional store. Avoids
404    * dirtying a cache line.
405    */
406   if (loop->stop_flag != 0)
407     loop->stop_flag = 0;
408 
409   return r;
410 }
411 
412 
413 void uv_update_time(uv_loop_t* loop) {
414   uv__update_time(loop);
415 }
416 
417 
418 int uv_is_active(const uv_handle_t* handle) {
419   return uv__is_active(handle);
420 }
421 
422 
423 /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
424 int uv__socket(int domain, int type, int protocol) {
425   int sockfd;
426   int err;
427 
428 #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
429   sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
430   if (sockfd != -1)
431     return sockfd;
432 
433   if (errno != EINVAL)
434     return UV__ERR(errno);
435 #endif
436 
437   sockfd = socket(domain, type, protocol);
438   if (sockfd == -1)
439     return UV__ERR(errno);
440 
441   err = uv__nonblock(sockfd, 1);
442   if (err == 0)
443     err = uv__cloexec(sockfd, 1);
444 
445   if (err) {
446     uv__close(sockfd);
447     return err;
448   }
449 
450 #if defined(SO_NOSIGPIPE)
451   {
452     int on = 1;
453     setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
454   }
455 #endif
456 
457   return sockfd;
458 }
459 
460 /* get a file pointer to a file in read-only and close-on-exec mode */
461 FILE* uv__open_file(const char* path) {
462   int fd;
463   FILE* fp;
464 
465   fd = uv__open_cloexec(path, O_RDONLY);
466   if (fd < 0)
467     return NULL;
468 
469    fp = fdopen(fd, "r");
470    if (fp == NULL)
471      uv__close(fd);
472 
473    return fp;
474 }
475 
476 
477 int uv__accept(int sockfd) {
478   int peerfd;
479   int err;
480 
481   (void) &err;
482   assert(sockfd >= 0);
483 
484   do
485 #ifdef uv__accept4
486     peerfd = uv__accept4(sockfd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
487 #else
488     peerfd = accept(sockfd, NULL, NULL);
489 #endif
490   while (peerfd == -1 && errno == EINTR);
491 
492   if (peerfd == -1)
493     return UV__ERR(errno);
494 
495 #ifndef uv__accept4
496   err = uv__cloexec(peerfd, 1);
497   if (err == 0)
498     err = uv__nonblock(peerfd, 1);
499 
500   if (err != 0) {
501     uv__close(peerfd);
502     return err;
503   }
504 #endif
505 
506   return peerfd;
507 }
508 
509 
510 /* close() on macos has the "interesting" quirk that it fails with EINTR
511  * without closing the file descriptor when a thread is in the cancel state.
512  * That's why libuv calls close$NOCANCEL() instead.
513  *
514  * glibc on linux has a similar issue: close() is a cancellation point and
515  * will unwind the thread when it's in the cancel state. Work around that
516  * by making the system call directly. Musl libc is unaffected.
517  */
518 int uv__close_nocancel(int fd) {
519 #if defined(__APPLE__)
520 #pragma GCC diagnostic push
521 #pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension"
522 #if defined(__LP64__) || TARGET_OS_IPHONE
523   extern int close$NOCANCEL(int);
524   return close$NOCANCEL(fd);
525 #else
526   extern int close$NOCANCEL$UNIX2003(int);
527   return close$NOCANCEL$UNIX2003(fd);
528 #endif
529 #pragma GCC diagnostic pop
530 #elif defined(__linux__)
531   return syscall(SYS_close, fd);
532 #else
533   return close(fd);
534 #endif
535 }
536 
537 
538 int uv__close_nocheckstdio(int fd) {
539   int saved_errno;
540   int rc;
541 
542   assert(fd > -1);  /* Catch uninitialized io_watcher.fd bugs. */
543 
544   saved_errno = errno;
545   rc = uv__close_nocancel(fd);
546   if (rc == -1) {
547     rc = UV__ERR(errno);
548     if (rc == UV_EINTR || rc == UV__ERR(EINPROGRESS))
549       rc = 0;    /* The close is in progress, not an error. */
550     errno = saved_errno;
551   }
552 
553   return rc;
554 }
555 
556 
557 int uv__close(int fd) {
558   assert(fd > STDERR_FILENO);  /* Catch stdio close bugs. */
559 #if defined(__MVS__)
560   SAVE_ERRNO(epoll_file_close(fd));
561 #endif
562   return uv__close_nocheckstdio(fd);
563 }
564 
565 
566 int uv__nonblock_ioctl(int fd, int set) {
567   int r;
568 
569   do
570     r = ioctl(fd, FIONBIO, &set);
571   while (r == -1 && errno == EINTR);
572 
573   if (r)
574     return UV__ERR(errno);
575 
576   return 0;
577 }
578 
579 
580 #if !defined(__CYGWIN__) && !defined(__MSYS__) && !defined(__HAIKU__)
581 int uv__cloexec_ioctl(int fd, int set) {
582   int r;
583 
584   do
585     r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
586   while (r == -1 && errno == EINTR);
587 
588   if (r)
589     return UV__ERR(errno);
590 
591   return 0;
592 }
593 #endif
594 
595 
596 int uv__nonblock_fcntl(int fd, int set) {
597   int flags;
598   int r;
599 
600   do
601     r = fcntl(fd, F_GETFL);
602   while (r == -1 && errno == EINTR);
603 
604   if (r == -1)
605     return UV__ERR(errno);
606 
607   /* Bail out now if already set/clear. */
608   if (!!(r & O_NONBLOCK) == !!set)
609     return 0;
610 
611   if (set)
612     flags = r | O_NONBLOCK;
613   else
614     flags = r & ~O_NONBLOCK;
615 
616   do
617     r = fcntl(fd, F_SETFL, flags);
618   while (r == -1 && errno == EINTR);
619 
620   if (r)
621     return UV__ERR(errno);
622 
623   return 0;
624 }
625 
626 
627 int uv__cloexec_fcntl(int fd, int set) {
628   int flags;
629   int r;
630 
631   do
632     r = fcntl(fd, F_GETFD);
633   while (r == -1 && errno == EINTR);
634 
635   if (r == -1)
636     return UV__ERR(errno);
637 
638   /* Bail out now if already set/clear. */
639   if (!!(r & FD_CLOEXEC) == !!set)
640     return 0;
641 
642   if (set)
643     flags = r | FD_CLOEXEC;
644   else
645     flags = r & ~FD_CLOEXEC;
646 
647   do
648     r = fcntl(fd, F_SETFD, flags);
649   while (r == -1 && errno == EINTR);
650 
651   if (r)
652     return UV__ERR(errno);
653 
654   return 0;
655 }
656 
657 
658 ssize_t uv__recvmsg(int fd, struct msghdr* msg, int flags) {
659   struct cmsghdr* cmsg;
660   ssize_t rc;
661   int* pfd;
662   int* end;
663 #if defined(__linux__)
664   static int no_msg_cmsg_cloexec;
665   if (no_msg_cmsg_cloexec == 0) {
666     rc = recvmsg(fd, msg, flags | 0x40000000);  /* MSG_CMSG_CLOEXEC */
667     if (rc != -1)
668       return rc;
669     if (errno != EINVAL)
670       return UV__ERR(errno);
671     rc = recvmsg(fd, msg, flags);
672     if (rc == -1)
673       return UV__ERR(errno);
674     no_msg_cmsg_cloexec = 1;
675   } else {
676     rc = recvmsg(fd, msg, flags);
677   }
678 #else
679   rc = recvmsg(fd, msg, flags);
680 #endif
681   if (rc == -1)
682     return UV__ERR(errno);
683   if (msg->msg_controllen == 0)
684     return rc;
685   for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg))
686     if (cmsg->cmsg_type == SCM_RIGHTS)
687       for (pfd = (int*) CMSG_DATA(cmsg),
688            end = (int*) ((char*) cmsg + cmsg->cmsg_len);
689            pfd < end;
690            pfd += 1)
691         uv__cloexec(*pfd, 1);
692   return rc;
693 }
694 
695 
696 int uv_cwd(char* buffer, size_t* size) {
697   char scratch[1 + UV__PATH_MAX];
698 
699   if (buffer == NULL || size == NULL)
700     return UV_EINVAL;
701 
702   /* Try to read directly into the user's buffer first... */
703   if (getcwd(buffer, *size) != NULL)
704     goto fixup;
705 
706   if (errno != ERANGE)
707     return UV__ERR(errno);
708 
709   /* ...or into scratch space if the user's buffer is too small
710    * so we can report how much space to provide on the next try.
711    */
712   if (getcwd(scratch, sizeof(scratch)) == NULL)
713     return UV__ERR(errno);
714 
715   buffer = scratch;
716 
717 fixup:
718 
719   *size = strlen(buffer);
720 
721   if (*size > 1 && buffer[*size - 1] == '/') {
722     *size -= 1;
723     buffer[*size] = '\0';
724   }
725 
726   if (buffer == scratch) {
727     *size += 1;
728     return UV_ENOBUFS;
729   }
730 
731   return 0;
732 }
733 
734 
735 int uv_chdir(const char* dir) {
736   if (chdir(dir))
737     return UV__ERR(errno);
738 
739   return 0;
740 }
741 
742 
743 void uv_disable_stdio_inheritance(void) {
744   int fd;
745 
746   /* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
747    * first 16 file descriptors. After that, bail out after the first error.
748    */
749   for (fd = 0; ; fd++)
750     if (uv__cloexec(fd, 1) && fd > 15)
751       break;
752 }
753 
754 
755 int uv_fileno(const uv_handle_t* handle, uv_os_fd_t* fd) {
756   int fd_out;
757 
758   switch (handle->type) {
759   case UV_TCP:
760   case UV_NAMED_PIPE:
761   case UV_TTY:
762     fd_out = uv__stream_fd((uv_stream_t*) handle);
763     break;
764 
765   case UV_UDP:
766     fd_out = ((uv_udp_t *) handle)->io_watcher.fd;
767     break;
768 
769   case UV_POLL:
770     fd_out = ((uv_poll_t *) handle)->io_watcher.fd;
771     break;
772 
773   default:
774     return UV_EINVAL;
775   }
776 
777   if (uv__is_closing(handle) || fd_out == -1)
778     return UV_EBADF;
779 
780   *fd = fd_out;
781   return 0;
782 }
783 
784 
785 static int uv__run_pending(uv_loop_t* loop) {
786   QUEUE* q;
787   QUEUE pq;
788   uv__io_t* w;
789 
790   if (QUEUE_EMPTY(&loop->pending_queue))
791     return 0;
792 
793   QUEUE_MOVE(&loop->pending_queue, &pq);
794 
795   while (!QUEUE_EMPTY(&pq)) {
796     q = QUEUE_HEAD(&pq);
797     QUEUE_REMOVE(q);
798     QUEUE_INIT(q);
799     w = QUEUE_DATA(q, uv__io_t, pending_queue);
800     w->cb(loop, w, POLLOUT);
801   }
802 
803   return 1;
804 }
805 
806 
807 static unsigned int next_power_of_two(unsigned int val) {
808   val -= 1;
809   val |= val >> 1;
810   val |= val >> 2;
811   val |= val >> 4;
812   val |= val >> 8;
813   val |= val >> 16;
814   val += 1;
815   return val;
816 }
817 
818 static void maybe_resize(uv_loop_t* loop, unsigned int len) {
819   uv__io_t** watchers;
820   void* fake_watcher_list;
821   void* fake_watcher_count;
822   unsigned int nwatchers;
823   unsigned int i;
824 
825   if (len <= loop->nwatchers)
826     return;
827 
828   /* Preserve fake watcher list and count at the end of the watchers */
829   if (loop->watchers != NULL) {
830     fake_watcher_list = loop->watchers[loop->nwatchers];
831     fake_watcher_count = loop->watchers[loop->nwatchers + 1];
832   } else {
833     fake_watcher_list = NULL;
834     fake_watcher_count = NULL;
835   }
836 
837   nwatchers = next_power_of_two(len + 2) - 2;
838   watchers = uv__reallocf(loop->watchers,
839                           (nwatchers + 2) * sizeof(loop->watchers[0]));
840 
841   if (watchers == NULL)
842     abort();
843   for (i = loop->nwatchers; i < nwatchers; i++)
844     watchers[i] = NULL;
845   watchers[nwatchers] = fake_watcher_list;
846   watchers[nwatchers + 1] = fake_watcher_count;
847 
848   loop->watchers = watchers;
849   loop->nwatchers = nwatchers;
850 }
851 
852 
853 void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd) {
854   assert(cb != NULL);
855   assert(fd >= -1);
856   QUEUE_INIT(&w->pending_queue);
857   QUEUE_INIT(&w->watcher_queue);
858   w->cb = cb;
859   w->fd = fd;
860   w->events = 0;
861   w->pevents = 0;
862 
863 #if defined(UV_HAVE_KQUEUE)
864   w->rcount = 0;
865   w->wcount = 0;
866 #endif /* defined(UV_HAVE_KQUEUE) */
867 }
868 
869 
870 void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
871   assert(0 == (events & ~(POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI)));
872   assert(0 != events);
873   assert(w->fd >= 0);
874   assert(w->fd < INT_MAX);
875 
876   w->pevents |= events;
877   maybe_resize(loop, w->fd + 1);
878 
879 #if !defined(__sun)
880   /* The event ports backend needs to rearm all file descriptors on each and
881    * every tick of the event loop but the other backends allow us to
882    * short-circuit here if the event mask is unchanged.
883    */
884   if (w->events == w->pevents)
885     return;
886 #endif
887 
888   if (QUEUE_EMPTY(&w->watcher_queue))
889     QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
890 
891   if (loop->watchers[w->fd] == NULL) {
892     loop->watchers[w->fd] = w;
893     loop->nfds++;
894   }
895 }
896 
897 
898 void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
899   assert(0 == (events & ~(POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI)));
900   assert(0 != events);
901 
902   if (w->fd == -1)
903     return;
904 
905   assert(w->fd >= 0);
906 
907   /* Happens when uv__io_stop() is called on a handle that was never started. */
908   if ((unsigned) w->fd >= loop->nwatchers)
909     return;
910 
911   w->pevents &= ~events;
912 
913   if (w->pevents == 0) {
914     QUEUE_REMOVE(&w->watcher_queue);
915     QUEUE_INIT(&w->watcher_queue);
916 
917     if (loop->watchers[w->fd] != NULL) {
918       assert(loop->watchers[w->fd] == w);
919       assert(loop->nfds > 0);
920       loop->watchers[w->fd] = NULL;
921       loop->nfds--;
922       w->events = 0;
923     }
924   }
925   else if (QUEUE_EMPTY(&w->watcher_queue))
926     QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
927 }
928 
929 
930 void uv__io_close(uv_loop_t* loop, uv__io_t* w) {
931   uv__io_stop(loop, w, POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
932   QUEUE_REMOVE(&w->pending_queue);
933 
934   /* Remove stale events for this file descriptor */
935   if (w->fd != -1)
936     uv__platform_invalidate_fd(loop, w->fd);
937 }
938 
939 
940 void uv__io_feed(uv_loop_t* loop, uv__io_t* w) {
941   if (QUEUE_EMPTY(&w->pending_queue))
942     QUEUE_INSERT_TAIL(&loop->pending_queue, &w->pending_queue);
943 }
944 
945 
946 int uv__io_active(const uv__io_t* w, unsigned int events) {
947   assert(0 == (events & ~(POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI)));
948   assert(0 != events);
949   return 0 != (w->pevents & events);
950 }
951 
952 
953 int uv__fd_exists(uv_loop_t* loop, int fd) {
954   return (unsigned) fd < loop->nwatchers && loop->watchers[fd] != NULL;
955 }
956 
957 
958 int uv_getrusage(uv_rusage_t* rusage) {
959   struct rusage usage;
960 
961   if (getrusage(RUSAGE_SELF, &usage))
962     return UV__ERR(errno);
963 
964   rusage->ru_utime.tv_sec = usage.ru_utime.tv_sec;
965   rusage->ru_utime.tv_usec = usage.ru_utime.tv_usec;
966 
967   rusage->ru_stime.tv_sec = usage.ru_stime.tv_sec;
968   rusage->ru_stime.tv_usec = usage.ru_stime.tv_usec;
969 
970 #if !defined(__MVS__) && !defined(__HAIKU__)
971   rusage->ru_maxrss = usage.ru_maxrss;
972   rusage->ru_ixrss = usage.ru_ixrss;
973   rusage->ru_idrss = usage.ru_idrss;
974   rusage->ru_isrss = usage.ru_isrss;
975   rusage->ru_minflt = usage.ru_minflt;
976   rusage->ru_majflt = usage.ru_majflt;
977   rusage->ru_nswap = usage.ru_nswap;
978   rusage->ru_inblock = usage.ru_inblock;
979   rusage->ru_oublock = usage.ru_oublock;
980   rusage->ru_msgsnd = usage.ru_msgsnd;
981   rusage->ru_msgrcv = usage.ru_msgrcv;
982   rusage->ru_nsignals = usage.ru_nsignals;
983   rusage->ru_nvcsw = usage.ru_nvcsw;
984   rusage->ru_nivcsw = usage.ru_nivcsw;
985 #endif
986 
987   return 0;
988 }
989 
990 
991 int uv__open_cloexec(const char* path, int flags) {
992 #if defined(O_CLOEXEC)
993   int fd;
994 
995   fd = open(path, flags | O_CLOEXEC);
996   if (fd == -1)
997     return UV__ERR(errno);
998 
999   return fd;
1000 #else  /* O_CLOEXEC */
1001   int err;
1002   int fd;
1003 
1004   fd = open(path, flags);
1005   if (fd == -1)
1006     return UV__ERR(errno);
1007 
1008   err = uv__cloexec(fd, 1);
1009   if (err) {
1010     uv__close(fd);
1011     return err;
1012   }
1013 
1014   return fd;
1015 #endif  /* O_CLOEXEC */
1016 }
1017 
1018 
1019 int uv__dup2_cloexec(int oldfd, int newfd) {
1020 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__linux__)
1021   int r;
1022 
1023   r = dup3(oldfd, newfd, O_CLOEXEC);
1024   if (r == -1)
1025     return UV__ERR(errno);
1026 
1027   return r;
1028 #else
1029   int err;
1030   int r;
1031 
1032   r = dup2(oldfd, newfd);  /* Never retry. */
1033   if (r == -1)
1034     return UV__ERR(errno);
1035 
1036   err = uv__cloexec(newfd, 1);
1037   if (err != 0) {
1038     uv__close(newfd);
1039     return err;
1040   }
1041 
1042   return r;
1043 #endif
1044 }
1045 
1046 
1047 int uv_os_homedir(char* buffer, size_t* size) {
1048   uv_passwd_t pwd;
1049   size_t len;
1050   int r;
1051 
1052   /* Check if the HOME environment variable is set first. The task of
1053      performing input validation on buffer and size is taken care of by
1054      uv_os_getenv(). */
1055   r = uv_os_getenv("HOME", buffer, size);
1056 
1057   if (r != UV_ENOENT)
1058     return r;
1059 
1060   /* HOME is not set, so call uv__getpwuid_r() */
1061   r = uv__getpwuid_r(&pwd);
1062 
1063   if (r != 0) {
1064     return r;
1065   }
1066 
1067   len = strlen(pwd.homedir);
1068 
1069   if (len >= *size) {
1070     *size = len + 1;
1071     uv_os_free_passwd(&pwd);
1072     return UV_ENOBUFS;
1073   }
1074 
1075   memcpy(buffer, pwd.homedir, len + 1);
1076   *size = len;
1077   uv_os_free_passwd(&pwd);
1078 
1079   return 0;
1080 }
1081 
1082 
1083 int uv_os_tmpdir(char* buffer, size_t* size) {
1084   const char* buf;
1085   size_t len;
1086 
1087   if (buffer == NULL || size == NULL || *size == 0)
1088     return UV_EINVAL;
1089 
1090 #define CHECK_ENV_VAR(name)                                                   \
1091   do {                                                                        \
1092     buf = getenv(name);                                                       \
1093     if (buf != NULL)                                                          \
1094       goto return_buffer;                                                     \
1095   }                                                                           \
1096   while (0)
1097 
1098   /* Check the TMPDIR, TMP, TEMP, and TEMPDIR environment variables in order */
1099   CHECK_ENV_VAR("TMPDIR");
1100   CHECK_ENV_VAR("TMP");
1101   CHECK_ENV_VAR("TEMP");
1102   CHECK_ENV_VAR("TEMPDIR");
1103 
1104 #undef CHECK_ENV_VAR
1105 
1106   /* No temp environment variables defined */
1107   #if defined(__ANDROID__)
1108     buf = "/data/local/tmp";
1109   #else
1110     buf = "/tmp";
1111   #endif
1112 
1113 return_buffer:
1114   len = strlen(buf);
1115 
1116   if (len >= *size) {
1117     *size = len + 1;
1118     return UV_ENOBUFS;
1119   }
1120 
1121   /* The returned directory should not have a trailing slash. */
1122   if (len > 1 && buf[len - 1] == '/') {
1123     len--;
1124   }
1125 
1126   memcpy(buffer, buf, len + 1);
1127   buffer[len] = '\0';
1128   *size = len;
1129 
1130   return 0;
1131 }
1132 
1133 
1134 int uv__getpwuid_r(uv_passwd_t* pwd) {
1135   struct passwd pw;
1136   struct passwd* result;
1137   char* buf;
1138   uid_t uid;
1139   size_t bufsize;
1140   size_t name_size;
1141   size_t homedir_size;
1142   size_t shell_size;
1143   long initsize;
1144   int r;
1145 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
1146   int (*getpwuid_r)(uid_t, struct passwd*, char*, size_t, struct passwd**);
1147 
1148   getpwuid_r = dlsym(RTLD_DEFAULT, "getpwuid_r");
1149   if (getpwuid_r == NULL)
1150     return UV_ENOSYS;
1151 #endif
1152 
1153   if (pwd == NULL)
1154     return UV_EINVAL;
1155 
1156   initsize = sysconf(_SC_GETPW_R_SIZE_MAX);
1157 
1158   if (initsize <= 0)
1159     bufsize = 4096;
1160   else
1161     bufsize = (size_t) initsize;
1162 
1163   uid = geteuid();
1164   buf = NULL;
1165 
1166   for (;;) {
1167     uv__free(buf);
1168     buf = uv__malloc(bufsize);
1169 
1170     if (buf == NULL)
1171       return UV_ENOMEM;
1172 
1173     r = getpwuid_r(uid, &pw, buf, bufsize, &result);
1174 
1175     if (r != ERANGE)
1176       break;
1177 
1178     bufsize *= 2;
1179   }
1180 
1181   if (r != 0) {
1182     uv__free(buf);
1183     return -r;
1184   }
1185 
1186   if (result == NULL) {
1187     uv__free(buf);
1188     return UV_ENOENT;
1189   }
1190 
1191   /* Allocate memory for the username, shell, and home directory */
1192   name_size = strlen(pw.pw_name) + 1;
1193   homedir_size = strlen(pw.pw_dir) + 1;
1194   shell_size = strlen(pw.pw_shell) + 1;
1195   pwd->username = uv__malloc(name_size + homedir_size + shell_size);
1196 
1197   if (pwd->username == NULL) {
1198     uv__free(buf);
1199     return UV_ENOMEM;
1200   }
1201 
1202   /* Copy the username */
1203   memcpy(pwd->username, pw.pw_name, name_size);
1204 
1205   /* Copy the home directory */
1206   pwd->homedir = pwd->username + name_size;
1207   memcpy(pwd->homedir, pw.pw_dir, homedir_size);
1208 
1209   /* Copy the shell */
1210   pwd->shell = pwd->homedir + homedir_size;
1211   memcpy(pwd->shell, pw.pw_shell, shell_size);
1212 
1213   /* Copy the uid and gid */
1214   pwd->uid = pw.pw_uid;
1215   pwd->gid = pw.pw_gid;
1216 
1217   uv__free(buf);
1218 
1219   return 0;
1220 }
1221 
1222 
1223 void uv_os_free_passwd(uv_passwd_t* pwd) {
1224   if (pwd == NULL)
1225     return;
1226 
1227   /*
1228     The memory for name, shell, and homedir are allocated in a single
1229     uv__malloc() call. The base of the pointer is stored in pwd->username, so
1230     that is the field that needs to be freed.
1231   */
1232   uv__free(pwd->username);
1233   pwd->username = NULL;
1234   pwd->shell = NULL;
1235   pwd->homedir = NULL;
1236 }
1237 
1238 
1239 int uv_os_get_passwd(uv_passwd_t* pwd) {
1240   return uv__getpwuid_r(pwd);
1241 }
1242 
1243 
1244 int uv_translate_sys_error(int sys_errno) {
1245   /* If < 0 then it's already a libuv error. */
1246   return sys_errno <= 0 ? sys_errno : -sys_errno;
1247 }
1248 
1249 
1250 int uv_os_environ(uv_env_item_t** envitems, int* count) {
1251   int i, j, cnt;
1252   uv_env_item_t* envitem;
1253 
1254   *envitems = NULL;
1255   *count = 0;
1256 
1257   for (i = 0; environ[i] != NULL; i++);
1258 
1259   *envitems = uv__calloc(i, sizeof(**envitems));
1260 
1261   if (*envitems == NULL)
1262     return UV_ENOMEM;
1263 
1264   for (j = 0, cnt = 0; j < i; j++) {
1265     char* buf;
1266     char* ptr;
1267 
1268     if (environ[j] == NULL)
1269       break;
1270 
1271     buf = uv__strdup(environ[j]);
1272     if (buf == NULL)
1273       goto fail;
1274 
1275     ptr = strchr(buf, '=');
1276     if (ptr == NULL) {
1277       uv__free(buf);
1278       continue;
1279     }
1280 
1281     *ptr = '\0';
1282 
1283     envitem = &(*envitems)[cnt];
1284     envitem->name = buf;
1285     envitem->value = ptr + 1;
1286 
1287     cnt++;
1288   }
1289 
1290   *count = cnt;
1291   return 0;
1292 
1293 fail:
1294   for (i = 0; i < cnt; i++) {
1295     envitem = &(*envitems)[cnt];
1296     uv__free(envitem->name);
1297   }
1298   uv__free(*envitems);
1299 
1300   *envitems = NULL;
1301   *count = 0;
1302   return UV_ENOMEM;
1303 }
1304 
1305 
1306 int uv_os_getenv(const char* name, char* buffer, size_t* size) {
1307   char* var;
1308   size_t len;
1309 
1310   if (name == NULL || buffer == NULL || size == NULL || *size == 0)
1311     return UV_EINVAL;
1312 
1313   var = getenv(name);
1314 
1315   if (var == NULL)
1316     return UV_ENOENT;
1317 
1318   len = strlen(var);
1319 
1320   if (len >= *size) {
1321     *size = len + 1;
1322     return UV_ENOBUFS;
1323   }
1324 
1325   memcpy(buffer, var, len + 1);
1326   *size = len;
1327 
1328   return 0;
1329 }
1330 
1331 
1332 int uv_os_setenv(const char* name, const char* value) {
1333   if (name == NULL || value == NULL)
1334     return UV_EINVAL;
1335 
1336   if (setenv(name, value, 1) != 0)
1337     return UV__ERR(errno);
1338 
1339   return 0;
1340 }
1341 
1342 
1343 int uv_os_unsetenv(const char* name) {
1344   if (name == NULL)
1345     return UV_EINVAL;
1346 
1347   if (unsetenv(name) != 0)
1348     return UV__ERR(errno);
1349 
1350   return 0;
1351 }
1352 
1353 
1354 int uv_os_gethostname(char* buffer, size_t* size) {
1355   /*
1356     On some platforms, if the input buffer is not large enough, gethostname()
1357     succeeds, but truncates the result. libuv can detect this and return ENOBUFS
1358     instead by creating a large enough buffer and comparing the hostname length
1359     to the size input.
1360   */
1361   char buf[UV_MAXHOSTNAMESIZE];
1362   size_t len;
1363 
1364   if (buffer == NULL || size == NULL || *size == 0)
1365     return UV_EINVAL;
1366 
1367   if (gethostname(buf, sizeof(buf)) != 0)
1368     return UV__ERR(errno);
1369 
1370   buf[sizeof(buf) - 1] = '\0'; /* Null terminate, just to be safe. */
1371   len = strlen(buf);
1372 
1373   if (len >= *size) {
1374     *size = len + 1;
1375     return UV_ENOBUFS;
1376   }
1377 
1378   memcpy(buffer, buf, len + 1);
1379   *size = len;
1380   return 0;
1381 }
1382 
1383 
1384 uv_os_fd_t uv_get_osfhandle(int fd) {
1385   return fd;
1386 }
1387 
1388 int uv_open_osfhandle(uv_os_fd_t os_fd) {
1389   return os_fd;
1390 }
1391 
1392 uv_pid_t uv_os_getpid(void) {
1393   return getpid();
1394 }
1395 
1396 
1397 uv_pid_t uv_os_getppid(void) {
1398   return getppid();
1399 }
1400 
1401 
1402 int uv_os_getpriority(uv_pid_t pid, int* priority) {
1403   int r;
1404 
1405   if (priority == NULL)
1406     return UV_EINVAL;
1407 
1408   errno = 0;
1409   r = getpriority(PRIO_PROCESS, (int) pid);
1410 
1411   if (r == -1 && errno != 0)
1412     return UV__ERR(errno);
1413 
1414   *priority = r;
1415   return 0;
1416 }
1417 
1418 
1419 int uv_os_setpriority(uv_pid_t pid, int priority) {
1420   if (priority < UV_PRIORITY_HIGHEST || priority > UV_PRIORITY_LOW)
1421     return UV_EINVAL;
1422 
1423   if (setpriority(PRIO_PROCESS, (int) pid, priority) != 0)
1424     return UV__ERR(errno);
1425 
1426   return 0;
1427 }
1428 
1429 
1430 int uv_os_uname(uv_utsname_t* buffer) {
1431   struct utsname buf;
1432   int r;
1433 
1434   if (buffer == NULL)
1435     return UV_EINVAL;
1436 
1437   if (uname(&buf) == -1) {
1438     r = UV__ERR(errno);
1439     goto error;
1440   }
1441 
1442   r = uv__strscpy(buffer->sysname, buf.sysname, sizeof(buffer->sysname));
1443   if (r == UV_E2BIG)
1444     goto error;
1445 
1446 #ifdef _AIX
1447   r = snprintf(buffer->release,
1448                sizeof(buffer->release),
1449                "%s.%s",
1450                buf.version,
1451                buf.release);
1452   if (r >= sizeof(buffer->release)) {
1453     r = UV_E2BIG;
1454     goto error;
1455   }
1456 #else
1457   r = uv__strscpy(buffer->release, buf.release, sizeof(buffer->release));
1458   if (r == UV_E2BIG)
1459     goto error;
1460 #endif
1461 
1462   r = uv__strscpy(buffer->version, buf.version, sizeof(buffer->version));
1463   if (r == UV_E2BIG)
1464     goto error;
1465 
1466 #if defined(_AIX) || defined(__PASE__)
1467   r = uv__strscpy(buffer->machine, "ppc64", sizeof(buffer->machine));
1468 #else
1469   r = uv__strscpy(buffer->machine, buf.machine, sizeof(buffer->machine));
1470 #endif
1471 
1472   if (r == UV_E2BIG)
1473     goto error;
1474 
1475   return 0;
1476 
1477 error:
1478   buffer->sysname[0] = '\0';
1479   buffer->release[0] = '\0';
1480   buffer->version[0] = '\0';
1481   buffer->machine[0] = '\0';
1482   return r;
1483 }
1484 
1485 int uv__getsockpeername(const uv_handle_t* handle,
1486                         uv__peersockfunc func,
1487                         struct sockaddr* name,
1488                         int* namelen) {
1489   socklen_t socklen;
1490   uv_os_fd_t fd;
1491   int r;
1492 
1493   r = uv_fileno(handle, &fd);
1494   if (r < 0)
1495     return r;
1496 
1497   /* sizeof(socklen_t) != sizeof(int) on some systems. */
1498   socklen = (socklen_t) *namelen;
1499 
1500   if (func(fd, name, &socklen))
1501     return UV__ERR(errno);
1502 
1503   *namelen = (int) socklen;
1504   return 0;
1505 }
1506 
1507 int uv_gettimeofday(uv_timeval64_t* tv) {
1508   struct timeval time;
1509 
1510   if (tv == NULL)
1511     return UV_EINVAL;
1512 
1513   if (gettimeofday(&time, NULL) != 0)
1514     return UV__ERR(errno);
1515 
1516   tv->tv_sec = (int64_t) time.tv_sec;
1517   tv->tv_usec = (int32_t) time.tv_usec;
1518   return 0;
1519 }
1520 
1521 void uv_sleep(unsigned int msec) {
1522   struct timespec timeout;
1523   int rc;
1524 
1525   timeout.tv_sec = msec / 1000;
1526   timeout.tv_nsec = (msec % 1000) * 1000 * 1000;
1527 
1528   do
1529     rc = nanosleep(&timeout, &timeout);
1530   while (rc == -1 && errno == EINTR);
1531 
1532   assert(rc == 0);
1533 }
1534