unix.h 18 KB

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