diff options
-rw-r--r-- | src/adbd-client.cpp | 126 | ||||
-rw-r--r-- | src/main.cpp | 1 | ||||
-rw-r--r-- | src/usb-manager.cpp | 26 | ||||
-rw-r--r-- | tests/unit/adbd-client-test.cpp | 13 | ||||
-rw-r--r-- | tests/unit/usb-snap-test.cpp | 2 |
5 files changed, 152 insertions, 16 deletions
diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 400c7c9..dfa97da 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -39,21 +39,31 @@ public: m_cancellable{g_cancellable_new()}, m_worker_thread{&Impl::worker_func, this} { +g_message("%s %s", G_STRLOC, G_STRFUNC); } ~Impl() { +g_message("%s %s DTOR DTOR dtor", G_STRLOC, G_STRFUNC); // tell the worker thread to stop whatever it's doing and exit. g_debug("%s Client::Impl dtor, cancelling m_cancellable", G_STRLOC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_cancellable_cancel(m_cancellable); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.notify_one(); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_sleep_cv.notify_one(); - m_worker_thread.join(); +g_message("%s %s", G_STRLOC, G_STRFUNC); + if (m_worker_thread.joinable()) + m_worker_thread.join(); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&m_cancellable); +g_message("%s %s", G_STRLOC, G_STRFUNC); } core::Signal<const PKRequest&>& on_pk_request() { +g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); return m_on_pk_request; } @@ -66,7 +76,7 @@ private: GCancellable* cancellable = nullptr; const std::string public_key; - PKIdleData(Impl* self_, GCancellable* cancellable_, std::string public_key_): + PKIdleData(Impl* self_, GCancellable* cancellable_, const std::string& public_key_): self(self_), cancellable(G_CANCELLABLE(g_object_ref(cancellable_))), public_key(public_key_) {} @@ -77,6 +87,7 @@ private: void pass_public_key_to_main_thread(const std::string& public_key) { +g_message("%s %s", G_STRLOC, G_STRFUNC); g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, on_public_key_request_static, new PKIdleData{this, m_cancellable, public_key}, @@ -87,28 +98,50 @@ private: { /* NB: It's possible (though unlikely) that data.self was destroyed while this callback was pending, so we must check is-cancelled FIRST */ +g_message("%s %s thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); auto data = static_cast<PKIdleData*>(gdata); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!g_cancellable_is_cancelled(data->cancellable)) { +g_message("%s %s", G_STRLOC, G_STRFUNC); // notify our listeners of the request +g_message("%s %s", G_STRLOC, G_STRFUNC); auto self = data->self; +g_message("%s %s", G_STRLOC, G_STRFUNC); struct PKRequest req; +g_message("%s %s", G_STRLOC, G_STRFUNC); req.public_key = data->public_key; +g_message("%s %s", G_STRLOC, G_STRFUNC); req.fingerprint = get_fingerprint(req.public_key); +g_message("%s %s", G_STRLOC, G_STRFUNC); req.respond = [self](PKResponse response){self->on_public_key_response(response);}; +g_message("%s %s", G_STRLOC, G_STRFUNC); self->m_on_pk_request(req); +g_message("%s %s", G_STRLOC, G_STRFUNC); } +g_message("%s %s", G_STRLOC, G_STRFUNC); +fflush(nullptr); return G_SOURCE_REMOVE; } void on_public_key_response(PKResponse response) { +g_message("%s %s", G_STRLOC, G_STRFUNC); + g_debug("%s thread %p got response %d", G_STRLOC, g_thread_self(), int(response)); + +g_message("%s %s", G_STRLOC, G_STRFUNC); // set m_pkresponse and wake up the waiting worker thread - std::unique_lock<std::mutex> lk(m_pkresponse_mutex); + //std::lock_guard<std::mutex> lk(m_pkresponse_mutex); + //std::unique_lock<std::mutex> lk(m_pkresponse_mutex); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = response; +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_ready = true; +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.notify_one(); +g_message("%s %s", G_STRLOC, G_STRFUNC); +fflush(nullptr); } /*** @@ -117,52 +150,89 @@ private: void worker_func() // runs in worker thread { +g_message("%s %s worker thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); const std::string socket_path {m_socket_path}; +g_message("%s %s", G_STRLOC, G_STRFUNC); while (!g_cancellable_is_cancelled(m_cancellable)) { - g_debug("%s creating a client socket to '%s'", G_STRLOC, socket_path.c_str()); +g_message("%s %s", G_STRLOC, G_STRFUNC); + g_debug("%s thread %p creating a client socket to '%s'", G_STRLOC, g_thread_self(), socket_path.c_str()); auto socket = create_client_socket(socket_path); +g_message("%s %s", G_STRLOC, G_STRFUNC); bool got_valid_req = false; - g_debug("%s calling read_request", G_STRLOC); + g_debug("%s thread %p calling read_request", g_thread_self(), G_STRLOC); std::string reqstr; +g_message("%s %s", G_STRLOC, G_STRFUNC); if (socket != nullptr) reqstr = read_request(socket); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!reqstr.empty()) g_debug("%s got request [%s]", G_STRLOC, reqstr.c_str()); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (reqstr.substr(0,2) == "PK") { +g_message("%s %s", G_STRLOC, G_STRFUNC); PKResponse response = PKResponse::DENY; +g_message("%s %s", G_STRLOC, G_STRFUNC); const auto public_key = reqstr.substr(2); - g_debug("%s got pk [%s]", G_STRLOC, public_key.c_str()); +g_message("%s %s", G_STRLOC, G_STRFUNC); + g_debug("%s thread %p got pk [%s]", G_STRLOC, g_thread_self(), public_key.c_str()); if (!public_key.empty()) { +g_message("%s %s", G_STRLOC, G_STRFUNC); got_valid_req = true; +g_message("%s %s", G_STRLOC, G_STRFUNC); std::unique_lock<std::mutex> lk(m_pkresponse_mutex); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_ready = false; +g_message("%s %s", G_STRLOC, G_STRFUNC); + m_pkresponse = AdbdClient::PKResponse::DENY; +g_message("%s %s", G_STRLOC, G_STRFUNC); pass_public_key_to_main_thread(public_key); - m_pkresponse_cv.wait(lk, [this](){ - return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); - }); +g_message("%s %s", G_STRLOC, G_STRFUNC); + g_debug("%s thread %p waiting", G_STRLOC, g_thread_self()); +g_message("%s %s", G_STRLOC, G_STRFUNC); + try { +g_message("%s %s", G_STRLOC, G_STRFUNC); + m_pkresponse_cv.wait(lk, [this](){ +g_message("%s %s", G_STRLOC, G_STRFUNC); + return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); + }); + } catch (std::system_error& e) { +g_message("%s %s", G_STRLOC, G_STRFUNC); + g_critical("%s thread %p unable to wait for response because of unexpected error '%s'", G_STRLOC, g_thread_self(), e.what()); + } +g_message("%s %s", G_STRLOC, G_STRFUNC); response = m_pkresponse; - g_debug("%s got response '%d', is-cancelled %d", G_STRLOC, + g_debug("%s thread %p got response '%d', is-cancelled %d", G_STRLOC, + g_thread_self(), int(response), int(g_cancellable_is_cancelled(m_cancellable))); } +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!g_cancellable_is_cancelled(m_cancellable)) send_pk_response(socket, response); +g_message("%s %s", G_STRLOC, G_STRFUNC); } else if (!reqstr.empty()) { +g_message("%s %s", G_STRLOC, G_STRFUNC); g_warning("Invalid ADB request: [%s]", reqstr.c_str()); } +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&socket); // If nothing interesting's happening, sleep a bit. // (Interval copied from UsbDebuggingManager.java) +g_message("%s %s", G_STRLOC, G_STRFUNC); static constexpr std::chrono::seconds sleep_interval {std::chrono::seconds(1)}; +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!got_valid_req && !g_cancellable_is_cancelled(m_cancellable)) { +g_message("%s %s", G_STRLOC, G_STRFUNC); std::unique_lock<std::mutex> lk(m_sleep_mutex); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_sleep_cv.wait_for(lk, sleep_interval); +g_message("%s %s", G_STRLOC, G_STRFUNC); } } } @@ -171,10 +241,12 @@ private: GSocket* create_client_socket(const std::string& socket_path) { GError* error {}; +g_message("%s %s", G_STRLOC, G_STRFUNC); auto socket = g_socket_new(G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (error != nullptr) { g_warning("Error creating adbd client socket: %s", error->message); g_clear_error(&error); @@ -182,55 +254,80 @@ private: return nullptr; } +g_message("%s %s", G_STRLOC, G_STRFUNC); auto address = g_unix_socket_address_new(socket_path.c_str()); +g_message("%s %s", G_STRLOC, G_STRFUNC); const auto connected = g_socket_connect(socket, address, m_cancellable, &error); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&address); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!connected) { +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("unable to connect to '%s': %s", socket_path.c_str(), error->message); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_error(&error); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&socket); +g_message("%s %s", G_STRLOC, G_STRFUNC); return nullptr; } +g_message("%s %s", G_STRLOC, G_STRFUNC); return socket; } std::string read_request(GSocket* socket) { +g_message("%s %s", G_STRLOC, G_STRFUNC); char buf[4096] = {}; +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s calling g_socket_receive()", G_STRLOC); +g_message("%s %s", G_STRLOC, G_STRFUNC); const auto n_bytes = g_socket_receive (socket, buf, sizeof(buf), m_cancellable, nullptr); +g_message("%s %s", G_STRLOC, G_STRFUNC); std::string ret; +g_message("%s %s", G_STRLOC, G_STRFUNC); if (n_bytes > 0) ret.append(buf, std::string::size_type(n_bytes)); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s g_socket_receive got %d bytes: [%s]", G_STRLOC, int(n_bytes), ret.c_str()); +g_message("%s %s", G_STRLOC, G_STRFUNC); return ret; } void send_pk_response(GSocket* socket, PKResponse response) { std::string response_str; +g_message("%s %s", G_STRLOC, G_STRFUNC); switch(response) { case PKResponse::ALLOW: response_str = "OK"; break; case PKResponse::DENY: response_str = "NO"; break; } g_debug("%s sending reply: [%s]", G_STRLOC, response_str.c_str()); +g_message("%s %s", G_STRLOC, G_STRFUNC); GError* error {}; +g_message("%s %s", G_STRLOC, G_STRFUNC); g_socket_send(socket, response_str.c_str(), response_str.size(), m_cancellable, &error); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (error != nullptr) { +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) +g_message("%s %s", G_STRLOC, G_STRFUNC); g_warning("GAdbdServer: Error accepting socket connection: %s", error->message); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_error(&error); } +g_message("%s %s", G_STRLOC, G_STRFUNC); } static std::string get_fingerprint(const std::string& public_key) { +g_message("%s %s", G_STRLOC, G_STRFUNC); // The first token is base64-encoded data, so cut on the first whitespace const std::string base64 ( public_key.begin(), @@ -240,14 +337,20 @@ private: ) ); +g_message("%s %s", G_STRLOC, G_STRFUNC); gsize digest_len {}; +g_message("%s %s", G_STRLOC, G_STRFUNC); auto digest = g_base64_decode(base64.c_str(), &digest_len); +g_message("%s %s", G_STRLOC, G_STRFUNC); auto checksum = g_compute_checksum_for_data(G_CHECKSUM_MD5, digest, digest_len); +g_message("%s %s", G_STRLOC, G_STRFUNC); const gsize checksum_len = checksum ? strlen(checksum) : 0; +g_message("%s %s", G_STRLOC, G_STRFUNC); // insert ':' between character pairs; eg "ff27b5f3" --> "ff:27:b5:f3" std::string fingerprint; +g_message("%s %s", G_STRLOC, G_STRFUNC); for (gsize i=0; i<checksum_len; ) { fingerprint.append(checksum+i, checksum+i+2); if (i < checksum_len-2) @@ -255,8 +358,11 @@ private: i += 2; } +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_pointer(&digest, g_free); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_pointer(&checksum, g_free); +g_message("%s %s", G_STRLOC, G_STRFUNC); return fingerprint; } diff --git a/src/main.cpp b/src/main.cpp index 52cdd58..0ece7dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,6 +46,7 @@ main(int /*argc*/, char** /*argv*/) g_warning("busname lost: '%s'", name.c_str()); g_main_loop_quit(loop); }; +g_message("%s %s main thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); // build all our indicators. // Right now we've only got one -- rotation lock -- but hey, we can dream. diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp index 65617c5..81c1b9d 100644 --- a/src/usb-manager.cpp +++ b/src/usb-manager.cpp @@ -45,15 +45,21 @@ public: m_usb_monitor{usb_monitor}, m_greeter{greeter} { +g_message("%s %s", G_STRLOC, G_STRFUNC); m_usb_monitor->on_usb_disconnected().connect([this](const std::string& /*usb_name*/) { +g_message("%s %s", G_STRLOC, G_STRFUNC); m_req.reset(); +g_message("%s %s", G_STRLOC, G_STRFUNC); }); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_greeter->state().changed().connect([this](const Greeter::State& state) { +g_message("%s %s", G_STRLOC, G_STRFUNC); if (state == Greeter::State::INACTIVE) maybe_snap(); else stop_snap(); +g_message("%s %s", G_STRLOC, G_STRFUNC); }); // create a new adbd client @@ -74,28 +80,34 @@ public: maybe_snap(); } ); - } ~Impl() { +g_message("%s %s", G_STRLOC, G_STRFUNC); if (m_request_complete_idle_tag) g_source_remove(m_request_complete_idle_tag); +g_message("%s %s", G_STRLOC, G_STRFUNC); } private: void stop_snap() { +g_message("%s %s", G_STRLOC, G_STRFUNC); m_snap_connections.clear(); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_snap.reset(); +g_message("%s %s", G_STRLOC, G_STRFUNC); } void maybe_snap() { +g_message("%s %s", G_STRLOC, G_STRFUNC); // only prompt if there's something to prompt about if (!m_req) return; +g_message("%s %s", G_STRLOC, G_STRFUNC); // only prompt in an unlocked session if (m_greeter->state().get() != Greeter::State::INACTIVE) return; @@ -126,20 +138,25 @@ private: } } )); +g_message("%s %s", G_STRLOC, G_STRFUNC); } void write_public_key(const std::string& public_key) { +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s writing public key '%s' to '%s'", G_STRLOC, public_key.c_str(), m_public_keys_filename.c_str()); // confirm the directory exists auto dirname = g_path_get_dirname(m_public_keys_filename.c_str()); +g_message("%s %s", G_STRLOC, G_STRFUNC); const auto dir_exists = g_file_test(dirname, G_FILE_TEST_IS_DIR); if (!dir_exists) g_warning("ADB data directory '%s' does not exist", dirname); g_clear_pointer(&dirname, g_free); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!dir_exists) return; +g_message("%s %s", G_STRLOC, G_STRFUNC); // open the file in append mode, with user rw and group r permissions const auto fd = open( @@ -147,16 +164,20 @@ private: O_APPEND|O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP ); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (fd == -1) { g_warning("Error opening ADB datafile: %s", g_strerror(errno)); +g_message("%s %s", G_STRLOC, G_STRFUNC); return; } +g_message("%s %s", G_STRLOC, G_STRFUNC); // write the new public key on its own line std::string buf {public_key + '\n'}; if (write(fd, buf.c_str(), buf.size()) == -1) g_warning("Error writing ADB datafile: %d %s", errno, g_strerror(errno)); close(fd); +g_message("%s %s", G_STRLOC, G_STRFUNC); } const std::string m_socket_path; @@ -189,5 +210,4 @@ UsbManager::UsbManager( UsbManager::~UsbManager() { -} - +}
\ No newline at end of file diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp index 754f76c..20d6ee4 100644 --- a/tests/unit/adbd-client-test.cpp +++ b/tests/unit/adbd-client-test.cpp @@ -70,26 +70,35 @@ TEST_F(AdbdClientFixture, SocketPlumbing) for (const auto& test : tests) { +g_message("%s %s thread %p starting test", G_STRLOC, G_STRFUNC, g_thread_self()); // start an AdbdClient that listens for PKRequests std::string pk; auto adbd_client = std::make_shared<GAdbdClient>(socket_path); - adbd_client->on_pk_request().connect([&pk, main_thread, test](const AdbdClient::PKRequest& req){ + auto connection = adbd_client->on_pk_request().connect([&pk, main_thread, test](const AdbdClient::PKRequest& req){ +g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); EXPECT_EQ(main_thread, g_thread_self()); +g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); g_message("in on_pk_request with %s", req.public_key.c_str()); +g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); pk = req.public_key; +g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); req.respond(test.response); +g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); }); // start a mock AdbdServer with to fire test key requests and wait for a response auto adbd_server = std::make_shared<GAdbdServer>(socket_path, std::vector<std::string>{test.request}); - wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 2000); + wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 5000); EXPECT_EQ(test.expected_pk, pk); ASSERT_EQ(1, adbd_server->m_responses.size()); EXPECT_EQ(test.expected_response, adbd_server->m_responses.front()); // cleanup +g_message("%s %s thread %p cleanup", G_STRLOC, G_STRFUNC, g_thread_self()); + connection.disconnect(); adbd_client.reset(); adbd_server.reset(); g_unlink(socket_path.c_str()); +g_message("%s %s thread %p test exit", G_STRLOC, G_STRFUNC, g_thread_self()); } } diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp index 6cfbbd9..2be4b27 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -88,7 +88,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip) auto snap = std::make_shared<UsbSnap>(test.fingerprint); AdbdClient::PKResponse user_response {}; bool user_response_set = false; - snap->on_user_response().connect([&user_response,&user_response_set](AdbdClient::PKResponse response, bool /*remember*/){ + auto connection = snap->on_user_response().connect([&user_response,&user_response_set](AdbdClient::PKResponse response, bool /*remember*/){ user_response = response; user_response_set = true; }); |