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