Apply aul request with timeout 07/285307/18
authorChanggyu Choi <changyu.choi@samsung.com>
Fri, 9 Dec 2022 02:04:11 +0000 (11:04 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Tue, 3 Jan 2023 06:24:33 +0000 (06:24 +0000)
Some apps want to set aul request timeout.
If there is a timeout value in the bundle, it changes the socket timeout to that value.

Change-Id: Ic8fd03dbce72ebed3ebc669f4ac11e2776eeaa6d
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
include/aul_key.h
src/aul_launch.c
src/aul_sock.cc
src/aul_svc_priv_key.h
src/aul_worker.c
src/aul_worker.h

index 31911b8..1a8a60f 100644 (file)
  * @brief Definition for AUL: The value of indicating a enabled light user for app.
  * @since_tizen 7.0
  */
-#define AUL_K_ENABLED_LIGHT_USER      "__AUL_ENABLED_LIGHT_USER__"
+#define AUL_K_ENABLED_LIGHT_USER        "__AUL_ENABLED_LIGHT_USER__"
 
 /**
  * @brief Definition for AUL: The complication mode.
  * @brief Definition for AUL: The package event name.
  * @since_tizen 7.0
  */
-#define AUL_K_PKG_EVENT_NAME "__AUL_K_PKG_EVENT_NAME__"
+#define AUL_K_PKG_EVENT_NAME            "__AUL_K_PKG_EVENT_NAME__"
 
 /**
  * @brief Definition for AUL: The package event
  * @since_tizen 7.0
  */
-#define AUL_K_PKG_EVENT_RESULT "__AUL_K_PKG_EVENT_RESULT__"
+#define AUL_K_PKG_EVENT_RESULT          "__AUL_K_PKG_EVENT_RESULT__"
+
+
+/**
+ * @brief Definition for AUL: The socket timeout milli seconds.
+ * @since_tizen 7.5
+ */
+#define AUL_K_SOCKET_TIMEOUT            "__AUL_SOCKET_TIMEOUT__"
index 5938207..528a1c8 100644 (file)
@@ -507,10 +507,13 @@ static GMainContext *__get_glib_context(int cmd, bundle *b)
        return context;
 }
 
-static void __process_app_pkt(app_pkt_t *pkt, int clifd)
+static void __process_app_pkt(app_pkt_t *pkt, int clifd, aul_worker_h worker)
 {
        struct aul_request_s *req;
        bundle *b = NULL;
+       char *timeout_str;
+       unsigned int timeout = 5000;
+       int ret;
 
        if (pkt->opt & AUL_SOCK_BUNDLE) {
                b = bundle_decode(pkt->data, pkt->len);
@@ -518,6 +521,12 @@ static void __process_app_pkt(app_pkt_t *pkt, int clifd)
                        _E("Failed to decode the packet");
                        return;
                }
+
+               ret = bundle_get_str(b, AUL_K_SOCKET_TIMEOUT, &timeout_str);
+               if (ret == BUNDLE_ERROR_NONE && isdigit(timeout_str[0])) {
+                       timeout = atoi(timeout_str);
+                       _I("timeout: %ums", timeout);
+               }
        }
 
        req = __create_request(pkt->cmd, clifd, b);
@@ -526,6 +535,7 @@ static void __process_app_pkt(app_pkt_t *pkt, int clifd)
                return;
        }
 
+       aul_worker_add_anr_timer(worker, pkt->cmd, timeout);
        __g_idle_add_full(__get_glib_context(pkt->cmd, b), G_PRIORITY_DEFAULT,
                        __dispatch_request, req);
 }
@@ -559,8 +569,7 @@ static bool __received_event_cb(int fd, int condition, void *user_data)
                return true;
        }
 
-       aul_worker_add_anr_timer(worker, pkt->cmd);
-       __process_app_pkt(pkt, fd);
+       __process_app_pkt(pkt, fd, worker);
        free(pkt);
 
        return true;
@@ -600,8 +609,7 @@ static bool __connected_event_cb(int fd, int condition, void *user_data)
                }
        }
 
-       aul_worker_add_anr_timer(worker, pkt->cmd);
-       __process_app_pkt(pkt, clifd);
+       __process_app_pkt(pkt, clifd, worker);
 
        if (pkt->cmd == WIDGET_GET_CONTENT)
                clifd = -1;
index d301d11..f74c4ab 100644 (file)
@@ -33,6 +33,7 @@
 #include <bundle_cpp.h>
 
 #include "aul_api.h"
+#include "aul_key.h"
 #include "aul_util.h"
 
 #include "aul/socket/client_socket.hh"
@@ -50,6 +51,9 @@ constexpr const char PATH_AMD_SOCK[] = "/run/aul/daemons/.amd-sock";
 int MAX_FDS = sysconf(_SC_OPEN_MAX);
 constexpr const char DEST_PROCESS[] = "amd";
 
