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