unix.h 18 KB

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