| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | /* this file contains system-dependent definitions used by ADB | 
|  | 18 | * they're related to threads, sockets and file descriptors | 
|  | 19 | */ | 
|  | 20 | #ifndef _ADB_SYSDEPS_H | 
|  | 21 | #define _ADB_SYSDEPS_H | 
|  | 22 |  | 
|  | 23 | #ifdef __CYGWIN__ | 
|  | 24 | #  undef _WIN32 | 
|  | 25 | #endif | 
|  | 26 |  | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 27 | #include <errno.h> | 
|  | 28 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 29 | #include <string> | 
| Spencer Low | a30b79a | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 30 | #include <vector> | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 31 |  | 
| Josh Gao | 13ea01d | 2016-05-13 14:52:06 -0700 | [diff] [blame] | 32 | // Include this before open/close/unlink are defined as macros below. | 
| Josh Gao | 3b3e10d | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 33 | #include <android-base/errors.h> | 
| Elliott Hughes | 3df2fa6 | 2017-11-28 18:05:27 -0800 | [diff] [blame] | 34 | #include <android-base/macros.h> | 
| Josh Gao | 13ea01d | 2016-05-13 14:52:06 -0700 | [diff] [blame] | 35 | #include <android-base/unique_fd.h> | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include <android-base/utf8.h> | 
| Spencer Low | d21dc82 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 37 |  | 
| Josh Gao | a3577e1 | 2016-12-05 13:24:48 -0800 | [diff] [blame] | 38 | #include "sysdeps/errno.h" | 
| Josh Gao | 46de1d7 | 2017-04-12 13:57:06 -0700 | [diff] [blame] | 39 | #include "sysdeps/network.h" | 
| Josh Gao | f551ea0 | 2016-07-28 18:09:48 -0700 | [diff] [blame] | 40 | #include "sysdeps/stat.h" | 
|  | 41 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 42 | // Some printf-like functions are implemented in terms of | 
|  | 43 | // android::base::StringAppendV, so they should use the same attribute for | 
|  | 44 | // compile-time format string checking. On Windows, if the mingw version of | 
|  | 45 | // vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd | 
|  | 46 | // and PRIu64 (and related) to be recognized by the compile-time checking. | 
|  | 47 | #define ADB_FORMAT_ARCHETYPE __printf__ | 
|  | 48 | #ifdef __USE_MINGW_ANSI_STDIO | 
|  | 49 | #if __USE_MINGW_ANSI_STDIO | 
|  | 50 | #undef ADB_FORMAT_ARCHETYPE | 
|  | 51 | #define ADB_FORMAT_ARCHETYPE gnu_printf | 
|  | 52 | #endif | 
|  | 53 | #endif | 
|  | 54 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | #ifdef _WIN32 | 
|  | 56 |  | 
| Josh Gao | a166e71 | 2016-01-29 12:08:34 -0800 | [diff] [blame] | 57 | // Clang-only nullability specifiers | 
|  | 58 | #define _Nonnull | 
|  | 59 | #define _Nullable | 
|  | 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 | d21dc82 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 75 | #include <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 |  | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 88 | static __inline__ int adb_thread_setname(const std::string& name) { | 
|  | 89 | // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set | 
|  | 90 | // the thread name in Windows. Unfortunately, it only works during debugging, but | 
|  | 91 | // our build process doesn't generate PDB files needed for debugging. | 
|  | 92 | return 0; | 
|  | 93 | } | 
|  | 94 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | static __inline__ void  close_on_exec(int  fd) | 
|  | 96 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 97 | /* nothing really */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 100 | extern int  adb_unlink(const char*  path); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | #undef  unlink | 
|  | 102 | #define unlink  ___xxx_unlink | 
|  | 103 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 104 | 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] | 105 | #undef   mkdir | 
|  | 106 | #define  mkdir  ___xxx_mkdir | 
|  | 107 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 108 | // See the comments for the !defined(_WIN32) versions of adb_*(). | 
| Josh Gao | 2e1e789 | 2018-03-23 13:03:28 -0700 | [diff] [blame] | 109 | extern int adb_open(const char* path, int options); | 
|  | 110 | extern int adb_creat(const char* path, int mode); | 
|  | 111 | extern int adb_read(int fd, void* buf, int len); | 
|  | 112 | extern int adb_write(int fd, const void* buf, int len); | 
|  | 113 | extern int adb_lseek(int fd, int pos, int where); | 
|  | 114 | extern int adb_shutdown(int fd, int direction = SHUT_RDWR); | 
|  | 115 | extern int adb_close(int fd); | 
|  | 116 | extern int adb_register_socket(SOCKET s); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 117 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 118 | // 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] | 119 | static __inline__ int  unix_close(int fd) | 
|  | 120 | { | 
|  | 121 | return close(fd); | 
|  | 122 | } | 
|  | 123 | #undef   close | 
|  | 124 | #define  close   ____xxx_close | 
|  | 125 |  | 
| Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 126 | // Like unix_read(), but may return EINTR. | 
|  | 127 | extern int  unix_read_interruptible(int  fd, void*  buf, size_t  len); | 
|  | 128 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 129 | // See the comments for the !defined(_WIN32) version of unix_read(). | 
| Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 130 | static __inline__ int unix_read(int fd, void* buf, size_t len) { | 
|  | 131 | return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len)); | 
|  | 132 | } | 
| Spencer Low | 5018406 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 133 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | #undef   read | 
|  | 135 | #define  read  ___xxx_read | 
|  | 136 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 137 | // 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] | 138 | static __inline__  int  unix_write(int  fd, const void*  buf, size_t  len) | 
|  | 139 | { | 
|  | 140 | return write(fd, buf, len); | 
|  | 141 | } | 
|  | 142 | #undef   write | 
|  | 143 | #define  write  ___xxx_write | 
|  | 144 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 145 | // 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] | 146 | static __inline__ int  adb_open_mode(const char* path, int options, int mode) | 
|  | 147 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 148 | return adb_open(path, options); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 151 | // See the comments for the !defined(_WIN32) version of unix_open(). | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 152 | extern int unix_open(const char* path, int options, ...); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | #define  open    ___xxx_unix_open | 
|  | 154 |  | 
| David Pursell | c5b8ad8 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 155 | // Checks if |fd| corresponds to a console. | 
|  | 156 | // Standard Windows isatty() returns 1 for both console FDs and character | 
|  | 157 | // devices like NUL. unix_isatty() performs some extra checking to only match | 
|  | 158 | // console FDs. | 
|  | 159 | // |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs | 
|  | 160 | // will work but adb_open() FDs will not. Additionally the OS handle associated | 
|  | 161 | // with |fd| must have GENERIC_READ access (which console FDs have by default). | 
|  | 162 | // Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after | 
|  | 163 | // calling this function is unreliable and should not be used. | 
|  | 164 | int unix_isatty(int fd); | 
|  | 165 | #define  isatty  ___xxx_isatty | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 167 | int network_inaddr_any_server(int port, int type, std::string* error); | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) { | 
|  | 170 | abort(); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) { | 
|  | 174 | abort(); | 
|  | 175 | } | 
|  | 176 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 177 | int network_connect(const std::string& host, int port, int type, int timeout, | 
|  | 178 | std::string* error); | 
|  | 179 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | extern int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen); | 
|  | 181 |  | 
|  | 182 | #undef   accept | 
|  | 183 | #define  accept  ___xxx_accept | 
|  | 184 |  | 
| David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 185 | // Returns the local port number of a bound socket, or -1 on failure. | 
|  | 186 | int adb_socket_get_local_port(int fd); | 
|  | 187 |  | 
| Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 188 | extern int  adb_setsockopt(int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen); | 
|  | 189 |  | 
|  | 190 | #undef   setsockopt | 
|  | 191 | #define  setsockopt  ___xxx_setsockopt | 
|  | 192 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | extern int  adb_socketpair( int  sv[2] ); | 
|  | 194 |  | 
| Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 195 | struct adb_pollfd { | 
|  | 196 | int fd; | 
|  | 197 | short events; | 
|  | 198 | short revents; | 
|  | 199 | }; | 
|  | 200 | extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout); | 
|  | 201 | #define poll ___xxx_poll | 
|  | 202 |  | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 203 | 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] | 204 | return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; | 
|  | 205 | } | 
|  | 206 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 207 | // UTF-8 versions of POSIX APIs. | 
|  | 208 | extern DIR* adb_opendir(const char* dirname); | 
|  | 209 | extern struct dirent* adb_readdir(DIR* dir); | 
|  | 210 | extern int adb_closedir(DIR* dir); | 
|  | 211 |  | 
|  | 212 | extern int adb_utime(const char *, struct utimbuf *); | 
|  | 213 | extern int adb_chmod(const char *, int); | 
|  | 214 |  | 
|  | 215 | extern int adb_vfprintf(FILE *stream, const char *format, va_list ap) | 
|  | 216 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0))); | 
| Spencer Low | a30b79a | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 217 | extern int adb_vprintf(const char *format, va_list ap) | 
|  | 218 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0))); | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 219 | extern int adb_fprintf(FILE *stream, const char *format, ...) | 
|  | 220 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))); | 
|  | 221 | extern int adb_printf(const char *format, ...) | 
|  | 222 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2))); | 
|  | 223 |  | 
|  | 224 | extern int adb_fputs(const char* buf, FILE* stream); | 
|  | 225 | extern int adb_fputc(int ch, FILE* stream); | 
| Spencer Low | a30b79a | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 226 | extern int adb_putchar(int ch); | 
|  | 227 | extern int adb_puts(const char* buf); | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 228 | extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, | 
|  | 229 | FILE* stream); | 
|  | 230 |  | 
|  | 231 | extern FILE* adb_fopen(const char* f, const char* m); | 
|  | 232 |  | 
|  | 233 | extern char* adb_getenv(const char* name); | 
|  | 234 |  | 
|  | 235 | extern char* adb_getcwd(char* buf, int size); | 
|  | 236 |  | 
|  | 237 | // Remap calls to POSIX APIs to our UTF-8 versions. | 
|  | 238 | #define opendir adb_opendir | 
|  | 239 | #define readdir adb_readdir | 
|  | 240 | #define closedir adb_closedir | 
|  | 241 | #define rewinddir rewinddir_utf8_not_yet_implemented | 
|  | 242 | #define telldir telldir_utf8_not_yet_implemented | 
| Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 243 | // Some compiler's C++ headers have members named seekdir, so we can't do the | 
|  | 244 | // macro technique and instead cause a link error if seekdir is called. | 
|  | 245 | inline void seekdir(DIR*, long) { | 
|  | 246 | extern int seekdir_utf8_not_yet_implemented; | 
|  | 247 | seekdir_utf8_not_yet_implemented = 1; | 
|  | 248 | } | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 249 |  | 
|  | 250 | #define utime adb_utime | 
|  | 251 | #define chmod adb_chmod | 
|  | 252 |  | 
|  | 253 | #define vfprintf adb_vfprintf | 
| Spencer Low | a30b79a | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 254 | #define vprintf adb_vprintf | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 255 | #define fprintf adb_fprintf | 
|  | 256 | #define printf adb_printf | 
|  | 257 | #define fputs adb_fputs | 
|  | 258 | #define fputc adb_fputc | 
| Spencer Low | a30b79a | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 259 | // putc may be a macro, so if so, undefine it, so that we can redefine it. | 
|  | 260 | #undef putc | 
|  | 261 | #define putc(c, s) adb_fputc(c, s) | 
|  | 262 | #define putchar adb_putchar | 
|  | 263 | #define puts adb_puts | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 264 | #define fwrite adb_fwrite | 
|  | 265 |  | 
|  | 266 | #define fopen adb_fopen | 
| Spencer Low | a30b79a | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 267 | #define freopen freopen_utf8_not_yet_implemented | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 268 |  | 
|  | 269 | #define getenv adb_getenv | 
|  | 270 | #define putenv putenv_utf8_not_yet_implemented | 
|  | 271 | #define setenv setenv_utf8_not_yet_implemented | 
|  | 272 | #define unsetenv unsetenv_utf8_not_yet_implemented | 
|  | 273 |  | 
|  | 274 | #define getcwd adb_getcwd | 
|  | 275 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 276 | // Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be | 
|  | 277 | // passed to main(). | 
|  | 278 | class NarrowArgs { | 
|  | 279 | public: | 
|  | 280 | NarrowArgs(int argc, wchar_t** argv); | 
|  | 281 | ~NarrowArgs(); | 
|  | 282 |  | 
|  | 283 | inline char** data() { | 
|  | 284 | return narrow_args; | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | private: | 
|  | 288 | char** narrow_args; | 
|  | 289 | }; | 
|  | 290 |  | 
| Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 291 | // Windows HANDLE values only use 32-bits of the type, even on 64-bit machines, | 
|  | 292 | // so they can fit in an int. To convert back, we just need to sign-extend. | 
|  | 293 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx | 
|  | 294 | // Note that this does not make a HANDLE value work with APIs like open(), nor | 
|  | 295 | // does this make a value from open() passable to APIs taking a HANDLE. This | 
|  | 296 | // just lets you take a HANDLE, pass it around as an int, and then use it again | 
|  | 297 | // as a HANDLE. | 
|  | 298 | inline int cast_handle_to_int(const HANDLE h) { | 
|  | 299 | // truncate | 
|  | 300 | return static_cast<int>(reinterpret_cast<INT_PTR>(h)); | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | inline HANDLE cast_int_to_handle(const int fd) { | 
|  | 304 | // sign-extend | 
|  | 305 | return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd)); | 
|  | 306 | } | 
|  | 307 |  | 
| Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 308 | // Deleter for unique_handle. Adapted from many sources, including: | 
|  | 309 | // http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api | 
|  | 310 | // https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx | 
|  | 311 | class handle_deleter { | 
|  | 312 | public: | 
|  | 313 | typedef HANDLE pointer; | 
|  | 314 |  | 
|  | 315 | void operator()(HANDLE h); | 
|  | 316 | }; | 
|  | 317 |  | 
|  | 318 | // Like std::unique_ptr, but for Windows HANDLE objects that should be | 
|  | 319 | // CloseHandle()'d. Operator bool() only checks if the handle != nullptr, | 
|  | 320 | // but does not check if the handle != INVALID_HANDLE_VALUE. | 
|  | 321 | typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle; | 
|  | 322 |  | 
| Spencer Low | a30b79a | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 323 | namespace internal { | 
|  | 324 |  | 
|  | 325 | size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes); | 
|  | 326 |  | 
|  | 327 | } | 
|  | 328 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | #else /* !_WIN32 a.k.a. Unix */ | 
|  | 330 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 331 | #include <cutils/sockets.h> | 
| Spencer Low | 8d8126a | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 332 | #include <cutils/threads.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | #include <fcntl.h> | 
| Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 334 | #include <poll.h> | 
|  | 335 | #include <signal.h> | 
|  | 336 | #include <sys/stat.h> | 
|  | 337 | #include <sys/wait.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 |  | 
|  | 339 | #include <pthread.h> | 
|  | 340 | #include <unistd.h> | 
|  | 341 | #include <fcntl.h> | 
|  | 342 | #include <stdarg.h> | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 343 | #include <netdb.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | #include <netinet/in.h> | 
|  | 345 | #include <netinet/tcp.h> | 
|  | 346 | #include <string.h> | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 347 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 349 | #include <string> | 
|  | 350 |  | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 351 | #define OS_PATH_SEPARATORS "/" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 352 | #define OS_PATH_SEPARATOR '/' | 
|  | 353 | #define OS_PATH_SEPARATOR_STR "/" | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 354 | #define ENV_PATH_SEPARATOR_STR ":" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 |  | 
| Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 356 | static __inline__ bool adb_is_separator(char c) { | 
|  | 357 | return c == '/'; | 
|  | 358 | } | 
|  | 359 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 360 | static __inline__ void  close_on_exec(int  fd) | 
|  | 361 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 362 | fcntl( fd, F_SETFD, FD_CLOEXEC ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | } | 
|  | 364 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 365 | // Open a file and return a file descriptor that may be used with unix_read(), | 
|  | 366 | // unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close(). | 
|  | 367 | // | 
|  | 368 | // On Unix, this is based on open(), so the file descriptor is a real OS file | 
|  | 369 | // descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a | 
|  | 370 | // file descriptor that can only be used with C Runtime APIs (which are wrapped | 
|  | 371 | // by unix_read(), unix_write(), unix_close()). Also, the C Runtime has | 
|  | 372 | // configurable CR/LF translation which defaults to text mode, but is settable | 
|  | 373 | // with _setmode(). | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | static __inline__ int  unix_open(const char*  path, int options,...) | 
|  | 375 | { | 
|  | 376 | if ((options & O_CREAT) == 0) | 
|  | 377 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 378 | return  TEMP_FAILURE_RETRY( open(path, options) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | } | 
|  | 380 | else | 
|  | 381 | { | 
|  | 382 | int      mode; | 
|  | 383 | va_list  args; | 
|  | 384 | va_start( args, options ); | 
|  | 385 | mode = va_arg( args, int ); | 
|  | 386 | va_end( args ); | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 387 | return TEMP_FAILURE_RETRY( open( path, options, mode ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 388 | } | 
|  | 389 | } | 
|  | 390 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 391 | // Similar to the two-argument adb_open(), but takes a mode parameter for file | 
|  | 392 | // creation. See adb_open() for more info. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 393 | static __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode ) | 
|  | 394 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 395 | return TEMP_FAILURE_RETRY( open( pathname, options, mode ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
|  | 398 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 399 | // Open a file and return a file descriptor that may be used with adb_read(), | 
|  | 400 | // adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close(). | 
|  | 401 | // | 
|  | 402 | // On Unix, this is based on open(), but the Windows implementation (in | 
|  | 403 | // sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime | 
|  | 404 | // and its CR/LF translation. The returned file descriptor should be used with | 
|  | 405 | // adb_read(), adb_write(), adb_close(), etc. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 406 | static __inline__ int  adb_open( const char*  pathname, int  options ) | 
|  | 407 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 408 | int  fd = TEMP_FAILURE_RETRY( open( pathname, options ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | if (fd < 0) | 
|  | 410 | return -1; | 
|  | 411 | close_on_exec( fd ); | 
|  | 412 | return fd; | 
|  | 413 | } | 
|  | 414 | #undef   open | 
|  | 415 | #define  open    ___xxx_open | 
|  | 416 |  | 
| Josh Gao | 2e1e789 | 2018-03-23 13:03:28 -0700 | [diff] [blame] | 417 | static __inline__ int adb_shutdown(int fd, int direction = SHUT_RDWR) { | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 418 | return shutdown(fd, direction); | 
|  | 419 | } | 
| Josh Gao | 2e1e789 | 2018-03-23 13:03:28 -0700 | [diff] [blame] | 420 |  | 
| Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 421 | #undef   shutdown | 
|  | 422 | #define  shutdown   ____xxx_shutdown | 
|  | 423 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 424 | // Closes a file descriptor that came from adb_open() or adb_open_mode(), but | 
|  | 425 | // not designed to take a file descriptor from unix_open(). See the comments | 
|  | 426 | // for adb_open() for more info. | 
| Josh Gao | f0d3b4f | 2016-03-04 15:15:56 -0800 | [diff] [blame] | 427 | __inline__ int adb_close(int fd) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 428 | return close(fd); | 
|  | 429 | } | 
|  | 430 | #undef   close | 
|  | 431 | #define  close   ____xxx_close | 
|  | 432 |  | 
| Casey Dahlin | 2fe9b60 | 2016-09-21 14:03:39 -0700 | [diff] [blame] | 433 | // On Windows, ADB has an indirection layer for file descriptors. If we get a | 
|  | 434 | // Win32 SOCKET object from an external library, we have to map it in to that | 
|  | 435 | // indirection layer, which this does. | 
|  | 436 | __inline__ int  adb_register_socket(int s) { | 
|  | 437 | return s; | 
|  | 438 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 |  | 
|  | 440 | static __inline__  int  adb_read(int  fd, void*  buf, size_t  len) | 
|  | 441 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 442 | return TEMP_FAILURE_RETRY( read( fd, buf, len ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 443 | } | 
|  | 444 |  | 
| Spencer Low | 2e02dc6 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 445 | // Like unix_read(), but does not handle EINTR. | 
|  | 446 | static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) { | 
|  | 447 | return read(fd, buf, len); | 
|  | 448 | } | 
|  | 449 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 | #undef   read | 
|  | 451 | #define  read  ___xxx_read | 
|  | 452 |  | 
|  | 453 | static __inline__  int  adb_write(int  fd, const void*  buf, size_t  len) | 
|  | 454 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 455 | return TEMP_FAILURE_RETRY( write( fd, buf, len ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 456 | } | 
|  | 457 | #undef   write | 
|  | 458 | #define  write  ___xxx_write | 
|  | 459 |  | 
|  | 460 | static __inline__ int   adb_lseek(int  fd, int  pos, int  where) | 
|  | 461 | { | 
|  | 462 | return lseek(fd, pos, where); | 
|  | 463 | } | 
|  | 464 | #undef   lseek | 
|  | 465 | #define  lseek   ___xxx_lseek | 
|  | 466 |  | 
|  | 467 | static __inline__  int    adb_unlink(const char*  path) | 
|  | 468 | { | 
|  | 469 | return  unlink(path); | 
|  | 470 | } | 
|  | 471 | #undef  unlink | 
|  | 472 | #define unlink  ___xxx_unlink | 
|  | 473 |  | 
|  | 474 | static __inline__  int  adb_creat(const char*  path, int  mode) | 
|  | 475 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 476 | int  fd = TEMP_FAILURE_RETRY( creat( path, mode ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 477 |  | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 478 | if ( fd < 0 ) | 
|  | 479 | return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 480 |  | 
|  | 481 | close_on_exec(fd); | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 482 | return fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 483 | } | 
|  | 484 | #undef   creat | 
|  | 485 | #define  creat  ___xxx_creat | 
|  | 486 |  | 
| David Pursell | c5b8ad8 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 487 | static __inline__ int unix_isatty(int fd) { | 
|  | 488 | return isatty(fd); | 
|  | 489 | } | 
|  | 490 | #define  isatty  ___xxx_isatty | 
|  | 491 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 492 | // Helper for network_* functions. | 
|  | 493 | inline int _fd_set_error_str(int fd, std::string* error) { | 
|  | 494 | if (fd == -1) { | 
|  | 495 | *error = strerror(errno); | 
|  | 496 | } | 
|  | 497 | return fd; | 
|  | 498 | } | 
|  | 499 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 500 | inline int network_inaddr_any_server(int port, int type, std::string* error) { | 
|  | 501 | return _fd_set_error_str(socket_inaddr_any_server(port, type), error); | 
|  | 502 | } | 
|  | 503 |  | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 504 | inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) { | 
|  | 505 | return _fd_set_error_str(socket_local_client(name, namespace_id, type), error); | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) { | 
|  | 509 | return _fd_set_error_str(socket_local_server(name, namespace_id, type), error); | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 510 | } | 
|  | 511 |  | 
|  | 512 | inline int network_connect(const std::string& host, int port, int type, | 
|  | 513 | int timeout, std::string* error) { | 
|  | 514 | int getaddrinfo_error = 0; | 
|  | 515 | int fd = socket_network_client_timeout(host.c_str(), port, type, timeout, | 
|  | 516 | &getaddrinfo_error); | 
|  | 517 | if (fd != -1) { | 
|  | 518 | return fd; | 
|  | 519 | } | 
|  | 520 | if (getaddrinfo_error != 0) { | 
|  | 521 | *error = gai_strerror(getaddrinfo_error); | 
|  | 522 | } else { | 
|  | 523 | *error = strerror(errno); | 
|  | 524 | } | 
|  | 525 | return -1; | 
|  | 526 | } | 
|  | 527 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 528 | static __inline__ int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen) | 
|  | 529 | { | 
| Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 530 | int fd; | 
|  | 531 |  | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 532 | fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) ); | 
| Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 533 | if (fd >= 0) | 
|  | 534 | close_on_exec(fd); | 
|  | 535 |  | 
|  | 536 | return fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | } | 
|  | 538 |  | 
|  | 539 | #undef   accept | 
|  | 540 | #define  accept  ___xxx_accept | 
|  | 541 |  | 
| David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 542 | inline int adb_socket_get_local_port(int fd) { | 
|  | 543 | return socket_get_local_port(fd); | 
|  | 544 | } | 
|  | 545 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 546 | // Operate on a file descriptor returned from unix_open() or a well-known file | 
|  | 547 | // descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO. | 
|  | 548 | // | 
|  | 549 | // On Unix, unix_read(), unix_write(), unix_close() map to adb_read(), | 
|  | 550 | // adb_write(), adb_close() (which all map to Unix system calls), but the | 
|  | 551 | // Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call | 
|  | 552 | // into the C Runtime and its configurable CR/LF translation (which is settable | 
|  | 553 | // via _setmode()). | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 554 | #define  unix_read   adb_read | 
|  | 555 | #define  unix_write  adb_write | 
|  | 556 | #define  unix_close  adb_close | 
|  | 557 |  | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 558 | static __inline__ int adb_thread_setname(const std::string& name) { | 
|  | 559 | #ifdef __APPLE__ | 
|  | 560 | return pthread_setname_np(name.c_str()); | 
|  | 561 | #else | 
| Elliott Hughes | 7462f18 | 2017-08-02 13:22:38 -0700 | [diff] [blame] | 562 | // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings. | 
|  | 563 | // glibc doesn't have strlcpy, so we have to fake it. | 
|  | 564 | char buf[16];  // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers. | 
|  | 565 | strncpy(buf, name.c_str(), sizeof(buf) - 1); | 
|  | 566 | buf[sizeof(buf) - 1] = '\0'; | 
|  | 567 | return pthread_setname_np(pthread_self(), buf); | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 568 | #endif | 
|  | 569 | } | 
|  | 570 |  | 
| Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 571 | static __inline__ int  adb_setsockopt( int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen ) | 
|  | 572 | { | 
|  | 573 | return setsockopt( fd, level, optname, optval, optlen ); | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | #undef   setsockopt | 
|  | 577 | #define  setsockopt  ___xxx_setsockopt | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 578 |  | 
|  | 579 | static __inline__ int  unix_socketpair( int  d, int  type, int  protocol, int sv[2] ) | 
|  | 580 | { | 
|  | 581 | return socketpair( d, type, protocol, sv ); | 
|  | 582 | } | 
|  | 583 |  | 
|  | 584 | static __inline__ int  adb_socketpair( int  sv[2] ) | 
|  | 585 | { | 
|  | 586 | int  rc; | 
|  | 587 |  | 
|  | 588 | rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); | 
|  | 589 | if (rc < 0) | 
|  | 590 | return -1; | 
|  | 591 |  | 
|  | 592 | close_on_exec( sv[0] ); | 
|  | 593 | close_on_exec( sv[1] ); | 
|  | 594 | return 0; | 
|  | 595 | } | 
|  | 596 |  | 
|  | 597 | #undef   socketpair | 
|  | 598 | #define  socketpair   ___xxx_socketpair | 
|  | 599 |  | 
| Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 600 | typedef struct pollfd adb_pollfd; | 
|  | 601 | static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) { | 
|  | 602 | return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout)); | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | #define poll ___xxx_poll | 
|  | 606 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 607 | 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] | 608 | { | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 609 | return mkdir(path.c_str(), mode); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 610 | } | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 611 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 612 | #undef   mkdir | 
|  | 613 | #define  mkdir  ___xxx_mkdir | 
|  | 614 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 615 | 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] | 616 | return path[0] == '/'; | 
|  | 617 | } | 
|  | 618 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 619 | #endif /* !_WIN32 */ | 
|  | 620 |  | 
| Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 621 | static inline void disable_tcp_nagle(int fd) { | 
|  | 622 | int off = 1; | 
|  | 623 | adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off)); | 
|  | 624 | } | 
|  | 625 |  | 
| David Pursell | bfd9503 | 2016-02-22 14:27:23 -0800 | [diff] [blame] | 626 | // Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set | 
|  | 627 | // |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be | 
|  | 628 | // configured to drop after 10 missed keepalives. Returns true on success. | 
|  | 629 | bool set_tcp_keepalive(int fd, int interval_sec); | 
|  | 630 |  | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 631 | #if defined(_WIN32) | 
|  | 632 | // Win32 defines ERROR, which we don't need, but which conflicts with google3 logging. | 
|  | 633 | #undef ERROR | 
|  | 634 | #endif | 
|  | 635 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 636 | #endif /* _ADB_SYSDEPS_H */ |