Home | History | Annotate | Line # | Download | only in gen
sysctl.c revision 1.18
      1 /*	$NetBSD: sysctl.c,v 1.18 2004/03/24 16:34:34 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.18 2004/03/24 16:34:34 atatat Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include "namespace.h"
     42 #include <sys/param.h>
     43 #include <sys/sysctl.h>
     44 
     45 #include <errno.h>
     46 #include <paths.h>
     47 #include <stdio.h>
     48 #include <string.h>
     49 #include <unistd.h>
     50 #include "extern.h"
     51 
     52 #ifdef __weak_alias
     53 __weak_alias(sysctl,_sysctl)
     54 #endif
     55 
     56 /*
     57  * handles requests off the user subtree
     58  */
     59 static int user_sysctl(int *, u_int, void *, size_t *, const void *, size_t);
     60 
     61 /*
     62  * copies out individual nodes taking target version into account
     63  */
     64 static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
     65 			     size_t *);
     66 
     67 #include <stdlib.h>
     68 
     69 int
     70 sysctl(name, namelen, oldp, oldlenp, newp, newlen)
     71 	int *name;
     72 	unsigned int namelen;
     73 	void *oldp;
     74 	const void *newp;
     75 	size_t *oldlenp, newlen;
     76 {
     77 	size_t oldlen, savelen;
     78 	int error;
     79 
     80 	if (name[0] != CTL_USER)
     81 		/* LINTED will fix when sysctl interface gets corrected */
     82 		/* XXX when will that be? */
     83 		return (__sysctl(name, namelen, oldp, oldlenp,
     84 				 (void *)newp, newlen));
     85 
     86 	oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
     87 	savelen = oldlen;
     88 	error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
     89 
     90 	if (error != 0) {
     91 		errno = error;
     92 		return (-1);
     93 	}
     94 
     95 	if (oldlenp != NULL) {
     96 		*oldlenp = oldlen;
     97 		if (oldp != NULL && oldlen > savelen) {
     98 			errno = ENOMEM;
     99 			return (-1);
    100 		}
    101 	}
    102 
    103 	return (0);
    104 }
    105 
    106 static int
    107 user_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    108 	int *name;
    109 	unsigned int namelen;
    110 	void *oldp;
    111 	const void *newp;
    112 	size_t *oldlenp, newlen;
    113 {
    114 #define _INT(s, n, v) {						\
    115 	.sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|	\
    116 			CTLTYPE_INT|SYSCTL_VERSION,		\
    117 	.sysctl_size = sizeof(int),				\
    118 	.sysctl_name = (s),					\
    119 	.sysctl_num = (n),					\
    120 	.sysctl_un = { .scu_idata = (v), }, }
    121 
    122 	/*
    123 	 * the nodes under the "user" node
    124 	 */
    125 	static const struct sysctlnode sysctl_usermib[] = {
    126 #if defined(lint)
    127 		/*
    128 		 * lint doesn't like my initializers
    129 		 */
    130 		0
    131 #else /* !lint */
    132 		{
    133 			.sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
    134 				CTLTYPE_STRING,
    135 			.sysctl_size = sizeof(_PATH_STDPATH),
    136 			.sysctl_name = "cs_path",
    137 			.sysctl_num = USER_CS_PATH,
    138 			.sysctl_data = _PATH_STDPATH,
    139 		},
    140 		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX),
    141 		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX),
    142 		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX),
    143 		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX),
    144 		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
    145 		     COLL_WEIGHTS_MAX),
    146 		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX),
    147 		_INT("line_max", USER_LINE_MAX, LINE_MAX),
    148 		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX),
    149 		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION),
    150 #ifdef POSIX2_C_BIND
    151 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1),
    152 #else
    153 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0),
    154 #endif
    155 #ifdef POSIX2_C_DEV
    156 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1),
    157 #else
    158 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0),
    159 #endif
    160 #ifdef POSIX2_CHAR_TERM
    161 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1),
    162 #else
    163 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0),
    164 #endif
    165 #ifdef POSIX2_FORT_DEV
    166 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1),
    167 #else
    168 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0),
    169 #endif
    170 #ifdef POSIX2_FORT_RUN
    171 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1),
    172 #else
    173 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0),
    174 #endif
    175 #ifdef POSIX2_LOCALEDEF
    176 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1),
    177 #else
    178 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0),
    179 #endif
    180 #ifdef POSIX2_SW_DEV
    181 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1),
    182 #else
    183 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0),
    184 #endif
    185 #ifdef POSIX2_UPE
    186 		_INT("posix2_upe", USER_POSIX2_UPE, 1),
    187 #else
    188 		_INT("posix2_upe", USER_POSIX2_UPE, 0),
    189 #endif
    190 		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX),
    191 		_INT("tzname_max", USER_TZNAME_MAX, NAME_MAX),
    192 		_INT("atexit_max", USER_ATEXIT_MAX, -1),
    193 #endif /* !lint */
    194 	};
    195 #undef _INT
    196 
    197 	static const int clen = sizeof(sysctl_usermib) /
    198 		sizeof(sysctl_usermib[0]);
    199 
    200 	const struct sysctlnode *node;
    201 	int ni;
    202 	size_t l, sz;
    203 
    204 	/*
    205 	 * none of these nodes are writable and they're all terminal (for now)
    206 	 */
    207 	if (namelen != 1)
    208 		return (EINVAL);
    209 
    210 	l = *oldlenp;
    211 	if (name[0] == CTL_QUERY) {
    212 		uint v;
    213 		node = newp;
    214 		if (node == NULL) {
    215 			v = SYSCTL_VERS_0;
    216 			newlen = sizeof(struct sysctlnode);
    217 		}
    218 		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_0 &&
    219 			 newlen == sizeof(struct sysctlnode))
    220 			v = SYSCTL_VERS_0;
    221 		else
    222 			return (EINVAL);
    223 
    224 		sz = 0;
    225 		for (ni = 0; ni < clen; ni++)
    226 			sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
    227 		*oldlenp = sz;
    228 		return (0);
    229 	}
    230 
    231 	/*
    232 	 * none of these nodes are writable
    233 	 */
    234 	if (newp != NULL || newlen != 0)
    235 		return (EPERM);
    236 
    237 	node = &sysctl_usermib[0];
    238 	for (ni = 0; ni	< clen; ni++)
    239 		if (name[0] == node[ni].sysctl_num)
    240 			break;
    241 	if (ni == clen)
    242 		return (EOPNOTSUPP);
    243 
    244 	node = &node[ni];
    245 	if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
    246 		switch (SYSCTL_TYPE(node->sysctl_flags)) {
    247 		case CTLTYPE_INT:
    248 			newp = &node->sysctl_idata;
    249 			break;
    250 		case CTLTYPE_QUAD:
    251 			newp = &node->sysctl_qdata;
    252 			break;
    253 		default:
    254 			return (EINVAL);
    255 		}
    256 	}
    257 	else
    258 		newp = node->sysctl_data;
    259 
    260 	l = MIN(l, node->sysctl_size);
    261 	if (oldp != NULL)
    262 		memcpy(oldp, newp, l);
    263 	*oldlenp = node->sysctl_size;
    264 
    265 	return (0);
    266 }
    267 
    268 static size_t
    269 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
    270 {
    271 	const void *src = n;
    272 	size_t sz;
    273 
    274 	switch (v) {
    275 	case SYSCTL_VERSION:
    276 		sz = sizeof(struct sysctlnode);
    277 		break;
    278 	default:
    279 		sz = 0;
    280 		break;
    281 	}
    282 
    283 	if (sz > 0 && *o != NULL && *l >= sz) {
    284 		memcpy(*o, src, sz);
    285 		*o = sz + (caddr_t)*o;
    286 		*l -= sz;
    287 	}
    288 
    289 	return(sz);
    290 }
    291