unix.h 19 KB

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