Handle new AUL commands related to proc group 50/296250/4
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 24 Jul 2023 09:47:28 +0000 (18:47 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 24 Jul 2023 23:27:40 +0000 (08:27 +0900)
The amd-mod-proc-group module handles PROC_GROUP_FOREACH and
PROC_GROUP_GET commands.

Requires:
 - https://review.tizen.org/gerrit/#/c/platform/core/appfw/aul-1/+/296246/

Change-Id: I8b87fc67be3e754569a6e68c4887dbaedd82ada6
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/lib/amd_util.c
src/lib/api/amd_api_noti_msg.h
src/modules/proc-group/CMakeLists.txt
src/modules/proc-group/amd_proc_group.cc
src/modules/ui-core/src/app_group.c

index 9fd9753a756a82a15eba2d2ad8e2a58e49197f34..7ad681b98b7cb01ccfa8a079786017a88c5fadb4 100644 (file)
@@ -338,8 +338,9 @@ static int __dispatch_app_set_process_group(request_h req)
        }
 
        ret = _signal_send_app_group_signal(owner_pid, child_pid, child_pkgid);
-
        _request_send_result(req, ret);
+       _noti_send(AMD_NOTI_MSG_UTIL_APP_SET_PROCESS_GROUP, owner_pid, child_pid,
+                       NULL, NULL);
        return 0;
 }
 
index 8264d3f192b1914cf18b98771317d9881d1dc6b1..bb0410b2dc15568a6dd97c1567d6952881d5014a 100644 (file)
@@ -1032,6 +1032,28 @@ extern "C" {
 #define AMD_NOTI_MSG_LAUNCH_MANAGER_APP_LAUNCHED                               \
        "launch_manager.app.launched"
 
+/**
+ * @brief Definition for the notification message: The app process group set.
+ * @details Input: arg1(int) The leader process ID.\n
+ *          Input: arg2(int) The sub process ID.\n
+ * @since_tizen 8.0
+ * 
+ * @see __dispatch_app_set_process_group()
+ */
+#define AMD_NOTI_MSG_UTIL_APP_SET_PROCESS_GROUP                                \
+       "util.app_set_process_group"
+
+/**
+ * @brief Definition for the notification message: The app group set.
+ * @details Input: arg1(int) The leader process ID.\n
+ *          Input: arg2(int) The sub process ID.\n
+ * @since_tizen 8.0
+ * 
+ * @see __app_group_context_set_status()
+ */
+#define AMD_NOTI_MSG_APP_GROUP_SET                                             \
+       "app_group.set"
+
 #ifdef __cplusplus
 }
 #endif
