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