From: Alexey Chernobaev Date: Wed, 29 Aug 2018 17:59:15 +0000 (+0300) Subject: close stdin/stdout/stderr before exiting X-Git-Tag: submit/tizen/20180911.125435~22 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c02f8655356d410b52aba888feb2885169e09194;p=sdk%2Ftools%2Fprofctl.git close stdin/stdout/stderr before exiting --- diff --git a/profctl.c b/profctl.c index cd48375..1c78b83 100644 --- 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); }