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