[PATCH] libnl-tiny: set SOCK_CLOEXEC if available

Joerg Vehlow lkml at jv-coder.de
Wed Oct 26 01:21:04 PDT 2022


From: Joerg Vehlow <joerg.vehlow at aox.de>

If CLOEXEC is not set on the netlink socket, restarting netifd using ubus
fails with "Failed to initialize system control", because the bind call
in nl_connect fails with EADDRINUSE, due to the inherited socket handle.

Also it does not make sense, to leak the handle to child processes.

See libnl3: ca0fc7558 ("socket: Set SOCK_CLOEXEC if available")

Signed-off-by: Joerg Vehlow <joerg.vehlow at aox.de>
---
 nl.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/nl.c b/nl.c
index c875573..32d26a3 100644
--- a/nl.c
+++ b/nl.c
@@ -106,9 +106,14 @@
 int nl_connect(struct nl_sock *sk, int protocol)
 {
 	int err;
+	int flags = 0;
 	socklen_t addrlen;
 
-	sk->s_fd = socket(AF_NETLINK, SOCK_RAW, protocol);
+#ifdef SOCK_CLOEXEC
+	flags = SOCK_CLOEXEC;
+#endif
+
+	sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
 	if (sk->s_fd < 0) {
 		err = -nl_syserr2nlerr(errno);
 		goto errout;
-- 
2.25.1




More information about the openwrt-devel mailing list