stop profctl if controlled application finished
authorAlexey Chernobaev <achernobaev@dev.rtsoft.ru>
Fri, 31 Aug 2018 17:16:36 +0000 (20:16 +0300)
committerAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Mon, 3 Sep 2018 15:21:42 +0000 (18:21 +0300)
proc_stat.c
profctl.c

index bf98d8eaa5ac548019f2fd068ed33311332f2428..3a6a3668e9955876fb885a8fb47c820f2528eada 100644 (file)
@@ -1,7 +1,6 @@
 #define _GNU_SOURCE
 #include <errno.h>
 #include <pthread.h>
-#include <signal.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #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;
 }
index f55b8f3b177b01c0ff462f67b039df64b1df7bd7..a9090b6c4c254c33eb23814014a7c1877529df45 100644 (file)
--- a/profctl.c
+++ b/profctl.c
@@ -24,9 +24,9 @@
 
 #include <sys/time.h>
 #include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/xattr.h>
-#include <sys/socket.h>
 #include <netinet/in.h>
 #include <fcntl.h>
 
@@ -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
        }