Increase timeout of rpc-port 76/315476/2
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 1 Aug 2024 09:40:54 +0000 (18:40 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 1 Aug 2024 09:48:00 +0000 (18:48 +0900)
The default timeout is increased to 25 seconds.
The following functions are added internally:
 - rpc_port_set_timeout()
 - rpc_port_get_timeout()

Change-Id: I6ae46d956c30e33606a74cd312a1b37ac67c2b02
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
include/rpc-port-internal.h
src/rpc-port/CMakeLists.txt
src/rpc-port/client-socket-internal.cc
src/rpc-port/port-internal.cc
src/rpc-port/proxy-internal.cc
src/rpc-port/rpc-port-internal.cc
test/unit_tests/rpc_port_test.cc

index cb557ea968d806a2069be6fba3fda74d2a226fb3..f666e48639464a4c7de40d09d37ba9872619a3db 100644 (file)
@@ -82,10 +82,31 @@ int rpc_port_get_peer_info(rpc_port_h h, pid_t *pid, uid_t *uid);
  * @param[out] has_request The flag, if true, the pending request exists
  * @return @c 0 on success,
  *         otherwise a negative error value
+ * @retval #RPC_PORT_ERROR_NONE Successful
  * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter
  */
 int rpc_port_stub_has_pending_request(rpc_port_stub_h h, bool *has_request);
 
+/**
+ * @brief Sets the timeout for the rpc port.
+ * @since_tizen 9.0
+ * @param[in] timeout The timeout in milliseconds
+ * @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_get_timeout()
+ */
+int rpc_port_set_timeout(int timeout);
+
+/**
+ * @brief Gets the timeout of the rpc port.
+ * @since_tizen 9.0
+ * @return The timeout in milliseconds
+ * @see rpc_port_set_timeout()
+ */
+int rpc_port_get_timeout(void);
+
 #ifdef __cplusplus
 }
 #endif
index fdee1072712e0434ff7316b1d89917c5e4562326..b38b2e25553d70abbfc4fa21947c055d20e86f41 100644 (file)
@@ -1,3 +1,5 @@
+ADD_DEFINITIONS("-DRPC_PORT_TIMEOUT=25000")
+
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} RPC_PORT_SRCS)
 
 ADD_LIBRARY(${TARGET_RPC_PORT} SHARED ${RPC_PORT_SRCS})
index c982e3150784c6d12f9f4f9e030edda875c6eea5..d2361fa82935fb79b25447906b772524b80b8a68 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/un.h>
 #include <unistd.h>
 
+#include "include/rpc-port-internal.h"
 #include "rpc-port/exception-internal.hh"
 #include "rpc-port/log-private.hh"
 
@@ -56,9 +57,14 @@ ClientSocket::ClientSocket() {
     _E("socket() is failed. errno(%d)", errno);
     THROW(fd_);
   }  // LCOV_EXCL_STOP
+
+  SetReceiveTimeout(-1);
 }
 
