diff mbox series

[1/2] FreeBSD: Rework notifier socket location

Message ID 8003F0C8-503D-492E-8FEE-8280649FBB6A@siemens.com
State Accepted
Headers show
Series [1/2] FreeBSD: Rework notifier socket location | expand

Commit Message

Storm, Christian Jan. 29, 2024, 1:09 p.m. UTC
Remove FreeBSD-specific KConfig CONFIG_SOCKET_NOTIFIER_DIRECTORY
in favor of using $RUNTIME_DIRECTORY, $TMPDIR, pr /tmp, in this
order, as done for the other sockets.

Note that although $RUNTIME_DIRECTORY is not native to FreeBSD, it's
considered for consistency with Linux integrations.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 Kconfig         |  8 -------
 core/notifier.c | 58 +++++++++++++++++++++++++++++--------------------
 2 files changed, 35 insertions(+), 31 deletions(-)
diff mbox series

Patch

diff --git a/Kconfig b/Kconfig
index cb346e12..341f062d 100644
--- a/Kconfig
+++ b/Kconfig
@@ -223,14 +223,6 @@  config SOCKET_PROGRESS_PATH
 	help
 	  Path to the socket progress information is sent to.
 
-config SOCKET_NOTIFIER_DIRECTORY
-	string "SWUpdate notifier socket directory"
-	depends on HAVE_FREEBSD
-	default "/tmp/"
-	help
-	  Path to SWUpdate's notifier sockets on FreeBSD as
-	  Linux-like abstract sockets are not available.
-
 endmenu
 
 config MTD
diff --git a/core/notifier.c b/core/notifier.c
index 7fd7eca8..967822e1 100644
--- a/core/notifier.c
+++ b/core/notifier.c
@@ -373,22 +373,14 @@  static void progress_notifier (RECOVERY_STATUS status, int event, int level, con
 
 #if defined(__FreeBSD__)
 static char* socket_path = NULL;
-static void unlink_socket(void)
+static void unlink_notifier_socket(void)
 {
-	if (socket_path) {
-		unlink(socket_path);
+	if (socket_path != NULL) {
+		(void)unlink(socket_path);
 		free(socket_path);
+		socket_path = NULL;
 	}
 }
-
-static void setup_socket_cleanup(struct sockaddr_un *addr)
-{
-	socket_path = strndup(addr->sun_path, sizeof(addr->sun_path));
-	if (atexit(unlink_socket) != 0) {
-		TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.", socket_path);
-	}
-	unlink(socket_path);
-}
 #endif
 
 /*
@@ -408,10 +400,21 @@  static void addr_init(struct sockaddr_un *addr, const char *path)
 	/*
 	 * Use filesystem-backed sockets on FreeBSD although this interface
 	 * is internal as there are no Linux-like abstract sockets.
+	 *
+	 * Note: $RUNTIME_DIRECTORY is not native to FreeBSD but for
+	 * consistency with Linux and containment, try to use it.
 	 */
-	strncpy(addr->sun_path, CONFIG_SOCKET_NOTIFIER_DIRECTORY, sizeof(addr->sun_path));
-	strncat(addr->sun_path, path,
-			sizeof(addr->sun_path)-strlen(CONFIG_SOCKET_NOTIFIER_DIRECTORY));
+	const char *socketdir = getenv("RUNTIME_DIRECTORY");
+	if(!socketdir){
+		socketdir = getenv("TMPDIR");
+	}
+	if (!socketdir) {
+		socketdir = "/tmp";
+	}
+	if (snprintf(addr->sun_path, sizeof(addr->sun_path), "%s/%s", socketdir, path) < 0) {
+		fprintf(stderr, "Error creating notifier socket, exiting.");
+		exit(2);
+	}
 #else
 	ERROR("Undetected OS, probably sockets won't function as expected.");
 #endif
@@ -441,10 +444,6 @@  static void *notifier_thread (void __attribute__ ((__unused__)) *data)
 		fprintf(stderr, "Could not set %d as cloexec: %s", serverfd, strerror(errno));
 	}
 
-#if defined(__FreeBSD__)
-	setup_socket_cleanup(&notify_server);
-#endif
-
 	int len_socket_name = strlen(&notify_server.sun_path[1]);
 
 	do {
@@ -459,7 +458,9 @@  static void *notifier_thread (void __attribute__ ((__unused__)) *data)
 				 */
 				notify_server.sun_path[len_socket_name + 1] = '0' + attempt;
 			} else {
-				fprintf(stderr, "Error binding notifier socket: %s, exiting.\n", strerror(errno));
+				fprintf(stderr,
+					"Error binding notifier socket %s: %s, exiting.\n",
+					(char *)&notify_server.sun_path[0], strerror(errno));
 				close(serverfd);
 				exit(2);
 			}
@@ -467,6 +468,13 @@  static void *notifier_thread (void __attribute__ ((__unused__)) *data)
 			break;
 	} while (1);
 
+#if defined(__FreeBSD__)
+	socket_path = strndup(&notify_server.sun_path[0], sizeof(notify_server.sun_path));
+	if (atexit(unlink_notifier_socket) != 0) {
+		TRACE("Cannot setup socket cleanup on exit, %s won't be unlinked.", socket_path);
+	}
+#endif
+
 	thread_ready();
 	do {
 		len =  recvfrom(serverfd, &msg, sizeof(msg), 0, NULL, NULL);
@@ -516,9 +524,6 @@  void notify_init(void)
 		char buf[60];
 		snprintf(buf, sizeof(buf), "Notify%d", pid);
 		addr_init(&notify_client, buf);
-#if defined(__FreeBSD__)
-		setup_socket_cleanup(&notify_client);
-#endif
 		notifyfd = socket(AF_UNIX, SOCK_DGRAM, 0);
 
 		if (notifyfd < 0) {
@@ -537,6 +542,13 @@  void notify_init(void)
 			close(notifyfd);
 			return;
 		}
+#if defined(__FreeBSD__)
+		/*
+		 * Note: atexit(unlink_socket) "callback" is inherited from parent,
+		 * reuse it, but with this child's socket_path.
+		 */
+		socket_path = strndup(&notify_client.sun_path[0], sizeof(notify_client.sun_path));
+#endif
 	} else {
 		/*
 		 * If this is the main process, start setting the name of the