sysctl.c revision 1.19
1/*	$NetBSD: sysctl.c,v 1.19 2004/03/24 17:21:02 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
35static char sccsid[] = "@(#)sysctl.c	8.2 (Berkeley) 1/4/94";
36#else
37__RCSID("$NetBSD: sysctl.c,v 1.19 2004/03/24 17:21:02 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 */
60static 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 */
65static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
66			     size_t *);
67
68#include <stdlib.h>
69
70int
71sysctl(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
107static int
108user_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) {						\
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_un = { .scu_idata = (v), }, }
122
123	/*
124	 * the nodes under the "user" node
125	 */
126	static const struct sysctlnode sysctl_usermib[] = {
127#if defined(lint)
128		/*
129		 * lint doesn't like my initializers
130		 */
131		0
132#else /* !lint */
133		{
134			.sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
135				CTLTYPE_STRING,
136			.sysctl_size = sizeof(_PATH_STDPATH),
137			.sysctl_name = "cs_path",
138			.sysctl_num = USER_CS_PATH,
139			.sysctl_data = _PATH_STDPATH,
140		},
141		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX),
142		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX),
143		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX),
144		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX),
145		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
146		     COLL_WEIGHTS_MAX),
147		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX),
148		_INT("line_max", USER_LINE_MAX, LINE_MAX),
149		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX),
150		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION),
151#ifdef POSIX2_C_BIND
152		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1),
153#else
154		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0),
155#endif
156#ifdef POSIX2_C_DEV
157		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1),
158#else
159		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0),
160#endif
161#ifdef POSIX2_CHAR_TERM
162		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1),
163#else
164		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0),
165#endif
166#ifdef POSIX2_FORT_DEV
167		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1),
168#else
169		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0),
170#endif
171#ifdef POSIX2_FORT_RUN
172		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1),
173#else
174		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0),
175#endif
176#ifdef POSIX2_LOCALEDEF
177		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1),
178#else
179		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0),
180#endif
181#ifdef POSIX2_SW_DEV
182		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1),
183#else
184		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0),
185#endif
186#ifdef POSIX2_UPE
187		_INT("posix2_upe", USER_POSIX2_UPE, 1),
188#else
189		_INT("posix2_upe", USER_POSIX2_UPE, 0),
190#endif
191		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX),
192		_INT("tzname_max", USER_TZNAME_MAX, NAME_MAX),
193		_INT("atexit_max", USER_ATEXIT_MAX, -1),
194#endif /* !lint */
195	};
196#undef _INT
197
198	static const int clen = sizeof(sysctl_usermib) /
199		sizeof(sysctl_usermib[0]);
200
201	const struct sysctlnode *node;
202	int ni;
203	size_t l, sz;
204
205	/*
206	 * none of these nodes are writable and they're all terminal (for now)
207	 */
208	if (namelen != 1)
209		return (EINVAL);
210
211	l = *oldlenp;
212	if (name[0] == CTL_QUERY) {
213		uint v;
214		node = newp;
215		if (node == NULL) {
216			v = SYSCTL_VERS_0;
217			newlen = sizeof(struct sysctlnode);
218		}
219		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_0 &&
220			 newlen == sizeof(struct sysctlnode0))
221			v = SYSCTL_VERS_0;
222		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
223			 newlen == sizeof(struct sysctlnode))
224			v = SYSCTL_VERS_1;
225		else
226			return (EINVAL);
227
228		sz = 0;
229		for (ni = 0; ni < clen; ni++)
230			sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
231		*oldlenp = sz;
232		return (0);
233	}
234
235	/*
236	 * none of these nodes are writable
237	 */
238	if (newp != NULL || newlen != 0)
239		return (EPERM);
240
241	node = &sysctl_usermib[0];
242	for (ni = 0; ni	< clen; ni++)
243		if (name[0] == node[ni].sysctl_num)
244			break;
245	if (ni == clen)
246		return (EOPNOTSUPP);
247
248	node = &node[ni];
249	if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
250		switch (SYSCTL_TYPE(node->sysctl_flags)) {
251		case CTLTYPE_INT:
252			newp = &node->sysctl_idata;
253			break;
254		case CTLTYPE_QUAD:
255			newp = &node->sysctl_qdata;
256			break;
257		default:
258			return (EINVAL);
259		}
260	}
261	else
262		newp = node->sysctl_data;
263
264	l = MIN(l, node->sysctl_size);
265	if (oldp != NULL)
266		memcpy(oldp, newp, l);
267	*oldlenp = node->sysctl_size;
268
269	return (0);
270}
271
272static size_t
273__cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
274{
275	struct sysctlnode0 node0;
276	const void *src = n;
277	size_t sz;
278
279	switch (v) {
280	    case SYSCTL_VERS_0:
281		memset(&node0, 0, sizeof(node0));
282		node0.sysctl0_flags = n->sysctl_flags;
283		node0.sysctl0_num = n->sysctl_num;
284		node0.sysctl0_size = n->sysctl_size;
285		memcpy(node0.sysctl0_name, n->sysctl_name, SYSCTL_NAMELEN);
286		node0.sysctl0_csize = n->sysctl_csize;
287		node0.sysctl0_clen = n->sysctl_clen;
288		node0.sysctl0_child = NULL;
289		node0.sysctl0_alias = n->sysctl_alias;
290		node0.sysctl0_idata = n->sysctl_idata;
291		node0.sysctl0_qdata = n->sysctl_qdata;
292		node0.sysctl0_data = n->sysctl_data;
293		node0.sysctl0_func = NULL;
294		node0.sysctl0_parent = NULL;
295		node0.sysctl0_ver = n->sysctl_ver;
296		node0.sysctl0_flags &= ~SYSCTL_VERS_MASK;
297		node0.sysctl0_flags |= SYSCTL_VERS_0;
298		src = &node0;
299		sz = sizeof(node0);
300		break;
301
302#if (SYSCTL_VERSION != SYSCTL_VERS_1)
303#error __cvt_node_out: no support for SYSCTL_VERSION
304#endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
305
306	case SYSCTL_VERSION:
307		sz = sizeof(struct sysctlnode);
308		break;
309	default:
310		sz = 0;
311		break;
312	}
313
314	if (sz > 0 && *o != NULL && *l >= sz) {
315		memcpy(*o, src, sz);
316		*o = sz + (caddr_t)*o;
317		*l -= sz;
318	}
319
320	return(sz);
321}
322