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