Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.165
      1 /*	$NetBSD: kern_sysctl.c,v 1.165 2004/03/24 17:40:02 atatat Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Brown.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1982, 1986, 1989, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Mike Karels at Berkeley Software Design, Inc.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)kern_sysctl.c	8.9 (Berkeley) 5/20/95
     71  */
     72 
     73 /*
     74  * sysctl system call.
     75  */
     76 
     77 #include <sys/cdefs.h>
     78 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.165 2004/03/24 17:40:02 atatat Exp $");
     79 
     80 #include "opt_defcorename.h"
     81 #include "opt_insecure.h"
     82 #include "ksyms.h"
     83 
     84 #include <sys/param.h>
     85 #define __COMPAT_SYSCTL
     86 #include <sys/sysctl.h>
     87 #include <sys/systm.h>
     88 #include <sys/buf.h>
     89 #include <sys/ksyms.h>
     90 #include <sys/malloc.h>
     91 #include <sys/mount.h>
     92 #include <sys/sa.h>
     93 #include <sys/syscallargs.h>
     94 #include <machine/stdarg.h>
     95 
     96 MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures");
     97 MALLOC_DEFINE(M_SYSCTLDATA, "sysctldata", "misc sysctl data");
     98 
     99 static int sysctl_mmap(SYSCTLFN_RWPROTO);
    100 static int sysctl_alloc(struct sysctlnode *, int);
    101 static int sysctl_realloc(struct sysctlnode *);
    102 
    103 static int sysctl_cvt_in(struct lwp *, int *, const void *, size_t,
    104 			 struct sysctlnode *);
    105 static int sysctl_cvt_out(struct lwp *, int, const struct sysctlnode *,
    106 			  void *, size_t, size_t *);
    107 
    108 static int sysctl_log_add(struct sysctllog **, struct sysctlnode *);
    109 static int sysctl_log_realloc(struct sysctllog *);
    110 
    111 struct sysctllog {
    112 	struct sysctlnode *log_root;
    113 	int *log_num;
    114 	int log_size, log_left;
    115 };
    116 
    117 /*
    118  * the "root" of the new sysctl tree
    119  */
    120 static struct sysctlnode sysctl_root = {
    121 	.sysctl_flags = SYSCTL_VERSION|
    122 	    CTLFLAG_ROOT|CTLFLAG_READWRITE|
    123 	    CTLTYPE_NODE,
    124 	.sysctl_num = 0,
    125 	.sysctl_size = sizeof(struct sysctlnode),
    126 	.sysctl_name = "(root)",
    127 };
    128 
    129 /*
    130  * link set of functions that add nodes at boot time (see also
    131  * sysctl_buildtree())
    132  */
    133 __link_set_decl(sysctl_funcs, sysctl_setup_func);
    134 
    135 /*
    136  * The `sysctl_lock' is intended to serialize access to the sysctl
    137  * tree.  Given that it is now (a) dynamic, and (b) most consumers of
    138  * sysctl are going to be copying data out, the old `sysctl_memlock'
    139  * has been `upgraded' to simply guard the whole tree.
    140  *
    141  * The two new data here are to keep track of the locked chunk of
    142  * memory, if there is one, so that it can be released more easily
    143  * from anywhere.
    144  */
    145 struct lock sysctl_treelock;
    146 caddr_t sysctl_memaddr;
    147 size_t sysctl_memsize;
    148 
    149 /*
    150  * Attributes stored in the kernel.
    151  */
    152 char hostname[MAXHOSTNAMELEN];
    153 int hostnamelen;
    154 
    155 char domainname[MAXHOSTNAMELEN];
    156 int domainnamelen;
    157 
    158 long hostid;
    159 
    160 #ifdef INSECURE
    161 int securelevel = -1;
    162 #else
    163 int securelevel = 0;
    164 #endif
    165 
    166 #ifndef DEFCORENAME
    167 #define	DEFCORENAME	"%n.core"
    168 #endif
    169 char defcorename[MAXPATHLEN] = DEFCORENAME;
    170 
    171 /*
    172  * ********************************************************************
    173  * Section 0: Some simple glue
    174  * ********************************************************************
    175  * By wrapping copyin(), copyout(), and copyinstr() like this, we can
    176  * stop caring about who's calling us and simplify some code a bunch.
    177  * ********************************************************************
    178  */
    179 static inline int
    180 sysctl_copyin(const struct lwp *l, const void *uaddr, void *kaddr, size_t len)
    181 {
    182 
    183 	if (l != NULL)
    184 		return (copyin(uaddr, kaddr, len));
    185 	else
    186 		return (kcopy(uaddr, kaddr, len));
    187 }
    188 
    189 static inline int
    190 sysctl_copyout(const struct lwp *l, const void *kaddr, void *uaddr, size_t len)
    191 {
    192 
    193 	if (l != NULL)
    194 		return (copyout(kaddr, uaddr, len));
    195 	else
    196 		return (kcopy(kaddr, uaddr, len));
    197 }
    198 
    199 static inline int
    200 sysctl_copyinstr(const struct lwp *l, const void *uaddr, void *kaddr,
    201 		 size_t len, size_t *done)
    202 {
    203 
    204 	if (l != NULL)
    205 		return (copyinstr(uaddr, kaddr, len, done));
    206 	else
    207 		return (copystr(uaddr, kaddr, len, done));
    208 }
    209 
    210 /*
    211  * ********************************************************************
    212  * Initialize sysctl subsystem.
    213  * ********************************************************************
    214  */
    215 void
    216 sysctl_init(void)
    217 {
    218 	sysctl_setup_func **sysctl_setup, f;
    219 
    220 	lockinit(&sysctl_treelock, PRIBIO|PCATCH, "sysctl", 0, 0);
    221 
    222 	/*
    223 	 * dynamic mib numbers start here
    224 	 */
    225 	sysctl_root.sysctl_num = CREATE_BASE;
    226 
    227         __link_set_foreach(sysctl_setup, sysctl_funcs) {
    228 		/*
    229 		 * XXX - why do i have to coerce the pointers like this?
    230 		 */
    231 		f = (void*)*sysctl_setup;
    232 		(*f)(NULL);
    233 	}
    234 
    235 	/*
    236 	 * setting this means no more permanent nodes can be added,
    237 	 * trees that claim to be readonly at the root now are, and if
    238 	 * the main tree is readonly, *everything* is.
    239 	 */
    240 	sysctl_root.sysctl_flags |= CTLFLAG_PERMANENT;
    241 
    242 }
    243 
    244 /*
    245  * ********************************************************************
    246  * The main native sysctl system call itself.
    247  * ********************************************************************
    248  */
    249 int
    250 sys___sysctl(struct lwp *l, void *v, register_t *retval)
    251 {
    252 	struct sys___sysctl_args /* {
    253 		syscallarg(int *) name;
    254 		syscallarg(u_int) namelen;
    255 		syscallarg(void *) old;
    256 		syscallarg(size_t *) oldlenp;
    257 		syscallarg(void *) new;
    258 		syscallarg(size_t) newlen;
    259 	} */ *uap = v;
    260 	int error, nerror, name[CTL_MAXNAME];
    261 	size_t oldlen, savelen, *oldlenp;
    262 
    263 	/*
    264 	 * get oldlen
    265 	 */
    266 	oldlen = 0;
    267 	oldlenp = SCARG(uap, oldlenp);
    268 	if (oldlenp != NULL) {
    269 		error = copyin(oldlenp, &oldlen, sizeof(oldlen));
    270 		if (error)
    271 			return (error);
    272 	}
    273 	savelen = oldlen;
    274 
    275 	/*
    276 	 * top-level sysctl names may or may not be non-terminal, but
    277 	 * we don't care
    278 	 */
    279 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
    280 		return (EINVAL);
    281 	error = copyin(SCARG(uap, name), &name,
    282 		       SCARG(uap, namelen) * sizeof(int));
    283 	if (error)
    284 		return (error);
    285 
    286 	/*
    287 	 * wire old so that copyout() is less likely to fail?
    288 	 */
    289 	error = sysctl_lock(l, SCARG(uap, old), savelen);
    290 	if (error)
    291 		return (error);
    292 
    293 	/*
    294 	 * do sysctl work (NULL means main built-in default tree)
    295 	 */
    296 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
    297 				SCARG(uap, old), &oldlen,
    298 				SCARG(uap, new), SCARG(uap, newlen),
    299 				&name[0], l, NULL);
    300 
    301 	/*
    302 	 * release the sysctl lock
    303 	 */
    304 	sysctl_unlock(l);
    305 
    306 	/*
    307 	 * set caller's oldlen to new value even in the face of an
    308 	 * error (if this gets an error and they didn't have one, they
    309 	 * get this one)
    310 	 */
    311 	if (oldlenp) {
    312 		nerror = copyout(&oldlen, oldlenp, sizeof(oldlen));
    313 		if (error == 0)
    314 			error = nerror;
    315 	}
    316 
    317 	/*
    318 	 * if the only problem is that we weren't given enough space,
    319 	 * that's an ENOMEM error
    320 	 */
    321 	if (error == 0 && SCARG(uap, old) != NULL && savelen < oldlen)
    322 		error = ENOMEM;
    323 
    324 	return (error);
    325 }
    326 
    327 /*
    328  * ********************************************************************
    329  * Section 1: How the tree is used
    330  * ********************************************************************
    331  * Implementations of sysctl for emulations should typically need only
    332  * these three functions in this order: lock the tree, dispatch
    333  * request into it, unlock the tree.
    334  * ********************************************************************
    335  */
    336 int
    337 sysctl_lock(struct lwp *l, void *oldp, size_t savelen)
    338 {
    339 	int error = 0;
    340 
    341 	error = lockmgr(&sysctl_treelock, LK_EXCLUSIVE, NULL);
    342 	if (error)
    343 		return (error);
    344 
    345 	if (l != NULL && oldp != NULL && savelen) {
    346 		error = uvm_vslock(l->l_proc, oldp, savelen, VM_PROT_WRITE);
    347 		if (error) {
    348 			(void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
    349 			return (error);
    350 		}
    351 		sysctl_memaddr = oldp;
    352 		sysctl_memsize = savelen;
    353 	}
    354 
    355 	return (0);
    356 }
    357 
    358 /*
    359  * ********************************************************************
    360  * the main sysctl dispatch routine.  scans the given tree and picks a
    361  * function to call based on what it finds.
    362  * ********************************************************************
    363  */
    364 int
    365 sysctl_dispatch(SYSCTLFN_RWARGS)
    366 {
    367 	int error;
    368 	sysctlfn fn;
    369 	int ni;
    370 
    371 	if (rnode && SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
    372 		printf("sysctl_dispatch: rnode %p wrong version\n", rnode);
    373 		return (EINVAL);
    374 	}
    375 
    376 	fn = NULL;
    377 	error = sysctl_locate(l, name, namelen, &rnode, &ni);
    378 
    379 	/*
    380 	 * the node we ended up at has a function, so call it.  it can
    381 	 * hand off to query or create if it wants to.
    382 	 */
    383 	if (rnode->sysctl_func != NULL)
    384 		fn = rnode->sysctl_func;
    385 
    386 	/*
    387 	 * we found the node they were looking for, so do a lookup.
    388 	 */
    389 	else if (error == 0)
    390 		fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */
    391 
    392 	/*
    393 	 * prospective parent node found, but the terminal node was
    394 	 * not.  generic operations associate with the parent.
    395 	 */
    396 	else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) {
    397 		switch (name[ni]) {
    398 		case CTL_QUERY:
    399 			fn = sysctl_query;
    400 			break;
    401 		case CTL_CREATE:
    402 #if NKSYMS > 0
    403 		case CTL_CREATESYM:
    404 #endif /* NKSYMS > 0 */
    405 			fn = (sysctlfn)sysctl_create; /* we own the rnode */
    406 			break;
    407 		case CTL_DESTROY:
    408 			fn = (sysctlfn)sysctl_destroy; /* we own the rnode */
    409 			break;
    410 		case CTL_MMAP:
    411 			fn = (sysctlfn)sysctl_mmap; /* we own the rnode */
    412 			break;
    413 		case CTL_DESCRIBE:
    414 			fn = sysctl_describe;
    415 			break;
    416 		default:
    417 			error = EOPNOTSUPP;
    418 			break;
    419 		}
    420 	}
    421 
    422 	/*
    423 	 * after all of that, maybe we found someone who knows how to
    424 	 * get us what we want?
    425 	 */
    426 	if (fn != NULL)
    427 		error = (*fn)(name + ni, namelen - ni, oldp, oldlenp,
    428 			      newp, newlen, name, l, rnode);
    429 
    430 	else if (error == 0)
    431 		error = EOPNOTSUPP;
    432 
    433 	return (error);
    434 }
    435 
    436 /*
    437  * ********************************************************************
    438  * Releases the tree lock.  Note that if uvm_vslock() was called when
    439  * the lock was taken, we release that memory now.  By keeping track
    440  * of where and how much by ourselves, the lock can be released much
    441  * more easily from anywhere.
    442  * ********************************************************************
    443  */
    444 void
    445 sysctl_unlock(struct lwp *l)
    446 {
    447 
    448 	if (l != NULL && sysctl_memsize != 0) {
    449 		uvm_vsunlock(l->l_proc, sysctl_memaddr, sysctl_memsize);
    450 		sysctl_memsize = 0;
    451 	}
    452 
    453 	(void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
    454 }
    455 
    456 /*
    457  * ********************************************************************
    458  * Section 2: The main tree interfaces
    459  * ********************************************************************
    460  * This is how sysctl_dispatch() does its work, and you can too, by
    461  * calling these routines from helpers (though typically only
    462  * sysctl_lookup() will be used).  The tree MUST BE LOCKED when these
    463  * are called.
    464  * ********************************************************************
    465  */
    466 
    467 /*
    468  * sysctl_locate -- Finds the node matching the given mib under the
    469  * given tree (via rv).  If no tree is given, we fall back to the
    470  * native tree.  The current process (via l) is used for access
    471  * control on the tree (some nodes may be traversable only by root) and
    472  * on return, nip will show how many numbers in the mib were consumed.
    473  */
    474 int
    475 sysctl_locate(struct lwp *l, const int *name, u_int namelen,
    476 	      struct sysctlnode **rnode, int *nip)
    477 {
    478 	struct sysctlnode *node, *pnode;
    479 	int tn, si, ni, error, alias;
    480 
    481 	/*
    482 	 * basic checks and setup
    483 	 */
    484 	if (*rnode == NULL)
    485 		*rnode = &sysctl_root;
    486 	if (nip)
    487 		*nip = 0;
    488 	if (namelen < 0)
    489 		return (EINVAL);
    490 	if (namelen == 0)
    491 		return (0);
    492 
    493 	/*
    494 	 * search starts from "root"
    495 	 */
    496 	pnode = *rnode;
    497 	if (SYSCTL_VERS(pnode->sysctl_flags) != SYSCTL_VERSION) {
    498 		printf("sysctl_locate: pnode %p wrong version\n", pnode);
    499 		return (EINVAL);
    500 	}
    501 	node = pnode->sysctl_child;
    502 	error = 0;
    503 
    504 	/*
    505 	 * scan for node to which new node should be attached
    506 	 */
    507 	for (ni = 0; ni < namelen; ni++) {
    508 		/*
    509 		 * walked off bottom of tree
    510 		 */
    511 		if (node == NULL) {
    512 			if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
    513 				error = ENOENT;
    514 			else
    515 				error = ENOTDIR;
    516 			break;
    517 		}
    518 		/*
    519 		 * can anyone traverse this node or only root?
    520 		 */
    521 		if (l != NULL && (pnode->sysctl_flags & CTLFLAG_PRIVATE) &&
    522 		    (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag))
    523 		    != 0)
    524 			return (error);
    525 		/*
    526 		 * find a child node with the right number
    527 		 */
    528 		tn = name[ni];
    529 		alias = 0;
    530 
    531 		si = 0;
    532 		/*
    533 		 * Note: ANYNUMBER only matches positive integers.
    534 		 * Since ANYNUMBER is only permitted on single-node
    535 		 * sub-trees (eg proc), check before the loop and skip
    536 		 * it if we can.
    537 		 */
    538 		if ((node[si].sysctl_flags & CTLFLAG_ANYNUMBER) && (tn >= 0))
    539 			goto foundit;
    540 		for (; si < pnode->sysctl_clen; si++) {
    541 			if (node[si].sysctl_num == tn) {
    542 				if (node[si].sysctl_flags & CTLFLAG_ALIAS) {
    543 					if (alias++ == 4)
    544 						break;
    545 					else {
    546 						tn = node[si].sysctl_alias;
    547 						si = -1;
    548 					}
    549 				}
    550 				else
    551 					goto foundit;
    552 			}
    553 		}
    554 		/*
    555 		 * if we ran off the end, it obviously doesn't exist
    556 		 */
    557 		error = ENOENT;
    558 		break;
    559 
    560 		/*
    561 		 * so far so good, move on down the line
    562 		 */
    563 	  foundit:
    564 		pnode = &node[si];
    565 		if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
    566 			node = node[si].sysctl_child;
    567 		else
    568 			node = NULL;
    569 	}
    570 
    571 	*rnode = pnode;
    572 	if (nip)
    573 		*nip = ni;
    574 
    575 	return (error);
    576 }
    577 
    578 /*
    579  * sysctl_query -- The auto-discovery engine.  Copies out the structs
    580  * describing nodes under the given node and handles overlay trees.
    581  */
    582 int
    583 sysctl_query(SYSCTLFN_ARGS)
    584 {
    585 	int error, ni, elim, v;
    586 	size_t out, left, t;
    587 	struct sysctlnode *enode, *onode, qnode;
    588 
    589 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
    590 		printf("sysctl_query: rnode %p wrong version\n", rnode);
    591 		return (EINVAL);
    592 	}
    593 
    594 	if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
    595 		return (ENOTDIR);
    596 	if (namelen != 1 || name[0] != CTL_QUERY)
    597 		return (EINVAL);
    598 
    599 	error = 0;
    600 	out = 0;
    601 	left = *oldlenp;
    602 	elim = 0;
    603 	enode = NULL;
    604 
    605 	/*
    606 	 * translate the given request to a current node
    607 	 */
    608 	error = sysctl_cvt_in(l, &v, newp, newlen, &qnode);
    609 	if (error)
    610 		return (error);
    611 
    612 	/*
    613 	 * if the request specifies a version, check it
    614 	 */
    615 	if (qnode.sysctl_ver != 0) {
    616 		enode = (struct sysctlnode *)rnode; /* discard const */
    617 		if (qnode.sysctl_ver != enode->sysctl_ver &&
    618 		    qnode.sysctl_ver != sysctl_rootof(enode)->sysctl_ver)
    619 			return (EINVAL);
    620 	}
    621 
    622 	/*
    623 	 * process has overlay tree
    624 	 */
    625 	if (l && l->l_proc->p_emul->e_sysctlovly) {
    626 		enode = l->l_proc->p_emul->e_sysctlovly;
    627 		elim = (name - oname);
    628 		error = sysctl_locate(l, oname, elim, &enode, NULL);
    629 		if (error == 0) {
    630 			/* ah, found parent in overlay */
    631 			elim = enode->sysctl_clen;
    632 			enode = enode->sysctl_child;
    633 		}
    634 		else {
    635 			error = 0;
    636 			elim = 0;
    637 			enode = NULL;
    638 		}
    639 	}
    640 
    641 	for (ni = 0; ni < rnode->sysctl_clen; ni++) {
    642 		onode = &rnode->sysctl_child[ni];
    643 		if (enode && enode->sysctl_num == onode->sysctl_num) {
    644 			if (SYSCTL_TYPE(enode->sysctl_flags) != CTLTYPE_NODE)
    645 				onode = enode;
    646 			if (--elim > 0)
    647 				enode++;
    648 			else
    649 				enode = NULL;
    650 		}
    651 		error = sysctl_cvt_out(l, v, onode, oldp, left, &t);
    652 		if (error)
    653 			return (error);
    654 		if (oldp != NULL)
    655 			oldp = (char*)oldp + t;
    656 		out += t;
    657 		left -= MIN(left, t);
    658 	}
    659 
    660 	/*
    661 	 * overlay trees *MUST* be entirely consumed
    662 	 */
    663 	KASSERT(enode == NULL);
    664 
    665 	*oldlenp = out;
    666 
    667 	return (error);
    668 }
    669 
    670 #ifdef SYSCTL_DEBUG_CREATE
    671 #undef sysctl_create
    672 #endif /* SYSCTL_DEBUG_CREATE */
    673 
    674 /*
    675  * sysctl_create -- Adds a node (the description of which is taken
    676  * from newp) to the tree, returning a copy of it in the space pointed
    677  * to by oldp.  In the event that the requested slot is already taken
    678  * (either by name or by number), the offending node is returned
    679  * instead.  Yes, this is complex, but we want to make sure everything
    680  * is proper.
    681  */
    682 int
    683 sysctl_create(SYSCTLFN_RWARGS)
    684 {
    685 	struct sysctlnode nnode, *node, *pnode;
    686 	int error, ni, at, nm, type, sz, flags, rw, anum, v;
    687 	void *own;
    688 
    689 	error = 0;
    690 	own = NULL;
    691 	anum = -1;
    692 
    693 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
    694 		printf("sysctl_create: rnode %p wrong version\n", rnode);
    695 		return (EINVAL);
    696 	}
    697 
    698 	if (namelen != 1 || (name[namelen - 1] != CTL_CREATE
    699 #if NKSYMS > 0
    700 			     && name[namelen - 1] != CTL_CREATESYM
    701 #endif /* NKSYMS > 0 */
    702 			     ))
    703 		return (EINVAL);
    704 
    705 	/*
    706 	 * processes can only add nodes at securelevel 0, must be
    707 	 * root, and can't add nodes to a parent that's not writeable
    708 	 */
    709 	if (l != NULL) {
    710 #ifndef SYSCTL_DISALLOW_CREATE
    711 		if (securelevel > 0)
    712 			return (EPERM);
    713 		error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
    714 		if (error)
    715 			return (error);
    716 		if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
    717 #endif /* SYSCTL_DISALLOW_CREATE */
    718 			return (EPERM);
    719 	}
    720 
    721 	/*
    722 	 * nothing can add a node if:
    723 	 * we've finished initial set up and
    724 	 * the tree itself is not writeable or
    725 	 * the entire sysctl system is not writeable
    726 	 */
    727 	if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) &&
    728 	    (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
    729 	     !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE)))
    730 		return (EPERM);
    731 
    732 	/*
    733 	 * it must be a "node", not a "int" or something
    734 	 */
    735 	if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
    736 		return (ENOTDIR);
    737 	pnode = rnode;
    738 
    739 	if (newp == NULL)
    740 		return (EINVAL);
    741 	error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
    742 	if (error)
    743 		return (error);
    744 
    745 	/*
    746 	 * nodes passed in don't *have* parents
    747 	 */
    748 	if (nnode.sysctl_parent != NULL)
    749 		return (EINVAL);
    750 
    751 	/*
    752 	 * if we are indeed adding it, it should be a "good" name and
    753 	 * number
    754 	 */
    755 	nm = nnode.sysctl_num;
    756 #if NKSYMS > 0
    757 	if (nm == CTL_CREATESYM)
    758 		nm = CTL_CREATE;
    759 #endif /* NKSYMS > 0 */
    760 	if (nm < 0 && nm != CTL_CREATE)
    761 		return (EINVAL);
    762 	sz = 0;
    763 
    764 	/*
    765 	 * the name can't start with a digit
    766 	 */
    767 	if (nnode.sysctl_name[sz] >= '0' &&
    768 	    nnode.sysctl_name[sz] <= '9')
    769 		return (EINVAL);
    770 
    771 	/*
    772 	 * the name must be only alphanumerics or - or _, longer than
    773 	 * 0 bytes and less that SYSCTL_NAMELEN
    774 	 */
    775 	while (sz < SYSCTL_NAMELEN && nnode.sysctl_name[sz] != '\0') {
    776 		if ((nnode.sysctl_name[sz] >= '0' &&
    777 		     nnode.sysctl_name[sz] <= '9') ||
    778 		    (nnode.sysctl_name[sz] >= 'A' &&
    779 		     nnode.sysctl_name[sz] <= 'Z') ||
    780 		    (nnode.sysctl_name[sz] >= 'a' &&
    781 		     nnode.sysctl_name[sz] <= 'z') ||
    782 		    nnode.sysctl_name[sz] == '-' ||
    783 		    nnode.sysctl_name[sz] == '_')
    784 			sz++;
    785 		else
    786 			return (EINVAL);
    787 	}
    788 	if (sz == 0 || sz == SYSCTL_NAMELEN)
    789 		return (EINVAL);
    790 
    791 	/*
    792 	 * various checks revolve around size vs type, etc
    793 	 */
    794 	type = SYSCTL_TYPE(nnode.sysctl_flags);
    795 	flags = SYSCTL_FLAGS(nnode.sysctl_flags);
    796 	rw = (flags & CTLFLAG_READWRITE) ? B_WRITE : B_READ;
    797 	sz = nnode.sysctl_size;
    798 
    799 	/*
    800 	 * find out if there's a collision, and if so, let the caller
    801 	 * know what they collided with
    802 	 */
    803 	node = pnode->sysctl_child;
    804 	if (((flags & CTLFLAG_ANYNUMBER) && node) ||
    805 	    (node && node->sysctl_flags & CTLFLAG_ANYNUMBER))
    806 		return (EINVAL);
    807 	for (ni = at = 0; ni < pnode->sysctl_clen; ni++) {
    808 		if (nm == node[ni].sysctl_num ||
    809 		    strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) {
    810 			/*
    811 			 * ignore error here, since we
    812 			 * are already fixed on EEXIST
    813 			 */
    814 			(void)sysctl_cvt_out(l, v, &node[ni], oldp,
    815 					     *oldlenp, oldlenp);
    816 			return (EEXIST);
    817 		}
    818 		if (nm > node[ni].sysctl_num)
    819 			at++;
    820 	}
    821 
    822 	/*
    823 	 * use sysctl_ver to add to the tree iff it hasn't changed
    824 	 */
    825 	if (nnode.sysctl_ver != 0) {
    826 		/*
    827 		 * a specified value must match either the parent
    828 		 * node's version or the root node's version
    829 		 */
    830 		if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver &&
    831 		    nnode.sysctl_ver != rnode->sysctl_ver) {
    832 			return (EINVAL);
    833 		}
    834 	}
    835 
    836 	/*
    837 	 * only the kernel can assign functions to entries
    838 	 */
    839 	if (l != NULL && nnode.sysctl_func != NULL)
    840 		return (EPERM);
    841 
    842 	/*
    843 	 * only the kernel can create permanent entries, and only then
    844 	 * before the kernel is finished setting itself up
    845 	 */
    846 	if (l != NULL && (flags & ~SYSCTL_USERFLAGS))
    847 		return (EPERM);
    848 	if ((flags & CTLFLAG_PERMANENT) &
    849 	    (sysctl_root.sysctl_flags & CTLFLAG_PERMANENT))
    850 		return (EPERM);
    851 	if ((flags & (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) ==
    852 	    (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE))
    853 		return (EINVAL);
    854 	if ((flags & CTLFLAG_IMMEDIATE) &&
    855 	    type != CTLTYPE_INT && type != CTLTYPE_QUAD)
    856 		return (EINVAL);
    857 
    858 	/*
    859 	 * check size, or set it if unset and we can figure it out.
    860 	 * kernel created nodes are allowed to have a function instead
    861 	 * of a size (or a data pointer).
    862 	 */
    863 	switch (type) {
    864 	case CTLTYPE_NODE:
    865 		/*
    866 		 * only *i* can assert the size of a node
    867 		 */
    868 		if (flags & CTLFLAG_ALIAS) {
    869 			anum = nnode.sysctl_alias;
    870 			if (anum < 0)
    871 				return (EINVAL);
    872 			nnode.sysctl_alias = 0;
    873 		}
    874 		if (sz != 0 || nnode.sysctl_data != NULL)
    875 			return (EINVAL);
    876 		if (nnode.sysctl_csize != 0 ||
    877 		    nnode.sysctl_clen != 0 ||
    878 		    nnode.sysctl_child != 0)
    879 			return (EINVAL);
    880 		if (flags & CTLFLAG_OWNDATA)
    881 			return (EINVAL);
    882 		sz = sizeof(struct sysctlnode);
    883 		break;
    884 	case CTLTYPE_INT:
    885 		/*
    886 		 * since an int is an int, if the size is not given or
    887 		 * is wrong, we can "int-uit" it.
    888 		 */
    889 		if (sz != 0 && sz != sizeof(int))
    890 			return (EINVAL);
    891 		sz = sizeof(int);
    892 		break;
    893 	case CTLTYPE_STRING:
    894 		/*
    895 		 * strings are a little more tricky
    896 		 */
    897 		if (sz == 0) {
    898 			if (l == NULL) {
    899 				if (nnode.sysctl_func == NULL) {
    900 					if (nnode.sysctl_data == NULL)
    901 						return (EINVAL);
    902 					else
    903 						sz = strlen(nnode.sysctl_data) +
    904 						    1;
    905 				}
    906 			}
    907 			else if (nnode.sysctl_data == NULL &&
    908 				 flags & CTLFLAG_OWNDATA) {
    909 				return (EINVAL);
    910 			}
    911 			else {
    912 				char v[PAGE_SIZE], *e;
    913 				size_t s;
    914 
    915 				/*
    916 				 * we want a rough idea of what the
    917 				 * size is now
    918 				 */
    919 				e = nnode.sysctl_data;
    920 				do {
    921 					error = copyinstr(e, &v[0], sizeof(v),
    922 							  &s);
    923 					if (error) {
    924 						if (error != ENAMETOOLONG)
    925 							return (error);
    926 						e += PAGE_SIZE;
    927 						if ((e - 32 * PAGE_SIZE) >
    928 						    (char*)nnode.sysctl_data)
    929 							return (ERANGE);
    930 					}
    931 				} while (error != 0);
    932 				sz = s + (e - (char*)nnode.sysctl_data);
    933 			}
    934 		}
    935 		break;
    936 	case CTLTYPE_QUAD:
    937 		if (sz != 0 && sz != sizeof(u_quad_t))
    938 			return (EINVAL);
    939 		sz = sizeof(u_quad_t);
    940 		break;
    941 	case CTLTYPE_STRUCT:
    942 		if (sz == 0) {
    943 			if (l != NULL || nnode.sysctl_func == NULL)
    944 				return (EINVAL);
    945 			if (flags & CTLFLAG_OWNDATA)
    946 				return (EINVAL);
    947 		}
    948 		break;
    949 	default:
    950 		return (EINVAL);
    951 	}
    952 
    953 	/*
    954 	 * at this point, if sz is zero, we *must* have a
    955 	 * function to go with it and we can't own it.
    956 	 */
    957 
    958 	/*
    959 	 *  l  ptr own
    960 	 *  0   0   0  -> EINVAL (if no func)
    961 	 *  0   0   1  -> own
    962 	 *  0   1   0  -> kptr
    963 	 *  0   1   1  -> kptr
    964 	 *  1   0   0  -> EINVAL
    965 	 *  1   0   1  -> own
    966 	 *  1   1   0  -> kptr, no own (fault on lookup)
    967 	 *  1   1   1  -> uptr, own
    968 	 */
    969 	if (type != CTLTYPE_NODE) {
    970 		if (sz != 0) {
    971 			if (flags & CTLFLAG_OWNDATA) {
    972 				own = malloc(sz, M_SYSCTLDATA,
    973 					     M_WAITOK|M_CANFAIL);
    974 				if (nnode.sysctl_data == NULL)
    975 					memset(own, 0, sz);
    976 				else {
    977 					error = sysctl_copyin(l,
    978 					    nnode.sysctl_data, own, sz);
    979 					if (error != 0) {
    980 						FREE(own, M_SYSCTLDATA);
    981 						return (error);
    982 					}
    983 				}
    984 			}
    985 			else if ((nnode.sysctl_data != NULL) &&
    986 				 !(flags & CTLFLAG_IMMEDIATE)) {
    987 #if NKSYMS > 0
    988 				if (name[namelen - 1] == CTL_CREATESYM) {
    989 					char symname[128]; /* XXX enough? */
    990 					u_long symaddr;
    991 					size_t symlen;
    992 
    993 					error = sysctl_copyinstr(l,
    994 					    nnode.sysctl_data, symname,
    995 					    sizeof(symname), &symlen);
    996 					if (error)
    997 						return (error);
    998 					error = ksyms_getval_from_kernel(NULL,
    999 					    symname, &symaddr, KSYMS_EXTERN);
   1000 					if (error)
   1001 						return (error); /* EINVAL? */
   1002 					nnode.sysctl_data = (void*)symaddr;
   1003 				}
   1004 #endif /* NKSYMS > 0 */
   1005 				/*
   1006 				 * Ideally, we'd like to verify here
   1007 				 * that this address is acceptable,
   1008 				 * but...
   1009 				 *
   1010 				 * - it might be valid now, only to
   1011 				 *   become invalid later
   1012 				 *
   1013 				 * - it might be invalid only for the
   1014 				 *   moment and valid later
   1015 				 *
   1016 				 * - or something else.
   1017 				 *
   1018 				 * Since we can't get a good answer,
   1019 				 * we'll just accept the address as
   1020 				 * given, and fault on individual
   1021 				 * lookups.
   1022 				 */
   1023 			}
   1024 		}
   1025 		else if (nnode.sysctl_func == NULL)
   1026 			return (EINVAL);
   1027 	}
   1028 
   1029 	/*
   1030 	 * a process can't assign a function to a node, and the kernel
   1031 	 * can't create a node that has no function or data.
   1032 	 * (XXX somewhat redundant check)
   1033 	 */
   1034 	if (l != NULL || nnode.sysctl_func == NULL) {
   1035 		if (type != CTLTYPE_NODE &&
   1036 		    nnode.sysctl_data == NULL &&
   1037 		    !(flags & CTLFLAG_IMMEDIATE) &&
   1038 		    own == NULL)
   1039 			return (EINVAL);
   1040 	}
   1041 
   1042 #ifdef SYSCTL_DISALLOW_KWRITE
   1043 	/*
   1044 	 * a process can't create a writable node unless it refers to
   1045 	 * new data.
   1046 	 */
   1047 	if (l != NULL && own == NULL && type != CTLTYPE_NODE &&
   1048 	    (flags & CTLFLAG_READWRITE) != CTLFLAG_READONLY &&
   1049 	    !(flags & CTLFLAG_IMMEDIATE))
   1050 		return (EPERM);
   1051 #endif /* SYSCTL_DISALLOW_KWRITE */
   1052 
   1053 	/*
   1054 	 * make sure there's somewhere to put the new stuff.
   1055 	 */
   1056 	if (pnode->sysctl_child == NULL) {
   1057 		if (flags & CTLFLAG_ANYNUMBER)
   1058 			error = sysctl_alloc(pnode, 1);
   1059 		else
   1060 			error = sysctl_alloc(pnode, 0);
   1061 		if (error)
   1062 			return (error);
   1063 	}
   1064 	node = pnode->sysctl_child;
   1065 
   1066 	/*
   1067 	 * no collisions, so pick a good dynamic number if we need to.
   1068 	 */
   1069 	if (nm == CTL_CREATE) {
   1070 		nm = ++sysctl_root.sysctl_num;
   1071 		for (ni = 0; ni < pnode->sysctl_clen; ni++) {
   1072 			if (nm == node[ni].sysctl_num) {
   1073 				nm++;
   1074 				ni = -1;
   1075 			}
   1076 			else if (nm > node[ni].sysctl_num)
   1077 				at = ni + 1;
   1078 		}
   1079 	}
   1080 
   1081 	/*
   1082 	 * oops...ran out of space
   1083 	 */
   1084 	if (pnode->sysctl_clen == pnode->sysctl_csize) {
   1085 		error = sysctl_realloc(pnode);
   1086 		if (error)
   1087 			return (error);
   1088 		node = pnode->sysctl_child;
   1089 	}
   1090 
   1091 	/*
   1092 	 * insert new node data
   1093 	 */
   1094 	if (at < pnode->sysctl_clen) {
   1095 		int t;
   1096 
   1097 		/*
   1098 		 * move the nodes that should come after the new one
   1099 		 */
   1100 		memmove(&node[at + 1], &node[at],
   1101 			(pnode->sysctl_clen - at) * sizeof(struct sysctlnode));
   1102 		memset(&node[at], 0, sizeof(struct sysctlnode));
   1103 		node[at].sysctl_parent = pnode;
   1104 		/*
   1105 		 * and...reparent any children of any moved nodes
   1106 		 */
   1107 		for (ni = at; ni <= pnode->sysctl_clen; ni++)
   1108 			if (SYSCTL_TYPE(node[ni].sysctl_flags) == CTLTYPE_NODE)
   1109 				for (t = 0; t < node[ni].sysctl_clen; t++)
   1110 					node[ni].sysctl_child[t].sysctl_parent =
   1111 						&node[ni];
   1112 	}
   1113 	node = &node[at];
   1114 	pnode->sysctl_clen++;
   1115 
   1116 	strlcpy(node->sysctl_name, nnode.sysctl_name,
   1117 		sizeof(node->sysctl_name));
   1118 	node->sysctl_num = nm;
   1119 	node->sysctl_size = sz;
   1120 	node->sysctl_flags = SYSCTL_VERSION|type|flags; /* XXX other trees */
   1121 	node->sysctl_csize = 0;
   1122 	node->sysctl_clen = 0;
   1123 	if (own) {
   1124 		node->sysctl_data = own;
   1125 		node->sysctl_flags |= CTLFLAG_OWNDATA;
   1126 	}
   1127 	else if (flags & CTLFLAG_ALIAS) {
   1128 		node->sysctl_alias = anum;
   1129 	}
   1130 	else if (flags & CTLFLAG_IMMEDIATE) {
   1131 		switch (type) {
   1132 		case CTLTYPE_INT:
   1133 			node->sysctl_idata = nnode.sysctl_idata;
   1134 			break;
   1135 		case CTLTYPE_QUAD:
   1136 			node->sysctl_qdata = nnode.sysctl_qdata;
   1137 			break;
   1138 		}
   1139 	}
   1140 	else {
   1141 		node->sysctl_data = nnode.sysctl_data;
   1142 		node->sysctl_flags &= ~CTLFLAG_OWNDATA;
   1143 	}
   1144         node->sysctl_func = nnode.sysctl_func;
   1145         node->sysctl_child = NULL;
   1146 	/* node->sysctl_parent should already be done */
   1147 
   1148 	/*
   1149 	 * update "version" on path to "root"
   1150 	 */
   1151 	for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
   1152 		;
   1153 	pnode = node;
   1154 	for (nm = rnode->sysctl_ver + 1; pnode != NULL;
   1155 	     pnode = pnode->sysctl_parent)
   1156 		pnode->sysctl_ver = nm;
   1157 
   1158 	error = sysctl_cvt_out(l, v, node, oldp, *oldlenp, oldlenp);
   1159 
   1160 	return (error);
   1161 }
   1162 
   1163 /*
   1164  * ********************************************************************
   1165  * A wrapper around sysctl_create() that prints the thing we're trying
   1166  * to add.
   1167  * ********************************************************************
   1168  */
   1169 #ifdef SYSCTL_DEBUG_CREATE
   1170 int _sysctl_create(SYSCTLFN_RWPROTO);
   1171 int
   1172 _sysctl_create(SYSCTLFN_RWARGS)
   1173 {
   1174 	const struct sysctlnode *node;
   1175 	int k, rc, ni, nl = namelen + (name - oname);
   1176 
   1177 	node = newp;
   1178 
   1179 	printf("namelen %d (", nl);
   1180 	for (ni = 0; ni < nl - 1; ni++)
   1181 		printf(" %d", oname[ni]);
   1182 	printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
   1183 	       k = node->sysctl_num,
   1184 	       node->sysctl_name,
   1185 	       node->sysctl_flags,
   1186 	       SYSCTL_FLAGS(node->sysctl_flags),
   1187 	       SYSCTL_TYPE(node->sysctl_flags),
   1188 	       node->sysctl_size);
   1189 
   1190 	node = rnode;
   1191 	rc = sysctl_create(SYSCTLFN_CALL(rnode));
   1192 
   1193 	printf("sysctl_create(");
   1194 	for (ni = 0; ni < nl - 1; ni++)
   1195 		printf(" %d", oname[ni]);
   1196 	printf(" %d ) returned %d\n", k, rc);
   1197 
   1198 	return (rc);
   1199 }
   1200 #define sysctl_create _sysctl_create
   1201 #endif /* SYSCTL_DEBUG_CREATE */
   1202 
   1203 /*
   1204  * sysctl_destroy -- Removes a node (as described by newp) from the
   1205  * given tree, returning (if successful) a copy of the dead node in
   1206  * oldp.  Since we're removing stuff, there's not much to check.
   1207  */
   1208 int
   1209 sysctl_destroy(SYSCTLFN_RWARGS)
   1210 {
   1211 	struct sysctlnode *node, *pnode, onode, nnode;
   1212 	int ni, error, v;
   1213 
   1214 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1215 		printf("sysctl_destroy: rnode %p wrong version\n", rnode);
   1216 		return (EINVAL);
   1217 	}
   1218 
   1219 	error = 0;
   1220 
   1221 	if (namelen != 1 || name[namelen - 1] != CTL_DESTROY)
   1222 		return (EINVAL);
   1223 
   1224 	/*
   1225 	 * processes can only destroy nodes at securelevel 0, must be
   1226 	 * root, and can't remove nodes from a parent that's not
   1227 	 * writeable
   1228 	 */
   1229 	if (l != NULL) {
   1230 #ifndef SYSCTL_DISALLOW_CREATE
   1231 		if (securelevel > 0)
   1232 			return (EPERM);
   1233 		error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
   1234 		if (error)
   1235 			return (error);
   1236 		if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
   1237 #endif /* SYSCTL_DISALLOW_CREATE */
   1238 			return (EPERM);
   1239 	}
   1240 
   1241 	/*
   1242 	 * nothing can remove a node if:
   1243 	 * the node is permanent (checked later) or
   1244 	 * the tree itself is not writeable or
   1245 	 * the entire sysctl system is not writeable
   1246 	 */
   1247 	if (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
   1248 	    !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))
   1249 		return (EPERM);
   1250 
   1251 	if (newp == NULL)
   1252 		return (EINVAL);
   1253 	error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
   1254 	if (error)
   1255 		return (error);
   1256 	memset(&onode, 0, sizeof(struct sysctlnode));
   1257 
   1258 	node = rnode->sysctl_child;
   1259 	for (ni = 0; ni < rnode->sysctl_clen; ni++) {
   1260 		if (nnode.sysctl_num == node[ni].sysctl_num) {
   1261 			/*
   1262 			 * if name specified, must match
   1263 			 */
   1264 			if (nnode.sysctl_name[0] != '\0' &&
   1265 			    strcmp(nnode.sysctl_name, node[ni].sysctl_name))
   1266 				continue;
   1267 			/*
   1268 			 * if version specified, must match
   1269 			 */
   1270 			if (nnode.sysctl_ver != 0 &&
   1271 			    nnode.sysctl_ver != node[ni].sysctl_ver)
   1272 				continue;
   1273 			/*
   1274 			 * this must be the one
   1275 			 */
   1276 			break;
   1277 		}
   1278 	}
   1279 	if (ni == rnode->sysctl_clen)
   1280 		return (ENOENT);
   1281 	node = &node[ni];
   1282 	pnode = node->sysctl_parent;
   1283 
   1284 	/*
   1285 	 * if the kernel says permanent, it is, so there.  nyah.
   1286 	 */
   1287 	if (SYSCTL_FLAGS(node->sysctl_flags) & CTLFLAG_PERMANENT)
   1288 		return (EPERM);
   1289 
   1290 	/*
   1291 	 * can't delete non-empty nodes
   1292 	 */
   1293 	if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
   1294 	    node->sysctl_clen != 0)
   1295 		return (ENOTEMPTY);
   1296 
   1297 	/*
   1298 	 * if the node "owns" data, release it now
   1299 	 */
   1300 	if (node->sysctl_flags & CTLFLAG_OWNDATA) {
   1301 		if (node->sysctl_data != NULL)
   1302 			FREE(node->sysctl_data, M_SYSCTLDATA);
   1303 		node->sysctl_data = NULL;
   1304 	}
   1305 
   1306 	/*
   1307 	 * if the node to be removed is not the last one on the list,
   1308 	 * move the remaining nodes up, and reparent any grandchildren
   1309 	 */
   1310 	onode = *node;
   1311 	if (ni < pnode->sysctl_clen - 1) {
   1312 		int t;
   1313 
   1314 		memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1],
   1315 			(pnode->sysctl_clen - ni - 1) *
   1316 			sizeof(struct sysctlnode));
   1317 		for (; ni < pnode->sysctl_clen - 1; ni++)
   1318 			if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) ==
   1319 			    CTLTYPE_NODE)
   1320 				for (t = 0;
   1321 				     t < pnode->sysctl_child[ni].sysctl_clen;
   1322 				     t++)
   1323 					pnode->sysctl_child[ni].sysctl_child[t].
   1324 						sysctl_parent =
   1325 						&pnode->sysctl_child[ni];
   1326 		ni = pnode->sysctl_clen - 1;
   1327 		node = &pnode->sysctl_child[ni];
   1328 	}
   1329 
   1330 	/*
   1331 	 * reset the space we just vacated
   1332 	 */
   1333 	memset(node, 0, sizeof(struct sysctlnode));
   1334 	node->sysctl_parent = pnode;
   1335 	pnode->sysctl_clen--;
   1336 
   1337 	/*
   1338 	 * if this parent just lost its last child, nuke the creche
   1339 	 */
   1340 	if (pnode->sysctl_clen == 0) {
   1341 		FREE(pnode->sysctl_child, M_SYSCTLNODE);
   1342 		pnode->sysctl_csize = 0;
   1343 		pnode->sysctl_child = NULL;
   1344 	}
   1345 
   1346 	/*
   1347 	 * update "version" on path to "root"
   1348 	 */
   1349         for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
   1350                 ;
   1351 	for (ni = rnode->sysctl_ver + 1; pnode != NULL;
   1352 	     pnode = pnode->sysctl_parent)
   1353 		pnode->sysctl_ver = ni;
   1354 
   1355 	error = sysctl_cvt_out(l, v, &onode, oldp, *oldlenp, oldlenp);
   1356 
   1357 	return (error);
   1358 }
   1359 
   1360 /*
   1361  * sysctl_lookup -- Handles copyin/copyout of new and old values.
   1362  * Partial reads are globally allowed.  Only root can write to things
   1363  * unless the node says otherwise.
   1364  */
   1365 int
   1366 sysctl_lookup(SYSCTLFN_RWARGS)
   1367 {
   1368 	int error, rw;
   1369 	size_t sz, len;
   1370 	void *d;
   1371 
   1372 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1373 		printf("sysctl_lookup: rnode %p wrong version\n", rnode);
   1374 		return (EINVAL);
   1375 	}
   1376 
   1377 	error = 0;
   1378 
   1379 	/*
   1380 	 * you can't "look up" a node.  you can "query" it, but you
   1381 	 * can't "look it up".
   1382 	 */
   1383 	if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0)
   1384 		return (EINVAL);
   1385 
   1386 	/*
   1387 	 * some nodes are private, so only root can look into them.
   1388 	 */
   1389 	if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) &&
   1390 	    (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
   1391 		return (error);
   1392 
   1393 	/*
   1394 	 * if a node wants to be writable according to different rules
   1395 	 * other than "only root can write to stuff unless a flag is
   1396 	 * set", then it needs its own function which should have been
   1397 	 * called and not us.
   1398 	 */
   1399 	if (l != NULL && newp != NULL &&
   1400 	    !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) &&
   1401 	    (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
   1402 		return (error);
   1403 
   1404 	/*
   1405 	 * is this node supposedly writable?
   1406 	 */
   1407 	rw = 0;
   1408 	switch (rnode->sysctl_flags & CTLFLAG_READWRITE) {
   1409 	    case CTLFLAG_READONLY1:
   1410 		rw = (securelevel < 1) ? 1 : 0;
   1411 		break;
   1412 	    case CTLFLAG_READONLY2:
   1413 		rw = (securelevel < 2) ? 1 : 0;
   1414 		break;
   1415 	    case CTLFLAG_READWRITE:
   1416 		rw = 1;
   1417 		break;
   1418 	}
   1419 
   1420 	/*
   1421 	 * it appears not to be writable at this time, so if someone
   1422 	 * tried to write to it, we must tell them to go away
   1423 	 */
   1424 	if (!rw && newp != NULL)
   1425 		return (EPERM);
   1426 
   1427 	/*
   1428 	 * step one, copy out the stuff we have presently
   1429 	 */
   1430 	if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) {
   1431 		switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
   1432 		case CTLTYPE_INT:
   1433 			d = &rnode->sysctl_idata;
   1434 			break;
   1435 		case CTLTYPE_QUAD:
   1436 			d = &rnode->sysctl_qdata;
   1437 			break;
   1438 		default:
   1439 			return (EINVAL);
   1440 		}
   1441 	}
   1442 	else
   1443 		d = rnode->sysctl_data;
   1444 	if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING)
   1445 		sz = strlen(d) + 1; /* XXX@@@ possible fault here */
   1446 	else
   1447 		sz = rnode->sysctl_size;
   1448 	if (oldp != NULL)
   1449 		error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp));
   1450 	if (error)
   1451 		return (error);
   1452 	*oldlenp = sz;
   1453 
   1454 	/*
   1455 	 * are we done?
   1456 	 */
   1457 	if (newp == NULL || newlen == 0)
   1458 		return (0);
   1459 
   1460 	/*
   1461 	 * hmm...not done.  must now "copy in" new value.  re-adjust
   1462 	 * sz to maximum value (strings are "weird").
   1463 	 */
   1464 	sz = rnode->sysctl_size;
   1465 	switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
   1466 	case CTLTYPE_INT:
   1467 	case CTLTYPE_QUAD:
   1468 	case CTLTYPE_STRUCT:
   1469 		/*
   1470 		 * these data must be *exactly* the same size coming
   1471 		 * in.
   1472 		 */
   1473 		if (newlen != sz)
   1474 			return (EINVAL);
   1475 		error = sysctl_copyin(l, newp, d, sz);
   1476 		break;
   1477 	case CTLTYPE_STRING: {
   1478 		/*
   1479 		 * strings, on the other hand, can be shorter, and we
   1480 		 * let userland be sloppy about the trailing nul.
   1481 		 */
   1482 		char *newbuf;
   1483 
   1484 		/*
   1485 		 * too much new string?
   1486 		 */
   1487 		if (newlen > sz)
   1488 			return (EINVAL);
   1489 
   1490 		/*
   1491 		 * temporary copy of new inbound string
   1492 		 */
   1493 		len = MIN(sz, newlen);
   1494 		newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   1495 		if (newbuf == NULL)
   1496 			return (ENOMEM);
   1497 		error = sysctl_copyin(l, newp, newbuf, len);
   1498 		if (error) {
   1499 			FREE(newbuf, M_SYSCTLDATA);
   1500 			return (error);
   1501 		}
   1502 
   1503 		/*
   1504 		 * did they null terminate it, or do we have space
   1505 		 * left to do it ourselves?
   1506 		 */
   1507 		if (newbuf[len - 1] != '\0' && len == sz) {
   1508 			FREE(newbuf, M_SYSCTLDATA);
   1509 			return (EINVAL);
   1510 		}
   1511 
   1512 		/*
   1513 		 * looks good, so pop it into place and zero the rest.
   1514 		 */
   1515 		if (len > 0)
   1516 			memcpy(rnode->sysctl_data, newbuf, len);
   1517 		if (sz != len)
   1518 			memset((char*)rnode->sysctl_data + len, 0, sz - len);
   1519 		FREE(newbuf, M_SYSCTLDATA);
   1520 		break;
   1521 	}
   1522 	default:
   1523 		return (EINVAL);
   1524 	}
   1525 
   1526 	return (error);
   1527 }
   1528 
   1529 /*
   1530  * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
   1531  * purport to handle it.  This interface isn't fully fleshed out yet,
   1532  * unfortunately.
   1533  */
   1534 static int
   1535 sysctl_mmap(SYSCTLFN_RWARGS)
   1536 {
   1537 	struct sysctlnode nnode, *node;
   1538 	int error;
   1539 
   1540 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1541 		printf("sysctl_mmap: rnode %p wrong version\n", rnode);
   1542 		return (EINVAL);
   1543 	}
   1544 
   1545 	/*
   1546 	 * let's just pretend that didn't happen, m'kay?
   1547 	 */
   1548 	if (l == NULL)
   1549 		return (EPERM);
   1550 
   1551 	/*
   1552 	 * is this a sysctlnode description of an mmap request?
   1553 	 */
   1554 	if (newp == NULL || newlen != sizeof(struct sysctlnode))
   1555 		return (EINVAL);
   1556 	error = sysctl_copyin(l, newp, &nnode, sizeof(nnode));
   1557 	if (error)
   1558 		return (error);
   1559 
   1560 	/*
   1561 	 * does the node they asked for exist?
   1562 	 */
   1563 	if (namelen != 1)
   1564 		return (EOPNOTSUPP);
   1565 	node = rnode;
   1566         error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
   1567 	if (error)
   1568 		return (error);
   1569 
   1570 	/*
   1571 	 * does this node that we have found purport to handle mmap?
   1572 	 */
   1573 	if (node->sysctl_func == NULL ||
   1574 	    !(node->sysctl_flags & CTLFLAG_MMAP))
   1575 		return (EOPNOTSUPP);
   1576 
   1577 	/*
   1578 	 * well...okay, they asked for it.
   1579 	 */
   1580 	return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
   1581 }
   1582 
   1583 int
   1584 sysctl_describe(SYSCTLFN_ARGS)
   1585 {
   1586 
   1587 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   1588 		printf("sysctl_query: rnode %p wrong version\n", rnode);
   1589 		return (EINVAL);
   1590 	}
   1591 
   1592 	if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
   1593 		return (ENOTDIR);
   1594 	if (namelen != 1 || name[0] != CTL_DESCRIBE)
   1595 		return (EINVAL);
   1596 
   1597 	/*
   1598 	 * implementation to be filled in later
   1599 	 */
   1600 
   1601 	*oldlenp = 0;
   1602 
   1603 	return (0);
   1604 }
   1605 
   1606 /*
   1607  * ********************************************************************
   1608  * Section 3: Create and destroy from inside the kernel
   1609  * ********************************************************************
   1610  * sysctl_createv() and sysctl_destroyv() are simpler-to-use
   1611  * interfaces for the kernel to fling new entries into the mib and rip
   1612  * them out later.  In the case of sysctl_createv(), the returned copy
   1613  * of the node (see sysctl_create()) will be translated back into a
   1614  * pointer to the actual node.
   1615  *
   1616  * Note that sysctl_createv() will return 0 if the create request
   1617  * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
   1618  * will return 0 if the node to be destroyed already does not exist
   1619  * (aka rm -f) or if it is a parent of other nodes.
   1620  *
   1621  * This allows two (or more) different subsystems to assert sub-tree
   1622  * existence before populating their own nodes, and to remove their
   1623  * own nodes without orphaning the others when they are done.
   1624  * ********************************************************************
   1625  */
   1626 int
   1627 sysctl_createv(struct sysctllog **log, int cflags,
   1628 	       struct sysctlnode **rnode, struct sysctlnode **cnode,
   1629 	       int flags, int type, const char *namep, const char *descr,
   1630 	       sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
   1631 	       ...)
   1632 {
   1633 	va_list ap;
   1634 	int error, ni, namelen, name[CTL_MAXNAME];
   1635 	struct sysctlnode *pnode, nnode, onode;
   1636 	size_t sz;
   1637 
   1638 	if (cnode != NULL)
   1639 		*cnode = NULL;
   1640 	if (cflags != 0)
   1641 		return (EINVAL);
   1642 
   1643 	/*
   1644 	 * where are we putting this?
   1645 	 */
   1646 	if (rnode != NULL && *rnode == NULL) {
   1647 		printf("sysctl_createv: rnode NULL\n");
   1648 		return (EINVAL);
   1649 	}
   1650 
   1651 	/*
   1652 	 * what is it?
   1653 	 */
   1654 	flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
   1655 	if (log != NULL)
   1656 		flags &= ~CTLFLAG_PERMANENT;
   1657 
   1658 	/*
   1659 	 * where do we put it?
   1660 	 */
   1661 	va_start(ap, newlen);
   1662 	namelen = 0;
   1663 	ni = -1;
   1664 	do {
   1665 		if (++ni == CTL_MAXNAME)
   1666 			return (ENAMETOOLONG);
   1667 		name[ni] = va_arg(ap, int);
   1668 		/*
   1669 		 * sorry, this is not supported from here
   1670 		 */
   1671 		if (name[ni] == CTL_CREATESYM)
   1672 			return (EINVAL);
   1673 	} while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
   1674 	namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
   1675 	va_end(ap);
   1676 
   1677 	/*
   1678 	 * what's it called
   1679 	 */
   1680 	if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >
   1681 	    sizeof(nnode.sysctl_name))
   1682 		return (ENAMETOOLONG);
   1683 
   1684 	/*
   1685 	 * cons up the description of the new node
   1686 	 */
   1687 	nnode.sysctl_num = name[namelen - 1];
   1688 	name[namelen - 1] = CTL_CREATE;
   1689 	nnode.sysctl_size = newlen;
   1690 	nnode.sysctl_flags = flags;
   1691 	if (type == CTLTYPE_NODE) {
   1692 		nnode.sysctl_csize = 0;
   1693 		nnode.sysctl_clen = 0;
   1694 		nnode.sysctl_child = NULL;
   1695 		if (flags & CTLFLAG_ALIAS)
   1696 			nnode.sysctl_alias = qv;
   1697 	}
   1698 	else if (flags & CTLFLAG_IMMEDIATE) {
   1699 		switch (type) {
   1700 		case CTLTYPE_INT:
   1701 			nnode.sysctl_idata = qv;
   1702 			break;
   1703 		case CTLTYPE_QUAD:
   1704 			nnode.sysctl_qdata = qv;
   1705 			break;
   1706 		default:
   1707 			return (EINVAL);
   1708 		}
   1709 	}
   1710 	else {
   1711 		nnode.sysctl_data = newp;
   1712 	}
   1713 	nnode.sysctl_func = func;
   1714 	nnode.sysctl_parent = NULL;
   1715 	nnode.sysctl_ver = 0;
   1716 
   1717 	/*
   1718 	 * initialize lock state -- we need locks if the main tree has
   1719 	 * been marked as complete, but since we could be called from
   1720 	 * either there, or from a device driver (say, at device
   1721 	 * insertion), or from an lkm (at lkm load time, say), we
   1722 	 * don't really want to "wait"...
   1723 	 */
   1724 	error = sysctl_lock(NULL, NULL, 0);
   1725 	if (error)
   1726 		return (error);
   1727 
   1728 	/*
   1729 	 * locate the prospective parent of the new node, and if we
   1730 	 * find it, add the new node.
   1731 	 */
   1732 	sz = sizeof(onode);
   1733 	pnode = rnode ? *rnode : NULL;
   1734 	error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
   1735 	if (error) {
   1736 		sysctl_unlock(NULL);
   1737 		return (error);
   1738 	}
   1739 	error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
   1740 			      &nnode, sizeof(nnode), &name[0], NULL,
   1741 			      pnode);
   1742 
   1743 	/*
   1744 	 * unfortunately the node we wanted to create is already
   1745 	 * there.  if the node that's already there is a reasonable
   1746 	 * facsimile of the node we wanted to create, just pretend
   1747 	 * (for the caller's benefit) that we managed to create the
   1748 	 * node they wanted.
   1749 	 */
   1750 	if (error == EEXIST) {
   1751 		/* name is the same as requested... */
   1752 		if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
   1753 		    /* they want the same function... */
   1754 		    nnode.sysctl_func == onode.sysctl_func &&
   1755 		    /* number is the same as requested, or... */
   1756 		    (nnode.sysctl_num == onode.sysctl_num ||
   1757 		     /* they didn't pick a number... */
   1758 		     nnode.sysctl_num == CTL_CREATE)) {
   1759 			/*
   1760 			 * collision here from trying to create
   1761 			 * something that already existed; let's give
   1762 			 * our customers a hand and tell them they got
   1763 			 * what they wanted.
   1764 			 */
   1765 #ifdef SYSCTL_DEBUG_CREATE
   1766 			printf("cleared\n");
   1767 #endif /* SYSCTL_DEBUG_CREATE */
   1768 			error = 0;
   1769 		}
   1770 	}
   1771 
   1772 	if (error == 0 &&
   1773 	    (cnode != NULL || log != NULL)) {
   1774 		/*
   1775 		 * sysctl_create() gave us back a copy of the node,
   1776 		 * but we need to know where it actually is...
   1777 		 */
   1778 		pnode = rnode ? *rnode : NULL;
   1779 		error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
   1780 
   1781 		/*
   1782 		 * manual scan of last layer so that aliased nodes
   1783 		 * aren't followed.
   1784 		 */
   1785 		if (error == 0) {
   1786 			for (ni = 0; ni < pnode->sysctl_clen; ni++)
   1787 				if (pnode->sysctl_child[ni].sysctl_num ==
   1788 				    onode.sysctl_num)
   1789 					break;
   1790 			if (ni < pnode->sysctl_clen)
   1791 				pnode = &pnode->sysctl_child[ni];
   1792 			else
   1793 				error = ENOENT;
   1794 		}
   1795 
   1796 		/*
   1797 		 * not expecting an error here, but...
   1798 		 */
   1799 		if (error == 0) {
   1800 			if (log != NULL)
   1801 				sysctl_log_add(log, pnode);
   1802 			if (cnode != NULL)
   1803 				*cnode = pnode;
   1804 		}
   1805 		else {
   1806 			printf("sysctl_create succeeded but node not found?!\n");
   1807 			/*
   1808 			 *  confusing, but the create said it
   1809 			 * succeeded, so...
   1810 			 */
   1811 			error = 0;
   1812 		}
   1813 	}
   1814 
   1815 	/*
   1816 	 * now it should be safe to release the lock state.  note that
   1817 	 * the pointer to the newly created node being passed back may
   1818 	 * not be "good" for very long.
   1819 	 */
   1820 	sysctl_unlock(NULL);
   1821 
   1822 	if (error != 0) {
   1823 		printf("sysctl_createv: sysctl_create(%s) returned %d\n",
   1824 		       nnode.sysctl_name, error);
   1825 #if 0
   1826 		if (error != ENOENT)
   1827 			sysctl_dump(&onode);
   1828 #endif
   1829 	}
   1830 
   1831 	return (error);
   1832 }
   1833 
   1834 int
   1835 sysctl_destroyv(struct sysctlnode *rnode, ...)
   1836 {
   1837 	va_list ap;
   1838 	int error, name[CTL_MAXNAME], namelen, ni;
   1839 	struct sysctlnode *pnode, *node, dnode;
   1840 	size_t sz;
   1841 
   1842 	va_start(ap, rnode);
   1843 	namelen = 0;
   1844 	ni = 0;
   1845 	do {
   1846 		if (ni == CTL_MAXNAME)
   1847 			return (ENAMETOOLONG);
   1848 		name[ni] = va_arg(ap, int);
   1849 	} while (name[ni++] != CTL_EOL);
   1850 	namelen = ni - 1;
   1851 	va_end(ap);
   1852 
   1853 	/*
   1854 	 * i can't imagine why we'd be destroying a node when the tree
   1855 	 * wasn't complete, but who knows?
   1856 	 */
   1857 	error = sysctl_lock(NULL, NULL, 0);
   1858 	if (error)
   1859 		return (error);
   1860 
   1861 	/*
   1862 	 * where is it?
   1863 	 */
   1864 	node = rnode;
   1865 	error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni);
   1866 	if (error) {
   1867 		/* they want it gone and it's not there, so... */
   1868 		sysctl_unlock(NULL);
   1869 		return (error == ENOENT ? 0 : error);
   1870 	}
   1871 
   1872 	/*
   1873 	 * set up the deletion
   1874 	 */
   1875 	pnode = node;
   1876 	node = &dnode;
   1877 	memset(&dnode, 0, sizeof(dnode));
   1878 	dnode.sysctl_num = name[namelen - 1];
   1879 
   1880 	/*
   1881 	 * we found it, now let's nuke it
   1882 	 */
   1883 	name[namelen - 1] = CTL_DESTROY;
   1884 	sz = 0;
   1885 	error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz,
   1886 			       node, sizeof(*node), &name[0], NULL,
   1887 			       pnode);
   1888 	if (error == ENOTEMPTY)
   1889 		/*
   1890 		 * think of trying to delete "foo" when "foo.bar"
   1891 		 * (which someone else put there) is still in
   1892 		 * existence
   1893 		 */
   1894 		error = 0;
   1895 
   1896         sysctl_unlock(NULL);
   1897 
   1898 	return (error);
   1899 }
   1900 
   1901 #if 0
   1902 /*
   1903  * ********************************************************************
   1904  * the dump routine.  i haven't yet decided how (if at all) i'll call
   1905  * this from userland when it's in the kernel.
   1906  * ********************************************************************
   1907  */
   1908 static const char *
   1909 sf(int f)
   1910 {
   1911 	static char s[256];
   1912 	char *c;
   1913 
   1914 	s[0] = '\0';
   1915 	c = "";
   1916 
   1917 #define print_flag(_f, _s, _c, _q, _x) \
   1918 	if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \
   1919 		strlcat((_s), (_c), sizeof(_s)); \
   1920 		strlcat((_s), __STRING(_q), sizeof(_s)); \
   1921 		(_c) = ","; \
   1922 		(_f) &= ~__CONCAT(CTLFLAG_,_x); \
   1923 	}
   1924 
   1925 	print_flag(f, s, c, READONLY,  READWRITE);
   1926 	print_flag(f, s, c, READONLY1, READWRITE);
   1927 	print_flag(f, s, c, READONLY2, READWRITE);
   1928 	print_flag(f, s, c, READWRITE, READWRITE);
   1929 	print_flag(f, s, c, ANYWRITE,  ANYWRITE);
   1930 	print_flag(f, s, c, PRIVATE,   PRIVATE);
   1931 	print_flag(f, s, c, PERMANENT, PERMANENT);
   1932 	print_flag(f, s, c, OWNDATA,   OWNDATA);
   1933 	print_flag(f, s, c, IMMEDIATE, IMMEDIATE);
   1934 	print_flag(f, s, c, HEX,       HEX);
   1935 	print_flag(f, s, c, ROOT,      ROOT);
   1936 	print_flag(f, s, c, ANYNUMBER, ANYNUMBER);
   1937 	print_flag(f, s, c, HIDDEN,    HIDDEN);
   1938 	print_flag(f, s, c, ALIAS,     ALIAS);
   1939 #undef print_flag
   1940 
   1941 	if (f) {
   1942 		char foo[9];
   1943 		snprintf(foo, sizeof(foo), "%x", f);
   1944 		strlcat(s, c, sizeof(s));
   1945 		strlcat(s, foo, sizeof(s));
   1946 	}
   1947 
   1948 	return (s);
   1949 }
   1950 
   1951 static const char *
   1952 st(int t)
   1953 {
   1954 
   1955 	switch (t) {
   1956 	case CTLTYPE_NODE:
   1957 		return "NODE";
   1958 	case CTLTYPE_INT:
   1959 		return "INT";
   1960 	case CTLTYPE_STRING:
   1961 		return "STRING";
   1962 	case CTLTYPE_QUAD:
   1963 		return "QUAD";
   1964 	case CTLTYPE_STRUCT:
   1965 		return "STRUCT";
   1966 	}
   1967 
   1968 	return "???";
   1969 }
   1970 
   1971 void
   1972 sysctl_dump(const struct sysctlnode *d)
   1973 {
   1974 	static char nmib[64], smib[256];
   1975 	static int indent;
   1976 	struct sysctlnode *n;
   1977 	char *np, *sp, tmp[20];
   1978 	int i;
   1979 
   1980 	if (d == NULL)
   1981 		return;
   1982 
   1983 	np = &nmib[strlen(nmib)];
   1984 	sp = &smib[strlen(smib)];
   1985 
   1986 	if (!(d->sysctl_flags & CTLFLAG_ROOT)) {
   1987 		snprintf(tmp, sizeof(tmp), "%d", d->sysctl_num);
   1988 		strcat(nmib, ".");
   1989 		strcat(smib, ".");
   1990 		strcat(nmib, tmp);
   1991 		strcat(smib, d->sysctl_name);
   1992 		printf("%s -> %s (%d)\n", &nmib[1], &smib[1],
   1993 		       SYSCTL_TYPE(d->sysctl_flags));
   1994 	}
   1995 
   1996 	if (1) {
   1997 		printf("%*s%p:\tsysctl_name  [%s]\n", indent, "",
   1998 		       d, d->sysctl_name);
   1999 		printf("%*s\t\tsysctl_num    %d\n",   indent, "",
   2000 		       d->sysctl_num);
   2001 		printf("%*s\t\tsysctl_flags  %x (flags=%x<%s> type=%d<%s> "
   2002 		       "size=%zu)\n",
   2003 		       indent, "", d->sysctl_flags,
   2004 		       SYSCTL_FLAGS(d->sysctl_flags),
   2005 		       sf(SYSCTL_FLAGS(d->sysctl_flags)),
   2006 		       SYSCTL_TYPE(d->sysctl_flags),
   2007 		       st(SYSCTL_TYPE(d->sysctl_flags)),
   2008 		       d->sysctl_size);
   2009 		if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
   2010 			printf("%*s\t\tsysctl_csize  %d\n",   indent, "",
   2011 			       d->sysctl_csize);
   2012 			printf("%*s\t\tsysctl_clen   %d\n",   indent, "",
   2013 			       d->sysctl_clen);
   2014 			printf("%*s\t\tsysctl_child  %p\n",   indent, "",
   2015 			       d->sysctl_child);
   2016 		}
   2017 		else
   2018 			printf("%*s\t\tsysctl_data   %p\n",   indent, "",
   2019 			       d->sysctl_data);
   2020 		printf("%*s\t\tsysctl_func   %p\n",   indent, "",
   2021 		       d->sysctl_func);
   2022 		printf("%*s\t\tsysctl_parent %p\n",   indent, "",
   2023 		       d->sysctl_parent);
   2024 		printf("%*s\t\tsysctl_ver    %d\n",   indent, "",
   2025 		       d->sysctl_ver);
   2026 	}
   2027 
   2028 	if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
   2029 		indent += 8;
   2030 		n = d->sysctl_child;
   2031 		for (i = 0; i < d->sysctl_clen; i++) {
   2032 			sysctl_dump(&n[i]);
   2033 		}
   2034 		indent -= 8;
   2035 	}
   2036 
   2037 	np[0] = '\0';
   2038 	sp[0] = '\0';
   2039 }
   2040 #endif /* 0 */
   2041 
   2042 /*
   2043  * ********************************************************************
   2044  * Deletes an entire n-ary tree.  Not recommended unless you know why
   2045  * you're doing it.  Personally, I don't know why you'd even think
   2046  * about it.
   2047  * ********************************************************************
   2048  */
   2049 void
   2050 sysctl_free(struct sysctlnode *rnode)
   2051 {
   2052 	struct sysctlnode *node, *pnode;
   2053 
   2054 	if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
   2055 		printf("sysctl_free: rnode %p wrong version\n", rnode);
   2056 		return;
   2057 	}
   2058 
   2059 	if (rnode == NULL)
   2060 		rnode = &sysctl_root;
   2061 	pnode = rnode;
   2062 
   2063 	node = pnode->sysctl_child;
   2064 	do {
   2065 		while (node != NULL && pnode->sysctl_csize > 0) {
   2066 			while (node <
   2067 			       &pnode->sysctl_child[pnode->sysctl_clen] &&
   2068 			       (SYSCTL_TYPE(node->sysctl_flags) !=
   2069 				CTLTYPE_NODE ||
   2070 				node->sysctl_csize == 0)) {
   2071 				if (SYSCTL_FLAGS(node->sysctl_flags) &
   2072 				    CTLFLAG_OWNDATA) {
   2073 					if (node->sysctl_data != NULL) {
   2074 						FREE(node->sysctl_data,
   2075 						     M_SYSCTLDATA);
   2076 						node->sysctl_data = NULL;
   2077 					}
   2078 				}
   2079 				node++;
   2080 			}
   2081 			if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
   2082 				pnode = node;
   2083 				node = node->sysctl_child;
   2084 			}
   2085 			else
   2086 				break;
   2087 		}
   2088 		if (pnode->sysctl_child != NULL)
   2089 			FREE(pnode->sysctl_child, M_SYSCTLNODE);
   2090 		pnode->sysctl_clen = 0;
   2091 		pnode->sysctl_csize = 0;
   2092 		pnode->sysctl_child = NULL;
   2093 		node = pnode;
   2094 		pnode = node->sysctl_parent;
   2095 	} while (pnode != NULL && node != rnode);
   2096 }
   2097 
   2098 int
   2099 sysctl_log_add(struct sysctllog **logp, struct sysctlnode *node)
   2100 {
   2101 	int name[CTL_MAXNAME], namelen, i;
   2102 	struct sysctlnode *pnode;
   2103 	struct sysctllog *log;
   2104 
   2105 	if (node->sysctl_flags & CTLFLAG_PERMANENT)
   2106 		return (0);
   2107 
   2108 	if (logp == NULL)
   2109 		return (0);
   2110 
   2111 	if (*logp == NULL) {
   2112 		MALLOC(log, struct sysctllog *, sizeof(struct sysctllog),
   2113 		       M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   2114 		if (log == NULL) {
   2115 			/* XXX print error message? */
   2116 			return (-1);
   2117 		}
   2118 		MALLOC(log->log_num, int *, 16 * sizeof(int),
   2119 		       M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   2120 		if (log->log_num == NULL) {
   2121 			/* XXX print error message? */
   2122 			free(log, M_SYSCTLDATA);
   2123 			return (-1);
   2124 		}
   2125 		memset(log->log_num, 0, 16 * sizeof(int));
   2126 		log->log_root = NULL;
   2127 		log->log_size = 16;
   2128 		log->log_left = 16;
   2129 		*logp = log;
   2130 	}
   2131 	else
   2132 		log = *logp;
   2133 
   2134 	/*
   2135 	 * check that the root is proper.  it's okay to record the
   2136 	 * address of the root of a tree.  it's the only thing that's
   2137 	 * guaranteed not to shift around as nodes come and go.
   2138 	 */
   2139 	if (log->log_root == NULL)
   2140 		log->log_root = sysctl_rootof(node);
   2141 	else if (log->log_root != sysctl_rootof(node)) {
   2142 		printf("sysctl: log %p root mismatch (%p)\n",
   2143 		       log->log_root, sysctl_rootof(node));
   2144 		return (-1);
   2145 	}
   2146 
   2147 	/*
   2148 	 * we will copy out name in reverse order
   2149 	 */
   2150 	for (pnode = node, namelen = 0;
   2151 	     pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT);
   2152 	     pnode = pnode->sysctl_parent)
   2153 		name[namelen++] = pnode->sysctl_num;
   2154 
   2155 	/*
   2156 	 * do we have space?
   2157 	 */
   2158 	if (log->log_left < (namelen + 3))
   2159 		sysctl_log_realloc(log);
   2160 	if (log->log_left < (namelen + 3))
   2161 		return (-1);
   2162 
   2163 	/*
   2164 	 * stuff name in, then namelen, then node type, and finally,
   2165 	 * the version for non-node nodes.
   2166 	 */
   2167 	for (i = 0; i < namelen; i++)
   2168 		log->log_num[--log->log_left] = name[i];
   2169 	log->log_num[--log->log_left] = namelen;
   2170 	log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags);
   2171 	if (log->log_num[log->log_left] != CTLTYPE_NODE)
   2172 		log->log_num[--log->log_left] = node->sysctl_ver;
   2173 	else
   2174 		log->log_num[--log->log_left] = 0;
   2175 
   2176 	return (0);
   2177 }
   2178 
   2179 void
   2180 sysctl_teardown(struct sysctllog **logp)
   2181 {
   2182 	struct sysctlnode node, *rnode;
   2183 	struct sysctllog *log;
   2184 	uint namelen;
   2185 	int *name, t, v, error, ni;
   2186 	size_t sz;
   2187 
   2188 	if (logp == NULL || *logp == NULL)
   2189 		return;
   2190 	log = *logp;
   2191 
   2192 	error = sysctl_lock(NULL, NULL, 0);
   2193 	if (error)
   2194 		return;
   2195 
   2196 	memset(&node, 0, sizeof(node));
   2197 
   2198 	while (log->log_left < log->log_size) {
   2199 		KASSERT((log->log_left + 3 < log->log_size) &&
   2200 			(log->log_left + log->log_num[log->log_left + 2] <=
   2201 			 log->log_size));
   2202 		v = log->log_num[log->log_left++];
   2203 		t = log->log_num[log->log_left++];
   2204 		namelen = log->log_num[log->log_left++];
   2205 		name = &log->log_num[log->log_left];
   2206 
   2207 		node.sysctl_num = name[namelen - 1];
   2208 		node.sysctl_flags = t;
   2209 		node.sysctl_ver = v;
   2210 
   2211 		rnode = log->log_root;
   2212 		error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni);
   2213 		if (error == 0) {
   2214 			name[namelen - 1] = CTL_DESTROY;
   2215 			rnode = rnode->sysctl_parent;
   2216 			sz = 0;
   2217 			(void)sysctl_destroy(&name[namelen - 1], 1, NULL,
   2218 					     &sz, &node, sizeof(node),
   2219 					     &name[0], NULL, rnode);
   2220 		}
   2221 
   2222 		log->log_left += namelen;
   2223 	}
   2224 
   2225 	KASSERT(log->log_size == log->log_left);
   2226 	free(log->log_num, M_SYSCTLDATA);
   2227 	free(log, M_SYSCTLDATA);
   2228 	*logp = NULL;
   2229 
   2230 	sysctl_unlock(NULL);
   2231 }
   2232 
   2233 /*
   2234  * ********************************************************************
   2235  * old_sysctl -- A routine to bridge old-style internal calls to the
   2236  * new infrastructure.
   2237  * ********************************************************************
   2238  */
   2239 int
   2240 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
   2241 	   void *newp, size_t newlen, struct lwp *l)
   2242 {
   2243 	int error;
   2244 	size_t savelen = *oldlenp;
   2245 
   2246 	error = sysctl_lock(l, oldp, savelen);
   2247 	if (error)
   2248 		return (error);
   2249 	error = sysctl_dispatch(name, namelen, oldp, oldlenp,
   2250 				newp, newlen, name, l, NULL);
   2251 	sysctl_unlock(l);
   2252 	if (error == 0 && oldp != NULL && savelen < *oldlenp)
   2253 		error = ENOMEM;
   2254 
   2255 	return (error);
   2256 }
   2257 
   2258 /*
   2259  * ********************************************************************
   2260  * Section 4: Generic helper routines
   2261  * ********************************************************************
   2262  * "helper" routines that can do more finely grained access control,
   2263  * construct structures from disparate information, create the
   2264  * appearance of more nodes and sub-trees, etc.  for example, if
   2265  * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
   2266  * with a dynamically created list of nodes that represented the
   2267  * currently running processes at that instant.
   2268  * ********************************************************************
   2269  */
   2270 
   2271 /*
   2272  * first, a few generic helpers that provide:
   2273  *
   2274  * sysctl_needfunc()		a readonly interface that emits a warning
   2275  * sysctl_notavail()		returns EOPNOTSUPP (generic error)
   2276  * sysctl_null()		an empty return buffer with no error
   2277  */
   2278 int
   2279 sysctl_needfunc(SYSCTLFN_ARGS)
   2280 {
   2281 	int error;
   2282 
   2283 	printf("!!SYSCTL_NEEDFUNC!!\n");
   2284 
   2285 	if (newp != NULL || namelen != 0)
   2286 		return (EOPNOTSUPP);
   2287 
   2288 	error = 0;
   2289 	if (oldp != NULL)
   2290 		error = sysctl_copyout(l, rnode->sysctl_data, oldp,
   2291 				       MIN(rnode->sysctl_size, *oldlenp));
   2292 	*oldlenp = rnode->sysctl_size;
   2293 
   2294 	return (error);
   2295 }
   2296 
   2297 int
   2298 sysctl_notavail(SYSCTLFN_ARGS)
   2299 {
   2300 
   2301 	if (namelen == 1 && name[0] == CTL_QUERY)
   2302 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2303 
   2304 	return (EOPNOTSUPP);
   2305 }
   2306 
   2307 int
   2308 sysctl_null(SYSCTLFN_ARGS)
   2309 {
   2310 
   2311 	*oldlenp = 0;
   2312 
   2313 	return (0);
   2314 }
   2315 
   2316 /*
   2317  * ********************************************************************
   2318  * Section 5: The machinery that makes it all go
   2319  * ********************************************************************
   2320  * Memory "manglement" routines.  Not much to this, eh?
   2321  * ********************************************************************
   2322  */
   2323 static int
   2324 sysctl_alloc(struct sysctlnode *p, int x)
   2325 {
   2326 	int i;
   2327 	struct sysctlnode *n;
   2328 
   2329 	assert(p->sysctl_child == NULL);
   2330 
   2331 	if (x == 1)
   2332 		MALLOC(n, struct sysctlnode *,
   2333 		       sizeof(struct sysctlnode),
   2334 		       M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
   2335 	else
   2336 		MALLOC(n, struct sysctlnode *,
   2337 		       SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
   2338 		       M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
   2339 	if (n == NULL)
   2340 		return (ENOMEM);
   2341 
   2342 	if (x == 1) {
   2343 		memset(n, 0, sizeof(struct sysctlnode));
   2344 		p->sysctl_csize = 1;
   2345 	}
   2346 	else {
   2347 		memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
   2348 		p->sysctl_csize = SYSCTL_DEFSIZE;
   2349 	}
   2350 	p->sysctl_clen = 0;
   2351 
   2352 	for (i = 0; i < p->sysctl_csize; i++)
   2353 		n[i].sysctl_parent = p;
   2354 
   2355 	p->sysctl_child = n;
   2356 	return (0);
   2357 }
   2358 
   2359 static int
   2360 sysctl_realloc(struct sysctlnode *p)
   2361 {
   2362 	int i, j;
   2363 	struct sysctlnode *n;
   2364 
   2365 	assert(p->sysctl_csize == p->sysctl_clen);
   2366 
   2367 	/*
   2368 	 * how many do we have...how many should we make?
   2369 	 */
   2370 	i = p->sysctl_clen;
   2371 	n = malloc(2 * i * sizeof(struct sysctlnode), M_SYSCTLNODE,
   2372 		   M_WAITOK|M_CANFAIL);
   2373 	if (n == NULL)
   2374 		return (ENOMEM);
   2375 
   2376 	/*
   2377 	 * move old children over...initialize new children
   2378 	 */
   2379 	memcpy(n, p->sysctl_child, i * sizeof(struct sysctlnode));
   2380 	memset(&n[i], 0, i * sizeof(struct sysctlnode));
   2381 	p->sysctl_csize = 2 * i;
   2382 
   2383 	/*
   2384 	 * reattach moved (and new) children to parent; if a moved
   2385 	 * child node has children, reattach the parent pointers of
   2386 	 * grandchildren
   2387 	 */
   2388         for (i = 0; i < p->sysctl_csize; i++) {
   2389                 n[i].sysctl_parent = p;
   2390 		if (n[i].sysctl_child != NULL) {
   2391 			for (j = 0; j < n[i].sysctl_csize; j++)
   2392 				n[i].sysctl_child[j].sysctl_parent = &n[i];
   2393 		}
   2394 	}
   2395 
   2396 	/*
   2397 	 * get out with the old and in with the new
   2398 	 */
   2399 	FREE(p->sysctl_child, M_SYSCTLNODE);
   2400 	p->sysctl_child = n;
   2401 
   2402 	return (0);
   2403 }
   2404 
   2405 static int
   2406 sysctl_log_realloc(struct sysctllog *log)
   2407 {
   2408 	int *n, s, d;
   2409 
   2410 	s = log->log_size * 2;
   2411 	d = log->log_size;
   2412 
   2413 	n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
   2414 	if (n == NULL)
   2415 		return (-1);
   2416 
   2417 	memset(n, 0, s * sizeof(int));
   2418 	memcpy(&n[d], log->log_num, d * sizeof(int));
   2419 	free(log->log_num, M_SYSCTLDATA);
   2420 	log->log_num = n;
   2421 	if (d)
   2422 		log->log_left += d;
   2423 	else
   2424 		log->log_left = s;
   2425 	log->log_size = s;
   2426 
   2427 	return (0);
   2428 }
   2429 
   2430 /*
   2431  * ********************************************************************
   2432  * Section 6: Conversion between API versions wrt the sysctlnode
   2433  * ********************************************************************
   2434  */
   2435 static int
   2436 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz,
   2437 	      struct sysctlnode *node)
   2438 {
   2439 	struct sysctlnode0 inode0;
   2440 	int error, flags;
   2441 
   2442 	if (i == NULL) {
   2443 		memset(node, 0, sizeof(*node));
   2444 		*vp = SYSCTL_VERS_0;
   2445 		return (0);
   2446 	}
   2447 
   2448 	if (sz < sizeof(flags))
   2449 		return (EINVAL);
   2450 
   2451 	error = sysctl_copyin(l, i, &flags, sizeof(flags));
   2452 	if (error)
   2453 		return (error);
   2454 
   2455 	if (sz == sizeof(inode0) &&
   2456 	    SYSCTL_VERS(flags) == SYSCTL_VERS_0) {
   2457 		error = sysctl_copyin(l, i, &inode0, sizeof(inode0));
   2458 		if (error)
   2459 			return (error);
   2460 
   2461 		node->sysctl_flags = inode0.sysctl0_flags;
   2462 		node->sysctl_num = inode0.sysctl0_num;
   2463 		memcpy(node->sysctl_name, inode0.sysctl0_name, SYSCTL_NAMELEN);
   2464 		node->sysctl_ver = inode0.sysctl0_ver;
   2465 		node->sysctl_csize = inode0.sysctl0_csize;
   2466 		node->sysctl_clen = inode0.sysctl0_clen;
   2467 		node->sysctl_child = NULL; /* inode.sysctl0_child; */
   2468 		node->sysctl_data = inode0.sysctl0_data;
   2469 		node->sysctl_offset = 0;
   2470 		node->sysctl_alias = inode0.sysctl0_alias;
   2471 		node->sysctl_idata = inode0.sysctl0_idata;
   2472 		node->sysctl_qdata = inode0.sysctl0_qdata;
   2473 		node->sysctl_size = inode0.sysctl0_size;
   2474 		node->sysctl_func = inode0.sysctl0_func;
   2475 		node->sysctl_parent = NULL; /* inode.sysctl0_parent; */
   2476 		node->sysctl_desc = NULL;
   2477 
   2478 		node->sysctl_flags &= ~SYSCTL_VERS_MASK;
   2479 		node->sysctl_flags |= SYSCTL_VERSION;
   2480 		if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
   2481 		    node->sysctl_size == sizeof(inode0))
   2482 			node->sysctl_size = sizeof(*node);
   2483 
   2484 		*vp = SYSCTL_VERS_0;
   2485 		return (0);
   2486 	}
   2487 
   2488 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
   2489 #error sysctl_cvt_in: no support for SYSCTL_VERSION
   2490 #endif /*  (SYSCTL_VERSION != SYSCTL_VERS_1) */
   2491 
   2492 	if (sz == sizeof(*node) &&
   2493 	    SYSCTL_VERS(flags) == SYSCTL_VERSION) {
   2494 		error = sysctl_copyin(l, i, node, sizeof(*node));
   2495 		if (error)
   2496 			return (error);
   2497 		*vp = SYSCTL_VERSION;
   2498 		return (0);
   2499 	}
   2500 
   2501 	return (EINVAL);
   2502 }
   2503 
   2504 static int
   2505 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i,
   2506 	       void *ovp, size_t left, size_t *szp)
   2507 {
   2508 	struct sysctlnode0 onode0;
   2509 	size_t sz = sizeof(*i);
   2510 	const void *src = i;
   2511 	int error;
   2512 
   2513 	switch (v) {
   2514 	case SYSCTL_VERS_0:
   2515 		sz = sizeof(onode0);
   2516 		src = &onode0;
   2517 		memset(&onode0, 0, sz);
   2518 		onode0.sysctl0_flags = i->sysctl_flags;
   2519 		onode0.sysctl0_num = i->sysctl_num;
   2520 		onode0.sysctl0_size = i->sysctl_size;
   2521 		memcpy(onode0.sysctl0_name, i->sysctl_name, SYSCTL_NAMELEN);
   2522 		onode0.sysctl0_csize = i->sysctl_csize;
   2523 		onode0.sysctl0_clen = i->sysctl_clen;
   2524 		onode0.sysctl0_child = NULL; /* i->sysctl_child; */
   2525 		onode0.sysctl0_alias = i->sysctl_alias;
   2526 		onode0.sysctl0_data = i->sysctl_data;
   2527 		onode0.sysctl0_idata = i->sysctl_idata;
   2528 		onode0.sysctl0_qdata = i->sysctl_qdata;
   2529 		onode0.sysctl0_func = i->sysctl_func;
   2530 		onode0.sysctl0_parent = NULL; /* i->syctl_parent; */
   2531 		onode0.sysctl0_ver = i->sysctl_ver;
   2532 
   2533 		onode0.sysctl0_flags &= ~SYSCTL_VERS_MASK;
   2534 		onode0.sysctl0_flags |= SYSCTL_VERS_0;
   2535 		if (SYSCTL_TYPE(onode0.sysctl0_flags) == CTLTYPE_NODE &&
   2536 		    onode0.sysctl0_size == sizeof(*i))
   2537 			onode0.sysctl0_size = sizeof(onode0);
   2538 
   2539 		break;
   2540 
   2541 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
   2542 #error sysctl_cvt_out: no support for SYSCTL_VERSION
   2543 #endif /*  (SYSCTL_VERSION != SYSCTL_VERS_1) */
   2544 
   2545 	case SYSCTL_VERSION:
   2546 		/* nothing more to do here */
   2547 		break;
   2548 	}
   2549 
   2550 	if (ovp != NULL && left >= sz) {
   2551 		error = sysctl_copyout(l, src, ovp, sz);
   2552 		if (error)
   2553 			return (error);
   2554 	}
   2555 
   2556 	if (szp != NULL)
   2557 		*szp = sz;
   2558 
   2559 	return (0);
   2560 }
   2561