From 74395b3facab0995af157791e04a83623172aff6 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Tue, 9 May 2017 13:43:35 -0700 Subject: 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 --- client/main.cpp | 15 +++++++++++++-- client/usb_dispatch.cpp | 6 ++++++ client/usb_libusb.cpp | 20 +++++++++++++------- client/usb_windows.cpp | 2 ++ 4 files changed, 34 insertions(+), 9 deletions(-) (limited to 'client') diff --git a/client/main.cpp b/client/main.cpp index fe5099c..bd9ad01 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -92,6 +92,16 @@ static BOOL WINAPI ctrlc_handler(DWORD type) { } #endif +void adb_server_cleanup() { + // Upon exit, we want to clean up in the following order: + // 1. close_smartsockets, so that we don't get any new clients + // 2. kick_all_transports, to avoid writing only part of a packet to a transport. + // 3. usb_cleanup, to tear down the USB stack. + close_smartsockets(); + kick_all_transports(); + usb_cleanup(); +} + int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd) { #if defined(_WIN32) // adb start-server starts us up with stdout and stderr hooked up to @@ -111,12 +121,13 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply SetConsoleCtrlHandler(ctrlc_handler, TRUE); #else signal(SIGINT, [](int) { - android::base::quick_exit(0); + fdevent_run_on_main_thread([]() { android::base::quick_exit(0); }); }); #endif - init_transport_registration(); + android::base::at_quick_exit(adb_server_cleanup); + init_transport_registration(); init_mdns_transport_discovery(); usb_init(); diff --git a/client/usb_dispatch.cpp b/client/usb_dispatch.cpp index 710a3ce..c4eed78 100644 --- a/client/usb_dispatch.cpp +++ b/client/usb_dispatch.cpp @@ -27,6 +27,12 @@ void usb_init() { } } +void usb_cleanup() { + if (should_use_libusb()) { + libusb::usb_cleanup(); + } +} + int usb_write(usb_handle* h, const void* data, int len) { return should_use_libusb() ? libusb::usb_write(reinterpret_cast(h), data, len) diff --git a/client/usb_libusb.cpp b/client/usb_libusb.cpp index 5a65865..18a8ff2 100644 --- a/client/usb_libusb.cpp +++ b/client/usb_libusb.cpp @@ -415,15 +415,21 @@ void usb_init() { // Spawn a thread to do device enumeration. // TODO: Use libusb_hotplug_* instead? + std::unique_lock lock(device_poll_mutex); device_poll_thread = new std::thread(poll_for_devices); - android::base::at_quick_exit([]() { - { - std::unique_lock lock(device_poll_mutex); - terminate_device_poll_thread = true; +} + +void usb_cleanup() { + { + std::unique_lock lock(device_poll_mutex); + terminate_device_poll_thread = true; + + if (!device_poll_thread) { + return; } - device_poll_cv.notify_all(); - device_poll_thread->join(); - }); + } + device_poll_cv.notify_all(); + device_poll_thread->join(); } // Dispatch a libusb transfer, unlock |device_lock|, and then wait for the result. diff --git a/client/usb_windows.cpp b/client/usb_windows.cpp index ec55b0e..1620e6e 100644 --- a/client/usb_windows.cpp +++ b/client/usb_windows.cpp @@ -265,6 +265,8 @@ void usb_init() { std::thread(_power_notification_thread).detach(); } +void usb_cleanup() {} + usb_handle* do_usb_open(const wchar_t* interface_name) { unsigned long name_len = 0; -- cgit v1.2.3