Add a new function to get pkgid async 11/311211/3
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 16 May 2024 06:29:14 +0000 (15:29 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 16 May 2024 06:38:50 +0000 (15:38 +0900)
Adds:
 - aul_app_get_pkgid_bypid_async()

Change-Id: Iee697515a47081d786a21ed60737c9edc9b41369
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul/include/aul.h
src/aul/pkginfo.cc
src/tool/aul_test/tests/aul_app_get_pkgid_bypid_async_test.cc [new file with mode: 0644]

index 8e1dd6c..dc68d89 100644 (file)
@@ -3186,16 +3186,17 @@ int aul_launch_worker_init(void);
 void aul_launch_worker_fini(void);
 
 /**
- * @brief Called when the application ID is delivered.
+ * @brief Called when the ID is delivered.
  * @since_tizen 8.0
  * @param[in]   result          The result
  * @param[in]   pid             The process ID
- * @param[in]   appid           The application ID
+ * @param[in]   id              The ID
  * @param[in]   user_data       The user data passed from the regitration function
  * @remarks This function is only for App Framework internally.
  * @see aul_app_get_appid_bypid_async()
+ * @see aul_app_get_pkgid_bypid_async()
  */
-typedef void (*aul_appid_cb)(int result, pid_t pid, const char *appid, void *user_data);
+typedef void (*aul_get_id_cb)(int result, pid_t pid, const char *appid, void *user_data);
 
 /**
  * @brief Gets the application ID from the process ID.
@@ -3210,9 +3211,9 @@ typedef void (*aul_appid_cb)(int result, pid_t pid, const char *appid, void *use
  * @retval      #AUL_R_ENOMEM   Out of memory
  * @retval      #AUL_R_ERROR    Internal I/O error
  * @remarks This function is only for App Framework internally.
- * @see aul_appid_cb()
+ * @see aul_get_id_cb()
  */
-int aul_app_get_appid_bypid_async(pid_t pid, aul_appid_cb callback, void *user_data);
+int aul_app_get_appid_bypid_async(pid_t pid, aul_get_id_cb callback, void *user_data);
 
 /**
  * @brief Called when the result is delivered.
@@ -3378,6 +3379,23 @@ int aul_restart_loader(const char *loader_name);
  */
 int aul_restart_loader_for_uid(const char *loader_name, uid_t uid);
 
+/**
+ * @brief Gets the package ID from the process ID.
+ * @since_tizen 8.0
+ * @param[in]   pid             The process ID
+ * @param[in]   callback        The callback function
+ * @param[in]   user_data       The user data to be passed to the callaback function
+ * @return      @c 0 on success,
+ *              otherwise a negative error value
+ * @retval      #AUL_R_OK       Successful
+ * @retval      #AUL_R_EINVAL   Invalid parameter
+ * @retval      #AUL_R_ENOMEM   Out of memory
+ * @retval      #AUL_R_ERROR    Internal I/O error
+ * @remarks This function is only for App Framework internally.
+ * @see aul_get_id_cb()
+ */
+int aul_app_get_pkgid_bypid_async(pid_t pid, aul_get_id_cb callback, void *user_data);
+
 #ifdef __cplusplus
         }
 #endif
