History log of /system/core/adb/client/
Revision Date Author Comments (<<< Hide modified files) (Show modified files >>>)
85366c15d4e45f453835f2261020acedf6b51bab 07-Jun-2017 Josh Gao <jmgao@google.com> adb: remove SendConnectOnHost.

This logic appears to be racy, and it shouldn't actually be needed, if
our devices follow the USB spec. Use libusb_set_interface_alt_setting
on device initialization as well, to add one more thing that should
reset the data toggles.

Bug: 67320882
Bug: http://b/32952319
Test: python test_device.py
Change-Id: I392198af3d72c524b893e5056afa2b4617cea49c
(cherry picked from commit 4b5d4da6f6a863638c8100cde5af834a83ac8956)
sb_libusb.cpp
c78ecca70b44be14b06f526f17db199773ad160c 13-Sep-2017 Josh Gao <jmgao@google.com> Revert "adb: fix deadlock between transport_unref and usb_close."

This reverts commit 7e197ef83307ffcf2f55d1b8a603d0d3bd2e1d4d.

The mutex lock in transport_unref hides a race that seems otherwise
hard to fix. Specifically, there's no synchronization between acquiring
a transport and attaching it to an asocket*, leading to badness if the
transport is closed in between the two operations.

Fix the original problem the reverted patch addressed by manually
unlocking before calling unregister_usb_transport.

Bug: http://b/65419665
Bug: 64709603 (presubmit balking at the line above)
Test: python test_device.py
Change-Id: I0ed0044129b1671b2c5dd1b9fa2e70a9b4475dc5
(cherry picked from commit e48ecce671a2ab6647cc26a3cb11163aec0b712c)
sb_libusb.cpp
6eadee860798bf4e0be0293450017fbc95ff41c8 15-Jun-2017 Elliott Hughes <enh@google.com> Give a clear error message if we can't start the server.

Bug: http://b/35218713
Test: adb kill-server ; export HOME=/ ; adb devices
Change-Id: I53cc30213be0eab84e42b09c7b501362d6e44b05
ain.cpp
5b8b10e078ef12f21144cdb5fbc932dda47be212 02-Jun-2017 Josh Gao <jmgao@google.com> adb: libusb: switch from polling for accessibility to a sleep.

For unclear reasons, it seems that for some people, devices are
becoming accessible, inaccessible, and then inaccessible again.
Switch to a sleep for now.

Bug: http://b/62200735
Test: manual
Change-Id: Id51dcb188c3534a57c35cbfd30a181c99115a23c
sb_libusb.cpp
7dd382ded948aa5adae3618f99d00c14c3703d05 02-Jun-2017 Josh Gao <jmgao@google.com> adb: improve race between device_connected and device_disconnected.

If a device is unplugged while it's being processed, we can be inside
register_usb_transport still when we call unregister_usb_transport,
leading to bad things happening. Move the call of
register_usb_transport into the scope guarded by usb_handles_mutex.

Bug: http://b/62200735
Test: manual
Change-Id: I2517dcb9d30d9a08cebbb5f715ae3fd30c510109
sb_libusb.cpp
60b8c26520922f296a36efb17b8914b6f1e115ea 06-Jun-2017 Josh Gao <jmgao@google.com> adb: libusb: don't try to delete a usb_handle twice.

Previously, we would attempt to delete a usb_handle in both
device_disconnected and usb_close. If the one in device_disconnected
happened to happen first, usb_close would abort when it failed to find
the handle it was supposed to own.

Bug: http://b/62020217
Test: unplugging device on darwin
Change-Id: I6c6bf61bf89a4d9a23458c00b457080d3d6cc744
sb_libusb.cpp
0b13c89f7a4df0c3105ca2aa4d9406ae4964fd8b 31-May-2017 Josh Gao <jmgao@google.com> adb: reunregress waiting for device on start-server.

Move the invocation of adb_notify_device_scan_complete to
the end of device_connected, where we decrement connecting_devices.
Also, create a dedicated thread for handling hotplug events, instead of
reusing the main thread for this, since the main thread blocks until
device scan is complete.

Test: `adb kill-server; adb devices`
Change-Id: Ia73b1a57538174282a48ef73ab0a3e58152d6f83
sb_libusb.cpp
d1a3e8f1ab653831a3cc4c2a90fb0f8b7a2a95ac 31-May-2017 Josh Gao <jmgao@google.com> adb: fix deadlock.

