[PATCH 3/4] alfred: close the listening socket on daemon setup errors

Sven Eckelmann <[email protected]> Fri, 31 Jul 2026 09:40:15 +0200
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
unix_sock_open_daemon() created the listening unix socket but returned -1
without closing it again when bind(), listen() or the epoll_ctl()
registration failed, leaking the descriptor.

Fixes: 23359bbf72aa ("alfred: initial commit")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 unix_sock.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/unix_sock.c b/unix_sock.c
index e24c788..d4261ba 100644
--- a/unix_sock.c
+++ b/unix_sock.c
@@ -51,12 +51,12 @@ int unix_sock_open_daemon(struct globals *globals)
 	if (bind(globals->unix_sock, (struct sockaddr *)&addr,
 		 sizeof(addr)) < 0) {
 		perror("can't bind unix socket");
-		return -1;
+		goto err;
 	}
 
 	if (listen(globals->unix_sock, 10) < 0) {
 		perror("can't listen on unix socket");
-		return -1;
+		goto err;
 	}
 
 	ev.events = EPOLLIN;
@@ -66,10 +66,15 @@ int unix_sock_open_daemon(struct globals *globals)
 	if (epoll_ctl(globals->epollfd, EPOLL_CTL_ADD, globals->unix_sock,
 		      &ev) == -1) {
 		perror("Failed to add epoll for check_timer");
-		return -1;
+		goto err;
 	}
 
 	return 0;
+
+err:
+	close(globals->unix_sock);
+	globals->unix_sock = -1;
+	return -1;
 }
 
 int unix_sock_open_client(struct globals *globals)

-- 
2.47.3