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