close stdin/stdout/stderr before exiting
authorAlexey Chernobaev <achernobaev@dev.rtsoft.ru>
Wed, 29 Aug 2018 17:59:15 +0000 (20:59 +0300)
committerAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Mon, 3 Sep 2018 15:21:42 +0000 (18:21 +0300)
profctl.c

index cd48375..1c78b83 100644 (file)
--- a/profctl.c
+++ b/profctl.c
@@ -48,6 +48,8 @@ static char *oname = NULL;
 
 static int isPipeOwner = 0;
 
+static FILE *orig_stdin = NULL;
+static FILE *orig_stdout = NULL;
 static FILE *orig_stderr = NULL;
 
 static int controlPort = -1;
@@ -165,6 +167,20 @@ static void *wait_thread(void *arg)
 }
 #endif /* TIZEN */
 
+// On Tizen, launch_app won't terminate until stdin, stdout and stderr are closed
+static void finish_close_stdio()
+{
+       if (orig_stdin != NULL) {
+               fclose(orig_stdin);
+       }
+       if (orig_stdout != NULL) {
+               fclose(orig_stdout);
+       }
+       if (orig_stderr != NULL) {
+               fclose(orig_stderr);
+       }
+}
+
 static void finish()
 {
 #if TIZEN
@@ -180,8 +196,12 @@ static void finish()
        }
        if (data_file_out != NULL) {
                fflush(data_file_out);
+               if (data_file_out != orig_stdout) {
+                       fclose(data_file_out);
+               }
        }
        log_error("=== finished ===");
+       finish_close_stdio();
        kill(getpid(), SIGKILL);
 }
 
@@ -637,6 +657,12 @@ static int openPort(int port)
 
 int main(int argc, char **argv)
 {
+       orig_stdin = stdin;
+       orig_stdout = stdout;
+       orig_stderr = stderr;
+
+       atexit(finish_close_stdio);
+
        if (pthread_mutex_init(&stderr_lock, NULL) != 0)
        {
                perror("mutex init failed\n");
@@ -655,6 +681,8 @@ int main(int argc, char **argv)
 
        while(!process_option(argc, argv));
 
+       atexit(finish);
+
 #if TIZEN
        if (appid == NULL) {
                log_error("Unknown app id");
@@ -671,7 +699,6 @@ int main(int argc, char **argv)
                log_system_error_and_exit("freopen");
        }
        setlinebuf(redir_stderr);
-       orig_stderr = stderr;
        stderr = redir_stderr;
 
        if (verbose) {
@@ -741,8 +768,6 @@ int main(int argc, char **argv)
                }
        }
 
-       atexit(finish);
-
        if (pname) {
                SimpleThread(&output_thread);
        }