From: Alexey Chernobaev Date: Fri, 31 Aug 2018 17:16:36 +0000 (+0300) Subject: stop profctl if controlled application finished X-Git-Tag: submit/tizen/20180911.125435~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=52ce7a228ad1279e99c9bd227aab91d3fe2f12dd;p=sdk%2Ftools%2Fprofctl.git stop profctl if controlled application finished --- diff --git a/proc_stat.c b/proc_stat.c index bf98d8e..3a6a366 100644 --- a/proc_stat.c +++ b/proc_stat.c @@ -1,7 +1,6 @@ #define _GNU_SOURCE #include #include -#include #include #include #include @@ -12,6 +11,10 @@ #include "proc_stat.h" #include "logging.h" +extern volatile int global_stop; + +extern int is_running(int pid); + // TODO!! check errors? static void split(char *line, char **array, int n) { @@ -37,11 +40,6 @@ static void split(char *line, char **array, int n) } } -static int is_running(int pid) -{ - return kill(pid, 0) == 0; -} - struct write_proc_stat_context { FILE *sstat; FILE *cpuf; @@ -64,8 +62,6 @@ static void uninit_write_proc_stat_context(struct write_proc_stat_context *c) if (c->pstat) fclose(c->pstat); } -extern volatile int global_stop; - static void write_proc_stat(FILE *stat_file_out, int pid, int timeout_usec, int verbose) { struct write_proc_stat_context c; @@ -286,6 +282,9 @@ static void *outstat_thread(void *arg) { struct outstat_info *oi = (struct outstat_info*)arg; write_proc_stat(oi->file, oi->pid, oi->timeout_usec, oi->verbose); + if (oi->verbose) { + log_message("stat output finished"); + } free(oi); return NULL; } diff --git a/profctl.c b/profctl.c index f55b8f3..a9090b6 100644 --- a/profctl.c +++ b/profctl.c @@ -24,9 +24,9 @@ #include #include +#include #include #include -#include #include #include @@ -61,6 +61,11 @@ static FILE *stat_file_out = NULL; volatile int global_stop = 0; +int is_running(int pid) +{ + return kill(pid, 0) == 0; +} + static struct option long_options[] = { {"appid", required_argument, 0, 'a'}, {"pipe", required_argument, 0, 'p'}, @@ -121,34 +126,6 @@ static void *data_output_thread(void *arg) return NULL; } -#if USE_AUL - -static void *wait_app_exited_thread(void *arg) -{ - intptr_t app_pid = (intptr_t)arg; - int app_runs = 1; -#if TIZEN - while (!global_stop && (app_runs = (aul_app_get_status_bypid(app_pid) >= 0))) { - usleep(500000); - } -#else - while (!global_stop && (app_runs = (kill(app_pid, 0) == 0))) { - usleep(500000); - } -#endif - if (verbose) { - if (!app_runs) { - log_message("controlled application finished (pid=%d)", app_pid); - } - if (!global_stop) { - log_message("stop flag set (wait_app_exited_thread)"); - } - } - global_stop = 1; -} - -#endif /* USE_AUL */ - // On Tizen, launch_app won't terminate until stdin, stdout and stderr are closed // TODO!! is needed? static void close_stdio() @@ -522,6 +499,7 @@ static void *command_loop_thread(void *arg) log_message("line: %s", line); } char *reply = line; // echo input line by default + char *buf = NULL; int pid; int signal = -1; if (is_command(line, "test", &pid, -1, NULL, 0) == 0) { @@ -541,12 +519,6 @@ static void *command_loop_thread(void *arg) #endif /* TIZEN */ } app_pid = pid; -#if USE_AUL - if (verbose) { - log_message("waiting for controlled application exit (pid=%d)", pid); - } - SimpleThread(&wait_app_exited_thread, (void*)(intptr_t)pid); -#endif /* USE_AUL */ if (doinfo) { if (stat_file_out == NULL) { stat_file_out = stdout; @@ -555,8 +527,11 @@ static void *command_loop_thread(void *arg) proc_stat_thread_runs = 1; } } - else { - reply = "!app_set_already"; // report error + else { // error + reply = "!app_set_already"; + if (asprintf(&buf, "%s (%s)", reply, line) != -1) { + reply = buf; + } } } else if (is_command(line, "exit", &pid, app_pid, NULL, 0) == 0) { @@ -578,11 +553,13 @@ static void *command_loop_thread(void *arg) signal = arg2; } } - char *buf = NULL; if (signal >= 0) { int result = send_signal(pid, signal); - if (asprintf(&buf, "%s : %d", reply, result) != -1) { - reply = buf; + if (result != 0) { // error + reply = "!kill_failed"; + if (asprintf(&buf, "%s (%s) : %d", reply, line, result) != -1) { + reply = buf; + } } } if (ctrl_file_out != NULL) { @@ -756,6 +733,8 @@ int main(int argc, char **argv) struct timespec poll_timeout; poll_timeout.tv_sec = 0; poll_timeout.tv_nsec = 250 * 1000000L; // 250 msec + + int last_app_pid = -1; while (!global_stop) { if (command_loop_thread_runs && pthread_timedjoin_np(command_loop_thread_id, NULL, &poll_timeout) == 0) @@ -770,6 +749,29 @@ int main(int argc, char **argv) { break; // data output thread finished } + if (global_stop) { + break; + } + if (app_pid != -1) { +#if USE_AUL + int app_runs = (aul_app_get_status_bypid(app_pid) >= 0); +#else + int app_runs = (kill(app_pid, 0) == 0); +#endif + if (!app_runs) { + if (verbose) { + log_message("controlled application finished (pid=%d)", app_pid); + } + global_stop = 1; + break; + } + if (last_app_pid == -1) { + last_app_pid = app_pid; + if (verbose) { + log_message("waiting for controlled application exit (pid=%d)", app_pid); + } + } + } // sleep to reduce CPU usage usleep(100 * 1000); // 100 msec }