Support instance termination 06/206706/5
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 23 May 2019 10:08:11 +0000 (19:08 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 23 May 2019 23:44:03 +0000 (08:44 +0900)
From Tizen 5.5, the component-based application is supported. The
component-based application has many components. We have to support
to terminate each instance.
When a developer calls aul_svc_subapp_terminate_request(), the daemon
terminates the launched instance.

Adds:
 - aul_terminate_instance_async()
 - aul_terminate_instance_async_for_uid()
 - aul_svc_subapp_termiante_request()

Change-Id: I14cb407e9af9f291fdf746f7dcf189e540ab0838
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
CMakeLists.txt
include/aul.h
include/aul_svc.h
include/launch.h
packaging/aul.spec
src/launch.c
src/launch_with_result.c
src/service.c

index 08ae927..551adbe 100755 (executable)
@@ -11,7 +11,22 @@ ADD_DEFINITIONS("-DSHARE_PREFIX=\"${SHARE_INSTALL_PREFIX}/aul\"")
 
 # Set required packages
 INCLUDE(FindPkgConfig)
-SET(AUL-1_LIB_PKG_CHECK_MODULES dlog bundle xdgmime libtzplatform-config pkgmgr-info capi-system-info vconf sqlite3 iniparser gio-2.0 glib-2.0 libxml-2.0 ttrace storage)
+SET(AUL-1_LIB_PKG_CHECK_MODULES
+       dlog
+       bundle
+       xdgmime
+       libtzplatform-config
+       pkgmgr-info
+       capi-system-info
+       vconf
+       sqlite3
+       iniparser
+       gio-2.0
+       glib-2.0
+       libxml-2.0
+       ttrace
+       storage
+       uuid)
 pkg_check_modules(libpkgs REQUIRED ${AUL-1_LIB_PKG_CHECK_MODULES})
 
 FOREACH(flag ${libpkgs_CFLAGS})
index 57fba92..921c043 100644 (file)
@@ -2904,6 +2904,13 @@ int aul_notify_start(void);
  */
 const char *aul_app_status_convert_to_string(int status);
 
+/**
+ * This API is only for App Framework internally.
+ */
+int aul_terminate_instance_async(const char *instance_id, int pid);
+int aul_terminate_instance_async_for_uid(const char *instance_id, int pid,
+               uid_t uid);
+
 #ifdef __cplusplus
        }
 #endif
index 5a20d75..7deb549 100755 (executable)
@@ -1384,6 +1384,11 @@ int aul_svc_set_comp_id(bundle *b, const char *comp_id);
  */
 const char *aul_svc_get_comp_id(bundle *b);
 
+/**
+ * This API is only for App Framework internally.
+ */
+int aul_svc_subapp_terminate_request(bundle *b, int pid);
+
 #ifdef __cplusplus
 }
 #endif
index 1a22306..789efa8 100644 (file)
@@ -52,3 +52,4 @@ int aul_send_launch_request_for_uid(const char *appid, bundle *b, uid_t uid,
 int aul_send_launch_request_sync_for_uid(const char *appid, bundle *b,
                uid_t uid, bundle **res_b);
 int app_request_local(int cmd, bundle *kb);
+int aul_subapp_terminate_request(const char *instance_id, int pid);
index 70c8f2f..1926135 100755 (executable)
@@ -31,6 +31,7 @@ BuildRequires:  pkgconfig(storage)
 BuildRequires:  pkgconfig(ttrace)
 BuildRequires:  pkgconfig(pkgmgr-installer)
 BuildRequires:  pkgconfig(libxml-2.0)
+BuildRequires:  pkgconfig(uuid)
 
 %description
 Application utility library
index 6f86d97..02611d1 100755 (executable)
@@ -914,3 +914,41 @@ API int aul_resume_app_by_instance_id_for_uid(const char *appid,
 
        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;
+}
index c461913..7eb856a 100644 (file)
@@ -54,6 +54,7 @@ static GList *__resultcb_list;
 static int __rand(int n)
 {
        unsigned int seed = time(NULL) + n;
+
        return rand_r(&seed);
 }
 
@@ -533,6 +534,30 @@ API int aul_subapp_terminate_request_pid(int pid)
        return ret;
 }
 
