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