git: b1a743405697 - main - devd: expose maximum record size via sysconf(3)

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

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

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

    devd: expose maximum record size via sysconf(3)
    
    Use UMA allocation size that holds the buffer.  The buffer is actually
    slightly smaller.  Document the latter so that clients don't need to use
    +1 if they later want to NUL-terminate a message.
    
    Reviewed by:            mjg, imp
    Differential Revision:  https://reviews.freebsd.org/D61346
---
 include/unistd.h              |  4 +++
 lib/libc/gen/sysconf.3        | 10 +++++++-
 lib/libc/gen/sysconf.c        |  7 +++++
 sbin/devd/devd.8              | 60 ++++++++++++++++++++++++++++++++++++++-----
 sbin/devd/tests/client_test.c | 15 ++++-------
 sys/kern/kern_devctl.c        |  9 +++++++
 6 files changed, 88 insertions(+), 17 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h
index 0bf0bc395161..adb658dc0086 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -312,6 +312,10 @@ typedef	__useconds_t	useconds_t;
 #define	_SC_NSIG		124
 #endif
 
+#if __BSD_VISIBLE
+#define	_SC_DEVCTL_MAXRECORD	125
+#endif
+
 /* Keys for the confstr(3) function. */
 #if __POSIX_VISIBLE >= 199209
 #define	_CS_PATH		1	/* default value of PATH */
diff --git a/lib/libc/gen/sysconf.3 b/lib/libc/gen/sysconf.3
index 290ef0dc158c..ba92e9420262 100644
--- a/lib/libc/gen/sysconf.3
+++ b/lib/libc/gen/sysconf.3
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd August 30, 2025
+.Dd November 7, 2026
 .Dt SYSCONF 3
 .Os
 .Sh NAME
@@ -227,6 +227,14 @@ Note that it is possible that the product of this value and the value of
 will overflow a
 .Vt long
 in some configurations on a 32bit machine.
+.It Li _SC_DEVCTL_MAXRECORD
+Maximum record size from
+.Xr devctl 4 .
+See
+.Xr devd 8
+and
+.Xr devctl 4
+for details.
 .El
 .Sh RETURN VALUES
 If the call to
diff --git a/lib/libc/gen/sysconf.c b/lib/libc/gen/sysconf.c
index 87aedc07c110..897b1ddad046 100644
--- a/lib/libc/gen/sysconf.c
+++ b/lib/libc/gen/sysconf.c
@@ -622,6 +622,13 @@ yesno:
 	case _SC_UEXTERR_MAXLEN:
 		return (UEXTERROR_MAXLEN);
 
+	case _SC_DEVCTL_MAXRECORD:
+		len = sizeof(value);
+		if (sysctlbyname("hw.bus.devctl_maxrecord", &value, &len, NULL,
+		    0) == -1)
+			return (-1);
+		return ((long)value);
+
 	default:
 		errno = EINVAL;
 		return (-1);
diff --git a/sbin/devd/devd.8 b/sbin/devd/devd.8
index f4e8e173399c..12f3f5bbcc8f 100644
--- a/sbin/devd/devd.8
+++ b/sbin/devd/devd.8
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd May 7, 2025
+.Dd November 7, 2026
 .Dt DEVD 8
 .Os
 .Sh NAME
@@ -131,16 +131,33 @@ wish to hook into the
 .Nm
 system without modifying the user's other
 config files.
-.Pp
+.Sh CLIENT SOCKET
 Since
 .Xr devctl 4
 allows only one active reader,
 .Nm
 multiplexes it, forwarding all events to any number of connected clients.
-Clients connect by opening the SOCK_SEQPACKET
-.Ux
+Clients connect by opening the
+.Dv SOCK_SEQPACKET
+.Xr unix 4
 domain socket at
-.Pa /var/run/devd.seqpacket.pipe .
+.Pa /var/run/devd.seqpacket.pipe ,
+also defined as
+.Va _PATH_DEVD_SOCKET
+in
+.In paths.h .
+.Pp
+The messages from
+.Xr devctl 4
+are copied verbatim to clients.
+This implies the newline termination and lack of NUL termination.
+The kernel and
+.Nm
+guarantee that a message shall be of a size less than
+.Fn sysconf _SC_DEVCTL_MAXRECORD .
+The above features allow a robust client implementation without
+a need to take care of a reassembly, see
+.Sx EXAMPLES .
 .Sh FILES
 .Bl -tag -width ".Pa /var/run/devd.seqpacket.pipe" -compact
 .It Pa /etc/devd.conf