index bfb4b1dc418efdb13d8a9788edd06137f310c620..b1bbe540c0c06334e58f3b5505dc91b79762b835 100644 (file)
@@ -4,6 +4,7 @@ ADD_LIBRARY(${TARGET_AMD_MOD_PROC_GROUP} ${AMD_MOD_PROC_GROUP_SRCS})
 
 TARGET_INCLUDE_DIRECTORIES(${TARGET_AMD_MOD_PROC_GROUP} PUBLIC
   ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CMAKE_CURRENT_SOURCE_DIR}/../
   ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/api)
 
 TARGET_LINK_LIBRARIES(${TARGET_AMD_MOD_PROC_GROUP} PRIVATE
index 54f22bc699fab5d9d908c2a65c6636e0be3c2c3d..273359ec3413eaf1d500d76a5a91fb2ac3e8f4bf 100644 (file)
@@ -16,7 +16,9 @@
 
 #include <aul.h>
 #include <aul_cmd.h>
+#include <aul_sock.h>
 #include <amd.h>
+#include <bundle_cpp.h>
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -27,7 +29,7 @@
 #include <string>
 #include <utility>
 
-#include "log_private.hh"
+#include "proc-group/log_private.hh"
 
 #undef EXPORT
 #define EXPORT __attribute__ ((visibility("default")))
@@ -65,6 +67,18 @@ class ProcGroup {
     return pid_;
   }
 
+  bool Contains(pid_t pid) const {
+    return std::find(childs_.begin(), childs_.end(), pid) != childs_.end();
+  }
+
+  std::list<int>::const_iterator begin() const {
+    return childs_.cbegin();
+  }
+
+  std::list<int>::const_iterator end() const {
+    return childs_.cend();
+  }
+
  private:
   pid_t pid_;
   std::list<int> childs_;
@@ -81,25 +95,44 @@ class ProcGroupManager {
     return inst;
   }
 
-  void AddProcGroup(pid_t pid) {
-    if (map_.find(pid) != map_.end())
-      return;
+  std::shared_ptr<ProcGroup> AddProcGroup(pid_t pid) {
+    auto group = FindProcGroup(pid);
+    if (group != nullptr)
+      return group;
 
-    map_[pid] = std::make_shared<ProcGroup>(pid);
+    group = std::make_shared<ProcGroup>(pid);
+    map_[pid] = group;
+    return group;
   }
 
   void RemoveProcGroup(pid_t pid) {
     auto found = map_.find(pid);
-    if (found == map_.end())
+    if (found == map_.end()) {
+      for (auto& iter : map_) {
+        auto& group = iter.second;
+        if (group->Contains(pid)) {
+          group->Remove(pid);
+          break;
+        }
+      }
+
       return;
+    }
 
     map_.erase(found);
   }
 
   std::shared_ptr<ProcGroup> FindProcGroup(pid_t pid) {
     auto found = map_.find(pid);
-    if (found == map_.end())
+    if (found == map_.end()) {
+      for (auto& iter : map_) {
+        auto& group = iter.second;
+        if (group->Contains(pid))
+          return group;
+      }
+
       return nullptr;
+    }
 
     return found->second;
   }
@@ -108,6 +141,10 @@ class ProcGroupManager {
     map_.clear();
   }
 
+  const std::unordered_map<pid_t, std::shared_ptr<ProcGroup>>& GetMap() const {
+    return map_;
+  }
+
  private:
   std::unordered_map<pid_t, std::shared_ptr<ProcGroup>> map_;
 };
@@ -141,10 +178,9 @@ static int DispatchProcGroupAdd(amd_request_h req) {
   }
 
   auto& inst = ProcGroupManager::GetInst();
-  inst.AddProcGroup(pgid);
+  auto group = inst.AddProcGroup(pgid);
 
   if (pid != pgid) {
-    auto group = inst.FindProcGroup(pgid);
     if (group->Add(pid) == 0) {
       const char* pkgid = amd_app_status_get_pkgid(app_status);
       amd_signal_send_app_group_signal(pgid, pid, pkgid);
@@ -175,7 +211,50 @@ static int DispatchProcGroupRemove(amd_request_h req) {
   }
 
   amd_request_send_result(req, 0);
-  _W("[PROC_DEREGISTER] pgid(%d), pid(%d)", pgid, pid);
+  _W("[PROC_GROUP_REMOVE] pgid(%d), pid(%d)", pgid, pid);
+  return 0;
+}
+
+static int DispatchProcGroupForeach(amd_request_h req) {
+  auto& inst = ProcGroupManager::GetInst();
+  int index = 0;
+  tizen_base::Bundle b;
+  b.Add("__K_LENGTH", std::to_string(inst.GetMap().size()));
+  for (auto& iter : inst.GetMap()) {
+    std::vector<std::string> pids;
+    auto group = iter.second;
+    pids.push_back(std::to_string(iter.first));
+    for (auto& pid : *group)
+      pids.push_back(std::to_string(pid));
+
+    b.Add(std::to_string(index++), pids);
+  }
+
+  aul_sock_send_bundle_with_fd(amd_request_remove_fd(req), APP_GET_INFO_OK,
+      b.GetHandle(), AUL_SOCK_NOREPLY);
+  _W("[PROC_GROUP_FOREACH]");
+  return 0;
+}
+
+static int DispatchProcGroupGet(amd_request_h req) {
+  pid_t pid = GetPidFromBundle(amd_request_get_bundle(req));
+  if (pid < 1) {
+    amd_request_send_result(req, -EINVAL);
+    return -1;
+  }
+
+  auto& inst = ProcGroupManager::GetInst();
+  auto group = inst.FindProcGroup(pid);
+  std::vector<std::string> pids;
+  pids.push_back(std::to_string(group->GetPid()));
+  for (auto& p : *group)
+    pids.push_back(std::to_string(p));
+
+  tizen_base::Bundle b;
+  b.Add("__K_GROUP_INFO", pids);
+  aul_sock_send_bundle_with_fd(amd_request_remove_fd(req), APP_GET_INFO_OK,
+      b.GetHandle(), AUL_SOCK_NOREPLY);
+  _W("[PROC_GROUP_GET]");
   return 0;
 }
 
@@ -194,6 +273,18 @@ static int CynaraCheckerProcGroup(amd_cynara_caller_info_h info,
   return AMD_CYNARA_RET_ALLOWED;
 }
 
+static int OnAppGroupSet(const char* msg, int arg1, int arg2, void* arg3,
+    bundle* arg4) {
+  pid_t leader_pid = static_cast<pid_t>(arg1);
+  pid_t sub_pid = static_cast<pid_t>(arg2);
+  auto& inst = ProcGroupManager::GetInst();
+  auto group = inst.AddProcGroup(leader_pid);
+  if (!group->Contains(sub_pid))
+    group->Add(sub_pid);
+
+  return AMD_NOTI_CONTINUE;
+}
+
 static int OnLaunchMainAppDead(const char* msg, int arg1, int arg2, void* arg3,
     bundle* arg4) {
   ProcGroupManager::GetInst().RemoveProcGroup(arg1);
@@ -211,6 +302,14 @@ extern "C" EXPORT int AMD_MOD_INIT(void) {
       .cmd = PROC_GROUP_REMOVE,
       .callback = DispatchProcGroupRemove
     },
+    {
+      .cmd = PROC_GROUP_FOREACH,
+      .callback = DispatchProcGroupForeach
+    },
+    {
+      .cmd = PROC_GROUP_GET,
+      .callback = DispatchProcGroupGet
+    },
   };
   static amd_cynara_checker cynara_checkers[] = {
     {
@@ -225,6 +324,18 @@ extern "C" EXPORT int AMD_MOD_INIT(void) {
       .data = nullptr,
       .priority = 10
     },
+    {
+      .cmd = PROC_GROUP_FOREACH,
+      .checker = amd_cynara_simple_checker,
+      .data = static_cast<void*>(const_cast<char*>(PRIVILEGE_PLATFORM)),
+      .priority = 10
+    },
+    {
+      .cmd = PROC_GROUP_GET,
+      .checker = amd_cynara_simple_checker,
+      .data = static_cast<void*>(const_cast<char*>(PRIVILEGE_PLATFORM)),
+      .priority = 10
+    },
   };
 
   int ret = amd_request_register_cmds(dispatch_table,
@@ -238,6 +349,8 @@ extern "C" EXPORT int AMD_MOD_INIT(void) {
     return -1;
 
   amd_noti_listen(AMD_NOTI_MSG_LAUNCH_MAIN_APP_DEAD, OnLaunchMainAppDead);
+  amd_noti_listen(AMD_NOTI_MSG_UTIL_APP_SET_PROCESS_GROUP, OnAppGroupSet);
+  amd_noti_listen(AMD_NOTI_MSG_APP_GROUP_SET, OnAppGroupSet);
   return 0;
 }
 
index 350a41d4119c803893522d67b2874d7ba6e9f419..26fb37a66275825778fc8b1b0c8236da8f99c37b 100644 (file)
@@ -1630,6 +1630,7 @@ static void __app_group_context_set_status(struct app_group_context_s *ctx,
                                amd_signal_send_app_group_signal(leader_pid,
                                                pid, pkgid);
                                ctx->group_sig = true;
+                               amd_noti_send(AMD_NOTI_MSG_APP_GROUP_SET, leader_pid, pid, NULL, NULL);
                        }
                } else {
                        __app_group_set_flag(group, pid, false, force);