From 996f5b96f12e77e922114734ed8524ce7d5f62bc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 21 Sep 2016 17:18:13 -0500 Subject: in PKIdleData, take the std::string argument as a const& to make cppcheck happy on yakkety --- src/adbd-client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 400c7c9..e436a43 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -66,7 +66,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_) {} -- cgit v1.2.3 From 3b7185fdf0d21897fd874d337d57379f911cc63c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 21 Sep 2016 19:05:02 -0500 Subject: in adbd-client's dtor, check the worker thread's joinable() state before calling join() on it --- src/adbd-client.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index e436a43..85389f4 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -48,7 +48,8 @@ public: g_cancellable_cancel(m_cancellable); m_pkresponse_cv.notify_one(); m_sleep_cv.notify_one(); - m_worker_thread.join(); + if (m_worker_thread.joinable()) + m_worker_thread.join(); g_clear_object(&m_cancellable); } @@ -104,6 +105,8 @@ private: void on_public_key_response(PKResponse response) { + g_debug("%s got response %d", G_STRLOC, int(response)); + // set m_pkresponse and wake up the waiting worker thread std::unique_lock lk(m_pkresponse_mutex); m_pkresponse = response; @@ -141,11 +144,13 @@ private: std::unique_lock lk(m_pkresponse_mutex); m_pkresponse_ready = false; pass_public_key_to_main_thread(public_key); + g_debug("%s thread %p waiting", G_STRLOC, g_thread_self()); m_pkresponse_cv.wait(lk, [this](){ return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); }); 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))); } -- cgit v1.2.3 From b74f5f6a37edaab28156e551462407096cfbfcbd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 21 Sep 2016 20:35:21 -0500 Subject: adbd-client's std::condition_variable::wait() call seems to be the system_error culprit, so wrapping in try-catch to be cautious --- src/adbd-client.cpp | 17 +++++++++++------ src/usb-manager.cpp | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 85389f4..83b15ac 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -124,11 +124,11 @@ private: while (!g_cancellable_is_cancelled(m_cancellable)) { - g_debug("%s creating a client socket to '%s'", G_STRLOC, socket_path.c_str()); + 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); 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; if (socket != nullptr) reqstr = read_request(socket); @@ -138,16 +138,21 @@ private: if (reqstr.substr(0,2) == "PK") { PKResponse response = PKResponse::DENY; const auto public_key = reqstr.substr(2); - g_debug("%s got pk [%s]", G_STRLOC, public_key.c_str()); + g_debug("%s thread %p got pk [%s]", G_STRLOC, g_thread_self(), public_key.c_str()); if (!public_key.empty()) { got_valid_req = true; std::unique_lock lk(m_pkresponse_mutex); m_pkresponse_ready = false; + m_pkresponse = AdbdClient::PKResponse::DENY; pass_public_key_to_main_thread(public_key); g_debug("%s thread %p waiting", G_STRLOC, g_thread_self()); - m_pkresponse_cv.wait(lk, [this](){ - return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); - }); + try { + m_pkresponse_cv.wait(lk, [this](){ + return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); + }); + } catch (std::system_error& e) { + g_critical("%s thread %p unable to wait for response because of unexpected error '%s'", G_STRLOC, g_thread_self(), e.what()); + } response = m_pkresponse; g_debug("%s thread %p got response '%d', is-cancelled %d", G_STRLOC, g_thread_self(), diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp index 4d750c0..aac289a 100644 --- a/src/usb-manager.cpp +++ b/src/usb-manager.cpp @@ -102,7 +102,7 @@ private: m_snap = std::make_shared(m_req.fingerprint); m_snap_connections.insert((*m_snap).on_user_response().connect( [this](AdbdClient::PKResponse response, bool remember_choice){ - g_debug("%s user responded! response %d, remember %d", G_STRLOC, int(response), int(remember_choice)); + g_debug("%s thread %p user responded! response %d, remember %d", G_STRLOC, g_thread_self(), int(response), int(remember_choice)); m_req.respond(response); if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) write_public_key(m_req.public_key); -- cgit v1.2.3 From c58d22061d944dc8b9b0b072082665c9f10877ac Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 09:10:48 -0500 Subject: add a plethora of log statements to help figure out what's causing the silo test failures --- src/adbd-client.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/usb-manager.cpp | 42 ++++++++++++++++++++++++ 2 files changed, 135 insertions(+) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 83b15ac..1539982 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -39,22 +39,31 @@ public: m_cancellable{g_cancellable_new()}, m_worker_thread{&Impl::worker_func, this} { +g_debug("%s %s", G_STRLOC, G_STRFUNC); } ~Impl() { +g_debug("%s %s", 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_debug("%s %s", G_STRLOC, G_STRFUNC); g_cancellable_cancel(m_cancellable); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.notify_one(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_sleep_cv.notify_one(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (m_worker_thread.joinable()) m_worker_thread.join(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&m_cancellable); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } core::Signal& on_pk_request() { +g_debug("%s %s", G_STRLOC, G_STRFUNC); return m_on_pk_request; } @@ -78,6 +87,7 @@ private: void pass_public_key_to_main_thread(const std::string& public_key) { +g_debug("%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}, @@ -88,30 +98,47 @@ 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_debug("%s %s", G_STRLOC, G_STRFUNC); auto data = static_cast(gdata); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (!g_cancellable_is_cancelled(data->cancellable)) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); // notify our listeners of the request +g_debug("%s %s", G_STRLOC, G_STRFUNC); auto self = data->self; +g_debug("%s %s", G_STRLOC, G_STRFUNC); struct PKRequest req; +g_debug("%s %s", G_STRLOC, G_STRFUNC); req.public_key = data->public_key; +g_debug("%s %s", G_STRLOC, G_STRFUNC); req.fingerprint = get_fingerprint(req.public_key); +g_debug("%s %s", G_STRLOC, G_STRFUNC); req.respond = [self](PKResponse response){self->on_public_key_response(response);}; +g_debug("%s %s", G_STRLOC, G_STRFUNC); self->m_on_pk_request(req); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } +g_debug("%s %s", G_STRLOC, G_STRFUNC); return G_SOURCE_REMOVE; } void on_public_key_response(PKResponse response) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s got response %d", G_STRLOC, int(response)); +g_debug("%s %s", G_STRLOC, G_STRFUNC); // set m_pkresponse and wake up the waiting worker thread std::unique_lock lk(m_pkresponse_mutex); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = response; +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_ready = true; +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.notify_one(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } /*** @@ -120,59 +147,89 @@ private: void worker_func() // runs in worker thread { +g_debug("%s %s", G_STRLOC, G_STRFUNC); const std::string socket_path {m_socket_path}; +g_debug("%s %s", G_STRLOC, G_STRFUNC); while (!g_cancellable_is_cancelled(m_cancellable)) { +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); bool got_valid_req = false; g_debug("%s thread %p calling read_request", g_thread_self(), G_STRLOC); std::string reqstr; +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (socket != nullptr) reqstr = read_request(socket); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (!reqstr.empty()) g_debug("%s got request [%s]", G_STRLOC, reqstr.c_str()); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (reqstr.substr(0,2) == "PK") { +g_debug("%s %s", G_STRLOC, G_STRFUNC); PKResponse response = PKResponse::DENY; +g_debug("%s %s", G_STRLOC, G_STRFUNC); const auto public_key = reqstr.substr(2); +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); got_valid_req = true; +g_debug("%s %s", G_STRLOC, G_STRFUNC); std::unique_lock lk(m_pkresponse_mutex); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_ready = false; +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = AdbdClient::PKResponse::DENY; +g_debug("%s %s", G_STRLOC, G_STRFUNC); pass_public_key_to_main_thread(public_key); +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s thread %p waiting", G_STRLOC, g_thread_self()); +g_debug("%s %s", G_STRLOC, G_STRFUNC); try { +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.wait(lk, [this](){ +g_debug("%s %s", G_STRLOC, G_STRFUNC); return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); }); } catch (std::system_error& e) { +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); response = m_pkresponse; 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_debug("%s %s", G_STRLOC, G_STRFUNC); if (!g_cancellable_is_cancelled(m_cancellable)) send_pk_response(socket, response); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } else if (!reqstr.empty()) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_warning("Invalid ADB request: [%s]", reqstr.c_str()); } +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&socket); // If nothing interesting's happening, sleep a bit. // (Interval copied from UsbDebuggingManager.java) +g_debug("%s %s", G_STRLOC, G_STRFUNC); static constexpr std::chrono::seconds sleep_interval {std::chrono::seconds(1)}; +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (!got_valid_req && !g_cancellable_is_cancelled(m_cancellable)) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); std::unique_lock lk(m_sleep_mutex); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_sleep_cv.wait_for(lk, sleep_interval); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } } } @@ -181,10 +238,12 @@ private: GSocket* create_client_socket(const std::string& socket_path) { GError* error {}; +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); if (error != nullptr) { g_warning("Error creating adbd client socket: %s", error->message); g_clear_error(&error); @@ -192,55 +251,80 @@ private: return nullptr; } +g_debug("%s %s", G_STRLOC, G_STRFUNC); auto address = g_unix_socket_address_new(socket_path.c_str()); +g_debug("%s %s", G_STRLOC, G_STRFUNC); const auto connected = g_socket_connect(socket, address, m_cancellable, &error); +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&address); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (!connected) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("unable to connect to '%s': %s", socket_path.c_str(), error->message); +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_clear_error(&error); +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&socket); +g_debug("%s %s", G_STRLOC, G_STRFUNC); return nullptr; } +g_debug("%s %s", G_STRLOC, G_STRFUNC); return socket; } std::string read_request(GSocket* socket) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); char buf[4096] = {}; +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s calling g_socket_receive()", G_STRLOC); +g_debug("%s %s", G_STRLOC, G_STRFUNC); const auto n_bytes = g_socket_receive (socket, buf, sizeof(buf), m_cancellable, nullptr); +g_debug("%s %s", G_STRLOC, G_STRFUNC); std::string ret; +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (n_bytes > 0) ret.append(buf, std::string::size_type(n_bytes)); +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); return ret; } void send_pk_response(GSocket* socket, PKResponse response) { std::string response_str; +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); GError* error {}; +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_socket_send(socket, response_str.c_str(), response_str.size(), m_cancellable, &error); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (error != nullptr) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_warning("GAdbdServer: Error accepting socket connection: %s", error->message); +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_clear_error(&error); } +g_debug("%s %s", G_STRLOC, G_STRFUNC); } static std::string get_fingerprint(const std::string& public_key) { +g_debug("%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(), @@ -250,14 +334,20 @@ private: ) ); +g_debug("%s %s", G_STRLOC, G_STRFUNC); gsize digest_len {}; +g_debug("%s %s", G_STRLOC, G_STRFUNC); auto digest = g_base64_decode(base64.c_str(), &digest_len); +g_debug("%s %s", G_STRLOC, G_STRFUNC); auto checksum = g_compute_checksum_for_data(G_CHECKSUM_MD5, digest, digest_len); +g_debug("%s %s", G_STRLOC, G_STRFUNC); const gsize checksum_len = checksum ? strlen(checksum) : 0; +g_debug("%s %s", G_STRLOC, G_STRFUNC); // insert ':' between character pairs; eg "ff27b5f3" --> "ff:27:b5:f3" std::string fingerprint; +g_debug("%s %s", G_STRLOC, G_STRFUNC); for (gsize i=0; ion_usb_disconnected().connect([this](const std::string& /*usb_name*/) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); restart(); }); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_greeter->is_active().changed().connect([this](bool /*is_active*/) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); maybe_snap(); }); +g_debug("%s %s", G_STRLOC, G_STRFUNC); restart(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } ~Impl() { +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (m_restart_idle_tag) g_source_remove(m_restart_idle_tag); +g_debug("%s %s", G_STRLOC, G_STRFUNC); clear(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } private: void clear() { +g_debug("%s %s", G_STRLOC, G_STRFUNC); // clear out old state m_snap_connections.clear(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_snap.reset(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_req = decltype(m_req)(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_adbd_client.reset(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } void restart() { +g_debug("%s %s", G_STRLOC, G_STRFUNC); clear(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); // set a new client m_adbd_client.reset(new GAdbdClient{m_socket_path}); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_adbd_client->on_pk_request().connect( [this](const AdbdClient::PKRequest& req) { +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s got pk request: %s", G_STRLOC, req.fingerprint.c_str()); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_req = req; +g_debug("%s %s", G_STRLOC, G_STRFUNC); maybe_snap(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } ); } void maybe_snap() { +g_debug("%s %s", G_STRLOC, G_STRFUNC); // don't prompt in the greeter! if (!m_req.public_key.empty() && !m_greeter->is_active().get()) snap(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } void snap() { +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_snap = std::make_shared(m_req.fingerprint); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_snap_connections.insert((*m_snap).on_user_response().connect( [this](AdbdClient::PKResponse response, bool remember_choice){ +g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s thread %p user responded! response %d, remember %d", G_STRLOC, g_thread_self(), int(response), int(remember_choice)); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_req.respond(response); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) write_public_key(m_req.public_key); +g_debug("%s %s", G_STRLOC, G_STRFUNC); m_restart_idle_tag = g_idle_add([](gpointer gself){ +g_debug("%s %s", G_STRLOC, G_STRFUNC); auto self = static_cast(gself); +g_debug("%s %s", G_STRLOC, G_STRFUNC); self->m_restart_idle_tag = 0; +g_debug("%s %s", G_STRLOC, G_STRFUNC); self->restart(); +g_debug("%s %s", G_STRLOC, G_STRFUNC); return G_SOURCE_REMOVE; }, this); } )); +g_debug("%s %s", G_STRLOC, G_STRFUNC); } void write_public_key(const std::string& public_key) { +g_debug("%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_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); if (!dir_exists) return; +g_debug("%s %s", G_STRLOC, G_STRFUNC); // open the file in append mode, with user rw and group r permissions const auto fd = open( @@ -135,16 +173,20 @@ private: O_APPEND|O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP ); +g_debug("%s %s", G_STRLOC, G_STRFUNC); if (fd == -1) { g_warning("Error opening ADB datafile: %s", g_strerror(errno)); +g_debug("%s %s", G_STRLOC, G_STRFUNC); return; } +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); } const std::string m_socket_path; -- cgit v1.2.3 From baa9f937ac60cf1077a0f65197478a376135134d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 12:03:49 -0500 Subject: GAdbdClient::Impl::on_public_key_response() doesn't need a condition variable wait, so use std::lock_guard rather than std::unique_lock --- src/adbd-client.cpp | 13 +++++++------ src/main.cpp | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 1539982..0d955ab 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -44,7 +44,7 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); ~Impl() { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_debug("%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_debug("%s %s", G_STRLOC, G_STRFUNC); @@ -63,7 +63,7 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); core::Signal& on_pk_request() { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_debug("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); return m_on_pk_request; } @@ -98,7 +98,7 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); { /* NB: It's possible (though unlikely) that data.self was destroyed while this callback was pending, so we must check is-cancelled FIRST */ -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_debug("%s %s thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); auto data = static_cast(gdata); g_debug("%s %s", G_STRLOC, G_STRFUNC); if (!g_cancellable_is_cancelled(data->cancellable)) @@ -127,11 +127,12 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); void on_public_key_response(PKResponse response) { g_debug("%s %s", G_STRLOC, G_STRFUNC); - g_debug("%s got response %d", G_STRLOC, int(response)); + g_debug("%s thread %p got response %d", G_STRLOC, g_thread_self(), int(response)); g_debug("%s %s", G_STRLOC, G_STRFUNC); // set m_pkresponse and wake up the waiting worker thread - std::unique_lock lk(m_pkresponse_mutex); + std::lock_guard lk(m_sleep_mutex); + //std::unique_lock lk(m_pkresponse_mutex); g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = response; g_debug("%s %s", G_STRLOC, G_STRFUNC); @@ -147,7 +148,7 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); void worker_func() // runs in worker thread { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_debug("%s %s worker thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); const std::string socket_path {m_socket_path}; g_debug("%s %s", G_STRLOC, G_STRFUNC); diff --git a/src/main.cpp b/src/main.cpp index 52cdd58..43dc5f5 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_debug("%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. -- cgit v1.2.3 From c17e7b8f11eead4105b6fc706650780e68f6634c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 12:43:02 -0500 Subject: fix r25 copypaste error, lock on the correct mutex --- src/adbd-client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 0d955ab..25aefa8 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -131,7 +131,7 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s %s", G_STRLOC, G_STRFUNC); // set m_pkresponse and wake up the waiting worker thread - std::lock_guard lk(m_sleep_mutex); + std::lock_guard lk(m_pkresponse_mutex); //std::unique_lock lk(m_pkresponse_mutex); g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = response; -- cgit v1.2.3 From 3a644063077a86899b59614048d84f86e77f75cd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 12:52:56 -0500 Subject: The notifying thread does not need to hold the lock on the same mutex as the one held by the waiting thread(s); in fact doing so is a pessimization. --- src/adbd-client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 25aefa8..597e69f 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -131,7 +131,7 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s %s", G_STRLOC, G_STRFUNC); // set m_pkresponse and wake up the waiting worker thread - std::lock_guard lk(m_pkresponse_mutex); + //std::lock_guard lk(m_pkresponse_mutex); //std::unique_lock lk(m_pkresponse_mutex); g_debug("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = response; -- cgit v1.2.3 From a17e7fc7ed94bf201b37cf7d9744ef272404c0f7 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 16:10:01 -0500 Subject: tweak the debug tracers --- src/adbd-client.cpp | 186 ++++++++++++++++++++++++++-------------------------- src/main.cpp | 2 +- src/usb-manager.cpp | 84 ++++++++++++------------ 3 files changed, 136 insertions(+), 136 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 597e69f..fbaf377 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -39,31 +39,31 @@ public: m_cancellable{g_cancellable_new()}, m_worker_thread{&Impl::worker_func, this} { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } ~Impl() { -g_debug("%s %s DTOR DTOR dtor", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_cancellable_cancel(m_cancellable); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.notify_one(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_sleep_cv.notify_one(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (m_worker_thread.joinable()) m_worker_thread.join(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&m_cancellable); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } core::Signal& on_pk_request() { -g_debug("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); +g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); return m_on_pk_request; } @@ -87,7 +87,7 @@ private: void pass_public_key_to_main_thread(const std::string& public_key) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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}, @@ -98,48 +98,48 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); { /* NB: It's possible (though unlikely) that data.self was destroyed while this callback was pending, so we must check is-cancelled FIRST */ -g_debug("%s %s thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); +g_message("%s %s thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); auto data = static_cast(gdata); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!g_cancellable_is_cancelled(data->cancellable)) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); // notify our listeners of the request -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); auto self = data->self; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); struct PKRequest req; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); req.public_key = data->public_key; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); req.fingerprint = get_fingerprint(req.public_key); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); req.respond = [self](PKResponse response){self->on_public_key_response(response);}; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); self->m_on_pk_request(req); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); return G_SOURCE_REMOVE; } void on_public_key_response(PKResponse response) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s thread %p got response %d", G_STRLOC, g_thread_self(), int(response)); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); // set m_pkresponse and wake up the waiting worker thread //std::lock_guard lk(m_pkresponse_mutex); //std::unique_lock lk(m_pkresponse_mutex); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = response; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_ready = true; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.notify_one(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } /*** @@ -148,89 +148,89 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); void worker_func() // runs in worker thread { -g_debug("%s %s worker thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); +g_message("%s %s worker thread is %p", G_STRLOC, G_STRFUNC, g_thread_self()); const std::string socket_path {m_socket_path}; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); while (!g_cancellable_is_cancelled(m_cancellable)) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); bool got_valid_req = false; g_debug("%s thread %p calling read_request", g_thread_self(), G_STRLOC); std::string reqstr; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (socket != nullptr) reqstr = read_request(socket); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!reqstr.empty()) g_debug("%s got request [%s]", G_STRLOC, reqstr.c_str()); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (reqstr.substr(0,2) == "PK") { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); PKResponse response = PKResponse::DENY; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); const auto public_key = reqstr.substr(2); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); got_valid_req = true; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); std::unique_lock lk(m_pkresponse_mutex); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_ready = false; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse = AdbdClient::PKResponse::DENY; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); pass_public_key_to_main_thread(public_key); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s thread %p waiting", G_STRLOC, g_thread_self()); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); try { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.wait(lk, [this](){ -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); }); } catch (std::system_error& e) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); response = m_pkresponse; 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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!g_cancellable_is_cancelled(m_cancellable)) send_pk_response(socket, response); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } else if (!reqstr.empty()) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_warning("Invalid ADB request: [%s]", reqstr.c_str()); } -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); static constexpr std::chrono::seconds sleep_interval {std::chrono::seconds(1)}; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!got_valid_req && !g_cancellable_is_cancelled(m_cancellable)) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); std::unique_lock lk(m_sleep_mutex); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_sleep_cv.wait_for(lk, sleep_interval); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } } } @@ -239,12 +239,12 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); GSocket* create_client_socket(const std::string& socket_path) { GError* error {}; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +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); @@ -252,80 +252,80 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); return nullptr; } -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); auto address = g_unix_socket_address_new(socket_path.c_str()); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); const auto connected = g_socket_connect(socket, address, m_cancellable, &error); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&address); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!connected) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("unable to connect to '%s': %s", socket_path.c_str(), error->message); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_error(&error); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_object(&socket); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); return nullptr; } -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); return socket; } std::string read_request(GSocket* socket) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); char buf[4096] = {}; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s calling g_socket_receive()", G_STRLOC); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); const auto n_bytes = g_socket_receive (socket, buf, sizeof(buf), m_cancellable, nullptr); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); std::string ret; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (n_bytes > 0) ret.append(buf, std::string::size_type(n_bytes)); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); return ret; } void send_pk_response(GSocket* socket, PKResponse response) { std::string response_str; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); GError* error {}; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_socket_send(socket, response_str.c_str(), response_str.size(), m_cancellable, &error); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (error != nullptr) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_warning("GAdbdServer: Error accepting socket connection: %s", error->message); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_clear_error(&error); } -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } static std::string get_fingerprint(const std::string& public_key) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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(), @@ -335,20 +335,20 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); ) ); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); gsize digest_len {}; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); auto digest = g_base64_decode(base64.c_str(), &digest_len); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); auto checksum = g_compute_checksum_for_data(G_CHECKSUM_MD5, digest, digest_len); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); const gsize checksum_len = checksum ? strlen(checksum) : 0; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); // insert ':' between character pairs; eg "ff27b5f3" --> "ff:27:b5:f3" std::string fingerprint; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); for (gsize i=0; ion_usb_disconnected().connect([this](const std::string& /*usb_name*/) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); restart(); }); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_greeter->is_active().changed().connect([this](bool /*is_active*/) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); maybe_snap(); }); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); restart(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } ~Impl() { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (m_restart_idle_tag) g_source_remove(m_restart_idle_tag); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); clear(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } private: void clear() { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); // clear out old state m_snap_connections.clear(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_snap.reset(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_req = decltype(m_req)(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_adbd_client.reset(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } void restart() { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); clear(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); // set a new client m_adbd_client.reset(new GAdbdClient{m_socket_path}); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_adbd_client->on_pk_request().connect( [this](const AdbdClient::PKRequest& req) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s got pk request: %s", G_STRLOC, req.fingerprint.c_str()); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_req = req; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); maybe_snap(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } ); } void maybe_snap() { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); // don't prompt in the greeter! if (!m_req.public_key.empty() && !m_greeter->is_active().get()) snap(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } void snap() { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_snap = std::make_shared(m_req.fingerprint); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_snap_connections.insert((*m_snap).on_user_response().connect( [this](AdbdClient::PKResponse response, bool remember_choice){ -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); g_debug("%s thread %p user responded! response %d, remember %d", G_STRLOC, g_thread_self(), int(response), int(remember_choice)); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_req.respond(response); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) write_public_key(m_req.public_key); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); m_restart_idle_tag = g_idle_add([](gpointer gself){ -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); auto self = static_cast(gself); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); self->m_restart_idle_tag = 0; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); self->restart(); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); return G_SOURCE_REMOVE; }, this); } )); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } void write_public_key(const std::string& public_key) { -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (!dir_exists) return; -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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( @@ -173,20 +173,20 @@ g_debug("%s %s", G_STRLOC, G_STRFUNC); O_APPEND|O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP ); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); if (fd == -1) { g_warning("Error opening ADB datafile: %s", g_strerror(errno)); -g_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); return; } -g_debug("%s %s", G_STRLOC, G_STRFUNC); +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_debug("%s %s", G_STRLOC, G_STRFUNC); +g_message("%s %s", G_STRLOC, G_STRFUNC); } const std::string m_socket_path; -- cgit v1.2.3 From fe6fe0ae1e87d46c6060045cc3b25865df4533bd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 16:11:46 -0500 Subject: keep piling 'em on --- src/adbd-client.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index fbaf377..dfa97da 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -121,6 +121,7 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); } g_message("%s %s", G_STRLOC, G_STRFUNC); +fflush(nullptr); return G_SOURCE_REMOVE; } @@ -140,6 +141,7 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); g_message("%s %s", G_STRLOC, G_STRFUNC); m_pkresponse_cv.notify_one(); g_message("%s %s", G_STRLOC, G_STRFUNC); +fflush(nullptr); } /*** -- cgit v1.2.3 From e616541cde47710de062367cc74c9b18821d9a73 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 16:33:48 -0500 Subject: fix tyop --- src/adbd-client.cpp | 17 +++-------------- src/usb-manager.cpp | 1 + 2 files changed, 4 insertions(+), 14 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index dfa97da..585352c 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -190,20 +190,9 @@ 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); -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); + m_pkresponse_cv.wait(lk, [this](){ + return m_pkresponse_ready || g_cancellable_is_cancelled(m_cancellable); + }); response = m_pkresponse; g_debug("%s thread %p got response '%d', is-cancelled %d", G_STRLOC, g_thread_self(), diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp index 81c1b9d..ee9b463 100644 --- a/src/usb-manager.cpp +++ b/src/usb-manager.cpp @@ -80,6 +80,7 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); maybe_snap(); } ); + } ~Impl() { -- cgit v1.2.3 From 549cd2b06a89216cdbf198d225723134fb551b32 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 22 Sep 2016 16:54:55 -0500 Subject: use a std::atomic for adbd-client's m_pkresponse_ready --- src/adbd-client.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 585352c..f36a823 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -365,7 +366,7 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); std::mutex m_pkresponse_mutex; std::condition_variable m_pkresponse_cv; - bool m_pkresponse_ready = false; + std::atomic m_pkresponse_ready {false}; PKResponse m_pkresponse = PKResponse::DENY; }; -- cgit v1.2.3 From 9e5e5d7e39c9f7bbeafa4d6f0dfa4b8697e9d770 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 19 Oct 2016 11:41:02 +0200 Subject: remove the temporary tracers from r34 and r35 --- src/adbd-client.cpp | 89 ----------------------------------------- src/main.cpp | 1 - src/usb-manager.cpp | 24 +---------- tests/unit/adbd-client-test.cpp | 8 ---- 4 files changed, 1 insertion(+), 121 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index f36a823..3f86818 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -40,31 +40,22 @@ 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(); -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& on_pk_request() { -g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); return m_on_pk_request; } @@ -88,7 +79,6 @@ 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}, @@ -99,50 +89,31 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); { /* 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(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::lock_guard lk(m_pkresponse_mutex); //std::unique_lock 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); } /*** @@ -151,45 +122,30 @@ fflush(nullptr); 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_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 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_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 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); @@ -200,29 +156,20 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); 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 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); } } } @@ -231,12 +178,10 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); 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); @@ -244,80 +189,55 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); 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(), @@ -327,20 +247,14 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); ) ); -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; ion_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 @@ -84,31 +78,24 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); ~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; @@ -139,25 +126,20 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); } } )); -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( @@ -165,20 +147,16 @@ g_message("%s %s", G_STRLOC, G_STRFUNC); 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; @@ -211,4 +189,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 20d6ee4..d1ce740 100644 --- a/tests/unit/adbd-client-test.cpp +++ b/tests/unit/adbd-client-test.cpp @@ -70,20 +70,14 @@ 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(socket_path); 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 @@ -94,11 +88,9 @@ g_message("%s %s thread %p", G_STRLOC, G_STRFUNC, g_thread_self()); 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()); } } -- cgit v1.2.3 From 144228c916b574e5f4cbc90b9b90c16f0ed9ac73 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 19 Oct 2016 11:43:52 +0200 Subject: remove dead code --- src/adbd-client.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 3f86818..8b675d5 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -109,8 +109,6 @@ private: g_debug("%s thread %p got response %d", G_STRLOC, g_thread_self(), int(response)); // set m_pkresponse and wake up the waiting worker thread - //std::lock_guard lk(m_pkresponse_mutex); - //std::unique_lock lk(m_pkresponse_mutex); m_pkresponse = response; m_pkresponse_ready = true; m_pkresponse_cv.notify_one(); -- cgit v1.2.3 From 7002fc4e6a6496fb5c0d3294540c957787689847 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 21 Oct 2016 13:32:21 +0200 Subject: add block braces as suggested by dobey --- src/adbd-client.cpp | 6 ++++-- src/greeter.cpp | 3 ++- src/usb-manager.cpp | 17 +++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) (limited to 'src/adbd-client.cpp') diff --git a/src/adbd-client.cpp b/src/adbd-client.cpp index 8b675d5..47914cb 100644 --- a/src/adbd-client.cpp +++ b/src/adbd-client.cpp @@ -49,8 +49,9 @@ public: g_cancellable_cancel(m_cancellable); m_pkresponse_cv.notify_one(); m_sleep_cv.notify_one(); - if (m_worker_thread.joinable()) + if (m_worker_thread.joinable()) { m_worker_thread.join(); + } g_clear_object(&m_cancellable); } @@ -154,8 +155,9 @@ private: int(response), int(g_cancellable_is_cancelled(m_cancellable))); } - if (!g_cancellable_is_cancelled(m_cancellable)) + if (!g_cancellable_is_cancelled(m_cancellable)) { send_pk_response(socket, response); + } } else if (!reqstr.empty()) { g_warning("Invalid ADB request: [%s]", reqstr.c_str()); } diff --git a/src/greeter.cpp b/src/greeter.cpp index 9bd5db0..3d0f347 100644 --- a/src/greeter.cpp +++ b/src/greeter.cpp @@ -146,8 +146,9 @@ private: GError* error {}; auto v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error); if (error != nullptr) { - if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { g_warning("Greeter: Error getting IsActive property: %s", error->message); + } g_clear_error(&error); } else { GVariant* is_active {}; diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp index 7d66e74..f83b5f1 100644 --- a/src/usb-manager.cpp +++ b/src/usb-manager.cpp @@ -50,10 +50,11 @@ public: }); m_greeter->state().changed().connect([this](const Greeter::State& state) { - if (state == Greeter::State::INACTIVE) + if (state == Greeter::State::INACTIVE) { maybe_snap(); - else + } else { stop_snap(); + } }); // create a new adbd client @@ -78,8 +79,9 @@ public: ~Impl() { - if (m_request_complete_idle_tag) + if (m_request_complete_idle_tag) { g_source_remove(m_request_complete_idle_tag); + } } private: @@ -93,12 +95,14 @@ private: void maybe_snap() { // only prompt if there's something to prompt about - if (!m_req) + if (!m_req) { return; + } // only prompt in an unlocked session - if (m_greeter->state().get() != Greeter::State::INACTIVE) + if (m_greeter->state().get() != Greeter::State::INACTIVE) { return; + } snap(); } @@ -109,8 +113,9 @@ private: m_snap_connections.insert((*m_snap).on_user_response().connect( [this](AdbdClient::PKResponse response, bool remember_choice){ - if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) + if (remember_choice && (response == AdbdClient::PKResponse::ALLOW)) { write_public_key(m_req->public_key); + } m_response = response; -- cgit v1.2.3