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