open data socket beforehand to prevent data loss
authorAlexey Chernobaev <achernobaev@dev.rtsoft.ru>
Wed, 29 Aug 2018 10:23:09 +0000 (13:23 +0300)
committerAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Mon, 3 Sep 2018 15:21:42 +0000 (18:21 +0300)
profctl.c

index 25aac4a5821614fdb0ed252647b829fc8fdbf040..7381088eb8bb0e621275a7fa69ab8e61f846959a 100644 (file)
--- 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) {