close sockets and file streams on exit
authorAlexey Chernobaev <achernobaev@dev.rtsoft.ru>
Wed, 29 Aug 2018 18:45:33 +0000 (21:45 +0300)
committerAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Mon, 3 Sep 2018 15:21:42 +0000 (18:21 +0300)
profctl.c

index 031f60b794679b061882c3090fba0134daa7a28b..c05e43448a8a03adfd556edfdcc3d2365e660dd3 100644 (file)
--- a/profctl.c
+++ b/profctl.c
@@ -55,6 +55,9 @@ static FILE *orig_stderr = NULL;
 static int controlPort = -1;
 static int dataPort = -1;
 static int statPort = -1;
+static int control_socket = -1;
+static int data_socket = -1;
+static int stat_socket = -1;
 static FILE *ctrl_file_in = NULL;
 static FILE *ctrl_file_out = NULL;
 static FILE *data_file_out = NULL;
@@ -185,8 +188,8 @@ static void *output_thread(void *arg)
        openFileProcess();
 
        if (dataPort > 0) {
-               int s = openPort(dataPort);
-               data_file_out = fdopen(s, "w");
+               data_socket = openPort(dataPort);
+               data_file_out = fdopen(data_socket, "w");
                if (data_file_out == NULL) {
                        log_system_error_and_exit("fdopen(data,w)");
                }
@@ -253,13 +256,35 @@ static void finish()
                        unlink(pname);
                }
        }
+       if (ctrl_file_in != NULL) {
+               if (ctrl_file_in != orig_stdin) {
+                       fclose(ctrl_file_in);
+               }
+       }
+       if (ctrl_file_out != NULL) {
+               if (ctrl_file_out != orig_stdout) {
+                       fclose(ctrl_file_out);
+               }
+       }
        if (data_file_out != NULL) {
                fflush(data_file_out);
                if (data_file_out != orig_stdout) {
                        fclose(data_file_out);
                }
        }
+       if (control_socket >= 0) {
+               close(control_socket);
+       }
+       if (data_socket >= 0) {
+               close(data_socket);
+       }
+       if (stat_socket >= 0) {
+               close(stat_socket);
+       }
        log_error("=== finished ===");
+       if (stderr != orig_stderr) {
+               fclose(stderr);
+       }
        finish_close_stdio();
        kill(getpid(), SIGKILL);
 }
@@ -669,8 +694,7 @@ static int openPort(int port)
        }
 
        int enable = 1;
-       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
-       {
+       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
                log_system_error_and_exit("setsockopt");
        }
 
@@ -679,12 +703,13 @@ static int openPort(int port)
        saddr.sin_addr.s_addr = INADDR_ANY;
        saddr.sin_port = htons(port);
 
-       if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
-       {
+       if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
                log_system_error_and_exit("bind");
        }
 
-       listen(sock, 5);
+       if (listen(sock, 5) < 0) {
+               log_system_error_and_exit("listen");
+       }
 
        if (verbose) {
                log_error("waiting for connection to port %d", port);
@@ -698,6 +723,7 @@ static int openPort(int port)
        if (verbose) {
                log_error("accepted connection to port %d", port);
        }
+       close(sock);
        return result;
 }
 
@@ -784,12 +810,12 @@ int main(int argc, char **argv)
 #endif /* TIZEN */
 
        if (controlPort > 0) {
-               int s = openPort(controlPort);
-               ctrl_file_in = fdopen(s, "r");
+               control_socket = openPort(controlPort);
+               ctrl_file_in = fdopen(control_socket, "r");
                if (ctrl_file_in == NULL) {
                        log_system_error_and_exit("fdopen(control,r)");
                }
-               ctrl_file_out = fdopen(s, "w");
+               ctrl_file_out = fdopen(control_socket, "w");
                if (ctrl_file_out == NULL) {
                        log_system_error_and_exit("fdopen(control,w)");
                }
@@ -798,8 +824,8 @@ int main(int argc, char **argv)
        }
 
        if (doinfo && (statPort > 0)) {
-               int s = openPort(statPort);
-               stat_file_out = fdopen(s, "w");
+               stat_socket = openPort(statPort);
+               stat_file_out = fdopen(stat_socket, "w");
                if (stat_file_out == NULL) {
                        log_system_error_and_exit("fdopen(stat,w)");
                }