Return kill/check result directly instead of writing on tmp file 95/91895/2 accepted/tizen/common/20161012.154713 accepted/tizen/ivi/20161014.022203 accepted/tizen/mobile/20161014.022230 accepted/tizen/tv/20161014.022144 accepted/tizen/wearable/20161014.022129 submit/tizen/20161012.064511
authorSangyoon Jang <s89.jang@samsung.com>
Wed, 12 Oct 2016 06:25:08 +0000 (15:25 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Wed, 12 Oct 2016 06:30:04 +0000 (15:30 +0900)
Submit together:
 - https://review.tizen.org/gerrit/91895
 - https://review.tizen.org/gerrit/91896

Change-Id: I6d32d1f8fe93a8cbfb13429f34560024ac074549
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
src/pkgmgr-server.c
src/request.c

index 2abd40f..0986995 100644 (file)
@@ -30,7 +30,6 @@
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <fcntl.h>
 #include <signal.h>
 #include <grp.h>
 
@@ -73,6 +72,7 @@ typedef struct  {
 typedef struct {
        uid_t uid;
        char *cmd;
+       int pid;
 } pkgcmd_data;
 
 /*
@@ -319,48 +319,6 @@ static int __check_csr(backend_info *ptr)
 }
 #endif
 
-static void __make_pid_info_file(char *req_key, int pid, uid_t uid)
-{
-       FILE* file;
-       int fd;
-       int ret;
-       char buf[MAX_PKG_TYPE_LEN] = {0};
-       char info_file[PATH_MAX] = {'\0'};
-       struct passwd pwd;
-       struct passwd *pwd_result;
-
-       if (req_key == NULL)
-               return;
-
-       ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &pwd_result);
-       if (ret != 0 || pwd_result == NULL) {
-               ERR("get uid failed(%d) for user(%d)", ret, uid);
-               return;
-       }
-       DBG("uid(%d), gid(%d)", uid, pwd.pw_gid);
-
-       snprintf(info_file, PATH_MAX, "/tmp/pkgmgr/%s", req_key);
-
-       DBG("info_path(%s)", info_file);
-       file = fopen(info_file, "w");
-       if (file == NULL) {
-               ERR("Couldn't open the file(%s)", info_file);
-               return;
-       }
-
-       snprintf(buf, MAX_PKG_TYPE_LEN, "%d\n", pid);
-       fwrite(buf, 1, strlen(buf), file);
-
-       fflush(file);
-       fd = fileno(file);
-       if (fchmod(fd, 0777) < 0)
-               ERR("chmod failed, errno(%d)", errno);
-       if (fchown(fd, uid, pwd.pw_gid) < 0)
-               ERR("chown failed, errno(%d)", errno);
-       fsync(fd);
-       fclose(file);
-}
-
 static int __kill_app(char *appid, uid_t uid)
 {
        int ret;
@@ -405,7 +363,6 @@ static int __pkgcmd_app_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
        char *appid;
        char *exec;
        int ret;
-       int pid = -1;
        pkgcmd_data *pdata = (pkgcmd_data *)user_data;
 
        if (handle == NULL) {
@@ -429,15 +386,13 @@ static int __pkgcmd_app_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
        }
 
        if (strcmp(pdata->cmd, "kill") == 0) {
-               pid = __check_app(appid, pdata->uid);
-               if (pid > 0)
+               pdata->pid = __check_app(appid, pdata->uid);
+               if (pdata->pid > 0)
                        ret = __kill_app(appid, pdata->uid);
        } else if (strcmp(pdata->cmd, "check") == 0) {
-               pid = __check_app(appid, pdata->uid);
+               pdata->pid = __check_app(appid, pdata->uid);
        }
 
-       __make_pid_info_file(pkgid, pid, pdata->uid);
-
        return 0;
 }
 
@@ -1076,33 +1031,41 @@ static int __process_kill(pm_dbus_msg *item)
 {
        int ret;
        pkgmgrinfo_pkginfo_h handle;
-       pkgcmd_data *pdata = NULL;
+       pkgcmd_data *pdata;
 
        ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(item->pkgid, item->uid,
                        &handle);
        if (ret < 0) {
                ERR("Failed to get handle");
+               __return_value_to_caller(item->req_id,
+                               g_variant_new("(ii)", PKGMGR_R_ERROR, 0));
                return -1;
        }
 
        pdata = calloc(1, sizeof(pkgcmd_data));
        if (pdata == NULL) {
                ERR("memory alloc failed");
+               __return_value_to_caller(item->req_id,
+                               g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
                return -1;
        }
        pdata->cmd = strdup("kill");
        if (pdata->cmd == NULL) {
                ERR("out of memory");
+               __return_value_to_caller(item->req_id,
+                               g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
                free(pdata);
-               pdata = NULL;
                return -1;
        }
        pdata->uid = item->uid;
        ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP,
                        __pkgcmd_app_cb, pdata, item->uid);
+
+       __return_value_to_caller(item->req_id,
+                       g_variant_new("(ii)", PKGMGR_R_OK, pdata->pid));
+
        free(pdata->cmd);
        free(pdata);
-       pdata = NULL;
        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
        if (ret < 0) {
                ERR("pkgmgrinfo_appinfo_get_list() failed");
@@ -1116,33 +1079,41 @@ static int __process_check(pm_dbus_msg *item)
 {
        int ret;
        pkgmgrinfo_pkginfo_h handle;
-       pkgcmd_data *pdata = NULL;
+       pkgcmd_data *pdata;
 
        ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(item->pkgid, item->uid,
                        &handle);
        if (ret < 0) {
                ERR("Failed to get handle");
+               __return_value_to_caller(item->req_id,
+                               g_variant_new("(ii)", PKGMGR_R_ERROR, 0));
                return -1;
        }
 
        pdata = calloc(1, sizeof(pkgcmd_data));
        if (pdata == NULL) {
                ERR("memory alloc failed");
+               __return_value_to_caller(item->req_id,
+                               g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
                return -1;
        }
        pdata->cmd = strdup("check");
        if (pdata->cmd == NULL) {
                ERR("out of memory");
+               __return_value_to_caller(item->req_id,
+                               g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
                free(pdata);
-               pdata = NULL;
                return -1;
        }
        pdata->uid = item->uid;
        ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP,
                        __pkgcmd_app_cb, pdata, item->uid);
+
+       __return_value_to_caller(item->req_id,
+                       g_variant_new("(ii)", PKGMGR_R_OK, pdata->pid));
+
        free(pdata->cmd);
        free(pdata);
-       pdata = NULL;
        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
        if (ret < 0) {
                ERR("pkgmgrinfo_appinfo_get_list() failed");
index d4d049f..e269030 100644 (file)
@@ -113,11 +113,13 @@ static const char instropection_xml[] =
        "      <arg type='u' name='uid' direction='in'/>"
        "      <arg type='s' name='pkgid' direction='in'/>"
        "      <arg type='i' name='ret' direction='out'/>"
+       "      <arg type='i' name='pid' direction='out'/>"
        "    </method>"
        "    <method name='check'>"
        "      <arg type='u' name='uid' direction='in'/>"
        "      <arg type='s' name='pkgid' direction='in'/>"
        "      <arg type='i' name='ret' direction='out'/>"
+       "      <arg type='i' name='pid' direction='out'/>"
        "    </method>"
        "    <method name='generate_license_request'>"
        "      <arg type='s' name='resp_data' direction='in'/>"
@@ -886,23 +888,32 @@ static int __handle_request_kill(uid_t uid,
 {
        uid_t target_uid = (uid_t)-1;
        char *pkgid = NULL;
+       char *reqkey = NULL;
 
        g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
        if (target_uid == (uid_t)-1 || pkgid == NULL) {
                g_dbus_method_invocation_return_value(invocation,
-                               g_variant_new("(i)", PKGMGR_R_ECOMM));
+                               g_variant_new("(ii)", PKGMGR_R_ECOMM, 0));
                return -1;
        }
 
-       if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_KILL, "default",
-                               pkgid, "")) {
+       reqkey = __generate_reqkey(pkgid);
+       if (reqkey == NULL) {
                g_dbus_method_invocation_return_value(invocation,
-                               g_variant_new("(i)", PKGMGR_R_ESYSTEM));
+                               g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
                return -1;
        }
 
-       g_dbus_method_invocation_return_value(invocation,
-                       g_variant_new("(i)", PKGMGR_R_OK));
+       if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_KILL,
+                               "default", pkgid, "")) {
+               g_dbus_method_invocation_return_value(invocation,
+                               g_variant_new("(ii)", PKGMGR_R_ESYSTEM, 0));
+               return -1;
+       }
+
+       if (!g_hash_table_insert(req_table, (gpointer)reqkey,
+                               (gpointer)invocation))
+               ERR("reqkey already exists");
 
        return 0;
 }
@@ -912,23 +923,32 @@ static int __handle_request_check(uid_t uid,
 {
        uid_t target_uid = (uid_t)-1;
        char *pkgid = NULL;
+       char *reqkey = NULL;
 
        g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
        if (target_uid == (uid_t)-1 || pkgid == NULL) {
                g_dbus_method_invocation_return_value(invocation,
-                               g_variant_new("(i)", PKGMGR_R_ECOMM));
+                               g_variant_new("(ii)", PKGMGR_R_ECOMM, 0));
                return -1;
        }
 
-       if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_CHECK, "default",
-                               pkgid, "")) {
+       reqkey = __generate_reqkey(pkgid);
+       if (reqkey == NULL) {
                g_dbus_method_invocation_return_value(invocation,
-                               g_variant_new("(i)", PKGMGR_R_ESYSTEM));
+                               g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
                return -1;
        }
 
-       g_dbus_method_invocation_return_value(invocation,
-                       g_variant_new("(i)", PKGMGR_R_OK));
+       if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_CHECK,
+                               "default", pkgid, "")) {
+               g_dbus_method_invocation_return_value(invocation,
+                               g_variant_new("(ii)", PKGMGR_R_ESYSTEM, 0));
+               return -1;
+       }
+
+       if (!g_hash_table_insert(req_table, (gpointer)reqkey,
+                               (gpointer)invocation))
+               ERR("reqkey already exists");
 
        return 0;
 }