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