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