diff mbox series

[2/2] FreeBSD: Set proper notifier socket buffer size

Message ID ED5494CF-1689-4218-8B4D-5A2676D7627B@siemens.com
State Accepted
Headers show
Series [1/2] FreeBSD: Rework notifier socket location | expand

Commit Message

Storm, Christian Jan. 29, 2024, 1:12 p.m. UTC
Explicitly set socket buffer sizes as the default may
be too small for struct notify_ipc_msg, resulting in
errno == 40 = EMSGSIZE = "Message too long" errors.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 core/notifier.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
diff mbox series

Patch

diff --git a/core/notifier.c b/core/notifier.c
index 967822e1..dace9909 100644
--- a/core/notifier.c
+++ b/core/notifier.c
@@ -381,6 +381,33 @@  static void unlink_notifier_socket(void)
 		socket_path = NULL;
 	}
 }
+
+static void set_socket_bufsize(int fd, int whichbuf, int size)
+{
+	/* Round size up to next ^2 ... */
+	int bufsize = 1;
+	while (bufsize < size) {
+		bufsize <<= 1;
+	}
+	/* ... and add some headroom. */
+	bufsize *= 4;
+	socklen_t buflen = sizeof(bufsize);
+	int res = setsockopt(fd, SOL_SOCKET, whichbuf, &bufsize, sizeof(bufsize));
+	if (res == -1) {
+		fprintf(stderr, "Error %d setsockopt %d=%d:%s\n",
+		errno, whichbuf, bufsize, strerror(errno));
+	}
+	int effective_bufsize = 0;
+	res = getsockopt(fd, SOL_SOCKET, whichbuf, &effective_bufsize, &buflen);
+	if (res == -1) {
+		fprintf(stderr, "Error %d getsockopt %d:%s\n",
+			errno, whichbuf, strerror(errno));
+	}
+	if (effective_bufsize < bufsize) {
+		WARN("Notifier socket buffer is %d, expected: %d.",
+		     effective_bufsize, bufsize);
+	}
+}
 #endif
 
 /*
@@ -444,6 +471,11 @@  static void *notifier_thread (void __attribute__ ((__unused__)) *data)
 		fprintf(stderr, "Could not set %d as cloexec: %s", serverfd, strerror(errno));
 	}
 
+#if defined(__FreeBSD__)
+	set_socket_bufsize(serverfd, SO_SNDBUF, sizeof(struct notify_ipc_msg));
+	set_socket_bufsize(serverfd, SO_RCVBUF, sizeof(struct notify_ipc_msg));
+#endif
+
 	int len_socket_name = strlen(&notify_server.sun_path[1]);
 
 	do {
@@ -534,6 +566,11 @@  void notify_init(void)
 		if (fcntl(notifyfd, F_SETFD, FD_CLOEXEC) < 0)
 			WARN("Could not set %d as cloexec: %s", notifyfd, strerror(errno));
 
+#if defined(__FreeBSD__)
+		set_socket_bufsize(notifyfd, SO_SNDBUF, sizeof(struct notify_ipc_msg));
+		set_socket_bufsize(notifyfd, SO_RCVBUF, sizeof(struct notify_ipc_msg));
+#endif
+
 		if (bind(notifyfd, (const struct sockaddr *) &notify_client,
 			sizeof(struct sockaddr_un)) < 0) {
 				/* Trace cannot work here, use printf */