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