Home | History | Annotate | Line # | Download | only in gen
sysctl.c revision 1.30.6.2
      1  1.30.6.2  christos /*	$NetBSD: sysctl.c,v 1.30.6.2 2008/08/27 08:56:50 christos Exp $	*/
      2  1.30.6.2  christos 
      3  1.30.6.2  christos /*-
      4  1.30.6.2  christos  * Copyright (c) 1993
      5  1.30.6.2  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.30.6.2  christos  *
      7  1.30.6.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.30.6.2  christos  * modification, are permitted provided that the following conditions
      9  1.30.6.2  christos  * are met:
     10  1.30.6.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.30.6.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.30.6.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.30.6.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.30.6.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.30.6.2  christos  * 3. Neither the name of the University nor the names of its contributors
     16  1.30.6.2  christos  *    may be used to endorse or promote products derived from this software
     17  1.30.6.2  christos  *    without specific prior written permission.
     18  1.30.6.2  christos  *
     19  1.30.6.2  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.30.6.2  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.30.6.2  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.30.6.2  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.30.6.2  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.30.6.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.30.6.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.30.6.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.30.6.2  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.30.6.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.30.6.2  christos  * SUCH DAMAGE.
     30  1.30.6.2  christos  */
     31  1.30.6.2  christos 
     32  1.30.6.2  christos #include <sys/cdefs.h>
     33  1.30.6.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     34  1.30.6.2  christos #if 0
     35  1.30.6.2  christos static char sccsid[] = "@(#)sysctl.c	8.2 (Berkeley) 1/4/94";
     36  1.30.6.2  christos #else
     37  1.30.6.2  christos __RCSID("$NetBSD: sysctl.c,v 1.30.6.2 2008/08/27 08:56:50 christos Exp $");
     38  1.30.6.2  christos #endif
     39  1.30.6.2  christos #endif /* LIBC_SCCS and not lint */
     40  1.30.6.2  christos 
     41  1.30.6.2  christos #include "namespace.h"
     42  1.30.6.2  christos #include <sys/param.h>
     43  1.30.6.2  christos #define __COMPAT_SYSCTL
     44  1.30.6.2  christos #include <sys/sysctl.h>
     45  1.30.6.2  christos 
     46  1.30.6.2  christos #include <errno.h>
     47  1.30.6.2  christos #include <paths.h>
     48  1.30.6.2  christos #include <stdio.h>
     49  1.30.6.2  christos #include <string.h>
     50  1.30.6.2  christos #include <unistd.h>
     51  1.30.6.2  christos #include "extern.h"
     52  1.30.6.2  christos 
     53  1.30.6.2  christos #ifdef __weak_alias
     54  1.30.6.2  christos __weak_alias(sysctl,_sysctl)
     55  1.30.6.2  christos #endif
     56  1.30.6.2  christos 
     57  1.30.6.2  christos /*
     58  1.30.6.2  christos  * handles requests off the user subtree
     59  1.30.6.2  christos  */
     60  1.30.6.2  christos static int user_sysctl(const int *, u_int, void *, size_t *,
     61  1.30.6.2  christos 			const void *, size_t);
     62  1.30.6.2  christos 
     63  1.30.6.2  christos /*
     64  1.30.6.2  christos  * copies out individual nodes taking target version into account
     65  1.30.6.2  christos  */
     66  1.30.6.2  christos static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
     67  1.30.6.2  christos 			     size_t *);
     68  1.30.6.2  christos 
     69  1.30.6.2  christos #include <stdlib.h>
     70  1.30.6.2  christos 
     71  1.30.6.2  christos int
     72  1.30.6.2  christos sysctl(name, namelen, oldp, oldlenp, newp, newlen)
     73  1.30.6.2  christos 	const int *name;
     74  1.30.6.2  christos 	unsigned int namelen;
     75  1.30.6.2  christos 	void *oldp;
     76  1.30.6.2  christos 	const void *newp;
     77  1.30.6.2  christos 	size_t *oldlenp, newlen;
     78  1.30.6.2  christos {
     79  1.30.6.2  christos 	size_t oldlen, savelen;
     80  1.30.6.2  christos 	int error;
     81  1.30.6.2  christos 
     82  1.30.6.2  christos 	if (name[0] != CTL_USER)
     83  1.30.6.2  christos 		return (__sysctl(name, namelen, oldp, oldlenp,
     84  1.30.6.2  christos 				 newp, newlen));
     85  1.30.6.2  christos 
     86  1.30.6.2  christos 	oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
     87  1.30.6.2  christos 	savelen = oldlen;
     88  1.30.6.2  christos 	error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
     89  1.30.6.2  christos 
     90  1.30.6.2  christos 	if (error != 0) {
     91  1.30.6.2  christos 		errno = error;
     92  1.30.6.2  christos 		return (-1);
     93  1.30.6.2  christos 	}
     94  1.30.6.2  christos 
     95  1.30.6.2  christos 	if (oldlenp != NULL) {
     96  1.30.6.2  christos 		*oldlenp = oldlen;
     97  1.30.6.2  christos 		if (oldp != NULL && oldlen > savelen) {
     98  1.30.6.2  christos 			errno = ENOMEM;
     99  1.30.6.2  christos 			return (-1);
    100  1.30.6.2  christos 		}
    101  1.30.6.2  christos 	}
    102  1.30.6.2  christos 
    103  1.30.6.2  christos 	return (0);
    104  1.30.6.2  christos }
    105  1.30.6.2  christos 
    106  1.30.6.2  christos static int
    107  1.30.6.2  christos user_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    108  1.30.6.2  christos 	const int *name;
    109  1.30.6.2  christos 	unsigned int namelen;
    110  1.30.6.2  christos 	void *oldp;
    111  1.30.6.2  christos 	const void *newp;
    112  1.30.6.2  christos 	size_t *oldlenp, newlen;
    113  1.30.6.2  christos {
    114  1.30.6.2  christos #define _INT(s, n, v, d) {					\
    115  1.30.6.2  christos 	.sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|	\
    116  1.30.6.2  christos 			CTLTYPE_INT|SYSCTL_VERSION,		\
    117  1.30.6.2  christos 	sysc_init_field(_sysctl_size, sizeof(int)),		\
    118  1.30.6.2  christos 	.sysctl_name = (s),					\
    119  1.30.6.2  christos 	.sysctl_num = (n),					\
    120  1.30.6.2  christos 	.sysctl_un = { .scu_idata = (v), },			\
    121  1.30.6.2  christos 	sysc_init_field(_sysctl_desc, (d)),			\
    122  1.30.6.2  christos 	}
    123  1.30.6.2  christos 
    124  1.30.6.2  christos 	/*
    125  1.30.6.2  christos 	 * the nodes under the "user" node
    126  1.30.6.2  christos 	 */
    127  1.30.6.2  christos 	static const struct sysctlnode sysctl_usermib[] = {
    128  1.30.6.2  christos #if defined(lint)
    129  1.30.6.2  christos 		/*
    130  1.30.6.2  christos 		 * lint doesn't like my initializers
    131  1.30.6.2  christos 		 */
    132  1.30.6.2  christos 		0
    133  1.30.6.2  christos #else /* !lint */
    134  1.30.6.2  christos 		{
    135  1.30.6.2  christos 			.sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
    136  1.30.6.2  christos 				CTLTYPE_STRING,
    137  1.30.6.2  christos 			sysc_init_field(_sysctl_size, sizeof(_PATH_STDPATH)),
    138  1.30.6.2  christos 			.sysctl_name = "cs_path",
    139  1.30.6.2  christos 			.sysctl_num = USER_CS_PATH,
    140  1.30.6.2  christos 			/*
    141  1.30.6.2  christos 			 * XXX these nasty initializers (and the one in
    142  1.30.6.2  christos 			 * the _INT() macro) can go away once all ports
    143  1.30.6.2  christos 			 * are using gcc3, and become
    144  1.30.6.2  christos 			 *
    145  1.30.6.2  christos 			 *	.sysctl_data = _PATH_STDPATH,
    146  1.30.6.2  christos 			 *	.sysctl_desc = NULL,
    147  1.30.6.2  christos 			 */
    148  1.30.6.2  christos 			.sysctl_un = { .scu_data = {
    149  1.30.6.2  christos 				sysc_init_field(_sud_data,
    150  1.30.6.2  christos 				__UNCONST(_PATH_STDPATH)),
    151  1.30.6.2  christos 				}, },
    152  1.30.6.2  christos 			sysc_init_field(_sysctl_desc,
    153  1.30.6.2  christos 				"A value for the PATH environment variable "
    154  1.30.6.2  christos 				"that finds all the standard utilities"),
    155  1.30.6.2  christos 		},
    156  1.30.6.2  christos 		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX,
    157  1.30.6.2  christos 		     "The maximum ibase/obase values in the bc(1) utility"),
    158  1.30.6.2  christos 		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX,
    159  1.30.6.2  christos 		     "The maximum array size in the bc(1) utility"),
    160  1.30.6.2  christos 		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX,
    161  1.30.6.2  christos 		     "The maximum scale value in the bc(1) utility"),
    162  1.30.6.2  christos 		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX,
    163  1.30.6.2  christos 		     "The maximum string length in the bc(1) utility"),
    164  1.30.6.2  christos 		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
    165  1.30.6.2  christos 		     COLL_WEIGHTS_MAX, "The maximum number of weights that can "
    166  1.30.6.2  christos 		     "be assigned to any entry of the LC_COLLATE order keyword "
    167  1.30.6.2  christos 		     "in the locale definition file"),
    168  1.30.6.2  christos 		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX,
    169  1.30.6.2  christos 		     "The maximum number of expressions that can be nested "
    170  1.30.6.2  christos 		     "within parenthesis by the expr(1) utility"),
    171  1.30.6.2  christos 		_INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length "
    172  1.30.6.2  christos 		     "in bytes of a text-processing utility's input line"),
    173  1.30.6.2  christos 		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum "
    174  1.30.6.2  christos 		     "number of repeated occurrences of a regular expression "
    175  1.30.6.2  christos 		     "permitted when using interval notation"),
    176  1.30.6.2  christos 		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION,
    177  1.30.6.2  christos 		     "The version of POSIX 1003.2 with which the system "
    178  1.30.6.2  christos 		     "attempts to comply"),
    179  1.30.6.2  christos #ifdef _POSIX2_C_BIND
    180  1.30.6.2  christos 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1,
    181  1.30.6.2  christos 		     "Whether the system's C-language development facilities "
    182  1.30.6.2  christos 		     "support the C-Language Bindings Option"),
    183  1.30.6.2  christos #else
    184  1.30.6.2  christos 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0,
    185  1.30.6.2  christos 		     "Whether the system's C-language development facilities "
    186  1.30.6.2  christos 		     "support the C-Language Bindings Option"),
    187  1.30.6.2  christos #endif
    188  1.30.6.2  christos #ifdef POSIX2_C_DEV
    189  1.30.6.2  christos 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1,
    190  1.30.6.2  christos 		     "Whether the system supports the C-Language Development "
    191  1.30.6.2  christos 		     "Utilities Option"),
    192  1.30.6.2  christos #else
    193  1.30.6.2  christos 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0,
    194  1.30.6.2  christos 		     "Whether the system supports the C-Language Development "
    195  1.30.6.2  christos 		     "Utilities Option"),
    196  1.30.6.2  christos #endif
    197  1.30.6.2  christos #ifdef POSIX2_CHAR_TERM
    198  1.30.6.2  christos 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1,
    199  1.30.6.2  christos 		     "Whether the system supports at least one terminal type "
    200  1.30.6.2  christos 		     "capable of all operations described in POSIX 1003.2"),
    201  1.30.6.2  christos #else
    202  1.30.6.2  christos 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0,
    203  1.30.6.2  christos 		     "Whether the system supports at least one terminal type "
    204  1.30.6.2  christos 		     "capable of all operations described in POSIX 1003.2"),
    205  1.30.6.2  christos #endif
    206  1.30.6.2  christos #ifdef POSIX2_FORT_DEV
    207  1.30.6.2  christos 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1,
    208  1.30.6.2  christos 		     "Whether the system supports the FORTRAN Development "
    209  1.30.6.2  christos 		     "Utilities Option"),
    210  1.30.6.2  christos #else
    211  1.30.6.2  christos 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0,
    212  1.30.6.2  christos 		     "Whether the system supports the FORTRAN Development "
    213  1.30.6.2  christos 		     "Utilities Option"),
    214  1.30.6.2  christos #endif
    215  1.30.6.2  christos #ifdef POSIX2_FORT_RUN
    216  1.30.6.2  christos 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1,
    217  1.30.6.2  christos 		     "Whether the system supports the FORTRAN Runtime "
    218  1.30.6.2  christos 		     "Utilities Option"),
    219  1.30.6.2  christos #else
    220  1.30.6.2  christos 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0,
    221  1.30.6.2  christos 		     "Whether the system supports the FORTRAN Runtime "
    222  1.30.6.2  christos 		     "Utilities Option"),
    223  1.30.6.2  christos #endif
    224  1.30.6.2  christos #ifdef POSIX2_LOCALEDEF
    225  1.30.6.2  christos 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1,
    226  1.30.6.2  christos 		     "Whether the system supports the creation of locales"),
    227  1.30.6.2  christos #else
    228  1.30.6.2  christos 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0,
    229  1.30.6.2  christos 		     "Whether the system supports the creation of locales"),
    230  1.30.6.2  christos #endif
    231  1.30.6.2  christos #ifdef POSIX2_SW_DEV
    232  1.30.6.2  christos 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1,
    233  1.30.6.2  christos 		     "Whether the system supports the Software Development "
    234  1.30.6.2  christos 		     "Utilities Option"),
    235  1.30.6.2  christos #else
    236  1.30.6.2  christos 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0,
    237  1.30.6.2  christos 		     "Whether the system supports the Software Development "
    238  1.30.6.2  christos 		     "Utilities Option"),
    239  1.30.6.2  christos #endif
    240  1.30.6.2  christos #ifdef POSIX2_UPE
    241  1.30.6.2  christos 		_INT("posix2_upe", USER_POSIX2_UPE, 1,
    242  1.30.6.2  christos 		     "Whether the system supports the User Portability "
    243  1.30.6.2  christos 		     "Utilities Option"),
    244  1.30.6.2  christos #else
    245  1.30.6.2  christos 		_INT("posix2_upe", USER_POSIX2_UPE, 0,
    246  1.30.6.2  christos 		     "Whether the system supports the User Portability "
    247  1.30.6.2  christos 		     "Utilities Option"),
    248  1.30.6.2  christos #endif
    249  1.30.6.2  christos 		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX,
    250  1.30.6.2  christos 		     "The minimum maximum number of streams that a process "
    251  1.30.6.2  christos 		     "may have open at any one time"),
    252  1.30.6.2  christos 		_INT("tzname_max", USER_TZNAME_MAX, NAME_MAX,
    253  1.30.6.2  christos 		     "The minimum maximum number of types supported for the "
    254  1.30.6.2  christos 		     "name of a timezone"),
    255  1.30.6.2  christos 		_INT("atexit_max", USER_ATEXIT_MAX, -1,
    256  1.30.6.2  christos 		     "The maximum number of functions that may be registered "
    257  1.30.6.2  christos 		     "with atexit(3)"),
    258  1.30.6.2  christos #endif /* !lint */
    259  1.30.6.2  christos 	};
    260  1.30.6.2  christos #undef _INT
    261  1.30.6.2  christos 
    262  1.30.6.2  christos 	static const int clen = sizeof(sysctl_usermib) /
    263  1.30.6.2  christos 		sizeof(sysctl_usermib[0]);
    264  1.30.6.2  christos 
    265  1.30.6.2  christos 	const struct sysctlnode *node;
    266  1.30.6.2  christos 	int ni;
    267  1.30.6.2  christos 	size_t l, sz;
    268  1.30.6.2  christos 
    269  1.30.6.2  christos 	/*
    270  1.30.6.2  christos 	 * none of these nodes are writable and they're all terminal (for now)
    271  1.30.6.2  christos 	 */
    272  1.30.6.2  christos 	if (namelen != 1)
    273  1.30.6.2  christos 		return (EINVAL);
    274  1.30.6.2  christos 
    275  1.30.6.2  christos 	l = *oldlenp;
    276  1.30.6.2  christos 	if (name[0] == CTL_QUERY) {
    277  1.30.6.2  christos 		uint v;
    278  1.30.6.2  christos 		node = newp;
    279  1.30.6.2  christos 		if (node == NULL)
    280  1.30.6.2  christos 			return (EINVAL);
    281  1.30.6.2  christos 		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
    282  1.30.6.2  christos 			 newlen == sizeof(struct sysctlnode))
    283  1.30.6.2  christos 			v = SYSCTL_VERS_1;
    284  1.30.6.2  christos 		else
    285  1.30.6.2  christos 			return (EINVAL);
    286  1.30.6.2  christos 
    287  1.30.6.2  christos 		sz = 0;
    288  1.30.6.2  christos 		for (ni = 0; ni < clen; ni++)
    289  1.30.6.2  christos 			sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
    290  1.30.6.2  christos 		*oldlenp = sz;
    291  1.30.6.2  christos 		return (0);
    292  1.30.6.2  christos 	}
    293  1.30.6.2  christos 
    294  1.30.6.2  christos 	if (name[0] == CTL_DESCRIBE) {
    295  1.30.6.2  christos 		/*
    296  1.30.6.2  christos 		 * XXX make sure this is larger than the largest
    297  1.30.6.2  christos 		 * "user" description
    298  1.30.6.2  christos 		 */
    299  1.30.6.2  christos 		char buf[192];
    300  1.30.6.2  christos 		struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp;
    301  1.30.6.2  christos 		size_t d;
    302  1.30.6.2  christos 
    303  1.30.6.2  christos 		node = newp;
    304  1.30.6.2  christos 		if (node != NULL &&
    305  1.30.6.2  christos 		    (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 ||
    306  1.30.6.2  christos 		     newlen != sizeof(struct sysctlnode)))
    307  1.30.6.2  christos 			return (EINVAL);
    308  1.30.6.2  christos 
    309  1.30.6.2  christos 		sz = 0;
    310  1.30.6.2  christos 		for (ni = 0; ni < clen; ni++) {
    311  1.30.6.2  christos 			memset(&buf[0], 0, sizeof(buf));
    312  1.30.6.2  christos 			if (node != NULL &&
    313  1.30.6.2  christos 			    node->sysctl_num != sysctl_usermib[ni].sysctl_num)
    314  1.30.6.2  christos 				continue;
    315  1.30.6.2  christos 			d1->descr_num = sysctl_usermib[ni].sysctl_num;
    316  1.30.6.2  christos 			d1->descr_ver = sysctl_usermib[ni].sysctl_ver;
    317  1.30.6.2  christos 			if (sysctl_usermib[ni].sysctl_desc == NULL)
    318  1.30.6.2  christos 				d1->descr_len = 1;
    319  1.30.6.2  christos 			else {
    320  1.30.6.2  christos 				(void)strlcpy(d1->descr_str,
    321  1.30.6.2  christos 					sysctl_usermib[ni].sysctl_desc,
    322  1.30.6.2  christos 					sizeof(buf) - sizeof(*d1));
    323  1.30.6.2  christos 				d1->descr_len = strlen(d1->descr_str) + 1;
    324  1.30.6.2  christos 			}
    325  1.30.6.2  christos 			d = (size_t)__sysc_desc_adv(NULL, d1->descr_len);
    326  1.30.6.2  christos 			if (d2 != NULL)
    327  1.30.6.2  christos 				memcpy(d2, d1, d);
    328  1.30.6.2  christos 			sz += d;
    329  1.30.6.2  christos 			if (node != NULL)
    330  1.30.6.2  christos 				break;
    331  1.30.6.2  christos 		}
    332  1.30.6.2  christos 		*oldlenp = sz;
    333  1.30.6.2  christos 		if (sz == 0 && node != NULL)
    334  1.30.6.2  christos 			return (ENOENT);
    335  1.30.6.2  christos 		return (0);
    336  1.30.6.2  christos 
    337  1.30.6.2  christos 	}
    338  1.30.6.2  christos 
    339  1.30.6.2  christos 	/*
    340  1.30.6.2  christos 	 * none of these nodes are writable
    341  1.30.6.2  christos 	 */
    342  1.30.6.2  christos 	if (newp != NULL || newlen != 0)
    343  1.30.6.2  christos 		return (EPERM);
    344  1.30.6.2  christos 
    345  1.30.6.2  christos 	node = &sysctl_usermib[0];
    346  1.30.6.2  christos 	for (ni = 0; ni	< clen; ni++)
    347  1.30.6.2  christos 		if (name[0] == node[ni].sysctl_num)
    348  1.30.6.2  christos 			break;
    349  1.30.6.2  christos 	if (ni == clen)
    350  1.30.6.2  christos 		return (EOPNOTSUPP);
    351  1.30.6.2  christos 
    352  1.30.6.2  christos 	node = &node[ni];
    353  1.30.6.2  christos 	if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
    354  1.30.6.2  christos 		switch (SYSCTL_TYPE(node->sysctl_flags)) {
    355  1.30.6.2  christos 		case CTLTYPE_INT:
    356  1.30.6.2  christos 			newp = &node->sysctl_idata;
    357  1.30.6.2  christos 			break;
    358  1.30.6.2  christos 		case CTLTYPE_QUAD:
    359  1.30.6.2  christos 			newp = &node->sysctl_qdata;
    360  1.30.6.2  christos 			break;
    361  1.30.6.2  christos 		default:
    362  1.30.6.2  christos 			return (EINVAL);
    363  1.30.6.2  christos 		}
    364  1.30.6.2  christos 	}
    365  1.30.6.2  christos 	else
    366  1.30.6.2  christos 		newp = node->sysctl_data;
    367  1.30.6.2  christos 
    368  1.30.6.2  christos 	l = MIN(l, node->sysctl_size);
    369  1.30.6.2  christos 	if (oldp != NULL)
    370  1.30.6.2  christos 		memcpy(oldp, newp, l);
    371  1.30.6.2  christos 	*oldlenp = node->sysctl_size;
    372  1.30.6.2  christos 
    373  1.30.6.2  christos 	return (0);
    374  1.30.6.2  christos }
    375  1.30.6.2  christos 
    376  1.30.6.2  christos static size_t
    377  1.30.6.2  christos __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
    378  1.30.6.2  christos {
    379  1.30.6.2  christos 	const void *src = n;
    380  1.30.6.2  christos 	size_t sz;
    381  1.30.6.2  christos 
    382  1.30.6.2  christos 	switch (v) {
    383  1.30.6.2  christos #if (SYSCTL_VERSION != SYSCTL_VERS_1)
    384  1.30.6.2  christos #error __cvt_node_out: no support for SYSCTL_VERSION
    385  1.30.6.2  christos #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
    386  1.30.6.2  christos 
    387  1.30.6.2  christos 	case SYSCTL_VERSION:
    388  1.30.6.2  christos 		sz = sizeof(struct sysctlnode);
    389  1.30.6.2  christos 		break;
    390  1.30.6.2  christos 
    391  1.30.6.2  christos 	default:
    392  1.30.6.2  christos 		sz = 0;
    393  1.30.6.2  christos 		break;
    394  1.30.6.2  christos 	}
    395  1.30.6.2  christos 
    396  1.30.6.2  christos 	if (sz > 0 && *o != NULL && *l >= sz) {
    397  1.30.6.2  christos 		memcpy(*o, src, sz);
    398  1.30.6.2  christos 		*o = sz + (caddr_t)*o;
    399  1.30.6.2  christos 		*l -= sz;
    400  1.30.6.2  christos 	}
    401  1.30.6.2  christos 
    402  1.30.6.2  christos 	return(sz);
    403  1.30.6.2  christos }
    404