From 1bc4ce4da97baac3a36d585b199ae5f4fc5cfa52 Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Sun, 31 Mar 2024 10:26:48 +0900 Subject: [PATCH] Use dbus activation for server execution Change-Id: Ia5274b1936d1a5bfc3b52c8f0fb5033b7480e16c Signed-off-by: Ilho Kim --- client/src/connector.cc | 86 ++++++++++++++++++++++++----------------- client/src/connector.hh | 2 +- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/client/src/connector.cc b/client/src/connector.cc index 381fe45..af400bd 100644 --- a/client/src/connector.cc +++ b/client/src/connector.cc @@ -124,7 +124,7 @@ bool Connector::ConnectForAdmin() { if (conn_admin_listener_.GetState() == ConnectionState::Connected) return true; - if (!activator_.Connect()) + if (!activator_.Connect(raw_pc_, "install")) return false; try { @@ -145,7 +145,7 @@ bool Connector::ConnectForInfo() { if (conn_info_listener_.GetState() == ConnectionState::Connected) return true; - if (!activator_.Connect()) + if (!activator_.Connect(raw_pc_, "getsize")) return false; try { @@ -166,7 +166,7 @@ bool Connector::ConnectForCache() { if (conn_cache_listener_.GetState() == ConnectionState::Connected) return true; - if (!activator_.Connect()) + if (!activator_.Connect(raw_pc_, "clearcache")) return false; try { @@ -187,7 +187,7 @@ bool Connector::ConnectForDelayedResult() { if (conn_delayed_result_listener_.GetState() == ConnectionState::Connected) return true; - if (!activator_.Connect()) + if (!activator_.Connect(raw_pc_, "install")) return false; try { @@ -234,41 +234,57 @@ pkgmgr_client_t* Connector::GetRawPc() { return raw_pc_; } -bool Connector::Activator::Connect() { - if (!Create()) - return false; - - SetTimeout(60 * 1000); - - int retry_cnt = 3; - do { - int ret = TryConnect(); - if (ret == 0) { - break; - } else if (ret < -1) { - _E("Maybe peer not launched or peer dead. path: %s, fd: %d", - PACKAGE_MANAGER_SOCKET_PATH, fd_); - // If requester is root, don't wait - if (getuid() == 0) - return false; - - usleep(100 * 1000); - --retry_cnt; - } else if (ret < 0) { - _E("Failed to connect to socket: %s, fd: %d", - PACKAGE_MANAGER_SOCKET_PATH, fd_); - return false; - } - } while (retry_cnt > 0); +bool Connector::Activator::Connect(pkgmgr_client_t* pc, std::string method) { + GVariant *result; + int ret = PKGMGR_R_ECOMM; - if (retry_cnt == 0) { - _E("Failed to connect with server"); - return false; - } + ret = pkgmgr_client_connection_send_request(pc, method.c_str(), + g_variant_new("()"), &result); + if (ret != PKGMGR_R_OK) { + _E("request failed: %d", ret); + return false; + } - return ReceiveReady(); + g_variant_get(result, "(i)", &ret); + g_variant_unref(result); + return ret == PKGMGR_R_OK ? true : false; } +//bool Connector::Activator::Connect() { +// if (!Create()) +// return false; +// +// SetTimeout(60 * 1000); +// +// int retry_cnt = 3; +// do { +// int ret = TryConnect(); +// if (ret == 0) { +// break; +// } else if (ret < -1) { +// _E("Maybe peer not launched or peer dead. path: %s, fd: %d", +// PACKAGE_MANAGER_SOCKET_PATH, fd_); +// // If requester is root, don't wait +// if (getuid() == 0) +// return false; +// +// usleep(100 * 1000); +// --retry_cnt; +// } else if (ret < 0) { +// _E("Failed to connect to socket: %s, fd: %d", +// PACKAGE_MANAGER_SOCKET_PATH, fd_); +// return false; +// } +// } while (retry_cnt > 0); +// +// if (retry_cnt == 0) { +// _E("Failed to connect with server"); +// return false; +// } +// +// return ReceiveReady(); +//} + bool Connector::Activator::Create() { if (fd_ != -1) return true; diff --git a/client/src/connector.hh b/client/src/connector.hh index 29236b3..d8f3499 100644 --- a/client/src/connector.hh +++ b/client/src/connector.hh @@ -72,7 +72,7 @@ class Connector { public: Activator() : fd_(-1), addr_{} {} - bool Connect(); + bool Connect(pkgmgr_client_t* pc, std::string method); bool Create(); void SetTimeout(int timeout_msec); int TryConnect(); -- 2.34.1