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