Currently adb only realizes a TCP transport has gone away when it tries
to send a packet, which caused problems in particular for `adb reboot`
since no packets are sent, leading to the client hanging until Ctrl+C.
This CL turns on TCP keepalive packets to send 1 packet every second,
allowing up to 10 failures before disconnecting. Using built-in TCP
functionality turns out to be much cleaner in this case than trying to
implement our own keepalive packets at the application layer, and
should be more lightweight since it's all done in the TCP stack.
Bug: http://b/23093474
Change-Id: Ifb41cbb85b9752a9f394e1eed3c6ac4da47a4e4d
Windows has a maximum fd limit of 2048, so we can avoid collision with
real file descriptors by starting from there. Also, fds would be
previously be allocated by a linear walk from the last allocated FD,
instead of the lowest available FD, as required by POSIX. Keep track of
the lowest available file descriptor to make things feel more familiar.
Change-Id: Id6ac1c54f4f7964a6cdfa8d3f4f96262e4881964
Pulls the Windows error string generation out of adb into libbase so
that it can be used by fastboot as well. Also makes a Unix equivalent
that just wraps strerror() so that upcoming fastboot error reporting
code can be platform-independent.
The intent here is just to provide a portable way to report an error to
the user. More general cross-platform error handling is out of scope.
Bug: http://b/26236380
Change-Id: I5a784a844775949562d069bb41dcb0ebd13a32bc
- Introduces unix_read_interruptible() which is like unix_read() except
that it can return EINTR.
- The big idea is that the Windows ReadConsoleInput() API will return an
event on window resize and then we return EINTR from
unix_read_interruptible() just like Unix.
- Only handles horizontal resize since Windows doesn't seem to give an
event for vertical resize when no special screen buffer is used. This
should be sufficient for the primary use case of adb on Windows
(people are not running vi in the first place).
Change-Id: Id8d1710b559834c8098f2d7fbecedf2d0ade4b88
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Previously, the various adb_printf, adb_fwrite, etc. functions did not
correctly handle the case of the passed buffer ending with an incomplete
UTF-8 sequence. This is fixed by buffering up incomplete UTF-8 sequences
in g_console_output_buffer (protected by the mutex
g_console_output_buffer) and outputting it later once the full sequence
is available.
A unittest for the main worker function, ParseCompleteUTF8(), was added
to adb_test.
Other fixes:
- Fix the return value of number-of-chars written to be number of UTF-8
bytes instead of number of UTF-16 characters.
- Don't overwrite errno in success cases of various adb_printf, etc.
functions. This might be excessive, but might be useful in the case
when these functions are used for debugging/tracing.
- Add missing UTF-8 stdio functions that aren't currently used by adb,
but might be in the future: vprintf, putc, putchar, puts.
- stdin_raw_init: If we can't get the console handle, don't call
SetConsoleMode(). Not a big deal, but this will prevent erroneous
trace output.
Change-Id: I8730e8af92882c42b884ad921b39a17b54465085
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Also, fix bugs that this uncovered. In particular, the sysdeps_win32
FATAL macro would only print __FUNCTION__.
Change-Id: I6307ec9749edec21b4fee192e135a86ec445c84b
Now that we have a more standardized API (also available in Chromium),
switch to it. Another benefit is real error handling instead of just
killing the process on invalid Unicode.
Make UTF8ToWide()/WideToUTF8() set errno to EILSEQ on bad input. This is
the same error code that wcsrtombs(3) uses.
Update the unittest to check for EILSEQ.
Change-Id: Ie92acf74d37adaea116cf610c1bf8cd433741e16
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
On exit, these destructors get invoked while other threads might
still be using them, potentially causing a crash, and definitely
causing tsan to report a race condition.
Bug: http://b/23384853
Change-Id: I94de55d22f97f4edd1d7cc1f34e8c1f8dfd56a5a
This reverts commit cc8cd59456.
With the dependency on libcutils (for gettid for non-bionic) removed,
this no longer breaks the build.
Change-Id: I645bd6876e2502ddc1535b69af1e645c0df9d178
28416d6d33 broke adb shell on Windows
because it was calling unix_read() with a buffer size of 1 byte which
wasn't supported by unix_read()/_console_read().
This change makes _console_read() support such a small buffer by
maintaining an internal input buffer.
This lets us simplify the existing code that was already maintaining a
cached INPUT_RECORD.
Also eliminate some duplicate code in the big switch statement.
Change-Id: I8f0aed4fb9f6f2f5b9a6b68ce60d2e368fec81c7
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
ae5a6c06cd made adb push use
android::base::ReadFileToString() for small files, but that API did not
support UTF-8 filenames on Windows, until this fix which does the
following:
- Add android::base::{WideToUTF8,UTF8ToWide}() which are only available
on Windows. The signatures are based on Chromium's APIs of the same
name.
- Add the namespace android::base::utf8 which has versions of APIs that
take UTF-8 strings. To use this, make sure your code is in a namespace
and then do "using namespace android::base::utf8;". On Windows, this will
make calls to open() call android::base::utf8::open(), and on other
platforms, it will just call the regular ::open().
- Make ReadFileToString() and WriteStringToFile() use utf8::open() and
utf8::unlink().
- Adapt unittests from Chromium.
- fastboot needs to link with libcutils because it links with libbase
which depends on libcutils for gettid() for logging.
Change-Id: I1aeac40ff358331d7a1ff457ce894bfb17863904
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Our Windows code has several different ways it checks whether an FD is
a console or not, some of which aren't exactly correct as they may
treat character devices (e.g. NUL) as consoles.
This CL disallows using the builtin isatty() function and provides
unix_isatty() instead which correctly checks these corner cases.
Change-Id: I6d551c745dae691c7eb3446b585265d62c1e62fa
This makes no measurable difference to the sync time; "adb sync" of
everything on /system for a Nexus 9 still takes 20s.
Change-Id: Ifa2626f7453937e43856b9c4ee06e1f5db0aa273
Improved mapping of Winsock error codes to POSIX error codes, especially
WSAECONNABORTED to EPIPE (which WriteFdExactly() looks for) when sending
to a closed socket and WSAECONNRESET to ECONNRESET when the peer resets
the connection.
Use a macro to map strerror() to adb_strerror() which handles these
POSIX error codes that the Windows C Runtime doesn't recognize.
Also:
* Unittest for adb_strerror().
* Don't trace when send() returns WSAEWOULDBLOCK because that is
expected.
Change-Id: If46aeb7b36de3eebfbbccf5478ff5b1bb087714b
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Non-interactive `adb shell` previously only read from the remote shell,
but we want it to write as well so interactive and non-interactive
shells can both send data. With this CL, we can now do:
$ echo foo | adb shell cat
foo
This is primarily usable with newer devices that support the shell_v2
features. Older devices will receive stdin but the shell will still
hang after all input has been sent, requiring user Ctrl+C. This seems
better than closing communication altogether which could potentially
miss an unpredictable amount of return data by closing too early.
Known issue: non-interactive stdin to a PTY shell isn't reliable.
However I don't think this is a common case as ssh doesn't seem to
handle it properly either. Examples:
* echo 'echo foo' | adb shell
* echo 'foo' | adb shell -t cat
Bug: http://b/24565284
Change-Id: I5b017fd12d8478765bb6e8400ea76d535c24ce42
Instead of using socket(..., 0), pass IPPROTO_TCP or IPPROTO_UDP. Using
zero wasn't buying us anything and was different than popular apps like
Chrome. We should stick to what everyone else does so that we don't go
down different code-paths and potentially hit Winsock service provider
issues that everyone else is (accidentally) avoiding.
Also CHECK() if send() returns an erroneous value as described in the
Chromium source.
Also add comment about socket buffer sizing and Windows Vista.
Change-Id: I63db8f6de352fe1e9525cbc9cfc040eb02a4b9cd
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Adds missing #ifdef guards to shell_service_protocol_test.cpp so the
test compiles on Windows.
Also fixes a bug in Windows socketpair write implementation. Previously
it was only checking for a closed pipe if the write happened to block.
This adds an additional pre-check to exit immediately on a closed pipe.
These two changes allow the test to compile and pass on Windows.
Change-Id: Ib8853ed72f015fc0d623da47c32982cb3ffa4a3d
adb_getenv() should be case-insensitive just like the real getenv() on
Windows.
Added a unittest for adb_getenv(). In the process, made adb_test link
with -municode so that the environment block is Unicode.
Move wmain() from main.cpp to sysdeps_win32.cpp so that adb_test could
also use it.
Because wmain() moved, it wasn't as easy to do the runtime check to
verify that -municode was used, so do that check in _ensure_env_setup()
since adb_getenv() is called early in adb anyway.
Added a utility ToLower() which is good enough for env vars whose keys
are probably always ASCII to begin with.
Change-Id: I082f7fdee9dfe2c7f76b878528d2f7863df6d8d1
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Always use LOG() for debug tracing.
Remove useless D_lock. I believe it is useless to lock just before and after fprintf.
I verified the log output both on host and on device. The output looks fine to me.
Change-Id: I96ccfe408ff56864361551afe9ad464d197ae104
When launching the adb server (typically from adb start-server),
redirect stdout/stderr to anonymous pipes which are read by threads in
the parent process, to make error diagnosis easier.
If there is an error during adb start-server, the output looks like:
> adb start-server
* daemon not running. starting it now on port 5037 *
error: could not blah # from server process
could not read ok from ADB Server # from launch_server
* failed to start daemon * # from adb_connect
error: cannot connect to daemon # from adb_commandline
Fix handle-leaks in launch_server by using new unique_handle class
that is based on std::unique_ptr.
In the server, close stdin and redirect to adb.log *before* sending the
ACK, so that any errors are reported early instead of after the ACK.
Change-Id: I943881210a0ea9458fc36851339f916c3d6a0830
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
adb can hang at shutdown due to a deadlock relating to WSACleanup().
This works around the issue by not calling WSACleanup() which shouldn't
be done anyway since threads aren't done using Winsock at shutdown.
A quick way to reproduce the original problem is to run many instances
of adb, many of which will call exit() soon:
for /l %i in (1,1,20) do @start adb nodaemon server
You may have to boost the 20 to 200, or set ADB_TRACE=1 or use Windows
10 instead of Windows 7, to affect the timing, but eventually there
should be hung adb processes with that repro.
A more complete fix to prevent problems like this from occuring in the
future, would be to additionally do the following:
- Investigate all static destructors that are called when exit() is
called.
- If they don't do anything important, switch all calls to exit() to
instead call _exit() and then ban exit() from being called.
Change-Id: Id1be3bf0053809a45f2eca4461e4c35b5ef9388d
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
- handle_forward_request
- Because we have detailed info about which syscall failed (at least
on Win32), use a more generic prefix of "cannot bind listener" followed
by the detailed info.
- install_listener
- Return string errors for a few errors even though I don't think any
callers actually output the string for those errors.
- Remove the printf since the callers print the message themselves.
- adb_main
- LOG(FATAL) calls abort() which on Windows calls the Windows Error
Reporting service which pops up a dialog asking if you want a
crashdump to be uploaded to Microsoft. So really, abort() is
designed for app bugs. Windows isn't the only one doing this, Chromium
also makes LOG(FATAL) crashdump-ready. Since an error here is not
necessarily an app-bug, use a 'normal' error output API like fatal()
which prints an error and just uses exit().
- sysdeps_win32.cpp
- When Winsock APIs fail, make the string clarify which API failed.
Use terse unix-style descriptions (like what you'd get from
cp/mv/dd/etc.).
- Don't trace WSAEWOULDBLOCK from recv() which is a normal occurrence.
- Add a comment about WSAEWOULDBLOCK => EAGAIN.
Change-Id: I58e47f49fa2f6c1b4b92a36d0c4bfe369b456f2a
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Define the UNICODE and _UNICODE preprocessor symbols to make passing
char* to Ansi/Unicode-agnostic Windows and C Runtime APIs break the
build. The solution is to call wide Windows and C Runtime APIs and use
widen(utf8).c_str(). Most code was already calling wide APIs. Defining
these symbols makes a call to CreateEvent() (which previously mapped to
CreateEventA()) turn into a call to CreateEventW().
Make SystemErrorCodeToString() use Unicode.
Add various comments.
Change-Id: I9b212412348a29826718e897a486489e1f142d16
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
When repeatedly opening and closing a file descriptor, the sequence of
fds returned was: 100...227,100,100,100,100,100... Basically, the first
wave was constantly increasing fds, but after the entire fd table was
traversed once, the alloc algorithm would switch to returning the first
free fd. This is sub-optimal for reliability because use-after-free bugs
would be more likely to be hit because right after a close, the same fd
would be given out next.
This change makes the alloc algorithm use a persistent clock hand that
walks forward through the fd table (wrapping around if necessary),
searching for a free fd.
This change adds locking for fd closing:
- This prevents multiple concurrent closes of the same fd.
- There was a race between alloc and close that wasn't guaranteed to be
correct: close would set f->clazz to NULL last, but without any
preceding memory barrier/fence, then the alloc thread would check for
NULL. It probably worked out ok in practice, but it is probably best
to fix this up with a lock (as in this change) or a memory barrier/fence
(but this code isn't about performance, so why go with a complicated
barrier/fence?)
Also in this change:
- Use errno = EMFILE for the out of fds case.
- Clear FH->name
Change-Id: Ic11d2a1a9d53996edfc1ca13566a2f46de4a4316
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Initial support for Unicode file/dir names. Unicode paths can be passed
on the command line, directory enumeration can enumerate Unicode paths,
Unicode paths are used for file access, and Unicode paths can be output
on the console correctly.
Also Unicode environment variable access.
Initial support for Unicode output from adb shell (which uses
adb_fwrite()). This is partial because the corner case of an
adb_fwrite() call with an incomplete UTF-8 multi-byte sequence does not
output correctly, but this should be uncommon, is better than what we
had before (*always* incorrect UTF-8 multi-byte sequences) and can be
fixed in the future.
Calls to Windows APIs with char strings were changed to pass wchar_t
strings to the FooW() variants.
For more details, see the giant comment in sysdeps_win32.cpp.
https://code.google.com/p/android/issues/detail?id=8185
Change-Id: I7ebf6713bb635638b986ccee97b354428837c9c5
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Call getaddrinfo() for connecting to IPv6 destinations.
Winsock APIs do not set errno. WSAGetLastError() returns Winsock errors
that are more numerous than BSD sockets, so it really doesn't make sense
to map those to BSD socket errors. Plus, even if we did that, the
Windows C Runtime (that mingw binaries use) has a strerror() that does
not recognize BSD socket error codes.
The solution is to wrap the various libcutils socket_* APIs with
sysdeps.h network_* APIs. For POSIX, the network_* APIs just call
strerror(). For Windows, they call SystemErrorCodeToString() (adapted
from Chromium).
Also in this change:
- Various other code was modified to return errors in a std::string*
argument, to be able to surface the error string to the end-user.
- Improved error checking and use of D() to log Winsock errors for
improved debuggability.
- For sysdeps_win32.cpp, added unique_fh class that works like
std::unique_ptr, for calling _fh_close().
- Fix win32 adb_socketpair() setting of errno in error case.
- Improve _socket_set_errno() D() logging to reduce confusion. Map
a few extra error codes.
- Move adb_shutdown() lower in sysdeps_win32.cpp so it can call
_socket_set_errno().
- Move network_connect() from adb_utils.cpp to sysdeps.h.
- Merge socket_loopback_server() and socket_inaddr_any_server() into
_network_server() since most of the code was identical.
Change-Id: I945f36870f320578b3a11ba093852ba6f7b93400
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Add missing \n to uses of legacy D() macro. This should make the legacy
logging easier to read (and harder to miss important stuff).
On POSIX, use gettid() from libcutils instead of pthread_self() so that
the output shows a more reasonable number instead of a pointer value.
This should be ok since libbase's logging already uses gettid().
Win32:
Don't let the Win32 last error get overwritten by API calls after the
original error'ing API. When encountering an unknown error, log the
specific error code.
Change-Id: Ib8f72754efa7ba895d2f1cd914251fec2a1d894c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Document the differences between adb_*() and unix_*() in the function
prototypes in sysdeps.h. See the file for the details (CR/LF
translation, well-known file descriptors, etc.).
Fix adb_read(), adb_write(), and adb_close() calls that should really be
unix_read(), unix_write(), and unix_close(). Note that this should have
no impact on unix because on unix, unix_read/unix_write/unix_close are
macros that map to adb_read/adb_write/adb_close.
Improve sysdeps_win32.cpp file descriptor diagnostic logging to output
the name of the function that was passed a bad file descriptor.
Change-Id: I0a1d9c28772656c80bcc303ef8b61fccf4cd637c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Instead of defining and undefining NOGDI:
1. Always #include "base/logging.h" after #include <windows.h>.
Unfortunately, I could not find an easy way to give the user a
warning/error if they include in the wrong order.
2. base/logging.h does #undef ERROR to undefine the evil ERROR macro
that is from another era and probably a bad idea to begin with.
Change-Id: I995d89620611e849af9d7ec47eb55fc0512377f2
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
The adb emu command was never working because the socket connection to
the emulator was closed without reading all of the data that the
emulator sent. On Windows, this caused the emulator's recv() call to
error-out, so it never got the command that was sent.
Before settling on this fix, I also experimented changing the arguments
to the socket shutdown() call and that didn't seem to help. I also tried
removing the call to shutdown() and that didn't help. So that should
rule out shutdown() as the problem. One experiment that helped was
delaying before calling adb_close(), but that is of course fragile and
doesn't address the real issue, which is not closing the socket until
the commands have been read.
https://code.google.com/p/android/issues/detail?id=21021
Change-Id: I8fa4d740a2faa2c9922ec50792e16564a94f6eed
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
This patch factors out a lot of the basic protocol code: sending OKAY,
sending FAIL, and sending a length-prefixed string.
ADB_TRACE has been non-optional for a long time, so let's just remove
the #ifs.
Also actually build the device tracker test tool (and remove its duplicate).
Bug: http://b/20666660
Change-Id: I6c7d59f18707bdc62ca69dea45547617f9f31fc6