@@ -155,6 +172,36 @@ to communicate with its clients.
 A deprecated socket retained for use with old clients.
 .El
 .Sh EXAMPLES
+A trivial client in C:
+.Bd -literal -offset indent
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <paths.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+	struct sockaddr_un sun = {
+		.sun_family = AF_LOCAL,
+		.sun_path = _PATH_DEVD_SOCKET,
+	};
+	ssize_t len, maxrec = sysconf(_SC_DEVCTL_MAXRECORD);
+	char event[maxrec];
+	int s;
+
+	s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+	connect(s, (struct sockaddr *)&sun, sizeof(sun));
+	while ((len = recv(s, event, maxrec, MSG_WAITALL)) > 0) {
+		event[len] = '\\0';
+		printf("%s", event);
+	}
+
+	return (0);
+}
+.Ed
+.Pp
 Use
 .Xr cat 1
 to monitor kernel events:
@@ -162,7 +209,8 @@ to monitor kernel events:
 .Dl cat /var/run/devd.seqpacket.pipe
 .Sh SEE ALSO
 .Xr devctl 4 ,
-.Xr devd.conf 5
+.Xr devd.conf 5 ,
+.Xr sysconf 3
 .Sh HISTORY
 The
 .Nm
diff --git a/sbin/devd/tests/client_test.c b/sbin/devd/tests/client_test.c
index f92d40dc9892..a7c15c82a13c 100644
--- a/sbin/devd/tests/client_test.c
+++ b/sbin/devd/tests/client_test.c
@@ -105,22 +105,17 @@ ATF_TC_BODY(seqpacket, tc)
 	 * If we never get both events, then the test will timeout.
 	 */
 	while (!(got_create_event && got_destroy_event)) {
-		int cmp;
-		ssize_t len;
-		char event[1024];
+		ssize_t len, maxrec = sysconf(_SC_DEVCTL_MAXRECORD);
+		char event[maxrec];
 
-		/* Read 1 less than sizeof(event) to allow space for NULL */
-		len = recv(s, event, sizeof(event) - 1, MSG_WAITALL);
+		len = recv(s, event, maxrec, MSG_WAITALL);
 		ATF_REQUIRE(len != -1);
-		/* NULL terminate the result */
 		event[len] = '\0';
 		printf("%s", event);
-		cmp = strncmp(event, create_pat, sizeof(create_pat) - 1);
-		if (cmp == 0)
+		if (strncmp(event, create_pat, sizeof(create_pat) - 1) == 0)
 			got_create_event = true;
 
-		cmp = strncmp(event, destroy_pat, sizeof(destroy_pat) - 1);
-		if (cmp == 0)
+		if (strncmp(event, destroy_pat, sizeof(destroy_pat) - 1) == 0)
 			got_destroy_event = true;
 	}
 
diff --git a/sys/kern/kern_devctl.c b/sys/kern/kern_devctl.c
index a37cb23efed8..8a7c720433b9 100644
--- a/sys/kern/kern_devctl.c
+++ b/sys/kern/kern_devctl.c
@@ -111,6 +111,15 @@ struct dev_event_info {
 	char dei_data[DEVCTL_BUFFER];
 };
 
+/*
+ * Used by sysconf(_SC_DEVCTL_MAXRECORD).  We suggest applications to use a
+ * slightly bigger buffer than DEVCTL_BUFFER, off just by size of a pointer.
+ * This is documented and allows applications to utilize this extra space for
+ * an optional NUL termination.  If this structure substantially changes, care
+ * should be taken to export proper size.
+ */
+SYSCTL_UINT(_hw_bus, OID_AUTO, devctl_maxrecord, CTLFLAG_RD,
+    SYSCTL_NULL_UINT_PTR, sizeof(struct dev_event_info), "");
 
 static struct cdevsw dev_cdevsw = {
 	.d_version =	D_VERSION,