From owner-svn-src-head@freebsd.org Sat Oct 24 14:46:39 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 679CE44B73F; Sat, 24 Oct 2020 14:46:39 +0000 (UTC) (envelope-from freqlabs@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CJP7M258Qz4dVt; Sat, 24 Oct 2020 14:46:39 +0000 (UTC) (envelope-from freqlabs@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 2B0D419E6A; Sat, 24 Oct 2020 14:46:39 +0000 (UTC) (envelope-from freqlabs@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09OEkdeY038322; Sat, 24 Oct 2020 14:46:39 GMT (envelope-from freqlabs@FreeBSD.org) Received: (from freqlabs@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09OEkdsF038321; Sat, 24 Oct 2020 14:46:39 GMT (envelope-from freqlabs@FreeBSD.org) Message-Id: <202010241446.09OEkdsF038321@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: freqlabs set sender to freqlabs@FreeBSD.org using -f From: Ryan Moeller Date: Sat, 24 Oct 2020 14:46:39 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367003 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: freqlabs X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 367003 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 Oct 2020 14:46:39 -0000 Author: freqlabs Date: Sat Oct 24 14:46:38 2020 New Revision: 367003 URL: https://svnweb.freebsd.org/changeset/base/367003 Log: kern_sysctl: Misc code cleanup Remove unused oidpp parameter from sysctl_sysctl_next_ls and add high level comments to describe how it works. No functional change. Reviewed by: imp MFC after: 1 week Sponsored by: iXsystems, Inc. Differential Revision: https://reviews.freebsd.org/D26854 Modified: head/sys/kern/kern_sysctl.c Modified: head/sys/kern/kern_sysctl.c ============================================================================== --- head/sys/kern/kern_sysctl.c Sat Oct 24 14:39:17 2020 (r367002) +++ head/sys/kern/kern_sysctl.c Sat Oct 24 14:46:38 2020 (r367003) @@ -1100,9 +1100,13 @@ sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, ""); +/* + * Walk the sysctl subtree at lsp until we find the given name, + * and return the next name in order by oid_number. + */ static int sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, - int *next, int *len, int level, struct sysctl_oid **oidpp, bool honor_skip) + int *next, int *len, int level, bool honor_skip) { struct sysctl_oid *oidp; @@ -1110,7 +1114,6 @@ sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *len = level; SLIST_FOREACH(oidp, lsp, oid_link) { *next = oidp->oid_number; - *oidpp = oidp; if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0) continue; @@ -1118,46 +1121,77 @@ sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0) continue; - if (!namelen) { + if (namelen == 0) { + /* + * We have reached a node with a full name match and are + * looking for the next oid in its children. + * + * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it + * has a handler) and move on to the children. + */ if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) return (0); if (oidp->oid_handler) - /* We really should call the handler here...*/ return (0); lsp = SYSCTL_CHILDREN(oidp); - if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, - len, level+1, oidpp, honor_skip)) + if (!sysctl_sysctl_next_ls(lsp, NULL, 0, next + 1, len, + level + 1, honor_skip)) return (0); + /* + * There were no useable children in this node. + * Continue searching for the next oid at this level. + */ goto emptynode; } + /* + * No match yet. Continue seeking the given name. + * + * We are iterating in order by oid_number, so skip oids lower + * than the one we are looking for. + * + * When the current oid_number is higher than the one we seek, + * that means we have reached the next oid in the sequence and + * should return it. + * + * If the oid_number matches the name at this level then we + * have to find a node to continue searching at the next level. + */ if (oidp->oid_number < *name) continue; - if (oidp->oid_number > *name) { + /* + * We have reached the next oid. + * + * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it + * has a handler) and move on to the children. + */ if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) return (0); if (oidp->oid_handler) return (0); lsp = SYSCTL_CHILDREN(oidp); - if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, - next+1, len, level+1, oidpp, honor_skip)) + if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1, + next + 1, len, level + 1, honor_skip)) return (0); goto next; } if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) continue; - if (oidp->oid_handler) continue; - lsp = SYSCTL_CHILDREN(oidp); - if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, - len, level+1, oidpp, honor_skip)) + if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1, + next + 1, len, level + 1, honor_skip)) return (0); next: + /* + * There were no useable children in this node. + * Continue searching for the next oid at the root level. + */ namelen = 1; emptynode: + /* Reset len in case a failed recursive call changed it. */ *len = level; } return (ENOENT); @@ -1169,13 +1203,12 @@ sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) int *name = (int *) arg1; u_int namelen = arg2; int len, error; - struct sysctl_oid *oid; struct sysctl_oid_list *lsp = &sysctl__children; struct rm_priotracker tracker; int next[CTL_MAXNAME]; SYSCTL_RLOCK(&tracker); - error = sysctl_sysctl_next_ls(lsp, name, namelen, next, &len, 1, &oid, + error = sysctl_sysctl_next_ls(lsp, name, namelen, next, &len, 1, oidp->oid_number == CTL_SYSCTL_NEXT); SYSCTL_RUNLOCK(&tracker); if (error)