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