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