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