Kill app using aul api instead of proc filesystem 77/91877/4
authorSangyoon Jang <s89.jang@samsung.com>
Wed, 12 Oct 2016 04:52:00 +0000 (13:52 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Wed, 12 Oct 2016 05:04:25 +0000 (14:04 +0900)
Change-Id: Ic48077821e5c81b378510deba30ecf0741b5e310
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
CMakeLists.txt
packaging/pkgmgr-server.spec
src/pkgmgr-server.c

index 3abb01a..3c76497 100644 (file)
@@ -33,7 +33,7 @@ SET(SRCS
        src/signal.c
        )
 
-SET(SERVER_CHECK_MODULES gio-2.0 dlog pkgmgr-parser pkgmgr-info libtzplatform-config drm-service-core-tizen libgum sqlite3 pkgmgr pkgmgr-installer libsystemd)
+SET(SERVER_CHECK_MODULES gio-2.0 dlog pkgmgr-parser pkgmgr-info libtzplatform-config drm-service-core-tizen libgum sqlite3 pkgmgr pkgmgr-installer libsystemd aul)
 IF(TIZEN_FEATURE_CSR)
        SET(SERVER_CHECK_MODULES "${SERVER_CHECK_MODULES} csr")
 ENDIF(TIZEN_FEATURE_CSR)
index dbb360f..ef78063 100644 (file)
@@ -13,6 +13,7 @@ BuildRequires:  pkgconfig(dbus-glib-1)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(aul)
 BuildRequires:  pkgconfig(bundle)
 BuildRequires:  pkgconfig(pkgmgr-info)
 BuildRequires:  pkgconfig(iniparser)
index 764894a..2abd40f 100644 (file)
@@ -29,7 +29,6 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
-#include <dirent.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -38,6 +37,7 @@
 #include <glib.h>
 #include <gio/gio.h>
 
+#include <aul.h>
 #include <pkgmgr-info.h>
 #include <pkgmgr/pkgmgr_parser_db.h>
 #include <tzplatform_config.h>
@@ -291,44 +291,6 @@ gboolean exit_server(void *data)
        return TRUE;
 }
 
-static int __pkgcmd_read_proc(const char *path, char *buf, int size)
-{
-       int fd;
-       int ret;
-       if (buf == NULL || path == NULL)
-               return -1;
-       fd = open(path, O_RDONLY);
-       if (fd < 0)
-               return -1;
-       ret = read(fd, buf, size - 1);
-       if (ret <= 0) {
-               close(fd);
-               return -1;
-       } else
-               buf[ret] = 0;
-       close(fd);
-       return ret;
-}
-
-static int __pkgcmd_find_pid_by_cmdline(const char *dname,
-                       const char *cmdline, const char *apppath)
-{
-       int pid = 0;
-       int pgid = 0;
-
-       if (strcmp(cmdline, apppath) == 0) {
-               pid = atoi(dname);
-               pgid = getpgid(pid);
-               if (pgid < 0) {
-                       ERR("getpgid failed, errno(%d)", errno);
-                       pid = 0;
-               }
-               if (pid != pgid)
-                       pid = 0;
-       }
-       return pid;
-}
-
 #ifdef TIZEN_FEATURE_CSR
 static int __check_csr(backend_info *ptr)
 {
@@ -357,55 +319,6 @@ static int __check_csr(backend_info *ptr)
 }
 #endif
 
