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