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