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