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