1
0

uv-unix.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_H
  22. #define UV_UNIX_H
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <dirent.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <netinet/tcp.h>
  30. #include <arpa/inet.h>
  31. #include <netdb.h>
  32. #include <termios.h>
  33. #include <pwd.h>
  34. #if !defined(__MVS__)
  35. #include <semaphore.h>
  36. #endif
  37. #include <pthread.h>
  38. #include <signal.h>
  39. #include "uv-threadpool.h"
  40. #if defined(__linux__)
  41. # include "uv-linux.h"
  42. #elif defined (__MVS__)
  43. # include "uv-os390.h"
  44. #elif defined(_AIX)
  45. # include "uv-aix.h"
  46. #elif defined(__sun)
  47. # include "uv-sunos.h"
  48. #elif defined(__APPLE__)
  49. # include "uv-darwin.h"
  50. #elif defined(__DragonFly__) || \
  51. defined(__FreeBSD__) || \
  52. defined(__FreeBSD_kernel__) || \
  53. defined(__OpenBSD__) || \
  54. defined(__NetBSD__)
  55. # include "uv-bsd.h"
  56. #elif defined(__CYGWIN__) || defined(__MSYS__)
  57. # include "uv-posix.h"
  58. #endif
  59. #ifndef PTHREAD_BARRIER_SERIAL_THREAD
  60. # include "pthread-barrier.h"
  61. #endif
  62. #ifndef NI_MAXHOST
  63. # define NI_MAXHOST 1025
  64. #endif
  65. #ifndef NI_MAXSERV
  66. # define NI_MAXSERV 32
  67. #endif
  68. #ifndef UV_IO_PRIVATE_PLATFORM_FIELDS
  69. # define UV_IO_PRIVATE_PLATFORM_FIELDS /* empty */
  70. #endif
  71. struct uv__io_s;
  72. struct uv_loop_s;
  73. typedef void (*uv__io_cb)(struct uv_loop_s* loop,
  74. struct uv__io_s* w,
  75. unsigned int events);
  76. typedef struct uv__io_s uv__io_t;
  77. struct uv__io_s {
  78. uv__io_cb cb;
  79. void* pending_queue[2];
  80. void* watcher_queue[2];
  81. unsigned int pevents; /* Pending event mask i.e. mask at next tick. */
  82. unsigned int events; /* Current event mask. */
  83. int fd;
  84. UV_IO_PRIVATE_PLATFORM_FIELDS
  85. };
  86. #ifndef UV_PLATFORM_SEM_T
  87. # define UV_PLATFORM_SEM_T sem_t
  88. #endif
  89. #ifndef UV_PLATFORM_LOOP_FIELDS
  90. # define UV_PLATFORM_LOOP_FIELDS /* empty */
  91. #endif
  92. #ifndef UV_PLATFORM_FS_EVENT_FIELDS
  93. # define UV_PLATFORM_FS_EVENT_FIELDS /* empty */
  94. #endif
  95. #ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS
  96. # define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */
  97. #endif
  98. /* Note: May be cast to struct iovec. See writev(2). */
  99. typedef struct uv_buf_t {
  100. char* base;
  101. size_t len;
  102. } uv_buf_t;
  103. typedef int uv_file;
  104. typedef int uv_os_sock_t;
  105. typedef int uv_os_fd_t;
  106. #define UV_ONCE_INIT PTHREAD_ONCE_INIT
  107. typedef pthread_once_t uv_once_t;
  108. typedef pthread_t uv_thread_t;
  109. typedef pthread_mutex_t uv_mutex_t;
  110. typedef pthread_rwlock_t uv_rwlock_t;
  111. typedef UV_PLATFORM_SEM_T uv_sem_t;
  112. typedef pthread_cond_t uv_cond_t;
  113. typedef pthread_key_t uv_key_t;
  114. typedef pthread_barrier_t uv_barrier_t;
  115. /* Platform-specific definitions for uv_spawn support. */
  116. typedef gid_t uv_gid_t;
  117. typedef uid_t uv_uid_t;
  118. typedef struct dirent uv__dirent_t;
  119. #if defined(DT_UNKNOWN)
  120. # define HAVE_DIRENT_TYPES
  121. # if defined(DT_REG)
  122. # define UV__DT_FILE DT_REG
  123. # else
  124. # define UV__DT_FILE -1
  125. # endif
  126. # if defined(DT_DIR)
  127. # define UV__DT_DIR DT_DIR
  128. # else
  129. # define UV__DT_DIR -2
  130. # endif
  131. # if defined(DT_LNK)
  132. # define UV__DT_LINK DT_LNK
  133. # else
  134. # define UV__DT_LINK -3
  135. # endif
  136. # if defined(DT_FIFO)
  137. # define UV__DT_FIFO DT_FIFO
  138. # else
  139. # define UV__DT_FIFO -4
  140. # endif
  141. # if defined(DT_SOCK)
  142. # define UV__DT_SOCKET DT_SOCK
  143. # else
  144. # define UV__DT_SOCKET -5
  145. # endif
  146. # if defined(DT_CHR)
  147. # define UV__DT_CHAR DT_CHR
  148. # else
  149. # define UV__DT_CHAR -6
  150. # endif
  151. # if defined(DT_BLK)
  152. # define UV__DT_BLOCK DT_BLK
  153. # else
  154. # define UV__DT_BLOCK -7
  155. # endif
  156. #endif
  157. /* Platform-specific definitions for uv_dlopen support. */
  158. #define UV_DYNAMIC /* empty */
  159. typedef struct {
  160. void* handle;
  161. char* errmsg;
  162. } uv_lib_t;
  163. #define UV_LOOP_PRIVATE_FIELDS \
  164. unsigned long flags; \
  165. int backend_fd; \
  166. void* pending_queue[2]; \
  167. void* watcher_queue[2]; \
  168. uv__io_t** watchers; \
  169. unsigned int nwatchers; \
  170. unsigned int nfds; \
  171. void* wq[2]; \
  172. uv_mutex_t wq_mutex; \
  173. uv_async_t wq_async; \
  174. uv_rwlock_t cloexec_lock; \
  175. uv_handle_t* closing_handles; \
  176. void* process_handles[2]; \
  177. void* prepare_handles[2]; \
  178. void* check_handles[2]; \
  179. void* idle_handles[2]; \
  180. void* async_handles[2]; \
  181. void (*async_unused)(void); /* TODO(bnoordhuis) Remove in libuv v2. */ \
  182. uv__io_t async_io_watcher; \
  183. int async_wfd; \
  184. struct { \
  185. void* min; \
  186. unsigned int nelts; \
  187. } timer_heap; \
  188. uint64_t timer_counter; \
  189. uint64_t time; \
  190. int signal_pipefd[2]; \
  191. uv__io_t signal_io_watcher; \
  192. uv_signal_t child_watcher; \
  193. int emfile_fd; \
  194. UV_PLATFORM_LOOP_FIELDS \
  195. #define UV_REQ_TYPE_PRIVATE /* empty */
  196. #define UV_REQ_PRIVATE_FIELDS /* empty */
  197. #define UV_PRIVATE_REQ_TYPES /* empty */
  198. #define UV_WRITE_PRIVATE_FIELDS \
  199. void* queue[2]; \
  200. unsigned int write_index; \
  201. uv_buf_t* bufs; \
  202. unsigned int nbufs; \
  203. int error; \
  204. uv_buf_t bufsml[4]; \
  205. #define UV_CONNECT_PRIVATE_FIELDS \
  206. void* queue[2]; \
  207. #define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */
  208. #define UV_UDP_SEND_PRIVATE_FIELDS \
  209. void* queue[2]; \
  210. struct sockaddr_storage addr; \
  211. unsigned int nbufs; \
  212. uv_buf_t* bufs; \
  213. ssize_t status; \
  214. uv_udp_send_cb send_cb; \
  215. uv_buf_t bufsml[4]; \
  216. #define UV_HANDLE_PRIVATE_FIELDS \
  217. uv_handle_t* next_closing; \
  218. unsigned int flags; \
  219. #define UV_STREAM_PRIVATE_FIELDS \
  220. uv_connect_t *connect_req; \
  221. uv_shutdown_t *shutdown_req; \
  222. uv__io_t io_watcher; \
  223. void* write_queue[2]; \
  224. void* write_completed_queue[2]; \
  225. uv_connection_cb connection_cb; \
  226. int delayed_error; \
  227. int accepted_fd; \
  228. void* queued_fds; \
  229. UV_STREAM_PRIVATE_PLATFORM_FIELDS \
  230. #define UV_TCP_PRIVATE_FIELDS /* empty */
  231. #define UV_UDP_PRIVATE_FIELDS \
  232. uv_alloc_cb alloc_cb; \
  233. uv_udp_recv_cb recv_cb; \
  234. uv__io_t io_watcher; \
  235. void* write_queue[2]; \
  236. void* write_completed_queue[2]; \
  237. #define UV_PIPE_PRIVATE_FIELDS \
  238. const char* pipe_fname; /* strdup'ed */
  239. #define UV_POLL_PRIVATE_FIELDS \
  240. uv__io_t io_watcher;
  241. #define UV_PREPARE_PRIVATE_FIELDS \
  242. uv_prepare_cb prepare_cb; \
  243. void* queue[2]; \
  244. #define UV_CHECK_PRIVATE_FIELDS \
  245. uv_check_cb check_cb; \
  246. void* queue[2]; \
  247. #define UV_IDLE_PRIVATE_FIELDS \
  248. uv_idle_cb idle_cb; \
  249. void* queue[2]; \
  250. #define UV_ASYNC_PRIVATE_FIELDS \
  251. uv_async_cb async_cb; \
  252. void* queue[2]; \
  253. int pending; \
  254. #define UV_TIMER_PRIVATE_FIELDS \
  255. uv_timer_cb timer_cb; \
  256. void* heap_node[3]; \
  257. uint64_t timeout; \
  258. uint64_t repeat; \
  259. uint64_t start_id;
  260. #define UV_GETADDRINFO_PRIVATE_FIELDS \
  261. struct uv__work work_req; \
  262. uv_getaddrinfo_cb cb; \
  263. struct addrinfo* hints; \
  264. char* hostname; \
  265. char* service; \
  266. struct addrinfo* addrinfo; \
  267. int retcode;
  268. #define UV_GETNAMEINFO_PRIVATE_FIELDS \
  269. struct uv__work work_req; \
  270. uv_getnameinfo_cb getnameinfo_cb; \
  271. struct sockaddr_storage storage; \
  272. int flags; \
  273. char host[NI_MAXHOST]; \
  274. char service[NI_MAXSERV]; \
  275. int retcode;
  276. #define UV_PROCESS_PRIVATE_FIELDS \
  277. void* queue[2]; \
  278. int status; \
  279. #define UV_FS_PRIVATE_FIELDS \
  280. const char *new_path; \
  281. uv_file file; \
  282. int flags; \
  283. mode_t mode; \
  284. unsigned int nbufs; \
  285. uv_buf_t* bufs; \
  286. off_t off; \
  287. uv_uid_t uid; \
  288. uv_gid_t gid; \
  289. double atime; \
  290. double mtime; \
  291. struct uv__work work_req; \
  292. uv_buf_t bufsml[4]; \
  293. #define UV_WORK_PRIVATE_FIELDS \
  294. struct uv__work work_req;
  295. #define UV_TTY_PRIVATE_FIELDS \
  296. struct termios orig_termios; \
  297. int mode;
  298. #define UV_SIGNAL_PRIVATE_FIELDS \
  299. /* RB_ENTRY(uv_signal_s) tree_entry; */ \
  300. struct { \
  301. struct uv_signal_s* rbe_left; \
  302. struct uv_signal_s* rbe_right; \
  303. struct uv_signal_s* rbe_parent; \
  304. int rbe_color; \
  305. } tree_entry; \
  306. /* Use two counters here so we don have to fiddle with atomics. */ \
  307. unsigned int caught_signals; \
  308. unsigned int dispatched_signals;
  309. #define UV_FS_EVENT_PRIVATE_FIELDS \
  310. uv_fs_event_cb cb; \
  311. UV_PLATFORM_FS_EVENT_FIELDS \
  312. #endif /* UV_UNIX_H */