diff mbox series

core: unlink socket files on normal termination

Message ID 20170915114030.25138-1-christian.storm@siemens.com
State Accepted
Headers show
Series core: unlink socket files on normal termination | expand

Commit Message

Storm, Christian Sept. 15, 2017, 11:40 a.m. UTC
Currently, the socket files are (re)created on SWUpdate
starting but are not unlinked on SWUpdate exiting. Leverage
atexit() and a SIGTERM handler to cleanup socket files on
normal program termination.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 core/swupdate.c           | 14 ++++++++++++++
 corelib/network_thread.c  | 10 ++++++++++
 corelib/progress_thread.c | 10 ++++++++++
 3 files changed, 34 insertions(+)

Comments

Stefano Babic Sept. 18, 2017, 10:36 a.m. UTC | #1
On 15/09/2017 13:40, Christian Storm wrote:
> Currently, the socket files are (re)created on SWUpdate
> starting but are not unlinked on SWUpdate exiting. Leverage
> atexit() and a SIGTERM handler to cleanup socket files on
> normal program termination.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  core/swupdate.c           | 14 ++++++++++++++
>  corelib/network_thread.c  | 10 ++++++++++
>  corelib/progress_thread.c | 10 ++++++++++
>  3 files changed, 34 insertions(+)
> 
> diff --git a/core/swupdate.c b/core/swupdate.c
> index 3057000..e35289e 100644
> --- a/core/swupdate.c
> +++ b/core/swupdate.c
> @@ -483,6 +483,11 @@ static int read_processes_settings(void *settings, void *data)
>  	return 0;
>  }
>  
> +static void sigterm_handler(int __attribute__ ((__unused__)) signum)
> +{
> +	pthread_cancel(network_daemon);
> +}
> +
>  int main(int argc, char **argv)
>  {
>  	int c;
> @@ -859,6 +864,15 @@ int main(int argc, char **argv)
>  		}
>  	}
>  
> +	/*
> +	 * Install a handler for SIGTERM that cancels
> +	 * the network_daemon thread to allow atexit()
> +	 * registered functions to run.
> +	 */
> +	memset(&sa, 0, sizeof(sa));
> +	sa.sa_handler = sigterm_handler;
> +	sigaction(SIGTERM, &sa, NULL);
> +
>  	/*
>  	 * Go into supervisor loop
>  	 */
> diff --git a/corelib/network_thread.c b/corelib/network_thread.c
> index 6c34614..5ed516e 100644
> --- a/corelib/network_thread.c
> +++ b/corelib/network_thread.c
> @@ -171,6 +171,11 @@ static void empty_pipe(int fd)
>  	} while (1);
>  }
>  
> +static void unlink_socket(void)
> +{
> +	unlink((char*)CONFIG_SOCKET_CTRL_PATH);
> +}
> +
>  void *network_thread (void *data)
>  {
>  	struct installer *instp = (struct installer *)data;
> @@ -200,6 +205,11 @@ void *network_thread (void *data)
>  		exit(2);
>  	}
>  
> +	if (atexit(unlink_socket) != 0) {
> +		TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.",
> +			  (char*)CONFIG_SOCKET_CTRL_PATH);
> +	}
> +
>  	do {
>  		clilen = sizeof(cliaddr);
>  		if ( (ctrlconnfd = accept(ctrllisten, (struct sockaddr *) &cliaddr, &clilen)) < 0) {
> diff --git a/corelib/progress_thread.c b/corelib/progress_thread.c
> index 14d4735..e0a11fe 100644
> --- a/corelib/progress_thread.c
> +++ b/corelib/progress_thread.c
> @@ -183,6 +183,11 @@ void swupdate_progress_done(const char *info)
>  	pthread_mutex_unlock(&prbar->lock);
>  }
>  
> +static void unlink_socket(void)
> +{
> +	unlink((char*)CONFIG_SOCKET_PROGRESS_PATH);
> +}
> +
>  void *progress_bar_thread (void __attribute__ ((__unused__)) *data)
>  {
>  	int listen, connfd;
> @@ -201,6 +206,11 @@ void *progress_bar_thread (void __attribute__ ((__unused__)) *data)
>  		exit(2);
>  	}
>  
> +	if (atexit(unlink_socket) != 0) {
> +		TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.",
> +			  (char*)CONFIG_SOCKET_PROGRESS_PATH);
> +	}
> +
>  	do {
>  		clilen = sizeof(cliaddr);
>  		if ( (connfd = accept(listen, (struct sockaddr *) &cliaddr, &clilen)) < 0) {
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/core/swupdate.c b/core/swupdate.c
index 3057000..e35289e 100644
--- a/core/swupdate.c
+++ b/core/swupdate.c
@@ -483,6 +483,11 @@  static int read_processes_settings(void *settings, void *data)
 	return 0;
 }
 
+static void sigterm_handler(int __attribute__ ((__unused__)) signum)
+{
+	pthread_cancel(network_daemon);
+}
+
 int main(int argc, char **argv)
 {
 	int c;
@@ -859,6 +864,15 @@  int main(int argc, char **argv)
 		}
 	}
 
+	/*
+	 * Install a handler for SIGTERM that cancels
+	 * the network_daemon thread to allow atexit()
+	 * registered functions to run.
+	 */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = sigterm_handler;
+	sigaction(SIGTERM, &sa, NULL);
+
 	/*
 	 * Go into supervisor loop
 	 */
diff --git a/corelib/network_thread.c b/corelib/network_thread.c
index 6c34614..5ed516e 100644
--- a/corelib/network_thread.c
+++ b/corelib/network_thread.c
@@ -171,6 +171,11 @@  static void empty_pipe(int fd)
 	} while (1);
 }
 
+static void unlink_socket(void)
+{
+	unlink((char*)CONFIG_SOCKET_CTRL_PATH);
+}
+
 void *network_thread (void *data)
 {
 	struct installer *instp = (struct installer *)data;
@@ -200,6 +205,11 @@  void *network_thread (void *data)
 		exit(2);
 	}
 
+	if (atexit(unlink_socket) != 0) {
+		TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.",
+			  (char*)CONFIG_SOCKET_CTRL_PATH);
+	}
+
 	do {
 		clilen = sizeof(cliaddr);
 		if ( (ctrlconnfd = accept(ctrllisten, (struct sockaddr *) &cliaddr, &clilen)) < 0) {
diff --git a/corelib/progress_thread.c b/corelib/progress_thread.c
index 14d4735..e0a11fe 100644
--- a/corelib/progress_thread.c
+++ b/corelib/progress_thread.c
@@ -183,6 +183,11 @@  void swupdate_progress_done(const char *info)
 	pthread_mutex_unlock(&prbar->lock);
 }
 
+static void unlink_socket(void)
+{
+	unlink((char*)CONFIG_SOCKET_PROGRESS_PATH);
+}
+
 void *progress_bar_thread (void __attribute__ ((__unused__)) *data)
 {
 	int listen, connfd;
@@ -201,6 +206,11 @@  void *progress_bar_thread (void __attribute__ ((__unused__)) *data)
 		exit(2);
 	}
 
+	if (atexit(unlink_socket) != 0) {
+		TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.",
+			  (char*)CONFIG_SOCKET_PROGRESS_PATH);
+	}
+
 	do {
 		clilen = sizeof(cliaddr);
 		if ( (connfd = accept(listen, (struct sockaddr *) &cliaddr, &clilen)) < 0) {