git: 80c0c83b5d77 - stable/13 - sysctl(3): Implement SYSCTL_FOREACH() to iterate all OIDs in a sysctl list.

From: Hans Petter Selasky <hselasky_at_FreeBSD.org>
Date: Tue, 04 Oct 2022 14:50:20 UTC
The branch stable/13 has been updated by hselasky:

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

commit 80c0c83b5d77d22b7556ba6cba20401692897d6e
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-09-27 13:48:16 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-10-04 14:40:53 +0000

    sysctl(3): Implement SYSCTL_FOREACH() to iterate all OIDs in a sysctl list.
    
    To avoid using the sysctl list macros directly in external kernel modules.
    
    Reviewed by:            asomers, manu and asiciliano
    Differential Revision:  https://reviews.freebsd.org/D36748
    Sponsored by:   NVIDIA Networking
    
    (cherry picked from commit c075ea46bca6d48ce9f639e4575077d06d392427)
---
 sys/kern/kern_sysctl.c | 11 +++++------
 sys/sys/sysctl.h       |  3 +++
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index ade5eed8edc3..6718f77ee75b 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -134,7 +134,7 @@ sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
 	struct sysctl_oid *oidp;
 
 	SYSCTL_ASSERT_LOCKED();
-	SLIST_FOREACH(oidp, list, oid_link) {
+	SYSCTL_FOREACH(oidp, list) {
 		if (strcmp(oidp->oid_name, name) == 0) {
 			return (oidp);
 		}
@@ -1016,7 +1016,7 @@ sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
 	struct sysctl_oid *oidp;
 
 	SYSCTL_ASSERT_LOCKED();
-	SLIST_FOREACH(oidp, l, oid_link) {
+	SYSCTL_FOREACH(oidp, l) {
 		for (k=0; k<i; k++)
 			printf(" ");
 
@@ -1332,13 +1332,12 @@ name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
 	for (*len = 0; *len < CTL_MAXNAME;) {
 		p = strsep(&name, ".");
 
-		oidp = SLIST_FIRST(lsp);
-		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
-			if (oidp == NULL)
-				return (ENOENT);
+		SYSCTL_FOREACH(oidp, lsp) {
 			if (strcmp(p, oidp->oid_name) == 0)
 				break;
 		}
+		if (oidp == NULL)
+			return (ENOENT);
 		*oid++ = oidp->oid_number;
 		(*len)++;
 
diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h
index 8f7741eb0b4c..ecf40cc1bfb5 100644
--- a/sys/sys/sysctl.h
+++ b/sys/sys/sysctl.h
@@ -905,6 +905,9 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
 	    NULL);							\
 })
 
+#define	SYSCTL_FOREACH(oidp, list) \
+	SLIST_FOREACH(oidp, list, oid_link)
+
 /*
  * A macro to generate a read-only sysctl to indicate the presence of optional
  * kernel features.