uv-unix.h 18 KB

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