diff mbox series

Let run multiple instance of SWUpdate for checking

Message ID 20180921190512.4262-1-sbabic@denx.de
State Accepted
Headers show
Series Let run multiple instance of SWUpdate for checking | expand

Commit Message

Stefano Babic Sept. 21, 2018, 7:05 p.m. UTC
It can be useful to run SWUpdate with -c option on the
target while the daemon was already startd. This is currently
forbidden because the notifier tries to bind to the same
abstract socket. Add an enumeration to the socket to
let a second instance to start successfully.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 core/notifier.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

Comments

Diego Rondini Sept. 24, 2018, 9:57 a.m. UTC | #1
Hi Stefano,

On Fri, Sep 21, 2018 at 9:05 PM, Stefano Babic <sbabic@denx.de> wrote:
> It can be useful to run SWUpdate with -c option on the
> target while the daemon was already startd. This is currently
> forbidden because the notifier tries to bind to the same
> abstract socket. Add an enumeration to the socket to
> let a second instance to start successfully.
>
> Signed-off-by: Stefano Babic <sbabic@denx.de>

I like the idea. Works as expected for me, so:
Tested-by: Diego Rondini <diego.rondini@kynetics.com>

> ---
>  core/notifier.c | 33 +++++++++++++++++++++++++++------
>  1 file changed, 27 insertions(+), 6 deletions(-)
>
> diff --git a/core/notifier.c b/core/notifier.c
> index e2a37df..8643c6f 100644
> --- a/core/notifier.c
> +++ b/core/notifier.c
> @@ -259,6 +259,7 @@ static void *notifier_thread (void __attribute__ ((__unused__)) *data)
>  {
>         int serverfd;
>         int len;
> +       int attempt = 0;
>         struct notify_ipc_msg msg;
>
>         /* Initialize and bind to UDS */
> @@ -272,12 +273,27 @@ static void *notifier_thread (void __attribute__ ((__unused__)) *data)
>         setup_socket_cleanup(&notify_server);
>  #endif
>
> -       if (bind(serverfd, (const struct sockaddr *) &notify_server,
> +       int len_socket_name = strlen(&notify_server.sun_path[1]);
> +
> +       do {
> +               errno = 0;
> +               if (bind(serverfd, (const struct sockaddr *) &notify_server,
>                         sizeof(struct sockaddr_un)) < 0) {
> -               fprintf(stderr, "Error binding notifier socket: %s, exiting.\n", strerror(errno));
> -               close(serverfd);
> -               exit(2);
> -       }
> +                       if (errno == EADDRINUSE && attempt < 10) {
> +                               attempt++;
> +                               /*
> +                                * Start increasing the socket as
> +                                * NotifyServer1, NotifyServer2...
> +                                */
> +                               notify_server.sun_path[len_socket_name + 1] = '0' + attempt;
> +                       } else {
> +                               fprintf(stderr, "Error binding notifier socket: %s, exiting.\n", strerror(errno));
> +                               close(serverfd);
> +                               exit(2);
> +                       }
> +               } else
> +                       break;
> +       } while (1);
>
>         do {
>                 len =  recvfrom(serverfd, &msg, sizeof(msg), 0, NULL, NULL);
> @@ -291,7 +307,6 @@ static void *notifier_thread (void __attribute__ ((__unused__)) *data)
>
>  void notify_init(void)
>  {
> -       addr_init(&notify_server, "NotifyServer");
>
>  #ifdef CONFIG_SYSTEMD
>         /*
> @@ -341,6 +356,12 @@ void notify_init(void)
>                         return;
>                 }
>         } else {
> +               /*
> +                * If this is the main process, start setting the name of the
> +                * socket. This can changed if more as one instance of swupdate
> +                * is started (name adjusted to avoid adrress is in use)
> +                */
> +               addr_init(&notify_server, "NotifyServer");
>                 STAILQ_INIT(&clients);
>                 register_notifier(console_notifier);
>                 register_notifier(process_notifier);
> --
> 2.17.1



Best regards,
Diego Rondini
Sr. Embedded Engineer

Kynetics
www.kynetics.com
diff mbox series

Patch

diff --git a/core/notifier.c b/core/notifier.c
index e2a37df..8643c6f 100644
--- a/core/notifier.c
+++ b/core/notifier.c
@@ -259,6 +259,7 @@  static void *notifier_thread (void __attribute__ ((__unused__)) *data)
 {
 	int serverfd;
 	int len;
+	int attempt = 0;
 	struct notify_ipc_msg msg;
 
 	/* Initialize and bind to UDS */
@@ -272,12 +273,27 @@  static void *notifier_thread (void __attribute__ ((__unused__)) *data)
 	setup_socket_cleanup(&notify_server);
 #endif
 
-	if (bind(serverfd, (const struct sockaddr *) &notify_server,
+	int len_socket_name = strlen(&notify_server.sun_path[1]);
+
+	do {
+		errno = 0;
+		if (bind(serverfd, (const struct sockaddr *) &notify_server,
 			sizeof(struct sockaddr_un)) < 0) {
-		fprintf(stderr, "Error binding notifier socket: %s, exiting.\n", strerror(errno));
-		close(serverfd);
-		exit(2);
-	}
+			if (errno == EADDRINUSE && attempt < 10) {
+				attempt++;
+				/*
+				 * Start increasing the socket as
+				 * NotifyServer1, NotifyServer2...
+				 */
+				notify_server.sun_path[len_socket_name + 1] = '0' + attempt;
+			} else {
+				fprintf(stderr, "Error binding notifier socket: %s, exiting.\n", strerror(errno));
+				close(serverfd);
+				exit(2);
+			}
+		} else
+			break;
+	} while (1);
 
 	do {
 		len =  recvfrom(serverfd, &msg, sizeof(msg), 0, NULL, NULL);
@@ -291,7 +307,6 @@  static void *notifier_thread (void __attribute__ ((__unused__)) *data)
 
 void notify_init(void)
 {
-	addr_init(&notify_server, "NotifyServer");
 
 #ifdef CONFIG_SYSTEMD
 	/*
@@ -341,6 +356,12 @@  void notify_init(void)
 			return;
 		}
 	} else {
+		/*
+		 * If this is the main process, start setting the name of the
+		 * socket. This can changed if more as one instance of swupdate
+		 * is started (name adjusted to avoid adrress is in use)
+		 */
+		addr_init(&notify_server, "NotifyServer");
 		STAILQ_INIT(&clients);
 		register_notifier(console_notifier);
 		register_notifier(process_notifier);