unix.h 20 KB

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