-ClientSocket::ClientSocket(int fd) : fd_(fd) { SetCloseOnExec(); }
+ClientSocket::ClientSocket(int fd) : fd_(fd) {
+  SetCloseOnExec();
+  SetReceiveTimeout(-1);
+}
 
 ClientSocket::~ClientSocket() {
   if (!IsClosed()) Close();
@@ -169,7 +175,7 @@ void ClientSocket::SetReceiveTimeout(int timeout) {
   if (timeout == INT_MAX) return;  // LCOV_EXCL_LINE
 
   if (timeout == -1)
-    timeout = 5000;
+    timeout = rpc_port_get_timeout();
 
   if (timeout < 0) {  // LCOV_EXCL_START
     _E("Invalid parameter");
index e52d4543207b55fd63c17459a650900997a8ca26..4924ebdba6e3346c735e2073974c54252b3187f2 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "rpc-port/port-internal.hh"
+
 #include <aul_rpc_port.h>
 #include <dlog.h>
 #include <fcntl.h>
 #include <chrono>
 #include <utility>
 
+#include "include/rpc-port-internal.h"
 #include "include/rpc-port.h"
-#include "log-private.hh"
-#include "message-sending-thread-internal.hh"
-#include "port-internal.hh"
+#include "rpc-port/log-private.hh"
+#include "rpc-port/message-sending-thread-internal.hh"
 
 namespace rpc_port {
 namespace internal {
@@ -60,7 +62,7 @@ Port::Port(int read_fd, int write_fd, std::string id)
   uuid_generate(u);
   uuid_unparse(u, uuid);
   instance_ = std::string(uuid) + ":" + id_;
-  SetReceiveTimeout(10000);
+  if (read_fd > -1) SetReceiveTimeout(-1);
 }
 
 Port::Port(int read_fd, int write_fd, std::string id, std::string instance)
@@ -69,7 +71,7 @@ Port::Port(int read_fd, int write_fd, std::string id, std::string instance)
       id_(std::move(id)),
       instance_(std::move(instance)),
       seq_(0) {
-  SetReceiveTimeout(10000);
+  if (read_fd > -1) SetReceiveTimeout(10000);
 }
 
 Port::~Port() {
@@ -171,7 +173,7 @@ int Port::Read(void* buf, unsigned int size) {
 int Port::SetReceiveTimeout(int timeout) {
   if (timeout == INT_MAX) return -EINVAL;
 
-  if (timeout == -1) timeout = 10000;
+  if (timeout == -1) timeout = rpc_port_get_timeout();
 
   if (timeout < 0) {
     _E("Invalid parameter");
@@ -287,7 +289,10 @@ int Port::Write(const void* buf, unsigned int size, int* sent_bytes) {
   return PORT_STATUS_ERROR_NONE;
 }
 
-void Port::SetReadFd(int read_fd) { read_fd_ = read_fd; }
+void Port::SetReadFd(int read_fd) {
+  read_fd_ = read_fd;
+  if (read_fd_ > -1) SetReceiveTimeout(-1);
+}
 
 int Port::GetReadFd() const { return read_fd_; }
 
index 1bc692db701efd91b6c30290f0b7d011d0f9706b..ac2f164ade47ea01619465b00993fbb60f8e6301 100644 (file)
@@ -240,7 +240,7 @@ int Proxy::ConnectSync(std::string appid, std::string port_name,
 
 bool Proxy::WaitUntilPortCreation() {
   file_monitor_.reset(new FileMonitor(port_path_));
-  int retry_count = 100;
+  int retry_count = rpc_port_get_timeout() / 1000;
   do {
     if (file_monitor_->Exist()) return true;
 
@@ -341,7 +341,7 @@ void Proxy::SetConnTimer() {
     // LCOV_EXCL_STOP
   }
 
-  GSource* source = g_timeout_source_new(10 * 1000);
+  GSource* source = g_timeout_source_new(rpc_port_get_timeout());
   if (source == nullptr) {
     // LCOV_EXCL_START
     _E("g_timeout_source_new() is failed");
index a54b8eb3bedbe99ce9e9d4cde7b559e1df8cd515..d3833584bc67cc390cb4cf56839fd250b459fd22 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "include/rpc-port-internal.h"
+
 #include <aul_proc.h>
 #include <sys/types.h>
 #include <tzplatform_config.h>
 
 #include <atomic>
 
-#include "include/rpc-port-internal.h"
 #include "include/rpc-port.h"
-#include "peer-cred-internal.hh"
-#include "port-internal.hh"
+#include "rpc-port/log-private.hh"
+#include "rpc-port/peer-cred-internal.hh"
+#include "rpc-port/port-internal.hh"
 
 #undef RPC_API
 #define RPC_API extern "C" __attribute__((visibility("default")))
@@ -34,6 +36,7 @@ using namespace rpc_port::internal;
 
 constexpr uid_t kRegularUidMin = 5000;
 std::atomic<uid_t> __target_uid { getuid() };
+std::atomic<int> rpc_port_timeout { RPC_PORT_TIMEOUT };
 
 }  // namespace
 
@@ -68,21 +71,32 @@ RPC_API int rpc_port_deregister_proc_info(void) {
   return RPC_PORT_ERROR_NONE;
 }
 
-RPC_API int  rpc_port_get_peer_info(rpc_port_h h, pid_t* pid, uid_t* uid) {
-  if (h == nullptr)
-    return RPC_PORT_ERROR_INVALID_PARAMETER;
+RPC_API int rpc_port_get_peer_info(rpc_port_h h, pid_t* pid, uid_t* uid) {
+  if (h == nullptr) return RPC_PORT_ERROR_INVALID_PARAMETER;
 
   auto port = static_cast<Port*>(h);
   std::shared_ptr<PeerCred> cred(PeerCred::Get(port->GetReadFd()));
-  if (cred.get() == nullptr)
-    return RPC_PORT_ERROR_IO_ERROR;
+  if (cred.get() == nullptr) return RPC_PORT_ERROR_IO_ERROR;
+
+  if (pid) *pid = cred->GetPid();
+  if (uid) *uid = cred->GetUid();
 
-  if (pid)
-    *pid = cred->GetPid();
+  return RPC_PORT_ERROR_NONE;
+}
+
+RPC_API int rpc_port_set_timeout(int timeout) {
+  if (timeout == INT_MAX) return RPC_PORT_ERROR_INVALID_PARAMETER;
 
-  if (uid)
-    *uid = cred->GetUid();
+  if (timeout == -1) timeout = RPC_PORT_TIMEOUT;
 
+  if (timeout < 0) return RPC_PORT_ERROR_INVALID_PARAMETER;
+
+  _E("timeout=%d", timeout);
+  rpc_port_timeout.exchange(timeout);
   return RPC_PORT_ERROR_NONE;
 }
 
+RPC_API int rpc_port_get_timeout(void) {
+  set_last_result(RPC_PORT_ERROR_NONE);
+  return rpc_port_timeout;
+}
index a5f93e77edb1e368312bc953c36e09a8558fde27..17b4687417cb7b8ad8a59577108a773e83e775e2 100644 (file)
@@ -550,10 +550,11 @@ TEST_F(RpcPortBase, rpc_port_proxy_event_reject_N) {
     }, this);
   ASSERT_EQ(ret, 0);
 
+  rpc_port_set_timeout(5000);
   ret = rpc_port_proxy_connect(proxy_handle_, "TestApp", "wrong_port");
   ASSERT_EQ(ret, 0);
 
-  guint tag = g_timeout_add_seconds(15,
+  guint tag = g_timeout_add_seconds(10,
       [](gpointer data) -> gboolean {
         auto* p = static_cast<RpcPortBase*>(data);
         p->Finish();
@@ -563,6 +564,7 @@ TEST_F(RpcPortBase, rpc_port_proxy_event_reject_N) {
   RunMainLoop();
   ASSERT_TRUE(touch_proxy_rejected_event_cb_);
   g_source_remove(tag);
+  rpc_port_set_timeout(-1);
 }
 
 TEST_F(RpcPortConnection, rpc_port_proxy_event_receive_p) {
@@ -666,3 +668,16 @@ TEST_F(RpcPortConnection, rpc_port_disconnect_P) {
   ASSERT_TRUE(touch_stub_disconnected_event_cb_);
   ASSERT_TRUE(touch_proxy_disconnected_event_cb_);
 }
+
+TEST_F(RpcPortConnection, rpc_port_set_timeout_P) {
+  int ret = rpc_port_set_timeout(1000);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+  ASSERT_EQ(rpc_port_get_timeout(), 1000);
+  rpc_port_set_timeout(-1);
+}
+
+TEST_F(RpcPortConnection, rpc_port_get_timeout_P) {
+  int timeout = rpc_port_get_timeout();
+  ASSERT_EQ(get_last_result(), RPC_PORT_ERROR_NONE);
+  ASSERT_NE(timeout, 0);
+}