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