From c68df363a0e8a65d34d21a0c3000df6655cdda9c Mon Sep 17 00:00:00 2001 From: Alexey Chernobaev Date: Wed, 29 Aug 2018 21:45:33 +0300 Subject: [PATCH] close sockets and file streams on exit --- profctl.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/profctl.c b/profctl.c index 031f60b..c05e434 100644 --- 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)"); } -- 2.7.4