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