1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* this file contains system-dependent definitions used by ADB 18 * they're related to threads, sockets and file descriptors 19 */ 20 #ifndef _ADB_SYSDEPS_H 21 #define _ADB_SYSDEPS_H 22 23 #ifdef __CYGWIN__ 24 # undef _WIN32 25 #endif 26 27 #include <errno.h> 28 29 #include <string> 30 #include <vector> 31 32 // Include this before open/close/unlink are defined as macros below. 33 #include <android-base/errors.h> 34 #include <android-base/unique_fd.h> 35 #include <android-base/utf8.h> 36 37 #include "sysdeps/stat.h" 38 39 /* 40 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of 41 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's 42 * not already defined, then define it here. 43 */ 44 #ifndef TEMP_FAILURE_RETRY 45 /* Used to retry syscalls that can return EINTR. */ 46 #define TEMP_FAILURE_RETRY(exp) ({ \ 47 typeof (exp) _rc; \ 48 do { \ 49 _rc = (exp); \ 50 } while (_rc == -1 && errno == EINTR); \ 51 _rc; }) 52 #endif 53 54 // Some printf-like functions are implemented in terms of 55 // android::base::StringAppendV, so they should use the same attribute for 56 // compile-time format string checking. On Windows, if the mingw version of 57 // vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd 58 // and PRIu64 (and related) to be recognized by the compile-time checking. 59 #define ADB_FORMAT_ARCHETYPE __printf__ 60 #ifdef __USE_MINGW_ANSI_STDIO 61 #if __USE_MINGW_ANSI_STDIO 62 #undef ADB_FORMAT_ARCHETYPE 63 #define ADB_FORMAT_ARCHETYPE gnu_printf 64 #endif 65 #endif 66 67 #ifdef _WIN32 68 69 // Clang-only nullability specifiers 70 #define _Nonnull 71 #define _Nullable 72 73 #include <ctype.h> 74 #include <direct.h> 75 #include <dirent.h> 76 #include <errno.h> 77 #include <fcntl.h> 78 #include <io.h> 79 #include <process.h> 80 #include <sys/stat.h> 81 #include <utime.h> 82 #include <winsock2.h> 83 #include <windows.h> 84 #include <ws2tcpip.h> 85 86 #include <memory> // unique_ptr 87 #include <string> 88 89 #include "fdevent.h" 90 91 #define OS_PATH_SEPARATORS "\\/" 92 #define OS_PATH_SEPARATOR '\\' 93 #define OS_PATH_SEPARATOR_STR "\\" 94 #define ENV_PATH_SEPARATOR_STR ";" 95 96 static __inline__ bool adb_is_separator(char c) { 97 return c == '\\' || c == '/'; 98 } 99 100 typedef CRITICAL_SECTION adb_mutex_t; 101 102 #define ADB_MUTEX_DEFINE(x) adb_mutex_t x 103 104 /* declare all mutexes */ 105 /* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */ 106 #define ADB_MUTEX(x) extern adb_mutex_t x; 107 #include "mutex_list.h" 108 109 extern void adb_sysdeps_init(void); 110 111 static __inline__ void adb_mutex_lock( adb_mutex_t* lock ) 112 { 113 EnterCriticalSection( lock ); 114 } 115 116 static __inline__ void adb_mutex_unlock( adb_mutex_t* lock ) 117 { 118 LeaveCriticalSection( lock ); 119 } 120 121 typedef void (*adb_thread_func_t)(void* arg); 122 typedef HANDLE adb_thread_t; 123 124 struct adb_winthread_args { 125 adb_thread_func_t func; 126 void* arg; 127 }; 128 129 static unsigned __stdcall adb_winthread_wrapper(void* heap_args) { 130 // Move the arguments from the heap onto the thread's stack. 131 adb_winthread_args thread_args = *static_cast<adb_winthread_args*>(heap_args); 132 delete static_cast<adb_winthread_args*>(heap_args); 133 thread_args.func(thread_args.arg); 134 return 0; 135 } 136 137 static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg, 138 adb_thread_t* thread = nullptr) { 139 adb_winthread_args* args = new adb_winthread_args{.func = func, .arg = arg}; 140 uintptr_t handle = _beginthreadex(nullptr, 0, adb_winthread_wrapper, args, 0, nullptr); 141 if (handle != static_cast<uintptr_t>(0)) { 142 if (thread) { 143 *thread = reinterpret_cast<HANDLE>(handle); 144 } else { 145 CloseHandle(thread); 146 } 147 return true; 148 } 149 return false; 150 } 151 152 static __inline__ bool adb_thread_join(adb_thread_t thread) { 153 switch (WaitForSingleObject(thread, INFINITE)) { 154 case WAIT_OBJECT_0: 155 CloseHandle(thread); 156 return true; 157 158 case WAIT_FAILED: 159 fprintf(stderr, "adb_thread_join failed: %s\n", 160 android::base::SystemErrorCodeToString(GetLastError()).c_str()); 161 break; 162 163 default: 164 abort(); 165 } 166 167 return false; 168 } 169 170 static __inline__ bool adb_thread_detach(adb_thread_t thread) { 171 CloseHandle(thread); 172 return true; 173 } 174 175 static __inline__ void __attribute__((noreturn)) adb_thread_exit() { 176 _endthreadex(0); 177 } 178 179 static __inline__ int adb_thread_setname(const std::string& name) { 180 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set 181 // the thread name in Windows. Unfortunately, it only works during debugging, but 182 // our build process doesn't generate PDB files needed for debugging. 183 return 0; 184 } 185 186 static __inline__ adb_thread_t adb_thread_self() { 187 return GetCurrentThread(); 188 } 189 190 static __inline__ bool adb_thread_equal(adb_thread_t lhs, adb_thread_t rhs) { 191 return GetThreadId(lhs) == GetThreadId(rhs); 192 } 193 194 static __inline__ unsigned long adb_thread_id() 195 { 196 return GetCurrentThreadId(); 197 } 198 199 static __inline__ void close_on_exec(int fd) 200 { 201 /* nothing really */ 202 } 203 204 #define S_ISLNK(m) 0 /* no symlinks on Win32 */ 205 206 extern int adb_unlink(const char* path); 207 #undef unlink 208 #define unlink ___xxx_unlink 209 210 extern int adb_mkdir(const std::string& path, int mode); 211 #undef mkdir 212 #define mkdir ___xxx_mkdir 213 214 // See the comments for the !defined(_WIN32) versions of adb_*(). 215 extern int adb_open(const char* path, int options); 216 extern int adb_creat(const char* path, int mode); 217 extern int adb_read(int fd, void* buf, int len); 218 extern int adb_write(int fd, const void* buf, int len); 219 extern int adb_lseek(int fd, int pos, int where); 220 extern int adb_shutdown(int fd); 221 extern int adb_close(int fd); 222 223 // See the comments for the !defined(_WIN32) version of unix_close(). 224 static __inline__ int unix_close(int fd) 225 { 226 return close(fd); 227 } 228 #undef close 229 #define close ____xxx_close 230 231 // Like unix_read(), but may return EINTR. 232 extern int unix_read_interruptible(int fd, void* buf, size_t len); 233 234 // See the comments for the !defined(_WIN32) version of unix_read(). 235 static __inline__ int unix_read(int fd, void* buf, size_t len) { 236 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len)); 237 } 238 239 #undef read 240 #define read ___xxx_read 241 242 // See the comments for the !defined(_WIN32) version of unix_write(). 243 static __inline__ int unix_write(int fd, const void* buf, size_t len) 244 { 245 return write(fd, buf, len); 246 } 247 #undef write 248 #define write ___xxx_write 249 250 // See the comments for the !defined(_WIN32) version of adb_open_mode(). 251 static __inline__ int adb_open_mode(const char* path, int options, int mode) 252 { 253 return adb_open(path, options); 254 } 255 256 // See the comments for the !defined(_WIN32) version of unix_open(). 257 extern int unix_open(const char* path, int options, ...); 258 #define open ___xxx_unix_open 259 260 // Checks if |fd| corresponds to a console. 261 // Standard Windows isatty() returns 1 for both console FDs and character 262 // devices like NUL. unix_isatty() performs some extra checking to only match 263 // console FDs. 264 // |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs 265 // will work but adb_open() FDs will not. Additionally the OS handle associated 266 // with |fd| must have GENERIC_READ access (which console FDs have by default). 267 // Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after 268 // calling this function is unreliable and should not be used. 269 int unix_isatty(int fd); 270 #define isatty ___xxx_isatty 271 272 /* normally provided by <cutils/misc.h> */ 273 extern void* load_file(const char* pathname, unsigned* psize); 274 275 static __inline__ void adb_sleep_ms( int mseconds ) 276 { 277 Sleep( mseconds ); 278 } 279 280 int network_loopback_client(int port, int type, std::string* error); 281 int network_loopback_server(int port, int type, std::string* error); 282 int network_inaddr_any_server(int port, int type, std::string* error); 283 int network_connect(const std::string& host, int port, int type, int timeout, 284 std::string* error); 285 286 extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen); 287 288 #undef accept 289 #define accept ___xxx_accept 290 291 extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen); 292 293 #undef setsockopt 294 #define setsockopt ___xxx_setsockopt 295 296 extern int adb_socketpair( int sv[2] ); 297 298 struct adb_pollfd { 299 int fd; 300 short events; 301 short revents; 302 }; 303 extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout); 304 #define poll ___xxx_poll 305 306 static __inline__ int adb_is_absolute_host_path(const char* path) { 307 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; 308 } 309 310 // UTF-8 versions of POSIX APIs. 311 extern DIR* adb_opendir(const char* dirname); 312 extern struct dirent* adb_readdir(DIR* dir); 313 extern int adb_closedir(DIR* dir); 314 315 extern int adb_utime(const char *, struct utimbuf *); 316 extern int adb_chmod(const char *, int); 317 318 extern int adb_vfprintf(FILE *stream, const char *format, va_list ap) 319 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0))); 320 extern int adb_vprintf(const char *format, va_list ap) 321 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0))); 322 extern int adb_fprintf(FILE *stream, const char *format, ...) 323 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))); 324 extern int adb_printf(const char *format, ...) 325 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2))); 326 327 extern int adb_fputs(const char* buf, FILE* stream); 328 extern int adb_fputc(int ch, FILE* stream); 329 extern int adb_putchar(int ch); 330 extern int adb_puts(const char* buf); 331 extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, 332 FILE* stream); 333 334 extern FILE* adb_fopen(const char* f, const char* m); 335 336 extern char* adb_getenv(const char* name); 337 338 extern char* adb_getcwd(char* buf, int size); 339 340 // Remap calls to POSIX APIs to our UTF-8 versions. 341 #define opendir adb_opendir 342 #define readdir adb_readdir 343 #define closedir adb_closedir 344 #define rewinddir rewinddir_utf8_not_yet_implemented 345 #define telldir telldir_utf8_not_yet_implemented 346 // Some compiler's C++ headers have members named seekdir, so we can't do the 347 // macro technique and instead cause a link error if seekdir is called. 348 inline void seekdir(DIR*, long) { 349 extern int seekdir_utf8_not_yet_implemented; 350 seekdir_utf8_not_yet_implemented = 1; 351 } 352 353 #define utime adb_utime 354 #define chmod adb_chmod 355 356 #define vfprintf adb_vfprintf 357 #define vprintf adb_vprintf 358 #define fprintf adb_fprintf 359 #define printf adb_printf 360 #define fputs adb_fputs 361 #define fputc adb_fputc 362 // putc may be a macro, so if so, undefine it, so that we can redefine it. 363 #undef putc 364 #define putc(c, s) adb_fputc(c, s) 365 #define putchar adb_putchar 366 #define puts adb_puts 367 #define fwrite adb_fwrite 368 369 #define fopen adb_fopen 370 #define freopen freopen_utf8_not_yet_implemented 371 372 #define getenv adb_getenv 373 #define putenv putenv_utf8_not_yet_implemented 374 #define setenv setenv_utf8_not_yet_implemented 375 #define unsetenv unsetenv_utf8_not_yet_implemented 376 377 #define getcwd adb_getcwd 378 379 char* adb_strerror(int err); 380 #define strerror adb_strerror 381 382 // Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be 383 // passed to main(). 384 class NarrowArgs { 385 public: 386 NarrowArgs(int argc, wchar_t** argv); 387 ~NarrowArgs(); 388 389 inline char** data() { 390 return narrow_args; 391 } 392 393 private: 394 char** narrow_args; 395 }; 396 397 // Windows HANDLE values only use 32-bits of the type, even on 64-bit machines, 398 // so they can fit in an int. To convert back, we just need to sign-extend. 399 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx 400 // Note that this does not make a HANDLE value work with APIs like open(), nor 401 // does this make a value from open() passable to APIs taking a HANDLE. This 402 // just lets you take a HANDLE, pass it around as an int, and then use it again 403 // as a HANDLE. 404 inline int cast_handle_to_int(const HANDLE h) { 405 // truncate 406 return static_cast<int>(reinterpret_cast<INT_PTR>(h)); 407 } 408 409 inline HANDLE cast_int_to_handle(const int fd) { 410 // sign-extend 411 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd)); 412 } 413 414 // Deleter for unique_handle. Adapted from many sources, including: 415 // http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api 416 // https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx 417 class handle_deleter { 418 public: 419 typedef HANDLE pointer; 420 421 void operator()(HANDLE h); 422 }; 423 424 // Like std::unique_ptr, but for Windows HANDLE objects that should be 425 // CloseHandle()'d. Operator bool() only checks if the handle != nullptr, 426 // but does not check if the handle != INVALID_HANDLE_VALUE. 427 typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle; 428 429 namespace internal { 430 431 size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes); 432 433 } 434 435 #else /* !_WIN32 a.k.a. Unix */ 436 437 #include <cutils/misc.h> 438 #include <cutils/sockets.h> 439 #include <cutils/threads.h> 440 #include <fcntl.h> 441 #include <poll.h> 442 #include <signal.h> 443 #include <sys/stat.h> 444 #include <sys/wait.h> 445 446 #include <pthread.h> 447 #include <unistd.h> 448 #include <fcntl.h> 449 #include <stdarg.h> 450 #include <netdb.h> 451 #include <netinet/in.h> 452 #include <netinet/tcp.h> 453 #include <string.h> 454 #include <unistd.h> 455 456 #include <string> 457 458 #define OS_PATH_SEPARATORS "/" 459 #define OS_PATH_SEPARATOR '/' 460 #define OS_PATH_SEPARATOR_STR "/" 461 #define ENV_PATH_SEPARATOR_STR ":" 462 463 static __inline__ bool adb_is_separator(char c) { 464 return c == '/'; 465 } 466 467 typedef pthread_mutex_t adb_mutex_t; 468 469 #define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 470 #define adb_mutex_init pthread_mutex_init 471 #define adb_mutex_lock pthread_mutex_lock 472 #define adb_mutex_unlock pthread_mutex_unlock 473 #define adb_mutex_destroy pthread_mutex_destroy 474 475 #define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER 476 477 #define adb_cond_t pthread_cond_t 478 #define adb_cond_init pthread_cond_init 479 #define adb_cond_wait pthread_cond_wait 480 #define adb_cond_broadcast pthread_cond_broadcast 481 #define adb_cond_signal pthread_cond_signal 482 #define adb_cond_destroy pthread_cond_destroy 483 484 /* declare all mutexes */ 485 #define ADB_MUTEX(x) extern adb_mutex_t x; 486 #include "mutex_list.h" 487 488 static __inline__ void close_on_exec(int fd) 489 { 490 fcntl( fd, F_SETFD, FD_CLOEXEC ); 491 } 492 493 // Open a file and return a file descriptor that may be used with unix_read(), 494 // unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close(). 495 // 496 // On Unix, this is based on open(), so the file descriptor is a real OS file 497 // descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a 498 // file descriptor that can only be used with C Runtime APIs (which are wrapped 499 // by unix_read(), unix_write(), unix_close()). Also, the C Runtime has 500 // configurable CR/LF translation which defaults to text mode, but is settable 501 // with _setmode(). 502 static __inline__ int unix_open(const char* path, int options,...) 503 { 504 if ((options & O_CREAT) == 0) 505 { 506 return TEMP_FAILURE_RETRY( open(path, options) ); 507 } 508 else 509 { 510 int mode; 511 va_list args; 512 va_start( args, options ); 513 mode = va_arg( args, int ); 514 va_end( args ); 515 return TEMP_FAILURE_RETRY( open( path, options, mode ) ); 516 } 517 } 518 519 // Similar to the two-argument adb_open(), but takes a mode parameter for file 520 // creation. See adb_open() for more info. 521 static __inline__ int adb_open_mode( const char* pathname, int options, int mode ) 522 { 523 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) ); 524 } 525 526 527 // Open a file and return a file descriptor that may be used with adb_read(), 528 // adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close(). 529 // 530 // On Unix, this is based on open(), but the Windows implementation (in 531 // sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime 532 // and its CR/LF translation. The returned file descriptor should be used with 533 // adb_read(), adb_write(), adb_close(), etc. 534 static __inline__ int adb_open( const char* pathname, int options ) 535 { 536 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) ); 537 if (fd < 0) 538 return -1; 539 close_on_exec( fd ); 540 return fd; 541 } 542 #undef open 543 #define open ___xxx_open 544 545 static __inline__ int adb_shutdown(int fd) 546 { 547 return shutdown(fd, SHUT_RDWR); 548 } 549 static __inline__ int adb_shutdown(int fd, int direction) 550 { 551 return shutdown(fd, direction); 552 } 553 #undef shutdown 554 #define shutdown ____xxx_shutdown 555 556 // Closes a file descriptor that came from adb_open() or adb_open_mode(), but 557 // not designed to take a file descriptor from unix_open(). See the comments 558 // for adb_open() for more info. 559 __inline__ int adb_close(int fd) { 560 return close(fd); 561 } 562 #undef close 563 #define close ____xxx_close 564 565 566 static __inline__ int adb_read(int fd, void* buf, size_t len) 567 { 568 return TEMP_FAILURE_RETRY( read( fd, buf, len ) ); 569 } 570 571 // Like unix_read(), but does not handle EINTR. 572 static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) { 573 return read(fd, buf, len); 574 } 575 576 #undef read 577 #define read ___xxx_read 578 579 static __inline__ int adb_write(int fd, const void* buf, size_t len) 580 { 581 return TEMP_FAILURE_RETRY( write( fd, buf, len ) ); 582 } 583 #undef write 584 #define write ___xxx_write 585 586 static __inline__ int adb_lseek(int fd, int pos, int where) 587 { 588 return lseek(fd, pos, where); 589 } 590 #undef lseek 591 #define lseek ___xxx_lseek 592 593 static __inline__ int adb_unlink(const char* path) 594 { 595 return unlink(path); 596 } 597 #undef unlink 598 #define unlink ___xxx_unlink 599 600 static __inline__ int adb_creat(const char* path, int mode) 601 { 602 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) ); 603 604 if ( fd < 0 ) 605 return -1; 606 607 close_on_exec(fd); 608 return fd; 609 } 610 #undef creat 611 #define creat ___xxx_creat 612 613 static __inline__ int unix_isatty(int fd) { 614 return isatty(fd); 615 } 616 #define isatty ___xxx_isatty 617 618 // Helper for network_* functions. 619 inline int _fd_set_error_str(int fd, std::string* error) { 620 if (fd == -1) { 621 *error = strerror(errno); 622 } 623 return fd; 624 } 625 626 inline int network_loopback_client(int port, int type, std::string* error) { 627 return _fd_set_error_str(socket_loopback_client(port, type), error); 628 } 629 630 inline int network_loopback_server(int port, int type, std::string* error) { 631 return _fd_set_error_str(socket_loopback_server(port, type), error); 632 } 633 634 inline int network_inaddr_any_server(int port, int type, std::string* error) { 635 return _fd_set_error_str(socket_inaddr_any_server(port, type), error); 636 } 637 638 inline int network_local_server(const char *name, int namespace_id, int type, 639 std::string* error) { 640 return _fd_set_error_str(socket_local_server(name, namespace_id, type), 641 error); 642 } 643 644 inline int network_connect(const std::string& host, int port, int type, 645 int timeout, std::string* error) { 646 int getaddrinfo_error = 0; 647 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout, 648 &getaddrinfo_error); 649 if (fd != -1) { 650 return fd; 651 } 652 if (getaddrinfo_error != 0) { 653 *error = gai_strerror(getaddrinfo_error); 654 } else { 655 *error = strerror(errno); 656 } 657 return -1; 658 } 659 660 static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen) 661 { 662 int fd; 663 664 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) ); 665 if (fd >= 0) 666 close_on_exec(fd); 667 668 return fd; 669 } 670 671 #undef accept 672 #define accept ___xxx_accept 673 674 // Operate on a file descriptor returned from unix_open() or a well-known file 675 // descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO. 676 // 677 // On Unix, unix_read(), unix_write(), unix_close() map to adb_read(), 678 // adb_write(), adb_close() (which all map to Unix system calls), but the 679 // Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call 680 // into the C Runtime and its configurable CR/LF translation (which is settable 681 // via _setmode()). 682 #define unix_read adb_read 683 #define unix_write adb_write 684 #define unix_close adb_close 685 686 // Win32 is limited to DWORDs for thread return values; limit the POSIX systems to this as well to 687 // ensure compatibility. 688 typedef void (*adb_thread_func_t)(void* arg); 689 typedef pthread_t adb_thread_t; 690 691 struct adb_pthread_args { 692 adb_thread_func_t func; 693 void* arg; 694 }; 695 696 static void* adb_pthread_wrapper(void* heap_args) { 697 // Move the arguments from the heap onto the thread's stack. 698 adb_pthread_args thread_args = *reinterpret_cast<adb_pthread_args*>(heap_args); 699 delete static_cast<adb_pthread_args*>(heap_args); 700 thread_args.func(thread_args.arg); 701 return nullptr; 702 } 703 704 static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg, 705 adb_thread_t* thread = nullptr) { 706 pthread_t temp; 707 pthread_attr_t attr; 708 pthread_attr_init(&attr); 709 pthread_attr_setdetachstate(&attr, thread ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED); 710 auto* pthread_args = new adb_pthread_args{.func = start, .arg = arg}; 711 errno = pthread_create(&temp, &attr, adb_pthread_wrapper, pthread_args); 712 if (errno == 0) { 713 if (thread) { 714 *thread = temp; 715 } 716 return true; 717 } 718 return false; 719 } 720 721 static __inline__ bool adb_thread_join(adb_thread_t thread) { 722 errno = pthread_join(thread, nullptr); 723 return errno == 0; 724 } 725 726 static __inline__ bool adb_thread_detach(adb_thread_t thread) { 727 errno = pthread_detach(thread); 728 return errno == 0; 729 } 730 731 static __inline__ void __attribute__((noreturn)) adb_thread_exit() { 732 pthread_exit(nullptr); 733 } 734 735 static __inline__ int adb_thread_setname(const std::string& name) { 736 #ifdef __APPLE__ 737 return pthread_setname_np(name.c_str()); 738 #else 739 const char *s = name.c_str(); 740 741 // pthread_setname_np fails rather than truncating long strings. 742 const int max_task_comm_len = 16; // including the null terminator 743 if (name.length() > (max_task_comm_len - 1)) { 744 char buf[max_task_comm_len]; 745 strncpy(buf, name.c_str(), sizeof(buf) - 1); 746 buf[sizeof(buf) - 1] = '\0'; 747 s = buf; 748 } 749 750 return pthread_setname_np(pthread_self(), s) ; 751 #endif 752 } 753 754 static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen ) 755 { 756 return setsockopt( fd, level, optname, optval, optlen ); 757 } 758 759 #undef setsockopt 760 #define setsockopt ___xxx_setsockopt 761 762 static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] ) 763 { 764 return socketpair( d, type, protocol, sv ); 765 } 766 767 static __inline__ int adb_socketpair( int sv[2] ) 768 { 769 int rc; 770 771 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); 772 if (rc < 0) 773 return -1; 774 775 close_on_exec( sv[0] ); 776 close_on_exec( sv[1] ); 777 return 0; 778 } 779 780 #undef socketpair 781 #define socketpair ___xxx_socketpair 782 783 typedef struct pollfd adb_pollfd; 784 static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) { 785 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout)); 786 } 787 788 #define poll ___xxx_poll 789 790 static __inline__ void adb_sleep_ms( int mseconds ) 791 { 792 usleep( mseconds*1000 ); 793 } 794 795 static __inline__ int adb_mkdir(const std::string& path, int mode) 796 { 797 return mkdir(path.c_str(), mode); 798 } 799 800 #undef mkdir 801 #define mkdir ___xxx_mkdir 802 803 static __inline__ void adb_sysdeps_init(void) 804 { 805 } 806 807 static __inline__ int adb_is_absolute_host_path(const char* path) { 808 return path[0] == '/'; 809 } 810 811 static __inline__ unsigned long adb_thread_id() 812 { 813 return (unsigned long)gettid(); 814 } 815 816 #endif /* !_WIN32 */ 817 818 static inline void disable_tcp_nagle(int fd) { 819 int off = 1; 820 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off)); 821 } 822 823 // Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set 824 // |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be 825 // configured to drop after 10 missed keepalives. Returns true on success. 826 bool set_tcp_keepalive(int fd, int interval_sec); 827 828 #endif /* _ADB_SYSDEPS_H */ 829