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