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> |
| 30 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 31 | /* |
| 32 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of |
| 33 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's |
| 34 | * not already defined, then define it here. |
| 35 | */ |
| 36 | #ifndef TEMP_FAILURE_RETRY |
| 37 | /* Used to retry syscalls that can return EINTR. */ |
| 38 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 39 | typeof (exp) _rc; \ |
| 40 | do { \ |
| 41 | _rc = (exp); \ |
| 42 | } while (_rc == -1 && errno == EINTR); \ |
| 43 | _rc; }) |
| 44 | #endif |
| 45 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 46 | // Some printf-like functions are implemented in terms of |
| 47 | // android::base::StringAppendV, so they should use the same attribute for |
| 48 | // compile-time format string checking. On Windows, if the mingw version of |
| 49 | // vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd |
| 50 | // and PRIu64 (and related) to be recognized by the compile-time checking. |
| 51 | #define ADB_FORMAT_ARCHETYPE __printf__ |
| 52 | #ifdef __USE_MINGW_ANSI_STDIO |
| 53 | #if __USE_MINGW_ANSI_STDIO |
| 54 | #undef ADB_FORMAT_ARCHETYPE |
| 55 | #define ADB_FORMAT_ARCHETYPE gnu_printf |
| 56 | #endif |
| 57 | #endif |
| 58 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | #ifdef _WIN32 |
| 60 | |
Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 61 | #include <ctype.h> |
| 62 | #include <direct.h> |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 63 | #include <dirent.h> |
Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 64 | #include <errno.h> |
| 65 | #include <fcntl.h> |
| 66 | #include <io.h> |
| 67 | #include <process.h> |
| 68 | #include <sys/stat.h> |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 69 | #include <utime.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | #include <winsock2.h> |
Stephen Hines | 2f431a8 | 2014-10-01 17:37:06 -0700 | [diff] [blame] | 71 | #include <windows.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | #include <ws2tcpip.h> |
Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 73 | |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame^] | 74 | #include <memory> // unique_ptr |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 75 | #include <string> // Prototypes for narrow() and widen() use std::(w)string. |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 76 | |
Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 77 | #include "fdevent.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 79 | #define OS_PATH_SEPARATORS "\\/" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | #define OS_PATH_SEPARATOR '\\' |
| 81 | #define OS_PATH_SEPARATOR_STR "\\" |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 82 | #define ENV_PATH_SEPARATOR_STR ";" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | |
| 84 | typedef CRITICAL_SECTION adb_mutex_t; |
| 85 | |
| 86 | #define ADB_MUTEX_DEFINE(x) adb_mutex_t x |
| 87 | |
| 88 | /* declare all mutexes */ |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 89 | /* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | #define ADB_MUTEX(x) extern adb_mutex_t x; |
| 91 | #include "mutex_list.h" |
| 92 | |
| 93 | extern void adb_sysdeps_init(void); |
| 94 | |
| 95 | static __inline__ void adb_mutex_lock( adb_mutex_t* lock ) |
| 96 | { |
| 97 | EnterCriticalSection( lock ); |
| 98 | } |
| 99 | |
| 100 | static __inline__ void adb_mutex_unlock( adb_mutex_t* lock ) |
| 101 | { |
| 102 | LeaveCriticalSection( lock ); |
| 103 | } |
| 104 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | typedef void* (*adb_thread_func_t)(void* arg); |
| 106 | |
| 107 | typedef void (*win_thread_func_t)(void* arg); |
| 108 | |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 109 | static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) { |
Elliott Hughes | 2e4a2ee | 2015-05-05 14:34:41 -0700 | [diff] [blame] | 110 | uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg); |
| 111 | return (tid != static_cast<uintptr_t>(-1L)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | } |
| 113 | |
leozwang | cbf0267 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 114 | static __inline__ unsigned long adb_thread_id() |
| 115 | { |
| 116 | return GetCurrentThreadId(); |
| 117 | } |
| 118 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | static __inline__ void close_on_exec(int fd) |
| 120 | { |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 121 | /* nothing really */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } |
| 123 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 | #define lstat stat /* no symlinks on Win32 */ |
| 125 | |
| 126 | #define S_ISLNK(m) 0 /* no symlinks on Win32 */ |
| 127 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 128 | extern int adb_unlink(const char* path); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | #undef unlink |
| 130 | #define unlink ___xxx_unlink |
| 131 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 132 | 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] | 133 | #undef mkdir |
| 134 | #define mkdir ___xxx_mkdir |
| 135 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 136 | // 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] | 137 | extern int adb_open(const char* path, int options); |
| 138 | extern int adb_creat(const char* path, int mode); |
| 139 | extern int adb_read(int fd, void* buf, int len); |
| 140 | extern int adb_write(int fd, const void* buf, int len); |
| 141 | extern int adb_lseek(int fd, int pos, int where); |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 142 | extern int adb_shutdown(int fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | extern int adb_close(int fd); |
| 144 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 145 | // 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] | 146 | static __inline__ int unix_close(int fd) |
| 147 | { |
| 148 | return close(fd); |
| 149 | } |
| 150 | #undef close |
| 151 | #define close ____xxx_close |
| 152 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 153 | // See the comments for the !defined(_WIN32) version of unix_read(). |
Spencer Low | 5018406 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 154 | extern int unix_read(int fd, void* buf, size_t len); |
| 155 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | #undef read |
| 157 | #define read ___xxx_read |
| 158 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 159 | // 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] | 160 | static __inline__ int unix_write(int fd, const void* buf, size_t len) |
| 161 | { |
| 162 | return write(fd, buf, len); |
| 163 | } |
| 164 | #undef write |
| 165 | #define write ___xxx_write |
| 166 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 167 | // 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] | 168 | static __inline__ int adb_open_mode(const char* path, int options, int mode) |
| 169 | { |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 170 | return adb_open(path, options); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 173 | // See the comments for the !defined(_WIN32) version of unix_open(). |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 174 | extern int unix_open(const char* path, int options, ...); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | #define open ___xxx_unix_open |
| 176 | |
| 177 | |
| 178 | /* normally provided by <cutils/misc.h> */ |
| 179 | extern void* load_file(const char* pathname, unsigned* psize); |
| 180 | |
David 'Digit' Turner | 414ff7d | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 181 | /* normally provided by "fdevent.h" */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | |
| 183 | #define FDE_READ 0x0001 |
| 184 | #define FDE_WRITE 0x0002 |
| 185 | #define FDE_ERROR 0x0004 |
| 186 | #define FDE_DONT_CLOSE 0x0080 |
| 187 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 188 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); |
| 189 | |
| 190 | fdevent *fdevent_create(int fd, fd_func func, void *arg); |
| 191 | void fdevent_destroy(fdevent *fde); |
| 192 | void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); |
| 193 | void fdevent_remove(fdevent *item); |
| 194 | void fdevent_set(fdevent *fde, unsigned events); |
| 195 | void fdevent_add(fdevent *fde, unsigned events); |
| 196 | void fdevent_del(fdevent *fde, unsigned events); |
| 197 | void fdevent_loop(); |
| 198 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | static __inline__ void adb_sleep_ms( int mseconds ) |
| 200 | { |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 201 | Sleep( mseconds ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 204 | int network_loopback_client(int port, int type, std::string* error); |
| 205 | int network_loopback_server(int port, int type, std::string* error); |
| 206 | int network_inaddr_any_server(int port, int type, std::string* error); |
| 207 | int network_connect(const std::string& host, int port, int type, int timeout, |
| 208 | std::string* error); |
| 209 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen); |
| 211 | |
| 212 | #undef accept |
| 213 | #define accept ___xxx_accept |
| 214 | |
Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 215 | extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen); |
| 216 | |
| 217 | #undef setsockopt |
| 218 | #define setsockopt ___xxx_setsockopt |
| 219 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) |
| 221 | { |
| 222 | int opt = bufsize; |
Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 223 | return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt)); |
| 224 | } |
| 225 | |
| 226 | static __inline__ void disable_tcp_nagle( int fd ) |
| 227 | { |
| 228 | int on = 1; |
| 229 | adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | extern int adb_socketpair( int sv[2] ); |
| 233 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 234 | 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] | 235 | return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; |
| 236 | } |
| 237 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 238 | // Like strerror(), but for Win32 error codes. |
| 239 | std::string SystemErrorCodeToString(DWORD error_code); |
| 240 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 241 | // We later define a macro mapping 'stat' to 'adb_stat'. This causes: |
| 242 | // struct stat s; |
| 243 | // stat(filename, &s); |
| 244 | // To turn into the following: |
| 245 | // struct adb_stat s; |
| 246 | // adb_stat(filename, &s); |
| 247 | // To get this to work, we need to make 'struct adb_stat' the same as |
| 248 | // 'struct stat'. Note that this definition of 'struct adb_stat' uses the |
| 249 | // *current* macro definition of stat, so it may actually be inheriting from |
| 250 | // struct _stat32i64 (or some other remapping). |
| 251 | struct adb_stat : public stat {}; |
| 252 | |
| 253 | static_assert(sizeof(struct adb_stat) == sizeof(struct stat), |
| 254 | "structures should be the same"); |
| 255 | |
| 256 | extern int adb_stat(const char* f, struct adb_stat* s); |
| 257 | |
| 258 | // stat is already a macro, undefine it so we can redefine it. |
| 259 | #undef stat |
| 260 | #define stat adb_stat |
| 261 | |
| 262 | // UTF-8 versions of POSIX APIs. |
| 263 | extern DIR* adb_opendir(const char* dirname); |
| 264 | extern struct dirent* adb_readdir(DIR* dir); |
| 265 | extern int adb_closedir(DIR* dir); |
| 266 | |
| 267 | extern int adb_utime(const char *, struct utimbuf *); |
| 268 | extern int adb_chmod(const char *, int); |
| 269 | |
| 270 | extern int adb_vfprintf(FILE *stream, const char *format, va_list ap) |
| 271 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0))); |
| 272 | extern int adb_fprintf(FILE *stream, const char *format, ...) |
| 273 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))); |
| 274 | extern int adb_printf(const char *format, ...) |
| 275 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2))); |
| 276 | |
| 277 | extern int adb_fputs(const char* buf, FILE* stream); |
| 278 | extern int adb_fputc(int ch, FILE* stream); |
| 279 | extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, |
| 280 | FILE* stream); |
| 281 | |
| 282 | extern FILE* adb_fopen(const char* f, const char* m); |
| 283 | |
| 284 | extern char* adb_getenv(const char* name); |
| 285 | |
| 286 | extern char* adb_getcwd(char* buf, int size); |
| 287 | |
| 288 | // Remap calls to POSIX APIs to our UTF-8 versions. |
| 289 | #define opendir adb_opendir |
| 290 | #define readdir adb_readdir |
| 291 | #define closedir adb_closedir |
| 292 | #define rewinddir rewinddir_utf8_not_yet_implemented |
| 293 | #define telldir telldir_utf8_not_yet_implemented |
| 294 | #define seekdir seekdir_utf8_not_yet_implemented |
| 295 | |
| 296 | #define utime adb_utime |
| 297 | #define chmod adb_chmod |
| 298 | |
| 299 | #define vfprintf adb_vfprintf |
| 300 | #define fprintf adb_fprintf |
| 301 | #define printf adb_printf |
| 302 | #define fputs adb_fputs |
| 303 | #define fputc adb_fputc |
| 304 | #define fwrite adb_fwrite |
| 305 | |
| 306 | #define fopen adb_fopen |
| 307 | |
| 308 | #define getenv adb_getenv |
| 309 | #define putenv putenv_utf8_not_yet_implemented |
| 310 | #define setenv setenv_utf8_not_yet_implemented |
| 311 | #define unsetenv unsetenv_utf8_not_yet_implemented |
| 312 | |
| 313 | #define getcwd adb_getcwd |
| 314 | |
| 315 | // Convert from UTF-8 to UTF-16, typically used to convert char strings into |
| 316 | // wchar_t strings that can be passed to wchar_t-based OS and C Runtime APIs |
| 317 | // on Windows. |
| 318 | extern std::wstring widen(const std::string& utf8); |
| 319 | extern std::wstring widen(const char* utf8); |
| 320 | |
| 321 | // Convert from UTF-16 to UTF-8, typically used to convert strings from OS and |
| 322 | // C Runtime APIs that return wchar_t, to a format for our char-based data |
| 323 | // structures. |
| 324 | extern std::string narrow(const std::wstring& utf16); |
| 325 | extern std::string narrow(const wchar_t* utf16); |
| 326 | |
| 327 | // Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be |
| 328 | // passed to main(). |
| 329 | class NarrowArgs { |
| 330 | public: |
| 331 | NarrowArgs(int argc, wchar_t** argv); |
| 332 | ~NarrowArgs(); |
| 333 | |
| 334 | inline char** data() { |
| 335 | return narrow_args; |
| 336 | } |
| 337 | |
| 338 | private: |
| 339 | char** narrow_args; |
| 340 | }; |
| 341 | |
Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 342 | // Windows HANDLE values only use 32-bits of the type, even on 64-bit machines, |
| 343 | // so they can fit in an int. To convert back, we just need to sign-extend. |
| 344 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx |
| 345 | // Note that this does not make a HANDLE value work with APIs like open(), nor |
| 346 | // does this make a value from open() passable to APIs taking a HANDLE. This |
| 347 | // just lets you take a HANDLE, pass it around as an int, and then use it again |
| 348 | // as a HANDLE. |
| 349 | inline int cast_handle_to_int(const HANDLE h) { |
| 350 | // truncate |
| 351 | return static_cast<int>(reinterpret_cast<INT_PTR>(h)); |
| 352 | } |
| 353 | |
| 354 | inline HANDLE cast_int_to_handle(const int fd) { |
| 355 | // sign-extend |
| 356 | return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd)); |
| 357 | } |
| 358 | |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame^] | 359 | // Deleter for unique_handle. Adapted from many sources, including: |
| 360 | // http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api |
| 361 | // https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx |
| 362 | class handle_deleter { |
| 363 | public: |
| 364 | typedef HANDLE pointer; |
| 365 | |
| 366 | void operator()(HANDLE h); |
| 367 | }; |
| 368 | |
| 369 | // Like std::unique_ptr, but for Windows HANDLE objects that should be |
| 370 | // CloseHandle()'d. Operator bool() only checks if the handle != nullptr, |
| 371 | // but does not check if the handle != INVALID_HANDLE_VALUE. |
| 372 | typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle; |
| 373 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | #else /* !_WIN32 a.k.a. Unix */ |
| 375 | |
David 'Digit' Turner | 414ff7d | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 376 | #include "fdevent.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 377 | #include <cutils/misc.h> |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 378 | #include <cutils/sockets.h> |
Spencer Low | 8d8126a | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 379 | #include <cutils/threads.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 380 | #include <signal.h> |
| 381 | #include <sys/wait.h> |
| 382 | #include <sys/stat.h> |
| 383 | #include <fcntl.h> |
| 384 | |
| 385 | #include <pthread.h> |
| 386 | #include <unistd.h> |
| 387 | #include <fcntl.h> |
| 388 | #include <stdarg.h> |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 389 | #include <netdb.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 390 | #include <netinet/in.h> |
| 391 | #include <netinet/tcp.h> |
| 392 | #include <string.h> |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 393 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 394 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 395 | #include <string> |
| 396 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 397 | #define OS_PATH_SEPARATORS "/" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | #define OS_PATH_SEPARATOR '/' |
| 399 | #define OS_PATH_SEPARATOR_STR "/" |
Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 400 | #define ENV_PATH_SEPARATOR_STR ":" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | |
| 402 | typedef pthread_mutex_t adb_mutex_t; |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 403 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 404 | #define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
| 405 | #define adb_mutex_init pthread_mutex_init |
| 406 | #define adb_mutex_lock pthread_mutex_lock |
| 407 | #define adb_mutex_unlock pthread_mutex_unlock |
| 408 | #define adb_mutex_destroy pthread_mutex_destroy |
| 409 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 410 | #define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 411 | |
| 412 | #define adb_cond_t pthread_cond_t |
| 413 | #define adb_cond_init pthread_cond_init |
| 414 | #define adb_cond_wait pthread_cond_wait |
| 415 | #define adb_cond_broadcast pthread_cond_broadcast |
| 416 | #define adb_cond_signal pthread_cond_signal |
| 417 | #define adb_cond_destroy pthread_cond_destroy |
| 418 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 419 | /* declare all mutexes */ |
| 420 | #define ADB_MUTEX(x) extern adb_mutex_t x; |
| 421 | #include "mutex_list.h" |
| 422 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | static __inline__ void close_on_exec(int fd) |
| 424 | { |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 425 | fcntl( fd, F_SETFD, FD_CLOEXEC ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 428 | // Open a file and return a file descriptor that may be used with unix_read(), |
| 429 | // unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close(). |
| 430 | // |
| 431 | // On Unix, this is based on open(), so the file descriptor is a real OS file |
| 432 | // descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a |
| 433 | // file descriptor that can only be used with C Runtime APIs (which are wrapped |
| 434 | // by unix_read(), unix_write(), unix_close()). Also, the C Runtime has |
| 435 | // configurable CR/LF translation which defaults to text mode, but is settable |
| 436 | // with _setmode(). |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 | static __inline__ int unix_open(const char* path, int options,...) |
| 438 | { |
| 439 | if ((options & O_CREAT) == 0) |
| 440 | { |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 441 | return TEMP_FAILURE_RETRY( open(path, options) ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 442 | } |
| 443 | else |
| 444 | { |
| 445 | int mode; |
| 446 | va_list args; |
| 447 | va_start( args, options ); |
| 448 | mode = va_arg( args, int ); |
| 449 | va_end( args ); |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 450 | return TEMP_FAILURE_RETRY( open( path, options, mode ) ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 454 | // Similar to the two-argument adb_open(), but takes a mode parameter for file |
| 455 | // creation. See adb_open() for more info. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 456 | static __inline__ int adb_open_mode( const char* pathname, int options, int mode ) |
| 457 | { |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 458 | return TEMP_FAILURE_RETRY( open( pathname, options, mode ) ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 462 | // Open a file and return a file descriptor that may be used with adb_read(), |
| 463 | // adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close(). |
| 464 | // |
| 465 | // On Unix, this is based on open(), but the Windows implementation (in |
| 466 | // sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime |
| 467 | // and its CR/LF translation. The returned file descriptor should be used with |
| 468 | // adb_read(), adb_write(), adb_close(), etc. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | static __inline__ int adb_open( const char* pathname, int options ) |
| 470 | { |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 471 | int fd = TEMP_FAILURE_RETRY( open( pathname, options ) ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | if (fd < 0) |
| 473 | return -1; |
| 474 | close_on_exec( fd ); |
| 475 | return fd; |
| 476 | } |
| 477 | #undef open |
| 478 | #define open ___xxx_open |
| 479 | |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 480 | static __inline__ int adb_shutdown(int fd) |
| 481 | { |
| 482 | return shutdown(fd, SHUT_RDWR); |
| 483 | } |
| 484 | #undef shutdown |
| 485 | #define shutdown ____xxx_shutdown |
| 486 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 487 | // Closes a file descriptor that came from adb_open() or adb_open_mode(), but |
| 488 | // not designed to take a file descriptor from unix_open(). See the comments |
| 489 | // for adb_open() for more info. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | static __inline__ int adb_close(int fd) |
| 491 | { |
| 492 | return close(fd); |
| 493 | } |
| 494 | #undef close |
| 495 | #define close ____xxx_close |
| 496 | |
| 497 | |
| 498 | static __inline__ int adb_read(int fd, void* buf, size_t len) |
| 499 | { |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 500 | return TEMP_FAILURE_RETRY( read( fd, buf, len ) ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | #undef read |
| 504 | #define read ___xxx_read |
| 505 | |
| 506 | static __inline__ int adb_write(int fd, const void* buf, size_t len) |
| 507 | { |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 508 | return TEMP_FAILURE_RETRY( write( fd, buf, len ) ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 509 | } |
| 510 | #undef write |
| 511 | #define write ___xxx_write |
| 512 | |
| 513 | static __inline__ int adb_lseek(int fd, int pos, int where) |
| 514 | { |
| 515 | return lseek(fd, pos, where); |
| 516 | } |
| 517 | #undef lseek |
| 518 | #define lseek ___xxx_lseek |
| 519 | |
| 520 | static __inline__ int adb_unlink(const char* path) |
| 521 | { |
| 522 | return unlink(path); |
| 523 | } |
| 524 | #undef unlink |
| 525 | #define unlink ___xxx_unlink |
| 526 | |
| 527 | static __inline__ int adb_creat(const char* path, int mode) |
| 528 | { |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 529 | int fd = TEMP_FAILURE_RETRY( creat( path, mode ) ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 531 | if ( fd < 0 ) |
| 532 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 533 | |
| 534 | close_on_exec(fd); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 535 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 536 | } |
| 537 | #undef creat |
| 538 | #define creat ___xxx_creat |
| 539 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 540 | // Helper for network_* functions. |
| 541 | inline int _fd_set_error_str(int fd, std::string* error) { |
| 542 | if (fd == -1) { |
| 543 | *error = strerror(errno); |
| 544 | } |
| 545 | return fd; |
| 546 | } |
| 547 | |
| 548 | inline int network_loopback_client(int port, int type, std::string* error) { |
| 549 | return _fd_set_error_str(socket_loopback_client(port, type), error); |
| 550 | } |
| 551 | |
| 552 | inline int network_loopback_server(int port, int type, std::string* error) { |
| 553 | return _fd_set_error_str(socket_loopback_server(port, type), error); |
| 554 | } |
| 555 | |
| 556 | inline int network_inaddr_any_server(int port, int type, std::string* error) { |
| 557 | return _fd_set_error_str(socket_inaddr_any_server(port, type), error); |
| 558 | } |
| 559 | |
| 560 | inline int network_local_server(const char *name, int namespace_id, int type, |
| 561 | std::string* error) { |
| 562 | return _fd_set_error_str(socket_local_server(name, namespace_id, type), |
| 563 | error); |
| 564 | } |
| 565 | |
| 566 | inline int network_connect(const std::string& host, int port, int type, |
| 567 | int timeout, std::string* error) { |
| 568 | int getaddrinfo_error = 0; |
| 569 | int fd = socket_network_client_timeout(host.c_str(), port, type, timeout, |
| 570 | &getaddrinfo_error); |
| 571 | if (fd != -1) { |
| 572 | return fd; |
| 573 | } |
| 574 | if (getaddrinfo_error != 0) { |
| 575 | *error = gai_strerror(getaddrinfo_error); |
| 576 | } else { |
| 577 | *error = strerror(errno); |
| 578 | } |
| 579 | return -1; |
| 580 | } |
| 581 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 582 | static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen) |
| 583 | { |
Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 584 | int fd; |
| 585 | |
Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 586 | fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) ); |
Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 587 | if (fd >= 0) |
| 588 | close_on_exec(fd); |
| 589 | |
| 590 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | #undef accept |
| 594 | #define accept ___xxx_accept |
| 595 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 596 | // Operate on a file descriptor returned from unix_open() or a well-known file |
| 597 | // descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO. |
| 598 | // |
| 599 | // On Unix, unix_read(), unix_write(), unix_close() map to adb_read(), |
| 600 | // adb_write(), adb_close() (which all map to Unix system calls), but the |
| 601 | // Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call |
| 602 | // into the C Runtime and its configurable CR/LF translation (which is settable |
| 603 | // via _setmode()). |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 604 | #define unix_read adb_read |
| 605 | #define unix_write adb_write |
| 606 | #define unix_close adb_close |
| 607 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 608 | typedef void* (*adb_thread_func_t)( void* arg ); |
| 609 | |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 610 | static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) { |
| 611 | pthread_attr_t attr; |
| 612 | pthread_attr_init(&attr); |
| 613 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 614 | |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 615 | pthread_t thread; |
| 616 | errno = pthread_create(&thread, &attr, start, arg); |
| 617 | return (errno == 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) |
| 621 | { |
| 622 | int opt = bufsize; |
| 623 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); |
| 624 | } |
| 625 | |
| 626 | static __inline__ void disable_tcp_nagle(int fd) |
| 627 | { |
| 628 | int on = 1; |
| 629 | setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) ); |
| 630 | } |
| 631 | |
Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 632 | static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen ) |
| 633 | { |
| 634 | return setsockopt( fd, level, optname, optval, optlen ); |
| 635 | } |
| 636 | |
| 637 | #undef setsockopt |
| 638 | #define setsockopt ___xxx_setsockopt |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 639 | |
| 640 | static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] ) |
| 641 | { |
| 642 | return socketpair( d, type, protocol, sv ); |
| 643 | } |
| 644 | |
| 645 | static __inline__ int adb_socketpair( int sv[2] ) |
| 646 | { |
| 647 | int rc; |
| 648 | |
| 649 | rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); |
| 650 | if (rc < 0) |
| 651 | return -1; |
| 652 | |
| 653 | close_on_exec( sv[0] ); |
| 654 | close_on_exec( sv[1] ); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | #undef socketpair |
| 659 | #define socketpair ___xxx_socketpair |
| 660 | |
| 661 | static __inline__ void adb_sleep_ms( int mseconds ) |
| 662 | { |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 663 | usleep( mseconds*1000 ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 666 | 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] | 667 | { |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 668 | return mkdir(path.c_str(), mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 669 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 670 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 671 | #undef mkdir |
| 672 | #define mkdir ___xxx_mkdir |
| 673 | |
| 674 | static __inline__ void adb_sysdeps_init(void) |
| 675 | { |
| 676 | } |
| 677 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 678 | 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] | 679 | return path[0] == '/'; |
| 680 | } |
| 681 | |
leozwang | cbf0267 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 682 | static __inline__ unsigned long adb_thread_id() |
| 683 | { |
Spencer Low | 8d8126a | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 684 | return (unsigned long)gettid(); |
leozwang | cbf0267 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 685 | } |
| 686 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 687 | #endif /* !_WIN32 */ |
| 688 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 689 | #endif /* _ADB_SYSDEPS_H */ |