unix.h 19 KB

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