+int aul_subapp_terminate_request(const char *instance_id, int pid)
+{
+       app_resultcb_info_t *info;
+       GList *iter;
+
+       if (pid <= 0)
+               return AUL_R_EINVAL;
+
+       pthread_mutex_lock(&__aul_mutex);
+       iter = __resultcb_list;
+       while (iter) {
+               info = (app_resultcb_info_t *)iter->data;
+               iter = g_list_next(iter);
+               if (info->launched_pid == pid && !info->error_cb) {
+                       __resultcb_list = g_list_remove(
+                                       __resultcb_list, info);
+                       __destroy_resultcb(info);
+               }
+       }
+       pthread_mutex_unlock(&__aul_mutex);
+
+       return aul_terminate_instance_async(instance_id, pid);
+}
+
 API int aul_add_caller_cb(int pid, void (*caller_cb)(int, void *),
                void *data)
 {
index ba0bb0c..6a0152d 100755 (executable)
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <uuid.h>
 #include <bundle.h>
 #include <bundle_internal.h>
 #include <glib.h>
@@ -38,6 +39,7 @@
 #include "aul_svc_priv_key.h"
 #include "launch.h"
 #include "aul_svc_internal.h"
+#include "aul_app_group.h"
 
 #define MAX_CHECKSUM_BUF       2048
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
@@ -276,22 +278,40 @@ static void __aul_error_cb(int err, void *data)
        __remove_rescb(cb_info);
 }
 
