Refactor pkginfo APIs 47/278747/10
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 3 Aug 2022 08:27:07 +0000 (17:27 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Wed, 3 Aug 2022 22:55:49 +0000 (22:55 +0000)
The pkginfo APIs are implmented using C++ language.

Change-Id: I7b300dd75ea7c6b4d1e5ad38cb846a9afcc16269
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/pkginfo.c [deleted file]
src/pkginfo.cc [new file with mode: 0644]

diff --git a/src/pkginfo.c b/src/pkginfo.c
deleted file mode 100644 (file)
index a0d60f1..0000000
+++ /dev/null
@@ -1,866 +0,0 @@
-/*
- * Copyright (c) 2000 - 2015 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.
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <bundle_internal.h>
-
-#include "aul.h"
-#include "aul_api.h"
-#include "menu_db_util.h"
-#include "aul_sock.h"
-#include "aul_util.h"
-#include "aul_proc.h"
-#include "aul_error.h"
-
-typedef struct _internal_param_t {
-       aul_app_info_iter_fn iter_fn;
-       void *user_data;
-} internal_param_t;
-
-static const char *__appid;
-static const char *__pkgid;
-static const char *__root_path;
-
-API int aul_app_get_pid(const char *appid)
-{
-       return aul_app_get_pid_for_uid(appid, getuid());
-}
-
-API int aul_app_get_pid_for_uid(const char *appid, uid_t uid)
-{
-       int ret;
-       char buf[MAX_PID_STR_BUFSZ];
-       bundle *b;
-
-       if (appid == NULL)
-               return -1;
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return -1;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add(b, AUL_K_APPID, appid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_PID,
-                       b, AUL_SOCK_NONE);
-       bundle_free(b);
-
-       return ret;
-}
-
-API int aul_app_is_running(const char *appid)
-{
-       return aul_app_is_running_for_uid(appid, getuid());
-}
-
-API int aul_app_is_running_for_uid(const char *appid, uid_t uid)
-{
-       int ret;
-       char buf[MAX_PID_STR_BUFSZ];
-       bundle *b;
-
-       if (appid == NULL)
-               return 0;
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return 0;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add(b, AUL_K_APPID, appid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_IS_RUNNING,
-                       b, AUL_SOCK_NONE);
-       bundle_free(b);
-       if (ret > 0)
-               return true;
-
-       return 0;
-}
-
-static void __running_app_info_cb(app_pkt_t *pkt, void *user_data)
-{
-       internal_param_t *param = (internal_param_t *)user_data;
-       bundle *b = NULL;
-       aul_app_info info;
-       const char *val;
-
-       if (pkt == NULL || param == NULL) {
-               _E("Invalid parameter");
-               return;
-       }
-
-       if (pkt->cmd == APP_GET_INFO_ERROR) {
-               _E("Failed to get app info");
-               return;
-       }
-
-       if (pkt->opt & AUL_SOCK_BUNDLE)
-               b = bundle_decode(pkt->data, pkt->len);
-
-       if (b == NULL)
-               return;
-
-       val = bundle_get_val(b, AUL_K_PID);
-       if (val == NULL) {
-               bundle_free(b);
-               return;
-       }
-       info.pid = atoi(val);
-
-       info.appid = (char *)bundle_get_val(b, AUL_K_APPID);
-       info.app_path = (char *)bundle_get_val(b, AUL_K_EXEC);
-       info.pkgid = (char *)bundle_get_val(b, AUL_K_PKGID);
-       info.instance_id = (char *)bundle_get_val(b, AUL_K_INSTANCE_ID);
-
-       val = bundle_get_val(b, AUL_K_STATUS);
-       if (val == NULL) {
-               bundle_free(b);
-               return;
-       }
-       info.status = atoi(val);
-
-       val = bundle_get_val(b, AUL_K_IS_SUBAPP);
-       if (val == NULL) {
-               bundle_free(b);
-               return;
-       }
-       info.is_sub_app = atoi(val);
-
-       info.pkg_name = info.appid;
-       param->iter_fn(&info, param->user_data);
-       bundle_free(b);
-}
-
-static int __get_running_app_info(int cmd, aul_app_info_iter_fn iter_fn,
-               void *user_data, uid_t uid)
-{
-       int ret;
-       int fd;
-       bundle *b;
-       char buf[MAX_PID_STR_BUFSZ];
-       internal_param_t param = {iter_fn, user_data};
-
-       if (iter_fn == NULL)
-               return AUL_R_EINVAL;
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, cmd, b, AUL_SOCK_ASYNC);
-       bundle_free(b);
-       if (fd < 0)
-               return aul_error_convert(fd);
-
-       ret = aul_sock_recv_pkt_with_cb(fd, __running_app_info_cb, &param);
-       if (ret < 0)
-               return aul_error_convert(ret);
-
-       return AUL_R_OK;
-}
-
-API int aul_app_get_running_app_info(aul_app_info_iter_fn iter_fn,
-               void *user_data)
-{
-       return aul_app_get_running_app_info_for_uid(iter_fn,
-                       user_data, getuid());
-}
-
-API int aul_app_get_running_app_info_for_uid(aul_app_info_iter_fn iter_fn,
-               void *user_data, uid_t uid)
-{
-       return __get_running_app_info(APP_RUNNING_INFO, iter_fn,
-                       user_data, uid);
-}
-
-API int aul_app_get_all_running_app_info(aul_app_info_iter_fn iter_fn,
-               void *user_data)
-{
-       return aul_app_get_all_running_app_info_for_uid(iter_fn,
-                       user_data, getuid());
-}
-
-API int aul_app_get_all_running_app_info_for_uid(aul_app_info_iter_fn iter_fn,
-               void *user_data, uid_t uid)
-{
-       return __get_running_app_info(APP_ALL_RUNNING_INFO, iter_fn,
-                       user_data, uid);
-}
-
-API int aul_app_get_running_app_instance_info(aul_app_info_iter_fn iter_fn,
-               void *user_data)
-{
-       return aul_app_get_running_app_instance_info_for_uid(iter_fn,
-                       user_data, getuid());
-}
-
-API int aul_app_get_running_app_instance_info_for_uid(
-               aul_app_info_iter_fn iter_fn, void *user_data, uid_t uid)
-{
-       return __get_running_app_info(APP_RUNNING_INSTANCE_INFO, iter_fn,
-                       user_data, uid);
-}
-
-API void aul_set_preinit_appid(const char *appid)
-{
-       __appid = appid;
-}
-
-const char *__get_preinit_appid(void)
-{
-       if (!__appid)
-               __appid = getenv("AUL_APPID");
-
-       return __appid;
-}
-
-API void aul_set_preinit_pkgid(const char *pkgid)
-{
-       __pkgid = pkgid;
-}
-
-const char *__get_preinit_pkgid(void)
-{
-       if (!__pkgid)
-               __pkgid = getenv("AUL_PKGID");
-
-       return __pkgid;
-}
-
-API void aul_set_preinit_root_path(const char *root_path)
-{
-       __root_path = root_path;
-}
-
-API const char *aul_get_preinit_root_path(void)
-{
-       if (!__root_path)
-               __root_path = getenv("AUL_ROOT_PATH");
-
-       return __root_path;
-}
-
-API int aul_app_get_pkgname_bypid(int pid, char *pkgname, int len)
-{
-       return aul_app_get_appid_bypid(pid, pkgname, len);
-}
-
-API int aul_app_get_appid_bypid_for_uid(int pid, char *appid, int len,
-               uid_t uid)
-{
-       app_pkt_t *pkt = NULL;
-       int fd;
-       int ret;
-       const char *preinit_appid;
-       bundle *b;
-       char buf[MAX_PID_STR_BUFSZ];
-
-       if (pid <= 0 || appid == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       if (getpid() == pid) {
-               preinit_appid = __get_preinit_appid();
-               if (preinit_appid) {
-                       snprintf(appid, len, "%s", preinit_appid);
-                       return AUL_R_OK;
-               }
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", pid);
-       bundle_add(b, AUL_K_PID, buf);
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_APPID_BYPID,
-                       b, AUL_SOCK_ASYNC);
-       bundle_free(b);
-       if (fd <= 0)
-               return AUL_R_ERROR;
-
-       ret = aul_sock_recv_reply_pkt(fd, &pkt);
-       if (ret < 0 || pkt == NULL)
-               return AUL_R_ERROR;
-
-       if (pkt->cmd == APP_GET_INFO_OK) {
-               snprintf(appid, len, "%s", pkt->data);
-               free(pkt);
-               return AUL_R_OK;
-       }
-       free(pkt);
-
-       return AUL_R_ERROR;
-}
-
-API int aul_app_get_appid_bypid(int pid, char *appid, int len)
-{
-       return aul_app_get_appid_bypid_for_uid(pid, appid, len, getuid());
-}
-
-static int __get_pkginfo(int pid, char *buf, int len, uid_t uid)
-{
-       const char *appid;
-       app_info_from_db *menu_info;
-
-       appid = __get_preinit_appid();
-       if (appid == NULL) {
-               _E("Failed to get preinit appid - %d", pid);
-               return -1;
-       }
-
-       menu_info = _get_app_info_from_db_by_appid_user(appid, uid);
-       if (menu_info == NULL) {
-               _E("Failed to get app info - %s", appid);
-               return -1;
-       }
-
-       snprintf(buf, len, "%s", _get_pkgid(menu_info));
-       _free_app_info_from_db(menu_info);
-
-       return 0;
-}
-
-API int aul_app_get_pkgid_bypid_for_uid(int pid, char *pkgid, int len,
-               uid_t uid)
-{
-       app_pkt_t *pkt = NULL;
-       int fd;
-       int ret;
-       const char *preinit_pkgid;
-       bundle *b;
-       char buf[MAX_PID_STR_BUFSZ];
-
-       if (pid <= 0 || pkgid == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       if (getpid() == pid) {
-               preinit_pkgid = __get_preinit_pkgid();
-               if (preinit_pkgid) {
-                       snprintf(pkgid, len, "%s", preinit_pkgid);
-                       return AUL_R_OK;
-               } else {
-                       /* fallback (for debugging) */
-                       ret = __get_pkginfo(pid, pkgid, len, uid);
-                       if (ret == 0)
-                               return AUL_R_OK;
-               }
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", pid);
-       bundle_add(b, AUL_K_PID, buf);
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_PKGID_BYPID,
-                       b, AUL_SOCK_ASYNC);
-       bundle_free(b);
-       if (fd <= 0)
-               return AUL_R_ERROR;
-
-       ret = aul_sock_recv_reply_pkt(fd, &pkt);
-       if (ret < 0 || pkt == NULL)
-               return AUL_R_ERROR;
-
-       if (pkt->cmd == APP_GET_INFO_OK) {
-               snprintf(pkgid, len, "%s", pkt->data);
-               free(pkt);
-               return AUL_R_OK;
-       }
-       free(pkt);
-
-       return AUL_R_ERROR;
-}
-
-API int aul_app_get_pkgid_bypid(int pid, char *pkgid, int len)
-{
-       return aul_app_get_pkgid_bypid_for_uid(pid, pkgid, len, getuid());
-}
-
-API int aul_update_rua_stat_for_uid(bundle *b, uid_t uid)
-{
-       int ret;
-       char buf[MAX_PID_STR_BUFSZ];
-
-       if (b == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_del(b, AUL_K_TARGET_UID);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, uid,
-                       APP_UPDATE_RUA_STAT, b, AUL_SOCK_NONE);
-       return ret;
-}
-
-API int aul_add_rua_history_for_uid(bundle *b, uid_t uid)
-{
-       int ret;
-       char buf[MAX_PID_STR_BUFSZ];
-
-       if (b == NULL) {
-               SECURE_LOGE("invalid param");
-               return AUL_R_EINVAL;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_del(b, AUL_K_TARGET_UID);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, uid,
-                       APP_ADD_HISTORY, b, AUL_SOCK_NONE);
-       return ret;
-}
-
-API int aul_delete_rua_history_for_uid(bundle *b, uid_t uid)
-{
-       int ret;
-       char buf[MAX_PID_STR_BUFSZ];
-       bundle *kb = NULL;
-
-       if (b == NULL) {
-               kb = bundle_create();
-               if (kb == NULL) {
-                       _E("out of memory");
-                       return AUL_R_ERROR;
-               }
-
-               b = kb;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_del(b, AUL_K_TARGET_UID);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_REMOVE_HISTORY,
-                       b, AUL_SOCK_NONE);
-       if (kb)
-               bundle_free(kb);
-
-       return ret;
-}
-
-API int aul_set_default_app_by_operation(bundle *b)
-{
-       int ret;
-
-       if (b == NULL)
-               return AUL_R_EINVAL;
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       APP_SET_APP_CONTROL_DEFAULT_APP, b, AUL_SOCK_NONE);
-       if (ret != 0) {
-               if (ret == -EILLEGALACCESS)
-                       return AUL_R_EILLACC;
-               else
-                       return AUL_R_ERROR;
-       }
-
-       return AUL_R_OK;
-}
-
-API int aul_unset_default_app_by_operation(const char *app_id)
-{
-       int ret;
-
-       if (app_id == NULL)
-               return AUL_R_EINVAL;
-
-       ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(), APP_UNSET_APP_CONTROL_DEFAULT_APP,
-                       (unsigned char *)app_id, strlen(app_id), AUL_SOCK_NONE);
-       if (ret != 0) {
-               if (ret == -EILLEGALACCESS)
-                       return AUL_R_EILLACC;
-               else
-                       return AUL_R_ERROR;
-       }
-
-       return AUL_R_OK;
-}
-
-API int aul_app_get_last_caller_pid(int pid)
-{
-       return aul_app_get_last_caller_pid_for_uid(pid, getuid());
-}
-
-API int aul_app_get_last_caller_pid_for_uid(int pid, uid_t uid)
-{
-       int ret;
-       char buf[MAX_PID_STR_BUFSZ];
-       bundle *b;
-
-       if (pid < 0) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("Failed to create bundle");
-               return AUL_R_ERROR;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", pid);
-       bundle_add(b, AUL_K_PID, buf);
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, uid, APP_GET_LAST_CALLER_PID,
-                       b, AUL_SOCK_NONE);
-       bundle_free(b);
-       if (ret < 0)
-               return aul_error_convert(ret);
-
-       return ret;
-}
-
-API int aul_set_alias_appid(const char *alias_appid, const char *appid)
-{
-       int ret;
-       bundle *b;
-
-       if (alias_appid == NULL || appid == NULL) {
-               _E("Invalid parameters");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-       bundle_add(b, AUL_K_ALIAS_APPID, alias_appid);
-       bundle_add(b, AUL_K_APPID, appid);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       APP_SET_ALIAS_APPID, b, AUL_SOCK_NONE);
-       bundle_free(b);
-       if (ret != AUL_R_OK)
-               return aul_error_convert(ret);
-
-       return AUL_R_OK;
-}
-
-API int aul_unset_alias_appid(const char *alias_appid)
-{
-       int ret;
-       bundle *b;
-
-       if (alias_appid == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-       bundle_add(b, AUL_K_ALIAS_APPID, alias_appid);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       APP_UNSET_ALIAS_APPID, b, AUL_SOCK_NONE);
-       bundle_free(b);
-       if (ret != AUL_R_OK)
-               return aul_error_convert(ret);
-
-       return AUL_R_OK;
-}
-
-API int aul_set_auto_restart(bundle *b)
-{
-       int ret;
-       bundle *kb;
-       bundle_raw *raw;
-       int len;
-
-       kb = bundle_create();
-       if (!kb) {
-               _E("Out of memory");
-               return AUL_R_ENOMEM;
-       }
-
-       bundle_add(kb, AUL_K_AUTO_RESTART, "true");
-       if (b) {
-               ret = bundle_encode(b, &raw, &len);
-               if (ret != BUNDLE_ERROR_NONE) {
-                       bundle_free(kb);
-                       return AUL_R_EINVAL;
-               }
-
-               bundle_add(kb, AUL_K_RESTART_EXTRA, (const char *)raw);
-               bundle_free_encoded_rawdata(&raw);
-       }
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       APP_SET_AUTO_RESTART, kb, AUL_SOCK_NONE);
-       bundle_free(kb);
-       if (ret != AUL_R_OK)
-               return aul_error_convert(ret);
-
-       return AUL_R_OK;
-}
-
-API int aul_unset_auto_restart()
-{
-       int ret;
-       bundle *kb;
-
-       kb = bundle_create();
-       if (kb == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-
-       bundle_add(kb, AUL_K_AUTO_RESTART, "false");
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                                  APP_SET_AUTO_RESTART, kb, AUL_SOCK_NONE);
-       bundle_free(kb);
-       if (ret != AUL_R_OK)
-               return aul_error_convert(ret);
-
-       return AUL_R_OK;
-}
-
-API int aul_enable_alias_info(const char *appid)
-{
-       int ret;
-       bundle *b;
-
-       if (appid == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-       bundle_add(b, AUL_K_APPID, appid);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       APP_ENABLE_ALIAS_INFO, b, AUL_SOCK_NONE);
-       bundle_free(b);
-       if (ret != AUL_R_OK)
-               return aul_error_convert(ret);
-
-       return AUL_R_OK;
-}
-
-API int aul_disable_alias_info(const char *appid)
-{
-       int ret;
-       bundle *b;
-
-       if (appid == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-       bundle_add(b, AUL_K_APPID, appid);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       APP_DISABLE_ALIAS_INFO, b, AUL_SOCK_NONE);
-       bundle_free(b);
-       if (ret != AUL_R_OK)
-               return aul_error_convert(ret);
-
-       return AUL_R_OK;
-}
-
-API int aul_app_get_instance_id_bypid_for_uid(int pid, char *instance_id,
-               int len, uid_t uid)
-{
-       app_pkt_t *pkt = NULL;
-       bundle *b;
-       int ret;
-       int fd;
-       char buf[MAX_PID_STR_BUFSZ];
-
-       if (pid <= 0 || instance_id == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("Out of memory");
-               return AUL_R_ERROR;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", pid);
-       bundle_add(b, AUL_K_PID, buf);
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       fd = aul_sock_send_bundle(AUL_UTIL_PID, uid,
-                       APP_GET_INSTANCE_ID_BYPID, b,
-                       AUL_SOCK_ASYNC);
-       bundle_free(b);
-       if (fd <= 0)
-               return AUL_R_ERROR;
-
-       ret = aul_sock_recv_reply_pkt(fd, &pkt);
-       if (ret < 0 || pkt == NULL)
-               return  AUL_R_ERROR;
-
-       if (pkt->cmd == APP_GET_INFO_OK) {
-               snprintf(instance_id, len, "%s", pkt->data);
-               free(pkt);
-               return AUL_R_OK;
-       }
-
-       free(pkt);
-
-       return AUL_R_ERROR;
-}
-
-API int aul_app_get_instance_id_bypid(int pid, char *instance_id, int len)
-{
-       return aul_app_get_instance_id_bypid_for_uid(pid,
-                       instance_id, len, getuid());
-}
-
-API int aul_app_is_running_with_instance_id(const char *appid,
-               const char *instance_id, bool *running)
-{
-       bundle *b;
-       int ret;
-
-       if (!appid || !instance_id || !running) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (!b) {
-               _E("out of memory");
-               return AUL_R_ENOMEM;
-       }
-
-       bundle_add(b, AUL_K_APPID, appid);
-       bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
-
-       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), APP_IS_RUNNING,
-                       b, AUL_SOCK_NONE);
-       bundle_free(b);
-       if (ret < 0)
-               return aul_error_convert(ret);
-
-       *running = (bool)ret;
-
-       return AUL_R_OK;
-}
-
-API int aul_get_default_app(bundle *b, char **appid)
-{
-       app_pkt_t *pkt = NULL;
-       const char* val;
-       bundle *res_b;
-       int ret;
-       int fd;
-
-       if (!b || !appid) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       fd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       APP_GET_APP_CONTROL_DEFAULT_APP, b, AUL_SOCK_ASYNC);
-       if (fd < 0)
-               return aul_error_convert(fd);
-
-       ret = aul_sock_recv_reply_pkt(fd, &pkt);
-       if (ret < 0 || pkt == NULL) {
-               _E("Failed to get reply. error(%d)", ret);
-               return aul_error_convert(ret);
-       }
-
-       if (pkt->cmd != APP_GET_INFO_OK) {
-               _E("Failed to get default app. error(%d)", pkt->cmd);
-               ret = aul_error_convert(pkt->cmd);
-               free(pkt);
-               return ret;
-       }
-
-       if (!(pkt->opt & AUL_SOCK_BUNDLE)) {
-               _E("Invalid protocol");
-               free(pkt);
-               return AUL_R_ERROR;
-       }
-
-       res_b = bundle_decode(pkt->data, pkt->len);
-       free(pkt);
-       if (!res_b) {
-               _E("Failed to decode bundle data");
-               return AUL_R_ERROR;
-       }
-
-       val = bundle_get_val(res_b, AUL_K_APPID);
-       if (!val) {
-               _E("Failed to get appid");
-               bundle_free(res_b);
-               return AUL_R_ERROR;
-       }
-
-       *appid = strdup(val);
-       bundle_free(res_b);
-       if (*appid == NULL) {
-               _E("Out of memory");
-               return AUL_R_ENOMEM;
-       }
-
-       return AUL_R_OK;
-}
diff --git a/src/pkginfo.cc b/src/pkginfo.cc
new file mode 100644 (file)
index 0000000..c3ac0dd
--- /dev/null
@@ -0,0 +1,606 @@
+/*
+ * Copyright (c) 2012 - 2022 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 <sys/types.h>
+#include <unistd.h>
+
+#include <bundle_cpp.h>
+#include <bundle_internal.h>
+
+#include <string>
+#include <memory>
+
+#include "app_request.h"
+#include "aul_api.h"
+#include "aul_error.h"
+#include "aul_util.h"
+#include "menu_db_util.h"
+#include "include/aul.h"
+#include "include/aul_error.h"
+#include "include/aul_sock.h"
+
+using namespace aul::internal;
+
+namespace {
+
+constexpr const char* kAppInfoKeys[] = {
+  AUL_K_PID,
+  AUL_K_APPID,
+  AUL_K_EXEC,
+  AUL_K_PKGID,
+  AUL_K_STATUS,
+  AUL_K_IS_SUBAPP,
+};
+
+class Context {
+ public:
+  Context() = default;
+
+  void Initialize() {
+    if (initialized_)
+      return;
+
+    auto* appid = getenv("AUL_APPID");
+    if (appid != nullptr)
+      appid_ = std::string(appid);
+
+    auto* pkgid = getenv("AUL_PKGID");
+    if (pkgid != nullptr)
+      pkgid_ = std::string(pkgid);
+
+    auto* root_path = getenv("AUL_ROOT_PATH");
+    if (root_path != nullptr)
+      root_path_ = std::string(root_path);
+
+    initialized_ = true;
+  }
+
+  const std::string& GetPreInitAppId() const {
+    return appid_;
+  }
+
+  const std::string& GetPreInitPkgId() const {
+    return pkgid_;
+  }
+
+  const std::string& GetPreInitRootPath() const {
+    return root_path_;
+  }
+
+  void SetPreInitAppId(std::string appid) {
+    appid_ = std::move(appid);
+  }
+
+  void SetPreInitPkgId(std::string pkgid) {
+    pkgid_ = std::move(pkgid);
+  }
+
+  void SetPreInitRootPath(std::string root_path) {
+    root_path_ = std::move(root_path);
+  }
+
+ private:
+  bool initialized_ = false;
+  std::string appid_;
+  std::string pkgid_;
+  std::string root_path_;
+};
+
+int SendAndReceive(int cmd, uid_t uid,
+    std::vector<tizen_base::Bundle>* results) {
+  int fd = AppRequest(cmd, uid).SendSimply(AUL_SOCK_ASYNC);
+  if (fd < 0)
+    return fd;
+
+  int ret = aul_sock_recv_pkt_with_cb(fd,
+      [](app_pkt_t* pkt, void* user_data) {
+        if (pkt == nullptr) {
+          _E("Invalid parameter");
+          return;
+        }
+
+        bundle* b = nullptr;
+        if (pkt->opt & AUL_SOCK_BUNDLE)
+          b = bundle_decode(pkt->data, pkt->len);
+
+        if (b == nullptr)
+          return;
+
+        auto* res = static_cast<std::vector<tizen_base::Bundle>*>(user_data);
+        res->push_back(tizen_base::Bundle(b, false, true));
+      }, results);
+  if (ret < 0)
+    return aul_error_convert(ret);
+
+  return AUL_R_OK;
+}
+
+int SetAppInfo(aul_app_info* info, const tizen_base::Bundle& b) {
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+  for (unsigned int i = 0; i < ARRAY_SIZE(kAppInfoKeys); ++i) {
+    if (b.GetType(kAppInfoKeys[i]) == BUNDLE_TYPE_NONE)
+      return -1;
+  }
+
+  info->pid = std::stoi(b.GetString(AUL_K_PID));
+  info->appid = const_cast<char*>(bundle_get_val(b.GetHandle(), AUL_K_APPID));
+  info->app_path = const_cast<char*>(bundle_get_val(b.GetHandle(), AUL_K_EXEC));
+  info->pkgid = const_cast<char*>(bundle_get_val(b.GetHandle(), AUL_K_PKGID));
+  info->instance_id = const_cast<char*>(
+      bundle_get_val(b.GetHandle(), AUL_K_INSTANCE_ID));
+  info->status = std::stoi(b.GetString(AUL_K_STATUS));
+  info->is_sub_app = std::stoi(b.GetString(AUL_K_IS_SUBAPP));
+  info->pkg_name = info->appid;
+  return 0;
+}
+
+int GetRunningAppInfoWithCb(int cmd, uid_t uid, aul_app_info_iter_fn cb,
+    void* user_data) {
+  if (cb == nullptr)
+    return AUL_R_EINVAL;
+
+  std::vector<tizen_base::Bundle> results;
+  int ret = SendAndReceive(cmd, uid, &results);
+  if (ret != AUL_R_OK)
+    return ret;
+
+  aul_app_info info;
+  for (auto const& b : results) {
+    if (SetAppInfo(&info, b) != 0)
+      break;
+
+    cb(&info, user_data);
+  }
+
+  return AUL_R_OK;
+}
+
+Context context;
+
+int GetPkgIdFromDB(int pid, char* buf, int len, uid_t uid) {
+  auto appid = context.GetPreInitAppId();
+  if (appid.empty()) {
+    _E("Failed to get pre-initialized appid");
+    return -1;
+  }
+
+  auto* menu_info = _get_app_info_from_db_by_appid_user(appid.c_str(), uid);
+  if (menu_info == nullptr) {
+    _E("Failed to get app info. appid(%s)", appid.c_str());
+    return -1;
+  }
+
+  snprintf(buf, len, "%s", _get_pkgid(menu_info));
+  _free_app_info_from_db(menu_info);
+  return 0;
+}
+
+}  // namespace
+
+extern "C" API int aul_app_get_pid(const char* appid) {
+  return aul_app_get_pid_for_uid(appid, getuid());
+}
+
+extern "C" API int aul_app_get_pid_for_uid(const char* appid, uid_t uid) {
+  if (appid == nullptr)
+    return -1;
+
+  return AppRequest(APP_GET_PID, uid)
+      .SetAppId(appid)
+      .SendSimply();
+}
+
+extern "C" API int aul_app_is_running(const char* appid) {
+  return aul_app_is_running_for_uid(appid, getuid());
+}
+
+extern "C" API int aul_app_is_running_for_uid(const char* appid, uid_t uid) {
+  if (appid == nullptr)
+    return 0;
+
+  int ret = AppRequest(APP_IS_RUNNING, uid)
+      .SetAppId(appid)
+      .SendSimply();
+  return ret > 0;
+}
+
+extern "C" API int aul_app_get_running_app_info(aul_app_info_iter_fn iter_fn,
+    void* user_data) {
+  return aul_app_get_running_app_info_for_uid(iter_fn, user_data, getuid());
+}
+
+extern "C" API int aul_app_get_running_app_info_for_uid(
+    aul_app_info_iter_fn iter_fn, void* user_data, uid_t uid) {
+  return GetRunningAppInfoWithCb(APP_RUNNING_INFO, uid, iter_fn, user_data);
+}
+
+extern "C" API int aul_app_get_all_running_app_info(
+    aul_app_info_iter_fn iter_fn, void* user_data) {
+  return aul_app_get_all_running_app_info_for_uid(iter_fn, user_data, getuid());
+}
+
+extern "C" API int aul_app_get_all_running_app_info_for_uid(
+    aul_app_info_iter_fn iter_fn, void* user_data, uid_t uid) {
+  return GetRunningAppInfoWithCb(APP_ALL_RUNNING_INFO, uid, iter_fn, user_data);
+}
+
+extern "C" API int aul_app_get_running_app_instance_info(
+    aul_app_info_iter_fn iter_fn, void* user_data) {
+  return aul_app_get_running_app_instance_info_for_uid(iter_fn, user_data,
+      getuid());
+}
+
+extern "C" API int aul_app_get_running_app_instance_info_for_uid(
+    aul_app_info_iter_fn iter_fn, void *user_data, uid_t uid) {
+  return GetRunningAppInfoWithCb(APP_RUNNING_INSTANCE_INFO, uid, iter_fn,
+      user_data);
+}
+
+extern "C" API void aul_set_preinit_appid(const char* appid) {
+  context.Initialize();
+  context.SetPreInitAppId(appid ? appid : "");
+}
+
+extern "C" API void aul_set_preinit_pkgid(const char* pkgid) {
+  context.Initialize();
+  context.SetPreInitPkgId(pkgid ? pkgid : "");
+}
+
+extern "C" API void aul_set_preinit_root_path(const char* root_path) {
+  context.Initialize();
+  context.SetPreInitRootPath(root_path ? root_path : "");
+}
+
+extern "C" API const char* aul_get_preinit_root_path(void) {
+  context.Initialize();
+  return context.GetPreInitRootPath().c_str();
+}
+
+extern "C" API int aul_app_get_pkgname_bypid(int pid, char* pkgname, int len) {
+  return aul_app_get_appid_bypid(pid, pkgname, len);
+}
+
+extern "C" API int aul_app_get_appid_bypid(int pid, char* appid, int len) {
+  return aul_app_get_appid_bypid_for_uid(pid, appid, len, getuid());
+}
+
+extern "C" API int aul_app_get_appid_bypid_for_uid(int pid, char* appid,
+    int len, uid_t uid) {
+  if (pid < 1 || appid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  if (getpid() == pid) {
+    context.Initialize();
+    auto& preinit_appid = context.GetPreInitAppId();
+    if (!preinit_appid.empty()) {
+      snprintf(appid, len, "%s", preinit_appid.c_str());
+      return AUL_R_OK;
+    }
+  }
+
+  int fd = AppRequest(APP_GET_APPID_BYPID, uid)
+      .SetPid(pid)
+      .SendSimply(AUL_SOCK_ASYNC);
+  if (fd < 0)
+    return AUL_R_ERROR;
+
+  app_pkt_t* pkt = nullptr;
+  int ret = aul_sock_recv_reply_pkt(fd, &pkt);
+  if (ret < 0 || pkt == nullptr)
+    return AUL_R_ERROR;
+
+  auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
+      pkt, std::free);
+  if (pkt->cmd != APP_GET_INFO_OK)
+    return AUL_R_ERROR;
+
+  snprintf(appid, len, "%s", pkt->data);
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_get_pkgid_bypid(int pid, char* pkgid, int len) {
+  return aul_app_get_pkgid_bypid_for_uid(pid, pkgid, len, getuid());
+}
+
+extern "C" API int aul_app_get_pkgid_bypid_for_uid(int pid, char* pkgid,
+    int len, uid_t uid) {
+  if (pid < 1 || pkgid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  if (getpid() == pid) {
+    context.Initialize();
+    auto& preinit_pkgid = context.GetPreInitPkgId();
+    if (!preinit_pkgid.empty()) {
+      snprintf(pkgid, len, "%s", preinit_pkgid.c_str());
+      return AUL_R_OK;
+    }
+
+    if (GetPkgIdFromDB(pid, pkgid, len, uid) == 0)
+      return AUL_R_OK;
+  }
+
+  int fd = AppRequest(APP_GET_PKGID_BYPID, uid)
+      .SetPid(pid)
+      .SendSimply(AUL_SOCK_ASYNC);
+  if (fd < 0)
+    return AUL_R_ERROR;
+
+  app_pkt_t* pkt = nullptr;
+  int ret = aul_sock_recv_reply_pkt(fd, &pkt);
+  if (ret < 0 || pkt == nullptr)
+    return AUL_R_ERROR;
+
+  auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
+      pkt, std::free);
+  if (pkt->cmd != APP_GET_INFO_OK)
+    return AUL_R_ERROR;
+
+  snprintf(pkgid, len, "%s", pkt->data);
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_update_rua_stat_for_uid(bundle* b, uid_t uid) {
+  if (b == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_UPDATE_RUA_STAT, uid)
+      .With(b)
+      .Send();
+}
+
+extern "C" API int aul_add_rua_history_for_uid(bundle* b, uid_t uid) {
+  if (b == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_ADD_HISTORY, uid)
+      .With(b)
+      .Send();
+}
+
+extern "C" API int aul_delete_rua_history_for_uid(bundle* b, uid_t uid) {
+  tizen_base::Bundle kb;
+  if (b != nullptr)
+    kb = std::move(tizen_base::Bundle(b, false, false));
+
+  return AppRequest(APP_REMOVE_HISTORY, uid)
+      .With(std::move(kb))
+      .Send();
+}
+
+extern "C" API int aul_set_default_app_by_operation(bundle* b) {
+  if (b == nullptr)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_SET_APP_CONTROL_DEFAULT_APP, getuid())
+      .With(b)
+      .Send();
+}
+
+extern "C" API int aul_unset_default_app_by_operation(const char* appid) {
+  if (appid == nullptr)
+    return AUL_R_EINVAL;
+
+  int ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
+      APP_UNSET_APP_CONTROL_DEFAULT_APP,
+      reinterpret_cast<unsigned char*>(const_cast<char*>(appid)),
+      strlen(appid), AUL_SOCK_NONE);
+  if (ret != 0)
+    return aul_error_convert(ret);
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_get_last_caller_pid(int pid) {
+  return aul_app_get_last_caller_pid_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_app_get_last_caller_pid_for_uid(int pid, uid_t uid) {
+  if (pid < 1) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_GET_LAST_CALLER_PID, uid)
+      .SetPid(pid)
+      .Send();
+}
+
+extern "C" API int aul_set_alias_appid(const char* alias_appid,
+    const char* appid) {
+  if (alias_appid == nullptr || appid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  tizen_base::Bundle b {
+    { AUL_K_ALIAS_APPID, alias_appid },
+    { AUL_K_APPID, appid }
+  };
+
+  return AppRequest(APP_SET_ALIAS_APPID, getuid())
+      .With(std::move(b))
+      .Send();
+}
+
+extern "C" API int aul_unset_alias_appid(const char* alias_appid) {
+  if (alias_appid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  tizen_base::Bundle b { { AUL_K_ALIAS_APPID, alias_appid } };
+  return AppRequest(APP_UNSET_ALIAS_APPID, getuid())
+      .With(std::move(b))
+      .Send();
+}
+
+extern "C" API int aul_enable_alias_info(const char* appid) {
+  if (appid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_ENABLE_ALIAS_INFO, getuid())
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" API int aul_disable_alias_info(const char* appid) {
+  if (appid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_DISABLE_ALIAS_INFO, getuid())
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" API int aul_set_auto_restart(bundle* b) {
+  tizen_base::Bundle kb { { AUL_K_AUTO_RESTART, "true" } };
+  if (b != nullptr) {
+    bundle_raw* raw;
+    int len;
+    if (bundle_encode(b, &raw, &len) != BUNDLE_ERROR_NONE)
+      return AUL_R_EINVAL;
+
+    kb.Add(AUL_K_RESTART_EXTRA, reinterpret_cast<char*>(raw));
+    bundle_free_encoded_rawdata(&raw);
+  }
+
+  return AppRequest(APP_SET_AUTO_RESTART, getuid())
+      .With(std::move(kb))
+      .Send();
+}
+
+extern "C" API int aul_unset_auto_restart(void) {
+  tizen_base::Bundle kb { { AUL_K_AUTO_RESTART, "false" } };
+  return AppRequest(APP_SET_AUTO_RESTART, getuid())
+      .With(std::move(kb))
+      .Send();
+}
+
+extern "C" API int aul_app_get_instance_id_bypid(int pid, char* instance_id,
+    int len) {
+  return aul_app_get_instance_id_bypid_for_uid(pid, instance_id, len, getuid());
+}
+
+extern "C" API int aul_app_get_instance_id_bypid_for_uid(int pid,
+    char* instance_id, int len, uid_t uid) {
+  if (pid < 1 || instance_id == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  int fd = AppRequest(APP_GET_INSTANCE_ID_BYPID, uid)
+      .SetPid(pid)
+      .SendSimply(AUL_SOCK_ASYNC);
+  if (fd < 0)
+    return AUL_R_ERROR;
+
+  app_pkt_t* pkt = nullptr;
+  int ret = aul_sock_recv_reply_pkt(fd, &pkt);
+  if (ret < 0 || pkt == nullptr)
+    return AUL_R_ERROR;
+
+  auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
+      pkt, std::free);
+  if (pkt->cmd != APP_GET_INFO_OK)
+    return AUL_R_ERROR;
+
+  snprintf(instance_id, len, "%s", pkt->data);
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_is_running_with_instance_id(const char* appid,
+    const char* instance_id, bool* running) {
+  if (appid == nullptr || instance_id == nullptr || running == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  int ret = AppRequest(APP_IS_RUNNING, getuid())
+      .SetAppId(appid)
+      .SetInstId(instance_id)
+      .Send();
+  if (ret < 0)
+    return ret;
+
+  *running = ret ? true : false;
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_get_default_app(bundle* b, char** appid) {
+  if (b == nullptr || appid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  int fd = AppRequest(APP_GET_APP_CONTROL_DEFAULT_APP, getuid())
+      .With(b)
+      .SendSimply(AUL_SOCK_ASYNC);
+  if (fd < 0)
+    return fd;
+
+  app_pkt_t* pkt = nullptr;
+  int ret = aul_sock_recv_reply_pkt(fd, &pkt);
+  if (ret < 0 || pkt == nullptr) {
+    _E("aul_sock_recv_reply_pkt() is failed. error(%d)", ret);
+    return aul_error_convert(ret);
+  }
+
+  auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
+      pkt, std::free);
+  if (pkt->cmd != APP_GET_INFO_OK) {
+    _E("Failed to get default app. error(%d)", pkt->cmd);
+    return aul_error_convert(pkt->cmd);
+  }
+
+  if (!(pkt->opt & AUL_SOCK_BUNDLE)) {
+    _E("Invalid protocol");
+    return AUL_R_ERROR;
+  }
+
+  bundle* res_b = bundle_decode(pkt->data, pkt->len);
+  if (res_b == nullptr) {
+    _E("bundle_decode() is failed");
+    return AUL_R_ERROR;
+  }
+
+  tizen_base::Bundle kb(res_b, false, true);
+  auto value = kb.GetString(AUL_K_APPID);
+  if (value.empty()) {
+    _E("Failed to get appid");
+    return AUL_R_ERROR;
+  }
+
+  *appid = strdup(value.c_str());
+  if (*appid == nullptr) {
+    _E("strdup() is failed. appid(%s)", value.c_str());
+    return AUL_R_ENOMEM;
+  }
+
+  return AUL_R_OK;
+}