index 6aa7d2f..dab6003 100644 (file)
@@ -54,7 +54,7 @@ constexpr const char kPathAmdReady[] = "/run/.amd_ready";
 
 class ResultInfo {
  public:
-  ResultInfo(pid_t pid, int fd, aul_appid_cb callback, void* user_data)
+  ResultInfo(pid_t pid, int fd, aul_get_id_cb callback, void* user_data)
       : pid_(pid), fd_(fd), callback_(callback), user_data_(user_data) {}
 
   ResultInfo(const ResultInfo&) = delete;
@@ -97,10 +97,10 @@ class ResultInfo {
       return;
     }
 
-    char appid[256] = { 0, };
-    snprintf(appid, sizeof(appid), "%s", pkt->data);
-    SECURE_LOGD("pid: %d, appid: %s", pid_, appid);
-    callback_(AUL_R_OK, pid_, appid, user_data_);
+    char buf[256] = { 0, };
+    snprintf(buf, sizeof(buf), "%s", pkt->data);
+    SECURE_LOGD("pid: %d, id: %s", pid_, buf);
+    callback_(AUL_R_OK, pid_, buf, user_data_);
   }
 
   void ProcessErrorEvent() {
@@ -124,7 +124,7 @@ class ResultInfo {
  private:
   pid_t pid_;
   int fd_;
-  aul_appid_cb callback_;
+  aul_get_id_cb callback_;
   void* user_data_;
   guint source_ = 0;
 };
@@ -754,7 +754,7 @@ extern "C" API int aul_package_pre_event_send(uid_t uid, bundle* b) {
 }
 
 extern "C" API int aul_app_get_appid_bypid_async(pid_t pid,
-    aul_appid_cb callback, void* user_data) {
+    aul_get_id_cb callback, void* user_data) {
   if (pid < 1 || callback == nullptr) {
     _E("Invalid parameter");
     return AUL_R_EINVAL;
@@ -786,3 +786,37 @@ extern "C" API int aul_app_get_appid_bypid_async(pid_t pid,
 
   return AUL_R_OK;
 }
+
+extern "C" API int aul_app_get_pkgid_bypid_async(pid_t pid,
+    aul_get_id_cb callback, void* user_data) {
+  if (pid < 1 || callback == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  if (getpid() == pid || getpgid(getpid()) == pid) {
+    context.Initialize();
+    const auto& preinit_pkgid = context.GetPreInitPkgId();
+    if (!preinit_pkgid.empty()) {
+      callback(AUL_R_OK, pid, preinit_pkgid.c_str(), user_data);
+      return AUL_R_OK;
+    }
+  }
+
+  int fd = AppRequest(APP_GET_PKGID_BYPID, getuid())
+      .SetPid(pid)
+      .SendSimply(AUL_SOCK_ASYNC);
+  if (fd < 0)
+    return AUL_R_ERROR;
+
+  try {
+    auto* result_info = new ResultInfo(pid, fd, callback, user_data);
+    result_info->Watch();
+  } catch (const std::exception& e) {
+    _E("Exception occurs. error: %s", e.what());
+    close(fd);
+    return AUL_R_ERROR;
+  }
+
+  return AUL_R_OK;
+}
diff --git a/src/tool/aul_test/tests/aul_app_get_pkgid_bypid_async_test.cc b/src/tool/aul_test/tests/aul_app_get_pkgid_bypid_async_test.cc
new file mode 100644 (file)
index 0000000..acec7bd
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+
+#include "include/aul.h"
+
+#include "aul_test.hh"
+#include "log_private.hh"
+
+namespace aul_test {
+
+class AulAppGetPkgIdBypidAsyncTest : public AulTest {
+ public:
+  AulAppGetPkgIdBypidAsyncTest()
+      : AulTest("get_pkgid_bypid_async", "aul_app_get_pkgid_bypid_async",
+                "get_pkgid_bypid_async <pid>", false) {}
+
+  virtual ~AulAppGetPkgIdBypidAsyncTest() {}
+
+  void SetUp() override {}
+
+  void TearDown() override {}
+
+  int Test(int argc, char** argv) override {
+    if (argc < 3) {
+      Usage();
+      return -1;
+    }
+
+    _D("[aul_app_get_pkgid_bypid_async test] %s", argv[2]);
+    return aul_app_get_pkgid_bypid_async(atoi(argv[2]), AulPkgIdCb, this);
+  }
+
+ private:
+  static void AulPkgIdCb(int result, pid_t pid, const char* pkgid,
+                         void* user_data) {
+    _D("Result: %d, pkgid: %s", result, pkgid);
+  }
+};
+
+AUL_TEST_REGISTER(AulAppGetPkgIdBypidAsyncTest, get_pkgid_bypid_async_test);
+
+}  // namespace aul_test