[FIX] kill app before start profiling
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Tue, 29 Oct 2013 10:12:35 +0000 (14:12 +0400)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Thu, 31 Oct 2013 09:48:22 +0000 (13:48 +0400)
Change-Id: Icc82cb2194833ee61ee95a8e427dd26f79ab9976
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/da_protocol.c
daemon/daemon.c
daemon/utils.c
daemon/utils.h

index 8620b78..b76fbf0 100644 (file)
@@ -1014,6 +1014,11 @@ static int process_msg_start(struct msg_buf_t *msg_control)
                goto send_ack;
        }
 
+       if (prepare_profiling() != 0) {
+               LOGE("failed to prepare profiling\n");
+               goto send_ack;
+       }
+
        if (ioctl_send_msg(msg_reply) != 0) {
                LOGE("cannot send message to device\n");
                goto send_ack;
index 975376a..44287c9 100644 (file)
@@ -294,6 +294,35 @@ static int stop_app_launch_timer()
        return 0;
 }
 
+int kill_app_by_info(const struct app_info_t *app_info)
+{
+       int res = 0;
+
+       if (app_info == NULL) {
+               LOGE("Cannot exec app. app_info is NULL");
+               return -1;
+       }
+
+       switch (app_info->app_type) {
+       case APP_TYPE_TIZEN:
+               res = kill_app(app_info->exe_path);
+               break;
+       case APP_TYPE_RUNNING:
+               // TODO: nothing, it's running
+               LOGI("already started\n");
+               break;
+       case APP_TYPE_COMMON:
+               res = kill_app(app_info->exe_path);
+               break;
+       default:
+               LOGE("Unknown app type %d\n", app_info->app_type);
+               res = -1;
+               break;
+       }
+
+       return res;
+}
+
 static int exec_app(const struct app_info_t *app_info)
 {
        int res = 0;
@@ -305,7 +334,6 @@ static int exec_app(const struct app_info_t *app_info)
 
        switch (app_info->app_type) {
        case APP_TYPE_TIZEN:
-               kill_app(app_info->exe_path);
                if (exec_app_tizen(app_info->app_id, app_info->exe_path)) {
                        LOGE("Cannot exec tizen app %s\n", app_info->app_id);
                        res = -1;
@@ -316,7 +344,6 @@ static int exec_app(const struct app_info_t *app_info)
                LOGI("already started\n");
                break;
        case APP_TYPE_COMMON:
-               kill_app(app_info->exe_path);
                if (exec_app_common(app_info->exe_path)) {
                        LOGE("Cannot exec common app %s\n", app_info->exe_path);
                        res = -1;
@@ -379,6 +406,30 @@ int launch_timer_start()
 static void epoll_add_input_events();
 static void epoll_del_input_events();
 
+int prepare_profiling()
+{
+       struct app_list_t *app = NULL;
+       const struct app_info_t *app_info = NULL;
+
+       app_info = app_info_get_first(&app);
+       if (app_info == NULL) {
+               LOGE("No app info found\n");
+               return -1;
+       }
+
+       //all apps
+       while (app_info != NULL) {
+               if (kill_app_by_info(app_info) != 0) {
+                       LOGE("kill app failed\n");
+                       return -1;
+               }
+               app_info = app_info_get_next(&app);
+       }
+
+       return 0;
+
+}
+
 int start_profiling()
 {
        struct app_list_t *app = NULL;
index f7461aa..c5d0b05 100644 (file)
@@ -34,6 +34,7 @@
 #include <string.h>
 #include <errno.h>
 
+#include <signal.h>                    // for signal
 #include <pthread.h>
 #include <unistd.h>            // for unlink
 #include <dirent.h>            // for opendir, readdir
@@ -375,17 +376,17 @@ int exec_app_tizen(const char *app_id, const char *exec_path)
 
 int exec_app_common(const char* exec_path)
 {
-       LOGI("kill %s\n", exec_path);
-
        pid_t pid;
        int hw_acc = 0;
        char manifest[PATH_MAX];
        char command[PATH_MAX];
+
+       LOGI("exec %s\n", exec_path);
 #ifndef LOCALTEST
        // TODO: check if this makes sense
        /* char appid[SMACK_LABEL_LEN]; */
 #endif
-       if (exec_path == NULL || !strlen(exec_path))  {
+       if (exec_path == NULL || !strlen(exec_path)) {
                LOGE("Executable path is not correct\n");
                return 1;
        }
@@ -475,52 +476,89 @@ int exec_app_common(const char* exec_path)
 // find process id from executable binary path
 pid_t find_pid_from_path(const char* path)
 {
-       pid_t status = -1;
+       pid_t status = 0;
 
-       char buffer [BUFFER_MAX];
-       char command [BUFFER_MAX];
+       char buffer[BUFFER_MAX] = {0};
+       char command[BUFFER_MAX] = {0};
 
        sprintf(command, "/bin/pidof %s", path);
+       LOGI("look for <%s>\n", path);
 
        FILE *fp = popen(command, "r");
        if (!fp)
-       {
-               LOGW("Getting pidof %s is failed\n", path);
                return status;
-       }
 
        while (fgets(buffer, BUFFER_MAX, fp) != NULL)
-       {
                LOGI("result of 'pidof' is %s\n", buffer);
-       }
 
        pclose(fp);
 
-       if (strlen(buffer) > 0)
-       {
-               if (sscanf(buffer,"%d\n", &status) != 1)
-               {
+       if (strlen(buffer) > 0) {
+               if (sscanf(buffer, "%d\n", &status) != 1) {
                        LOGW("Failed to read result buffer of 'pidof',"
-                                " status(%d) with cmd '%s'\n", status, command);
-                       return -1;
+                            " status(%d) with cmd '%s'\n", status, command);
+                       return 0;
                }
        }
 
        return status;
 }
 
-void kill_app(const char* binary_path)
+pid_t get_pid_by_path(const char *binary_path)
 {
-       LOGI("kill %s\n", binary_path);
-
        pid_t pkg_pid;
-       char command[PATH_MAX];
+       int len;
+       char *real_path;
+       static const char exe_line[] = ".exe";
+
 
        pkg_pid = find_pid_from_path(binary_path);
-       if (pkg_pid > 0) {
-               sprintf(command, "kill -9 %d", pkg_pid);
-               system(command);
+       if (pkg_pid == 0) {
+               len = strlen(binary_path);
+               real_path = malloc(len + sizeof(exe_line));
+               if (real_path == NULL) {
+                       LOGE("cannot alloc memory\n");
+                       return -1;
+               }
+               memcpy(real_path, binary_path, len + 1);
+
+               // try remove or add ".exe" and get pid
+               if (strcmp(real_path + len - (sizeof(exe_line) - 1), exe_line) == 0)
+                       // remove .exe from tPath
+                       real_path[len - (sizeof(exe_line) - 1)] = '\0';
+               else
+                       // add .exe
+                       memcpy(&real_path[len], exe_line, sizeof(exe_line));
+
+               pkg_pid = find_pid_from_path(real_path);
+               free(real_path);
        }
+
+       return pkg_pid;
+}
+
+int kill_app(const char *binary_path)
+{
+       pid_t pkg_pid;
+
+       LOGI("kill %s (%d)\n", binary_path, SIGKILL);
+       pkg_pid = get_pid_by_path(binary_path);
+       if (pkg_pid != 0) {
+               if (kill(pkg_pid, SIGTERM) == -1) {
+                       LOGE("cannot kill %d -%d err<%s>\n", pkg_pid, SIGKILL,
+                            strerror(errno));
+                       return -1;
+               } else {
+                       // we need sleep up there because kill() function
+                       // returns control immediately after send signal
+                       // without it app_launch returns err on start app
+                       sleep(1);
+                       LOGI("killed %d -%d\n", pkg_pid, SIGKILL);
+               }
+       } else
+               LOGI("cannot kill <%s>; process not found\n", binary_path);
+
+       return 0;
 }
 
 int get_executable(char* appPath, char* buf, int buflen)
index a84f0cc..fec50d7 100644 (file)
@@ -61,7 +61,7 @@ int remove_indir(const char *dirname);
 
 char* get_app_name(char* binary_path);
 
-void kill_app(const char* binary_path);
+int kill_app(const char *binary_path);
 
 pid_t find_pid_from_path(const char* path);