-static int __pkgcmd_proc_iter_kill_cmdline(const char *apppath, int option)
-{
-       DIR *dp;
-       struct dirent dentry, *result;
-       int pid;
-       int ret;
-       char buf[1024] = {'\0'};
-       int pgid;
-
-       dp = opendir("/proc");
-       if (dp == NULL)
-               return -1;
-
-       for (ret = readdir_r(dp, &dentry, &result);
-                       ret == 0 && result != NULL;
-                       ret = readdir_r(dp, &dentry, &result)) {
-               if (!isdigit(dentry.d_name[0]))
-                       continue;
-
-               snprintf(buf, sizeof(buf), "/proc/%s/cmdline", dentry.d_name);
-               ret = __pkgcmd_read_proc(buf, buf, sizeof(buf));
-               if (ret <= 0)
-                       continue;
-
-               pid = __pkgcmd_find_pid_by_cmdline(dentry.d_name, buf, apppath);
-               if (pid > 0) {
-                       if (option == 0) {
-                               closedir(dp);
-                               return pid;
-                       }
-                       pgid = getpgid(pid);
-                       if (pgid <= 1) {
-                               closedir(dp);
-                               ERR("getpgid failed, errno(%d)", errno);
-                               return -1;
-                       }
-                       if (killpg(pgid, SIGKILL) < 0) {
-                               closedir(dp);
-                               ERR("killpg failed, errno(%d)", errno);
-                               return -1;
-                       }
-                       closedir(dp);
-                       return pid;
-               }
-       }
-       closedir(dp);
-       return 0;
-}
-
 static void __make_pid_info_file(char *req_key, int pid, uid_t uid)
 {
        FILE* file;
@@ -450,31 +363,46 @@ static void __make_pid_info_file(char *req_key, int pid, uid_t uid)
 
 static int __kill_app(char *appid, uid_t uid)
 {
-       pkgmgrinfo_appinfo_h appinfo;
-       int ret = PMINFO_R_ERROR;
-       char *exec = NULL;
+       int ret;
+       int pid;
+       int is_running;
 
-       ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &appinfo);
-       if (ret != PMINFO_R_OK)
-               return PMINFO_R_ERROR;
+       is_running = aul_app_is_running_for_uid(appid, uid);
+       /* app is not running */
+       if (!is_running)
+               return 0;
 
-       ret = pkgmgrinfo_appinfo_get_exec(appinfo, &exec);
-       if (ret != PMINFO_R_OK) {
-               pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
-               return PMINFO_R_ERROR;
+       pid = aul_app_get_pid_for_uid(appid, uid);
+       if (pid == -1)
+               return -1;
+
+       ret = aul_terminate_pid_for_uid(pid, uid);
+       if (ret != AUL_R_OK) {
+               ERR("failed to terminate app(%d)", appid);
+               return -1;
        }
 
-       ret = __pkgcmd_proc_iter_kill_cmdline(exec, 1);
-       if (ret != PMINFO_R_OK)
-               DBG("failed to kill app[%s], exec[%s]", appid, exec);
+       return 0;
+}
 
-       pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
-       return ret;
+static int __check_app(char *appid, uid_t uid)
+{
+       int pid;
+       int is_running;
+
+       is_running = aul_app_is_running_for_uid(appid, uid);
+       if (!is_running)
+               return 0;
+
+       pid = aul_app_get_pid_for_uid(appid, uid);
+
+       return pid;
 }
 
 static int __pkgcmd_app_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
 {
        char *pkgid;
+       char *appid;
        char *exec;
        int ret;
        int pid = -1;
@@ -489,16 +417,24 @@ static int __pkgcmd_app_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
                perror("Failed to get app exec path\n");
                exit(1);
        }
+       ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
+       if (ret) {
+               perror("Failed to get appid\n");
+               exit(1);
+       }
        ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
        if (ret) {
                perror("Failed to get pkgid\n");
                exit(1);
        }
 
-       if (strcmp(pdata->cmd, "kill") == 0)
-               pid = __pkgcmd_proc_iter_kill_cmdline(exec, 1);
-       else if (strcmp(pdata->cmd, "check") == 0)
-               pid = __pkgcmd_proc_iter_kill_cmdline(exec, 0);
+       if (strcmp(pdata->cmd, "kill") == 0) {
+               pid = __check_app(appid, pdata->uid);
+               if (pid > 0)
+                       ret = __kill_app(appid, pdata->uid);
+       } else if (strcmp(pdata->cmd, "check") == 0) {
+               pid = __check_app(appid, pdata->uid);
+       }
 
        __make_pid_info_file(pkgid, pid, pdata->uid);
 
@@ -987,7 +923,7 @@ static int __process_disable_app(pm_dbus_msg *item)
                        PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR, item->req_type);
 
        ret = __kill_app(item->appid, item->uid);
-       if (ret != PMINFO_R_OK) {
+       if (ret != 0) {
                __send_app_signal(item->uid, item->req_id, item->pkgid, item->appid,
                                PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR,
                                item->req_type);