From ffcc0911b2ad4da548a18574d487dea92be1fda7 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 18 Jul 2019 07:21:17 +0900 Subject: [PATCH 01/16] Release version 1.3.16 Changes: - Cancel sending dbus message Change-Id: I5ec86f8eec9c3b54c92e260c5ebfed731ec02513 Signed-off-by: Hwankyu Jhun --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index f52ae77..4d3bb69 100755 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.15 +Version: 1.3.16 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4 From 6212af446ce1852de8abbf1984c44fb851adaf49 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 22 Jul 2019 15:46:22 +0900 Subject: [PATCH 02/16] Declare Cynara Destructor If a class has no user-declared destructor, a destructor is implicitly declared as defaulted. An implicitly-declared destructor is an inline public member of its class. Inline definition is equal to inlining and it cause build warning. A function defined within a class definition is an inline function. Problem(Warning): - /home/abuild/rpmbuild/BUILD/rpc-port-1.3.16/src/ac-internal.h:43:9: warning: inlining failed in call to 'rpc_port::internal::AccessController::Cynara::~Cynara() noexcept': call is unlikely and code size would grow [-Winline] Change-Id: I6dc55fb7648da8e41261b53d6f4d2ed472f1292c Signed-off-by: Hwankyu Jhun --- src/ac-internal.cc | 2 ++ src/ac-internal.h | 1 + 2 files changed, 3 insertions(+) diff --git a/src/ac-internal.cc b/src/ac-internal.cc index f8be409..ec3d936 100644 --- a/src/ac-internal.cc +++ b/src/ac-internal.cc @@ -116,6 +116,8 @@ AccessController::Cynara::Cynara() } } +AccessController::Cynara::~Cynara() = default; + int AccessController::Cynara::FetchCredsFromDBus(GDBusConnection* connection, const char* sender) { char* user = nullptr; diff --git a/src/ac-internal.h b/src/ac-internal.h index 174f231..cb3b674 100644 --- a/src/ac-internal.h +++ b/src/ac-internal.h @@ -43,6 +43,7 @@ class AccessController { class Cynara { public: Cynara(); + ~Cynara(); int FetchCredsFromDBus(GDBusConnection *connection, const char *sender); int Check(const std::string& privilege) const; -- 2.7.4 From 2a6c399d39bf198b3ec2d2e167b8d5abc85ac781 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 22 Jul 2019 17:37:58 +0900 Subject: [PATCH 03/16] Release version 1.3.17 Changes: - Declare Cynara Destructor Change-Id: I196327ecd88c68ef95edbb70430ebc120bd411f4 Signed-off-by: Hwankyu Jhun --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index 4d3bb69..4f402a6 100755 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.16 +Version: 1.3.17 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4 From b6c0dd8e247394ace18fb8aedbf4bd3c9b39c40a Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Sun, 11 Aug 2019 01:14:34 +0900 Subject: [PATCH 04/16] Fix log message Change-Id: Ie34bdd3e39ddb990694478bcec1906085e9513c9 Signed-off-by: Hwankyu Jhun --- src/ac-internal.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ac-internal.cc b/src/ac-internal.cc index ec3d936..2e97a9a 100644 --- a/src/ac-internal.cc +++ b/src/ac-internal.cc @@ -71,8 +71,7 @@ int AccessController::CheckTrusted(const char* sender_appid) { return -1; } if (res != PMINFO_CERT_COMPARE_MATCH) { - LOGE("CheckCertificate() Failed : " \ - "MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH"); + LOGE("CheckCertificate() Failed : Certificate Not Matched"); return -1; } -- 2.7.4 From 84469cd7124722c6ea9751f911ba63bbaf8170ef Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 19 Aug 2019 08:39:16 +0900 Subject: [PATCH 05/16] Release version 1.3.18 Changes: - Fix log message Change-Id: I7e43be2bbe73496fd2dfc28b7c52686e2e2889a9 Signed-off-by: Hwankyu Jhun --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index 4f402a6..4dd156a 100755 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.17 +Version: 1.3.18 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4 From 13c213def812fdc2eaf9941298a9a594feb23afa Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Wed, 28 Aug 2019 08:56:12 +0900 Subject: [PATCH 06/16] Prevent invalid memory accress - This patch prevents bad memory access - If you try to read data larger than parcel size, the data read will be empty Change-Id: I7792cb9d36b247bea61c3dfc19e55b44aac7af69 Signed-off-by: Junghoon Park --- src/parcel-internal.cc | 10 ++++++++++ src/parcel-internal.h | 4 +++- src/rpc-port-parcel.cc | 7 ++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/parcel-internal.cc b/src/parcel-internal.cc index cae8988..6ece0b9 100644 --- a/src/parcel-internal.cc +++ b/src/parcel-internal.cc @@ -99,7 +99,11 @@ double Parcel::ReadDouble() { const char* Parcel::ReadString() { int l = Read(); + if (l <= 0) + return nullptr; const char* str = reinterpret_cast(&data_[reader_]); + if (static_cast(strlen(str) + 1) != l) + return nullptr; reader_ += l; return str; @@ -112,7 +116,11 @@ bool Parcel::ReadBool() { bundle* Parcel::ReadBundle() { int l = Read(); + if (l <= 0) + return nullptr; const bundle_raw* str = reinterpret_cast(&data_[reader_]); + if (static_cast(strlen(reinterpret_cast(str)) + 1) != l) + return nullptr; reader_ += l; bundle* b = bundle_decode(str, l - 1); @@ -133,6 +141,8 @@ void Parcel::Write(const unsigned char* buf, unsigned int size) { } void Parcel::Read(unsigned char* buf, unsigned int size) { + if (reader_ + size > data_.size()) + return; std::copy(&data_[reader_], &data_[reader_] + size, buf); reader_ += size; } diff --git a/src/parcel-internal.h b/src/parcel-internal.h index 3b491d0..ac2290a 100644 --- a/src/parcel-internal.h +++ b/src/parcel-internal.h @@ -65,9 +65,11 @@ class Parcel { template T Read() { - T d; + T d = 0; unsigned char* p = reinterpret_cast(&d); + if (reader_ + sizeof(T) > data_.size()) + return d; std::copy(&data_[reader_], &data_[reader_] + sizeof(T), p); reader_ += sizeof(T); return d; diff --git a/src/rpc-port-parcel.cc b/src/rpc-port-parcel.cc index 307808a..f49d9e2 100755 --- a/src/rpc-port-parcel.cc +++ b/src/rpc-port-parcel.cc @@ -297,8 +297,8 @@ RPC_API int rpc_port_parcel_read_string(rpc_port_parcel_h h, char** str) { return RPC_PORT_ERROR_INVALID_PARAMETER; Parcel* p = static_cast(h); - - *str = strdup(p->ReadString()); + const char* read = p->ReadString(); + *str = read == nullptr ? strdup("") : strdup(read); return RPC_PORT_ERROR_NONE; } @@ -319,8 +319,9 @@ RPC_API int rpc_port_parcel_read_bundle(rpc_port_parcel_h h, bundle** b) { return RPC_PORT_ERROR_INVALID_PARAMETER; Parcel* p = static_cast(h); + bundle* read = p->ReadBundle(); - *b = p->ReadBundle(); + *b = read == nullptr ? bundle_create() : read; return RPC_PORT_ERROR_NONE; } -- 2.7.4 From da1341668f936925b2485108378755526f14b212 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 28 Aug 2019 15:23:54 +0900 Subject: [PATCH 07/16] Release version 1.3.19 Changes: - Prevent invalid memory access Change-Id: Ib31b4d970f383128c67c5c5a5d47452c3497923e Signed-off-by: Hwankyu Jhun --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index 4dd156a..2fa9d28 100755 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.18 +Version: 1.3.19 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4 From 54693046c8767893bf930626d33f522e92150a3e Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Fri, 30 Aug 2019 17:17:38 +0900 Subject: [PATCH 08/16] Add exception handler for parcel data Change-Id: I856cb4f34af9682c6ecb7e59a1c70ba3bb29d076 Signed-off-by: Junghoon Park --- src/parcel-internal.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/parcel-internal.cc b/src/parcel-internal.cc index 6ece0b9..3fce740 100644 --- a/src/parcel-internal.cc +++ b/src/parcel-internal.cc @@ -101,9 +101,11 @@ const char* Parcel::ReadString() { if (l <= 0) return nullptr; - const char* str = reinterpret_cast(&data_[reader_]); - if (static_cast(strlen(str) + 1) != l) + if (reader_ + l > data_.size()) + return nullptr; + if (data_[reader_ + l - 1] != 0) return nullptr; + const char* str = reinterpret_cast(&data_[reader_]); reader_ += l; return str; @@ -118,9 +120,11 @@ bundle* Parcel::ReadBundle() { if (l <= 0) return nullptr; - const bundle_raw* str = reinterpret_cast(&data_[reader_]); - if (static_cast(strlen(reinterpret_cast(str)) + 1) != l) + if (reader_ + l > data_.size()) + return nullptr; + if (data_[reader_ + l - 1] != 0) return nullptr; + const bundle_raw* str = reinterpret_cast(&data_[reader_]); reader_ += l; bundle* b = bundle_decode(str, l - 1); -- 2.7.4 From 0cb1c5ddbe9a4e385cd802f45f3f0557efbf8d27 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 30 Aug 2019 18:14:41 +0900 Subject: [PATCH 09/16] Release version 1.3.20 Changes: - Add exception handler for parcel data Change-Id: Ifc435d91d3a31cd28fcc3da13702ba493e8a7c61 Signed-off-by: Hwankyu Jhun --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index 2fa9d28..df7c9c6 100755 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.19 +Version: 1.3.20 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4 From bf65d6c5a8b1027b49ba9dc7e134a1b8170ced1c Mon Sep 17 00:00:00 2001 From: "mk5004.lee" Date: Tue, 1 Oct 2019 09:20:27 +0900 Subject: [PATCH 10/16] Update doxygen - change file mode Change-Id: I65438ba547e89c21b80c22ba39ea5cd527e24852 Signed-off-by: mk5004.lee --- include/rpc-port-parcel.h | 3 ++- include/rpc-port.h | 2 +- packaging/rpc-port.spec | 0 src/rpc-port-parcel.cc | 5 ++++- src/rpc-port.cc | 0 5 files changed, 7 insertions(+), 3 deletions(-) mode change 100755 => 100644 include/rpc-port.h mode change 100755 => 100644 packaging/rpc-port.spec mode change 100755 => 100644 src/rpc-port-parcel.cc mode change 100755 => 100644 src/rpc-port.cc diff --git a/include/rpc-port-parcel.h b/include/rpc-port-parcel.h index 1a3d661..202086b 100644 --- a/include/rpc-port-parcel.h +++ b/include/rpc-port-parcel.h @@ -53,6 +53,7 @@ typedef struct __rpc_port_parcelable { * @return @c 0 on success, * otherwise a negative error value * @retval #RPC_PORT_ERROR_NONE Successful + * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter * @see rpc_port_parcel_destroy() */ int rpc_port_parcel_create(rpc_port_parcel_h *h); @@ -409,7 +410,7 @@ int rpc_port_parcel_read(rpc_port_parcel_h h, rpc_port_parcelable_t *parcelable, int rpc_port_parcel_burst_read(rpc_port_parcel_h h, unsigned char *buf, unsigned int size); /** - * @brief Write bytes to rpc port parcel handle. + * @brief Writes bytes to rpc port parcel handle. * @since_tizen @if MOBILE 4.0 @elseif WEARABLE 5.0 @endif * @param[in] h The rpc port parcel handle * @param[in] buf The array buffer to write diff --git a/include/rpc-port.h b/include/rpc-port.h old mode 100755 new mode 100644 index 559141b..5f4ab6d --- a/include/rpc-port.h +++ b/include/rpc-port.h @@ -186,9 +186,9 @@ int rpc_port_proxy_destroy(rpc_port_proxy_h h); * @return @c 0 on success, * otherwise a negative error value * @retval #RPC_PORT_ERROR_NONE Successful + * @retval #RPC_PORT_ERROR_PERMISSION_DENIED Permission denied * @retval #RPC_PORT_ERROR_INVALID_PARAMETER The specified @a h is NULL * @retval #RPC_PORT_ERROR_IO_ERROR Internal I/O error - * @retval #RPC_PORT_ERROR_PERMISSION_DENIED Permission denied */ int rpc_port_proxy_connect(rpc_port_proxy_h h, const char *appid, const char *port); diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec old mode 100755 new mode 100644 diff --git a/src/rpc-port-parcel.cc b/src/rpc-port-parcel.cc old mode 100755 new mode 100644 index f49d9e2..55c10b2 --- a/src/rpc-port-parcel.cc +++ b/src/rpc-port-parcel.cc @@ -29,6 +29,9 @@ using namespace rpc_port; RPC_API int rpc_port_parcel_create(rpc_port_parcel_h* h) { + if (h == nullptr) + return RPC_PORT_ERROR_INVALID_PARAMETER; + Parcel* p = new Parcel(); *h = p; @@ -41,7 +44,7 @@ RPC_API int rpc_port_parcel_create_from_port(rpc_port_parcel_h* h, int len; unsigned char* buf; - if (port == nullptr) + if (h == nullptr || port == nullptr) return RPC_PORT_ERROR_INVALID_PARAMETER; internal::Port* pt = static_cast(port); diff --git a/src/rpc-port.cc b/src/rpc-port.cc old mode 100755 new mode 100644 -- 2.7.4 From 92aff7e886491bb0ae59dfa31471fda397697ee2 Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Mon, 28 Oct 2019 19:46:16 +0900 Subject: [PATCH 11/16] Add timer for connection - Dbus name for stub app should be appeared within 10s Change-Id: I11bd7b54afc9df013171c4083d8f2e1555ea7606 Signed-off-by: Junghoon Park --- src/proxy-internal.cc | 23 +++++++++++++++++++++++ src/proxy-internal.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/src/proxy-internal.cc b/src/proxy-internal.cc index 04deef8..3546273 100644 --- a/src/proxy-internal.cc +++ b/src/proxy-internal.cc @@ -40,6 +40,10 @@ Proxy::Proxy(bool mock) Proxy::~Proxy() { LOGD("Proxy::~Proxy"); + if (conn_timer_) { + g_source_remove(conn_timer_); + conn_timer_ = 0; + } } gboolean Proxy::OnSocketDisconnected(GIOChannel *gio, GIOCondition cond, @@ -96,6 +100,10 @@ void Proxy::OnPortRejected(const std::string& appid) { void Proxy::OnPortAppeared(const std::string& appid, const std::string& port_name) { LOGD("endpoint(%s), port_name(%s)", appid.c_str(), port_name.c_str()); + if (conn_timer_) { + g_source_remove(conn_timer_); + conn_timer_ = 0; + } if (listener_ == nullptr) return; @@ -164,9 +172,24 @@ int Proxy::Connect(std::string appid, std::string port_name, return RPC_PORT_ERROR_IO_ERROR; } + if (conn_timer_) + g_source_remove(conn_timer_); + conn_timer_ = g_timeout_add(10 * 1000, DbusNameTimeout, this); + return RPC_PORT_ERROR_NONE; } +gboolean Proxy::DbusNameTimeout(gpointer user_data) { + Proxy* obj = static_cast(user_data); + + LOGW("[__DbusNameTimeout__] endpoint(%s)", obj->target_appid_.c_str()); + if (obj->listener_) + obj->listener_->OnRejected(obj->target_appid_); + + obj->conn_timer_ = 0; + return G_SOURCE_REMOVE; +} + Proxy::ProxyPort::ProxyPort(Proxy* parent, int fd, const std::string& id, bool receive) : Port(fd, id), parent_(parent) { Watch(receive); diff --git a/src/proxy-internal.h b/src/proxy-internal.h index 642be5b..dce40a0 100644 --- a/src/proxy-internal.h +++ b/src/proxy-internal.h @@ -80,6 +80,7 @@ class Proxy : public FdBroker::IEventWatcher { gpointer data); static gboolean OnDataReceived(GIOChannel *gio, GIOCondition cond, gpointer data); + static gboolean DbusNameTimeout(gpointer user_data); void OnPortAppeared(const std::string& appid, const std::string& port_name) override; void OnPortVanished(const std::string& appid, @@ -98,6 +99,7 @@ class Proxy : public FdBroker::IEventWatcher { FdBroker fd_broker_; std::string target_appid_; int fds_[2]; + guint conn_timer_ = 0; }; } // namespace internal -- 2.7.4 From 270d5d6ff9f613427e5e404ab267104fac43dd58 Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Mon, 4 Nov 2019 09:46:55 +0900 Subject: [PATCH 12/16] Release version 1.3.21 Changes: - Update doxygen - Add timer for connection Change-Id: I25da31bc4ec8e86f4dede54202c96c72730128d9 Signed-off-by: Junghoon Park --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index df7c9c6..ede53ee 100644 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.20 +Version: 1.3.21 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4 From 29d529edf3bff2d8c6f6122e71a6409ddf7ebe89 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 6 Nov 2019 09:57:01 +0900 Subject: [PATCH 13/16] Implement rpc_port_proxy_connect_sync() Change-Id: I655bc3d13124c8623359578842eed431ba81a661 Signed-off-by: Hwankyu Jhun --- include/rpc-port-internal.h | 4 ++ src/fdbroker-internal.cc | 168 ++++++++++++++++++++++++++++++++++++++++---- src/fdbroker-internal.h | 3 + src/proxy-internal.cc | 41 +++++++++++ src/proxy-internal.h | 1 + src/rpc-port.cc | 11 +++ 6 files changed, 216 insertions(+), 12 deletions(-) diff --git a/include/rpc-port-internal.h b/include/rpc-port-internal.h index 5a9af2a..3af29e4 100644 --- a/include/rpc-port-internal.h +++ b/include/rpc-port-internal.h @@ -24,8 +24,12 @@ extern "C" { #endif int rpc_port_proxy_create_mockup(rpc_port_proxy_h *h); + int rpc_port_stub_create_mockup(rpc_port_stub_h *h, const char *port_name); +int rpc_port_proxy_connect_sync(rpc_port_proxy_h h, const char *appid, + const char *port_name); + #ifdef __cplusplus } #endif diff --git a/src/fdbroker-internal.cc b/src/fdbroker-internal.cc index a8cf58a..c77f2d5 100644 --- a/src/fdbroker-internal.cc +++ b/src/fdbroker-internal.cc @@ -304,6 +304,142 @@ int FdBroker::Send(const std::string& target_appid, return (*fds)[0]; } +int FdBroker::SendSync(const std::string& target_appid, + const std::string& port_name, + int (*fds)[2]) { + char sender_appid[255] = { 0, }; + int ret; + if (!mock_) { + ret = aul_app_get_appid_bypid(getpid(), sender_appid, sizeof(sender_appid)); + if (ret != AUL_R_OK) { + LOGE("Failed to get application ID. ret(%d)", ret); + return -1; + } + } + + SocketPair main_sock_pair(mock_); + ret = main_sock_pair.Request(target_appid, port_name); + if (ret != 0) + return -1; + + SocketPair delegate_sock_pair(mock_); + ret = delegate_sock_pair.Request(target_appid, port_name); + if (ret != 0) + return -1; + + if (mock_) { + int send_fds[2]; + send_fds[0] = main_sock_pair.Detach(SocketPair::RECEIVER); + send_fds[1] = delegate_sock_pair.Detach(SocketPair::RECEIVER); + + ret = DBusMock::GetInst().Send("TestApp", port_name, send_fds); + if (ret < 0) + return ret; + + (*fds)[0] = main_sock_pair.Detach(SocketPair::SENDER); + (*fds)[1] = delegate_sock_pair.Detach(SocketPair::SENDER); + return (*fds)[0]; + } + + FdList fd_list; + ret = fd_list.Add(main_sock_pair.Detach(SocketPair::RECEIVER)); + if (ret != 0) + return -1; + + ret = fd_list.Add(delegate_sock_pair.Detach(SocketPair::RECEIVER)); + if (ret != 0) + return -1; + + std::string interface_name = GetInterfaceName(target_appid, port_name); + +#define MAX_CNT 100 +#define MAX_SLEEP 100 +#define MIN_SLEEP 50 +#define BASE_SLEEP (1000 * 1000) + GDBusMessage* reply; + GError* err = nullptr; + struct timespec try_sleep_time = { 0, MIN_SLEEP * BASE_SLEEP }; + struct timespec start_time = { 0, }; + struct timespec end_time = { 0, }; + int max_timeout = MAX_CNT * MAX_SLEEP; + + do { + clock_gettime(CLOCK_MONOTONIC, &start_time); + GDBusMessage* msg = g_dbus_message_new_method_call(interface_name.c_str(), + RPC_PORT_OBJECT_PATH, interface_name.c_str(), "send_message"); + if (msg == nullptr) { + LOGE("g_dbus_message_new_method_call() is failed"); + return -1; + } + + g_dbus_message_set_unix_fd_list(msg, fd_list.GetRaw()); + + reply = g_dbus_connection_send_message_with_reply_sync( + DBusConnectionManager::GetInst().GetConnection(), + msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 500, nullptr, nullptr, &err); + clock_gettime(CLOCK_MONOTONIC, &end_time); + g_object_unref(msg); + + if (reply && !g_dbus_message_to_gerror(reply, &err)) + break; + + if (reply == nullptr) { + LOGE("g_dbus_connection_send_message_with_reply_sync() is failed"); + if (err) { + LOGE("error(%s)", err->message); + g_error_free(err); + } + } else if (g_dbus_message_to_gerror(reply, &err)) { + LOGE("error(%s) was set", err->message); + g_error_free(err); + g_object_unref(reply); + } + err = nullptr; + + max_timeout -= (((end_time.tv_sec - start_time.tv_sec) * 1000) + + ((end_time.tv_nsec - start_time.tv_nsec) / BASE_SLEEP)); + if (max_timeout <= 0) + break; + + nanosleep(&try_sleep_time, 0); + max_timeout -= (try_sleep_time.tv_nsec / BASE_SLEEP); + if (max_timeout <= 0) + break; + + try_sleep_time.tv_nsec *= 2; + if (try_sleep_time.tv_nsec > (MAX_SLEEP * BASE_SLEEP)) + try_sleep_time.tv_nsec = MAX_SLEEP * BASE_SLEEP; + + LOGD("Retry"); + } while (max_timeout > 0); + + if (max_timeout <= 0) { + LOGE("Timed out"); + return -1; + } + + GVariant* reply_body = g_dbus_message_get_body(reply); + if (reply_body == nullptr) { + LOGE("g_dbus_message_get_body() is failed"); + g_object_unref(reply); + return -1; + } + + g_variant_get(reply_body, "(i)", &ret); + if (ret != 0) { + LOGE("Access Denied[sender_appid: %s, result: %d]", sender_appid, ret); + g_object_unref(reply); + return -EILLEGALACCESS; + } + + LOGD("[Reply: %d]", ret); + + (*fds)[0] = main_sock_pair.Detach(SocketPair::SENDER); + (*fds)[1] = delegate_sock_pair.Detach(SocketPair::SENDER); + + return (*fds)[0]; +} + void FdBroker::ReceiveMessage(const char* sender_appid, GDBusMethodInvocation* invocation) { GDBusMessage* msg; @@ -557,27 +693,20 @@ void FdBroker::OnNameVanished(GDBusConnection *connection, int FdBroker::Watch(IEventWatcher* ev, const std::string& target_appid, const std::string& port_name) { - int r; - if (ev == nullptr) return -1; - if (!mock_) { - r = aul_rpc_port_prepare_stub(target_appid.c_str(), port_name.c_str()); - if (r != AUL_R_OK) { - LOGE("Failed to prepare stub %s:%s [ret : %d]", - target_appid.c_str(), port_name.c_str(), r); - return r; - } - } + int ret = Prepare(target_appid, port_name); + if (ret < 0) + return ret; watcher_ = ev; watch_appid_ = target_appid; watch_port_name_ = port_name; if (mock_) { - r = DBusMock::GetInst().Watch(ev, target_appid, port_name); - if (r < 0) + ret = DBusMock::GetInst().Watch(ev, target_appid, port_name); + if (ret < 0) return -1; return 0; @@ -608,6 +737,21 @@ int FdBroker::Watch(IEventWatcher* ev, const std::string& target_appid, return 0; } +int FdBroker::Prepare(const std::string& target_appid, + const std::string& port_name) { + if (!mock_) { + int ret = aul_rpc_port_prepare_stub(target_appid.c_str(), + port_name.c_str()); + if (ret != AUL_R_OK) { + LOGE("Failed to prepare stub %s:%s [ret : %d]", + target_appid.c_str(), port_name.c_str(), ret); + return ret; + } + } + + return 0; +} + void FdBroker::OnResultReceived(GObject* source_object, GAsyncResult* res, gpointer user_data) { diff --git a/src/fdbroker-internal.h b/src/fdbroker-internal.h index aac7584..ca489ab 100644 --- a/src/fdbroker-internal.h +++ b/src/fdbroker-internal.h @@ -65,6 +65,9 @@ class FdBroker { AccessController& GetAccessController(); int Watch(IEventWatcher* ev, const std::string& target_appid, const std::string& port_name); + int Prepare(const std::string& target_appid, const std::string& port_name); + int SendSync(const std::string& target_appid, const std::string& port_name, + int (*fds)[2]); private: class DBusConnectionManager { diff --git a/src/proxy-internal.cc b/src/proxy-internal.cc index 3546273..f09a493 100644 --- a/src/proxy-internal.cc +++ b/src/proxy-internal.cc @@ -179,6 +179,47 @@ int Proxy::Connect(std::string appid, std::string port_name, return RPC_PORT_ERROR_NONE; } +int Proxy::ConnectSync(std::string appid, std::string port_name, + IEventListener* ev) { + if (ev == nullptr) + return RPC_PORT_ERROR_INVALID_PARAMETER; + + if (listener_ != nullptr) { + LOGW("Already connected"); + return RPC_PORT_ERROR_INVALID_PARAMETER; + } + + listener_ = ev; + target_appid_ = std::move(appid); + port_name_ = std::move(port_name); + + int ret = fd_broker_.Prepare(target_appid_, port_name_); + if (ret < 0) { + listener_ = nullptr; + if (ret == -EILLEGALACCESS) + return RPC_PORT_ERROR_PERMISSION_DENIED; + + return RPC_PORT_ERROR_IO_ERROR; + } + + fds_[0] = 0; + fds_[1] = 0; + ret = fd_broker_.SendSync(target_appid_, port_name_, &fds_); + if (ret <= 0) { + listener_ = nullptr; + if (ret == -EILLEGALACCESS) + return RPC_PORT_ERROR_PERMISSION_DENIED; + + return RPC_PORT_ERROR_IO_ERROR; + } + + main_port_.reset(new ProxyPort(this, fds_[0], target_appid_, false)); + delegate_port_.reset(new ProxyPort(this, fds_[1], target_appid_)); + listener_->OnConnected(target_appid_, main_port_.get()); + + return RPC_PORT_ERROR_NONE; +} + gboolean Proxy::DbusNameTimeout(gpointer user_data) { Proxy* obj = static_cast(user_data); diff --git a/src/proxy-internal.h b/src/proxy-internal.h index dce40a0..75b30e2 100644 --- a/src/proxy-internal.h +++ b/src/proxy-internal.h @@ -44,6 +44,7 @@ class Proxy : public FdBroker::IEventWatcher { }; int Connect(std::string appid, std::string port_name, IEventListener* ev); + int ConnectSync(std::string appid, std::string port_name, IEventListener* ev); std::shared_ptr GetPort() const { return main_port_; diff --git a/src/rpc-port.cc b/src/rpc-port.cc index 48d7518..88046bb 100644 --- a/src/rpc-port.cc +++ b/src/rpc-port.cc @@ -232,6 +232,17 @@ RPC_API int rpc_port_proxy_connect(rpc_port_proxy_h h, const char* appid, return p->Connect(appid, port, p); } +RPC_API int rpc_port_proxy_connect_sync(rpc_port_proxy_h h, const char* appid, + const char* port) { + if (h == nullptr || appid == nullptr || port == nullptr) + return RPC_PORT_ERROR_INVALID_PARAMETER; + + auto p = static_cast<::ProxyExt*>(h); + std::lock_guard lock(p->GetMutex()); + + return p->ConnectSync(appid, port, p); +} + RPC_API int rpc_port_proxy_add_connected_event_cb(rpc_port_proxy_h h, rpc_port_proxy_connected_event_cb cb, void *user_data) { if (h == nullptr || cb == nullptr) -- 2.7.4 From ec77162303165e7e1f4f9592a6b91b184dc35781 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 11 Nov 2019 10:49:18 +0900 Subject: [PATCH 14/16] Release version 1.3.22 Changes: - Implement rpc_port_proxy_connect_sync() Change-Id: I8dc0ef343f518bb444d16b05ef0a9a5c68e38c9e Signed-off-by: Hwankyu Jhun --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index ede53ee..2dd4af6 100644 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.21 +Version: 1.3.22 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4 From 1d896e69b4de9faad0bb8ff9576c9b52c2afaa90 Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Wed, 13 Nov 2019 08:44:13 +0900 Subject: [PATCH 15/16] Add internal APIs for parcel - int rpc_port_parcel_reset_reader(rpc_port_parcel_h h); - int rpc_port_parcel_to_array(rpc_port_parcel_h h, void **array, unsigned int *size); - int rpc_port_parcel_from_array(rpc_port_parcel_h h, const void *array, unsigned int size); Change-Id: I62b659cb2c8b79e9f566303352dbb619ffd47300 Signed-off-by: Junghoon Park --- src/parcel-internal.cc | 14 ++++++++++++++ src/parcel-internal.h | 3 +++ src/rpc-port-parcel.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/parcel-internal.cc b/src/parcel-internal.cc index 3fce740..54f2d99 100644 --- a/src/parcel-internal.cc +++ b/src/parcel-internal.cc @@ -151,4 +151,18 @@ void Parcel::Read(unsigned char* buf, unsigned int size) { reader_ += size; } +void Parcel::ResetReader() { + reader_ = 0; +} + +void Parcel::Clear() { + data_.clear(); + reader_ = 0; +} + +void Parcel::Reset(const unsigned char* buf, unsigned int size) { + Clear(); + Write(buf, size); +} + } // namespace rpc_port diff --git a/src/parcel-internal.h b/src/parcel-internal.h index ac2290a..991fe17 100644 --- a/src/parcel-internal.h +++ b/src/parcel-internal.h @@ -54,6 +54,9 @@ class Parcel { bundle* ReadBundle(); int ReadArrayCount(); const std::vector& GetRaw(); + void ResetReader(); + void Clear(); + void Reset(const unsigned char* buf, unsigned int size); private: template diff --git a/src/rpc-port-parcel.cc b/src/rpc-port-parcel.cc index 55c10b2..8ce15d3 100644 --- a/src/rpc-port-parcel.cc +++ b/src/rpc-port-parcel.cc @@ -376,3 +376,45 @@ RPC_API int rpc_port_parcel_burst_write(rpc_port_parcel_h h, return RPC_PORT_ERROR_NONE; } + +RPC_API int rpc_port_parcel_reset_reader(rpc_port_parcel_h h) { + if (h == nullptr) + return RPC_PORT_ERROR_INVALID_PARAMETER; + + Parcel* p = static_cast(h); + + p->ResetReader(); + + return RPC_PORT_ERROR_NONE; +} + +RPC_API int rpc_port_parcel_to_array(rpc_port_parcel_h h, void **array, + unsigned int *size) { + if (h == nullptr || !array || !size) + return RPC_PORT_ERROR_INVALID_PARAMETER; + + Parcel* p = static_cast(h); + + const auto& ptr = p->GetRaw(); + void* array_ptr = malloc(ptr.size()); + if (!array_ptr) + return RPC_PORT_ERROR_OUT_OF_MEMORY; + + memcpy(array_ptr, ptr.data(), ptr.size()); + *array = array_ptr; + *size = ptr.size(); + + return RPC_PORT_ERROR_NONE; +} + +RPC_API int rpc_port_parcel_from_array(rpc_port_parcel_h h, const void *array, + unsigned int size) { + if (h == nullptr || !array || size == 0) + return RPC_PORT_ERROR_INVALID_PARAMETER; + + Parcel* p = static_cast(h); + + p->Reset(reinterpret_cast(array), size); + + return RPC_PORT_ERROR_NONE; +} \ No newline at end of file -- 2.7.4 From b9314682979ce01e26df8da5264e3131e6a26671 Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Wed, 13 Nov 2019 10:57:56 +0900 Subject: [PATCH 16/16] Release version 1.3.23 Changes: - Add internal APIs for parcel Change-Id: I7d5eabbc0cc2082d123cade32924faaeeeedce12 Signed-off-by: Junghoon Park --- packaging/rpc-port.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/rpc-port.spec b/packaging/rpc-port.spec index 2dd4af6..bf7dccb 100644 --- a/packaging/rpc-port.spec +++ b/packaging/rpc-port.spec @@ -1,6 +1,6 @@ Name: rpc-port Summary: RPC Port library -Version: 1.3.22 +Version: 1.3.23 Release: 0 Group: Application Framework/Libraries License: Apache-2.0 -- 2.7.4