uv-unix.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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(__PASE__)
  45. # include "uv-posix.h"
  46. #elif defined(_AIX)
  47. # include "uv-aix.h"
  48. #elif defined(__sun)
  49. # include "uv-sunos.h"
  50. #elif defined(__APPLE__)
  51. # include "uv-darwin.h"
  52. #elif defined(__DragonFly__) || \
  53. defined(__FreeBSD__) || \
  54. defined(__FreeBSD_kernel__) || \
  55. defined(__OpenBSD__) || \
  56. defined(__NetBSD__)
  57. # include "uv-bsd.h"
  58. #elif defined(__CYGWIN__) || defined(__MSYS__)
  59. # include "uv-posix.h"
  60. #endif
  61. #ifndef PTHREAD_BARRIER_SERIAL_THREAD
  62. # include "pthread-barrier.h"
  63. #endif
  64. #ifndef NI_MAXHOST
  65. # define NI_MAXHOST 1025
  66. #endif
  67. #ifndef NI_MAXSERV
  68. # define NI_MAXSERV 32
  69. #endif
  70. #ifndef UV_IO_PRIVATE_PLATFORM_FIELDS
  71. # define UV_IO_PRIVATE_PLATFORM_FIELDS /* empty */
  72. #endif
  73. struct uv__io_s;
  74. struct uv_loop_s;
  75. typedef void (*uv__io_cb)(struct uv_loop_s* loop,
  76. struct uv__io_s* w,
  77. unsigned int events);
  78. typedef struct uv__io_s uv__io_t;
  79. struct uv__io_s {
  80. uv__io_cb cb;
  81. void* pending_queue[2];
  82. void* watcher_queue[2];
  83. unsigned int pevents; /* Pending event mask i.e. mask at next tick. */
  84. unsigned int events; /* Current event mask. */
  85. int fd;
  86. UV_IO_PRIVATE_PLATFORM_FIELDS
  87. };
  88. #ifndef UV_PLATFORM_SEM_T
  89. # define UV_PLATFORM_SEM_T sem_t
  90. #endif
  91. #ifndef UV_PLATFORM_LOOP_FIELDS
  92. # define UV_PLATFORM_LOOP_FIELDS /* empty */
  93. #endif
  94. #ifndef UV_PLATFORM_FS_EVENT_FIELDS
  95. # define UV_PLATFORM_FS_EVENT_FIELDS /* empty */
  96. #endif
  97. #ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS
  98. # define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */
  99. #endif
  100. /* Note: May be cast to struct iovec. See writev(2). */
  101. typedef struct uv_buf_t {
  102. char* base;
  103. size_t len;
  104. } uv_buf_t;
  105. typedef int uv_file;
  106. typedef int uv_os_sock_t;
  107. typedef int uv_os_fd_t;
  108. typedef pid_t uv_pid_t;
  109. #define UV_ONCE_INIT PTHREAD_ONCE_INIT
  110. typedef pthread_once_t uv_once_t;
  111. typedef pthread_t uv_thread_t;
  112. typedef pthread_mutex_t uv_mutex_t;
  113. typedef pthread_rwlock_t uv_rwlock_t;
  114. typedef UV_PLATFORM_SEM_T uv_sem_t;
  115. typedef pthread_cond_t uv_cond_t;
  116. typedef pthread_key_t uv_key_t;
  117. typedef pthread_barrier_t uv_barrier_t;
  118. /* Platform-specific definitions for uv_spawn support. */
  119. typedef gid_t uv_gid_t;
  120. typedef uid_t uv_uid_t;
  121. typedef struct dirent uv__dirent_t;
  122. #if defined(DT_UNKNOWN)
  123. # define HAVE_DIRENT_TYPES
  124. # if defined(DT_REG)
  125. # define UV__DT_FILE DT_REG
  126. # else
  127. # define UV__DT_FILE -1
  128. # endif
  129. # if defined(DT_DIR)
  130. # define UV__DT_DIR DT_DIR
  131. # else
  132. # define UV__DT_DIR -2
  133. # endif
  134. # if defined(DT_LNK)
  135. # define UV__DT_LINK DT_LNK
  136. # else
  137. # define UV__DT_LINK -3
  138. # endif
  139. # if defined(DT_FIFO)
  140. # define UV__DT_FIFO DT_FIFO
  141. # else
  142. # define UV__DT_FIFO -4
  143. # endif
  144. # if defined(DT_SOCK)
  145. # define UV__DT_SOCKET DT_SOCK
  146. # else
  147. # define UV__DT_SOCKET -5
  148. # endif
  149. # if defined(DT_CHR)
  150. # define UV__DT_CHAR DT_CHR
  151. # else
  152. # define UV__DT_CHAR -6
  153. # endif
  154. # if defined(DT_BLK)
  155. # define UV__DT_BLOCK DT_BLK
  156. # else
  157. # define UV__DT_BLOCK -7
  158. # endif
  159. #endif
  160. /* Platform-specific definitions for uv_dlopen support. */
  161. #define UV_DYNAMIC /* empty */
  162. typedef struct {
  163. void* handle;
  164. char* errmsg;
  165. } uv_lib_t;
  166. #define UV_LOOP_PRIVATE_FIELDS \
  167. unsigned long flags; \
  168. int backend_fd; \
  169. void* pending_queue[2]; \
  170. void* watcher_queue[2]; \
  171. uv__io_t** watchers; \
  172. unsigned int nwatchers; \
  173. unsigned int nfds; \
  174. void* wq[2]; \
  175. uv_mutex_t wq_mutex; \
  176. uv_async_t wq_async; \
  177. uv_rwlock_t cloexec_lock; \
  178. uv_handle_t* closing_handles; \
  179. void* process_handles[2]; \
  180. void* prepare_handles[2]; \
  181. void* check_handles[2]; \
  182. void* idle_handles[2]; \
  183. void* async_handles[2]; \
  184. void (*async_unused)(void); /* TODO(bnoordhuis) Remove in libuv v2. */ \
  185. uv__io_t async_io_watcher; \
  186. int async_wfd; \
  187. struct { \
  188. void* min; \
  189. unsigned int nelts; \
  190. } timer_heap; \
  191. uint64_t timer_counter; \
  192. uint64_t time; \
  193. int signal_pipefd[2]; \
  194. uv__io_t signal_io_watcher; \
  195. uv_signal_t child_watcher; \
  196. int emfile_fd; \
  197. UV_PLATFORM_LOOP_FIELDS \
  198. #define UV_REQ_TYPE_PRIVATE /* empty */
  199. #define UV_REQ_PRIVATE_FIELDS /* empty */
  200. #define UV_PRIVATE_REQ_TYPES /* empty */
  201. #define UV_WRITE_PRIVATE_FIELDS \
  202. void* queue[2]; \
  203. unsigned int write_index; \
  204. uv_buf_t* bufs; \
  205. unsigned int nbufs; \
  206. int error; \
  207. uv_buf_t bufsml[4]; \
  208. #define UV_CONNECT_PRIVATE_FIELDS \
  209. void* queue[2]; \
  210. #define UV_SHUTDOWN_PRIVATE_FIELDS /* empty */
  211. #define UV_UDP_SEND_PRIVATE_FIELDS \
  212. void* queue[2]; \
  213. struct sockaddr_storage addr; \
  214. unsigned int nbufs; \
  215. uv_buf_t* bufs; \
  216. ssize_t status; \
  217. uv_udp_send_cb send_cb; \
  218. uv_buf_t bufsml[4]; \
  219. #define UV_HANDLE_PRIVATE_FIELDS \
  220. uv_handle_t* next_closing; \
  221. unsigned int flags; \
  222. #define UV_STREAM_PRIVATE_FIELDS \
  223. uv_connect_t *connect_req; \
  224. uv_shutdown_t *shutdown_req; \
  225. uv__io_t io_watcher; \
  226. void* write_queue[2]; \
  227. void* write_completed_queue[2]; \
  228. uv_connection_cb connection_cb; \
  229. int delayed_error; \
  230. int accepted_fd; \
  231. void* queued_fds; \
  232. UV_STREAM_PRIVATE_PLATFORM_FIELDS \
  233. #define UV_TCP_PRIVATE_FIELDS /* empty */
  234. #define UV_UDP_PRIVATE_FIELDS \
  235. uv_alloc_cb alloc_cb; \
  236. uv_udp_recv_cb recv_cb; \
  237. uv__io_t io_watcher; \
  238. void* write_queue[2]; \
  239. void* write_completed_queue[2]; \
  240. #define UV_PIPE_PRIVATE_FIELDS \
  241. const char* pipe_fname; /* strdup'ed */
  242. #define UV_POLL_PRIVATE_FIELDS \
  243. uv__io_t io_watcher;
  244. #define UV_PREPARE_PRIVATE_FIELDS \
  245. uv_prepare_cb prepare_cb; \
  246. void* queue[2]; \
  247. #define UV_CHECK_PRIVATE_FIELDS \
  248. uv_check_cb check_cb; \
  249. void* queue[2]; \
  250. #define UV_IDLE_PRIVATE_FIELDS \
  251. uv_idle_cb idle_cb; \
  252. void* queue[2]; \
  253. #define UV_ASYNC_PRIVATE_FIELDS \
  254. uv_async_cb async_cb; \
  255. void* queue[2]; \
  256. int pending; \
  257. #define UV_TIMER_PRIVATE_FIELDS \
  258. uv_timer_cb timer_cb; \
  259. void* heap_node[3]; \
  260. uint64_t timeout; \
  261. uint64_t repeat; \
  262. uint64_t start_id;
  263. #define UV_GETADDRINFO_PRIVATE_FIELDS \
  264. struct uv__work work_req; \
  265. uv_getaddrinfo_cb cb; \
  266. struct addrinfo* hints; \
  267. char* hostname; \
  268. char* service; \
  269. struct addrinfo* addrinfo; \
  270. int retcode;
  271. #define UV_GETNAMEINFO_PRIVATE_FIELDS \
  272. struct uv__work work_req; \
  273. uv_getnameinfo_cb getnameinfo_cb; \
  274. struct sockaddr_storage storage; \
  275. int flags; \
  276. char host[NI_MAXHOST]; \
  277. char service[NI_MAXSERV]; \
  278. int retcode;
  279. #define UV_PROCESS_PRIVATE_FIELDS \
  280. void* queue[2]; \
  281. int status; \
  282. #define UV_FS_PRIVATE_FIELDS \
  283. const char *new_path; \
  284. uv_file file; \
  285. int flags; \
  286. mode_t mode; \
  287. unsigned int nbufs; \
  288. uv_buf_t* bufs; \
  289. off_t off; \
  290. uv_uid_t uid; \
  291. uv_gid_t gid; \
  292. double atime; \
  293. double mtime; \
  294. struct uv__work work_req; \
  295. uv_buf_t bufsml[4]; \
  296. #define UV_WORK_PRIVATE_FIELDS \
  297. struct uv__work work_req;
  298. #define UV_TTY_PRIVATE_FIELDS \
  299. struct termios orig_termios; \
  300. int mode;
  301. #define UV_SIGNAL_PRIVATE_FIELDS \
  302. /* RB_ENTRY(uv_signal_s) tree_entry; */ \
  303. struct { \
  304. struct uv_signal_s* rbe_left; \
  305. struct uv_signal_s* rbe_right; \
  306. struct uv_signal_s* rbe_parent; \
  307. int rbe_color; \
  308. } tree_entry; \
  309. /* Use two counters here so we don have to fiddle with atomics. */ \
  310. unsigned int caught_signals; \
  311. unsigned int dispatched_signals;
  312. #define UV_FS_EVENT_PRIVATE_FIELDS \
  313. uv_fs_event_cb cb; \
  314. UV_PLATFORM_FS_EVENT_FIELDS \
  315. /* fs open() flags supported on this platform: */
  316. #if defined(O_APPEND)
  317. # define UV_FS_O_APPEND O_APPEND
  318. #else
  319. # define UV_FS_O_APPEND 0
  320. #endif
  321. #if defined(O_CREAT)
  322. # define UV_FS_O_CREAT O_CREAT
  323. #else
  324. # define UV_FS_O_CREAT 0
  325. #endif
  326. #if defined(O_DIRECT)
  327. # define UV_FS_O_DIRECT O_DIRECT
  328. #else
  329. # define UV_FS_O_DIRECT 0
  330. #endif
  331. #if defined(O_DIRECTORY)
  332. # define UV_FS_O_DIRECTORY O_DIRECTORY
  333. #else
  334. # define UV_FS_O_DIRECTORY 0
  335. #endif
  336. #if defined(O_DSYNC)
  337. # define UV_FS_O_DSYNC O_DSYNC
  338. #else
  339. # define UV_FS_O_DSYNC 0
  340. #endif
  341. #if defined(O_EXCL)
  342. # define UV_FS_O_EXCL O_EXCL
  343. #else
  344. # define UV_FS_O_EXCL 0
  345. #endif
  346. #if defined(O_EXLOCK)
  347. # define UV_FS_O_EXLOCK O_EXLOCK
  348. #else
  349. # define UV_FS_O_EXLOCK 0
  350. #endif
  351. #if defined(O_NOATIME)
  352. # define UV_FS_O_NOATIME O_NOATIME
  353. #else
  354. # define UV_FS_O_NOATIME 0
  355. #endif
  356. #if defined(O_NOCTTY)
  357. # define UV_FS_O_NOCTTY O_NOCTTY
  358. #else
  359. # define UV_FS_O_NOCTTY 0
  360. #endif
  361. #if defined(O_NOFOLLOW)
  362. # define UV_FS_O_NOFOLLOW O_NOFOLLOW
  363. #else
  364. # define UV_FS_O_NOFOLLOW 0
  365. #endif
  366. #if defined(O_NONBLOCK)
  367. # define UV_FS_O_NONBLOCK O_NONBLOCK
  368. #else
  369. # define UV_FS_O_NONBLOCK 0
  370. #endif
  371. #if defined(O_RDONLY)
  372. # define UV_FS_O_RDONLY O_RDONLY
  373. #else
  374. # define UV_FS_O_RDONLY 0
  375. #endif
  376. #if defined(O_RDWR)
  377. # define UV_FS_O_RDWR O_RDWR
  378. #else
  379. # define UV_FS_O_RDWR 0
  380. #endif
  381. #if defined(O_SYMLINK)
  382. # define UV_FS_O_SYMLINK O_SYMLINK
  383. #else
  384. # define UV_FS_O_SYMLINK 0
  385. #endif
  386. #if defined(O_SYNC)
  387. # define UV_FS_O_SYNC O_SYNC
  388. #else
  389. # define UV_FS_O_SYNC 0
  390. #endif
  391. #if defined(O_TRUNC)
  392. # define UV_FS_O_TRUNC O_TRUNC
  393. #else
  394. # define UV_FS_O_TRUNC 0
  395. #endif
  396. #if defined(O_WRONLY)
  397. # define UV_FS_O_WRONLY O_WRONLY
  398. #else
  399. # define UV_FS_O_WRONLY 0
  400. #endif
  401. /* fs open() flags supported on other platforms: */
  402. #define UV_FS_O_RANDOM 0
  403. #define UV_FS_O_SHORT_LIVED 0
  404. #define UV_FS_O_SEQUENTIAL 0
  405. #define UV_FS_O_TEMPORARY 0
  406. #endif /* UV_UNIX_H */