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