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;
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;
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;
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;
#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
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;
}
// 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)