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