internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /*
  56. * Define common detection for active Thread Sanitizer
  57. * - clang uses __has_feature(thread_sanitizer)
  58. * - gcc-7+ uses __SANITIZE_THREAD__
  59. */
  60. #if defined(__has_feature)
  61. # if __has_feature(thread_sanitizer)
  62. # define __SANITIZE_THREAD__ 1
  63. # endif
  64. #endif
  65. #if defined(PATH_MAX)
  66. # define UV__PATH_MAX PATH_MAX
  67. #else
  68. # define UV__PATH_MAX 8192
  69. #endif
  70. #if defined(__ANDROID__)
  71. int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset);
  72. # ifdef pthread_sigmask
  73. # undef pthread_sigmask
  74. # endif
  75. # define pthread_sigmask(how, set, oldset) uv__pthread_sigmask(how, set, oldset)
  76. #endif
  77. #define ACCESS_ONCE(type, var) \
  78. (*(volatile type*) &(var))
  79. #define ROUND_UP(a, b) \
  80. ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
  81. #define UNREACHABLE() \
  82. do { \
  83. assert(0 && "unreachable code"); \
  84. abort(); \
  85. } \
  86. while (0)
  87. #define SAVE_ERRNO(block) \
  88. do { \
  89. int _saved_errno = errno; \
  90. do { block; } while (0); \
  91. errno = _saved_errno; \
  92. } \
  93. while (0)
  94. /* The __clang__ and __INTEL_COMPILER checks are superfluous because they
  95. * define __GNUC__. They are here to convey to you, dear reader, that these
  96. * macros are enabled when compiling with clang or icc.
  97. */
  98. #if defined(__clang__) || \
  99. defined(__GNUC__) || \
  100. defined(__INTEL_COMPILER)
  101. # define UV_UNUSED(declaration) __attribute__((unused)) declaration
  102. #else
  103. # define UV_UNUSED(declaration) declaration
  104. #endif
  105. /* Leans on the fact that, on Linux, POLLRDHUP == EPOLLRDHUP. */
  106. #ifdef POLLRDHUP
  107. # define UV__POLLRDHUP POLLRDHUP
  108. #else
  109. # define UV__POLLRDHUP 0x2000
  110. #endif
  111. #ifdef POLLPRI
  112. # define UV__POLLPRI POLLPRI
  113. #else
  114. # define UV__POLLPRI 0
  115. #endif
  116. #if !defined(O_CLOEXEC) && defined(__FreeBSD__)
  117. /*
  118. * It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
  119. * Try using fixed value const and give up, if it doesn't work
  120. */
  121. # define O_CLOEXEC 0x00100000
  122. #endif
  123. typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
  124. /* loop flags */
  125. enum {
  126. UV_LOOP_BLOCK_SIGPROF = 0x1,
  127. UV_LOOP_REAP_CHILDREN = 0x2
  128. };
  129. /* flags of excluding ifaddr */
  130. enum {
  131. UV__EXCLUDE_IFPHYS,
  132. UV__EXCLUDE_IFADDR
  133. };
  134. typedef enum {
  135. UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */
  136. UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */
  137. } uv_clocktype_t;
  138. struct uv__stream_queued_fds_s {
  139. unsigned int size;
  140. unsigned int offset;
  141. int fds[1];
  142. };
  143. #if defined(_AIX) || \
  144. defined(__APPLE__) || \
  145. defined(__DragonFly__) || \
  146. defined(__FreeBSD__) || \
  147. defined(__FreeBSD_kernel__) || \
  148. defined(__linux__) || \
  149. defined(__OpenBSD__) || \
  150. defined(__NetBSD__)
  151. #define uv__nonblock uv__nonblock_ioctl
  152. #define UV__NONBLOCK_IS_IOCTL 1
  153. #else
  154. #define uv__nonblock uv__nonblock_fcntl
  155. #define UV__NONBLOCK_IS_IOCTL 0
  156. #endif
  157. /* On Linux, uv__nonblock_fcntl() and uv__nonblock_ioctl() do not commute
  158. * when O_NDELAY is not equal to O_NONBLOCK. Case in point: linux/sparc32
  159. * and linux/sparc64, where O_NDELAY is O_NONBLOCK + another bit.
  160. *
  161. * Libuv uses uv__nonblock_fcntl() directly sometimes so ensure that it
  162. * commutes with uv__nonblock().
  163. */
  164. #if defined(__linux__) && O_NDELAY != O_NONBLOCK
  165. #undef uv__nonblock
  166. #define uv__nonblock uv__nonblock_fcntl
  167. #endif
  168. /* core */
  169. int uv__cloexec(int fd, int set);
  170. int uv__nonblock_ioctl(int fd, int set);
  171. int uv__nonblock_fcntl(int fd, int set);
  172. int uv__close(int fd); /* preserves errno */
  173. int uv__close_nocheckstdio(int fd);
  174. int uv__close_nocancel(int fd);
  175. int uv__socket(int domain, int type, int protocol);
  176. ssize_t uv__recvmsg(int fd, struct msghdr *msg, int flags);
  177. void uv__make_close_pending(uv_handle_t* handle);
  178. int uv__getiovmax(void);
  179. void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd);
  180. void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  181. void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  182. void uv__io_close(uv_loop_t* loop, uv__io_t* w);
  183. void uv__io_feed(uv_loop_t* loop, uv__io_t* w);
  184. int uv__io_active(const uv__io_t* w, unsigned int events);
  185. int uv__io_check_fd(uv_loop_t* loop, int fd);
  186. void uv__io_poll(uv_loop_t* loop, int timeout); /* in milliseconds or -1 */
  187. int uv__io_fork(uv_loop_t* loop);
  188. int uv__fd_exists(uv_loop_t* loop, int fd);
  189. /* async */
  190. void uv__async_stop(uv_loop_t* loop);
  191. int uv__async_fork(uv_loop_t* loop);
  192. /* loop */
  193. void uv__run_idle(uv_loop_t* loop);
  194. void uv__run_check(uv_loop_t* loop);
  195. void uv__run_prepare(uv_loop_t* loop);
  196. /* stream */
  197. void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
  198. uv_handle_type type);
  199. int uv__stream_open(uv_stream_t*, int fd, int flags);
  200. void uv__stream_destroy(uv_stream_t* stream);
  201. #if defined(__APPLE__)
  202. int uv__stream_try_select(uv_stream_t* stream, int* fd);
  203. #endif /* defined(__APPLE__) */
  204. void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
  205. int uv__accept(int sockfd);
  206. int uv__dup2_cloexec(int oldfd, int newfd);
  207. int uv__open_cloexec(const char* path, int flags);
  208. int uv__slurp(const char* filename, char* buf, size_t len);
  209. /* tcp */
  210. int uv__tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
  211. int uv__tcp_nodelay(int fd, int on);
  212. int uv__tcp_keepalive(int fd, int on, unsigned int delay);
  213. /* pipe */
  214. int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
  215. /* signal */
  216. void uv__signal_close(uv_signal_t* handle);
  217. void uv__signal_global_once_init(void);
  218. void uv__signal_loop_cleanup(uv_loop_t* loop);
  219. int uv__signal_loop_fork(uv_loop_t* loop);
  220. /* platform specific */
  221. uint64_t uv__hrtime(uv_clocktype_t type);
  222. int uv__kqueue_init(uv_loop_t* loop);
  223. int uv__epoll_init(uv_loop_t* loop);
  224. int uv__platform_loop_init(uv_loop_t* loop);
  225. void uv__platform_loop_delete(uv_loop_t* loop);
  226. void uv__platform_invalidate_fd(uv_loop_t* loop, int fd);
  227. /* various */
  228. void uv__async_close(uv_async_t* handle);
  229. void uv__check_close(uv_check_t* handle);
  230. void uv__fs_event_close(uv_fs_event_t* handle);
  231. void uv__idle_close(uv_idle_t* handle);
  232. void uv__pipe_close(uv_pipe_t* handle);
  233. void uv__poll_close(uv_poll_t* handle);
  234. void uv__prepare_close(uv_prepare_t* handle);
  235. void uv__process_close(uv_process_t* handle);
  236. void uv__stream_close(uv_stream_t* handle);
  237. void uv__tcp_close(uv_tcp_t* handle);
  238. size_t uv__thread_stack_size(void);
  239. void uv__udp_close(uv_udp_t* handle);
  240. void uv__udp_finish_close(uv_udp_t* handle);
  241. FILE* uv__open_file(const char* path);
  242. int uv__getpwuid_r(uv_passwd_t* pwd);
  243. int uv__search_path(const char* prog, char* buf, size_t* buflen);
  244. void uv__wait_children(uv_loop_t* loop);
  245. /* random */
  246. int uv__random_devurandom(void* buf, size_t buflen);
  247. int uv__random_getrandom(void* buf, size_t buflen);
  248. int uv__random_getentropy(void* buf, size_t buflen);
  249. int uv__random_readpath(const char* path, void* buf, size_t buflen);
  250. int uv__random_sysctl(void* buf, size_t buflen);
  251. #if defined(__APPLE__)
  252. int uv___stream_fd(const uv_stream_t* handle);
  253. #define uv__stream_fd(handle) (uv___stream_fd((const uv_stream_t*) (handle)))
  254. #else
  255. #define uv__stream_fd(handle) ((handle)->io_watcher.fd)
  256. #endif /* defined(__APPLE__) */
  257. int uv__make_pipe(int fds[2], int flags);
  258. #if defined(__APPLE__)
  259. int uv__fsevents_init(uv_fs_event_t* handle);
  260. int uv__fsevents_close(uv_fs_event_t* handle);
  261. void uv__fsevents_loop_delete(uv_loop_t* loop);
  262. #endif /* defined(__APPLE__) */
  263. UV_UNUSED(static void uv__update_time(uv_loop_t* loop)) {
  264. /* Use a fast time source if available. We only need millisecond precision.
  265. */
  266. loop->time = uv__hrtime(UV_CLOCK_FAST) / 1000000;
  267. }
  268. UV_UNUSED(static char* uv__basename_r(const char* path)) {
  269. char* s;
  270. s = strrchr(path, '/');
  271. if (s == NULL)
  272. return (char*) path;
  273. return s + 1;
  274. }
  275. #if defined(__linux__)
  276. int uv__inotify_fork(uv_loop_t* loop, void* old_watchers);
  277. #endif
  278. typedef int (*uv__peersockfunc)(int, struct sockaddr*, socklen_t*);
  279. int uv__getsockpeername(const uv_handle_t* handle,
  280. uv__peersockfunc func,
  281. struct sockaddr* name,
  282. int* namelen);
  283. #if defined(__linux__) || \
  284. defined(__FreeBSD__) || \
  285. defined(__FreeBSD_kernel__) || \
  286. defined(__DragonFly__)
  287. #define HAVE_MMSG 1
  288. struct uv__mmsghdr {
  289. struct msghdr msg_hdr;
  290. unsigned int msg_len;
  291. };
  292. int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
  293. int uv__sendmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen);
  294. #else
  295. #define HAVE_MMSG 0
  296. #endif
  297. #if defined(__sun)
  298. #if !defined(_POSIX_VERSION) || _POSIX_VERSION < 200809L
  299. size_t strnlen(const char* s, size_t maxlen);
  300. #endif
  301. #endif
  302. #if defined(__FreeBSD__)
  303. ssize_t
  304. uv__fs_copy_file_range(int fd_in,
  305. off_t* off_in,
  306. int fd_out,
  307. off_t* off_out,
  308. size_t len,
  309. unsigned int flags);
  310. #endif
  311. #endif /* UV_UNIX_INTERNAL_H_ */