sysctl.c revision 1.25 1 /* $NetBSD: sysctl.c,v 1.25 2004/04/11 09:10:58 he 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.25 2004/04/11 09:10:58 he 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 */
60 static 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 */
65 static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
66 size_t *);
67
68 #include <stdlib.h>
69
70 int
71 sysctl(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
107 static int
108 user_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, d) { \
116 .sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT| \
117 CTLTYPE_INT|SYSCTL_VERSION, \
118 sysc_init_field(_sysctl_size, sizeof(int)), \
119 .sysctl_name = (s), \
120 .sysctl_num = (n), \
121 .sysctl_un = { .scu_idata = (v), }, \
122 sysc_init_field(_sysctl_desc, (d)), \
123 }
124
125 /*
126 * the nodes under the "user" node
127 */
128 static const struct sysctlnode sysctl_usermib[] = {
129 #if defined(lint)
130 /*
131 * lint doesn't like my initializers
132 */
133 0
134 #else /* !lint */
135 {
136 .sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
137 CTLTYPE_STRING,
138 sysc_init_field(_sysctl_size, sizeof(_PATH_STDPATH)),
139 .sysctl_name = "cs_path",
140 .sysctl_num = USER_CS_PATH,
141 /*
142 * XXX these nasty initializers (and the one in
143 * the _INT() macro) can go away once all ports
144 * are using gcc3, and become
145 *
146 * .sysctl_data = _PATH_STDPATH,
147 * .sysctl_desc = NULL,
148 */
149 .sysctl_un = { .scu_data = {
150 sysc_init_field(_sud_data, _PATH_STDPATH),
151 }, },
152 sysc_init_field(_sysctl_desc,
153 "A value for the PATH environment variable "
154 "that finds all the standard utilities"),
155 },
156 _INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX,
157 "The maximum ibase/obase values in the bc(1) utility"),
158 _INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX,
159 "The maximum array size in the bc(1) utility"),
160 _INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX,
161 "The maximum scale value in the bc(1) utility"),
162 _INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX,
163 "The maximum string length in the bc(1) utility"),
164 _INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
165 COLL_WEIGHTS_MAX, "The maximum number of weights that can "
166 "be assigned to any entry of the LC_COLLATE order keyword "
167 "in the locale definition file"),
168 _INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX,
169 "The maximum number of expressions that can be nested "
170 "within parenthesis by the expr(1) utility"),
171 _INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length "
172 "in bytes of a text-processing utility's input line"),
173 _INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum "
174 "number of repeated occurrences of a regular expression "
175 "permitted when using interval notation"),
176 _INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION,
177 "The version of POSIX 1003.2 with which the system "
178 "attempts to comply"),
179 #ifdef POSIX2_C_BIND
180 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 1,
181 "Whether the system's C-language development facilities "
182 "support the C-Language Bindings Option"),
183 #else
184 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 0,
185 "Whether the system's C-language development facilities "
186 "support the C-Language Bindings Option"),
187 #endif
188 #ifdef POSIX2_C_DEV
189 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 1,
190 "Whether the system supports the C-Language Development "
191 "Utilities Option"),
192 #else
193 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 0,
194 "Whether the system supports the C-Language Development "
195 "Utilities Option"),
196 #endif
197 #ifdef POSIX2_CHAR_TERM
198 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1,
199 "Whether the system supports at least one terminal type "
200 "capable of all operations described in POSIX 1003.2"),
201 #else
202 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0,
203 "Whether the system supports at least one terminal type "
204 "capable of all operations described in POSIX 1003.2"),
205 #endif
206 #ifdef POSIX2_FORT_DEV
207 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1,
208 "Whether the system supports the FORTRAN Development "
209 "Utilities Option"),
210 #else
211 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0,
212 "Whether the system supports the FORTRAN Development "
213 "Utilities Option"),
214 #endif
215 #ifdef POSIX2_FORT_RUN
216 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1,
217 "Whether the system supports the FORTRAN Runtime "
218 "Utilities Option"),
219 #else
220 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0,
221 "Whether the system supports the FORTRAN Runtime "
222 "Utilities Option"),
223 #endif
224 #ifdef POSIX2_LOCALEDEF
225 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1,
226 "Whether the system supports the creation of locales"),
227 #else
228 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0,
229 "Whether the system supports the creation of locales"),
230 #endif
231 #ifdef POSIX2_SW_DEV
232 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1,
233 "Whether the system supports the Software Development "
234 "Utilities Option"),
235 #else
236 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0,
237 "Whether the system supports the Software Development "
238 "Utilities Option"),
239 #endif
240 #ifdef POSIX2_UPE
241 _INT("posix2_upe", USER_POSIX2_UPE, 1,
242 "Whether the system supports the User Portability "
243 "Utilities Option"),
244 #else
245 _INT("posix2_upe", USER_POSIX2_UPE, 0,
246 "Whether the system supports the User Portability "
247 "Utilities Option"),
248 #endif
249 _INT("stream_max", USER_STREAM_MAX, FOPEN_MAX,
250 "The minimum maximum number of streams that a process "
251 "may have open at any one time"),
252 _INT("tzname_max", USER_TZNAME_MAX, NAME_MAX,
253 "The minimum maximum number of types supported for the "
254 "name of a timezone"),
255 _INT("atexit_max", USER_ATEXIT_MAX, -1,
256 "The maximum number of functions that may be registered "
257 "with atexit(3)"),
258 #endif /* !lint */
259 };
260 #undef _INT
261
262 static const int clen = sizeof(sysctl_usermib) /
263 sizeof(sysctl_usermib[0]);
264
265 const struct sysctlnode *node;
266 int ni;
267 size_t l, sz;
268
269 /*
270 * none of these nodes are writable and they're all terminal (for now)
271 */
272 if (namelen != 1)
273 return (EINVAL);
274
275 l = *oldlenp;
276 if (name[0] == CTL_QUERY) {
277 uint v;
278 node = newp;
279 if (node == NULL) {
280 v = SYSCTL_VERS_0;
281 newlen = sizeof(struct sysctlnode);
282 }
283 else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_0 &&
284 newlen == sizeof(struct sysctlnode0))
285 v = SYSCTL_VERS_0;
286 else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
287 newlen == sizeof(struct sysctlnode))
288 v = SYSCTL_VERS_1;
289 else
290 return (EINVAL);
291
292 sz = 0;
293 for (ni = 0; ni < clen; ni++)
294 sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
295 *oldlenp = sz;
296 return (0);
297 }
298
299 if (name[0] == CTL_DESCRIBE) {
300 /*
301 * XXX make sure this is larger than the largest
302 * "user" description
303 */
304 char buf[192];
305 struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp;
306 size_t d;
307
308 node = newp;
309 if (node != NULL &&
310 (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 ||
311 newlen != sizeof(struct sysctlnode)))
312 return (EINVAL);
313
314 sz = 0;
315 for (ni = 0; ni < clen; ni++) {
316 memset(&buf[0], 0, sizeof(buf));
317 if (node != NULL &&
318 node->sysctl_num != sysctl_usermib[ni].sysctl_num)
319 continue;
320 d1->descr_num = sysctl_usermib[ni].sysctl_num;
321 d1->descr_ver = sysctl_usermib[ni].sysctl_ver;
322 if (sysctl_usermib[ni].sysctl_desc == NULL)
323 d1->descr_len = 1;
324 else {
325 strncpy(d1->descr_str,
326 sysctl_usermib[ni].sysctl_desc,
327 sizeof(buf) - sizeof(*d1));
328 buf[sizeof(buf) - 1] = '\0';
329 d1->descr_len = strlen(d1->descr_str) + 1;
330 }
331 d = (size_t)__sysc_desc_adv(NULL, d1->descr_len);
332 if (d2 != NULL)
333 memcpy(d2, d1, d);
334 sz += d;
335 if (node != NULL)
336 break;
337 }
338 *oldlenp = sz;
339 if (sz == 0 && node != NULL)
340 return (ENOENT);
341 return (0);
342
343 }
344
345 /*
346 * none of these nodes are writable
347 */
348 if (newp != NULL || newlen != 0)
349 return (EPERM);
350
351 node = &sysctl_usermib[0];
352 for (ni = 0; ni < clen; ni++)
353 if (name[0] == node[ni].sysctl_num)
354 break;
355 if (ni == clen)
356 return (EOPNOTSUPP);
357
358 node = &node[ni];
359 if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
360 switch (SYSCTL_TYPE(node->sysctl_flags)) {
361 case CTLTYPE_INT:
362 newp = &node->sysctl_idata;
363 break;
364 case CTLTYPE_QUAD:
365 newp = &node->sysctl_qdata;
366 break;
367 default:
368 return (EINVAL);
369 }
370 }
371 else
372 newp = node->sysctl_data;
373
374 l = MIN(l, node->sysctl_size);
375 if (oldp != NULL)
376 memcpy(oldp, newp, l);
377 *oldlenp = node->sysctl_size;
378
379 return (0);
380 }
381
382 static size_t
383 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
384 {
385 struct sysctlnode0 node0;
386 const void *src = n;
387 size_t sz;
388
389 switch (v) {
390 case SYSCTL_VERS_0:
391 memset(&node0, 0, sizeof(node0));
392 node0.sysctl0_flags = n->sysctl_flags;
393 node0.sysctl0_num = n->sysctl_num;
394 node0.sysctl0_size = n->sysctl_size;
395 memcpy(node0.sysctl0_name, n->sysctl_name, SYSCTL_NAMELEN);
396 node0.sysctl0_csize = n->sysctl_csize;
397 node0.sysctl0_clen = n->sysctl_clen;
398 node0.sysctl0_child = NULL;
399 node0.sysctl0_alias = n->sysctl_alias;
400 node0.sysctl0_idata = n->sysctl_idata;
401 node0.sysctl0_qdata = n->sysctl_qdata;
402 node0.sysctl0_data = n->sysctl_data;
403 node0.sysctl0_func = NULL;
404 node0.sysctl0_parent = NULL;
405 node0.sysctl0_ver = n->sysctl_ver;
406 node0.sysctl0_flags &= ~SYSCTL_VERS_MASK;
407 node0.sysctl0_flags |= SYSCTL_VERS_0;
408 src = &node0;
409 sz = sizeof(node0);
410 break;
411
412 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
413 #error __cvt_node_out: no support for SYSCTL_VERSION
414 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
415
416 case SYSCTL_VERSION:
417 sz = sizeof(struct sysctlnode);
418 break;
419 default:
420 sz = 0;
421 break;
422 }
423
424 if (sz > 0 && *o != NULL && *l >= sz) {
425 memcpy(*o, src, sz);
426 *o = sz + (caddr_t)*o;
427 *l -= sz;
428 }
429
430 return(sz);
431 }
432