1 /* $NetBSD: sysctl.c,v 1.29 2006/02/24 19:33:09 drochner Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)sysctl.c 8.2 (Berkeley) 1/4/94";
37 __RCSID("$NetBSD: sysctl.c,v 1.29 2006/02/24 19:33:09 drochner Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include <sys/param.h>
43 #define __COMPAT_SYSCTL
44 #include <sys/sysctl.h>
54 __weak_alias(sysctl
,_sysctl
)
58 * handles requests off the user subtree
60 static int user_sysctl(const int *, u_int
, void *, size_t *,
61 const void *, size_t);
64 * copies out individual nodes taking target version into account
66 static size_t __cvt_node_out(uint
, const struct sysctlnode
*, void **,
72 sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
77 size_t *oldlenp
, newlen
;
79 size_t oldlen
, savelen
;
82 if (name
[0] != CTL_USER
)
83 return (__sysctl(name
, namelen
, oldp
, oldlenp
,
86 oldlen
= (oldlenp
== NULL
) ? 0 : *oldlenp
;
88 error
= user_sysctl(name
+ 1, namelen
- 1, oldp
, &oldlen
, newp
, newlen
);
95 if (oldlenp
!= NULL
) {
97 if (oldp
!= NULL
&& oldlen
> savelen
) {
107 user_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
109 unsigned int namelen
;
112 size_t *oldlenp
, newlen
;
114 #define _INT(s, n, v, d) { \
115 .sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT| \
116 CTLTYPE_INT|SYSCTL_VERSION, \
117 sysc_init_field(_sysctl_size, sizeof(int)), \
118 .sysctl_name = (s), \
120 .sysctl_un = { .scu_idata = (v), }, \
121 sysc_init_field(_sysctl_desc, (d)), \
125 * the nodes under the "user" node
127 static const struct sysctlnode sysctl_usermib
[] = {
130 * lint doesn't like my initializers
135 .sysctl_flags
= SYSCTL_VERSION
|CTLFLAG_PERMANENT
|
137 sysc_init_field(_sysctl_size
, sizeof(_PATH_STDPATH
)),
138 .sysctl_name
= "cs_path",
139 .sysctl_num
= USER_CS_PATH
,
141 * XXX these nasty initializers (and the one in
142 * the _INT() macro) can go away once all ports
143 * are using gcc3, and become
145 * .sysctl_data = _PATH_STDPATH,
146 * .sysctl_desc = NULL,
148 .sysctl_un
= { .scu_data
= {
149 sysc_init_field(_sud_data
,
150 __UNCONST(_PATH_STDPATH
)),
152 sysc_init_field(_sysctl_desc
,
153 "A value for the PATH environment variable "
154 "that finds all the standard utilities"),
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"),
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"),
189 _INT("posix2_c_dev", USER_POSIX2_C_DEV
, 1,
190 "Whether the system supports the C-Language Development "
193 _INT("posix2_c_dev", USER_POSIX2_C_DEV
, 0,
194 "Whether the system supports the C-Language Development "
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"),
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"),
206 #ifdef POSIX2_FORT_DEV
207 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV
, 1,
208 "Whether the system supports the FORTRAN Development "
211 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV
, 0,
212 "Whether the system supports the FORTRAN Development "
215 #ifdef POSIX2_FORT_RUN
216 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN
, 1,
217 "Whether the system supports the FORTRAN Runtime "
220 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN
, 0,
221 "Whether the system supports the FORTRAN Runtime "
224 #ifdef POSIX2_LOCALEDEF
225 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF
, 1,
226 "Whether the system supports the creation of locales"),
228 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF
, 0,
229 "Whether the system supports the creation of locales"),
232 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV
, 1,
233 "Whether the system supports the Software Development "
236 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV
, 0,
237 "Whether the system supports the Software Development "
241 _INT("posix2_upe", USER_POSIX2_UPE
, 1,
242 "Whether the system supports the User Portability "
245 _INT("posix2_upe", USER_POSIX2_UPE
, 0,
246 "Whether the system supports the User Portability "
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 "
262 static const int clen
= sizeof(sysctl_usermib
) /
263 sizeof(sysctl_usermib
[0]);
265 const struct sysctlnode
*node
;
270 * none of these nodes are writable and they're all terminal (for now)
276 if (name
[0] == CTL_QUERY
) {
281 else if (SYSCTL_VERS(node
->sysctl_flags
) == SYSCTL_VERS_1
&&
282 newlen
== sizeof(struct sysctlnode
))
288 for (ni
= 0; ni
< clen
; ni
++)
289 sz
+= __cvt_node_out(v
, &sysctl_usermib
[ni
], &oldp
, &l
);
294 if (name
[0] == CTL_DESCRIBE
) {
296 * XXX make sure this is larger than the largest
300 struct sysctldesc
*d1
= (void *)&buf
[0], *d2
= oldp
;
305 (SYSCTL_VERS(node
->sysctl_flags
) < SYSCTL_VERS_1
||
306 newlen
!= sizeof(struct sysctlnode
)))
310 for (ni
= 0; ni
< clen
; ni
++) {
311 memset(&buf
[0], 0, sizeof(buf
));
313 node
->sysctl_num
!= sysctl_usermib
[ni
].sysctl_num
)
315 d1
->descr_num
= sysctl_usermib
[ni
].sysctl_num
;
316 d1
->descr_ver
= sysctl_usermib
[ni
].sysctl_ver
;
317 if (sysctl_usermib
[ni
].sysctl_desc
== NULL
)
320 (void)strlcpy(d1
->descr_str
,
321 sysctl_usermib
[ni
].sysctl_desc
,
322 sizeof(buf
) - sizeof(*d1
));
323 d1
->descr_len
= strlen(d1
->descr_str
) + 1;
325 d
= (size_t)__sysc_desc_adv(NULL
, d1
->descr_len
);
333 if (sz
== 0 && node
!= NULL
)
340 * none of these nodes are writable
342 if (newp
!= NULL
|| newlen
!= 0)
345 node
= &sysctl_usermib
[0];
346 for (ni
= 0; ni
< clen
; ni
++)
347 if (name
[0] == node
[ni
].sysctl_num
)
353 if (node
->sysctl_flags
& CTLFLAG_IMMEDIATE
) {
354 switch (SYSCTL_TYPE(node
->sysctl_flags
)) {
356 newp
= &node
->sysctl_idata
;
359 newp
= &node
->sysctl_qdata
;
366 newp
= node
->sysctl_data
;
368 l
= MIN(l
, node
->sysctl_size
);
370 memcpy(oldp
, newp
, l
);
371 *oldlenp
= node
->sysctl_size
;
377 __cvt_node_out(uint v
, const struct sysctlnode
*n
, void **o
, size_t *l
)
383 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
384 #error __cvt_node_out: no support for SYSCTL_VERSION
385 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
388 sz
= sizeof(struct sysctlnode
);
396 if (sz
> 0 && *o
!= NULL
&& *l
>= sz
) {
398 *o
= sz
+ (caddr_t
)*o
;