Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SYSDEPS |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include "sysdeps.h" |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 21 | #include <lmcons.h> |
Stephen Hines | b117085 | 2014-10-01 17:37:06 -0700 | [diff] [blame] | 22 | #include <windows.h> |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 23 | #include <winsock2.h> /* winsock.h *must* be included before windows.h. */ |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 24 | |
| 25 | #include <errno.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
Christopher Ferris | 054d170 | 2014-11-06 14:34:24 -0800 | [diff] [blame] | 27 | #include <stdlib.h> |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 28 | |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 29 | #include <algorithm> |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 30 | #include <memory> |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 31 | #include <mutex> |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 32 | #include <string> |
Josh Gao | f0c4403 | 2018-12-12 16:12:28 -0800 | [diff] [blame] | 33 | #include <string_view> |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 34 | #include <unordered_map> |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 35 | #include <vector> |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | fe44751 | 2015-07-24 11:35:40 -0700 | [diff] [blame] | 37 | #include <cutils/sockets.h> |
| 38 | |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 39 | #include <android-base/errors.h> |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 40 | #include <android-base/file.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 42 | #include <android-base/macros.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
| 44 | #include <android-base/strings.h> |
| 45 | #include <android-base/utf8.h> |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 46 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include "adb.h" |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 48 | #include "adb_utils.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 50 | #include "sysdeps/uio.h" |
| 51 | |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 52 | /* forward declarations */ |
| 53 | |
| 54 | typedef const struct FHClassRec_* FHClass; |
| 55 | typedef struct FHRec_* FH; |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 56 | |
| 57 | typedef struct FHClassRec_ { |
| 58 | void (*_fh_init)(FH); |
| 59 | int (*_fh_close)(FH); |
Elliott Hughes | 9dcbc21 | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 60 | int64_t (*_fh_lseek)(FH, int64_t, int); |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 61 | int (*_fh_read)(FH, void*, int); |
| 62 | int (*_fh_write)(FH, const void*, int); |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 63 | int (*_fh_writev)(FH, const adb_iovec*, int); |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 64 | intptr_t (*_fh_get_os_handle)(FH); |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 65 | } FHClassRec; |
| 66 | |
| 67 | static void _fh_file_init(FH); |
| 68 | static int _fh_file_close(FH); |
Elliott Hughes | 9dcbc21 | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 69 | static int64_t _fh_file_lseek(FH, int64_t, int); |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 70 | static int _fh_file_read(FH, void*, int); |
| 71 | static int _fh_file_write(FH, const void*, int); |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 72 | static int _fh_file_writev(FH, const adb_iovec*, int); |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 73 | static intptr_t _fh_file_get_os_handle(FH f); |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 74 | |
| 75 | static const FHClassRec _fh_file_class = { |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 76 | _fh_file_init, _fh_file_close, _fh_file_lseek, _fh_file_read, |
| 77 | _fh_file_write, _fh_file_writev, _fh_file_get_os_handle, |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static void _fh_socket_init(FH); |
| 81 | static int _fh_socket_close(FH); |
Elliott Hughes | 9dcbc21 | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 82 | static int64_t _fh_socket_lseek(FH, int64_t, int); |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 83 | static int _fh_socket_read(FH, void*, int); |
| 84 | static int _fh_socket_write(FH, const void*, int); |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 85 | static int _fh_socket_writev(FH, const adb_iovec*, int); |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 86 | static intptr_t _fh_socket_get_os_handle(FH f); |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 87 | |
| 88 | static const FHClassRec _fh_socket_class = { |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 89 | _fh_socket_init, _fh_socket_close, _fh_socket_lseek, _fh_socket_read, |
| 90 | _fh_socket_write, _fh_socket_writev, _fh_socket_get_os_handle, |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Pirama Arumuga Nainar | 5231aff | 2018-08-08 10:33:24 -0700 | [diff] [blame] | 93 | #if defined(assert) |
| 94 | #undef assert |
| 95 | #endif |
| 96 | |
Spencer Low | 2bbb3a9 | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 97 | void handle_deleter::operator()(HANDLE h) { |
| 98 | // CreateFile() is documented to return INVALID_HANDLE_FILE on error, |
| 99 | // implying that NULL is a valid handle, but this is probably impossible. |
| 100 | // Other APIs like CreateEvent() are documented to return NULL on error, |
| 101 | // implying that INVALID_HANDLE_VALUE is a valid handle, but this is also |
| 102 | // probably impossible. Thus, consider both NULL and INVALID_HANDLE_VALUE |
| 103 | // as invalid handles. std::unique_ptr won't call a deleter with NULL, so we |
| 104 | // only need to check for INVALID_HANDLE_VALUE. |
| 105 | if (h != INVALID_HANDLE_VALUE) { |
| 106 | if (!CloseHandle(h)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 107 | D("CloseHandle(%p) failed: %s", h, |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 108 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); |
Spencer Low | 2bbb3a9 | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | /**************************************************************************/ |
| 114 | /**************************************************************************/ |
| 115 | /***** *****/ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | /***** common file descriptor handling *****/ |
| 117 | /***** *****/ |
| 118 | /**************************************************************************/ |
| 119 | /**************************************************************************/ |
| 120 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | typedef struct FHRec_ |
| 122 | { |
| 123 | FHClass clazz; |
| 124 | int used; |
| 125 | int eof; |
| 126 | union { |
| 127 | HANDLE handle; |
| 128 | SOCKET socket; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | } u; |
| 130 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | char name[32]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | } FHRec; |
| 133 | |
| 134 | #define fh_handle u.handle |
| 135 | #define fh_socket u.socket |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | |
Josh Gao | 4f657a7 | 2016-02-17 16:45:39 -0800 | [diff] [blame] | 137 | #define WIN32_FH_BASE 2048 |
Josh Gao | 7c9e5fb | 2016-04-18 11:09:28 -0700 | [diff] [blame] | 138 | #define WIN32_MAX_FHS 2048 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 140 | static std::mutex& _win32_lock = *new std::mutex(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | static FHRec _win32_fhs[ WIN32_MAX_FHS ]; |
Spencer Low | b732a37 | 2015-07-24 15:38:19 -0700 | [diff] [blame] | 142 | static int _win32_fh_next; // where to start search for free FHRec |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 144 | static FH _fh_from_int(borrowed_fd bfd, const char* func) { |
| 145 | FH f; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 147 | int fd = bfd.get(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | fd -= WIN32_FH_BASE; |
| 149 | |
Spencer Low | b732a37 | 2015-07-24 15:38:19 -0700 | [diff] [blame] | 150 | if (fd < 0 || fd >= WIN32_MAX_FHS) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 151 | D("_fh_from_int: invalid fd %d passed to %s", fd + WIN32_FH_BASE, func); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | errno = EBADF; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 153 | return nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | f = &_win32_fhs[fd]; |
| 157 | |
| 158 | if (f->used == 0) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 159 | D("_fh_from_int: invalid fd %d passed to %s", fd + WIN32_FH_BASE, func); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | errno = EBADF; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 161 | return nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return f; |
| 165 | } |
| 166 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 167 | static int _fh_to_int(FH f) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | if (f && f->used && f >= _win32_fhs && f < _win32_fhs + WIN32_MAX_FHS) |
| 169 | return (int)(f - _win32_fhs) + WIN32_FH_BASE; |
| 170 | |
| 171 | return -1; |
| 172 | } |
| 173 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 174 | static FH _fh_alloc(FHClass clazz) { |
| 175 | FH f = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 177 | std::lock_guard<std::mutex> lock(_win32_lock); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | |
Josh Gao | 4f657a7 | 2016-02-17 16:45:39 -0800 | [diff] [blame] | 179 | for (int i = _win32_fh_next; i < WIN32_MAX_FHS; ++i) { |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 180 | if (_win32_fhs[i].clazz == nullptr) { |
Josh Gao | 4f657a7 | 2016-02-17 16:45:39 -0800 | [diff] [blame] | 181 | f = &_win32_fhs[i]; |
| 182 | _win32_fh_next = i + 1; |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 183 | f->clazz = clazz; |
| 184 | f->used = 1; |
| 185 | f->eof = 0; |
| 186 | f->name[0] = '\0'; |
| 187 | clazz->_fh_init(f); |
| 188 | return f; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 191 | |
| 192 | D("_fh_alloc: no more free file descriptors"); |
| 193 | errno = EMFILE; // Too many open files |
| 194 | return nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 197 | static int _fh_close(FH f) { |
Spencer Low | b732a37 | 2015-07-24 15:38:19 -0700 | [diff] [blame] | 198 | // Use lock so that closing only happens once and so that _fh_alloc can't |
| 199 | // allocate a FH that we're in the middle of closing. |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 200 | std::lock_guard<std::mutex> lock(_win32_lock); |
Josh Gao | 4f657a7 | 2016-02-17 16:45:39 -0800 | [diff] [blame] | 201 | |
| 202 | int offset = f - _win32_fhs; |
| 203 | if (_win32_fh_next > offset) { |
| 204 | _win32_fh_next = offset; |
| 205 | } |
| 206 | |
Spencer Low | b732a37 | 2015-07-24 15:38:19 -0700 | [diff] [blame] | 207 | if (f->used) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 208 | f->clazz->_fh_close( f ); |
Spencer Low | b732a37 | 2015-07-24 15:38:19 -0700 | [diff] [blame] | 209 | f->name[0] = '\0'; |
| 210 | f->eof = 0; |
| 211 | f->used = 0; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 212 | f->clazz = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 217 | // Deleter for unique_fh. |
| 218 | class fh_deleter { |
| 219 | public: |
| 220 | void operator()(struct FHRec_* fh) { |
| 221 | // We're called from a destructor and destructors should not overwrite |
| 222 | // errno because callers may do: |
| 223 | // errno = EBLAH; |
| 224 | // return -1; // calls destructor, which should not overwrite errno |
| 225 | const int saved_errno = errno; |
| 226 | _fh_close(fh); |
| 227 | errno = saved_errno; |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | // Like std::unique_ptr, but calls _fh_close() instead of operator delete(). |
| 232 | typedef std::unique_ptr<struct FHRec_, fh_deleter> unique_fh; |
| 233 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | /**************************************************************************/ |
| 235 | /**************************************************************************/ |
| 236 | /***** *****/ |
| 237 | /***** file-based descriptor handling *****/ |
| 238 | /***** *****/ |
| 239 | /**************************************************************************/ |
| 240 | /**************************************************************************/ |
| 241 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 242 | static void _fh_file_init(FH f) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | f->fh_handle = INVALID_HANDLE_VALUE; |
| 244 | } |
| 245 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 246 | static int _fh_file_close(FH f) { |
| 247 | CloseHandle(f->fh_handle); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 248 | f->fh_handle = INVALID_HANDLE_VALUE; |
| 249 | return 0; |
| 250 | } |
| 251 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 252 | static int _fh_file_read(FH f, void* buf, int len) { |
| 253 | DWORD read_bytes; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 254 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 255 | if (!ReadFile(f->fh_handle, buf, (DWORD)len, &read_bytes, nullptr)) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 256 | D("adb_read: could not read %d bytes from %s", len, f->name); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 257 | errno = EIO; |
| 258 | return -1; |
| 259 | } else if (read_bytes < (DWORD)len) { |
| 260 | f->eof = 1; |
| 261 | } |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 262 | return read_bytes; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 265 | static int _fh_file_write(FH f, const void* buf, int len) { |
| 266 | DWORD wrote_bytes; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 268 | if (!WriteFile(f->fh_handle, buf, (DWORD)len, &wrote_bytes, nullptr)) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 269 | D("adb_file_write: could not write %d bytes from %s", len, f->name); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | errno = EIO; |
| 271 | return -1; |
| 272 | } else if (wrote_bytes < (DWORD)len) { |
| 273 | f->eof = 1; |
| 274 | } |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 275 | return wrote_bytes; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 278 | static int _fh_file_writev(FH f, const adb_iovec* iov, int iovcnt) { |
| 279 | if (iovcnt <= 0) { |
| 280 | errno = EINVAL; |
| 281 | return -1; |
| 282 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 284 | DWORD wrote_bytes = 0; |
| 285 | |
| 286 | for (int i = 0; i < iovcnt; ++i) { |
| 287 | ssize_t rc = _fh_file_write(f, iov[i].iov_base, iov[i].iov_len); |
| 288 | if (rc == -1) { |
| 289 | return wrote_bytes > 0 ? wrote_bytes : -1; |
| 290 | } else if (rc == 0) { |
| 291 | return wrote_bytes; |
| 292 | } |
| 293 | |
| 294 | wrote_bytes += rc; |
| 295 | |
| 296 | if (static_cast<size_t>(rc) < iov[i].iov_len) { |
| 297 | return wrote_bytes; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return wrote_bytes; |
| 302 | } |
| 303 | |
Elliott Hughes | 9dcbc21 | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 304 | static int64_t _fh_file_lseek(FH f, int64_t pos, int origin) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 305 | DWORD method; |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 306 | switch (origin) { |
| 307 | case SEEK_SET: |
| 308 | method = FILE_BEGIN; |
| 309 | break; |
| 310 | case SEEK_CUR: |
| 311 | method = FILE_CURRENT; |
| 312 | break; |
| 313 | case SEEK_END: |
| 314 | method = FILE_END; |
| 315 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | default: |
| 317 | errno = EINVAL; |
| 318 | return -1; |
| 319 | } |
| 320 | |
Elliott Hughes | 9dcbc21 | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 321 | LARGE_INTEGER li = {.QuadPart = pos}; |
| 322 | if (!SetFilePointerEx(f->fh_handle, li, &li, method)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 323 | errno = EIO; |
| 324 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | } |
Elliott Hughes | 9dcbc21 | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 326 | f->eof = 0; |
| 327 | return li.QuadPart; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 330 | static intptr_t _fh_file_get_os_handle(FH f) { |
| 331 | return reinterpret_cast<intptr_t>(f->u.handle); |
| 332 | } |
| 333 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 334 | /**************************************************************************/ |
| 335 | /**************************************************************************/ |
| 336 | /***** *****/ |
| 337 | /***** file-based descriptor handling *****/ |
| 338 | /***** *****/ |
| 339 | /**************************************************************************/ |
| 340 | /**************************************************************************/ |
| 341 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 342 | int adb_open(const char* path, int options) { |
| 343 | FH f; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 345 | DWORD desiredAccess = 0; |
| 346 | DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 347 | |
Josh Gao | 3befb59 | 2019-02-07 14:13:39 -0800 | [diff] [blame] | 348 | // CreateFileW is inherently O_CLOEXEC by default. |
| 349 | options &= ~O_CLOEXEC; |
| 350 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 | switch (options) { |
| 352 | case O_RDONLY: |
| 353 | desiredAccess = GENERIC_READ; |
| 354 | break; |
| 355 | case O_WRONLY: |
| 356 | desiredAccess = GENERIC_WRITE; |
| 357 | break; |
| 358 | case O_RDWR: |
| 359 | desiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 360 | break; |
| 361 | default: |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 362 | D("adb_open: invalid options (0x%0x)", options); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | errno = EINVAL; |
| 364 | return -1; |
| 365 | } |
| 366 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 367 | f = _fh_alloc(&_fh_file_class); |
| 368 | if (!f) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 369 | return -1; |
| 370 | } |
| 371 | |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 372 | std::wstring path_wide; |
| 373 | if (!android::base::UTF8ToWide(path, &path_wide)) { |
| 374 | return -1; |
| 375 | } |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 376 | f->fh_handle = |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 377 | CreateFileW(path_wide.c_str(), desiredAccess, shareMode, nullptr, OPEN_EXISTING, 0, nullptr); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 379 | if (f->fh_handle == INVALID_HANDLE_VALUE) { |
Spencer Low | 5c761bd | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 380 | const DWORD err = GetLastError(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 381 | _fh_close(f); |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 382 | D("adb_open: could not open '%s': ", path); |
Spencer Low | 5c761bd | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 383 | switch (err) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 384 | case ERROR_FILE_NOT_FOUND: |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 385 | D("file not found"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | errno = ENOENT; |
| 387 | return -1; |
| 388 | |
| 389 | case ERROR_PATH_NOT_FOUND: |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 390 | D("path not found"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | errno = ENOTDIR; |
| 392 | return -1; |
| 393 | |
| 394 | default: |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 395 | D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 396 | errno = ENOENT; |
| 397 | return -1; |
| 398 | } |
| 399 | } |
Vladimir Chtchetkine | b87b828 | 2011-11-30 10:20:27 -0800 | [diff] [blame] | 400 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 401 | snprintf(f->name, sizeof(f->name), "%d(%s)", _fh_to_int(f), path); |
| 402 | D("adb_open: '%s' => fd %d", path, _fh_to_int(f)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 | return _fh_to_int(f); |
| 404 | } |
| 405 | |
| 406 | /* ignore mode on Win32 */ |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 407 | int adb_creat(const char* path, int mode) { |
| 408 | FH f; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 410 | f = _fh_alloc(&_fh_file_class); |
| 411 | if (!f) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 412 | return -1; |
| 413 | } |
| 414 | |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 415 | std::wstring path_wide; |
| 416 | if (!android::base::UTF8ToWide(path, &path_wide)) { |
| 417 | return -1; |
| 418 | } |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 419 | f->fh_handle = CreateFileW(path_wide.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 420 | nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 422 | if (f->fh_handle == INVALID_HANDLE_VALUE) { |
Spencer Low | 5c761bd | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 423 | const DWORD err = GetLastError(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 | _fh_close(f); |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 425 | D("adb_creat: could not open '%s': ", path); |
Spencer Low | 5c761bd | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 426 | switch (err) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | case ERROR_FILE_NOT_FOUND: |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 428 | D("file not found"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | errno = ENOENT; |
| 430 | return -1; |
| 431 | |
| 432 | case ERROR_PATH_NOT_FOUND: |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 433 | D("path not found"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | errno = ENOTDIR; |
| 435 | return -1; |
| 436 | |
| 437 | default: |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 438 | D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | errno = ENOENT; |
| 440 | return -1; |
| 441 | } |
| 442 | } |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 443 | snprintf(f->name, sizeof(f->name), "%d(%s)", _fh_to_int(f), path); |
| 444 | D("adb_creat: '%s' => fd %d", path, _fh_to_int(f)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | return _fh_to_int(f); |
| 446 | } |
| 447 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 448 | int adb_read(borrowed_fd fd, void* buf, int len) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 449 | FH f = _fh_from_int(fd, __func__); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 451 | if (f == nullptr) { |
Josh Gao | de16596 | 2018-04-05 18:09:39 -0700 | [diff] [blame] | 452 | errno = EBADF; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 453 | return -1; |
| 454 | } |
| 455 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 456 | return f->clazz->_fh_read(f, buf, len); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 459 | int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset) { |
| 460 | OVERLAPPED overlapped = {}; |
| 461 | overlapped.Offset = static_cast<DWORD>(offset); |
| 462 | overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32); |
| 463 | DWORD bytes_read; |
| 464 | if (!::ReadFile(adb_get_os_handle(fd), buf, static_cast<DWORD>(len), &bytes_read, |
| 465 | &overlapped)) { |
| 466 | D("adb_pread: could not read %d bytes from FD %d", len, fd.get()); |
| 467 | switch (::GetLastError()) { |
| 468 | case ERROR_IO_PENDING: |
| 469 | errno = EAGAIN; |
| 470 | return -1; |
| 471 | default: |
| 472 | errno = EINVAL; |
| 473 | return -1; |
| 474 | } |
| 475 | } |
| 476 | return static_cast<int>(bytes_read); |
| 477 | } |
| 478 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 479 | int adb_write(borrowed_fd fd, const void* buf, int len) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 480 | FH f = _fh_from_int(fd, __func__); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 481 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 482 | if (f == nullptr) { |
Josh Gao | de16596 | 2018-04-05 18:09:39 -0700 | [diff] [blame] | 483 | errno = EBADF; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | return f->clazz->_fh_write(f, buf, len); |
| 488 | } |
| 489 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 490 | ssize_t adb_writev(borrowed_fd fd, const adb_iovec* iov, int iovcnt) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 491 | FH f = _fh_from_int(fd, __func__); |
| 492 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 493 | if (f == nullptr) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 494 | errno = EBADF; |
| 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | return f->clazz->_fh_writev(f, iov, iovcnt); |
| 499 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 501 | int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset) { |
| 502 | OVERLAPPED params = {}; |
| 503 | params.Offset = static_cast<DWORD>(offset); |
| 504 | params.OffsetHigh = static_cast<DWORD>(offset >> 32); |
| 505 | DWORD bytes_written = 0; |
| 506 | if (!::WriteFile(adb_get_os_handle(fd), buf, len, &bytes_written, ¶ms)) { |
| 507 | D("adb_pwrite: could not write %d bytes to FD %d", len, fd.get()); |
| 508 | switch (::GetLastError()) { |
| 509 | case ERROR_IO_PENDING: |
| 510 | errno = EAGAIN; |
| 511 | return -1; |
| 512 | default: |
| 513 | errno = EINVAL; |
| 514 | return -1; |
| 515 | } |
| 516 | } |
| 517 | return static_cast<int>(bytes_written); |
| 518 | } |
| 519 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 520 | int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) { |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 521 | FH f = _fh_from_int(fd, __func__); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 522 | if (!f) { |
Josh Gao | de16596 | 2018-04-05 18:09:39 -0700 | [diff] [blame] | 523 | errno = EBADF; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | return -1; |
| 525 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | return f->clazz->_fh_lseek(f, pos, where); |
| 527 | } |
| 528 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 529 | int adb_close(int fd) { |
| 530 | FH f = _fh_from_int(fd, __func__); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 531 | |
| 532 | if (!f) { |
Josh Gao | de16596 | 2018-04-05 18:09:39 -0700 | [diff] [blame] | 533 | errno = EBADF; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 534 | return -1; |
| 535 | } |
| 536 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 537 | D("adb_close: %s", f->name); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 538 | _fh_close(f); |
| 539 | return 0; |
| 540 | } |
| 541 | |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 542 | HANDLE adb_get_os_handle(borrowed_fd fd) { |
| 543 | FH f = _fh_from_int(fd, __func__); |
| 544 | |
| 545 | if (!f) { |
| 546 | errno = EBADF; |
| 547 | return nullptr; |
| 548 | } |
| 549 | |
| 550 | D("adb_get_os_handle: %s", f->name); |
| 551 | const intptr_t intptr_handle = f->clazz->_fh_get_os_handle(f); |
| 552 | const HANDLE handle = reinterpret_cast<const HANDLE>(intptr_handle); |
| 553 | return handle; |
| 554 | } |
| 555 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 556 | /**************************************************************************/ |
| 557 | /**************************************************************************/ |
| 558 | /***** *****/ |
| 559 | /***** socket-based file descriptors *****/ |
| 560 | /***** *****/ |
| 561 | /**************************************************************************/ |
| 562 | /**************************************************************************/ |
| 563 | |
Spencer Low | 31aafa6 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 564 | #undef setsockopt |
| 565 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 566 | static void _socket_set_errno( const DWORD err ) { |
Spencer Low | 028e159 | 2015-10-18 16:45:09 -0700 | [diff] [blame] | 567 | // Because the Windows C Runtime (MSVCRT.DLL) strerror() does not support a |
| 568 | // lot of POSIX and socket error codes, some of the resulting error codes |
Josh Gao | 75e96bb | 2016-12-05 13:24:48 -0800 | [diff] [blame] | 569 | // are mapped to strings by adb_strerror(). |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 570 | switch ( err ) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 571 | case 0: errno = 0; break; |
Spencer Low | 028e159 | 2015-10-18 16:45:09 -0700 | [diff] [blame] | 572 | // Don't map WSAEINTR since that is only for Winsock 1.1 which we don't use. |
| 573 | // case WSAEINTR: errno = EINTR; break; |
| 574 | case WSAEFAULT: errno = EFAULT; break; |
| 575 | case WSAEINVAL: errno = EINVAL; break; |
| 576 | case WSAEMFILE: errno = EMFILE; break; |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 577 | // Mapping WSAEWOULDBLOCK to EAGAIN is absolutely critical because |
| 578 | // non-blocking sockets can cause an error code of WSAEWOULDBLOCK and |
| 579 | // callers check specifically for EAGAIN. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | case WSAEWOULDBLOCK: errno = EAGAIN; break; |
Spencer Low | 028e159 | 2015-10-18 16:45:09 -0700 | [diff] [blame] | 581 | case WSAENOTSOCK: errno = ENOTSOCK; break; |
| 582 | case WSAENOPROTOOPT: errno = ENOPROTOOPT; break; |
| 583 | case WSAEOPNOTSUPP: errno = EOPNOTSUPP; break; |
| 584 | case WSAENETDOWN: errno = ENETDOWN; break; |
| 585 | case WSAENETRESET: errno = ENETRESET; break; |
| 586 | // Map WSAECONNABORTED to EPIPE instead of ECONNABORTED because POSIX seems |
| 587 | // to use EPIPE for these situations and there are some callers that look |
| 588 | // for EPIPE. |
| 589 | case WSAECONNABORTED: errno = EPIPE; break; |
| 590 | case WSAECONNRESET: errno = ECONNRESET; break; |
| 591 | case WSAENOBUFS: errno = ENOBUFS; break; |
| 592 | case WSAENOTCONN: errno = ENOTCONN; break; |
| 593 | // Don't map WSAETIMEDOUT because we don't currently use SO_RCVTIMEO or |
| 594 | // SO_SNDTIMEO which would cause WSAETIMEDOUT to be returned. Future |
| 595 | // considerations: Reportedly send() can return zero on timeout, and POSIX |
| 596 | // code may expect EAGAIN instead of ETIMEDOUT on timeout. |
| 597 | // case WSAETIMEDOUT: errno = ETIMEDOUT; break; |
| 598 | case WSAEHOSTUNREACH: errno = EHOSTUNREACH; break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 599 | default: |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | errno = EINVAL; |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 601 | D( "_socket_set_errno: mapping Windows error code %lu to errno %d", |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 602 | err, errno ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 606 | extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) { |
| 607 | // WSAPoll doesn't handle invalid/non-socket handles, so we need to handle them ourselves. |
| 608 | int skipped = 0; |
| 609 | std::vector<WSAPOLLFD> sockets; |
| 610 | std::vector<adb_pollfd*> original; |
Josh Gao | be2ee7b | 2018-03-29 12:34:28 -0700 | [diff] [blame] | 611 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 612 | for (size_t i = 0; i < nfds; ++i) { |
| 613 | FH fh = _fh_from_int(fds[i].fd, __func__); |
| 614 | if (!fh || !fh->used || fh->clazz != &_fh_socket_class) { |
| 615 | D("adb_poll received bad FD %d", fds[i].fd); |
| 616 | fds[i].revents = POLLNVAL; |
| 617 | ++skipped; |
| 618 | } else { |
| 619 | WSAPOLLFD wsapollfd = { |
| 620 | .fd = fh->u.socket, |
| 621 | .events = static_cast<short>(fds[i].events) |
| 622 | }; |
| 623 | sockets.push_back(wsapollfd); |
| 624 | original.push_back(&fds[i]); |
| 625 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 626 | } |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 627 | |
| 628 | if (sockets.empty()) { |
| 629 | return skipped; |
| 630 | } |
| 631 | |
Josh Gao | be2ee7b | 2018-03-29 12:34:28 -0700 | [diff] [blame] | 632 | // If we have any invalid FDs in our FD set, make sure to return immediately. |
| 633 | if (skipped > 0) { |
| 634 | timeout = 0; |
| 635 | } |
| 636 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 637 | int result = WSAPoll(sockets.data(), sockets.size(), timeout); |
| 638 | if (result == SOCKET_ERROR) { |
| 639 | _socket_set_errno(WSAGetLastError()); |
| 640 | return -1; |
| 641 | } |
| 642 | |
| 643 | // Map the results back onto the original set. |
| 644 | for (size_t i = 0; i < sockets.size(); ++i) { |
| 645 | original[i]->revents = sockets[i].revents; |
| 646 | } |
| 647 | |
Josh Gao | be2ee7b | 2018-03-29 12:34:28 -0700 | [diff] [blame] | 648 | // WSAPoll appears to return the number of unique FDs with available events, instead of how many |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 649 | // of the pollfd elements have a non-zero revents field, which is what it and poll are specified |
| 650 | // to do. Ignore its result and calculate the proper return value. |
| 651 | result = 0; |
| 652 | for (size_t i = 0; i < nfds; ++i) { |
| 653 | if (fds[i].revents != 0) { |
| 654 | ++result; |
| 655 | } |
| 656 | } |
| 657 | return result; |
| 658 | } |
| 659 | |
| 660 | static void _fh_socket_init(FH f) { |
| 661 | f->fh_socket = INVALID_SOCKET; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 662 | } |
| 663 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 664 | static int _fh_socket_close(FH f) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 665 | if (f->fh_socket != INVALID_SOCKET) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 666 | if (closesocket(f->fh_socket) == SOCKET_ERROR) { |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 667 | // Don't set errno here, since adb_close will ignore it. |
| 668 | const DWORD err = WSAGetLastError(); |
| 669 | D("closesocket failed: %s", android::base::SystemErrorCodeToString(err).c_str()); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 670 | } |
| 671 | f->fh_socket = INVALID_SOCKET; |
| 672 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
Elliott Hughes | 9dcbc21 | 2018-09-20 13:59:49 -0700 | [diff] [blame] | 676 | static int64_t _fh_socket_lseek(FH f, int64_t pos, int origin) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 677 | errno = EPIPE; |
| 678 | return -1; |
| 679 | } |
| 680 | |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 681 | static int _fh_socket_read(FH f, void* buf, int len) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 682 | int result = recv(f->fh_socket, reinterpret_cast<char*>(buf), len, 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 683 | if (result == SOCKET_ERROR) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 684 | const DWORD err = WSAGetLastError(); |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 685 | // WSAEWOULDBLOCK is normal with a non-blocking socket, so don't trace |
| 686 | // that to reduce spam and confusion. |
| 687 | if (err != WSAEWOULDBLOCK) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 688 | D("recv fd %d failed: %s", _fh_to_int(f), |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 689 | android::base::SystemErrorCodeToString(err).c_str()); |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 690 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 691 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 692 | result = -1; |
| 693 | } |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 694 | return result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 697 | static int _fh_socket_write(FH f, const void* buf, int len) { |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 698 | int result = send(f->fh_socket, reinterpret_cast<const char*>(buf), len, 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 699 | if (result == SOCKET_ERROR) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 700 | const DWORD err = WSAGetLastError(); |
Spencer Low | 028e159 | 2015-10-18 16:45:09 -0700 | [diff] [blame] | 701 | // WSAEWOULDBLOCK is normal with a non-blocking socket, so don't trace |
| 702 | // that to reduce spam and confusion. |
| 703 | if (err != WSAEWOULDBLOCK) { |
| 704 | D("send fd %d failed: %s", _fh_to_int(f), |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 705 | android::base::SystemErrorCodeToString(err).c_str()); |
Spencer Low | 028e159 | 2015-10-18 16:45:09 -0700 | [diff] [blame] | 706 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 707 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 708 | result = -1; |
Spencer Low | c7c4561 | 2015-09-29 15:05:29 -0700 | [diff] [blame] | 709 | } else { |
| 710 | // According to https://code.google.com/p/chromium/issues/detail?id=27870 |
| 711 | // Winsock Layered Service Providers may cause this. |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 712 | CHECK_LE(result, len) << "Tried to write " << len << " bytes to " << f->name << ", but " |
| 713 | << result << " bytes reportedly written"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 714 | } |
| 715 | return result; |
| 716 | } |
| 717 | |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 718 | // Make sure that adb_iovec is compatible with WSABUF. |
| 719 | static_assert(sizeof(adb_iovec) == sizeof(WSABUF), ""); |
| 720 | static_assert(SIZEOF_MEMBER(adb_iovec, iov_len) == SIZEOF_MEMBER(WSABUF, len), ""); |
| 721 | static_assert(offsetof(adb_iovec, iov_len) == offsetof(WSABUF, len), ""); |
| 722 | |
| 723 | static_assert(SIZEOF_MEMBER(adb_iovec, iov_base) == SIZEOF_MEMBER(WSABUF, buf), ""); |
| 724 | static_assert(offsetof(adb_iovec, iov_base) == offsetof(WSABUF, buf), ""); |
| 725 | |
| 726 | static int _fh_socket_writev(FH f, const adb_iovec* iov, int iovcnt) { |
| 727 | if (iovcnt <= 0) { |
| 728 | errno = EINVAL; |
| 729 | return -1; |
| 730 | } |
| 731 | |
| 732 | WSABUF* wsabuf = reinterpret_cast<WSABUF*>(const_cast<adb_iovec*>(iov)); |
| 733 | DWORD bytes_written = 0; |
| 734 | int result = WSASend(f->fh_socket, wsabuf, iovcnt, &bytes_written, 0, nullptr, nullptr); |
| 735 | if (result == SOCKET_ERROR) { |
| 736 | const DWORD err = WSAGetLastError(); |
| 737 | // WSAEWOULDBLOCK is normal with a non-blocking socket, so don't trace |
| 738 | // that to reduce spam and confusion. |
| 739 | if (err != WSAEWOULDBLOCK) { |
| 740 | D("send fd %d failed: %s", _fh_to_int(f), |
| 741 | android::base::SystemErrorCodeToString(err).c_str()); |
| 742 | } |
| 743 | _socket_set_errno(err); |
Josh Gao | ee977a3 | 2019-08-07 18:26:47 -0700 | [diff] [blame] | 744 | return -1; |
Josh Gao | 1bbdd25 | 2018-04-05 17:55:25 -0700 | [diff] [blame] | 745 | } |
| 746 | CHECK_GE(static_cast<DWORD>(std::numeric_limits<int>::max()), bytes_written); |
| 747 | return static_cast<int>(bytes_written); |
| 748 | } |
| 749 | |
Yurii Zubrytskyi | 243d248 | 2019-07-10 17:59:34 -0700 | [diff] [blame] | 750 | static intptr_t _fh_socket_get_os_handle(FH f) { |
| 751 | return f->u.socket; |
| 752 | } |
| 753 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 754 | /**************************************************************************/ |
| 755 | /**************************************************************************/ |
| 756 | /***** *****/ |
| 757 | /***** replacement for libs/cutils/socket_xxxx.c *****/ |
| 758 | /***** *****/ |
| 759 | /**************************************************************************/ |
| 760 | /**************************************************************************/ |
| 761 | |
Spencer Low | a090368 | 2018-08-10 16:20:57 -0700 | [diff] [blame] | 762 | static void _init_winsock() { |
Josh Gao | caeda2c | 2018-04-05 18:10:03 -0700 | [diff] [blame] | 763 | static std::once_flag once; |
| 764 | std::call_once(once, []() { |
| 765 | WSADATA wsaData; |
| 766 | int rc = WSAStartup(MAKEWORD(2, 2), &wsaData); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 767 | if (rc != 0) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 768 | LOG(FATAL) << "could not initialize Winsock: " |
| 769 | << android::base::SystemErrorCodeToString(rc); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 770 | } |
Spencer Low | c7c1ca6 | 2015-08-12 18:19:16 -0700 | [diff] [blame] | 771 | |
| 772 | // Note that we do not call atexit() to register WSACleanup to be called |
| 773 | // at normal process termination because: |
| 774 | // 1) When exit() is called, there are still threads actively using |
| 775 | // Winsock because we don't cleanly shutdown all threads, so it |
| 776 | // doesn't make sense to call WSACleanup() and may cause problems |
| 777 | // with those threads. |
| 778 | // 2) A deadlock can occur when exit() holds a C Runtime lock, then it |
| 779 | // calls WSACleanup() which tries to unload a DLL, which tries to |
| 780 | // grab the LoaderLock. This conflicts with the device_poll_thread |
| 781 | // which holds the LoaderLock because AdbWinApi.dll calls |
| 782 | // setupapi.dll which tries to load wintrust.dll which tries to load |
| 783 | // crypt32.dll which calls atexit() which tries to acquire the C |
| 784 | // Runtime lock that the other thread holds. |
Josh Gao | caeda2c | 2018-04-05 18:10:03 -0700 | [diff] [blame] | 785 | }); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Spencer Low | c7c4561 | 2015-09-29 15:05:29 -0700 | [diff] [blame] | 788 | // Map a socket type to an explicit socket protocol instead of using the socket |
| 789 | // protocol of 0. Explicit socket protocols are used by most apps and we should |
| 790 | // do the same to reduce the chance of exercising uncommon code-paths that might |
| 791 | // have problems or that might load different Winsock service providers that |
| 792 | // have problems. |
| 793 | static int GetSocketProtocolFromSocketType(int type) { |
| 794 | switch (type) { |
| 795 | case SOCK_STREAM: |
| 796 | return IPPROTO_TCP; |
| 797 | case SOCK_DGRAM: |
| 798 | return IPPROTO_UDP; |
| 799 | default: |
| 800 | LOG(FATAL) << "Unknown socket type: " << type; |
| 801 | return 0; |
| 802 | } |
| 803 | } |
| 804 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 805 | int network_loopback_client(int port, int type, std::string* error) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 806 | struct sockaddr_in addr; |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 807 | SOCKET s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 808 | |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 809 | unique_fh f(_fh_alloc(&_fh_socket_class)); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 810 | if (!f) { |
| 811 | *error = strerror(errno); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 812 | return -1; |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 813 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 814 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 815 | memset(&addr, 0, sizeof(addr)); |
| 816 | addr.sin_family = AF_INET; |
| 817 | addr.sin_port = htons(port); |
| 818 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 819 | |
Spencer Low | c7c4561 | 2015-09-29 15:05:29 -0700 | [diff] [blame] | 820 | s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type)); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 821 | if (s == INVALID_SOCKET) { |
| 822 | const DWORD err = WSAGetLastError(); |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 823 | *error = android::base::StringPrintf("cannot create socket: %s", |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 824 | android::base::SystemErrorCodeToString(err).c_str()); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 825 | D("%s", error->c_str()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 826 | _socket_set_errno(err); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 827 | return -1; |
| 828 | } |
| 829 | f->fh_socket = s; |
| 830 | |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 831 | if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 832 | // Save err just in case inet_ntoa() or ntohs() changes the last error. |
| 833 | const DWORD err = WSAGetLastError(); |
| 834 | *error = android::base::StringPrintf("cannot connect to %s:%u: %s", |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 835 | inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), |
| 836 | android::base::SystemErrorCodeToString(err).c_str()); |
| 837 | D("could not connect to %s:%d: %s", type != SOCK_STREAM ? "udp" : "tcp", port, |
| 838 | error->c_str()); |
| 839 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 840 | return -1; |
| 841 | } |
| 842 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 843 | const int fd = _fh_to_int(f.get()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 844 | snprintf(f->name, sizeof(f->name), "%d(lo-client:%s%d)", fd, type != SOCK_STREAM ? "udp:" : "", |
| 845 | port); |
| 846 | D("port %d type %s => fd %d", port, type != SOCK_STREAM ? "udp" : "tcp", fd); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 847 | f.release(); |
| 848 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 849 | } |
| 850 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 851 | // interface_address is INADDR_LOOPBACK or INADDR_ANY. |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 852 | static int _network_server(int port, int type, u_long interface_address, std::string* error) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 853 | struct sockaddr_in addr; |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 854 | SOCKET s; |
| 855 | int n; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 856 | |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 857 | unique_fh f(_fh_alloc(&_fh_socket_class)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 858 | if (!f) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 859 | *error = strerror(errno); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 860 | return -1; |
| 861 | } |
| 862 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 863 | memset(&addr, 0, sizeof(addr)); |
| 864 | addr.sin_family = AF_INET; |
| 865 | addr.sin_port = htons(port); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 866 | addr.sin_addr.s_addr = htonl(interface_address); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 867 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 868 | // TODO: Consider using dual-stack socket that can simultaneously listen on |
| 869 | // IPv4 and IPv6. |
Spencer Low | c7c4561 | 2015-09-29 15:05:29 -0700 | [diff] [blame] | 870 | s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type)); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 871 | if (s == INVALID_SOCKET) { |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 872 | const DWORD err = WSAGetLastError(); |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 873 | *error = android::base::StringPrintf("cannot create socket: %s", |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 874 | android::base::SystemErrorCodeToString(err).c_str()); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 875 | D("%s", error->c_str()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 876 | _socket_set_errno(err); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 877 | return -1; |
| 878 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 879 | |
| 880 | f->fh_socket = s; |
| 881 | |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 882 | // Note: SO_REUSEADDR on Windows allows multiple processes to bind to the |
| 883 | // same port, so instead use SO_EXCLUSIVEADDRUSE. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 884 | n = 1; |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 885 | if (setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char*)&n, sizeof(n)) == SOCKET_ERROR) { |
| 886 | const DWORD err = WSAGetLastError(); |
| 887 | *error = android::base::StringPrintf("cannot set socket option SO_EXCLUSIVEADDRUSE: %s", |
| 888 | android::base::SystemErrorCodeToString(err).c_str()); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 889 | D("%s", error->c_str()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 890 | _socket_set_errno(err); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 891 | return -1; |
| 892 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 893 | |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 894 | if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 895 | // Save err just in case inet_ntoa() or ntohs() changes the last error. |
| 896 | const DWORD err = WSAGetLastError(); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 897 | *error = android::base::StringPrintf("cannot bind to %s:%u: %s", inet_ntoa(addr.sin_addr), |
| 898 | ntohs(addr.sin_port), |
| 899 | android::base::SystemErrorCodeToString(err).c_str()); |
| 900 | D("could not bind to %s:%d: %s", type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str()); |
| 901 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 902 | return -1; |
| 903 | } |
| 904 | if (type == SOCK_STREAM) { |
Josh Gao | a076b15 | 2018-03-20 14:25:03 -0700 | [diff] [blame] | 905 | if (listen(s, SOMAXCONN) == SOCKET_ERROR) { |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 906 | const DWORD err = WSAGetLastError(); |
| 907 | *error = android::base::StringPrintf( |
| 908 | "cannot listen on socket: %s", android::base::SystemErrorCodeToString(err).c_str()); |
| 909 | D("could not listen on %s:%d: %s", type != SOCK_STREAM ? "udp" : "tcp", port, |
| 910 | error->c_str()); |
| 911 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 912 | return -1; |
| 913 | } |
| 914 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 915 | const int fd = _fh_to_int(f.get()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 916 | snprintf(f->name, sizeof(f->name), "%d(%s-server:%s%d)", fd, |
| 917 | interface_address == INADDR_LOOPBACK ? "lo" : "any", type != SOCK_STREAM ? "udp:" : "", |
| 918 | port); |
| 919 | D("port %d type %s => fd %d", port, type != SOCK_STREAM ? "udp" : "tcp", fd); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 920 | f.release(); |
| 921 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Callum Ryan | 04efea3 | 2019-10-31 07:21:42 -0700 | [diff] [blame] | 924 | int network_loopback_server(int port, int type, std::string* error, bool prefer_ipv4) { |
| 925 | // TODO implement IPv6 support on windows |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 926 | return _network_server(port, type, INADDR_LOOPBACK, error); |
| 927 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 928 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 929 | int network_inaddr_any_server(int port, int type, std::string* error) { |
| 930 | return _network_server(port, type, INADDR_ANY, error); |
| 931 | } |
| 932 | |
| 933 | int network_connect(const std::string& host, int port, int type, int timeout, std::string* error) { |
| 934 | unique_fh f(_fh_alloc(&_fh_socket_class)); |
| 935 | if (!f) { |
| 936 | *error = strerror(errno); |
| 937 | return -1; |
| 938 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 939 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 940 | struct addrinfo hints; |
| 941 | memset(&hints, 0, sizeof(hints)); |
| 942 | hints.ai_family = AF_UNSPEC; |
| 943 | hints.ai_socktype = type; |
Spencer Low | c7c4561 | 2015-09-29 15:05:29 -0700 | [diff] [blame] | 944 | hints.ai_protocol = GetSocketProtocolFromSocketType(type); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 945 | |
| 946 | char port_str[16]; |
| 947 | snprintf(port_str, sizeof(port_str), "%d", port); |
| 948 | |
| 949 | struct addrinfo* addrinfo_ptr = nullptr; |
Spencer Low | cc467f1 | 2015-08-02 18:13:54 -0700 | [diff] [blame] | 950 | |
| 951 | #if (NTDDI_VERSION >= NTDDI_WINXPSP2) || (_WIN32_WINNT >= _WIN32_WINNT_WS03) |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 952 | // TODO: When the Android SDK tools increases the Windows system |
| 953 | // requirements >= WinXP SP2, switch to android::base::UTF8ToWide() + GetAddrInfoW(). |
Spencer Low | cc467f1 | 2015-08-02 18:13:54 -0700 | [diff] [blame] | 954 | #else |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 955 | // Otherwise, keep using getaddrinfo(), or do runtime API detection |
| 956 | // with GetProcAddress("GetAddrInfoW"). |
Spencer Low | cc467f1 | 2015-08-02 18:13:54 -0700 | [diff] [blame] | 957 | #endif |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 958 | if (getaddrinfo(host.c_str(), port_str, &hints, &addrinfo_ptr) != 0) { |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 959 | const DWORD err = WSAGetLastError(); |
| 960 | *error = android::base::StringPrintf("cannot resolve host '%s' and port %s: %s", |
| 961 | host.c_str(), port_str, |
| 962 | android::base::SystemErrorCodeToString(err).c_str()); |
| 963 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 964 | D("%s", error->c_str()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 965 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 966 | return -1; |
| 967 | } |
Elliott Hughes | 8ac4599 | 2016-08-08 12:52:37 -0700 | [diff] [blame] | 968 | std::unique_ptr<struct addrinfo, decltype(&freeaddrinfo)> addrinfo(addrinfo_ptr, freeaddrinfo); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 969 | addrinfo_ptr = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 970 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 971 | // TODO: Try all the addresses if there's more than one? This just uses |
| 972 | // the first. Or, could call WSAConnectByName() (Windows Vista and newer) |
| 973 | // which tries all addresses, takes a timeout and more. |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 974 | SOCKET s = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol); |
| 975 | if (s == INVALID_SOCKET) { |
| 976 | const DWORD err = WSAGetLastError(); |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 977 | *error = android::base::StringPrintf("cannot create socket: %s", |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 978 | android::base::SystemErrorCodeToString(err).c_str()); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 979 | D("%s", error->c_str()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 980 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 981 | return -1; |
| 982 | } |
| 983 | f->fh_socket = s; |
| 984 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 985 | // TODO: Implement timeouts for Windows. Seems like the default in theory |
| 986 | // (according to http://serverfault.com/a/671453) and in practice is 21 sec. |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 987 | if (connect(s, addrinfo->ai_addr, addrinfo->ai_addrlen) == SOCKET_ERROR) { |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 988 | // TODO: Use WSAAddressToString or inet_ntop on address. |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 989 | const DWORD err = WSAGetLastError(); |
| 990 | *error = android::base::StringPrintf("cannot connect to %s:%s: %s", host.c_str(), port_str, |
| 991 | android::base::SystemErrorCodeToString(err).c_str()); |
| 992 | D("could not connect to %s:%s:%s: %s", type != SOCK_STREAM ? "udp" : "tcp", host.c_str(), |
| 993 | port_str, error->c_str()); |
| 994 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 995 | return -1; |
| 996 | } |
| 997 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 998 | const int fd = _fh_to_int(f.get()); |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 999 | snprintf(f->name, sizeof(f->name), "%d(net-client:%s%d)", fd, type != SOCK_STREAM ? "udp:" : "", |
| 1000 | port); |
| 1001 | D("host '%s' port %d type %s => fd %d", host.c_str(), port, type != SOCK_STREAM ? "udp" : "tcp", |
| 1002 | fd); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1003 | f.release(); |
| 1004 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1007 | int adb_register_socket(SOCKET s) { |
| 1008 | FH f = _fh_alloc(&_fh_socket_class); |
Casey Dahlin | 20238f2 | 2016-09-21 14:03:39 -0700 | [diff] [blame] | 1009 | f->fh_socket = s; |
| 1010 | return _fh_to_int(f); |
| 1011 | } |
| 1012 | |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 1013 | static bool isBlankStr(const char* str) { |
| 1014 | for (; *str != '\0'; ++str) { |
| 1015 | if (!isblank(*str)) { |
| 1016 | return false; |
| 1017 | } |
| 1018 | } |
| 1019 | return true; |
| 1020 | } |
| 1021 | |
| 1022 | int adb_gethostname(char* name, size_t len) { |
| 1023 | const char* computerName = adb_getenv("COMPUTERNAME"); |
| 1024 | if (computerName && !isBlankStr(computerName)) { |
| 1025 | strncpy(name, computerName, len); |
| 1026 | name[len - 1] = '\0'; |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | wchar_t buffer[MAX_COMPUTERNAME_LENGTH + 1]; |
| 1031 | DWORD size = sizeof(buffer); |
| 1032 | if (!GetComputerNameW(buffer, &size)) { |
| 1033 | return -1; |
| 1034 | } |
| 1035 | std::string name_utf8; |
| 1036 | if (!android::base::WideToUTF8(buffer, &name_utf8)) { |
| 1037 | return -1; |
| 1038 | } |
| 1039 | |
| 1040 | strncpy(name, name_utf8.c_str(), len); |
| 1041 | name[len - 1] = '\0'; |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | int adb_getlogin_r(char* buf, size_t bufsize) { |
| 1046 | wchar_t buffer[UNLEN + 1]; |
| 1047 | DWORD len = sizeof(buffer); |
| 1048 | if (!GetUserNameW(buffer, &len)) { |
| 1049 | return -1; |
| 1050 | } |
| 1051 | |
| 1052 | std::string login; |
| 1053 | if (!android::base::WideToUTF8(buffer, &login)) { |
| 1054 | return -1; |
| 1055 | } |
| 1056 | |
| 1057 | strncpy(buf, login.c_str(), bufsize); |
| 1058 | buf[bufsize - 1] = '\0'; |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1062 | #undef accept |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1063 | int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen) { |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1064 | FH serverfh = _fh_from_int(serverfd, __func__); |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 1065 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1066 | if (!serverfh || serverfh->clazz != &_fh_socket_class) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1067 | D("adb_socket_accept: invalid fd %d", serverfd.get()); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1068 | errno = EBADF; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1069 | return -1; |
| 1070 | } |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 1071 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1072 | unique_fh fh(_fh_alloc(&_fh_socket_class)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1073 | if (!fh) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1074 | PLOG(ERROR) << "adb_socket_accept: failed to allocate accepted socket " |
| 1075 | "descriptor"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1076 | return -1; |
| 1077 | } |
| 1078 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1079 | fh->fh_socket = accept(serverfh->fh_socket, addr, addrlen); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1080 | if (fh->fh_socket == INVALID_SOCKET) { |
Spencer Low | 5c761bd | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 1081 | const DWORD err = WSAGetLastError(); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1082 | LOG(ERROR) << "adb_socket_accept: accept on fd " << serverfd.get() |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1083 | << " failed: " + android::base::SystemErrorCodeToString(err); |
| 1084 | _socket_set_errno(err); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1085 | return -1; |
| 1086 | } |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 1087 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1088 | const int fd = _fh_to_int(fh.get()); |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1089 | snprintf(fh->name, sizeof(fh->name), "%d(accept:%s)", fd, serverfh->name); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1090 | D("adb_socket_accept on fd %d returns fd %d", serverfd.get(), fd); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1091 | fh.release(); |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1092 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1095 | int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval, socklen_t optlen) { |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1096 | FH fh = _fh_from_int(fd, __func__); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1097 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1098 | if (!fh || fh->clazz != &_fh_socket_class) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1099 | D("adb_setsockopt: invalid fd %d", fd.get()); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1100 | errno = EBADF; |
| 1101 | return -1; |
| 1102 | } |
Spencer Low | c7c4561 | 2015-09-29 15:05:29 -0700 | [diff] [blame] | 1103 | |
| 1104 | // TODO: Once we can assume Windows Vista or later, if the caller is trying |
| 1105 | // to set SOL_SOCKET, SO_SNDBUF/SO_RCVBUF, ignore it since the OS has |
| 1106 | // auto-tuning. |
| 1107 | |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1108 | int result = |
| 1109 | setsockopt(fh->fh_socket, level, optname, reinterpret_cast<const char*>(optval), optlen); |
| 1110 | if (result == SOCKET_ERROR) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1111 | const DWORD err = WSAGetLastError(); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1112 | D("adb_setsockopt: setsockopt on fd %d level %d optname %d failed: %s\n", fd.get(), level, |
Josh Gao | 08229f6 | 2018-04-05 18:09:02 -0700 | [diff] [blame] | 1113 | optname, android::base::SystemErrorCodeToString(err).c_str()); |
| 1114 | _socket_set_errno(err); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1115 | result = -1; |
| 1116 | } |
| 1117 | return result; |
| 1118 | } |
| 1119 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1120 | static int adb_getsockname(borrowed_fd fd, struct sockaddr* sockaddr, socklen_t* optlen) { |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1121 | FH fh = _fh_from_int(fd, __func__); |
| 1122 | |
| 1123 | if (!fh || fh->clazz != &_fh_socket_class) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1124 | D("adb_getsockname: invalid fd %d", fd.get()); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1125 | errno = EBADF; |
| 1126 | return -1; |
| 1127 | } |
| 1128 | |
Josh Gao | 4f6f442 | 2017-03-30 13:04:35 -0700 | [diff] [blame] | 1129 | int result = getsockname(fh->fh_socket, sockaddr, optlen); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1130 | if (result == SOCKET_ERROR) { |
| 1131 | const DWORD err = WSAGetLastError(); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1132 | D("adb_getsockname: setsockopt on fd %d failed: %s\n", fd.get(), |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1133 | android::base::SystemErrorCodeToString(err).c_str()); |
| 1134 | _socket_set_errno(err); |
| 1135 | result = -1; |
| 1136 | } |
| 1137 | return result; |
| 1138 | } |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1139 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1140 | int adb_socket_get_local_port(borrowed_fd fd) { |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1141 | sockaddr_storage addr_storage; |
| 1142 | socklen_t addr_len = sizeof(addr_storage); |
| 1143 | |
| 1144 | if (adb_getsockname(fd, reinterpret_cast<sockaddr*>(&addr_storage), &addr_len) < 0) { |
| 1145 | D("adb_socket_get_local_port: adb_getsockname failed: %s", strerror(errno)); |
| 1146 | return -1; |
| 1147 | } |
| 1148 | |
| 1149 | if (!(addr_storage.ss_family == AF_INET || addr_storage.ss_family == AF_INET6)) { |
| 1150 | D("adb_socket_get_local_port: unknown address family received: %d", addr_storage.ss_family); |
| 1151 | errno = ECONNABORTED; |
| 1152 | return -1; |
| 1153 | } |
| 1154 | |
| 1155 | return ntohs(reinterpret_cast<sockaddr_in*>(&addr_storage)->sin_port); |
| 1156 | } |
| 1157 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1158 | int adb_shutdown(borrowed_fd fd, int direction) { |
Josh Gao | 96049b9 | 2018-03-23 13:03:28 -0700 | [diff] [blame] | 1159 | FH f = _fh_from_int(fd, __func__); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1160 | |
| 1161 | if (!f || f->clazz != &_fh_socket_class) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1162 | D("adb_shutdown: invalid fd %d", fd.get()); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1163 | errno = EBADF; |
Spencer Low | 31aafa6 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 1164 | return -1; |
| 1165 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1166 | |
Josh Gao | 96049b9 | 2018-03-23 13:03:28 -0700 | [diff] [blame] | 1167 | D("adb_shutdown: %s", f->name); |
| 1168 | if (shutdown(f->fh_socket, direction) == SOCKET_ERROR) { |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1169 | const DWORD err = WSAGetLastError(); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1170 | D("socket shutdown fd %d failed: %s", fd.get(), |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 1171 | android::base::SystemErrorCodeToString(err).c_str()); |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1172 | _socket_set_errno(err); |
| 1173 | return -1; |
| 1174 | } |
| 1175 | return 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1178 | // Emulate socketpair(2) by binding and connecting to a socket. |
| 1179 | int adb_socketpair(int sv[2]) { |
| 1180 | int server = -1; |
| 1181 | int client = -1; |
| 1182 | int accepted = -1; |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1183 | int local_port = -1; |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1184 | std::string error; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1185 | |
Callum Ryan | 04efea3 | 2019-10-31 07:21:42 -0700 | [diff] [blame] | 1186 | server = network_loopback_server(0, SOCK_STREAM, &error, true); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1187 | if (server < 0) { |
| 1188 | D("adb_socketpair: failed to create server: %s", error.c_str()); |
| 1189 | goto fail; |
David Pursell | 7616ae1 | 2015-09-11 16:06:59 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1192 | local_port = adb_socket_get_local_port(server); |
| 1193 | if (local_port < 0) { |
| 1194 | D("adb_socketpair: failed to get server port number: %s", error.c_str()); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1195 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1196 | } |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1197 | D("adb_socketpair: bound on port %d", local_port); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1198 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1199 | client = network_loopback_client(local_port, SOCK_STREAM, &error); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1200 | if (client < 0) { |
| 1201 | D("adb_socketpair: failed to connect client: %s", error.c_str()); |
| 1202 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1203 | } |
| 1204 | |
Josh Gao | 4f6f442 | 2017-03-30 13:04:35 -0700 | [diff] [blame] | 1205 | accepted = adb_socket_accept(server, nullptr, nullptr); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1206 | if (accepted < 0) { |
Josh Gao | 61eda8d | 2016-02-18 13:43:55 -0800 | [diff] [blame] | 1207 | D("adb_socketpair: failed to accept: %s", strerror(errno)); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1208 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1209 | } |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1210 | adb_close(server); |
| 1211 | sv[0] = client; |
| 1212 | sv[1] = accepted; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1213 | return 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1214 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1215 | fail: |
| 1216 | if (server >= 0) { |
| 1217 | adb_close(server); |
| 1218 | } |
| 1219 | if (client >= 0) { |
| 1220 | adb_close(client); |
| 1221 | } |
| 1222 | if (accepted >= 0) { |
| 1223 | adb_close(accepted); |
| 1224 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1225 | return -1; |
| 1226 | } |
| 1227 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1228 | bool set_file_block_mode(borrowed_fd fd, bool block) { |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1229 | FH fh = _fh_from_int(fd, __func__); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1230 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1231 | if (!fh || !fh->used) { |
| 1232 | errno = EBADF; |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1233 | D("Setting nonblocking on bad file descriptor %d", fd.get()); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1234 | return false; |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 1235 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1236 | |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1237 | if (fh->clazz == &_fh_socket_class) { |
| 1238 | u_long x = !block; |
| 1239 | if (ioctlsocket(fh->u.socket, FIONBIO, &x) != 0) { |
Casey Dahlin | 20238f2 | 2016-09-21 14:03:39 -0700 | [diff] [blame] | 1240 | int error = WSAGetLastError(); |
| 1241 | _socket_set_errno(error); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1242 | D("Setting %d nonblocking failed (%d)", fd.get(), error); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1243 | return false; |
| 1244 | } |
| 1245 | return true; |
Elliott Hughes | 6a09693 | 2015-04-16 16:47:02 -0700 | [diff] [blame] | 1246 | } else { |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1247 | errno = ENOTSOCK; |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1248 | D("Setting nonblocking on non-socket %d", fd.get()); |
Josh Gao | e738812 | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 1249 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1253 | bool set_tcp_keepalive(borrowed_fd fd, int interval_sec) { |
David Pursell | c25a34e | 2016-02-22 14:27:23 -0800 | [diff] [blame] | 1254 | FH fh = _fh_from_int(fd, __func__); |
| 1255 | |
| 1256 | if (!fh || fh->clazz != &_fh_socket_class) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1257 | D("set_tcp_keepalive(%d) failed: invalid fd", fd.get()); |
David Pursell | c25a34e | 2016-02-22 14:27:23 -0800 | [diff] [blame] | 1258 | errno = EBADF; |
| 1259 | return false; |
| 1260 | } |
| 1261 | |
| 1262 | tcp_keepalive keepalive; |
| 1263 | keepalive.onoff = (interval_sec > 0); |
| 1264 | keepalive.keepalivetime = interval_sec * 1000; |
| 1265 | keepalive.keepaliveinterval = interval_sec * 1000; |
| 1266 | |
| 1267 | DWORD bytes_returned = 0; |
| 1268 | if (WSAIoctl(fh->fh_socket, SIO_KEEPALIVE_VALS, &keepalive, sizeof(keepalive), nullptr, 0, |
| 1269 | &bytes_returned, nullptr, nullptr) != 0) { |
| 1270 | const DWORD err = WSAGetLastError(); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1271 | D("set_tcp_keepalive(%d) failed: %s", fd.get(), |
David Pursell | c25a34e | 2016-02-22 14:27:23 -0800 | [diff] [blame] | 1272 | android::base::SystemErrorCodeToString(err).c_str()); |
| 1273 | _socket_set_errno(err); |
| 1274 | return false; |
| 1275 | } |
| 1276 | |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1280 | /**************************************************************************/ |
| 1281 | /**************************************************************************/ |
| 1282 | /***** *****/ |
| 1283 | /***** Console Window Terminal Emulation *****/ |
| 1284 | /***** *****/ |
| 1285 | /**************************************************************************/ |
| 1286 | /**************************************************************************/ |
| 1287 | |
| 1288 | // This reads input from a Win32 console window and translates it into Unix |
| 1289 | // terminal-style sequences. This emulates mostly Gnome Terminal (in Normal |
| 1290 | // mode, not Application mode), which itself emulates xterm. Gnome Terminal |
| 1291 | // is emulated instead of xterm because it is probably more popular than xterm: |
| 1292 | // Ubuntu's default Ctrl-Alt-T shortcut opens Gnome Terminal, Gnome Terminal |
| 1293 | // supports modern fonts, etc. It seems best to emulate the terminal that most |
| 1294 | // Android developers use because they'll fix apps (the shell, etc.) to keep |
| 1295 | // working with that terminal's emulation. |
| 1296 | // |
| 1297 | // The point of this emulation is not to be perfect or to solve all issues with |
| 1298 | // console windows on Windows, but to be better than the original code which |
| 1299 | // just called read() (which called ReadFile(), which called ReadConsoleA()) |
| 1300 | // which did not support Ctrl-C, tab completion, shell input line editing |
| 1301 | // keys, server echo, and more. |
| 1302 | // |
| 1303 | // This implementation reconfigures the console with SetConsoleMode(), then |
| 1304 | // calls ReadConsoleInput() to get raw input which it remaps to Unix |
| 1305 | // terminal-style sequences which is returned via unix_read() which is used |
| 1306 | // by the 'adb shell' command. |
| 1307 | // |
| 1308 | // Code organization: |
| 1309 | // |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 1310 | // * _get_console_handle() and unix_isatty() provide console information. |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1311 | // * stdin_raw_init() and stdin_raw_restore() reconfigure the console. |
| 1312 | // * unix_read() detects console windows (as opposed to pipes, files, etc.). |
| 1313 | // * _console_read() is the main code of the emulation. |
| 1314 | |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 1315 | // Returns a console HANDLE if |fd| is a console, otherwise returns nullptr. |
| 1316 | // If a valid HANDLE is returned and |mode| is not null, |mode| is also filled |
| 1317 | // with the console mode. Requires GENERIC_READ access to the underlying HANDLE. |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1318 | static HANDLE _get_console_handle(borrowed_fd fd, DWORD* mode = nullptr) { |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 1319 | // First check isatty(); this is very fast and eliminates most non-console |
| 1320 | // FDs, but returns 1 for both consoles and character devices like NUL. |
| 1321 | #pragma push_macro("isatty") |
| 1322 | #undef isatty |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1323 | if (!isatty(fd.get())) { |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 1324 | return nullptr; |
| 1325 | } |
| 1326 | #pragma pop_macro("isatty") |
| 1327 | |
| 1328 | // To differentiate between character devices and consoles we need to get |
| 1329 | // the underlying HANDLE and use GetConsoleMode(), which is what requires |
| 1330 | // GENERIC_READ permissions. |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1331 | const intptr_t intptr_handle = _get_osfhandle(fd.get()); |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 1332 | if (intptr_handle == -1) { |
| 1333 | return nullptr; |
| 1334 | } |
| 1335 | const HANDLE handle = reinterpret_cast<const HANDLE>(intptr_handle); |
| 1336 | DWORD temp_mode = 0; |
| 1337 | if (!GetConsoleMode(handle, mode ? mode : &temp_mode)) { |
| 1338 | return nullptr; |
| 1339 | } |
| 1340 | |
| 1341 | return handle; |
| 1342 | } |
| 1343 | |
| 1344 | // Returns a console handle if |stream| is a console, otherwise returns nullptr. |
| 1345 | static HANDLE _get_console_handle(FILE* const stream) { |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 1346 | // Save and restore errno to make it easier for callers to prevent from overwriting errno. |
| 1347 | android::base::ErrnoRestorer er; |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 1348 | const int fd = fileno(stream); |
| 1349 | if (fd < 0) { |
| 1350 | return nullptr; |
| 1351 | } |
| 1352 | return _get_console_handle(fd); |
| 1353 | } |
| 1354 | |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1355 | int unix_isatty(borrowed_fd fd) { |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 1356 | return _get_console_handle(fd) ? 1 : 0; |
| 1357 | } |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1358 | |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1359 | // Get the next KEY_EVENT_RECORD that should be processed. |
| 1360 | static bool _get_key_event_record(const HANDLE console, INPUT_RECORD* const input_record) { |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1361 | for (;;) { |
| 1362 | DWORD read_count = 0; |
| 1363 | memset(input_record, 0, sizeof(*input_record)); |
| 1364 | if (!ReadConsoleInputA(console, input_record, 1, &read_count)) { |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1365 | D("_get_key_event_record: ReadConsoleInputA() failed: %s\n", |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 1366 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1367 | errno = EIO; |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | if (read_count == 0) { // should be impossible |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1372 | LOG(FATAL) << "ReadConsoleInputA returned 0"; |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | if (read_count != 1) { // should be impossible |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1376 | LOG(FATAL) << "ReadConsoleInputA did not return one input record"; |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Spencer Low | 5544140 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 1379 | // If the console window is resized, emulate SIGWINCH by breaking out |
| 1380 | // of read() with errno == EINTR. Note that there is no event on |
| 1381 | // vertical resize because we don't give the console our own custom |
| 1382 | // screen buffer (with CreateConsoleScreenBuffer() + |
| 1383 | // SetConsoleActiveScreenBuffer()). Instead, we use the default which |
| 1384 | // supports scrollback, but doesn't seem to raise an event for vertical |
| 1385 | // window resize. |
| 1386 | if (input_record->EventType == WINDOW_BUFFER_SIZE_EVENT) { |
| 1387 | errno = EINTR; |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1391 | if ((input_record->EventType == KEY_EVENT) && |
| 1392 | (input_record->Event.KeyEvent.bKeyDown)) { |
| 1393 | if (input_record->Event.KeyEvent.wRepeatCount == 0) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 1394 | LOG(FATAL) << "ReadConsoleInputA returned a key event with zero repeat count"; |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | // Got an interesting INPUT_RECORD, so return |
| 1398 | return true; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1403 | static __inline__ bool _is_shift_pressed(const DWORD control_key_state) { |
| 1404 | return (control_key_state & SHIFT_PRESSED) != 0; |
| 1405 | } |
| 1406 | |
| 1407 | static __inline__ bool _is_ctrl_pressed(const DWORD control_key_state) { |
| 1408 | return (control_key_state & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) != 0; |
| 1409 | } |
| 1410 | |
| 1411 | static __inline__ bool _is_alt_pressed(const DWORD control_key_state) { |
| 1412 | return (control_key_state & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) != 0; |
| 1413 | } |
| 1414 | |
| 1415 | static __inline__ bool _is_numlock_on(const DWORD control_key_state) { |
| 1416 | return (control_key_state & NUMLOCK_ON) != 0; |
| 1417 | } |
| 1418 | |
| 1419 | static __inline__ bool _is_capslock_on(const DWORD control_key_state) { |
| 1420 | return (control_key_state & CAPSLOCK_ON) != 0; |
| 1421 | } |
| 1422 | |
| 1423 | static __inline__ bool _is_enhanced_key(const DWORD control_key_state) { |
| 1424 | return (control_key_state & ENHANCED_KEY) != 0; |
| 1425 | } |
| 1426 | |
| 1427 | // Constants from MSDN for ToAscii(). |
| 1428 | static const BYTE TOASCII_KEY_OFF = 0x00; |
| 1429 | static const BYTE TOASCII_KEY_DOWN = 0x80; |
| 1430 | static const BYTE TOASCII_KEY_TOGGLED_ON = 0x01; // for CapsLock |
| 1431 | |
| 1432 | // Given a key event, ignore a modifier key and return the character that was |
| 1433 | // entered without the modifier. Writes to *ch and returns the number of bytes |
| 1434 | // written. |
| 1435 | static size_t _get_char_ignoring_modifier(char* const ch, |
| 1436 | const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state, |
| 1437 | const WORD modifier) { |
| 1438 | // If there is no character from Windows, try ignoring the specified |
| 1439 | // modifier and look for a character. Note that if AltGr is being used, |
| 1440 | // there will be a character from Windows. |
| 1441 | if (key_event->uChar.AsciiChar == '\0') { |
| 1442 | // Note that we read the control key state from the passed in argument |
| 1443 | // instead of from key_event since the argument has been normalized. |
| 1444 | if (((modifier == VK_SHIFT) && |
| 1445 | _is_shift_pressed(control_key_state)) || |
| 1446 | ((modifier == VK_CONTROL) && |
| 1447 | _is_ctrl_pressed(control_key_state)) || |
| 1448 | ((modifier == VK_MENU) && _is_alt_pressed(control_key_state))) { |
| 1449 | |
| 1450 | BYTE key_state[256] = {0}; |
| 1451 | key_state[VK_SHIFT] = _is_shift_pressed(control_key_state) ? |
| 1452 | TOASCII_KEY_DOWN : TOASCII_KEY_OFF; |
| 1453 | key_state[VK_CONTROL] = _is_ctrl_pressed(control_key_state) ? |
| 1454 | TOASCII_KEY_DOWN : TOASCII_KEY_OFF; |
| 1455 | key_state[VK_MENU] = _is_alt_pressed(control_key_state) ? |
| 1456 | TOASCII_KEY_DOWN : TOASCII_KEY_OFF; |
| 1457 | key_state[VK_CAPITAL] = _is_capslock_on(control_key_state) ? |
| 1458 | TOASCII_KEY_TOGGLED_ON : TOASCII_KEY_OFF; |
| 1459 | |
| 1460 | // cause this modifier to be ignored |
| 1461 | key_state[modifier] = TOASCII_KEY_OFF; |
| 1462 | |
| 1463 | WORD translated = 0; |
| 1464 | if (ToAscii(key_event->wVirtualKeyCode, |
| 1465 | key_event->wVirtualScanCode, key_state, &translated, 0) == 1) { |
| 1466 | // Ignoring the modifier, we found a character. |
| 1467 | *ch = (CHAR)translated; |
| 1468 | return 1; |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | // Just use whatever Windows told us originally. |
| 1474 | *ch = key_event->uChar.AsciiChar; |
| 1475 | |
| 1476 | // If the character from Windows is NULL, return a size of zero. |
| 1477 | return (*ch == '\0') ? 0 : 1; |
| 1478 | } |
| 1479 | |
| 1480 | // If a Ctrl key is pressed, lookup the character, ignoring the Ctrl key, |
| 1481 | // but taking into account the shift key. This is because for a sequence like |
| 1482 | // Ctrl-Alt-0, we want to find the character '0' and for Ctrl-Alt-Shift-0, |
| 1483 | // we want to find the character ')'. |
| 1484 | // |
| 1485 | // Note that Windows doesn't seem to pass bKeyDown for Ctrl-Shift-NoAlt-0 |
| 1486 | // because it is the default key-sequence to switch the input language. |
| 1487 | // This is configurable in the Region and Language control panel. |
| 1488 | static __inline__ size_t _get_non_control_char(char* const ch, |
| 1489 | const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state) { |
| 1490 | return _get_char_ignoring_modifier(ch, key_event, control_key_state, |
| 1491 | VK_CONTROL); |
| 1492 | } |
| 1493 | |
| 1494 | // Get without Alt. |
| 1495 | static __inline__ size_t _get_non_alt_char(char* const ch, |
| 1496 | const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state) { |
| 1497 | return _get_char_ignoring_modifier(ch, key_event, control_key_state, |
| 1498 | VK_MENU); |
| 1499 | } |
| 1500 | |
| 1501 | // Ignore the control key, find the character from Windows, and apply any |
| 1502 | // Control key mappings (for example, Ctrl-2 is a NULL character). Writes to |
| 1503 | // *pch and returns number of bytes written. |
| 1504 | static size_t _get_control_character(char* const pch, |
| 1505 | const KEY_EVENT_RECORD* const key_event, const DWORD control_key_state) { |
| 1506 | const size_t len = _get_non_control_char(pch, key_event, |
| 1507 | control_key_state); |
| 1508 | |
| 1509 | if ((len == 1) && _is_ctrl_pressed(control_key_state)) { |
| 1510 | char ch = *pch; |
| 1511 | switch (ch) { |
| 1512 | case '2': |
| 1513 | case '@': |
| 1514 | case '`': |
| 1515 | ch = '\0'; |
| 1516 | break; |
| 1517 | case '3': |
| 1518 | case '[': |
| 1519 | case '{': |
| 1520 | ch = '\x1b'; |
| 1521 | break; |
| 1522 | case '4': |
| 1523 | case '\\': |
| 1524 | case '|': |
| 1525 | ch = '\x1c'; |
| 1526 | break; |
| 1527 | case '5': |
| 1528 | case ']': |
| 1529 | case '}': |
| 1530 | ch = '\x1d'; |
| 1531 | break; |
| 1532 | case '6': |
| 1533 | case '^': |
| 1534 | case '~': |
| 1535 | ch = '\x1e'; |
| 1536 | break; |
| 1537 | case '7': |
| 1538 | case '-': |
| 1539 | case '_': |
| 1540 | ch = '\x1f'; |
| 1541 | break; |
| 1542 | case '8': |
| 1543 | ch = '\x7f'; |
| 1544 | break; |
| 1545 | case '/': |
| 1546 | if (!_is_alt_pressed(control_key_state)) { |
| 1547 | ch = '\x1f'; |
| 1548 | } |
| 1549 | break; |
| 1550 | case '?': |
| 1551 | if (!_is_alt_pressed(control_key_state)) { |
| 1552 | ch = '\x7f'; |
| 1553 | } |
| 1554 | break; |
| 1555 | } |
| 1556 | *pch = ch; |
| 1557 | } |
| 1558 | |
| 1559 | return len; |
| 1560 | } |
| 1561 | |
| 1562 | static DWORD _normalize_altgr_control_key_state( |
| 1563 | const KEY_EVENT_RECORD* const key_event) { |
| 1564 | DWORD control_key_state = key_event->dwControlKeyState; |
| 1565 | |
| 1566 | // If we're in an AltGr situation where the AltGr key is down (depending on |
| 1567 | // the keyboard layout, that might be the physical right alt key which |
| 1568 | // produces a control_key_state where Right-Alt and Left-Ctrl are down) or |
| 1569 | // AltGr-equivalent keys are down (any Ctrl key + any Alt key), and we have |
| 1570 | // a character (which indicates that there was an AltGr mapping), then act |
| 1571 | // as if alt and control are not really down for the purposes of modifiers. |
| 1572 | // This makes it so that if the user with, say, a German keyboard layout |
| 1573 | // presses AltGr-] (which we see as Right-Alt + Left-Ctrl + key), we just |
| 1574 | // output the key and we don't see the Alt and Ctrl keys. |
| 1575 | if (_is_ctrl_pressed(control_key_state) && |
| 1576 | _is_alt_pressed(control_key_state) |
| 1577 | && (key_event->uChar.AsciiChar != '\0')) { |
| 1578 | // Try to remove as few bits as possible to improve our chances of |
| 1579 | // detecting combinations like Left-Alt + AltGr, Right-Ctrl + AltGr, or |
| 1580 | // Left-Alt + Right-Ctrl + AltGr. |
| 1581 | if ((control_key_state & RIGHT_ALT_PRESSED) != 0) { |
| 1582 | // Remove Right-Alt. |
| 1583 | control_key_state &= ~RIGHT_ALT_PRESSED; |
| 1584 | // If uChar is set, a Ctrl key is pressed, and Right-Alt is |
| 1585 | // pressed, Left-Ctrl is almost always set, except if the user |
| 1586 | // presses Right-Ctrl, then AltGr (in that specific order) for |
| 1587 | // whatever reason. At any rate, make sure the bit is not set. |
| 1588 | control_key_state &= ~LEFT_CTRL_PRESSED; |
| 1589 | } else if ((control_key_state & LEFT_ALT_PRESSED) != 0) { |
| 1590 | // Remove Left-Alt. |
| 1591 | control_key_state &= ~LEFT_ALT_PRESSED; |
| 1592 | // Whichever Ctrl key is down, remove it from the state. We only |
| 1593 | // remove one key, to improve our chances of detecting the |
| 1594 | // corner-case of Left-Ctrl + Left-Alt + Right-Ctrl. |
| 1595 | if ((control_key_state & LEFT_CTRL_PRESSED) != 0) { |
| 1596 | // Remove Left-Ctrl. |
| 1597 | control_key_state &= ~LEFT_CTRL_PRESSED; |
| 1598 | } else if ((control_key_state & RIGHT_CTRL_PRESSED) != 0) { |
| 1599 | // Remove Right-Ctrl. |
| 1600 | control_key_state &= ~RIGHT_CTRL_PRESSED; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | // Note that this logic isn't 100% perfect because Windows doesn't |
| 1605 | // allow us to detect all combinations because a physical AltGr key |
| 1606 | // press shows up as two bits, plus some combinations are ambiguous |
| 1607 | // about what is actually physically pressed. |
| 1608 | } |
| 1609 | |
| 1610 | return control_key_state; |
| 1611 | } |
| 1612 | |
| 1613 | // If NumLock is on and Shift is pressed, SHIFT_PRESSED is not set in |
| 1614 | // dwControlKeyState for the following keypad keys: period, 0-9. If we detect |
| 1615 | // this scenario, set the SHIFT_PRESSED bit so we can add modifiers |
| 1616 | // appropriately. |
| 1617 | static DWORD _normalize_keypad_control_key_state(const WORD vk, |
| 1618 | const DWORD control_key_state) { |
| 1619 | if (!_is_numlock_on(control_key_state)) { |
| 1620 | return control_key_state; |
| 1621 | } |
| 1622 | if (!_is_enhanced_key(control_key_state)) { |
| 1623 | switch (vk) { |
| 1624 | case VK_INSERT: // 0 |
| 1625 | case VK_DELETE: // . |
| 1626 | case VK_END: // 1 |
| 1627 | case VK_DOWN: // 2 |
| 1628 | case VK_NEXT: // 3 |
| 1629 | case VK_LEFT: // 4 |
| 1630 | case VK_CLEAR: // 5 |
| 1631 | case VK_RIGHT: // 6 |
| 1632 | case VK_HOME: // 7 |
| 1633 | case VK_UP: // 8 |
| 1634 | case VK_PRIOR: // 9 |
| 1635 | return control_key_state | SHIFT_PRESSED; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | return control_key_state; |
| 1640 | } |
| 1641 | |
| 1642 | static const char* _get_keypad_sequence(const DWORD control_key_state, |
| 1643 | const char* const normal, const char* const shifted) { |
| 1644 | if (_is_shift_pressed(control_key_state)) { |
| 1645 | // Shift is pressed and NumLock is off |
| 1646 | return shifted; |
| 1647 | } else { |
| 1648 | // Shift is not pressed and NumLock is off, or, |
| 1649 | // Shift is pressed and NumLock is on, in which case we want the |
| 1650 | // NumLock and Shift to neutralize each other, thus, we want the normal |
| 1651 | // sequence. |
| 1652 | return normal; |
| 1653 | } |
| 1654 | // If Shift is not pressed and NumLock is on, a different virtual key code |
| 1655 | // is returned by Windows, which can be taken care of by a different case |
| 1656 | // statement in _console_read(). |
| 1657 | } |
| 1658 | |
| 1659 | // Write sequence to buf and return the number of bytes written. |
| 1660 | static size_t _get_modifier_sequence(char* const buf, const WORD vk, |
| 1661 | DWORD control_key_state, const char* const normal) { |
| 1662 | // Copy the base sequence into buf. |
| 1663 | const size_t len = strlen(normal); |
| 1664 | memcpy(buf, normal, len); |
| 1665 | |
| 1666 | int code = 0; |
| 1667 | |
| 1668 | control_key_state = _normalize_keypad_control_key_state(vk, |
| 1669 | control_key_state); |
| 1670 | |
| 1671 | if (_is_shift_pressed(control_key_state)) { |
| 1672 | code |= 0x1; |
| 1673 | } |
| 1674 | if (_is_alt_pressed(control_key_state)) { // any alt key pressed |
| 1675 | code |= 0x2; |
| 1676 | } |
| 1677 | if (_is_ctrl_pressed(control_key_state)) { // any control key pressed |
| 1678 | code |= 0x4; |
| 1679 | } |
| 1680 | // If some modifier was held down, then we need to insert the modifier code |
| 1681 | if (code != 0) { |
| 1682 | if (len == 0) { |
| 1683 | // Should be impossible because caller should pass a string of |
| 1684 | // non-zero length. |
| 1685 | return 0; |
| 1686 | } |
| 1687 | size_t index = len - 1; |
| 1688 | const char lastChar = buf[index]; |
| 1689 | if (lastChar != '~') { |
| 1690 | buf[index++] = '1'; |
| 1691 | } |
| 1692 | buf[index++] = ';'; // modifier separator |
| 1693 | // 2 = shift, 3 = alt, 4 = shift & alt, 5 = control, |
| 1694 | // 6 = shift & control, 7 = alt & control, 8 = shift & alt & control |
| 1695 | buf[index++] = '1' + code; |
| 1696 | buf[index++] = lastChar; // move ~ (or other last char) to the end |
| 1697 | return index; |
| 1698 | } |
| 1699 | return len; |
| 1700 | } |
| 1701 | |
| 1702 | // Write sequence to buf and return the number of bytes written. |
| 1703 | static size_t _get_modifier_keypad_sequence(char* const buf, const WORD vk, |
| 1704 | const DWORD control_key_state, const char* const normal, |
| 1705 | const char shifted) { |
| 1706 | if (_is_shift_pressed(control_key_state)) { |
| 1707 | // Shift is pressed and NumLock is off |
| 1708 | if (shifted != '\0') { |
| 1709 | buf[0] = shifted; |
| 1710 | return sizeof(buf[0]); |
| 1711 | } else { |
| 1712 | return 0; |
| 1713 | } |
| 1714 | } else { |
| 1715 | // Shift is not pressed and NumLock is off, or, |
| 1716 | // Shift is pressed and NumLock is on, in which case we want the |
| 1717 | // NumLock and Shift to neutralize each other, thus, we want the normal |
| 1718 | // sequence. |
| 1719 | return _get_modifier_sequence(buf, vk, control_key_state, normal); |
| 1720 | } |
| 1721 | // If Shift is not pressed and NumLock is on, a different virtual key code |
| 1722 | // is returned by Windows, which can be taken care of by a different case |
| 1723 | // statement in _console_read(). |
| 1724 | } |
| 1725 | |
| 1726 | // The decimal key on the keypad produces a '.' for U.S. English and a ',' for |
| 1727 | // Standard German. Figure this out at runtime so we know what to output for |
| 1728 | // Shift-VK_DELETE. |
| 1729 | static char _get_decimal_char() { |
| 1730 | return (char)MapVirtualKeyA(VK_DECIMAL, MAPVK_VK_TO_CHAR); |
| 1731 | } |
| 1732 | |
| 1733 | // Prefix the len bytes in buf with the escape character, and then return the |
| 1734 | // new buffer length. |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 1735 | static size_t _escape_prefix(char* const buf, const size_t len) { |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1736 | // If nothing to prefix, don't do anything. We might be called with |
| 1737 | // len == 0, if alt was held down with a dead key which produced nothing. |
| 1738 | if (len == 0) { |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | memmove(&buf[1], buf, len); |
| 1743 | buf[0] = '\x1b'; |
| 1744 | return len + 1; |
| 1745 | } |
| 1746 | |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1747 | // Internal buffer to satisfy future _console_read() calls. |
Josh Gao | e3a87d0 | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 1748 | static auto& g_console_input_buffer = *new std::vector<char>(); |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1749 | |
| 1750 | // Writes to buffer buf (of length len), returning number of bytes written or -1 on error. Never |
| 1751 | // returns zero on console closure because Win32 consoles are never 'closed' (as far as I can tell). |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1752 | static int _console_read(const HANDLE console, void* buf, size_t len) { |
| 1753 | for (;;) { |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1754 | // Read of zero bytes should not block waiting for something from the console. |
| 1755 | if (len == 0) { |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | // Flush as much as possible from input buffer. |
| 1760 | if (!g_console_input_buffer.empty()) { |
| 1761 | const int bytes_read = std::min(len, g_console_input_buffer.size()); |
| 1762 | memcpy(buf, g_console_input_buffer.data(), bytes_read); |
| 1763 | const auto begin = g_console_input_buffer.begin(); |
| 1764 | g_console_input_buffer.erase(begin, begin + bytes_read); |
| 1765 | return bytes_read; |
| 1766 | } |
| 1767 | |
| 1768 | // Read from the actual console. This may block until input. |
| 1769 | INPUT_RECORD input_record; |
| 1770 | if (!_get_key_event_record(console, &input_record)) { |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1771 | return -1; |
| 1772 | } |
| 1773 | |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1774 | KEY_EVENT_RECORD* const key_event = &input_record.Event.KeyEvent; |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1775 | const WORD vk = key_event->wVirtualKeyCode; |
| 1776 | const CHAR ch = key_event->uChar.AsciiChar; |
| 1777 | const DWORD control_key_state = _normalize_altgr_control_key_state( |
| 1778 | key_event); |
| 1779 | |
| 1780 | // The following emulation code should write the output sequence to |
| 1781 | // either seqstr or to seqbuf and seqbuflen. |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 1782 | const char* seqstr = nullptr; // NULL terminated C-string |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1783 | // Enough space for max sequence string below, plus modifiers and/or |
| 1784 | // escape prefix. |
| 1785 | char seqbuf[16]; |
| 1786 | size_t seqbuflen = 0; // Space used in seqbuf. |
| 1787 | |
| 1788 | #define MATCH(vk, normal) \ |
| 1789 | case (vk): \ |
| 1790 | { \ |
| 1791 | seqstr = (normal); \ |
| 1792 | } \ |
| 1793 | break; |
| 1794 | |
| 1795 | // Modifier keys should affect the output sequence. |
| 1796 | #define MATCH_MODIFIER(vk, normal) \ |
| 1797 | case (vk): \ |
| 1798 | { \ |
| 1799 | seqbuflen = _get_modifier_sequence(seqbuf, (vk), \ |
| 1800 | control_key_state, (normal)); \ |
| 1801 | } \ |
| 1802 | break; |
| 1803 | |
| 1804 | // The shift key should affect the output sequence. |
| 1805 | #define MATCH_KEYPAD(vk, normal, shifted) \ |
| 1806 | case (vk): \ |
| 1807 | { \ |
| 1808 | seqstr = _get_keypad_sequence(control_key_state, (normal), \ |
| 1809 | (shifted)); \ |
| 1810 | } \ |
| 1811 | break; |
| 1812 | |
| 1813 | // The shift key and other modifier keys should affect the output |
| 1814 | // sequence. |
| 1815 | #define MATCH_MODIFIER_KEYPAD(vk, normal, shifted) \ |
| 1816 | case (vk): \ |
| 1817 | { \ |
| 1818 | seqbuflen = _get_modifier_keypad_sequence(seqbuf, (vk), \ |
| 1819 | control_key_state, (normal), (shifted)); \ |
| 1820 | } \ |
| 1821 | break; |
| 1822 | |
| 1823 | #define ESC "\x1b" |
| 1824 | #define CSI ESC "[" |
| 1825 | #define SS3 ESC "O" |
| 1826 | |
| 1827 | // Only support normal mode, not application mode. |
| 1828 | |
| 1829 | // Enhanced keys: |
| 1830 | // * 6-pack: insert, delete, home, end, page up, page down |
| 1831 | // * cursor keys: up, down, right, left |
| 1832 | // * keypad: divide, enter |
| 1833 | // * Undocumented: VK_PAUSE (Ctrl-NumLock), VK_SNAPSHOT, |
| 1834 | // VK_CANCEL (Ctrl-Pause/Break), VK_NUMLOCK |
| 1835 | if (_is_enhanced_key(control_key_state)) { |
| 1836 | switch (vk) { |
| 1837 | case VK_RETURN: // Enter key on keypad |
| 1838 | if (_is_ctrl_pressed(control_key_state)) { |
| 1839 | seqstr = "\n"; |
| 1840 | } else { |
| 1841 | seqstr = "\r"; |
| 1842 | } |
| 1843 | break; |
| 1844 | |
| 1845 | MATCH_MODIFIER(VK_PRIOR, CSI "5~"); // Page Up |
| 1846 | MATCH_MODIFIER(VK_NEXT, CSI "6~"); // Page Down |
| 1847 | |
| 1848 | // gnome-terminal currently sends SS3 "F" and SS3 "H", but that |
| 1849 | // will be fixed soon to match xterm which sends CSI "F" and |
| 1850 | // CSI "H". https://bugzilla.redhat.com/show_bug.cgi?id=1119764 |
| 1851 | MATCH(VK_END, CSI "F"); |
| 1852 | MATCH(VK_HOME, CSI "H"); |
| 1853 | |
| 1854 | MATCH_MODIFIER(VK_LEFT, CSI "D"); |
| 1855 | MATCH_MODIFIER(VK_UP, CSI "A"); |
| 1856 | MATCH_MODIFIER(VK_RIGHT, CSI "C"); |
| 1857 | MATCH_MODIFIER(VK_DOWN, CSI "B"); |
| 1858 | |
| 1859 | MATCH_MODIFIER(VK_INSERT, CSI "2~"); |
| 1860 | MATCH_MODIFIER(VK_DELETE, CSI "3~"); |
| 1861 | |
| 1862 | MATCH(VK_DIVIDE, "/"); |
| 1863 | } |
| 1864 | } else { // Non-enhanced keys: |
| 1865 | switch (vk) { |
| 1866 | case VK_BACK: // backspace |
| 1867 | if (_is_alt_pressed(control_key_state)) { |
| 1868 | seqstr = ESC "\x7f"; |
| 1869 | } else { |
| 1870 | seqstr = "\x7f"; |
| 1871 | } |
| 1872 | break; |
| 1873 | |
| 1874 | case VK_TAB: |
| 1875 | if (_is_shift_pressed(control_key_state)) { |
| 1876 | seqstr = CSI "Z"; |
| 1877 | } else { |
| 1878 | seqstr = "\t"; |
| 1879 | } |
| 1880 | break; |
| 1881 | |
| 1882 | // Number 5 key in keypad when NumLock is off, or if NumLock is |
| 1883 | // on and Shift is down. |
| 1884 | MATCH_KEYPAD(VK_CLEAR, CSI "E", "5"); |
| 1885 | |
| 1886 | case VK_RETURN: // Enter key on main keyboard |
| 1887 | if (_is_alt_pressed(control_key_state)) { |
| 1888 | seqstr = ESC "\n"; |
| 1889 | } else if (_is_ctrl_pressed(control_key_state)) { |
| 1890 | seqstr = "\n"; |
| 1891 | } else { |
| 1892 | seqstr = "\r"; |
| 1893 | } |
| 1894 | break; |
| 1895 | |
| 1896 | // VK_ESCAPE: Don't do any special handling. The OS uses many |
| 1897 | // of the sequences with Escape and many of the remaining |
| 1898 | // sequences don't produce bKeyDown messages, only !bKeyDown |
| 1899 | // for whatever reason. |
| 1900 | |
| 1901 | case VK_SPACE: |
| 1902 | if (_is_alt_pressed(control_key_state)) { |
| 1903 | seqstr = ESC " "; |
| 1904 | } else if (_is_ctrl_pressed(control_key_state)) { |
| 1905 | seqbuf[0] = '\0'; // NULL char |
| 1906 | seqbuflen = 1; |
| 1907 | } else { |
| 1908 | seqstr = " "; |
| 1909 | } |
| 1910 | break; |
| 1911 | |
| 1912 | MATCH_MODIFIER_KEYPAD(VK_PRIOR, CSI "5~", '9'); // Page Up |
| 1913 | MATCH_MODIFIER_KEYPAD(VK_NEXT, CSI "6~", '3'); // Page Down |
| 1914 | |
| 1915 | MATCH_KEYPAD(VK_END, CSI "4~", "1"); |
| 1916 | MATCH_KEYPAD(VK_HOME, CSI "1~", "7"); |
| 1917 | |
| 1918 | MATCH_MODIFIER_KEYPAD(VK_LEFT, CSI "D", '4'); |
| 1919 | MATCH_MODIFIER_KEYPAD(VK_UP, CSI "A", '8'); |
| 1920 | MATCH_MODIFIER_KEYPAD(VK_RIGHT, CSI "C", '6'); |
| 1921 | MATCH_MODIFIER_KEYPAD(VK_DOWN, CSI "B", '2'); |
| 1922 | |
| 1923 | MATCH_MODIFIER_KEYPAD(VK_INSERT, CSI "2~", '0'); |
| 1924 | MATCH_MODIFIER_KEYPAD(VK_DELETE, CSI "3~", |
| 1925 | _get_decimal_char()); |
| 1926 | |
| 1927 | case 0x30: // 0 |
| 1928 | case 0x31: // 1 |
| 1929 | case 0x39: // 9 |
| 1930 | case VK_OEM_1: // ;: |
| 1931 | case VK_OEM_PLUS: // =+ |
| 1932 | case VK_OEM_COMMA: // ,< |
| 1933 | case VK_OEM_PERIOD: // .> |
| 1934 | case VK_OEM_7: // '" |
| 1935 | case VK_OEM_102: // depends on keyboard, could be <> or \| |
| 1936 | case VK_OEM_2: // /? |
| 1937 | case VK_OEM_3: // `~ |
| 1938 | case VK_OEM_4: // [{ |
| 1939 | case VK_OEM_5: // \| |
| 1940 | case VK_OEM_6: // ]} |
| 1941 | { |
| 1942 | seqbuflen = _get_control_character(seqbuf, key_event, |
| 1943 | control_key_state); |
| 1944 | |
| 1945 | if (_is_alt_pressed(control_key_state)) { |
| 1946 | seqbuflen = _escape_prefix(seqbuf, seqbuflen); |
| 1947 | } |
| 1948 | } |
| 1949 | break; |
| 1950 | |
| 1951 | case 0x32: // 2 |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1952 | case 0x33: // 3 |
| 1953 | case 0x34: // 4 |
| 1954 | case 0x35: // 5 |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1955 | case 0x36: // 6 |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 1956 | case 0x37: // 7 |
| 1957 | case 0x38: // 8 |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1958 | case VK_OEM_MINUS: // -_ |
| 1959 | { |
| 1960 | seqbuflen = _get_control_character(seqbuf, key_event, |
| 1961 | control_key_state); |
| 1962 | |
| 1963 | // If Alt is pressed and it isn't Ctrl-Alt-ShiftUp, then |
| 1964 | // prefix with escape. |
| 1965 | if (_is_alt_pressed(control_key_state) && |
| 1966 | !(_is_ctrl_pressed(control_key_state) && |
| 1967 | !_is_shift_pressed(control_key_state))) { |
| 1968 | seqbuflen = _escape_prefix(seqbuf, seqbuflen); |
| 1969 | } |
| 1970 | } |
| 1971 | break; |
| 1972 | |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 1973 | case 0x41: // a |
| 1974 | case 0x42: // b |
| 1975 | case 0x43: // c |
| 1976 | case 0x44: // d |
| 1977 | case 0x45: // e |
| 1978 | case 0x46: // f |
| 1979 | case 0x47: // g |
| 1980 | case 0x48: // h |
| 1981 | case 0x49: // i |
| 1982 | case 0x4a: // j |
| 1983 | case 0x4b: // k |
| 1984 | case 0x4c: // l |
| 1985 | case 0x4d: // m |
| 1986 | case 0x4e: // n |
| 1987 | case 0x4f: // o |
| 1988 | case 0x50: // p |
| 1989 | case 0x51: // q |
| 1990 | case 0x52: // r |
| 1991 | case 0x53: // s |
| 1992 | case 0x54: // t |
| 1993 | case 0x55: // u |
| 1994 | case 0x56: // v |
| 1995 | case 0x57: // w |
| 1996 | case 0x58: // x |
| 1997 | case 0x59: // y |
| 1998 | case 0x5a: // z |
| 1999 | { |
| 2000 | seqbuflen = _get_non_alt_char(seqbuf, key_event, |
| 2001 | control_key_state); |
| 2002 | |
| 2003 | // If Alt is pressed, then prefix with escape. |
| 2004 | if (_is_alt_pressed(control_key_state)) { |
| 2005 | seqbuflen = _escape_prefix(seqbuf, seqbuflen); |
| 2006 | } |
| 2007 | } |
| 2008 | break; |
| 2009 | |
| 2010 | // These virtual key codes are generated by the keys on the |
| 2011 | // keypad *when NumLock is on* and *Shift is up*. |
| 2012 | MATCH(VK_NUMPAD0, "0"); |
| 2013 | MATCH(VK_NUMPAD1, "1"); |
| 2014 | MATCH(VK_NUMPAD2, "2"); |
| 2015 | MATCH(VK_NUMPAD3, "3"); |
| 2016 | MATCH(VK_NUMPAD4, "4"); |
| 2017 | MATCH(VK_NUMPAD5, "5"); |
| 2018 | MATCH(VK_NUMPAD6, "6"); |
| 2019 | MATCH(VK_NUMPAD7, "7"); |
| 2020 | MATCH(VK_NUMPAD8, "8"); |
| 2021 | MATCH(VK_NUMPAD9, "9"); |
| 2022 | |
| 2023 | MATCH(VK_MULTIPLY, "*"); |
| 2024 | MATCH(VK_ADD, "+"); |
| 2025 | MATCH(VK_SUBTRACT, "-"); |
| 2026 | // VK_DECIMAL is generated by the . key on the keypad *when |
| 2027 | // NumLock is on* and *Shift is up* and the sequence is not |
| 2028 | // Ctrl-Alt-NoShift-. (which causes Ctrl-Alt-Del and the |
| 2029 | // Windows Security screen to come up). |
| 2030 | case VK_DECIMAL: |
| 2031 | // U.S. English uses '.', Germany German uses ','. |
| 2032 | seqbuflen = _get_non_control_char(seqbuf, key_event, |
| 2033 | control_key_state); |
| 2034 | break; |
| 2035 | |
| 2036 | MATCH_MODIFIER(VK_F1, SS3 "P"); |
| 2037 | MATCH_MODIFIER(VK_F2, SS3 "Q"); |
| 2038 | MATCH_MODIFIER(VK_F3, SS3 "R"); |
| 2039 | MATCH_MODIFIER(VK_F4, SS3 "S"); |
| 2040 | MATCH_MODIFIER(VK_F5, CSI "15~"); |
| 2041 | MATCH_MODIFIER(VK_F6, CSI "17~"); |
| 2042 | MATCH_MODIFIER(VK_F7, CSI "18~"); |
| 2043 | MATCH_MODIFIER(VK_F8, CSI "19~"); |
| 2044 | MATCH_MODIFIER(VK_F9, CSI "20~"); |
| 2045 | MATCH_MODIFIER(VK_F10, CSI "21~"); |
| 2046 | MATCH_MODIFIER(VK_F11, CSI "23~"); |
| 2047 | MATCH_MODIFIER(VK_F12, CSI "24~"); |
| 2048 | |
| 2049 | MATCH_MODIFIER(VK_F13, CSI "25~"); |
| 2050 | MATCH_MODIFIER(VK_F14, CSI "26~"); |
| 2051 | MATCH_MODIFIER(VK_F15, CSI "28~"); |
| 2052 | MATCH_MODIFIER(VK_F16, CSI "29~"); |
| 2053 | MATCH_MODIFIER(VK_F17, CSI "31~"); |
| 2054 | MATCH_MODIFIER(VK_F18, CSI "32~"); |
| 2055 | MATCH_MODIFIER(VK_F19, CSI "33~"); |
| 2056 | MATCH_MODIFIER(VK_F20, CSI "34~"); |
| 2057 | |
| 2058 | // MATCH_MODIFIER(VK_F21, ???); |
| 2059 | // MATCH_MODIFIER(VK_F22, ???); |
| 2060 | // MATCH_MODIFIER(VK_F23, ???); |
| 2061 | // MATCH_MODIFIER(VK_F24, ???); |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | #undef MATCH |
| 2066 | #undef MATCH_MODIFIER |
| 2067 | #undef MATCH_KEYPAD |
| 2068 | #undef MATCH_MODIFIER_KEYPAD |
| 2069 | #undef ESC |
| 2070 | #undef CSI |
| 2071 | #undef SS3 |
| 2072 | |
| 2073 | const char* out; |
| 2074 | size_t outlen; |
| 2075 | |
| 2076 | // Check for output in any of: |
| 2077 | // * seqstr is set (and strlen can be used to determine the length). |
| 2078 | // * seqbuf and seqbuflen are set |
| 2079 | // Fallback to ch from Windows. |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 2080 | if (seqstr != nullptr) { |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2081 | out = seqstr; |
| 2082 | outlen = strlen(seqstr); |
| 2083 | } else if (seqbuflen > 0) { |
| 2084 | out = seqbuf; |
| 2085 | outlen = seqbuflen; |
| 2086 | } else if (ch != '\0') { |
| 2087 | // Use whatever Windows told us it is. |
| 2088 | seqbuf[0] = ch; |
| 2089 | seqbuflen = 1; |
| 2090 | out = seqbuf; |
| 2091 | outlen = seqbuflen; |
| 2092 | } else { |
| 2093 | // No special handling for the virtual key code and Windows isn't |
| 2094 | // telling us a character code, then we don't know how to translate |
| 2095 | // the key press. |
| 2096 | // |
| 2097 | // Consume the input and 'continue' to cause us to get a new key |
| 2098 | // event. |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 2099 | D("_console_read: unknown virtual key code: %d, enhanced: %s", |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2100 | vk, _is_enhanced_key(control_key_state) ? "true" : "false"); |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2101 | continue; |
| 2102 | } |
| 2103 | |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 2104 | // put output wRepeatCount times into g_console_input_buffer |
| 2105 | while (key_event->wRepeatCount-- > 0) { |
| 2106 | g_console_input_buffer.insert(g_console_input_buffer.end(), out, out + outlen); |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
Spencer Low | 9c8f746 | 2015-11-10 19:17:16 -0800 | [diff] [blame] | 2109 | // Loop around and try to flush g_console_input_buffer |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | static DWORD _old_console_mode; // previous GetConsoleMode() result |
| 2114 | static HANDLE _console_handle; // when set, console mode should be restored |
| 2115 | |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 2116 | void stdin_raw_init() { |
| 2117 | const HANDLE in = _get_console_handle(STDIN_FILENO, &_old_console_mode); |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2118 | if (in == nullptr) { |
| 2119 | return; |
| 2120 | } |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2121 | |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 2122 | // Disable ENABLE_PROCESSED_INPUT so that Ctrl-C is read instead of |
| 2123 | // calling the process Ctrl-C routine (configured by |
| 2124 | // SetConsoleCtrlHandler()). |
| 2125 | // Disable ENABLE_LINE_INPUT so that input is immediately sent. |
| 2126 | // Disable ENABLE_ECHO_INPUT to disable local echo. Disabling this |
| 2127 | // flag also seems necessary to have proper line-ending processing. |
Spencer Low | 5544140 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 2128 | DWORD new_console_mode = _old_console_mode & ~(ENABLE_PROCESSED_INPUT | |
| 2129 | ENABLE_LINE_INPUT | |
| 2130 | ENABLE_ECHO_INPUT); |
| 2131 | // Enable ENABLE_WINDOW_INPUT to get window resizes. |
| 2132 | new_console_mode |= ENABLE_WINDOW_INPUT; |
| 2133 | |
| 2134 | if (!SetConsoleMode(in, new_console_mode)) { |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 2135 | // This really should not fail. |
| 2136 | D("stdin_raw_init: SetConsoleMode() failed: %s", |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 2137 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2138 | } |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 2139 | |
| 2140 | // Once this is set, it means that stdin has been configured for |
| 2141 | // reading from and that the old console mode should be restored later. |
| 2142 | _console_handle = in; |
| 2143 | |
| 2144 | // Note that we don't need to configure C Runtime line-ending |
| 2145 | // translation because _console_read() does not call the C Runtime to |
| 2146 | // read from the console. |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2147 | } |
| 2148 | |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 2149 | void stdin_raw_restore() { |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 2150 | if (_console_handle != nullptr) { |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 2151 | const HANDLE in = _console_handle; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 2152 | _console_handle = nullptr; // clear state |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2153 | |
Elliott Hughes | a826579 | 2015-11-03 11:18:40 -0800 | [diff] [blame] | 2154 | if (!SetConsoleMode(in, _old_console_mode)) { |
| 2155 | // This really should not fail. |
| 2156 | D("stdin_raw_restore: SetConsoleMode() failed: %s", |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 2157 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | |
Spencer Low | 5544140 | 2015-11-07 17:34:39 -0800 | [diff] [blame] | 2162 | // Called by 'adb shell' and 'adb exec-in' (via unix_read()) to read from stdin. |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 2163 | int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) { |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 2164 | if ((fd == STDIN_FILENO) && (_console_handle != nullptr)) { |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2165 | // If it is a request to read from stdin, and stdin_raw_init() has been |
| 2166 | // called, and it successfully configured the console, then read from |
| 2167 | // the console using Win32 console APIs and partially emulate a unix |
| 2168 | // terminal. |
| 2169 | return _console_read(_console_handle, buf, len); |
| 2170 | } else { |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 2171 | // On older versions of Windows (definitely 7, definitely not 10), |
| 2172 | // ReadConsole() with a size >= 31367 fails, so if |fd| is a console |
David Pursell | 5880536 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 2173 | // we need to limit the read size. |
| 2174 | if (len > 4096 && unix_isatty(fd)) { |
David Pursell | 3fe11f6 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 2175 | len = 4096; |
| 2176 | } |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2177 | // Just call into C Runtime which can read from pipes/files and which |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 2178 | // can do LF/CR translation (which is overridable with _setmode()). |
| 2179 | // Undefine the macro that is set in sysdeps.h which bans calls to |
| 2180 | // plain read() in favor of unix_read() or adb_read(). |
| 2181 | #pragma push_macro("read") |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2182 | #undef read |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 2183 | return read(fd.get(), buf, len); |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 2184 | #pragma pop_macro("read") |
Spencer Low | beb6198 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 2185 | } |
| 2186 | } |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2187 | |
| 2188 | /**************************************************************************/ |
| 2189 | /**************************************************************************/ |
| 2190 | /***** *****/ |
| 2191 | /***** Unicode support *****/ |
| 2192 | /***** *****/ |
| 2193 | /**************************************************************************/ |
| 2194 | /**************************************************************************/ |
| 2195 | |
| 2196 | // This implements support for using files with Unicode filenames and for |
| 2197 | // outputting Unicode text to a Win32 console window. This is inspired from |
| 2198 | // http://utf8everywhere.org/. |
| 2199 | // |
| 2200 | // Background |
| 2201 | // ---------- |
| 2202 | // |
| 2203 | // On POSIX systems, to deal with files with Unicode filenames, just pass UTF-8 |
| 2204 | // filenames to APIs such as open(). This works because filenames are largely |
| 2205 | // opaque 'cookies' (perhaps excluding path separators). |
| 2206 | // |
| 2207 | // On Windows, the native file APIs such as CreateFileW() take 2-byte wchar_t |
| 2208 | // UTF-16 strings. There is an API, CreateFileA() that takes 1-byte char |
| 2209 | // strings, but the strings are in the ANSI codepage and not UTF-8. (The |
| 2210 | // CreateFile() API is really just a macro that adds the W/A based on whether |
| 2211 | // the UNICODE preprocessor symbol is defined). |
| 2212 | // |
| 2213 | // Options |
| 2214 | // ------- |
| 2215 | // |
| 2216 | // Thus, to write a portable program, there are a few options: |
| 2217 | // |
| 2218 | // 1. Write the program with wchar_t filenames (wchar_t path[256];). |
| 2219 | // For Windows, just call CreateFileW(). For POSIX, write a wrapper openW() |
| 2220 | // that takes a wchar_t string, converts it to UTF-8 and then calls the real |
| 2221 | // open() API. |
| 2222 | // |
| 2223 | // 2. Write the program with a TCHAR typedef that is 2 bytes on Windows and |
| 2224 | // 1 byte on POSIX. Make T-* wrappers for various OS APIs and call those, |
| 2225 | // potentially touching a lot of code. |
| 2226 | // |
| 2227 | // 3. Write the program with a 1-byte char filenames (char path[256];) that are |
| 2228 | // UTF-8. For POSIX, just call open(). For Windows, write a wrapper that |
| 2229 | // takes a UTF-8 string, converts it to UTF-16 and then calls the real OS |
| 2230 | // or C Runtime API. |
| 2231 | // |
| 2232 | // The Choice |
| 2233 | // ---------- |
| 2234 | // |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2235 | // The code below chooses option 3, the UTF-8 everywhere strategy. It uses |
| 2236 | // android::base::WideToUTF8() which converts UTF-16 to UTF-8. This is used by the |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2237 | // NarrowArgs helper class that is used to convert wmain() args into UTF-8 |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2238 | // args that are passed to main() at the beginning of program startup. We also use |
| 2239 | // android::base::UTF8ToWide() which converts from UTF-8 to UTF-16. This is used to |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2240 | // implement wrappers below that call UTF-16 OS and C Runtime APIs. |
| 2241 | // |
| 2242 | // Unicode console output |
| 2243 | // ---------------------- |
| 2244 | // |
| 2245 | // The way to output Unicode to a Win32 console window is to call |
| 2246 | // WriteConsoleW() with UTF-16 text. (The user must also choose a proper font |
Spencer Low | cc467f1 | 2015-08-02 18:13:54 -0700 | [diff] [blame] | 2247 | // such as Lucida Console or Consolas, and in the case of East Asian languages |
| 2248 | // (such as Chinese, Japanese, Korean), the user must go to the Control Panel |
| 2249 | // and change the "system locale" to Chinese, etc., which allows a Chinese, etc. |
| 2250 | // font to be used in console windows.) |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2251 | // |
| 2252 | // The problem is getting the C Runtime to make fprintf and related APIs call |
| 2253 | // WriteConsoleW() under the covers. The C Runtime API, _setmode() sounds |
| 2254 | // promising, but the various modes have issues: |
| 2255 | // |
| 2256 | // 1. _setmode(_O_TEXT) (the default) does not use WriteConsoleW() so UTF-8 and |
| 2257 | // UTF-16 do not display properly. |
| 2258 | // 2. _setmode(_O_BINARY) does not use WriteConsoleW() and the text comes out |
| 2259 | // totally wrong. |
| 2260 | // 3. _setmode(_O_U8TEXT) seems to cause the C Runtime _invalid_parameter |
| 2261 | // handler to be called (upon a later I/O call), aborting the process. |
| 2262 | // 4. _setmode(_O_U16TEXT) and _setmode(_O_WTEXT) cause non-wide printf/fprintf |
| 2263 | // to output nothing. |
| 2264 | // |
| 2265 | // So the only solution is to write our own adb_fprintf() that converts UTF-8 |
| 2266 | // to UTF-16 and then calls WriteConsoleW(). |
| 2267 | |
| 2268 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2269 | // Constructor for helper class to convert wmain() UTF-16 args to UTF-8 to |
| 2270 | // be passed to main(). |
| 2271 | NarrowArgs::NarrowArgs(const int argc, wchar_t** const argv) { |
| 2272 | narrow_args = new char*[argc + 1]; |
| 2273 | |
| 2274 | for (int i = 0; i < argc; ++i) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2275 | std::string arg_narrow; |
| 2276 | if (!android::base::WideToUTF8(argv[i], &arg_narrow)) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 2277 | PLOG(FATAL) << "cannot convert argument from UTF-16 to UTF-8"; |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2278 | } |
| 2279 | narrow_args[i] = strdup(arg_narrow.c_str()); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2280 | } |
| 2281 | narrow_args[argc] = nullptr; // terminate |
| 2282 | } |
| 2283 | |
| 2284 | NarrowArgs::~NarrowArgs() { |
| 2285 | if (narrow_args != nullptr) { |
| 2286 | for (char** argp = narrow_args; *argp != nullptr; ++argp) { |
| 2287 | free(*argp); |
| 2288 | } |
| 2289 | delete[] narrow_args; |
| 2290 | narrow_args = nullptr; |
| 2291 | } |
| 2292 | } |
| 2293 | |
Josh Gao | f0c4403 | 2018-12-12 16:12:28 -0800 | [diff] [blame] | 2294 | int unix_open(std::string_view path, int options, ...) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2295 | std::wstring path_wide; |
Josh Gao | f0c4403 | 2018-12-12 16:12:28 -0800 | [diff] [blame] | 2296 | if (!android::base::UTF8ToWide(path.data(), path.size(), &path_wide)) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2297 | return -1; |
| 2298 | } |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2299 | if ((options & O_CREAT) == 0) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2300 | return _wopen(path_wide.c_str(), options); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2301 | } else { |
Josh Gao | f0c4403 | 2018-12-12 16:12:28 -0800 | [diff] [blame] | 2302 | int mode; |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2303 | va_list args; |
| 2304 | va_start(args, options); |
| 2305 | mode = va_arg(args, int); |
| 2306 | va_end(args); |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2307 | return _wopen(path_wide.c_str(), options, mode); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2308 | } |
| 2309 | } |
| 2310 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2311 | // Version of opendir() that takes a UTF-8 path. |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2312 | DIR* adb_opendir(const char* path) { |
| 2313 | std::wstring path_wide; |
| 2314 | if (!android::base::UTF8ToWide(path, &path_wide)) { |
| 2315 | return nullptr; |
| 2316 | } |
| 2317 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2318 | // Just cast _WDIR* to DIR*. This doesn't work if the caller reads any of |
| 2319 | // the fields, but right now all the callers treat the structure as |
| 2320 | // opaque. |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2321 | return reinterpret_cast<DIR*>(_wopendir(path_wide.c_str())); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | // Version of readdir() that returns UTF-8 paths. |
| 2325 | struct dirent* adb_readdir(DIR* dir) { |
| 2326 | _WDIR* const wdir = reinterpret_cast<_WDIR*>(dir); |
| 2327 | struct _wdirent* const went = _wreaddir(wdir); |
| 2328 | if (went == nullptr) { |
| 2329 | return nullptr; |
| 2330 | } |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2331 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2332 | // Convert from UTF-16 to UTF-8. |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2333 | std::string name_utf8; |
| 2334 | if (!android::base::WideToUTF8(went->d_name, &name_utf8)) { |
| 2335 | return nullptr; |
| 2336 | } |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2337 | |
| 2338 | // Cast the _wdirent* to dirent* and overwrite the d_name field (which has |
| 2339 | // space for UTF-16 wchar_t's) with UTF-8 char's. |
| 2340 | struct dirent* ent = reinterpret_cast<struct dirent*>(went); |
| 2341 | |
| 2342 | if (name_utf8.length() + 1 > sizeof(went->d_name)) { |
| 2343 | // Name too big to fit in existing buffer. |
| 2344 | errno = ENOMEM; |
| 2345 | return nullptr; |
| 2346 | } |
| 2347 | |
| 2348 | // Note that sizeof(_wdirent::d_name) is bigger than sizeof(dirent::d_name) |
| 2349 | // because _wdirent contains wchar_t instead of char. So even if name_utf8 |
| 2350 | // can fit in _wdirent::d_name, the resulting dirent::d_name field may be |
| 2351 | // bigger than the caller expects because they expect a dirent structure |
| 2352 | // which has a smaller d_name field. Ignore this since the caller should be |
| 2353 | // resilient. |
| 2354 | |
| 2355 | // Rewrite the UTF-16 d_name field to UTF-8. |
| 2356 | strcpy(ent->d_name, name_utf8.c_str()); |
| 2357 | |
| 2358 | return ent; |
| 2359 | } |
| 2360 | |
| 2361 | // Version of closedir() to go with our version of adb_opendir(). |
| 2362 | int adb_closedir(DIR* dir) { |
| 2363 | return _wclosedir(reinterpret_cast<_WDIR*>(dir)); |
| 2364 | } |
| 2365 | |
| 2366 | // Version of unlink() that takes a UTF-8 path. |
| 2367 | int adb_unlink(const char* path) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2368 | std::wstring wpath; |
| 2369 | if (!android::base::UTF8ToWide(path, &wpath)) { |
| 2370 | return -1; |
| 2371 | } |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2372 | |
| 2373 | int rc = _wunlink(wpath.c_str()); |
| 2374 | |
| 2375 | if (rc == -1 && errno == EACCES) { |
| 2376 | /* unlink returns EACCES when the file is read-only, so we first */ |
| 2377 | /* try to make it writable, then unlink again... */ |
| 2378 | rc = _wchmod(wpath.c_str(), _S_IREAD | _S_IWRITE); |
| 2379 | if (rc == 0) |
| 2380 | rc = _wunlink(wpath.c_str()); |
| 2381 | } |
| 2382 | return rc; |
| 2383 | } |
| 2384 | |
| 2385 | // Version of mkdir() that takes a UTF-8 path. |
| 2386 | int adb_mkdir(const std::string& path, int mode) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2387 | std::wstring path_wide; |
| 2388 | if (!android::base::UTF8ToWide(path, &path_wide)) { |
| 2389 | return -1; |
| 2390 | } |
| 2391 | |
| 2392 | return _wmkdir(path_wide.c_str()); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame^] | 2395 | int adb_rename(const char* oldpath, const char* newpath) { |
| 2396 | std::wstring oldpath_wide, newpath_wide; |
| 2397 | if (!android::base::UTF8ToWide(oldpath, &oldpath_wide)) { |
| 2398 | return -1; |
| 2399 | } |
| 2400 | if (!android::base::UTF8ToWide(newpath, &newpath_wide)) { |
| 2401 | return -1; |
| 2402 | } |
| 2403 | |
| 2404 | // MSDN just says the return value is non-zero on failure, make sure it |
| 2405 | // returns -1 on failure so that it behaves the same as other systems. |
| 2406 | return _wrename(oldpath_wide.c_str(), newpath_wide.c_str()) ? -1 : 0; |
| 2407 | } |
| 2408 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2409 | // Version of utime() that takes a UTF-8 path. |
| 2410 | int adb_utime(const char* path, struct utimbuf* u) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2411 | std::wstring path_wide; |
| 2412 | if (!android::base::UTF8ToWide(path, &path_wide)) { |
| 2413 | return -1; |
| 2414 | } |
| 2415 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2416 | static_assert(sizeof(struct utimbuf) == sizeof(struct _utimbuf), |
| 2417 | "utimbuf and _utimbuf should be the same size because they both " |
| 2418 | "contain the same types, namely time_t"); |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2419 | return _wutime(path_wide.c_str(), reinterpret_cast<struct _utimbuf*>(u)); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | // Version of chmod() that takes a UTF-8 path. |
| 2423 | int adb_chmod(const char* path, int mode) { |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2424 | std::wstring path_wide; |
| 2425 | if (!android::base::UTF8ToWide(path, &path_wide)) { |
| 2426 | return -1; |
| 2427 | } |
| 2428 | |
| 2429 | return _wchmod(path_wide.c_str(), mode); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2432 | // From libutils/Unicode.cpp, get the length of a UTF-8 sequence given the lead byte. |
| 2433 | static inline size_t utf8_codepoint_len(uint8_t ch) { |
| 2434 | return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1; |
| 2435 | } |
Elliott Hughes | 37be38a | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 2436 | |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2437 | namespace internal { |
| 2438 | |
| 2439 | // Given a sequence of UTF-8 bytes (denoted by the range [first, last)), return the number of bytes |
| 2440 | // (from the beginning) that are complete UTF-8 sequences and append the remaining bytes to |
| 2441 | // remaining_bytes. |
| 2442 | size_t ParseCompleteUTF8(const char* const first, const char* const last, |
| 2443 | std::vector<char>* const remaining_bytes) { |
| 2444 | // Walk backwards from the end of the sequence looking for the beginning of a UTF-8 sequence. |
| 2445 | // Current_after points one byte past the current byte to be examined. |
| 2446 | for (const char* current_after = last; current_after != first; --current_after) { |
| 2447 | const char* const current = current_after - 1; |
| 2448 | const char ch = *current; |
| 2449 | const char kHighBit = 0x80u; |
| 2450 | const char kTwoHighestBits = 0xC0u; |
| 2451 | if ((ch & kHighBit) == 0) { // high bit not set |
| 2452 | // The buffer ends with a one-byte UTF-8 sequence, possibly followed by invalid trailing |
| 2453 | // bytes with no leading byte, so return the entire buffer. |
| 2454 | break; |
| 2455 | } else if ((ch & kTwoHighestBits) == kTwoHighestBits) { // top two highest bits set |
| 2456 | // Lead byte in UTF-8 sequence, so check if we have all the bytes in the sequence. |
| 2457 | const size_t bytes_available = last - current; |
| 2458 | if (bytes_available < utf8_codepoint_len(ch)) { |
| 2459 | // We don't have all the bytes in the UTF-8 sequence, so return all the bytes |
| 2460 | // preceding the current incomplete UTF-8 sequence and append the remaining bytes |
| 2461 | // to remaining_bytes. |
| 2462 | remaining_bytes->insert(remaining_bytes->end(), current, last); |
| 2463 | return current - first; |
| 2464 | } else { |
| 2465 | // The buffer ends with a complete UTF-8 sequence, possibly followed by invalid |
| 2466 | // trailing bytes with no lead byte, so return the entire buffer. |
| 2467 | break; |
| 2468 | } |
| 2469 | } else { |
| 2470 | // Trailing byte, so keep going backwards looking for the lead byte. |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | // Return the size of the entire buffer. It is possible that we walked backward past invalid |
| 2475 | // trailing bytes with no lead byte, in which case we want to return all those invalid bytes |
| 2476 | // so that they can be processed. |
| 2477 | return last - first; |
| 2478 | } |
| 2479 | |
| 2480 | } |
| 2481 | |
| 2482 | // Bytes that have not yet been output to the console because they are incomplete UTF-8 sequences. |
| 2483 | // Note that we use only one buffer even though stderr and stdout are logically separate streams. |
| 2484 | // This matches the behavior of Linux. |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2485 | |
| 2486 | // Internal helper function to write UTF-8 bytes to a console. Returns -1 on error. |
| 2487 | static int _console_write_utf8(const char* const buf, const size_t buf_size, FILE* stream, |
| 2488 | HANDLE console) { |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 2489 | static std::mutex& console_output_buffer_lock = *new std::mutex(); |
| 2490 | static auto& console_output_buffer = *new std::vector<char>(); |
| 2491 | |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2492 | const int saved_errno = errno; |
| 2493 | std::vector<char> combined_buffer; |
| 2494 | |
| 2495 | // Complete UTF-8 sequences that should be immediately written to the console. |
| 2496 | const char* utf8; |
| 2497 | size_t utf8_size; |
| 2498 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 2499 | { |
| 2500 | std::lock_guard<std::mutex> lock(console_output_buffer_lock); |
| 2501 | if (console_output_buffer.empty()) { |
| 2502 | // If console_output_buffer doesn't have a buffered up incomplete UTF-8 sequence (the |
| 2503 | // common case with plain ASCII), parse buf directly. |
| 2504 | utf8 = buf; |
| 2505 | utf8_size = internal::ParseCompleteUTF8(buf, buf + buf_size, &console_output_buffer); |
| 2506 | } else { |
| 2507 | // If console_output_buffer has a buffered up incomplete UTF-8 sequence, move it to |
| 2508 | // combined_buffer (and effectively clear console_output_buffer) and append buf to |
| 2509 | // combined_buffer, then parse it all together. |
| 2510 | combined_buffer.swap(console_output_buffer); |
| 2511 | combined_buffer.insert(combined_buffer.end(), buf, buf + buf_size); |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2512 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 2513 | utf8 = combined_buffer.data(); |
| 2514 | utf8_size = internal::ParseCompleteUTF8(utf8, utf8 + combined_buffer.size(), |
| 2515 | &console_output_buffer); |
| 2516 | } |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2517 | } |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2518 | |
| 2519 | std::wstring utf16; |
| 2520 | |
| 2521 | // Try to convert from data that might be UTF-8 to UTF-16, ignoring errors (just like Linux |
| 2522 | // which does not return an error on bad UTF-8). Data might not be UTF-8 if the user cat's |
| 2523 | // random data, runs dmesg (which might have non-UTF-8), etc. |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2524 | // This could throw std::bad_alloc. |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2525 | (void)android::base::UTF8ToWide(utf8, utf8_size, &utf16); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2526 | |
| 2527 | // Note that this does not do \n => \r\n translation because that |
| 2528 | // doesn't seem necessary for the Windows console. For the Windows |
| 2529 | // console \r moves to the beginning of the line and \n moves to a new |
| 2530 | // line. |
| 2531 | |
| 2532 | // Flush any stream buffering so that our output is afterwards which |
| 2533 | // makes sense because our call is afterwards. |
| 2534 | (void)fflush(stream); |
| 2535 | |
| 2536 | // Write UTF-16 to the console. |
| 2537 | DWORD written = 0; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 2538 | if (!WriteConsoleW(console, utf16.c_str(), utf16.length(), &written, nullptr)) { |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2539 | errno = EIO; |
| 2540 | return -1; |
| 2541 | } |
| 2542 | |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2543 | // Return the size of the original buffer passed in, signifying that we consumed it all, even |
| 2544 | // if nothing was displayed, in the case of being passed an incomplete UTF-8 sequence. This |
| 2545 | // matches the Linux behavior. |
| 2546 | errno = saved_errno; |
| 2547 | return buf_size; |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | // Function prototype because attributes cannot be placed on func definitions. |
Elliott Hughes | 874c941 | 2018-06-26 13:06:15 -0700 | [diff] [blame] | 2551 | static int _console_vfprintf(const HANDLE console, FILE* stream, const char* format, va_list ap) |
| 2552 | __attribute__((__format__(__printf__, 3, 0))); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2553 | |
| 2554 | // Internal function to format a UTF-8 string and write it to a Win32 console. |
| 2555 | // Returns -1 on error. |
| 2556 | static int _console_vfprintf(const HANDLE console, FILE* stream, |
| 2557 | const char *format, va_list ap) { |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2558 | const int saved_errno = errno; |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2559 | std::string output_utf8; |
| 2560 | |
| 2561 | // Format the string. |
| 2562 | // This could throw std::bad_alloc. |
| 2563 | android::base::StringAppendV(&output_utf8, format, ap); |
| 2564 | |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2565 | const int result = _console_write_utf8(output_utf8.c_str(), output_utf8.length(), stream, |
| 2566 | console); |
| 2567 | if (result != -1) { |
| 2568 | errno = saved_errno; |
| 2569 | } else { |
| 2570 | // If -1 was returned, errno has been set. |
| 2571 | } |
| 2572 | return result; |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | // Version of vfprintf() that takes UTF-8 and can write Unicode to a |
| 2576 | // Windows console. |
| 2577 | int adb_vfprintf(FILE *stream, const char *format, va_list ap) { |
| 2578 | const HANDLE console = _get_console_handle(stream); |
| 2579 | |
| 2580 | // If there is an associated Win32 console, write to it specially, |
| 2581 | // otherwise defer to the regular C Runtime, passing it UTF-8. |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 2582 | if (console != nullptr) { |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2583 | return _console_vfprintf(console, stream, format, ap); |
| 2584 | } else { |
| 2585 | // If vfprintf is a macro, undefine it, so we can call the real |
| 2586 | // C Runtime API. |
| 2587 | #pragma push_macro("vfprintf") |
| 2588 | #undef vfprintf |
| 2589 | return vfprintf(stream, format, ap); |
| 2590 | #pragma pop_macro("vfprintf") |
| 2591 | } |
| 2592 | } |
| 2593 | |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2594 | // Version of vprintf() that takes UTF-8 and can write Unicode to a Windows console. |
| 2595 | int adb_vprintf(const char *format, va_list ap) { |
| 2596 | return adb_vfprintf(stdout, format, ap); |
| 2597 | } |
| 2598 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2599 | // Version of fprintf() that takes UTF-8 and can write Unicode to a |
| 2600 | // Windows console. |
| 2601 | int adb_fprintf(FILE *stream, const char *format, ...) { |
| 2602 | va_list ap; |
| 2603 | va_start(ap, format); |
| 2604 | const int result = adb_vfprintf(stream, format, ap); |
| 2605 | va_end(ap); |
| 2606 | |
| 2607 | return result; |
| 2608 | } |
| 2609 | |
| 2610 | // Version of printf() that takes UTF-8 and can write Unicode to a |
| 2611 | // Windows console. |
| 2612 | int adb_printf(const char *format, ...) { |
| 2613 | va_list ap; |
| 2614 | va_start(ap, format); |
| 2615 | const int result = adb_vfprintf(stdout, format, ap); |
| 2616 | va_end(ap); |
| 2617 | |
| 2618 | return result; |
| 2619 | } |
| 2620 | |
| 2621 | // Version of fputs() that takes UTF-8 and can write Unicode to a |
| 2622 | // Windows console. |
| 2623 | int adb_fputs(const char* buf, FILE* stream) { |
| 2624 | // adb_fprintf returns -1 on error, which is conveniently the same as EOF |
| 2625 | // which fputs (and hence adb_fputs) should return on error. |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2626 | static_assert(EOF == -1, "EOF is not -1, so this code needs to be fixed"); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2627 | return adb_fprintf(stream, "%s", buf); |
| 2628 | } |
| 2629 | |
| 2630 | // Version of fputc() that takes UTF-8 and can write Unicode to a |
| 2631 | // Windows console. |
| 2632 | int adb_fputc(int ch, FILE* stream) { |
| 2633 | const int result = adb_fprintf(stream, "%c", ch); |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2634 | if (result == -1) { |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2635 | return EOF; |
| 2636 | } |
| 2637 | // For success, fputc returns the char, cast to unsigned char, then to int. |
| 2638 | return static_cast<unsigned char>(ch); |
| 2639 | } |
| 2640 | |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2641 | // Version of putchar() that takes UTF-8 and can write Unicode to a Windows console. |
| 2642 | int adb_putchar(int ch) { |
| 2643 | return adb_fputc(ch, stdout); |
| 2644 | } |
| 2645 | |
| 2646 | // Version of puts() that takes UTF-8 and can write Unicode to a Windows console. |
| 2647 | int adb_puts(const char* buf) { |
| 2648 | // adb_printf returns -1 on error, which is conveniently the same as EOF |
| 2649 | // which puts (and hence adb_puts) should return on error. |
| 2650 | static_assert(EOF == -1, "EOF is not -1, so this code needs to be fixed"); |
| 2651 | return adb_printf("%s\n", buf); |
| 2652 | } |
| 2653 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2654 | // Internal function to write UTF-8 to a Win32 console. Returns the number of |
| 2655 | // items (of length size) written. On error, returns a short item count or 0. |
| 2656 | static size_t _console_fwrite(const void* ptr, size_t size, size_t nmemb, |
| 2657 | FILE* stream, HANDLE console) { |
Spencer Low | f373c35 | 2015-11-15 16:29:36 -0800 | [diff] [blame] | 2658 | const int result = _console_write_utf8(reinterpret_cast<const char*>(ptr), size * nmemb, stream, |
| 2659 | console); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2660 | if (result == -1) { |
| 2661 | return 0; |
| 2662 | } |
| 2663 | return result / size; |
| 2664 | } |
| 2665 | |
| 2666 | // Version of fwrite() that takes UTF-8 and can write Unicode to a |
| 2667 | // Windows console. |
| 2668 | size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream) { |
| 2669 | const HANDLE console = _get_console_handle(stream); |
| 2670 | |
| 2671 | // If there is an associated Win32 console, write to it specially, |
| 2672 | // otherwise defer to the regular C Runtime, passing it UTF-8. |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 2673 | if (console != nullptr) { |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2674 | return _console_fwrite(ptr, size, nmemb, stream, console); |
| 2675 | } else { |
| 2676 | // If fwrite is a macro, undefine it, so we can call the real |
| 2677 | // C Runtime API. |
| 2678 | #pragma push_macro("fwrite") |
| 2679 | #undef fwrite |
| 2680 | return fwrite(ptr, size, nmemb, stream); |
| 2681 | #pragma pop_macro("fwrite") |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | // Version of fopen() that takes a UTF-8 filename and can access a file with |
| 2686 | // a Unicode filename. |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2687 | FILE* adb_fopen(const char* path, const char* mode) { |
| 2688 | std::wstring path_wide; |
| 2689 | if (!android::base::UTF8ToWide(path, &path_wide)) { |
| 2690 | return nullptr; |
| 2691 | } |
| 2692 | |
| 2693 | std::wstring mode_wide; |
| 2694 | if (!android::base::UTF8ToWide(mode, &mode_wide)) { |
| 2695 | return nullptr; |
| 2696 | } |
| 2697 | |
| 2698 | return _wfopen(path_wide.c_str(), mode_wide.c_str()); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2701 | // Return a lowercase version of the argument. Uses C Runtime tolower() on |
| 2702 | // each byte which is not UTF-8 aware, and theoretically uses the current C |
| 2703 | // Runtime locale (which in practice is not changed, so this becomes a ASCII |
| 2704 | // conversion). |
| 2705 | static std::string ToLower(const std::string& anycase) { |
| 2706 | // copy string |
| 2707 | std::string str(anycase); |
| 2708 | // transform the copy |
| 2709 | std::transform(str.begin(), str.end(), str.begin(), tolower); |
| 2710 | return str; |
| 2711 | } |
| 2712 | |
| 2713 | extern "C" int main(int argc, char** argv); |
| 2714 | |
| 2715 | // Link with -municode to cause this wmain() to be used as the program |
| 2716 | // entrypoint. It will convert the args from UTF-16 to UTF-8 and call the |
| 2717 | // regular main() with UTF-8 args. |
| 2718 | extern "C" int wmain(int argc, wchar_t **argv) { |
| 2719 | // Convert args from UTF-16 to UTF-8 and pass that to main(). |
| 2720 | NarrowArgs narrow_args(argc, argv); |
Josh Gao | e08e045 | 2019-06-10 12:48:34 -0700 | [diff] [blame] | 2721 | |
| 2722 | // Avoid destructing NarrowArgs: argv might have been mutated to point to string literals. |
| 2723 | _exit(main(argc, narrow_args.data())); |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2726 | // Shadow UTF-8 environment variable name/value pairs that are created from |
Spencer Low | a090368 | 2018-08-10 16:20:57 -0700 | [diff] [blame] | 2727 | // _wenviron by _init_env(). Note that this is not currently updated if putenv, setenv, unsetenv are |
| 2728 | // called. Note that no thread synchronization is done, but we're called early enough in |
Spencer Low | cc467f1 | 2015-08-02 18:13:54 -0700 | [diff] [blame] | 2729 | // single-threaded startup that things work ok. |
Josh Gao | e3a87d0 | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 2730 | static auto& g_environ_utf8 = *new std::unordered_map<std::string, char*>(); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2731 | |
Spencer Low | a090368 | 2018-08-10 16:20:57 -0700 | [diff] [blame] | 2732 | // Setup shadow UTF-8 environment variables. |
| 2733 | static void _init_env() { |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2734 | // If some name/value pairs exist, then we've already done the setup below. |
| 2735 | if (g_environ_utf8.size() != 0) { |
| 2736 | return; |
| 2737 | } |
| 2738 | |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2739 | if (_wenviron == nullptr) { |
| 2740 | // If _wenviron is null, then -municode probably wasn't used. That |
| 2741 | // linker flag will cause the entry point to setup _wenviron. It will |
| 2742 | // also require an implementation of wmain() (which we provide above). |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 2743 | LOG(FATAL) << "_wenviron is not set, did you link with -municode?"; |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2746 | // Read name/value pairs from UTF-16 _wenviron and write new name/value |
| 2747 | // pairs to UTF-8 g_environ_utf8. Note that it probably does not make sense |
| 2748 | // to use the D() macro here because that tracing only works if the |
| 2749 | // ADB_TRACE environment variable is setup, but that env var can't be read |
| 2750 | // until this code completes. |
| 2751 | for (wchar_t** env = _wenviron; *env != nullptr; ++env) { |
| 2752 | wchar_t* const equal = wcschr(*env, L'='); |
| 2753 | if (equal == nullptr) { |
| 2754 | // Malformed environment variable with no equal sign. Shouldn't |
| 2755 | // really happen, but we should be resilient to this. |
| 2756 | continue; |
| 2757 | } |
| 2758 | |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2759 | // If we encounter an error converting UTF-16, don't error-out on account of a single env |
| 2760 | // var because the program might never even read this particular variable. |
| 2761 | std::string name_utf8; |
| 2762 | if (!android::base::WideToUTF8(*env, equal - *env, &name_utf8)) { |
| 2763 | continue; |
| 2764 | } |
| 2765 | |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2766 | // Store lowercase name so that we can do case-insensitive searches. |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2767 | name_utf8 = ToLower(name_utf8); |
| 2768 | |
| 2769 | std::string value_utf8; |
| 2770 | if (!android::base::WideToUTF8(equal + 1, &value_utf8)) { |
| 2771 | continue; |
| 2772 | } |
| 2773 | |
| 2774 | char* const value_dup = strdup(value_utf8.c_str()); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2775 | |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2776 | // Don't overwrite a previus env var with the same name. In reality, |
| 2777 | // the system probably won't let two env vars with the same name exist |
| 2778 | // in _wenviron. |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2779 | g_environ_utf8.insert({name_utf8, value_dup}); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | // Version of getenv() that takes a UTF-8 environment variable name and |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2784 | // retrieves a UTF-8 value. Case-insensitive to match getenv() on Windows. |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2785 | char* adb_getenv(const char* name) { |
Spencer Low | 50740f5 | 2015-09-08 17:13:04 -0700 | [diff] [blame] | 2786 | // Case-insensitive search by searching for lowercase name in a map of |
| 2787 | // lowercase names. |
| 2788 | const auto it = g_environ_utf8.find(ToLower(std::string(name))); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2789 | if (it == g_environ_utf8.end()) { |
| 2790 | return nullptr; |
| 2791 | } |
| 2792 | |
| 2793 | return it->second; |
| 2794 | } |
| 2795 | |
| 2796 | // Version of getcwd() that returns the current working directory in UTF-8. |
| 2797 | char* adb_getcwd(char* buf, int size) { |
| 2798 | wchar_t* wbuf = _wgetcwd(nullptr, 0); |
| 2799 | if (wbuf == nullptr) { |
| 2800 | return nullptr; |
| 2801 | } |
| 2802 | |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2803 | std::string buf_utf8; |
| 2804 | const bool narrow_result = android::base::WideToUTF8(wbuf, &buf_utf8); |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2805 | free(wbuf); |
| 2806 | wbuf = nullptr; |
| 2807 | |
Spencer Low | 50f5bf1 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 2808 | if (!narrow_result) { |
| 2809 | return nullptr; |
| 2810 | } |
| 2811 | |
Spencer Low | 6815c07 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 2812 | // If size was specified, make sure all the chars will fit. |
| 2813 | if (size != 0) { |
| 2814 | if (size < static_cast<int>(buf_utf8.length() + 1)) { |
| 2815 | errno = ERANGE; |
| 2816 | return nullptr; |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | // If buf was not specified, allocate storage. |
| 2821 | if (buf == nullptr) { |
| 2822 | if (size == 0) { |
| 2823 | size = buf_utf8.length() + 1; |
| 2824 | } |
| 2825 | buf = reinterpret_cast<char*>(malloc(size)); |
| 2826 | if (buf == nullptr) { |
| 2827 | return nullptr; |
| 2828 | } |
| 2829 | } |
| 2830 | |
| 2831 | // Destination buffer was allocated with enough space, or we've already |
| 2832 | // checked an existing buffer size for enough space. |
| 2833 | strcpy(buf, buf_utf8.c_str()); |
| 2834 | |
| 2835 | return buf; |
| 2836 | } |
Spencer Low | ae37a31 | 2018-09-03 16:03:22 -0700 | [diff] [blame] | 2837 | |
Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 2838 | void enable_inherit(borrowed_fd fd) { |
| 2839 | auto osh = adb_get_os_handle(fd); |
| 2840 | const auto h = reinterpret_cast<HANDLE>(osh); |
| 2841 | ::SetHandleInformation(h, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); |
| 2842 | } |
| 2843 | |
| 2844 | void disable_inherit(borrowed_fd fd) { |
| 2845 | auto osh = adb_get_os_handle(fd); |
| 2846 | const auto h = reinterpret_cast<HANDLE>(osh); |
| 2847 | ::SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0); |
| 2848 | } |
| 2849 | |
| 2850 | Process adb_launch_process(std::string_view executable, std::vector<std::string> args, |
| 2851 | std::initializer_list<int> fds_to_inherit) { |
| 2852 | std::wstring wexe; |
| 2853 | if (!android::base::UTF8ToWide(executable.data(), executable.size(), &wexe)) { |
| 2854 | return Process(); |
| 2855 | } |
| 2856 | |
| 2857 | std::wstring wargs = L"\"" + wexe + L"\""; |
| 2858 | std::wstring warg; |
| 2859 | for (auto arg : args) { |
| 2860 | warg.clear(); |
| 2861 | if (!android::base::UTF8ToWide(arg.data(), arg.size(), &warg)) { |
| 2862 | return Process(); |
| 2863 | } |
| 2864 | wargs += L" \""; |
| 2865 | wargs += warg; |
| 2866 | wargs += L'\"'; |
| 2867 | } |
| 2868 | |
| 2869 | STARTUPINFOW sinfo = {sizeof(sinfo)}; |
| 2870 | PROCESS_INFORMATION pinfo = {}; |
| 2871 | |
| 2872 | // TODO: use the Vista+ API to pass the list of inherited handles explicitly; |
| 2873 | // see http://blogs.msdn.com/b/oldnewthing/archive/2011/12/16/10248328.aspx |
| 2874 | for (auto fd : fds_to_inherit) { |
| 2875 | enable_inherit(fd); |
| 2876 | } |
| 2877 | const auto created = CreateProcessW(wexe.c_str(), wargs.data(), |
| 2878 | nullptr, // process attributes |
| 2879 | nullptr, // thread attributes |
| 2880 | fds_to_inherit.size() > 0, // inherit any handles? |
| 2881 | 0, // flags |
| 2882 | nullptr, // environment |
| 2883 | nullptr, // current directory |
| 2884 | &sinfo, // startup info |
| 2885 | &pinfo); |
| 2886 | for (auto fd : fds_to_inherit) { |
| 2887 | disable_inherit(fd); |
| 2888 | } |
| 2889 | |
| 2890 | if (!created) { |
| 2891 | return Process(); |
| 2892 | } |
| 2893 | |
| 2894 | ::CloseHandle(pinfo.hThread); |
| 2895 | return Process(pinfo.hProcess); |
| 2896 | } |
| 2897 | |
Spencer Low | ae37a31 | 2018-09-03 16:03:22 -0700 | [diff] [blame] | 2898 | // The SetThreadDescription API was brought in version 1607 of Windows 10. |
| 2899 | typedef HRESULT(WINAPI* SetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription); |
| 2900 | |
| 2901 | // Based on PlatformThread::SetName() from |
| 2902 | // https://cs.chromium.org/chromium/src/base/threading/platform_thread_win.cc |
| 2903 | int adb_thread_setname(const std::string& name) { |
| 2904 | // The SetThreadDescription API works even if no debugger is attached. |
| 2905 | auto set_thread_description_func = reinterpret_cast<SetThreadDescription>( |
| 2906 | ::GetProcAddress(::GetModuleHandleW(L"Kernel32.dll"), "SetThreadDescription")); |
| 2907 | if (set_thread_description_func) { |
| 2908 | std::wstring name_wide; |
| 2909 | if (!android::base::UTF8ToWide(name.c_str(), &name_wide)) { |
| 2910 | return errno; |
| 2911 | } |
| 2912 | set_thread_description_func(::GetCurrentThread(), name_wide.c_str()); |
| 2913 | } |
| 2914 | |
| 2915 | // Don't use the thread naming SEH exception because we're compiled with -fno-exceptions. |
| 2916 | // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2017 |
| 2917 | |
| 2918 | return 0; |
| 2919 | } |
Spencer Low | a090368 | 2018-08-10 16:20:57 -0700 | [diff] [blame] | 2920 | |
| 2921 | #if !defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
| 2922 | #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 |
| 2923 | #endif |
| 2924 | |
| 2925 | #if !defined(DISABLE_NEWLINE_AUTO_RETURN) |
| 2926 | #define DISABLE_NEWLINE_AUTO_RETURN 0x0008 |
| 2927 | #endif |
| 2928 | |
| 2929 | static void _init_console() { |
| 2930 | DWORD old_out_console_mode; |
| 2931 | |
| 2932 | const HANDLE out = _get_console_handle(STDOUT_FILENO, &old_out_console_mode); |
| 2933 | if (out == nullptr) { |
| 2934 | return; |
| 2935 | } |
| 2936 | |
| 2937 | // Try to use ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output console to process virtual |
| 2938 | // terminal sequences on newer versions of Windows 10 and later. |
| 2939 | // https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences |
| 2940 | // On older OSes that don't support the flag, SetConsoleMode() will return an error. |
| 2941 | // ENABLE_VIRTUAL_TERMINAL_PROCESSING also solves a problem where the last column of the |
| 2942 | // console cannot be overwritten. |
| 2943 | // |
| 2944 | // Note that we don't use DISABLE_NEWLINE_AUTO_RETURN because it doesn't seem to be necessary. |
| 2945 | // If we use DISABLE_NEWLINE_AUTO_RETURN, _console_write_utf8() would need to be modified to |
| 2946 | // translate \n to \r\n. |
| 2947 | if (!SetConsoleMode(out, old_out_console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) { |
| 2948 | return; |
| 2949 | } |
| 2950 | |
| 2951 | // If SetConsoleMode() succeeded, the console supports virtual terminal processing, so we |
| 2952 | // should set the TERM env var to match so that it will be propagated to adbd on devices. |
| 2953 | // |
| 2954 | // Below's direct manipulation of env vars and not g_environ_utf8 assumes that _init_env() has |
| 2955 | // not yet been called. If this fails, _init_env() should be called after _init_console(). |
| 2956 | if (g_environ_utf8.size() > 0) { |
| 2957 | LOG(FATAL) << "environment variables have already been converted to UTF-8"; |
| 2958 | } |
| 2959 | |
| 2960 | #pragma push_macro("getenv") |
| 2961 | #undef getenv |
| 2962 | #pragma push_macro("putenv") |
| 2963 | #undef putenv |
| 2964 | if (getenv("TERM") == nullptr) { |
| 2965 | // This is the same TERM value used by Gnome Terminal and the version of ssh included with |
| 2966 | // Windows. |
| 2967 | putenv("TERM=xterm-256color"); |
| 2968 | } |
| 2969 | #pragma pop_macro("putenv") |
| 2970 | #pragma pop_macro("getenv") |
| 2971 | } |
| 2972 | |
| 2973 | static bool _init_sysdeps() { |
| 2974 | // _init_console() depends on _init_env() not being called yet. |
| 2975 | _init_console(); |
| 2976 | _init_env(); |
| 2977 | _init_winsock(); |
| 2978 | return true; |
| 2979 | } |
| 2980 | |
| 2981 | static bool _sysdeps_init = _init_sysdeps(); |