Bump up efl module version.
[platform/core/appfw/launchpad.git] / src / launchpad_lib.c
index 42c8b28..4041141 100644 (file)
 #include <sys/types.h>
 #include <sys/prctl.h>
 #include <malloc.h>
-#ifdef _APPFW_FEATURE_LOADER_PRIORITY
-#include <sys/time.h>
-#include <sys/resource.h>
-#endif
 #include <bundle_internal.h>
 #include <aul.h>
 #include <security-manager.h>
+#include <trust-anchor.h>
+#include <buxton2.h>
+#include <vconf.h>
 
 #include "launchpad_common.h"
 #include "launchpad.h"
@@ -39,7 +38,6 @@
 #endif
 
 #define AUL_PR_NAME 16
-#define LOWEST_PRIO 20
 
 static loader_lifecycle_callback_s *__loader_callbacks;
 static loader_adapter_s *__loader_adapter;
@@ -57,24 +55,59 @@ static void __at_exit_to_release_bundle(void)
 }
 
 static int __prepare_exec(const char *appid, const char *app_path,
-                       const char *pkg_type, int type)
+                       const char *pkg_type, int type, const char* pkgid,
+                       bool global, bundle *kb)
 {
        const char *file_name = NULL;
        char process_name[AUL_PR_NAME] = { 0, };
        int ret;
+       struct buxton_client *bxt_cli;
 
        __preexec_run(pkg_type, appid, app_path);
 
+       ret = _enable_external_pkg(kb, pkgid, global ? GLOBAL_USER : getuid());
+       if (ret < 0)
+               return -1;
+
        /* SET PRIVILEGES*/
        SECURE_LOGD("[candidata] appid : %s / pkg_type : %s / app_path : %s",
                appid, pkg_type, app_path);
+
+       if (global)
+               ret = trust_anchor_launch(pkgid, GLOBAL_USER);
+       else
+               ret = trust_anchor_launch(pkgid, getuid());
+
+       if (ret != TRUST_ANCHOR_ERROR_NONE &&
+                       ret != TRUST_ANCHOR_ERROR_NOT_INSTALLED) {
+               _E("trust_anchor_launch() returns %d", ret);
+               return -1;
+       }
+
+       _W("security_manager_prepare_app ++");
        ret = security_manager_prepare_app(appid);
+       _W("security_manager_prepare_app --");
        if (ret != SECURITY_MANAGER_SUCCESS) {
-               _D("fail to set privileges - " \
-                               "check your package's credential: %d\n", ret);
+               _E("Failed to set privileges %s:%d", appid, ret);
                return -1;
        }
 
+       _send_cmd_to_amd(APP_STARTUP_SIGNAL);
+       _setup_stdio(basename(app_path));
+
+       ret = buxton_open(&bxt_cli, NULL, NULL);
+       if (ret != 0) {
+               _E("buxton_open() failed, errno(%d)", errno);
+               return -1;
+       }
+       ret = buxton_update_client_label_sync(bxt_cli);
+       if (ret != 0) {
+               _E("buxton_update_client_label() failed, errno(%d)", errno);
+               buxton_close(bxt_cli);
+               return -1;
+       }
+       buxton_close(bxt_cli);
+
        /*
         * SET DUMPABLE - for coredump
         * This dumpable flag should be set after
@@ -104,32 +137,31 @@ static int __prepare_exec(const char *appid, const char *app_path,
        snprintf(process_name, AUL_PR_NAME, "%s", file_name);
        prctl(PR_SET_NAME, process_name);
 
+       ret = _wait_tep_mount(kb);
+       if (ret < 0)
+               return -1;
+
+       ret = _prepare_app_socket();
+       if (ret < 0)
+               return -1;
+
+       ret = _prepare_id_file();
+       if (ret < 0)
+               return -1;
+
        return 0;
 }
 
 static int __default_launch_cb(bundle *kb, const char *appid,
                const char *app_path, const char *root_path,
-               const char *pkgid, const char *pkg_type, int loader_type)
+               const char *pkgid, const char *pkg_type, int loader_type, bool global)
 {
        char err_str[MAX_LOCAL_BUFSZ] = { 0, };
-#ifdef _APPFW_FEATURE_PRIORITY_CHANGE
-       int res;
-       const char *high_priority = bundle_get_val(kb, AUL_K_HIGHPRIORITY);
-
-       _D("high_priority: %s", high_priority);
-       if (strncmp(high_priority, "true", 4) == 0) {
-               res = setpriority(PRIO_PROCESS, 0, -10);
-               if (res == -1) {
-                       SECURE_LOGE("Setting process (%d) priority " \
-                               "to -10 failed, errno: %d (%s)",
-                               getpid(), errno,
-                               strerror_r(errno, err_str, sizeof(err_str)));
-               }
-       }
-       bundle_del(kb, AUL_K_HIGHPRIORITY);
-#endif
+       int r;
 
-       if (__prepare_exec(appid, app_path, pkg_type, loader_type) < 0) {
+       r = __prepare_exec(appid, app_path, pkg_type, loader_type, pkgid,
+                       global, kb);
+       if (r < 0) {
                _E("__candidate_process_prepare_exec() failed");
                if (access(app_path, F_OK | R_OK)) {
                        SECURE_LOGE("access() failed for file: \"%s\", " \
@@ -204,11 +236,14 @@ static int __candidate_process_launchpad_main_loop(app_pkt_t *pkt,
 
        SECURE_LOGD("pkg id: %s", menu_info->pkgid);
 
+       /* Set environments */
+       _set_env(menu_info, kb);
+
        tmp_argv = _create_argc_argv(kb, &tmp_argc);
 
        __default_launch_cb(kb, menu_info->appid, app_path,
                        menu_info->root_path, menu_info->pkgid,
-                       menu_info->pkg_type, type);
+                       menu_info->pkg_type, type, menu_info->global);
 
        if (__loader_callbacks->launch) {
                ret = __loader_callbacks->launch(tmp_argc, tmp_argv, app_path,
@@ -216,9 +251,6 @@ static int __candidate_process_launchpad_main_loop(app_pkt_t *pkt,
                                menu_info->pkg_type, __loader_user_data);
        }
 
-       /* SET ENVIROMENT*/
-       _set_env(menu_info, kb);
-
        if (out_app_path != NULL && out_argc != NULL && out_argv != NULL) {
                memset(out_app_path, '\0', strlen(out_app_path));
                snprintf(out_app_path, LOADER_ARG_LEN, "%s", app_path);
@@ -249,25 +281,14 @@ static int __candidate_process_launchpad_main_loop(app_pkt_t *pkt,
 static void __receiver_cb(int fd)
 {
        int ret = -1;
-       int recv_ret;
        app_pkt_t *pkt;
 
        _D("[candidate] ECORE_FD_READ");
-       pkt = (app_pkt_t *)malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
+       pkt = _recv_pkt_raw(fd);
        if (!pkt) {
-               _D("[candidate] out of memory1");
+               _D("[candidate] _recv_pkt_raw error!");
                exit(-1);
        }
-       memset(pkt, 0, AUL_SOCK_MAXBUFF);
-
-       recv_ret = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
-       if (recv_ret == -1) {
-               _D("[condidate] recv error!");
-               close(fd);
-               free(pkt);
-               exit(-1);
-       }
-       _D("[candidate] recv_ret: %d, pkt->len: %d", recv_ret, pkt->len);
 
        __loader_adapter->remove_fd(__loader_user_data, fd);
        close(fd);
@@ -284,21 +305,23 @@ static void __receiver_cb(int fd)
        }
 }
 
+static void __update_lang(keynode_t *node, void *user_data)
+{
+       char *lang = vconf_keynode_get_str(node);
+
+       if (lang)
+               setenv("LANG", lang, 1);
+}
+
 static int __before_loop(int argc, char **argv)
 {
        int client_fd;
        int ret = -1;
        bundle *extra = NULL;
-#ifdef _APPFW_FEATURE_LOADER_PRIORITY
-       char err_str[MAX_LOCAL_BUFSZ] = { 0, };
-       int res = setpriority(PRIO_PROCESS, 0, LOWEST_PRIO);
 
-       if (res == -1) {
-               SECURE_LOGE("Setting process (%d) priority to %d failed, " \
-                               "errno: %d (%s)", getpid(), LOWEST_PRIO, errno,
-                               strerror_r(errno, err_str, sizeof(err_str)));
-       }
-#endif
+       if (_verify_proc_caps() < 0)
+               return -1;
+
        __preexec_init(argc, argv);
 
        /* Set new session ID & new process group ID*/
@@ -325,14 +348,7 @@ static int __before_loop(int argc, char **argv)
                bundle_free(extra);
 
        malloc_trim(0);
-#ifdef _APPFW_FEATURE_LOADER_PRIORITY
-       res = setpriority(PRIO_PGRP, 0, 0);
-       if (res == -1) {
-               SECURE_LOGE("Setting process (%d) priority to 0 failed, " \
-                               "errno: %d (%s)", getpid(), errno,
-                               strerror_r(errno, err_str, sizeof(err_str)));
-       }
-#endif
+
        client_fd = _connect_to_launchpad(__loader_type, __loader_id);
        if (client_fd == -1) {
                _D("Connecting to candidate process was failed.");
@@ -341,11 +357,16 @@ static int __before_loop(int argc, char **argv)
 
        __loader_adapter->add_fd(__loader_user_data, client_fd, __receiver_cb);
 
+       if (vconf_notify_key_changed(VCONFKEY_LANGSET, __update_lang, NULL) != 0)
+               _E("vconf_notify_key_changed failed");
+
        return ret;
 }
 
 static int __after_loop(void)
 {
+       vconf_ignore_key_changed(VCONFKEY_LANGSET, __update_lang);
+
        if (__loader_callbacks->terminate) {
                return __loader_callbacks->terminate(__argc, __argv,
                                __loader_user_data);
@@ -398,8 +419,10 @@ API int launchpad_loader_main(int argc, char **argv,
        __argc = argc;
        __argv = argv;
 
-       if (__before_loop(argc, argv) != 0)
+       if (__before_loop(argc, argv) != 0) {
+               _E("Failed to prepare running loader. type(%d)", __loader_type);
                return -1;
+       }
 
        _D("[candidate] ecore main loop begin");
        __loader_adapter->loop_begin(__loader_user_data);
@@ -407,3 +430,7 @@ API int launchpad_loader_main(int argc, char **argv,
        return __after_loop();
 }
 
+API int launchpad_loader_set_priority(int prio)
+{
+       return _set_priority(prio);
+}