xref: /minix3/lib/libc/gen/sysctl.c (revision 5ba302fdeaa9e153d58b5dcaef42d660aedee92e)
1 /*	$NetBSD: sysctl.c,v 1.32 2012/03/20 16:36:05 matt 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.32 2012/03/20 16:36:05 matt 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 <assert.h>
47 #include <errno.h>
48 #include <paths.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "extern.h"
53 
54 #ifdef __weak_alias
55 __weak_alias(sysctl,_sysctl)
56 #endif
57 
58 /*
59  * handles requests off the user subtree
60  */
61 static int user_sysctl(const int *, u_int, void *, size_t *,
62 			const void *, size_t);
63 
64 /*
65  * copies out individual nodes taking target version into account
66  */
67 static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
68 			     size_t *);
69 
70 #include <stdlib.h>
71 
72 #if defined(__minix)
73 int __sysctl(const int *name, unsigned int namelen,
74 	void *oldp, size_t *oldlenp,
75 	const void *newp, size_t newlen)
76 {
77 	return ENOENT;
78 }
79 #endif /* defined(__minix) */
80 
81 int
82 sysctl(const int *name, unsigned int namelen,
83 	void *oldp, size_t *oldlenp,
84 	const void *newp, size_t newlen)
85 {
86 	size_t oldlen, savelen;
87 	int error;
88 
89 	if (name[0] != CTL_USER)
90 		return (__sysctl(name, namelen, oldp, oldlenp,
91 				 newp, newlen));
92 
93 	oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
94 	savelen = oldlen;
95 	error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
96 
97 	if (error != 0) {
98 		errno = error;
99 		return (-1);
100 	}
101 
102 	if (oldlenp != NULL) {
103 		*oldlenp = oldlen;
104 		if (oldp != NULL && oldlen > savelen) {
105 			errno = ENOMEM;
106 			return (-1);
107 		}
108 	}
109 
110 	return (0);
111 }
112 
113 static int
114 user_sysctl(const int *name, unsigned int namelen,
115 	void *oldp, size_t *oldlenp,
116 	const void *newp, size_t newlen)
117 {
118 #define _INT(s, n, v, d) {					\
119 	.sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|	\
120 			CTLTYPE_INT|SYSCTL_VERSION,		\
121 	sysc_init_field(_sysctl_size, sizeof(int)),		\
122 	.sysctl_name = (s),					\
123 	.sysctl_num = (n),					\
124 	.sysctl_un = { .scu_idata = (v), },			\
125 	sysc_init_field(_sysctl_desc, (d)),			\
126 	}
127 
128 	/*
129 	 * the nodes under the "user" node
130 	 */
131 	static const struct sysctlnode sysctl_usermib[] = {
132 #if defined(lint)
133 		/*
134 		 * lint doesn't like my initializers
135 		 */
136 		0
137 #else /* !lint */
138 		{
139 			.sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
140 				CTLTYPE_STRING,
141 			sysc_init_field(_sysctl_size, sizeof(_PATH_STDPATH)),
142 			.sysctl_name = "cs_path",
143 			.sysctl_num = USER_CS_PATH,
144 			/*
145 			 * XXX these nasty initializers (and the one in
146 			 * the _INT() macro) can go away once all ports
147 			 * are using gcc3, and become
148 			 *
149 			 *	.sysctl_data = _PATH_STDPATH,
150 			 *	.sysctl_desc = NULL,
151 			 */
152 			.sysctl_un = { .scu_data = {
153 				sysc_init_field(_sud_data,
154 				__UNCONST(_PATH_STDPATH)),
155 				}, },
156 			sysc_init_field(_sysctl_desc,
157 				"A value for the PATH environment variable "
158 				"that finds all the standard utilities"),
159 		},
160 		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX,
161 		     "The maximum ibase/obase values in the bc(1) utility"),
162 		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX,
163 		     "The maximum array size in the bc(1) utility"),
164 		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX,
165 		     "The maximum scale value in the bc(1) utility"),
166 		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX,
167 		     "The maximum string length in the bc(1) utility"),
168 		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
169 		     COLL_WEIGHTS_MAX, "The maximum number of weights that can "
170 		     "be assigned to any entry of the LC_COLLATE order keyword "
171 		     "in the locale definition file"),
172 		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX,
173 		     "The maximum number of expressions that can be nested "
174 		     "within parenthesis by the expr(1) utility"),
175 		_INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length "
176 		     "in bytes of a text-processing utility's input line"),
177 		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum "
178 		     "number of repeated occurrences of a regular expression "
179 		     "permitted when using interval notation"),
180 		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION,
181 		     "The version of POSIX 1003.2 with which the system "
182 		     "attempts to comply"),
183 #ifdef _POSIX2_C_BIND
184 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1,
185 		     "Whether the system's C-language development facilities "
186 		     "support the C-Language Bindings Option"),
187 #else
188 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0,
189 		     "Whether the system's C-language development facilities "
190 		     "support the C-Language Bindings Option"),
191 #endif
192 #ifdef POSIX2_C_DEV
193 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1,
194 		     "Whether the system supports the C-Language Development "
195 		     "Utilities Option"),
196 #else
197 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0,
198 		     "Whether the system supports the C-Language Development "
199 		     "Utilities Option"),
200 #endif
201 #ifdef POSIX2_CHAR_TERM
202 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1,
203 		     "Whether the system supports at least one terminal type "
204 		     "capable of all operations described in POSIX 1003.2"),
205 #else
206 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0,
207 		     "Whether the system supports at least one terminal type "
208 		     "capable of all operations described in POSIX 1003.2"),
209 #endif
210 #ifdef POSIX2_FORT_DEV
211 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1,
212 		     "Whether the system supports the FORTRAN Development "
213 		     "Utilities Option"),
214 #else
215 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0,
216 		     "Whether the system supports the FORTRAN Development "
217 		     "Utilities Option"),
218 #endif
219 #ifdef POSIX2_FORT_RUN
220 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1,
221 		     "Whether the system supports the FORTRAN Runtime "
222 		     "Utilities Option"),
223 #else
224 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0,
225 		     "Whether the system supports the FORTRAN Runtime "
226 		     "Utilities Option"),
227 #endif
228 #ifdef POSIX2_LOCALEDEF
229 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1,
230 		     "Whether the system supports the creation of locales"),
231 #else
232 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0,
233 		     "Whether the system supports the creation of locales"),
234 #endif
235 #ifdef POSIX2_SW_DEV
236 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1,
237 		     "Whether the system supports the Software Development "
238 		     "Utilities Option"),
239 #else
240 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0,
241 		     "Whether the system supports the Software Development "
242 		     "Utilities Option"),
243 #endif
244 #ifdef POSIX2_UPE
245 		_INT("posix2_upe", USER_POSIX2_UPE, 1,
246 		     "Whether the system supports the User Portability "
247 		     "Utilities Option"),
248 #else
249 		_INT("posix2_upe", USER_POSIX2_UPE, 0,
250 		     "Whether the system supports the User Portability "
251 		     "Utilities Option"),
252 #endif
253 		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX,
254 		     "The minimum maximum number of streams that a process "
255 		     "may have open at any one time"),
256 		_INT("tzname_max", USER_TZNAME_MAX, NAME_MAX,
257 		     "The minimum maximum number of types supported for the "
258 		     "name of a timezone"),
259 		_INT("atexit_max", USER_ATEXIT_MAX, -1,
260 		     "The maximum number of functions that may be registered "
261 		     "with atexit(3)"),
262 #endif /* !lint */
263 	};
264 #undef _INT
265 
266 	static const int clen = sizeof(sysctl_usermib) /
267 		sizeof(sysctl_usermib[0]);
268 
269 	const struct sysctlnode *node;
270 	int ni;
271 	size_t l, sz;
272 
273 	/*
274 	 * none of these nodes are writable and they're all terminal (for now)
275 	 */
276 	if (namelen != 1)
277 		return (EINVAL);
278 
279 	l = *oldlenp;
280 	if (name[0] == CTL_QUERY) {
281 		uint v;
282 		node = newp;
283 		if (node == NULL)
284 			return (EINVAL);
285 		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
286 			 newlen == sizeof(struct sysctlnode))
287 			v = SYSCTL_VERS_1;
288 		else
289 			return (EINVAL);
290 
291 		sz = 0;
292 		for (ni = 0; ni < clen; ni++)
293 			sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
294 		*oldlenp = sz;
295 		return (0);
296 	}
297 
298 	if (name[0] == CTL_DESCRIBE) {
299 		/*
300 		 * XXX make sure this is larger than the largest
301 		 * "user" description
302 		 */
303 		char buf[192];
304 		struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp;
305 		size_t d;
306 
307 		node = newp;
308 		if (node != NULL &&
309 		    (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 ||
310 		     newlen != sizeof(struct sysctlnode)))
311 			return (EINVAL);
312 
313 		sz = 0;
314 		for (ni = 0; ni < clen; ni++) {
315 			memset(&buf[0], 0, sizeof(buf));
316 			if (node != NULL &&
317 			    node->sysctl_num != sysctl_usermib[ni].sysctl_num)
318 				continue;
319 			d1->descr_num = sysctl_usermib[ni].sysctl_num;
320 			d1->descr_ver = sysctl_usermib[ni].sysctl_ver;
321 			if (sysctl_usermib[ni].sysctl_desc == NULL)
322 				d1->descr_len = 1;
323 			else {
324 				size_t dlen;
325 				(void)strlcpy(d1->descr_str,
326 					sysctl_usermib[ni].sysctl_desc,
327 					sizeof(buf) - sizeof(*d1));
328 				dlen = strlen(d1->descr_str) + 1;
329 				_DIAGASSERT(__type_fit(uint32_t, dlen));
330 				d1->descr_len = (uint32_t)dlen;
331 			}
332 			d = (size_t)__sysc_desc_adv(NULL, d1->descr_len);
333 			if (d2 != NULL)
334 				memcpy(d2, d1, d);
335 			sz += d;
336 			if (node != NULL)
337 				break;
338 		}
339 		*oldlenp = sz;
340 		if (sz == 0 && node != NULL)
341 			return (ENOENT);
342 		return (0);
343 
344 	}
345 
346 	/*
347 	 * none of these nodes are writable
348 	 */
349 	if (newp != NULL || newlen != 0)
350 		return (EPERM);
351 
352 	node = &sysctl_usermib[0];
353 	for (ni = 0; ni	< clen; ni++)
354 		if (name[0] == node[ni].sysctl_num)
355 			break;
356 	if (ni == clen)
357 		return (EOPNOTSUPP);
358 
359 	node = &node[ni];
360 	if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
361 		switch (SYSCTL_TYPE(node->sysctl_flags)) {
362 		case CTLTYPE_INT:
363 			newp = &node->sysctl_idata;
364 			break;
365 		case CTLTYPE_QUAD:
366 			newp = &node->sysctl_qdata;
367 			break;
368 		default:
369 			return (EINVAL);
370 		}
371 	}
372 	else
373 		newp = node->sysctl_data;
374 
375 	l = MIN(l, node->sysctl_size);
376 	if (oldp != NULL)
377 		memcpy(oldp, newp, l);
378 	*oldlenp = node->sysctl_size;
379 
380 	return (0);
381 }
382 
383 static size_t
384 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
385 {
386 	const void *src = n;
387 	size_t sz;
388 
389 	switch (v) {
390 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
391 #error __cvt_node_out: no support for SYSCTL_VERSION
392 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
393 
394 	case SYSCTL_VERSION:
395 		sz = sizeof(struct sysctlnode);
396 		break;
397 
398 	default:
399 		sz = 0;
400 		break;
401 	}
402 
403 	if (sz > 0 && *o != NULL && *l >= sz) {
404 		memcpy(*o, src, sz);
405 		*o = sz + (caddr_t)*o;
406 		*l -= sz;
407 	}
408 
409 	return(sz);
410 }
411