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