Refactor launch.c 04/258604/22
authorjh9216.park <jh9216.park@samsung.com>
Fri, 21 May 2021 04:42:23 +0000 (00:42 -0400)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 25 May 2021 00:40:19 +0000 (09:40 +0900)
- Require : https://review.tizen.org/gerrit/#/c/platform/core/base/bundle/+/258595/
- Use c++ syntax
- Add 'AppRequest' class using builder pattern

Change-Id: I511368a0eb25dd61dd76a3fcdc9dac23dd0af137
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
src/launch.c [deleted file]
src/launch.cc [new file with mode: 0644]

diff --git a/src/launch.c b/src/launch.c
deleted file mode 100644 (file)
index 0d0cf66..0000000
+++ /dev/null
@@ -1,1083 +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 <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <dirent.h>
-#include <ctype.h>
-#include <glib.h>
-#include <gio/gio.h>
-#include <ttrace.h>
-
-#include <bundle_internal.h>
-
-#include "app_signal.h"
-#include "aul.h"
-#include "aul_api.h"
-#include "aul_sock.h"
-#include "aul_util.h"
-#include "launch.h"
-#include "key.h"
-#include "aul_app_com.h"
-#include "aul_error.h"
-
-#define TEP_ISMOUNT_MAX_RETRY_CNT 20
-
-static int aul_initialized = 0;
-static int aul_fd;
-static void *__window_object = NULL;
-static void *__bg_object = NULL;
-static void *__conformant_object = NULL;
-
-static void __clear_internal_key(bundle *kb);
-static inline void __set_stime(bundle *kb);
-
-int aul_is_initialized(void)
-{
-       return aul_initialized;
-}
-
-static int __send_cmd_for_uid_opt(int pid, uid_t uid, int cmd, bundle *kb, int opt)
-{
-       int res;
-
-       res = aul_sock_send_bundle(pid, uid, cmd, kb, opt);
-       if (res < 0)
-               res = aul_error_convert(res);
-
-       return res;
-}
-
-static int __send_cmd_noreply_for_uid_opt(int pid, uid_t uid,
-               int cmd, bundle *kb, int opt)
-{
-       int res;
-
-       res = aul_sock_send_bundle(pid, uid, cmd, kb, opt | AUL_SOCK_NOREPLY);
-       if (res < 0)
-               res = aul_error_convert(res);
-
-       return res;
-}
-
-static int __send_cmd_async_for_uid_opt(int pid, uid_t uid,
-               int cmd, bundle *kb, int opt)
-{
-       int res;
-
-       res = aul_sock_send_bundle(pid, uid, cmd, kb, opt | AUL_SOCK_ASYNC);
-       if (res < 0)
-               res = aul_error_convert(res);
-
-       return res;
-}
-
-/**
- * @brief      encode kb and send it to 'pid'
- * @param[in]  pid             receiver's pid
- * @param[in]  cmd             message's status (APP_START | APP_RESULT)
- * @param[in]  kb              data
- */
-API int app_send_cmd(int pid, int cmd, bundle *kb)
-{
-       return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NONE);
-}
-
-API int app_send_cmd_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
-{
-       return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_NONE);
-}
-
-API int app_send_cmd_with_queue_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
-{
-       return __send_cmd_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
-}
-
-API int app_send_cmd_with_queue_noreply_for_uid(int pid, uid_t uid,
-                                       int cmd, bundle *kb)
-{
-       return __send_cmd_noreply_for_uid_opt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
-}
-
-API int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
-{
-       return __send_cmd_for_uid_opt(pid, getuid(), cmd, kb, AUL_SOCK_NOREPLY);
-}
-
-API int app_send_cmd_to_launchpad(const char *pad_type, uid_t uid, int cmd, bundle *kb)
-{
-       int fd;
-       int len;
-       int res;
-       char buf[1024];
-
-       fd = aul_sock_create_launchpad_client(pad_type, uid);
-       if (fd < 0)
-               return -1;
-
-       res = aul_sock_send_bundle_with_fd(fd, cmd,
-                       kb, AUL_SOCK_ASYNC);
-       if (res < 0) {
-               close(fd);
-               return res;
-       }
-
-retry_recv:
-       len = recv(fd, &res, sizeof(int), 0);
-       if (len == -1) {
-               if (errno == EAGAIN) {
-                       _E("recv timeout: %d(%s)",
-                                       errno,
-                                       strerror_r(errno, buf, sizeof(buf)));
-                       res = -EAGAIN;
-               } else if (errno == EINTR) {
-                       _D("recv: %d(%s)",
-                                       errno,
-                                       strerror_r(errno, buf, sizeof(buf)));
-                       goto retry_recv;
-               } else {
-                       _E("recv error: %d(%s)",
-                                       errno,
-                                       strerror_r(errno, buf, sizeof(buf)));
-                       res = -ECOMM;
-               }
-       }
-
-       close(fd);
-
-       return res;
-}
-
-static void __clear_internal_key(bundle *kb)
-{
-       bundle_del(kb, AUL_K_CALLER_PID);
-       bundle_del(kb, AUL_K_APPID);
-       bundle_del(kb, AUL_K_WAIT_RESULT);
-       bundle_del(kb, AUL_K_SEND_RESULT);
-       bundle_del(kb, AUL_K_ARGV0);
-}
-
-static inline void __set_stime(bundle *kb)
-{
-       struct timespec start;
-       char tmp[MAX_LOCAL_BUFSZ];
-
-       clock_gettime(CLOCK_MONOTONIC, &start);
-       snprintf(tmp, sizeof(tmp), "%ld/%ld", start.tv_sec, start.tv_nsec);
-       bundle_del(kb, AUL_K_STARTTIME);
-       bundle_add(kb, AUL_K_STARTTIME, tmp);
-}
-
-int app_request_local(int cmd, bundle *kb)
-{
-       bundle *b;
-
-       _E("app_request_to_launchpad : Same Process Send Local");
-
-       switch (cmd) {
-       case APP_START:
-       case APP_START_RES:
-       case APP_START_ASYNC:
-       case WIDGET_UPDATE:
-       case APP_START_RES_ASYNC:
-       case APP_SEND_LAUNCH_REQUEST:
-               b = bundle_dup(kb);
-               return aul_launch_local(b);
-       case APP_OPEN:
-       case APP_RESUME:
-       case APP_RESUME_BY_PID:
-       case APP_RESUME_BY_PID_ASYNC:
-       case APP_SEND_RESUME_REQUEST:
-               return aul_resume_local();
-       default:
-               _E("no support packet");
-               return AUL_R_LOCAL;
-       }
-}
-
-/**
- * @brief      start caller with kb
- * @return     callee's pid
- */
-int app_request_to_launchpad(int cmd, const char *appid, bundle *kb)
-{
-       return app_request_to_launchpad_for_uid(cmd, appid, kb, getuid());
-}
-
-int app_request_to_launchpad_for_uid(int cmd, const char *appid, bundle *kb, uid_t uid)
-{
-       int must_free = 0;
-       int ret = 0;
-       char buf[MAX_PID_STR_BUFSZ];
-
-       traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:REQ_TO_PAD");
-       _W("request cmd(%d) : appid(%s), target_uid(%d)", cmd, appid, uid);
-       if (kb == NULL) {
-               kb = bundle_create();
-               must_free = 1;
-       } else {
-               __clear_internal_key(kb);
-       }
-
-       bundle_del(kb, AUL_K_APPID);
-       bundle_add(kb, AUL_K_APPID, appid);
-       __set_stime(kb);
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_del(kb, AUL_K_TARGET_UID);
-       bundle_add(kb, AUL_K_TARGET_UID, buf);
-
-       switch (cmd) {
-       case APP_PAUSE:
-       case APP_PAUSE_BY_PID:
-               ret = app_send_cmd_with_queue_noreply_for_uid(AUL_UTIL_PID,
-                               uid, cmd, kb);
-               break;
-       case APP_SEND_LAUNCH_REQUEST:
-       case APP_SEND_LAUNCH_REQUEST_SYNC:
-       case APP_SEND_RESUME_REQUEST:
-               ret = __send_cmd_async_for_uid_opt(AUL_UTIL_PID,
-                               uid, cmd, kb, AUL_SOCK_QUEUE);
-               break;
-       default:
-               ret = app_send_cmd_with_queue_for_uid(AUL_UTIL_PID, uid, cmd,
-                               kb);
-               break;
-       }
-
-       _W("request cmd(%d) result : %d", cmd, ret);
-       if (ret == AUL_R_LOCAL)
-               ret = app_request_local(cmd, kb);
-
-       /* cleanup */
-       if (must_free)
-               bundle_free(kb);
-
-       traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
-
-       return ret;
-}
-
-static int __check_env_pid(void)
-{
-       const char *env_str;
-       int env_pid;
-
-       env_str = getenv("AUL_PID");
-       if (env_str && isdigit(*env_str)) {
-               env_pid = atoi(env_str);
-               if (env_pid != getpid()) {
-                       _W("pid(%d) is not equal to AUL_PID(%d)",
-                                       getpid(), env_pid);
-                       return -1;
-               }
-       }
-
-       return 0;
-}
-
-static int __get_preinit_fd(void)
-{
-       int fd = -1;
-       const char *listen_fd;
-
-       if (__check_env_pid() != 0)
-               return fd;
-
-       listen_fd = getenv("AUL_LISTEN_FD");
-       if (listen_fd) {
-               if (isdigit(*listen_fd))
-                       fd = atoi(listen_fd);
-               setenv("AUL_LISTEN_FD", "-1", 1);
-       }
-
-       return fd;
-}
-
-int aul_initialize(void)
-{
-       int flag;
-
-       if (aul_initialized)
-               return AUL_R_ECANCELED;
-
-       aul_fd = __get_preinit_fd();
-       if (aul_fd > 0 && aul_fd < sysconf(_SC_OPEN_MAX)) {
-               flag = fcntl(aul_fd, F_GETFD);
-               flag |= FD_CLOEXEC;
-               (void)fcntl(aul_fd, F_SETFD, flag);
-       } else {
-               _W("Failed to get preinit fd");
-               aul_fd = aul_sock_create_server(getpid(), getuid());
-               if (aul_fd < 0) {
-                       _E("aul_init create sock failed");
-                       return AUL_R_ECOMM;
-               }
-       }
-       aul_notify_start();
-
-       aul_initialized = 1;
-
-       return aul_fd;
-}
-
-API void aul_finalize()
-{
-       aul_launch_fini();
-
-       if (aul_initialized) {
-               aul_sock_destroy_server(aul_fd);
-               aul_fd = -1;
-       }
-
-       return;
-}
-
-API int aul_request_data_control_socket_pair(bundle *kb, int *fd)
-{
-       bundle *b = kb;
-       int ret;
-       int clifd;
-       int fds[2] = { 0, };
-
-       if (!fd)
-               return AUL_R_EINVAL;
-
-       if (b) {
-               __clear_internal_key(b);
-       } else {
-               b = bundle_create();
-               if (!b)
-                       return AUL_R_ERROR;
-       }
-
-       clifd = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), APP_GET_DC_SOCKET_PAIR, b, AUL_SOCK_ASYNC);
-       if (kb == NULL)
-               bundle_free(b);
-
-       if (clifd > 0) {
-               ret = aul_sock_recv_result_with_fd(clifd);
-               if (ret < 0) {
-                       close(clifd);
-                       if (ret == -EILLEGALACCESS) {
-                               _E("Illegal access in datacontrol socket pair request");
-                               return AUL_R_EILLACC;
-                       }
-                       return ret;
-               }
-
-               ret = aul_sock_recv_reply_sock_fd(clifd, &fds, 1);
-               if (ret == 0)
-                       fd[0] = fds[0];
-       } else {
-               return AUL_R_ERROR;
-       }
-
-       return ret;
-}
-
-API int aul_request_message_port_socket_pair(int *fd)
-{
-       int ret;
-       int fds[2] = {0,};
-
-       if (!fd)
-               return AUL_R_EINVAL;
-
-       ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
-                       APP_GET_MP_SOCKET_PAIR, NULL, 0, AUL_SOCK_ASYNC);
-       if (ret > 0) {
-               ret = aul_sock_recv_reply_sock_fd(ret, &fds, 2);
-               if (ret == 0) {
-                       fd[0] = fds[0];
-                       fd[1] = fds[1];
-               }
-       }
-
-       return ret;
-}
-
-API int aul_launch_app(const char *appid, bundle *kb)
-{
-       return aul_launch_app_for_uid(appid, kb, getuid());
-}
-
-API int aul_launch_app_for_uid(const char *appid, bundle *kb, uid_t uid)
-{
-       int ret;
-
-       if (appid == NULL)
-               return AUL_R_EINVAL;
-
-       ret = app_request_to_launchpad_for_uid(APP_START, appid, kb, uid);
-       return ret;
-}
-
-API int aul_open_app(const char *appid)
-{
-       return aul_open_app_for_uid(appid, getuid());
-}
-
-API int aul_open_app_for_uid(const char *appid, uid_t uid)
-{
-       int ret;
-
-       if (appid == NULL)
-               return AUL_R_EINVAL;
-
-       ret = app_request_to_launchpad_for_uid(APP_OPEN, appid, NULL, uid);
-       return ret;
-}
-
-API int aul_resume_app(const char *appid)
-{
-       return aul_resume_app_for_uid(appid, getuid());
-}
-
-API int aul_resume_app_for_uid(const char *appid, uid_t uid)
-{
-       int ret;
-
-       if (appid == NULL)
-               return AUL_R_EINVAL;
-
-       ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, NULL, uid);
-       return ret;
-}
-
-API int aul_resume_pid(int pid)
-{
-       return aul_resume_pid_for_uid(pid, getuid());
-}
-
-API int aul_resume_pid_for_uid(int pid, uid_t uid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID,
-                       pid_str, NULL, uid);
-       return ret;
-}
-
-API int aul_terminate_pid(int pid)
-{
-       return aul_terminate_pid_for_uid(pid, getuid());
-}
-
-API int aul_terminate_pid_for_uid(int pid, uid_t uid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID,
-                       pid_str, NULL, uid);
-       if (ret == pid)
-               ret = AUL_R_OK;
-
-       return ret;
-}
-
-API int aul_terminate_bgapp_pid(int pid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad(APP_TERM_BGAPP_BY_PID, pid_str, NULL);
-       if (ret == pid)
-               ret = AUL_R_OK;
-
-       return ret;
-}
-
-API int aul_terminate_pid_without_restart(int pid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad(APP_TERM_BY_PID_WITHOUT_RESTART,
-                       pid_str, NULL);
-       return ret;
-}
-
-API int aul_terminate_pid_sync_without_restart(int pid)
-{
-       return aul_terminate_pid_sync_without_restart_for_uid(pid, getuid());
-}
-
-API int aul_terminate_pid_sync_without_restart_for_uid(int pid, uid_t uid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC_WITHOUT_RESTART,
-                       pid_str, NULL, uid);
-       return ret;
-}
-
-API int aul_terminate_pid_async(int pid)
-{
-       return aul_terminate_pid_async_for_uid(pid, getuid());
-}
-
-API int aul_terminate_pid_async_for_uid(int pid, uid_t uid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_ASYNC, pid_str,
-                       NULL, uid);
-       return ret;
-}
-
-API int aul_kill_pid(int pid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad(APP_KILL_BY_PID, pid_str, NULL);
-       return ret;
-}
-
-API void aul_set_preinit_window(void *evas_object)
-{
-       __window_object = evas_object;
-}
-
-API void* aul_get_preinit_window(const char *win_name)
-{
-       return __window_object;
-}
-
-API void aul_set_preinit_background(void *evas_object)
-{
-       __bg_object = evas_object;
-}
-
-API void* aul_get_preinit_background(void)
-{
-       return __bg_object;
-}
-
-API void aul_set_preinit_conformant(void *evas_object)
-{
-       __conformant_object = evas_object;
-}
-
-API void* aul_get_preinit_conformant(void)
-{
-       return __conformant_object;
-}
-
-API int aul_pause_app(const char *appid)
-{
-       return aul_pause_app_for_uid(appid, getuid());
-}
-
-API int aul_pause_app_for_uid(const char *appid, uid_t uid)
-{
-       int ret;
-
-       if (appid == NULL)
-               return AUL_R_EINVAL;
-
-       ret = app_request_to_launchpad_for_uid(APP_PAUSE, appid, NULL, uid);
-       return ret;
-}
-
-API int aul_pause_pid(int pid)
-{
-       return aul_pause_pid_for_uid(pid, getuid());
-}
-
-API int aul_pause_pid_for_uid(int pid, uid_t uid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_PAUSE_BY_PID,
-                       pid_str, NULL, uid);
-       return ret;
-}
-
-API int aul_reload_appinfo(void)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-
-       snprintf(pid_str, sizeof(pid_str), "%d", getpid());
-
-       return app_request_to_launchpad(AMD_RELOAD_APPINFO, pid_str, NULL);
-}
-
-API int aul_is_tep_mount_dbus_done(const char *tep_string)
-{
-       GError *err = NULL;
-       GDBusConnection *conn;
-       GDBusMessage *msg = NULL;
-       GDBusMessage *reply = NULL;
-       GVariant *body;
-       int ret = AUL_R_ERROR;
-
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (conn == NULL) {
-               _E("g_bus_get_sync() is failed. %s", err->message);
-               g_error_free(err);
-               return AUL_R_ERROR;
-       }
-
-       msg = g_dbus_message_new_method_call(TEP_BUS_NAME,
-                                       TEP_OBJECT_PATH,
-                                       TEP_INTERFACE_NAME,
-                                       TEP_IS_MOUNTED_METHOD);
-       if (msg == NULL) {
-               _E("g_dbus_message_new_method_call() is failed. %s",
-                               err->message);
-               goto end;
-       }
-       g_dbus_message_set_body(msg, g_variant_new("(s)", tep_string));
-
-       reply = g_dbus_connection_send_message_with_reply_sync(conn,
-                                       msg,
-                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-                                       500,
-                                       NULL,
-                                       NULL,
-                                       &err);
-       if (reply == NULL) {
-               _E("g_dbus_connection_send_message_with_reply_sync() "
-                                       "is failed. %s", err->message);
-               goto end;
-       }
-
-       body = g_dbus_message_get_body(reply);
-       if (body == NULL) {
-               _E("g_dbus_message_get_body() is failed.");
-               goto end;
-       }
-
-       g_variant_get(body, "(i)", &ret);
-
-end:
-       if (msg)
-               g_object_unref(msg);
-       if (reply)
-               g_object_unref(reply);
-       if (conn)
-               g_object_unref(conn);
-
-       g_clear_error(&err);
-
-       return ret;
-}
-
-API int aul_check_tep_mount(const char *tep_path)
-{
-       if (tep_path) {
-               int rv = -1;
-               int cnt = 0;
-               while (cnt < TEP_ISMOUNT_MAX_RETRY_CNT) {
-                       rv = aul_is_tep_mount_dbus_done(tep_path);
-                       if (rv == 1)
-                               break;
-                       usleep(50 * 1000);
-                       cnt++;
-               }
-               /* incase after trying 1 sec, not getting mounted then quit */
-               if (rv != 1) {
-                       _E("Not able to mount within 1 sec");
-                       return -1;
-               }
-       }
-       return 0;
-}
-
-API int aul_add_loader(const char *loader_path, bundle *kb)
-{
-       return aul_add_loader_for_uid(loader_path, kb, getuid());
-}
-
-API int aul_add_loader_for_uid(const char *loader_path, bundle *kb, uid_t uid)
-{
-       int ret;
-       bundle *b;
-       bundle_raw *kb_raw = NULL;
-       int len;
-       char buf[MAX_PID_STR_BUFSZ];
-
-       if (loader_path == NULL)
-               return AUL_R_EINVAL;
-
-       b = bundle_create();
-       if (b == NULL)
-               return AUL_R_ERROR;
-
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add_str(b, AUL_K_TARGET_UID, buf);
-       bundle_add_str(b, AUL_K_LOADER_PATH, loader_path);
-
-       if (kb) {
-               ret = bundle_encode(kb, &kb_raw, &len);
-               if (ret != BUNDLE_ERROR_NONE) {
-                       bundle_free(b);
-                       return AUL_R_EINVAL;
-               }
-
-               bundle_add_str(b, AUL_K_LOADER_EXTRA, (const char *)kb_raw);
-       }
-
-       ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_ADD_LOADER, b);
-       bundle_free(b);
-       if (kb_raw)
-               free(kb_raw);
-
-       return ret;
-}
-
-API int aul_remove_loader(int loader_id)
-{
-       return aul_remove_loader_for_uid(loader_id, getuid());
-}
-
-API int aul_remove_loader_for_uid(int loader_id, uid_t uid)
-{
-       char buf[MAX_PID_STR_BUFSZ];
-       int ret;
-       bundle *b;
-
-       if (loader_id <= 0)
-               return AUL_R_EINVAL;
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", loader_id);
-       bundle_add_str(b, AUL_K_LOADER_ID, buf);
-       snprintf(buf, sizeof(buf), "%d", uid);
-       bundle_add_str(b, AUL_K_TARGET_UID, buf);
-
-       ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, APP_REMOVE_LOADER, b);
-       bundle_free(b);
-
-       return ret;
-}
-
-API int aul_app_register_pid(const char *appid, int pid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-       bundle *b;
-
-       if (!appid || pid <= 0)
-               return AUL_R_EINVAL;
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("out of memory");
-               return AUL_R_ERROR;
-       }
-
-       bundle_add_str(b, AUL_K_APPID, appid);
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       bundle_add_str(b, AUL_K_PID, pid_str);
-
-       ret = app_send_cmd_with_noreply(AUL_UTIL_PID, APP_REGISTER_PID, b);
-       bundle_free(b);
-
-       return ret;
-}
-
-API int aul_launch_app_async(const char *appid, bundle *kb)
-{
-       return aul_launch_app_async_for_uid(appid, kb, getuid());
-}
-
-API int aul_launch_app_async_for_uid(const char *appid, bundle *kb, uid_t uid)
-{
-       int ret;
-
-       if (appid == NULL)
-               return AUL_R_EINVAL;
-
-       ret = app_request_to_launchpad_for_uid(APP_START_ASYNC, appid, kb, uid);
-       return ret;
-}
-
-API int aul_prepare_candidate_process(void)
-{
-       unsigned char dummy[1] = { 0 };
-
-       return aul_sock_send_raw(AUL_UTIL_PID, getuid(),
-                       APP_PREPARE_CANDIDATE_PROCESS, dummy, 0, AUL_SOCK_NONE);
-}
-
-API int aul_terminate_pid_sync(int pid)
-{
-       return aul_terminate_pid_sync_for_uid(pid, getuid());
-}
-
-API int aul_terminate_pid_sync_for_uid(int pid, uid_t uid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_TERM_BY_PID_SYNC, pid_str,
-                       NULL, uid);
-       return ret;
-}
-
-API int aul_resume_pid_async(int pid)
-{
-       return aul_resume_pid_async_for_uid(pid, getuid());
-}
-
-API int aul_resume_pid_async_for_uid(int pid, uid_t uid)
-{
-       char pid_str[MAX_PID_STR_BUFSZ];
-       int ret;
-
-       if (pid <= 0)
-               return AUL_R_EINVAL;
-
-       snprintf(pid_str, sizeof(pid_str), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_RESUME_BY_PID_ASYNC,
-                       pid_str, NULL, uid);
-       return ret;
-}
-
-API int aul_resume_app_by_instance_id(const char *appid,
-               const char *instance_id)
-{
-       return aul_resume_app_by_instance_id_for_uid(appid,
-                       instance_id, getuid());
-}
-
-API int aul_resume_app_by_instance_id_for_uid(const char *appid,
-               const char *instance_id, uid_t uid)
-{
-       int ret;
-       bundle *b;
-
-       if (appid == NULL || instance_id == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("Out of memory");
-               return AUL_R_EINVAL;
-       }
-
-       ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
-       if (ret != BUNDLE_ERROR_NONE) {
-               _E("Failed to add instance id(%s)", instance_id);
-               bundle_free(b);
-               return AUL_R_ERROR;
-       }
-
-       ret = app_request_to_launchpad_for_uid(APP_RESUME, appid, b, uid);
-       bundle_free(b);
-
-       return ret;
-}
-
-API int aul_terminate_instance_async(const char *instance_id, int pid)
-{
-       return aul_terminate_instance_async_for_uid(instance_id, pid, getuid());
-}
-
-API int aul_terminate_instance_async_for_uid(const char *instance_id, int pid,
-               uid_t uid)
-{
-       char buf[32];
-       int ret;
-       bundle *b;
-
-       if (instance_id == NULL) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       b = bundle_create();
-       if (b == NULL) {
-               _E("Out of memory");
-               return AUL_R_ERROR;
-       }
-
-       ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
-       if (ret != BUNDLE_ERROR_NONE) {
-               _E("Failed to add instance ID(%s)", instance_id);
-               bundle_free(b);
-               return AUL_R_ERROR;
-       }
-
-       snprintf(buf, sizeof(buf), "%d", pid);
-       ret = app_request_to_launchpad_for_uid(APP_TERM_INSTANCE_ASYNC,
-                       buf, b, uid);
-       bundle_free(b);
-
-       return ret;
-}
-
-static int __send_request(int cmd, uid_t uid, const char *appid,
-               const char *instance_id)
-{
-       bundle *b;
-       int ret;
-
-       b = bundle_create();
-       if (!b) {
-               _E("Out of memory");
-               return AUL_R_ENOMEM;
-       }
-
-       if (instance_id) {
-               ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
-               if (ret != BUNDLE_ERROR_NONE) {
-                       _E("Failed to add instance ID");
-                       bundle_free(b);
-                       if (ret == BUNDLE_ERROR_OUT_OF_MEMORY)
-                               return AUL_R_ENOMEM;
-                       else if (ret == BUNDLE_ERROR_INVALID_PARAMETER)
-                               return AUL_R_EINVAL;
-
-                       return AUL_R_ERROR;
-               }
-       }
-
-       ret = app_request_to_launchpad_for_uid(cmd, appid, b, uid);
-       bundle_free(b);
-       if (ret < 0)
-               _E("Failed to send request(%d). error(%d)", cmd, ret);
-
-       return ret;
-}
-
-API int aul_terminate_app_with_instance_id(const char *appid,
-               const char *instance_id)
-{
-       return aul_terminate_app_with_instance_id_for_uid(appid, instance_id,
-                       getuid());
-}
-
-API int aul_terminate_app_with_instance_id_for_uid(const char *appid,
-               const char *instance_id, uid_t uid)
-{
-       if (!appid || !instance_id) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       return __send_request(APP_TERMINATE, uid, appid, instance_id);
-}
-
-API int aul_terminate_app(const char *appid)
-{
-       return aul_terminate_app_for_uid(appid, getuid());
-}
-
-API int aul_terminate_app_for_uid(const char *appid, uid_t uid)
-{
-       if (!appid) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       return __send_request(APP_TERMINATE, uid, appid, NULL);
-}
-
-API int aul_prepare_app_defined_loader(const char *loader_name)
-{
-       return aul_prepare_app_defined_loader_for_uid(loader_name, getuid());
-}
-
-API int aul_prepare_app_defined_loader_for_uid(const char *loader_name,
-               uid_t uid)
-{
-       char buf[MAX_PID_STR_BUFSZ];
-       bundle *b;
-       int ret;
-
-       if (!loader_name) {
-               _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_LOADER_NAME, loader_name);
-       snprintf(buf, sizeof(buf), "%u", uid);
-       bundle_add(b, AUL_K_TARGET_UID, buf);
-
-       ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid,
-                       APP_PREPARE_APP_DEFINED_LOADER, b);
-       bundle_free(b);
-       if (ret < 0) {
-               _E("Failed to prepare app-defined loader. error(%d)", ret);
-               return ret;
-       }
-
-       _I("loader id(%d)", ret);
-       return ret;
-}
diff --git a/src/launch.cc b/src/launch.cc
new file mode 100644 (file)
index 0000000..bb658b6
--- /dev/null
@@ -0,0 +1,849 @@
+/*
+ * Copyright (c) 2000 - 2021 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 <bundle_cpp.h>
+#include <bundle_internal.h>
+#include <ctype.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <gio/gio.h>
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <ttrace.h>
+
+#include <memory>
+
+#include "app_signal.h"
+#include "aul.h"
+#include "aul_api.h"
+#include "aul_app_com.h"
+#include "aul_error.h"
+#include "aul_sock.h"
+#include "aul_util.h"
+#include "key.h"
+#include "launch.h"
+
+namespace {
+
+constexpr const int TEP_ISMOUNT_MAX_RETRY_CNT = 20;
+
+class AppRequest {
+ public:
+  AppRequest(int cmd, uid_t uid) : cmd_(cmd), uid_(uid) {
+    SetUid(uid);
+  }
+
+  AppRequest(int cmd) : cmd_(cmd), uid_(getuid()) {
+    SetUid(uid_);
+  }
+
+  AppRequest& With(tizen_base::Bundle b) {
+    bundle_ = std::move(b);
+    ClearInternalKey();
+    SetUid(uid_);
+    return *this;
+  }
+
+  AppRequest& With(bundle* b) {
+    if (b == nullptr)
+      return *this;
+    bundle_ = tizen_base::Bundle(b, false, false);
+    ClearInternalKey();
+    SetUid(uid_);
+    return *this;
+  }
+
+  AppRequest& SetAppId(const std::string& app_id) {
+    bundle_.Delete(AUL_K_APPID);
+    bundle_.Add(AUL_K_APPID, app_id);
+    return *this;
+  }
+
+  AppRequest& SetInstId(const std::string& inst_id) {
+    bundle_.Delete(AUL_K_INSTANCE_ID);
+    bundle_.Add(AUL_K_INSTANCE_ID, inst_id);
+    return *this;
+  }
+
+  AppRequest& SetAppIdAsPid(pid_t pid) {
+    char buf[MAX_PID_STR_BUFSZ];
+    snprintf(buf, sizeof(buf), "%d", pid);
+    bundle_.Delete(AUL_K_APPID);
+    bundle_.Add(AUL_K_APPID, buf);
+    return *this;
+  }
+
+  AppRequest& SetPid(pid_t pid) {
+    char buf[MAX_PID_STR_BUFSZ];
+    snprintf(buf, sizeof(buf), "%d", pid);
+    bundle_.Delete(AUL_K_PID);
+    bundle_.Add(AUL_K_PID, buf);
+    return *this;
+  }
+
+  int Send(int opt = AUL_SOCK_QUEUE) {
+    traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:REQ_TO_PAD");
+    std::string appid = bundle_.GetString(AUL_K_APPID);
+    _W("Request cmd(%d): appid(%s), target_uid(%u)",
+        cmd_, appid.c_str(), uid_);
+
+    SetTime();
+    switch (cmd_) {
+      case APP_SEND_LAUNCH_REQUEST:
+      case APP_SEND_LAUNCH_REQUEST_SYNC:
+      case APP_SEND_RESUME_REQUEST:
+        opt |= AUL_SOCK_ASYNC;
+        break;
+    }
+
+    int ret = aul_sock_send_bundle(AUL_UTIL_PID, uid_, cmd_,
+        bundle_.GetHandle(), opt);
+    if (ret < 0)
+      ret = aul_error_convert(ret);
+    _W("Request cmd(%d): result(%d)", cmd_, ret);
+    if (ret == AUL_R_LOCAL)
+      ret = app_request_local(cmd_, bundle_.GetHandle());
+    traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+    return ret;
+  }
+
+  int SendSimply(int opt = AUL_SOCK_NONE) {
+    _W("Request cmd(%d): target_uid(%u)", cmd_, uid_);
+    int ret = aul_sock_send_bundle(AUL_UTIL_PID, uid_, cmd_,
+        bundle_.GetHandle(), opt);
+    if (ret < 0)
+      ret = aul_error_convert(ret);
+    _W("Request cmd(%d): result(%d)", cmd_, ret);
+    return ret;
+  }
+
+  int SendCmdOnly(int opt = AUL_SOCK_NONE) {
+    _W("Request cmd(%d): target_uid(%u)", cmd_, uid_);
+    int ret = aul_sock_send_raw(AUL_UTIL_PID, uid_, cmd_, nullptr, 0, opt);
+    _W("Request cmd(%d): result(%d)", cmd_, ret);
+    return ret;
+  }
+
+ private:
+  void ClearInternalKey() {
+    bundle_.Delete(AUL_K_CALLER_PID);
+    bundle_.Delete(AUL_K_APPID);
+    bundle_.Delete(AUL_K_WAIT_RESULT);
+    bundle_.Delete(AUL_K_SEND_RESULT);
+    bundle_.Delete(AUL_K_ARGV0);
+  }
+
+  void SetTime() {
+    struct timespec start;
+    clock_gettime(CLOCK_MONOTONIC, &start);
+    char tmp[MAX_LOCAL_BUFSZ];
+    snprintf(tmp, sizeof(tmp), "%ld/%ld", start.tv_sec, start.tv_nsec);
+    bundle_.Delete(AUL_K_STARTTIME);
+    bundle_.Add(AUL_K_STARTTIME, tmp);
+  }
+
+  void SetUid(uid_t uid) {
+    char buf[MAX_PID_STR_BUFSZ];
+    snprintf(buf, sizeof(buf), "%d", uid);
+    bundle_.Delete(AUL_K_TARGET_UID);
+    bundle_.Add(AUL_K_TARGET_UID, buf);
+  }
+
+ private:
+  int cmd_;
+  uid_t uid_;
+  tizen_base::Bundle bundle_;
+};
+
+int aul_initialized = 0;
+int aul_fd;
+void* window_object = nullptr;
+void* bg_object = nullptr;
+void* conformant_object = nullptr;
+
+int SendCmdForUidOpt(int pid, uid_t uid, int cmd, bundle* kb, int opt) {
+  int res = aul_sock_send_bundle(pid, uid, cmd, kb, opt);
+  if (res < 0)
+    res = aul_error_convert(res);
+
+  return res;
+}
+
+int SendCmdNoReplyForUidOpt(int pid, uid_t uid, int cmd, bundle* kb, int opt) {
+  int res = aul_sock_send_bundle(pid, uid, cmd, kb, opt | AUL_SOCK_NOREPLY);
+  if (res < 0)
+    res = aul_error_convert(res);
+
+  return res;
+}
+
+int CheckEnvPid() {
+  const char* env_str = getenv("AUL_PID");
+  if (env_str && isdigit(*env_str)) {
+    int env_pid = atoi(env_str);
+    if (env_pid != getpid()) {
+      _W("pid(%d) is not equal to AUL_PID(%d)",
+          getpid(), env_pid);
+      return -1;
+    }
+  }
+
+  return 0;
+}
+
+int GetPreInitFd() {
+  int fd = -1;
+
+  if (CheckEnvPid() != 0)
+    return fd;
+
+  const char* listen_fd = getenv("AUL_LISTEN_FD");
+  if (listen_fd) {
+    if (isdigit(*listen_fd))
+      fd = atoi(listen_fd);
+    setenv("AUL_LISTEN_FD", "-1", 1);
+  }
+
+  return fd;
+}
+
+}  // namespace
+
+extern "C" int aul_is_initialized() {
+  return aul_initialized;
+}
+
+extern "C" API int app_send_cmd(int pid, int cmd, bundle* kb) {
+  return SendCmdForUidOpt(pid, getuid(), cmd, kb, AUL_SOCK_NONE);
+}
+
+extern "C" API int app_send_cmd_for_uid(int pid, uid_t uid, int cmd,
+    bundle* kb) {
+  return SendCmdForUidOpt(pid, uid, cmd, kb, AUL_SOCK_NONE);
+}
+
+extern "C" API int app_send_cmd_with_queue_for_uid(int pid, uid_t uid, int cmd,
+    bundle* kb) {
+  return SendCmdForUidOpt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
+}
+
+extern "C" API int app_send_cmd_with_queue_noreply_for_uid(int pid, uid_t uid,
+    int cmd, bundle* kb) {
+  return SendCmdNoReplyForUidOpt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
+}
+
+extern "C" API int app_send_cmd_with_noreply(int pid, int cmd, bundle* kb) {
+  return SendCmdForUidOpt(pid, getuid(), cmd, kb, AUL_SOCK_NOREPLY);
+}
+
+extern "C" API int app_send_cmd_to_launchpad(const char* pad_type, uid_t uid,
+    int cmd, bundle* kb) {
+  int fd = aul_sock_create_launchpad_client(pad_type, uid);
+  if (fd < 0)
+    return -1;
+
+  int res = aul_sock_send_bundle_with_fd(fd, cmd, kb, AUL_SOCK_ASYNC);
+  if (res < 0) {
+    close(fd);
+    return res;
+  }
+
+retry_recv:
+  int len = recv(fd, &res, sizeof(int), 0);
+  if (len == -1) {
+    if (errno == EAGAIN) {
+      char buf[1024];
+      _E("recv timeout: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
+      res = -EAGAIN;
+    } else if (errno == EINTR) {
+      char buf[1024];
+      _D("recv: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
+      goto retry_recv;
+    } else {
+      char buf[1024];
+      _E("recv error: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
+      res = -ECOMM;
+    }
+  }
+
+  close(fd);
+  return res;
+}
+
+extern "C" int app_request_local(int cmd, bundle* kb) {
+  _E("app_request_to_launchpad : Same Process Send Local");
+  switch (cmd) {
+  case APP_START:
+  case APP_START_RES:
+  case APP_START_ASYNC:
+  case WIDGET_UPDATE:
+  case APP_START_RES_ASYNC:
+  case APP_SEND_LAUNCH_REQUEST:
+    return aul_launch_local(bundle_dup(kb));
+  case APP_OPEN:
+  case APP_RESUME:
+  case APP_RESUME_BY_PID:
+  case APP_RESUME_BY_PID_ASYNC:
+  case APP_SEND_RESUME_REQUEST:
+    return aul_resume_local();
+  default:
+    _E("no support packet");
+    return AUL_R_LOCAL;
+  }
+}
+
+extern "C" int app_request_to_launchpad(int cmd, const char* appid,
+    bundle* kb) {
+  return app_request_to_launchpad_for_uid(cmd, appid, kb, getuid());
+}
+
+extern "C" int app_request_to_launchpad_for_uid(int cmd, const char* appid,
+    bundle* kb, uid_t uid) {
+  return AppRequest(cmd, uid)
+      .With(kb)
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" int aul_initialize() {
+  if (aul_initialized)
+    return AUL_R_ECANCELED;
+
+  aul_fd = GetPreInitFd();
+  if (aul_fd > 0 && aul_fd < sysconf(_SC_OPEN_MAX)) {
+    int flag = fcntl(aul_fd, F_GETFD);
+    flag |= FD_CLOEXEC;
+    (void)fcntl(aul_fd, F_SETFD, flag);
+  } else {
+    _W("Failed to get preinit fd");
+    aul_fd = aul_sock_create_server(getpid(), getuid());
+    if (aul_fd < 0) {
+      _E("aul_init create sock failed");
+      return AUL_R_ECOMM;
+    }
+  }
+  aul_notify_start();
+
+  aul_initialized = 1;
+  return aul_fd;
+}
+
+extern "C" API void aul_finalize() {
+  aul_launch_fini();
+
+  if (aul_initialized) {
+    aul_sock_destroy_server(aul_fd);
+    aul_fd = -1;
+  }
+}
+
+extern "C" API int aul_request_data_control_socket_pair(bundle* kb, int* fd) {
+  if (fd == nullptr)
+    return AUL_R_EINVAL;
+
+  int clifd = AppRequest(APP_GET_DC_SOCKET_PAIR)
+      .With(kb)
+      .SendSimply(AUL_SOCK_ASYNC);
+  if (clifd <= 0)
+    return AUL_R_ERROR;
+
+  int ret = aul_sock_recv_result_with_fd(clifd);
+  if (ret < 0) {
+    close(clifd);
+    if (ret == -EILLEGALACCESS) {
+      _E("Illegal access in datacontrol socket pair request");
+      return AUL_R_EILLACC;
+    }
+    return ret;
+  }
+
+  int fds[2] = { 0, };
+  ret = aul_sock_recv_reply_sock_fd(clifd, &fds, 1);
+  if (ret == 0)
+    fd[0] = fds[0];
+
+  return ret;
+}
+
+extern "C" API int aul_request_message_port_socket_pair(int* fd) {
+  if (fd == nullptr)
+    return AUL_R_EINVAL;
+
+  int ret = AppRequest(APP_GET_MP_SOCKET_PAIR)
+      .SendCmdOnly(AUL_SOCK_ASYNC);
+  if (ret > 0) {
+    int fds[2] = {0,};
+    ret = aul_sock_recv_reply_sock_fd(ret, &fds, 2);
+    if (ret == 0) {
+      fd[0] = fds[0];
+      fd[1] = fds[1];
+    }
+  }
+
+  return ret;
+}
+
+extern "C" API int aul_launch_app(const char* appid, bundle* kb) {
+  return aul_launch_app_for_uid(appid, kb, getuid());
+}
+
+extern "C" API int aul_launch_app_for_uid(const char* appid, bundle* kb,
+    uid_t uid) {
+  if (appid == nullptr)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_START, uid)
+      .With(kb)
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" API int aul_open_app(const char* appid) {
+  return aul_open_app_for_uid(appid, getuid());
+}
+
+extern "C" API int aul_open_app_for_uid(const char* appid, uid_t uid) {
+  if (appid == nullptr)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_OPEN, uid)
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" API int aul_resume_app(const char* appid) {
+  return aul_resume_app_for_uid(appid, getuid());
+}
+
+extern "C" API int aul_resume_app_for_uid(const char* appid, uid_t uid) {
+  if (appid == nullptr)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_RESUME, uid)
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" API int aul_resume_pid(int pid) {
+  return aul_resume_pid_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_resume_pid_for_uid(int pid, uid_t uid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_RESUME_BY_PID, uid)
+      .SetAppIdAsPid(pid)
+      .Send();
+}
+
+extern "C" API int aul_terminate_pid(int pid) {
+  return aul_terminate_pid_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_terminate_pid_for_uid(int pid, uid_t uid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  int ret = AppRequest(APP_TERM_BY_PID, uid)
+      .SetAppIdAsPid(pid)
+      .Send();
+  if (ret == pid)
+    ret = AUL_R_OK;
+
+  return ret;
+}
+
+extern "C" API int aul_terminate_bgapp_pid(int pid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  int ret = AppRequest(APP_TERM_BGAPP_BY_PID)
+      .SetAppIdAsPid(pid)
+      .Send();
+  if (ret == pid)
+    ret = AUL_R_OK;
+
+  return ret;
+}
+
+extern "C" API int aul_terminate_pid_without_restart(int pid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_TERM_BY_PID_WITHOUT_RESTART)
+      .SetAppIdAsPid(pid)
+      .Send();
+}
+
+extern "C" API int aul_terminate_pid_sync_without_restart(int pid) {
+  return aul_terminate_pid_sync_without_restart_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_terminate_pid_sync_without_restart_for_uid(int pid,
+    uid_t uid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_TERM_BY_PID_SYNC_WITHOUT_RESTART, uid)
+      .SetAppIdAsPid(pid)
+      .Send();
+}
+
+extern "C" API int aul_terminate_pid_async(int pid) {
+  return aul_terminate_pid_async_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_terminate_pid_async_for_uid(int pid, uid_t uid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_TERM_BY_PID_ASYNC, uid)
+      .SetAppIdAsPid(pid)
+      .Send();
+}
+
+extern "C" API int aul_kill_pid(int pid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_KILL_BY_PID)
+      .SetAppIdAsPid(pid)
+      .Send();
+}
+
+extern "C" API void aul_set_preinit_window(void* evas_object) {
+  window_object = evas_object;
+}
+
+extern "C" API void* aul_get_preinit_window(const char* win_name) {
+  return window_object;
+}
+
+extern "C" API void aul_set_preinit_background(void* evas_object) {
+  bg_object = evas_object;
+}
+
+extern "C" API void* aul_get_preinit_background(void) {
+  return bg_object;
+}
+
+extern "C" API void aul_set_preinit_conformant(void* evas_object) {
+  conformant_object = evas_object;
+}
+
+extern "C" API void* aul_get_preinit_conformant(void) {
+  return conformant_object;
+}
+
+extern "C" API int aul_pause_app(const char* appid) {
+  return aul_pause_app_for_uid(appid, getuid());
+}
+
+extern "C" API int aul_pause_app_for_uid(const char* appid, uid_t uid) {
+  if (appid == nullptr)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_PAUSE, uid)
+      .SetAppId(appid)
+      .Send(AUL_SOCK_QUEUE | AUL_SOCK_NOREPLY);
+}
+
+extern "C" API int aul_pause_pid(int pid) {
+  return aul_pause_pid_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_pause_pid_for_uid(int pid, uid_t uid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_PAUSE_BY_PID, uid)
+      .SetAppIdAsPid(pid)
+      .Send(AUL_SOCK_QUEUE | AUL_SOCK_NOREPLY);
+}
+
+extern "C" API int aul_reload_appinfo(void) {
+  return AppRequest(AMD_RELOAD_APPINFO)
+      .SetAppIdAsPid(getpid())
+      .Send();
+}
+
+extern "C" API int aul_is_tep_mount_dbus_done(const char* tep_string) {
+  GError* err = nullptr;
+  GDBusConnection* conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &err);
+  if (conn == nullptr) {
+    _E("g_bus_get_sync() is failed. error(%s)", err ? err->message : "Unknown");
+    g_clear_error(&err);
+    return AUL_R_ERROR;
+  }
+
+  std::unique_ptr<GDBusConnection, decltype(g_object_unref)*> conn_ptr(
+      conn, g_object_unref);
+
+  GDBusMessage* msg = g_dbus_message_new_method_call(TEP_BUS_NAME,
+          TEP_OBJECT_PATH,
+          TEP_INTERFACE_NAME,
+          TEP_IS_MOUNTED_METHOD);
+  if (msg == nullptr) {
+    _E("g_dbus_message_new_method_call() is failed. error(%s)",
+        err ? err->message : "Unknown");
+    return AUL_R_ERROR;
+  }
+
+  std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> msg_ptr(
+      msg, g_object_unref);
+  g_dbus_message_set_body(msg, g_variant_new("(s)", tep_string));
+
+  GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(conn,
+          msg,
+          G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+          500,
+          nullptr,
+          nullptr,
+          &err);
+  if (reply == nullptr || err != nullptr) {
+    _E("g_dbus_connection_send_message_with_reply_sync() is failed. error(%s)",
+        err ? err->message : "Unknown");
+    g_clear_error(&err);
+    return AUL_R_ERROR;
+  }
+
+  std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> reply_ptr(
+      reply, g_object_unref);
+
+  GVariant* body = g_dbus_message_get_body(reply);
+  if (body == nullptr) {
+    _E("g_dbus_message_get_body() is failed.");
+    return AUL_R_ERROR;
+  }
+
+  int ret = AUL_R_ERROR;
+  g_variant_get(body, "(i)", &ret);
+  return ret;
+}
+
+extern "C" API int aul_check_tep_mount(const char* tep_path) {
+  if (tep_path) {
+    int ret = -1;
+    int cnt = 0;
+    while (cnt < TEP_ISMOUNT_MAX_RETRY_CNT) {
+      ret = aul_is_tep_mount_dbus_done(tep_path);
+      if (ret == 1)
+        break;
+      usleep(50 * 1000);
+      cnt++;
+    }
+    /* incase after trying 1 sec, not getting mounted then quit */
+    if (ret != 1) {
+      _E("Not able to mount within 1 sec");
+      return -1;
+    }
+  }
+  return 0;
+}
+
+extern "C" API int aul_add_loader(const char* loader_path, bundle* kb) {
+  return aul_add_loader_for_uid(loader_path, kb, getuid());
+}
+
+extern "C" API int aul_add_loader_for_uid(const char* loader_path, bundle* kb,
+    uid_t uid) {
+  if (loader_path == nullptr)
+    return AUL_R_EINVAL;
+
+  tizen_base::Bundle b = { {AUL_K_LOADER_PATH, loader_path} };
+
+  if (kb) {
+    try {
+      tizen_base::Bundle extra(kb, false, false);
+      auto raw = extra.ToRaw();
+      b.Add(AUL_K_LOADER_EXTRA, (const char*)(raw.first.get()));
+    } catch (const std::bad_alloc&) {
+      return AUL_R_EINVAL;
+    }
+  }
+
+  return AppRequest(APP_ADD_LOADER, uid)
+      .With(std::move(b))
+      .SendSimply();
+}
+
+extern "C" API int aul_remove_loader(int loader_id) {
+  return aul_remove_loader_for_uid(loader_id, getuid());
+}
+
+extern "C" API int aul_remove_loader_for_uid(int loader_id, uid_t uid) {
+  if (loader_id <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_REMOVE_LOADER, uid)
+      .With({{AUL_K_LOADER_ID, std::to_string(loader_id)}})
+      .SendSimply();
+}
+
+extern "C" API int aul_app_register_pid(const char* appid, int pid) {
+  if (appid == nullptr || pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_REGISTER_PID)
+      .SetAppId(appid)
+      .SetPid(pid)
+      .SendSimply(AUL_SOCK_NOREPLY);
+}
+
+extern "C" API int aul_launch_app_async(const char* appid, bundle* kb) {
+  return aul_launch_app_async_for_uid(appid, kb, getuid());
+}
+
+extern "C" API int aul_launch_app_async_for_uid(const char* appid, bundle* kb,
+    uid_t uid) {
+  if (appid == nullptr)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_START_ASYNC, uid)
+      .With(kb)
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" API int aul_prepare_candidate_process(void) {
+  return AppRequest(APP_PREPARE_CANDIDATE_PROCESS)
+      .SendCmdOnly();
+}
+
+extern "C" API int aul_terminate_pid_sync(int pid) {
+  return aul_terminate_pid_sync_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_terminate_pid_sync_for_uid(int pid, uid_t uid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_TERM_BY_PID_SYNC, uid)
+      .SetAppIdAsPid(pid)
+      .Send();
+}
+
+extern "C" API int aul_resume_pid_async(int pid) {
+  return aul_resume_pid_async_for_uid(pid, getuid());
+}
+
+extern "C" API int aul_resume_pid_async_for_uid(int pid, uid_t uid) {
+  if (pid <= 0)
+    return AUL_R_EINVAL;
+
+  return AppRequest(APP_RESUME_BY_PID_ASYNC, uid)
+      .SetAppIdAsPid(pid)
+      .Send();
+}
+
+extern "C" API int aul_resume_app_by_instance_id(const char* appid,
+    const char *instance_id) {
+  return aul_resume_app_by_instance_id_for_uid(appid, instance_id, getuid());
+}
+
+extern "C" API int aul_resume_app_by_instance_id_for_uid(const char* appid,
+    const char* instance_id, uid_t uid) {
+  if (appid == nullptr || instance_id == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_RESUME, uid)
+      .SetAppId(appid)
+      .SetInstId(instance_id)
+      .Send();
+}
+
+extern "C" API int aul_terminate_instance_async(const char* instance_id,
+    int pid) {
+  return aul_terminate_instance_async_for_uid(instance_id, pid, getuid());
+}
+
+extern "C" API int aul_terminate_instance_async_for_uid(const char* instance_id,
+    int pid, uid_t uid) {
+  if (instance_id == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_TERM_INSTANCE_ASYNC, uid)
+      .SetAppIdAsPid(pid)
+      .SetInstId(instance_id)
+      .Send();
+}
+
+extern "C" API int aul_terminate_app_with_instance_id(const char* appid,
+    const char* instance_id) {
+  return aul_terminate_app_with_instance_id_for_uid(appid, instance_id,
+      getuid());
+}
+
+extern "C" API int aul_terminate_app_with_instance_id_for_uid(const char* appid,
+    const char* instance_id, uid_t uid) {
+  if (appid == nullptr || instance_id == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_TERMINATE, uid)
+      .SetAppId(appid)
+      .SetInstId(instance_id)
+      .Send();
+}
+
+extern "C" API int aul_terminate_app(const char* appid) {
+  return aul_terminate_app_for_uid(appid, getuid());
+}
+
+extern "C" API int aul_terminate_app_for_uid(const char* appid, uid_t uid) {
+  if (appid == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  return AppRequest(APP_TERMINATE, uid)
+      .SetAppId(appid)
+      .Send();
+}
+
+extern "C" API int aul_prepare_app_defined_loader(const char* loader_name) {
+  return aul_prepare_app_defined_loader_for_uid(loader_name, getuid());
+}
+
+extern "C" API int aul_prepare_app_defined_loader_for_uid(
+    const char* loader_name, uid_t uid) {
+  if (loader_name == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  int ret = AppRequest(APP_PREPARE_APP_DEFINED_LOADER, uid)
+      .With({{AUL_K_LOADER_NAME, loader_name}})
+      .SendSimply();
+  if (ret < 0) {
+    _E("Failed to prepare app-defined loader. error(%d)", ret);
+    return ret;
+  }
+
+  _I("loader id(%d)", ret);
+  return ret;
+}