+constexpr unsigned int SOCKET_TIMEOUT_MIN = 5000U;
+constexpr unsigned int SOCKET_TIMEOUT_MAX = 30000U;
+
 // POD type
 struct PacketHeader {
   int cmd;
@@ -270,6 +274,26 @@ int SendAndReceive(int pid, uid_t uid, int cmd, unsigned char* data,
   return ret;
 }
 
+int SendAndReceiveWithTimeout(int pid, uid_t uid, int cmd, unsigned char* data,
+    int datalen, int opt, int timeout) {
+  int ret;
+  try {
+    ClientSocket client;
+    client.Connect(GetSocketPath(pid, uid));
+    aul_sock_set_sock_option(client.GetFd(), 1);
+    _W("timeout: %dms", timeout);
+    client.SetReceiveTimeout(timeout);
+
+    const auto& inherit = CPUInheritance(pid, cmd);
+    ret = SendAndReceive(&client, cmd, data, datalen, opt);
+  } catch (const Exception& e) {
+    _E("Exception occurs. error(%d)", e.GetErrorCode());
+    return e.GetErrorCode();
+  }
+
+  return ret;
+}
+
 int ReceiveAppPacket(ClientSocket* client, app_pkt_t** out_pkt) {
   PacketHeader header;
   static_assert(std::is_standard_layout<PacketHeader>(),
@@ -422,6 +446,10 @@ void DeleteDirectories(const std::string& path) {
   closedir(dp);
 }
 
+unsigned int GetSocketTimeout(unsigned int timeout) {
+  return std::min(std::max(timeout, SOCKET_TIMEOUT_MIN), SOCKET_TIMEOUT_MAX);
+}
+
 SocketTimeout socket_timeout;
 SocketLink socket_link;
 
@@ -526,10 +554,25 @@ extern "C" API int aul_sock_send_bundle(int pid, uid_t uid, int cmd,
     return -EINVAL;
 
   tizen_base::Bundle b(kb, false, false);
-  auto raw = b.ToRaw();
-  return SendAndReceive(pid, uid, cmd,
-      reinterpret_cast<unsigned char*>(raw.first.get()), raw.second,
-      opt | AUL_SOCK_BUNDLE);
+  std::string timeout_str = b.GetString(AUL_K_SOCKET_TIMEOUT);
+  if (timeout_str.empty() || !isdigit(timeout_str[0])) {
+    auto [ptr, size] = b.ToRaw();
+    return SendAndReceive(pid, uid, cmd,
+        reinterpret_cast<unsigned char*>(ptr.get()),
+        size, opt | AUL_SOCK_BUNDLE);
+  }
+
+  unsigned int timeout = static_cast<unsigned int>(std::stoi(timeout_str));
+  if (timeout < SOCKET_TIMEOUT_MIN || timeout > SOCKET_TIMEOUT_MAX) {
+    timeout = GetSocketTimeout(timeout);
+    b.Delete(AUL_K_SOCKET_TIMEOUT);
+    b.Add(AUL_K_SOCKET_TIMEOUT, std::to_string(timeout));
+  }
+
+  auto [ptr, size] = b.ToRaw();
+  return SendAndReceiveWithTimeout(
+      pid, uid, cmd, reinterpret_cast<unsigned char*>(ptr.get()), size,
+      opt | AUL_SOCK_BUNDLE, timeout);
 }
 
 extern "C" API app_pkt_t* aul_sock_recv_pkt(int fd, int* clifd,
index a3b03ae..c3c0cc3 100644 (file)
@@ -34,5 +34,3 @@
 #define AUL_SVC_K_WIN_ID    "__APP_SVC_K_WIN_ID__"
 /** AUL SVC internal private key */
 #define AUL_SVC_K_LAUNCH_MODE   "__APP_SVC_LAUNCH_MODE__"
-
-
index 511effa..ee6f497 100644 (file)
@@ -488,7 +488,7 @@ static gboolean __timeout_cb(gpointer data)
        return G_SOURCE_REMOVE;
 }
 
-int aul_worker_add_anr_timer(aul_worker_h handle, int cmd)
+int aul_worker_add_anr_timer(aul_worker_h handle, int cmd, unsigned int timeout)
 {
        struct aul_worker_s *worker = handle;
        struct anr_timer_s *anr_timer;
@@ -516,12 +516,12 @@ int aul_worker_add_anr_timer(aul_worker_h handle, int cmd)
 
        current_time = g_get_monotonic_time();
        if (anr_timer->start_time == 0) {
-               interval = DEFAULT_INTERVAL;
+               interval = timeout;
                anr_timer->start_time = current_time;
        } else {
                elapsed_time = (unsigned int)((current_time -
                                anr_timer->start_time) / 1000);
-               interval = DEFAULT_INTERVAL + (DEFAULT_INTERVAL - elapsed_time);
+               interval = timeout + (DEFAULT_INTERVAL - elapsed_time);
                anr_timer->start_time = current_time;
        }
 
index afdd79a..5f8ff2f 100644 (file)
@@ -42,7 +42,8 @@ int aul_worker_add_io_job(aul_worker_h handle, const char *job_name,
 
 void aul_worker_remove_io_job(aul_worker_h handle, int fd);
 
-int aul_worker_add_anr_timer(aul_worker_h handle, int cmd);
+int aul_worker_add_anr_timer(aul_worker_h handle,
+               int cmd, unsigned int timeout);
 
 int aul_worker_remove_anr_timer(aul_worker_h handle);