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