Home | History | Annotate | Line # | Download | only in gen
sysctl.c revision 1.20
      1 /*	$NetBSD: sysctl.c,v 1.20 2004/03/24 19:31:46 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.20 2004/03/24 19:31:46 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 	.sysctl_size = sizeof(int),				\
    119 	.sysctl_name = (s),					\
    120 	.sysctl_num = (n),					\
    121 	.sysctl_idata = (v),					\
    122 	.sysctl_desc = (d), }
    123 
    124 	/*
    125 	 * the nodes under the "user" node
    126 	 */
    127 	static const struct sysctlnode sysctl_usermib[] = {
    128 #if defined(lint)
    129 		/*
    130 		 * lint doesn't like my initializers
    131 		 */
    132 		0
    133 #else /* !lint */
    134 		{
    135 			.sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
    136 				CTLTYPE_STRING,
    137 			.sysctl_size = sizeof(_PATH_STDPATH),
    138 			.sysctl_name = "cs_path",
    139 			.sysctl_num = USER_CS_PATH,
    140 			.sysctl_data = _PATH_STDPATH,
    141 			.sysctl_desc = NULL,
    142 		},
    143 		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX, NULL),
    144 		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX, NULL),
    145 		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX, NULL),
    146 		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX, NULL),
    147 		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
    148 		     COLL_WEIGHTS_MAX, NULL),
    149 		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX, NULL),
    150 		_INT("line_max", USER_LINE_MAX, LINE_MAX, NULL),
    151 		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, NULL),
    152 		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION, NULL),
    153 #ifdef POSIX2_C_BIND
    154 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1, NULL),
    155 #else
    156 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0, NULL),
    157 #endif
    158 #ifdef POSIX2_C_DEV
    159 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1, NULL),
    160 #else
    161 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0, NULL),
    162 #endif
    163 #ifdef POSIX2_CHAR_TERM
    164 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1, NULL),
    165 #else
    166 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0, NULL),
    167 #endif
    168 #ifdef POSIX2_FORT_DEV
    169 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1, NULL),
    170 #else
    171 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0, NULL),
    172 #endif
    173 #ifdef POSIX2_FORT_RUN
    174 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1, NULL),
    175 #else
    176 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0, NULL),
    177 #endif
    178 #ifdef POSIX2_LOCALEDEF
    179 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1, NULL),
    180 #else
    181 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0, NULL),
    182 #endif
    183 #ifdef POSIX2_SW_DEV
    184 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1, NULL),
    185 #else
    186 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0, NULL),
    187 #endif
    188 #ifdef POSIX2_UPE
    189 		_INT("posix2_upe", USER_POSIX2_UPE, 1, NULL),
    190 #else
    191 		_INT("posix2_upe", USER_POSIX2_UPE, 0, NULL),
    192 #endif
    193 		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX, NULL),
    194 		_INT("tzname_max", USER_TZNAME_MAX, NAME_MAX, NULL),
    195 		_INT("atexit_max", USER_ATEXIT_MAX, -1, NULL),
    196 #endif /* !lint */
    197 	};
    198 #undef _INT
    199 
    200 	static const int clen = sizeof(sysctl_usermib) /
    201 		sizeof(sysctl_usermib[0]);
    202 
    203 	const struct sysctlnode *node;
    204 	int ni;
    205 	size_t l, sz;
    206 
    207 	/*
    208 	 * none of these nodes are writable and they're all terminal (for now)
    209 	 */
    210 	if (namelen != 1)
    211 		return (EINVAL);
    212 
    213 	l = *oldlenp;
    214 	if (name[0] == CTL_QUERY) {
    215 		uint v;
    216 		node = newp;
    217 		if (node == NULL) {
    218 			v = SYSCTL_VERS_0;
    219 			newlen = sizeof(struct sysctlnode);
    220 		}
    221 		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_0 &&
    222 			 newlen == sizeof(struct sysctlnode0))
    223 			v = SYSCTL_VERS_0;
    224 		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
    225 			 newlen == sizeof(struct sysctlnode))
    226 			v = SYSCTL_VERS_1;
    227 		else
    228 			return (EINVAL);
    229 
    230 		sz = 0;
    231 		for (ni = 0; ni < clen; ni++)
    232 			sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
    233 		*oldlenp = sz;
    234 		return (0);
    235 	}
    236 
    237 	if (name[0] == CTL_DESCRIBE) {
    238 		char buf[128];
    239 		struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp;
    240 		size_t d;
    241 
    242 		node = newp;
    243 		if (node != NULL &&
    244 		    (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 ||
    245 		     newlen != sizeof(struct sysctlnode)))
    246 			return (EINVAL);
    247 
    248 		sz = 0;
    249 		for (ni = 0; ni < clen; ni++) {
    250 			memset(&buf[0], 0, sizeof(buf));
    251 			if (node != NULL &&
    252 			    node->sysctl_num != sysctl_usermib[ni].sysctl_num)
    253 				continue;
    254 			d1->descr_num = sysctl_usermib[ni].sysctl_num;
    255 			d1->descr_ver = sysctl_usermib[ni].sysctl_ver;
    256 			if (sysctl_usermib[ni].sysctl_desc == NULL)
    257 				d1->descr_len = 1;
    258 			else {
    259 				strncpy(d1->descr_str,
    260 					sysctl_usermib[ni].sysctl_desc,
    261 					sizeof(buf) - sizeof(*d1));
    262 				buf[sizeof(buf) - 1] = '\0';
    263 				d1->descr_len = strlen(d1->descr_str) + 1;
    264 			}
    265 			d = (size_t)__sysc_desc_adv(NULL, d1->descr_len);
    266 			if (d2 != NULL)
    267 				memcpy(d2, d1, d);
    268 			sz += d;
    269 			if (node != NULL)
    270 				break;
    271 		}
    272 		*oldlenp = sz;
    273 		if (sz == 0 && node != NULL)
    274 			return (ENOENT);
    275 		return (0);
    276 
    277 	}
    278 
    279 	/*
    280 	 * none of these nodes are writable
    281 	 */
    282 	if (newp != NULL || newlen != 0)
    283 		return (EPERM);
    284 
    285 	node = &sysctl_usermib[0];
    286 	for (ni = 0; ni	< clen; ni++)
    287 		if (name[0] == node[ni].sysctl_num)
    288 			break;
    289 	if (ni == clen)
    290 		return (EOPNOTSUPP);
    291 
    292 	node = &node[ni];
    293 	if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
    294 		switch (SYSCTL_TYPE(node->sysctl_flags)) {
    295 		case CTLTYPE_INT:
    296 			newp = &node->sysctl_idata;
    297 			break;
    298 		case CTLTYPE_QUAD:
    299 			newp = &node->sysctl_qdata;
    300 			break;
    301 		default:
    302 			return (EINVAL);
    303 		}
    304 	}
    305 	else
    306 		newp = node->sysctl_data;
    307 
    308 	l = MIN(l, node->sysctl_size);
    309 	if (oldp != NULL)
    310 		memcpy(oldp, newp, l);
    311 	*oldlenp = node->sysctl_size;
    312 
    313 	return (0);
    314 }
    315 
    316 static size_t
    317 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
    318 {
    319 	struct sysctlnode0 node0;
    320 	const void *src = n;
    321 	size_t sz;
    322 
    323 	switch (v) {
    324 	    case SYSCTL_VERS_0:
    325 		memset(&node0, 0, sizeof(node0));
    326 		node0.sysctl0_flags = n->sysctl_flags;
    327 		node0.sysctl0_num = n->sysctl_num;
    328 		node0.sysctl0_size = n->sysctl_size;
    329 		memcpy(node0.sysctl0_name, n->sysctl_name, SYSCTL_NAMELEN);
    330 		node0.sysctl0_csize = n->sysctl_csize;
    331 		node0.sysctl0_clen = n->sysctl_clen;
    332 		node0.sysctl0_child = NULL;
    333 		node0.sysctl0_alias = n->sysctl_alias;
    334 		node0.sysctl0_idata = n->sysctl_idata;
    335 		node0.sysctl0_qdata = n->sysctl_qdata;
    336 		node0.sysctl0_data = n->sysctl_data;
    337 		node0.sysctl0_func = NULL;
    338 		node0.sysctl0_parent = NULL;
    339 		node0.sysctl0_ver = n->sysctl_ver;
    340 		node0.sysctl0_flags &= ~SYSCTL_VERS_MASK;
    341 		node0.sysctl0_flags |= SYSCTL_VERS_0;
    342 		src = &node0;
    343 		sz = sizeof(node0);
    344 		break;
    345 
    346 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
    347 #error __cvt_node_out: no support for SYSCTL_VERSION
    348 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
    349 
    350 	case SYSCTL_VERSION:
    351 		sz = sizeof(struct sysctlnode);
    352 		break;
    353 	default:
    354 		sz = 0;
    355 		break;
    356 	}
    357 
    358 	if (sz > 0 && *o != NULL && *l >= sz) {
    359 		memcpy(*o, src, sz);
    360 		*o = sz + (caddr_t)*o;
    361 		*l -= sz;
    362 	}
    363 
    364 	return(sz);
    365 }
    366