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