From: Alexey Chernobaev Date: Wed, 29 Aug 2018 10:23:09 +0000 (+0300) Subject: open data socket beforehand to prevent data loss X-Git-Tag: submit/tizen/20180911.125435~15 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4d1e7a69b33bc6f360b92dbaf90a54bffdfa5f62;p=sdk%2Ftools%2Fprofctl.git open data socket beforehand to prevent data loss --- diff --git a/profctl.c b/profctl.c index 25aac4a..7381088 100644 --- a/profctl.c +++ b/profctl.c @@ -80,37 +80,12 @@ static volatile int command_loop_thread_runs = 0; static volatile int proc_stat_thread_runs = 0; static pthread_t proc_stat_thread_id; -static int openFileProcess(); -static int openPort(int port); static FILE *pipef; static void *data_output_thread(void *arg) { do { - if (openFileProcess() != 0) { - break; - } - - if (dataPort > 0) { - data_socket = openPort(dataPort); - if (data_socket < 0) { - break; - } - data_file_out = fdopen(data_socket, "w"); - if (data_file_out == NULL) { - log_system_error("fdopen(data,w)"); - break; - } - if (fprintf(data_file_out, "ready\n") < 0 || fflush(data_file_out) != 0) { - log_system_error("cannot write 'ready' prompt to data stream"); - break; - } - } - else { - data_file_out = stdout; - } - int linesRead = 0; char *buffer = NULL; @@ -395,7 +370,7 @@ static int openPort(int port) int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { log_system_error("socket"); - return sock; + return -1; } int enable = 1; @@ -675,6 +650,32 @@ int main(int argc, char **argv) ctrl_file_in = stdin; } + if (pname) { + // TODO!! log to stderr as well if failed to start data_output_thread (i.e. before all 'return 1') + if (openFileProcess() != 0) { + return 1; + } + + if (dataPort > 0) { + data_socket = openPort(dataPort); + if (data_socket < 0) { + return 1; + } + data_file_out = fdopen(data_socket, "w"); + if (data_file_out == NULL) { + log_system_error("fdopen(data,w)"); + return 1; + } + if (fprintf(data_file_out, "ready\n") < 0 || fflush(data_file_out) != 0) { + log_system_error("cannot write 'ready' prompt to data stream"); + return 1; + } + } + else { + data_file_out = stdout; + } + } + if (doinfo && (statPort > 0)) { stat_socket = openPort(statPort); if (stat_socket < 0) {