internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #ifndef UV_UNIX_INTERNAL_H_
  22. #define UV_UNIX_INTERNAL_H_
  23. #include "uv-common.h"
  24. #include <assert.h>
  25. #include <limits.h> /* _POSIX_PATH_MAX, PATH_MAX */
  26. #include <stdlib.h> /* abort */
  27. #include <string.h> /* strrchr */
  28. #include <fcntl.h> /* O_CLOEXEC and O_NONBLOCK, if supported. */
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <sys/socket.h>
  32. #if defined(__STRICT_ANSI__)
  33. # define inline __inline
  34. #endif
  35. #if defined(__linux__)
  36. # include "linux-syscalls.h"
  37. #endif /* __linux__ */
  38. #if defined(__MVS__)
  39. # include "os390-syscalls.h"
  40. #endif /* __MVS__ */
  41. #if defined(__sun)
  42. # include <sys/port.h>
  43. # include <port.h>
  44. #endif /* __sun */
  45. #if defined(_AIX)
  46. # define reqevents events
  47. # define rtnevents revents
  48. # include <sys/poll.h>
  49. #else
  50. # include <poll.h>
  51. #endif /* _AIX */
  52. #if defined(__APPLE__) && !TARGET_OS_IPHONE
  53. # include <AvailabilityMacros.h>
  54. #endif
  55. #if defined(PATH_MAX)
  56. # define UV__PATH_MAX PATH_MAX
  57. #else
  58. # define UV__PATH_MAX 8192
  59. #endif
  60. #if defined(__ANDROID__)
  61. int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
  62. # ifdef pthread_sigmask
  63. # undef pthread_sigmask
  64. # endif
  65. # define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset)
  66. #endif
  67. #define ACCESS_ONCE(type, var) \
  68. (*(volatile type*) &(var))
  69. #define ROUND_UP(a, b) \
  70. ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
  71. #define UNREACHABLE() \
  72. do { \
  73. assert(0 && "unreachable code"); \
  74. abort(); \
  75. } \
  76. while (0)
  77. #define SAVE_ERRNO(block) \
  78. do { \
  79. int _saved_errno = errno; \
  80. do { block; } while (0); \
  81. errno = _saved_errno; \
  82. } \
  83. while (0)
  84. /* The __clang__ and __INTEL_COMPILER checks are superfluous because they
  85. * define __GNUC__. They are here to convey to you, dear reader, that these
  86. * macros are enabled when compiling with clang or icc.
  87. */
  88. #if defined(__clang__) || \
  89. defined(__GNUC__) || \
  90. defined(__INTEL_COMPILER)
  91. # define UV_UNUSED(declaration) __attribute__((unused)) declaration
  92. #else
  93. # define UV_UNUSED(declaration) declaration
  94. #endif
  95. /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
  96. #ifdef POLLRDHUP
  97. # define UV__POLLRDHUP POLLRDHUP
  98. #else
  99. # define UV__POLLRDHUP 0x2000
  100. #endif
  101. #ifdef POLLPRI
  102. # define UV__POLLPRI POLLPRI
  103. #else
  104. # define UV__POLLPRI 0
  105. #endif
  106. #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
  107. /*
  108. * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
  109. * Try using fixed value const and give up, if it doesn't work
  110. */
  111. # define O_CLOEXEC 0x00100000
  112. #endif
  113. typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
  114. /* loop flags */
  115. enum {
  116. UV_LOOP_BLOCK_SIGPROF = 1
  117. };
  118. /* flags of excluding ifaddr */
  119. enum {
  120. UV__EXCLUDE_IFPHYS,
  121. UV__EXCLUDE_IFADDR
  122. };
  123. typedef enum {
  124. UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */
  125. UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */
  126. } uv_clocktype_t;
  127. struct uv__stream_queued_fds_s {
  128. unsigned int size;
  129. unsigned int offset;
  130. int fds[1];
  131. };
  132. #if defined(_AIX) || \
  133. defined(__APPLE__) || \
  134. defined(__DragonFly__) || \
  135. defined(__FreeBSD__) || \
  136. defined(__FreeBSD_kernel__) || \
  137. defined(__linux__) || \
  138. defined(__OpenBSD__) || \
  139. defined(__NetBSD__)
  140. #define uv__cloexec uv__cloexec_ioctl
  141. #define uv__nonblock uv__nonblock_ioctl
  142. #else
  143. #define uv__cloexec uv__cloexec_fcntl
  144. #define uv__nonblock uv__nonblock_fcntl
  145. #endif
  146. /* On Linux, uv__nonblock_fcntl() and uv__nonblock_ioctl() do not commute
  147. * when O_NDELAY is not equal to O_NONBLOCK. Case in point: linux/sparc32
  148. * and linux/sparc64, where O_NDELAY is O_NONBLOCK + another bit.
  149. *
  150. * Libuv uses uv__nonblock_fcntl() directly sometimes so ensure that it
  151. * commutes with uv__nonblock().
  152. */
  153. #if defined(__linux__) && O_NDELAY != O_NONBLOCK
  154. #undef uv__nonblock
  155. #define uv__nonblock uv__nonblock_fcntl
  156. #endif
  157. /* core */
  158. int uv__cloexec_ioctl(int fd, int set);
  159. int uv__cloexec_fcntl(int fd, int set);
  160. int uv__nonblock_ioctl(int fd, int set);
  161. int uv__nonblock_fcntl(int fd, int set);
  162. int uv__close(int fd); /* preserves errno */
  163. int uv__close_nocheckstdio(int fd);
  164. int uv__close_nocancel(int fd);
  165. int uv__socket(int domain, int type, int protocol);
  166. ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
  167. void uv__make_close_pending(uv_handle_t* handle);
  168. int uv__getiovmax(void);
  169. void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
  170. void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  171. void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  172. void uv__io_close(uv_loop_t* loop, uv__io_t* w);
  173. void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
  174. int uv__io_active(const uv__io_t* w, unsigned int events);
  175. int uv__io_check_fd(uv_loop_t* loop, int fd);
  176. void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
  177. int uv__io_fork(uv_loop_t* loop);
  178. int uv__fd_exists(uv_loop_t* loop, int fd);
  179. /* async */
  180. void uv__async_stop(uv_loop_t* loop);
  181. int uv__async_fork(uv_loop_t* loop);
  182. /* loop */
  183. void uv__run_idle(uv_loop_t* loop);
  184. void uv__run_check(uv_loop_t* loop);
  185. void uv__run_prepare(uv_loop_t* loop);
  186. /* stream */
  187. void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
  188. uv_handle_type type);
  189. int uv__stream_open(uv_stream_t*, int fd, int flags);
  190. void uv__stream_destroy(uv_stream_t* stream);
  191. #if defined(__APPLE__)
  192. int uv__stream_try_select(uv_stream_t* stream, int* fd);
  193. #endif /* defined(__APPLE__) */
  194. void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  195. int uv__accept(int sockfd);
  196. int uv__dup2_cloexec(int oldfd, int newfd);
  197. int uv__open_cloexec(const char* path, int flags);
  198. /* tcp */
  199. int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
  200. int uv__tcp_nodelay(int fd, int on);
  201. int uv__tcp_keepalive(int fd, int on, unsigned int delay);
  202. /* pipe */
  203. int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
  204. /* signal */
  205. void uv__signal_close(uv_signal_t* handle);
  206. void uv__signal_global_once_init(void);
  207. void uv__signal_loop_cleanup(uv_loop_t* loop);
  208. int uv__signal_loop_fork(uv_loop_t* loop);
  209. /* platform specific */
  210. uint64_t uv__hrtime(uv_clocktype_t type);
  211. int uv__kqueue_init(uv_loop_t* loop);
  212. int uv__platform_loop_init(uv_loop_t* loop);
  213. void uv__platform_loop_delete(uv_loop_t* loop);
  214. void uv__platform_invalidate_fd(uv_loop_t* loop, int fd);
  215. /* various */
  216. void uv__async_close(uv_async_t* handle);
  217. void uv__check_close(uv_check_t* handle);
  218. void uv__fs_event_close(uv_fs_event_t* handle);
  219. void uv__idle_close(uv_idle_t* handle);
  220. void uv__pipe_close(uv_pipe_t* handle);
  221. void uv__poll_close(uv_poll_t* handle);
  222. void uv__prepare_close(uv_prepare_t* handle);
  223. void uv__process_close(uv_process_t* handle);
  224. void uv__stream_close(uv_stream_t* handle);
  225. void uv__tcp_close(uv_tcp_t* handle);
  226. void uv__udp_close(uv_udp_t* handle);
  227. void uv__udp_finish_close(uv_udp_t* handle);
  228. uv_handle_type uv__handle_type(int fd);
  229. FILE* uv__open_file(const char* path);
  230. int uv__getpwuid_r(uv_passwd_t* pwd);
  231. int uv__search_path(const char* prog, char* buf, size_t* buflen);
  232. /* random */
  233. int uv__random_devurandom(void* buf, size_t buflen);
  234. int uv__random_getrandom(void* buf, size_t buflen);
  235. int uv__random_getentropy(void* buf, size_t buflen);
  236. int uv__random_readpath(const char* path, void* buf, size_t buflen);
  237. int uv__random_sysctl(void* buf, size_t buflen);
  238. #if defined(__APPLE__)
  239. int uv___stream_fd(const uv_stream_t* handle);
  240. #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
  241. #else
  242. #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
  243. #endif /* defined(__APPLE__) */
  244. #ifdef O_NONBLOCK
  245. # define UV__F_NONBLOCK O_NONBLOCK
  246. #else
  247. # define UV__F_NONBLOCK 1
  248. #endif
  249. int uv__make_pipe(int fds[2], int flags);
  250. #if defined(__APPLE__)
  251. int uv__fsevents_init(uv_fs_event_t* handle);
  252. int uv__fsevents_close(uv_fs_event_t* handle);
  253. void uv__fsevents_loop_delete(uv_loop_t* loop);
  254. #endif /* defined(__APPLE__) */
  255. UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
  256. /* Use a fast time source if available. We only need millisecond precision.
  257. */
  258. loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
  259. }
  260. UV_UNUSED(static char* uv__basename_r(const char* path)) {
  261. char* s;
  262. s = strrchr(path, '/');
  263. if (s == NULL)
  264. return (char*) path;
  265. return s + 1;
  266. }
  267. #if defined(__linux__)
  268. int uv__inotify_fork(uv_loop_t* loop, void* old_watchers);
  269. #endif
  270. typedef int (*uv__peersockfunc)(int, struct sockaddr*, socklen_t*);
  271. int uv__getsockpeername(const uv_handle_t* handle,
  272. uv__peersockfunc func,
  273. struct sockaddr* name,
  274. int* namelen);
  275. #if defined(__linux__) || \
  276. defined(__FreeBSD__) || \
  277. defined(__FreeBSD_kernel__)
  278. #define HAVE_MMSG 1
  279. struct uv__mmsghdr {
  280. struct msghdr msg_hdr;
  281. unsigned int msg_len;
  282. };
  283. int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
  284. int uv__sendmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
  285. #else
  286. #define HAVE_MMSG 0
  287. #endif
  288. #endif /* UV_UNIX_INTERNAL_H_ */