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