libusb hotplug callbacks are called with the libusb lock taken, and we
call into libusb with our local mutex, so we need to enforce an ordering
between the two. Instead of calling device_connected or
device_disconnected directly, enqueue them onto the main thread.

Bug: http://b/62200735
Test: manually hotplugged a device
Change-Id: Ic5d55db83b47f4bb60f124ce94ddfe06f5f1a0c6
sb_libusb.cpp
16c5a133ab2b80af2b56f86e42de04a4399c60fc 16-May-2017 Josh Gao <jmgao@google.com> adb: redirect stdout/stderr before initializing usb.

Avoid printing USB logging to the console.

Test: None
Change-Id: Ib8ad0ad6c559689d0f8feec2e66b42380edd26fb
ain.cpp
425aefdcf0f1b2068f56ca091047af5f02ae3f3d 13-May-2017 Josh Gao <jmgao@google.com> adb: libusb: recognize devices with multiple interfaces.

A bug was introduced by commit 8bf37d7a wherein we accidentally only
look at the first interface of a device when checking whether a USB
device was an ADB device or not.

Bug: http://b/38201318
Test: none
Change-Id: I8e8e0963c77cd2cb03538d926ab735f4b57e52b7
sb_libusb.cpp
3f60a968e35b051943249fb9ac70e349323a0590 12-May-2017 Josh Gao <jmgao@google.com> adb: libusb: wait for devices to become accessible.

Android's host linux libusb uses netlink instead of udev for device
hotplug notification, which means we can get hotplug notifications
before udev has updated ownership/perms on the device.

When detecting a new device, poll the device file for a while until we
can access it, before trying to open it.

Bug: http://b/38170349
Test: manually incrased timeout and chmodded a device betwen 0 and 664
Change-Id: I3c714f630940df02b407442592301e2bbb3d9653
sb_libusb.cpp
6da1cd49b5eaeabb3febe584845de6bb3ae45873 12-May-2017 Josh Gao <jmgao@google.com> adb: libusb: switch to hotplug for device detection.

Switch from polling in a loop to using libusb's hotplug API to detect
when devices arrive and leave. Use this to remove devices that were
inaccessible when they're unplugged.

Bug: http://b/38170349
Test: plugged in device
Change-Id: Id157412eb46834debecb0cd45b47b1ced50c2274
sb_libusb.cpp
362b4041f3162ef3d219d6bcebdd5170ae28a662 10-May-2017 Josh Gao <jmgao@google.com> adb: fix darwin build break.

Fix the following build breakage:

system/core/adb/client/usb_osx.cpp:433:13: error: static declaration of 'usb_cleanup' follows non-static declaration
static void usb_cleanup() NO_THREAD_SAFETY_ANALYSIS {
^
system/core/adb/usb.h:48:5: note: previous declaration is here
ADB_USB_INTERFACE(native::usb_handle*);

Bug: http://b/38203892
Test: mma on linux, darwin
Change-Id: I1c042e4d64ddcc0713f329202bc2037e590a9b4f
sb_dispatch.cpp
sb_linux.cpp
sb_osx.cpp
01b7bc43e90b951675cabe88313b96f57b2da712 09-May-2017 Josh Gao <jmgao@google.com> adb: move all cleanup to a function with defined ordering.

We want to explicitly define the order in which we teardown adb, so
move all of the at_quick_exits sprinkled throughout into one function
containing all of the cleanup functions.

Bug: http://b/37104408
Test: adb kill-server; adb start-server
Change-Id: I394f5782eb147e394d4b87df1ba364c061de4b90
ain.cpp
sb_dispatch.cpp
sb_libusb.cpp
sb_windows.cpp
707a6e469f9c9621f09f42c6c5419587580398ab 09-May-2017 Josh Gao <jmgao@google.com> adb: libusb: replace sleep with timed CV wait.

Instead of sleeping for 500ms at the end of every device poll loop, use
a timed condition variable wait so that we can tell the device poll
thread to immediately commit suicide.

Bug: http://b/37869663
Test: adb kill-server; adb start-server
Change-Id: I597071866f7d9ef91900411727345d32c1a97556
sb_libusb.cpp
8bf37d7a4d0b8e05a5fe6500c2e3c13eef2a99e7 06-May-2017 Josh Gao <jmgao@google.com> adb: properly handle libusb_clear_halt failure.

The original code used continue to attempt to try to skip the current
device, but there was an loop between the outside one and the continue.
Move the device handling logic into a function and replace continue
with return.

Test: mma
Change-Id: Iaa7f4b5ddc26d2ce03f1172d37d6307190b44412
sb_libusb.cpp
4acb36020bfa3cb63ed81358754758e76fc5787d 06-May-2017 Elliott Hughes <enh@google.com> Fix macOS build.

system/core/adb/client/usb_libusb.cpp:162:20: error: unused function 'get_device_serial_path' [-Werror,-Wunused-function]
static std::string get_device_serial_path(libusb_device* device) {
^

Bug: N/A
Test: no choice but to submit and see...
Change-Id: I0026d3273891588c27df5ccd71c41ad9907b038d
sb_libusb.cpp
cd35664cdc5097bf38fa2f12aa310855b3c123af 04-May-2017 Elliott Hughes <enh@google.com> Improve udev failure diagnostics.

A couple of folks had trouble understanding the existing message.

Before:

8XV7N15917000596 no permissions (udev requires plugdev group membership); see [http://developer.android.com/tools/device.html]

After:

8XV7N15917000596 no permissions (user buttmunch is not in the plugdev group); see [http://developer.android.com/tools/device.html]

This also fixes a libusb regression where we wouldn't show anything for
devices where we don't have permissions.

Bug: http://b/37707122
Test: ran "adb devices" as user buttmunch
Change-Id: I2fcd735ff4178145432b532a6e4dc8c93b2743fd
sb_libusb.cpp
fd713e53e890f5b3bf26a1c5a980137cc4378c3a 04-May-2017 Josh Gao <jmgao@google.com> adb: wait for devices to come up instead of sleeping for 3s.

Replace a hard-coded 3 second sleep with logic to wait until we've
scanned USB devices once and they've all come online.

Before:
adb shell true 0.00s user 0.00s system 0% cpu 3.047 total

After:
adb shell true 0.00s user 0.00s system 9% cpu 0.041 total

Bug: http://b/37869663
Test: `time adb shell true` after adb kill-server
Change-Id: I251d42afb885908ed9d03167287594ea16650d3f
ain.cpp
sb_libusb.cpp
04a8218f09d342b3d0c4a67fc4df9ad805bc49cc 05-May-2017 Josh Gao <jmgao@google.com> adb: make darwin build with thread safety analysis.

Test: mma on darwin
Change-Id: I44dfc53fc381e60de83f3bc503691cdb8179ca91
sb_osx.cpp
ef3d343254405cc360b4df843c6e4a843c335012 03-May-2017 Josh Gao <jmgao@google.com> adb: use the actual wMaxPacketSize for usb endpoints.

Previously, adb was assuming a fixed maximum packet size of 1024 bytes
(the value for an endpoint connected via USB 3.0). When connected to an
endpoint that has an actual maximum packet size of 512 bytes (i.e.
every single device over USB 2.0), the following could occur:

device sends amessage with 512 byte payload
client reads amessage
client tries to read payload with a length of 1024

In this scenario, the kernel will block, waiting for an additional
packet which won't arrive until something else gets sent across the
wire, which will result in the previous read failing, and the new
packet being dropped.

Bug: http://b/37783561
Test: python test_device.py on linux/darwin, with native/libusb
Change-Id: I556f5344945e22dd1533b076f662a97eea24628e
sb_dispatch.cpp
sb_libusb.cpp
sb_linux.cpp
sb_osx.cpp
sb_windows.cpp
b5e11415d9fdb929321c66889063dac50fb737af 11-Mar-2017 Yabin Cui <yabinc@google.com> adb: fix two device offline problems.

When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.

1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.

The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.

2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.

The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.

To be safe, this patch doesn't change any logic on device.

Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
sb_libusb.cpp
sb_linux.cpp
sb_osx.cpp
sb_windows.cpp
e1dacfc1b65b04958924d1f982263d4aa71b0b94 13-Apr-2017 Josh Gao <jmgao@google.com> adb: kill adb_thread_{create, join, detach, exit}.

We have std::thread now, so we can delete this cruft.

Test: python test_device.py
Test: adb_test
Test: wine adb_test.exe
Test: /data/nativetest/adbd_test/adbd_test
Change-Id: Ie1c1792547b20dec45e2a62ce6515fcb981c3ef8
sb_linux.cpp
sb_osx.cpp
sb_windows.cpp
5d002b8d6ae0a1eeefe09309ae6687a16e82ed08 04-Apr-2017 Yabin Cui <yabinc@google.com> adb: don't reset usb when connecting it.

Calling libusb_reset_device() resets the usb connection with device
on linux platform. It can interfere with other usb interfaces and only
works on linux.
Removing it only affects when adb server is killed while doing IO with
device, which will be solved in later patches.

Bug: http://b/32952319
Test: none
Change-Id: I19522435eadd938ff95d49cbae3bcaa5c8f6d184
sb_libusb.cpp
13a269ea24a98111d2e84a7ffa3c05ab8a4e73a9 23-Jun-2016 Casey Dahlin <sadmac@google.com> Add mDNS device discovery for adb client

Test: Was able to discover a raspberry pi.
Bug: 28074466
(cherry picked from e292cd16760321fccc99c8c261cb92fa4b6462ab)

Change-Id: Id9571576457a4a0a078e48a274a4e8eac78bfe2b
ain.cpp
5d1756ceb58246a721e10464f788e81cdcdc455a 23-Feb-2017 Josh Gao <jmgao@google.com> adb: add `adb host-features`, report libusb status.

Add a 'host-features' command to get the features of the currently
running host adb server. Abuse it to report libusb status.

Bug: http://b/34983123
Test: adb host-features; adb kill-server; ADB_LIBUSB=1 adb start-server; adb host-features
Change-Id: I0e8d503a2dbdff9002ebb6ce8a298498a9421422
sb_dispatch.cpp
aced420f20538eb64137e154b1b5e9a00551ffce 01-Feb-2017 Nick Kralevich <nnk@google.com> usb_dispatch.cpp: Lower severity of logging statement.

The first time you run adb, it starts the adbd server and annoyingly
generates the following message:

adb I 02-01 11:57:58 99717 99717 usb_dispatch.cpp:30] using native
backend

Drop the severity of the message to avoid visible spammy messages only
intended for debugging.

Test: code compiles and no more log message in normal adb use
Change-Id: I882092a6cd14bc67980d88416397d51ab8393b1b
sb_dispatch.cpp
06766a8b291e74c120b94a54052f5032adf5af48 26-Jan-2017 Josh Gao <jmgao@google.com> adb: fix darwin build.

Bug: none
Test: mma on mac
Change-Id: I49ef3c4892c7942a235d317d90386c7d8a872868
sb_osx.cpp
1c70e1bcbcced190b351d4fb418f32b4e428f496 28-Sep-2016 Josh Gao <jmgao@google.com> adb: add libusb implementation for Linux/Darwin.

Add a libusb-based implementation alongside the existing native
implementations, controlled by the ADB_LIBUSB environment variable.

Windows will need more work for the usb driver.

Bug: http://b/31321337
Test: python test_device.py on linux/darwin, with ADB_LIBUSB=0 and 1
Change-Id: Ib68fb2c6c05475eae3ff4cc19f55802a6f489bb7
ain.cpp
sb_dispatch.cpp
sb_libusb.cpp
sb_linux.cpp
456e1ad42034316cee5a6467c4bcb23643b51e5a 19-Jan-2017 Josh Gao <jmgao@google.com> adb: move usb_* to client/usb_*.

Bug: http://b/31321337
Test: mma
Change-Id: I21eefab953737e87d211959616ac4f48926dd92c
sb_linux.cpp
sb_osx.cpp
sb_windows.cpp
f0f854bc76458b10b6b3ed88655fd87cd156dc70 13-Dec-2016 Josh Gao <jmgao@google.com> adb: retry install_listener for a while.

install_listener can fail if we told a previous adb server to quit and
it hasn't finished doing so yet. Retry it for a few hundred
milliseconds to avoid this.

Bug: http://b/28618716
Test: nc -l 5037; adb server nodaemon
Change-Id: Ibbda8f2718b85a2b6a08985aa8d29aa2204a3ead
ain.cpp
3562fd0fa85ca59e5650f1fa21ae1f1e0cd9cf46 22-Sep-2016 Treehugger Robot <treehugger-gerrit@google.com> Merge "Ignore setsid error in some cases."
0cd3ae1c281fa890389987cd05b2bf01cdf1e4c5 21-Sep-2016 Josh Gao <jmgao@google.com> adb: kill adb_mutex_t, adb_cond_t.

Now that we have support for std::mutex and std::condition_variable on
Windows, remove our mutex compatibility layer in favor of the C++ one.

Bug: http://b/31653591
Test: mma && $ANDROID_HOST_OUT/nativetest64/adb_test/adb_test && \
python test_adb.py && python test_device.py
(also on Windows)

Change-Id: I5b7ed9c45cc2a32edcf4e77b56dc28e441f15f34
ain.cpp
c8fab89f1b5ae8c3752979e1a1004eda8b185687 21-Sep-2016 Tao Wu <lepton@google.com> Ignore setsid error in some cases.

If the calling process has already been a leading process of session.
setsid just fail with EPERM, ignore such error.

Test: killall adb;exec 3>f;adb fork-server server --reply-fd 3 & cat f
Change-Id: I1aeac079f29e10aa63ed724b5a43663f25c25ad5
Signed-off-by: Tao Wu <lepton@google.com>
ain.cpp
9c869b58a8cf4f7c3bc88931fbd27d3f5187b2db 26-Aug-2016 Josh Gao <jmgao@google.com> adb: allow use of arbitrary socket specs for command socket.

Bug: http://b/30445394
Change-Id: I474ede35ec3c56ad86da503c9703f83ef5e80862
ain.cpp
698e065e14840541f218a80241fa880703a7dbc5 20-Jul-2016 Felipe Leme <felipeal@google.com> DO NOT MERGE: Split bugreport() into its own file and added unit tests.

bugreport() will be soon refactored to track progress, which will
require more comprehensive unit tests.

As such, it's better to move it to its own files, which in turn also
requires moving send_shell_command() and usage() to commandline.h.

Fixes: 30100363
Bug: 30268737

Change-Id: I3cdf114a0b5547293320042ff0749a60886440b0
(cherry picked from commit 78e0963e4bce9cc9f0bbf0b686004ba15b1e3929)
(cherry picked from commit 218e1ff75998052c7bb30b483c15e75a853283a8)
ain.cpp
0aeb50500c76ea67d6f452907f5503d590e81a54 30-Jun-2016 Elliott Hughes <enh@google.com> Clean up key handling in adb.

This includes the locking we need to be able to re-load the keys at runtime.

We should rename "adb_auth_client.cpp" to "adb_auth_adbd.cpp" or
"adbd_auth.cpp" in a later change.

Change-Id: I9e1d5b6b7d0497d6f6e5d9c4fb660118cdff05a8
Test: "adb devices" works against a non-AOSP device with $ADB_VENDOR_KEYS set, says "unauthorized" without.
Bug: http://b/29273531
ain.cpp
d89a6c2285b64320918b25b1c818da24dd4d02b4 07-Jun-2016 Elliott Hughes <enh@google.com> Allow multiple (sequential) adb users on Linux.

Before this, adb will fail to start for the second user who tries because
/tmp/adb.log already exists and isn't writable by the second user.

Also allow $TMPDIR to override the use of /tmp.

Bug: https://code.google.com/p/android/issues/detail?id=211420
Change-Id: Ic53da981ac0fa45bfed62e7b351d75dca0540235
ain.cpp
eaae97e127717750b4264d9b6617b845f9bc701f 07-Apr-2016 David Pursell <dpursell@google.com> adb: support forwarding TCP port 0.

This CL adds support to forward or reverse TCP port 0 to allow the
system to automatically select an open port. The resolved port number
will be printed to stdout:
$ adb forward tcp:0 tcp:8000
12345
$ adb reverse tcp:0 tcp:9000
23456
This allows testing to be more robust by not hardcoding TCP ports which
may already be in use.

Forwarding port 0 is a host-only change and will work with any device,
but reversing port 0 requires the device to be updated with a new adbd
binary.

This CL also does a little bit of cleanup such as moving the alistener
class out of adb.h, and adds some error checking and additional tests.

Bug: 28051746
Test: python -m unittest discover
Test: adb_test
Test: `adb forward` and `adb reverse` with tcp:0
Change-Id: Icaa87346685b403ab5da7f0e6aa186aa091da572
ain.cpp
6bf323b97a11194d6186f8db2ee4a5eaca8d0141 09-Feb-2016 Yabin Cui <yabinc@google.com> adb: setsid() for adb host server.

To create a daemon for adb host server, we should call setsid()
for the daemon process. However, previously we call setsid() for
the adb client process, which results in nothing but EPERM error.

Bug: 26982628
Change-Id: I2763ae3d5a243706927d7ef6af5095138c0ce2d8
ain.cpp
b72b3f8c92a0b2d4377d5bec15eb45869b8711ac 04-Feb-2016 Josh Gao <jmgao@google.com> adb: make ctrl-c when spawning a daemon not kill the daemon.

Previously, using ctrl-c in a command that needs to spawn a daemon
because one isn't already available would kill the daemon along with the
foreground process.

Bug: http://b/26982628
Change-Id: I7fefc531c3e4895423e7b466322b5426d01dc9ef
ain.cpp
5f787ed2b3b9f6cc02aa5923b95d77e2a5865438 27-Jan-2016 David Pursell <dpursell@google.com> base: add SystemErrorCodeToString() function.

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
ain.cpp
aae38d9704a6c5a2a7dfd9b628bd89822f1cbb60 10-Dec-2015 Josh Gao <jmgao@google.com> Merge "adb: shell: add -n flag to not read from stdin."
7d586073609723cb2f6ed37de0ad1a7996e621ae 21-Nov-2015 Josh Gao <jmgao@google.com> adb: shell: add -n flag to not read from stdin.

Shell scripts of the following form do not work properly with adb:
echo "foo\nbar\nbaz" | {
read FOO
while [ "$FOO" != "" ]; do
adb shell echo $FOO
read FOO
done
}
The first run of adb shell will consume all of the contents of stdin,
causing the loop to immediately end. ssh solves this by providing a -n
flag that causes it to not read from stdin. This commit adds the same.

Bug: http://b/25817224
Change-Id: Id74ca62ef520bcf03678b50f4bf203916fd81038
ain.cpp
4f71319df011d796a60a43fc1bc68e16fbf7d321 05-Dec-2015 Elliott Hughes <enh@google.com> Track rename of base/ to android-base/.

Change-Id: Idf9444fece4aa89c93e15640de59a91f6e758ccf
ain.cpp
65433da1cbd8d2869a60ce3bf18b6b440461741d 19-Nov-2015 Elliott Hughes <enh@google.com> Avoid SIGPIPE in adb.

We're now able to send packets faster than the device can handle them,
meaning that sometimes we're several packets through before the device
says "hey, wait, I can't write" and closes the connection. At best this
led to us reporting that we couldn't sync because "Connection reset";
at worst we'd get SIGPIPE because we were still streaming to a connection
that had already been closed.

This change renames adb_main adb_server_main, and moves the ignoring of
SIGPIPE into adb_commandline so it applies to both client and server (but
not adbd).

This change doesn't address the "wrong error message" part of the problem,
but at least it means you'll get *an* error message.

Bug: http://b/25230872
Change-Id: Ic60e4d13ed03fdcdf0d5cbc97201ebd1097c16ed
ain.cpp
d21dc825bbecad6ce480c5e5c574cc77eadcd779 13-Nov-2015 Spencer Low <CompareAndSwap@gmail.com> adb: win32: remove widen()/narrow() in favor of UTF8ToWide()/WideToUTF8()

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>
ain.cpp
aed3c61c4437ebb05eadfb3bf85d6962c30b9935 23-Sep-2015 Yabin Cui <yabinc@google.com> Adb: use VLOG() to replace D() for verbose logging.

As there are too many D(), we can keep both VLOG() and D() now, and get
rid of D() gradually.

Change-Id: I2f1cb70bcab3e82c99fed939341d03f6b2216076
ain.cpp
e6ae5735713f172eff39f9b8bc75b478b50aee7f 09-Sep-2015 Spencer Low <CompareAndSwap@gmail.com> adb: win32: make adb_getenv() case-insensitive

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>
ain.cpp
7a3f8d6691b3fbd8014a98de8455dbcfcc9629e4 03-Sep-2015 Yabin Cui <yabinc@google.com> adb: clean up debug tracing a little.

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
ain.cpp
c6424585a27bbbf6922574a2cccc3bf6c42bfb2c 31-Aug-2015 Yabin Cui <yabinc@google.com> adb: remove workaround for b/6558362.

I believe the problem has been fixed in https://android-review.googlesource.com/#/c/168412/.
So it's time to remove the workaround.

Bug: 6558362
Change-Id: I399c18eda0693f3c51feac07ff59a0a4b5558128
ain.cpp
2122c7a1483dc6050b33f58fad4a15d5acd79fdd 27-Aug-2015 Spencer Low <CompareAndSwap@gmail.com> win32: adb start-server shows stdout/stderr output from actual server

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>
ain.cpp
743883b570b200ff4d73d60798d63136f3e3d82e 19-Aug-2015 Siva Velusamy <vsiva@google.com> Use glog for logging in the OSX USB layer

Change-Id: I8a3a4dfc768fdd335f03dc582c85b410c808aeb7
ain.cpp
57532b2a067082fa5968094c2c615f1832fa1971 13-Aug-2015 Elliott Hughes <enh@google.com> Merge "adb: improve network error info"
f18fc0879be9cd506be6b1e051feb543b0746bdb 12-Aug-2015 Spencer Low <CompareAndSwap@gmail.com> adb: start-server and kill-server error output

- handle_host_request
- When the host:kill command comes in, shutdown the socket before
calling exit(). If we don't do this, the client will output error info
even though everything is working ok.

- adb_connect()
- If we can't parse the version string, explain this in error output
and don't goto error which would try to close an fd we already closed.
- If host:kill doesn't work, output error info. Don't try to close
already closed fd.

- adb_main()
- If writing the ACK somehow has an error, output error info (I doubt
this will ever get hit).

- adb_commandline()
- Fix typo about max port number.
- Make 'adb kill-server' and 'adb start-server' output any detailed
error info.

Change-Id: Id1a309cc1bf516f7f49bd332b34d30f148b406da
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
ain.cpp
317acfb88065f68bc3030a5624a63ba4cfaab414 12-Aug-2015 Yabin Cui <yabinc@google.com> Merge "Remove confusing variable HOST."
155159c545ee1a6d8e3cbea866ea66096f4db5f3 12-Aug-2015 Spencer Low <CompareAndSwap@gmail.com> adb: make stdin/stdout/stderr redirection errors fatal

Make these fatal errors:

- Win32 GetTempPathW() failures.

- Errors opening /dev/null (and don't use LOG(FATAL) for this error
since that will do a crash-dump on Windows which isn't appropriate for a
transient runtime error).

- Errors with dup2.

- Errors opening adb.log.

Change-Id: Ided76a5436d8c6f059d8f6799c49ba04c87181ae
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
ain.cpp
bf7c605d87f87c03066c384cecb0f0c91aa31403 12-Aug-2015 Spencer Low <CompareAndSwap@gmail.com> adb: improve network error info

- 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>
ain.cpp
661327e8e40f95076e0e8abe86193da16b061bd8 11-Aug-2015 Yabin Cui <yabinc@google.com> Remove confusing variable HOST.

First, HOST is always 0 in adbd, which matches ADB_HOST=0.
Second, HOST is always 1 when adb_main is called, which matches ADB_HOST=1.
For adb client that doesn't call adb_main, it never touches local_init(),
init_transport_registration() and fdevent_loop(). So the changes in adb.cpp,
services.cpp and transport_local.cpp do nothing with it.
As a conclusion, I think we can remove HOST and use ADB_HOST instead.

Change-Id: Ide0e0eca7468b6c3c130f6b50974406280678b2e
ain.cpp
5c398d2ce96150e94e596e959bfe2246b94de660 09-Aug-2015 Spencer Low <CompareAndSwap@gmail.com> adb: win32: write ACK to separate pipe instead of stdout

The win32 version of 9f2d1a9cfc04e1d5970823da1878097288a9a9cd. The big
technique is to fit a Win32 HANDLE value in an int because it only uses
32-bits. This allows most of the other adb code to stay the same.

Also, fix a regression in the 'adb server nodaemon' command that was
erroneously returning an error when --reply-fd was not used, which
should not be necessary for this particular command.

Change-Id: I37e9c609014b813af93bf0d6c12f665b59c93c41
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
ain.cpp
9f2d1a9cfc04e1d5970823da1878097288a9a9cd 07-Aug-2015 Siva Velusamy <vsiva@google.com> adb start-server: Use a separate fd for sending initial OK

When "adb start-server" is issued, and a server needs to be launched,
adb client forks itself and the child process runs the server routine.
Once the server initializes its various components, it sends an "OK\n"
back to the client via its stderror (or stdout on Windows).

This sequence breaks down if before sending the "OK\n", the server
happens to log something on its stderr. In order to avoid this, the
client now expects the ack to come on a different fd rather than one
of the standard streams.

Bug: https://code.google.com/p/android/issues/detail?id=182150

Change-Id: I9d58a08068d71eb3b77e8a7377e934631c016466
ain.cpp
cf4ff64f2d3cdc4ffa2f06e594d18a965f591a82 11-May-2015 Spencer Low <CompareAndSwap@gmail.com> adb: win32: Unicode path names, env vars, some console support

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>
ain.cpp
5200c6670f041550c23821fec8e8e49b30ef6d29 31-Jul-2015 Spencer Low <CompareAndSwap@gmail.com> adb: win32: initial IPv6 support and improved Winsock error reporting

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>
ain.cpp
d0f66c361641dc30162877ee3e4b4690d6833442 21-May-2015 Spencer Low <CompareAndSwap@gmail.com> adb: win32: fix logging to adb.log

In the adb client, redirect stdin and stderr of the adb server to `nul',
so that when the adb server starts up, it avoids issues in the C Runtime
where it closes stderr, making it hard to properly reopen. There are
probably other ways to avoid this issue, but I think this is the
cleanest that will keep working over the years and will exercise the
most commonly used code-paths in the C Runtime.

Fix some adb_close() calls to be unix_close() (only really matters on
Windows).

Make stderr non-buffered on Windows, to match the (sensible) Linux
behavior.

Change-Id: I1b15c64240e50dbeb56788b0d0d901f4536ad788
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
ain.cpp
9313c0df20d7cc8ea6a074a3d53022c4b3b5ea05 21-May-2015 Dan Albert <danalbert@google.com> Improve logging.

Any output from the LOG family will now go to stderr and logcat on the
device. stderr is usually redirected to a log file, but that is now
inhibited for adbd if being run from a tty (useful when debugging with
the serial console).

This also fixes sending logs to the file on device for the trace mask
of "all". The "all" tag was specifically handled to return early from
the function, preventing the file initialization from happening.

Change-Id: Id253577bfd1500fbce92dbfba0f9be23dbfd5ee4
ain.cpp
d396dc93a7b2e2f55eb6a34f04fe796769e9ecea 12-May-2015 Spencer Low <CompareAndSwap@gmail.com> adb: win32: fix daemon acknowledgement

The daemon failed to startup because main.cpp was changed from calling
WriteFile() to android::base::WriteStringToFd(), the later which calls
write() in the C Runtime which by default has stdout in textmode which
does \n to \r\n translation.

The quick fix is to change stdout's mode from text to binary since right
after it is reopened to redirect to the daemon log file anyway.

Change-Id: I322fc9eae5d6abbf63f3d5917b0beb2171b5a15c
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
ain.cpp
c89e0ccd40624e8b2ce30d425e8bed0264fae548 09-May-2015 Dan Albert <danalbert@google.com> Revert "Revert "Split adb_main.cpp into client and daemon.""

This reverts commit 218dbccefa3d874d988e4784bda5e45b7643cb5a.

Change-Id: I74088db34983dc99e316a07c6ddc294340e0eb71
ain.cpp
218dbccefa3d874d988e4784bda5e45b7643cb5a 09-May-2015 Dan Albert <danalbert@google.com> Revert "Split adb_main.cpp into client and daemon."

This reverts commit cf07494ac2a101c3afbe23a7d85121553f586cf7.
ain.cpp
3e1cb6d98d54d38820de70bba07732ea87e84ffb 09-May-2015 Dan Albert <danalbert@google.com> Revert "Probably fix the Mac build."

This reverts commit 49513cbcc7534a6d31fb40f4b1172eee0673c407.
ain.cpp
49513cbcc7534a6d31fb40f4b1172eee0673c407 08-May-2015 Dan Albert <danalbert@google.com> Probably fix the Mac build.

This was throwing an unused variable warning for kWorkaroundBug6558362
on Darwin.

Change-Id: I1cbf381708e9884180a37f3246af36795d07dfc2
ain.cpp
cf07494ac2a101c3afbe23a7d85121553f586cf7 06-May-2015 Dan Albert <danalbert@google.com> Split adb_main.cpp into client and daemon.

The name "client" is somewhat misleading as it also contains the host
side adb server, but it's a part of the client binary.

Change-Id: I128b7bab213e330eb21b5010cd1fec5f7a62c8af
ain.cpp