-static int __run_svc_with_pkgname(char *pkgname, bundle *b, int request_code,
-               aul_svc_res_fn cbfunc, aul_svc_err_cb err_cb,
-               void *data, uid_t uid, bool sync)
+static void __set_instance_id(bundle *b, const char *appid)
 {
-       aul_svc_cb_info_t *cb_info = NULL;
-       int ret = -1;
+       const char *comp_id;
+       char buf[1024];
+       char uuid[37];
+       uuid_t u;
+
+       uuid_generate(u);
+       uuid_unparse(u, uuid);
+
+       comp_id = bundle_get_val(b, AUL_K_COMPONENT_ID);
+       if (comp_id)
+               snprintf(buf, sizeof(buf), "%s:%s:%s", uuid, appid, comp_id);
+       else
+               snprintf(buf, sizeof(buf), "%s:%s", uuid, appid);
 
-       if (bundle_get_type(b, AUL_SVC_K_SELECTOR_EXTRA_LIST) != BUNDLE_TYPE_NONE) {
+       bundle_add(b, AUL_K_INSTANCE_ID, buf);
+}
+
+static void __verify_request(bundle *b, char **appid)
+{
+       const char *launch_mode;
+       int ret;
+
+       ret = bundle_get_type(b, AUL_SVC_K_SELECTOR_EXTRA_LIST);
+       if (ret != BUNDLE_TYPE_NONE) {
                if (!aul_svc_get_pkgname(b))
-                       pkgname = APP_SELECTOR;
+                       *appid = APP_SELECTOR;
        }
 
        if (bundle_get_val(b, AUL_K_FORCE_LAUNCH_APP_SELECTOR))
-               pkgname = APP_SELECTOR;
+               *appid = APP_SELECTOR;
 
-       if (__is_special_app(pkgname) || __is_special_operation(b)) {
+       if (__is_special_app(*appid) || __is_special_operation(b)) {
                bundle_del(b, AUL_SVC_K_CAN_BE_LEADER);
                bundle_add_str(b, AUL_SVC_K_CAN_BE_LEADER, "true");
                bundle_del(b, AUL_SVC_K_REROUTE);
@@ -300,6 +320,23 @@ static int __run_svc_with_pkgname(char *pkgname, bundle *b, int request_code,
                bundle_add_str(b, AUL_SVC_K_RECYCLE, "true");
        }
 
+       launch_mode = aul_svc_get_launch_mode(b);
+       if (launch_mode && !strcmp(launch_mode, "group")) {
+               ret = bundle_get_type(b, AUL_K_INSTANCE_ID);
+               if (ret == BUNDLE_TYPE_NONE)
+                       __set_instance_id(b, *appid);
+       }
+}
+
+static int __run_svc_with_pkgname(char *pkgname, bundle *b, int request_code,
+               aul_svc_res_fn cbfunc, aul_svc_err_cb err_cb,
+               void *data, uid_t uid, bool sync)
+{
+       aul_svc_cb_info_t *cb_info = NULL;
+       int ret = -1;
+
+       __verify_request(b, &pkgname);
+
        if (cbfunc || err_cb) {
                SECURE_LOGD("pkg_name : %s - with result", pkgname);
 
@@ -1238,7 +1275,6 @@ static int __get_appid(bundle *b, uid_t uid, char **appid)
 
        *appid = info.appid;
 
-
        return AUL_SVC_RET_OK;
 }
 
@@ -2019,7 +2055,7 @@ API int aul_svc_send_launch_request_for_uid(bundle *b, int request_code,
 API int aul_svc_send_launch_request_sync_for_uid(bundle *b, int request_code,
                bundle **res_b, aul_svc_result_val *res, uid_t uid)
 {
-       const char *pkgname = NULL;
+       char *pkgname = NULL;
        const char *val;
        char *appid = NULL;
        int ret;
@@ -2031,22 +2067,7 @@ API int aul_svc_send_launch_request_sync_for_uid(bundle *b, int request_code,
        }
 
        pkgname = appid;
-       if (bundle_get_type(b, AUL_SVC_K_SELECTOR_EXTRA_LIST) != BUNDLE_TYPE_NONE) {
-               if (!aul_svc_get_pkgname(b))
-                       pkgname = APP_SELECTOR;
-       }
-
-       if (bundle_get_val(b, AUL_K_FORCE_LAUNCH_APP_SELECTOR))
-               pkgname = APP_SELECTOR;
-
-       if (__is_special_app(pkgname) || __is_special_operation(b)) {
-               bundle_del(b, AUL_SVC_K_CAN_BE_LEADER);
-               bundle_add_str(b, AUL_SVC_K_CAN_BE_LEADER, "true");
-               bundle_del(b, AUL_SVC_K_REROUTE);
-               bundle_add_str(b, AUL_SVC_K_REROUTE, "true");
-               bundle_del(b, AUL_SVC_K_RECYCLE);
-               bundle_add_str(b, AUL_SVC_K_RECYCLE, "true");
-       }
+       __verify_request(b, &pkgname);
 
        ret = aul_send_launch_request_sync_for_uid(pkgname, b, uid, res_b);
        if (ret > 0) {
@@ -2263,3 +2284,47 @@ API const char *aul_svc_get_comp_id(bundle *b)
 {
        return bundle_get_val(b, AUL_K_COMPONENT_ID);
 }
+
+static void __foreach_group_info(aul_app_group_info_h info, void *data)
+{
+       int *cnt = (int *)data;
+
+       (*cnt)++;
+}
+
+API int aul_svc_subapp_terminate_request(bundle *b, int pid)
+{
+       const char *caller_inst_id;
+       const char *inst_id;
+       char buf[512] = { 0, };
+       int cnt = 0;
+       int ret;
+
+       if (!b || pid < 0) {
+               _E("Invalid parameter");
+               return AUL_SVC_RET_EINVAL;
+       }
+
+       inst_id = bundle_get_val(b, AUL_K_INSTANCE_ID);
+       if (!inst_id) {
+               _E("Invalid request");
+               return AUL_SVC_RET_EINVAL;
+       }
+
+       caller_inst_id = bundle_get_val(b, AUL_K_CALLER_INSTANCE_ID);
+       if (!caller_inst_id) {
+               ret = aul_app_get_instance_id_bypid(getpid(), buf, sizeof(buf));
+               if (ret != AUL_R_OK) {
+                       _E("Failed to get caller instance ID");
+                       return AUL_SVC_RET_ERROR;
+               }
+               caller_inst_id = buf;
+       }
+
+       aul_app_group_foreach_group_info(caller_inst_id,
+                       __foreach_group_info, (void *)&cnt);
+       if (cnt == 0)
+               return aul_subapp_terminate_request(inst_id, pid);
+
+       return aul_app_group_clear_top();
+}