git: 4edbd4e35c4e - main - snmp_hostres(3): strengthen work on the devd(8) socket

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 13 Nov 2026 02:38:02 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=4edbd4e35c4e0fb7cf10ed5e9b12a2b76bef2cec

commit 4edbd4e35c4e0fb7cf10ed5e9b12a2b76bef2cec
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2026-11-13 02:37:33 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2026-11-13 02:37:33 +0000

    snmp_hostres(3): strengthen work on the devd(8) socket
    
    Use the SOCK_SEQPACKET version of the socket.  This guarantees that
    messages read won't be glued together.  Use sufficient buffer size to
    read any message.
    
    Close socket on any kind of error or a short read.
    
    This also fixes a bug when a SOCK_STREAM socket containing exactly
    multiple of 512 bytes would block the daemon in read(2) until some
    other devd(8) event.  Thank to David for catching and analyzing it.
    
    Reported by:            David Pfitzner <dpfitzner@netflix.com>
    
    Reviewed by:            imp
    Differential Revision:  https://reviews.freebsd.org/D61146
---
 .../modules/snmp_hostres/hostres_device_tbl.c      | 72 +++++++++-------------
 .../bsnmpd/modules/snmp_hostres/hostres_snmp.h     |  3 -
 2 files changed, 28 insertions(+), 47 deletions(-)

diff --git a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c
index b769b17484a4..80f20aba0b9b 100644
--- a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c
+++ b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c
@@ -37,6 +37,7 @@
 #include <assert.h>
 #include <err.h>
 #include <errno.h>
+#include <paths.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
@@ -82,10 +83,11 @@ static uint64_t device_tick = 0;
 /* maximum number of ticks between updates of device table */
 uint32_t device_tbl_refresh = 10 * 100;
 
-/* socket for /var/run/devd.pipe */
+/* socket to devd(8) */
 static int devd_sock = -1;
+static size_t devd_maxrecord;
 
-/* used to wait notifications from /var/run/devd.pipe */
+/* used to wait notifications from devd(8) */
 static void *devd_fd;
 
 /* some constants */
@@ -401,29 +403,26 @@ device_collector(struct devinfo_dev *dev, void *arg)
 static int
 create_devd_socket(void)
 {
+	struct sockaddr_un devd_addr = {
+		.sun_family = AF_LOCAL,
+		.sun_len = sizeof(devd_addr),
+		.sun_path = _PATH_DEVD_SOCKET,
+	};
 	int d_sock;
- 	struct sockaddr_un devd_addr;
 
- 	bzero(&devd_addr, sizeof(struct sockaddr_un));
-
- 	if ((d_sock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
+	if ((d_sock = socket(PF_LOCAL, SOCK_SEQPACKET, 0)) < 0) {
  		syslog(LOG_ERR, "Failed to create the socket for %s: %m",
-		    PATH_DEVD_PIPE);
+		    devd_addr.sun_path);
  		return (-1);
  	}
 
- 	devd_addr.sun_family = PF_LOCAL;
-	devd_addr.sun_len = sizeof(devd_addr);
- 	strlcpy(devd_addr.sun_path, PATH_DEVD_PIPE,
-	    sizeof(devd_addr.sun_path) - 1);
-
  	if (connect(d_sock, (struct sockaddr *)&devd_addr,
 	    sizeof(devd_addr)) == -1) {
  		syslog(LOG_ERR,"Failed to connect socket for %s: %m",
-		    PATH_DEVD_PIPE);
+		    devd_addr.sun_path);
  		if (close(d_sock) < 0 )
  			syslog(LOG_ERR,"Failed to close socket for %s: %m",
-			    PATH_DEVD_PIPE);
+			    devd_addr.sun_path);
 		return (-1);
  	}
 
@@ -439,46 +438,29 @@ create_devd_socket(void)
 static void
 devd_socket_callback(int fd, void *arg __unused)
 {
-	char buf[512];
-	int read_len = -1;
+	char buf[devd_maxrecord];
+	ssize_t len;
 
 	assert(fd == devd_sock);
 
 	HRDBG("called");
 
-again:
-	read_len = read(fd, buf, sizeof(buf));
-	if (read_len < 0) {
-		if (errno == EBADF) {
-			devd_sock = -1;
-			if (devd_fd != NULL) {
-				fd_deselect(devd_fd);
-				devd_fd = NULL;
-			}
-			syslog(LOG_ERR, "Closing devd_fd, revert to "
-			    "devinfo polling");
-		}
-
-	} else if (read_len == 0) {
-		syslog(LOG_ERR, "zero bytes read from devd pipe... "
-		    "closing socket!");
-
-		if (close(devd_sock) < 0 )
- 			syslog(LOG_ERR, "Failed to close devd socket: %m");
-
+	if ((len = recv(fd, buf, devd_maxrecord, MSG_WAITALL)) > 0) {
+		/* Only refresh device table on a device add or remove event. */
+		if (buf[0] == '+' || buf[0] == '-')
+			refresh_device_tbl(1);
+	} else {
+		if (len == 0)
+			syslog(LOG_ERR, "zero bytes read(2) on devd socket");
+		else
+			syslog(LOG_ERR, "read(2) on devd socket: %m");
+		syslog(LOG_ERR, "Reverting to devinfo polling");
+		(void)close(devd_sock);
 		devd_sock = -1;
 		if (devd_fd != NULL) {
 			fd_deselect(devd_fd);
 			devd_fd = NULL;
 		}
-		syslog(LOG_ERR, "Closing devd_fd, revert to devinfo polling");
-
-	} else {
-		if (read_len == sizeof(buf))
-			goto again;
-		/* Only refresh device table on a device add or remove event. */
-		if (buf[0] == '+' || buf[0] == '-')
-			refresh_device_tbl(1);
 	}
 }
 
@@ -494,6 +476,8 @@ init_device_tbl(void)
 
 	/* no problem if that fails - just use polling mode */
 	devd_sock = create_devd_socket();
+
+	devd_maxrecord = sysconf(_SC_DEVCTL_MAXRECORD);
 }
 
 /**
diff --git a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h
index 4ea6db1403ab..8a427327bedf 100644
--- a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h
+++ b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h
@@ -80,9 +80,6 @@ struct statfs;
 
 #endif /*NDEBUG*/
 
-/* path to devd(8) output pipe */
-#define	PATH_DEVD_PIPE	"/var/run/devd.pipe"
-
 #define	IS_KERNPROC(kp)	(((kp)->ki_flag & P_KPROC) == P_KPROC)
 
 enum snmpTCTruthValue {