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