From 6933d54e0944c773a39ee69ecb34ede3bebf3799 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Tue, 26 Mar 2019 13:21:42 -0700 Subject: [PATCH] adbd: listen to all functionfs events. Monitor for FUNCTIONFS_UNBIND as well, so that in the case where we get FUNCTIONFS_BIND, FUNCTIONFS_UNBIND, FUNCTIONFS_BIND, we don't trigger an assertion failure from seeing two FUNCTIONFS_BINDs. Bug: http://b/129134256 Test: manual Change-Id: I80af5f4b833513e932262638b9f8d76bbcb35504 --- adb/daemon/usb.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/adb/daemon/usb.cpp b/adb/daemon/usb.cpp index 765357971..8a5000336 100644 --- a/adb/daemon/usb.cpp +++ b/adb/daemon/usb.cpp @@ -267,7 +267,7 @@ struct UsbFfsConnection : public Connection { adb_thread_setname("UsbFfs-monitor"); bool bound = false; - bool started = false; + bool enabled = false; bool running = true; while (running) { adb_pollfd pfd[2] = { @@ -298,16 +298,32 @@ struct UsbFfsConnection : public Connection { switch (event.type) { case FUNCTIONFS_BIND: CHECK(!bound) << "received FUNCTIONFS_BIND while already bound?"; + CHECK(!enabled) << "received FUNCTIONFS_BIND while already enabled?"; bound = true; + break; case FUNCTIONFS_ENABLE: - CHECK(!started) << "received FUNCTIONFS_ENABLE while already running?"; - started = true; + CHECK(bound) << "received FUNCTIONFS_ENABLE while not bound?"; + CHECK(!enabled) << "received FUNCTIONFS_ENABLE while already enabled?"; + enabled = true; + StartWorker(); break; case FUNCTIONFS_DISABLE: + CHECK(bound) << "received FUNCTIONFS_DISABLE while not bound?"; + CHECK(enabled) << "received FUNCTIONFS_DISABLE while not enabled?"; + enabled = false; + + running = false; + break; + + case FUNCTIONFS_UNBIND: + CHECK(!enabled) << "received FUNCTIONFS_UNBIND while still enabled?"; + CHECK(bound) << "received FUNCTIONFS_UNBIND when not bound?"; + bound = false; + running = false; break; } @@ -339,7 +355,7 @@ struct UsbFfsConnection : public Connection { LOG(FATAL) << "hit EOF on eventfd"; } - WaitForEvents(); + ReadEvents(); } }); } @@ -389,7 +405,7 @@ struct UsbFfsConnection : public Connection { return block; } - void WaitForEvents() { + void ReadEvents() { static constexpr size_t kMaxEvents = kUsbReadQueueDepth + kUsbWriteQueueDepth; struct io_event events[kMaxEvents]; struct timespec timeout = {.tv_sec = 0, .tv_nsec = 0};