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