Home | History | Annotate | Line # | Download | only in gen
sysctl.c revision 1.15
      1 /*	$NetBSD: sysctl.c,v 1.15 2003/12/04 19:45:19 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.15 2003/12/04 19:45:19 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 #include <stdlib.h>
     62 
     63 int
     64 sysctl(name, namelen, oldp, oldlenp, newp, newlen)
     65 	int *name;
     66 	unsigned int namelen;
     67 	void *oldp;
     68 	const void *newp;
     69 	size_t *oldlenp, newlen;
     70 {
     71 	size_t oldlen, savelen;
     72 	int error;
     73 
     74 	if (name[0] != CTL_USER)
     75 		/* LINTED will fix when sysctl interface gets corrected */
     76 		/* XXX when will that be? */
     77 		return (__sysctl(name, namelen, oldp, oldlenp,
     78 				 (void *)newp, newlen));
     79 
     80 	oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
     81 	savelen = oldlen;
     82 	error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
     83 
     84 	if (error != 0) {
     85 		errno = error;
     86 		return (-1);
     87 	}
     88 
     89 	if (oldlenp != NULL) {
     90 		*oldlenp = oldlen;
     91 		if (oldp != NULL && oldlen > savelen) {
     92 			errno = ENOMEM;
     93 			return (-1);
     94 		}
     95 	}
     96 
     97 	return (0);
     98 }
     99 
    100 static int
    101 user_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    102 	int *name;
    103 	unsigned int namelen;
    104 	void *oldp;
    105 	const void *newp;
    106 	size_t *oldlenp, newlen;
    107 {
    108 #define _INT(s, n, v) {						\
    109 	.sysctl_flags = SYSCTL_IMMEDIATE|SYSCTL_PERMANENT|SYSCTL_READONLY| \
    110 			SYSCTL_TYPE(CTLTYPE_INT),		\
    111 	.sysctl_size = sizeof(int),				\
    112 	.sysctl_name = (s),					\
    113 	.sysctl_num = (n),					\
    114 	.sysctl_un = { .scu_idata = (v), }, }
    115 
    116 	/*
    117 	 * the nodes under the "user" node
    118 	 */
    119 	static const struct sysctlnode sysctl_usermib[] = {
    120 #if defined(lint)
    121 		/*
    122 		 * lint doesn't like my initializers
    123 		 */
    124 		0
    125 #else /* !lint */
    126 		{
    127 			.sysctl_flags = SYSCTL_READONLY|SYSCTL_PERMANENT|
    128 				SYSCTL_TYPE(CTLTYPE_STRING),
    129 			.sysctl_size = sizeof(_PATH_STDPATH),
    130 			.sysctl_name = "cs_path",
    131 			.sysctl_num = USER_CS_PATH,
    132 			.sysctl_un = { .scu_data = _PATH_STDPATH, },
    133 		},
    134 		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX),
    135 		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX),
    136 		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX),
    137 		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX),
    138 		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
    139 		     COLL_WEIGHTS_MAX),
    140 		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX),
    141 		_INT("line_max", USER_LINE_MAX, LINE_MAX),
    142 		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX),
    143 		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION),
    144 #ifdef POSIX2_C_BIND
    145 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1),
    146 #else
    147 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0),
    148 #endif
    149 #ifdef POSIX2_C_DEV
    150 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1),
    151 #else
    152 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0),
    153 #endif
    154 #ifdef POSIX2_CHAR_TERM
    155 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1),
    156 #else
    157 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0),
    158 #endif
    159 #ifdef POSIX2_FORT_DEV
    160 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1),
    161 #else
    162 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0),
    163 #endif
    164 #ifdef POSIX2_FORT_RUN
    165 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1),
    166 #else
    167 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0),
    168 #endif
    169 #ifdef POSIX2_LOCALEDEF
    170 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1),
    171 #else
    172 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0),
    173 #endif
    174 #ifdef POSIX2_SW_DEV
    175 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1),
    176 #else
    177 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0),
    178 #endif
    179 #ifdef POSIX2_UPE
    180 		_INT("posix2_upe", USER_POSIX2_UPE, 1),
    181 #else
    182 		_INT("posix2_upe", USER_POSIX2_UPE, 0),
    183 #endif
    184 		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX),
    185 		_INT("tzname_max", USER_TZNAME_MAX, NAME_MAX),
    186 		_INT("atexit_max", USER_ATEXIT_MAX, -1),
    187 #endif /* !lint */
    188 	};
    189 #undef _INT
    190 
    191 	static const int clen = sizeof(sysctl_usermib) /
    192 		sizeof(sysctl_usermib[0]);
    193 
    194 	const struct sysctlnode *node;
    195 	int ni;
    196 	size_t l, sz;
    197 
    198 	/*
    199 	 * none of these nodes are writable and they're all terminal (for now)
    200 	 */
    201 	if (newp != NULL || newlen != 0)
    202 		return (EPERM);
    203 	if (namelen != 1)
    204 		return (EINVAL);
    205 
    206 	l = *oldlenp;
    207 	if (name[0] == CTL_QUERY) {
    208 		sz = clen * sizeof(struct sysctlnode);
    209 		l = MIN(l, sz);
    210 		if (oldp != NULL)
    211 			memcpy(oldp, &sysctl_usermib[0], l);
    212 		*oldlenp = sz;
    213 		return (0);
    214 	}
    215 
    216 	node = &sysctl_usermib[0];
    217 	for (ni = 0; ni	< clen; ni++)
    218 		if (name[0] == node[ni].sysctl_num)
    219 			break;
    220 	if (ni == clen)
    221 		return (EOPNOTSUPP);
    222 
    223 	node = &node[ni];
    224 	if (node->sysctl_flags & SYSCTL_IMMEDIATE) {
    225 		switch (SYSCTL_TYPE(node->sysctl_flags)) {
    226 		case CTLTYPE_INT:
    227 			newp = &node->sysctl_idata;
    228 			break;
    229 		case CTLTYPE_QUAD:
    230 			newp = &node->sysctl_qdata;
    231 			break;
    232 		default:
    233 			return (EINVAL);
    234 		}
    235 	}
    236 	else
    237 		newp = node->sysctl_data;
    238 
    239 	l = MIN(l, node->sysctl_size);
    240 	if (oldp != NULL)
    241 		memcpy(oldp, newp, l);
    242 	*oldlenp = node->sysctl_size;
    243 
    244 	return (0);
    245 }
    246