sysctl.c revision 1.36 1 1.36 christos /* $NetBSD: sysctl.c,v 1.36 2017/01/10 17:46:47 christos Exp $ */
2 1.2 cgd
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.13 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.5 christos #include <sys/cdefs.h>
33 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
34 1.2 cgd #if 0
35 1.1 cgd static char sccsid[] = "@(#)sysctl.c 8.2 (Berkeley) 1/4/94";
36 1.2 cgd #else
37 1.36 christos __RCSID("$NetBSD: sysctl.c,v 1.36 2017/01/10 17:46:47 christos Exp $");
38 1.2 cgd #endif
39 1.1 cgd #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.6 jtc #include "namespace.h"
42 1.1 cgd #include <sys/param.h>
43 1.19 atatat #define __COMPAT_SYSCTL
44 1.1 cgd #include <sys/sysctl.h>
45 1.1 cgd
46 1.31 christos #include <assert.h>
47 1.1 cgd #include <errno.h>
48 1.1 cgd #include <paths.h>
49 1.1 cgd #include <stdio.h>
50 1.4 jtc #include <string.h>
51 1.1 cgd #include <unistd.h>
52 1.5 christos #include "extern.h"
53 1.6 jtc
54 1.6 jtc #ifdef __weak_alias
55 1.11 mycroft __weak_alias(sysctl,_sysctl)
56 1.6 jtc #endif
57 1.1 cgd
58 1.14 atatat /*
59 1.14 atatat * handles requests off the user subtree
60 1.14 atatat */
61 1.29 drochner static int user_sysctl(const int *, u_int, void *, size_t *,
62 1.29 drochner const void *, size_t);
63 1.14 atatat
64 1.18 atatat /*
65 1.18 atatat * copies out individual nodes taking target version into account
66 1.18 atatat */
67 1.18 atatat static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
68 1.18 atatat size_t *);
69 1.18 atatat
70 1.14 atatat #include <stdlib.h>
71 1.14 atatat
72 1.1 cgd int
73 1.32 matt sysctl(const int *name, unsigned int namelen,
74 1.32 matt void *oldp, size_t *oldlenp,
75 1.32 matt const void *newp, size_t newlen)
76 1.1 cgd {
77 1.14 atatat size_t oldlen, savelen;
78 1.14 atatat int error;
79 1.9 lukem
80 1.1 cgd if (name[0] != CTL_USER)
81 1.9 lukem return (__sysctl(name, namelen, oldp, oldlenp,
82 1.29 drochner newp, newlen));
83 1.14 atatat
84 1.14 atatat oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
85 1.14 atatat savelen = oldlen;
86 1.14 atatat error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
87 1.1 cgd
88 1.14 atatat if (error != 0) {
89 1.14 atatat errno = error;
90 1.1 cgd return (-1);
91 1.1 cgd }
92 1.1 cgd
93 1.14 atatat if (oldlenp != NULL) {
94 1.14 atatat *oldlenp = oldlen;
95 1.14 atatat if (oldp != NULL && oldlen > savelen) {
96 1.14 atatat errno = ENOMEM;
97 1.14 atatat return (-1);
98 1.14 atatat }
99 1.1 cgd }
100 1.1 cgd
101 1.14 atatat return (0);
102 1.14 atatat }
103 1.1 cgd
104 1.14 atatat static int
105 1.32 matt user_sysctl(const int *name, unsigned int namelen,
106 1.32 matt void *oldp, size_t *oldlenp,
107 1.32 matt const void *newp, size_t newlen)
108 1.14 atatat {
109 1.20 atatat #define _INT(s, n, v, d) { \
110 1.16 atatat .sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT| \
111 1.16 atatat CTLTYPE_INT|SYSCTL_VERSION, \
112 1.34 martin .sysctl_size = sizeof(int), \
113 1.14 atatat .sysctl_name = (s), \
114 1.14 atatat .sysctl_num = (n), \
115 1.34 martin .sysctl_un.scu_idata = (v), \
116 1.34 martin .sysctl_desc = (d), \
117 1.23 he }
118 1.14 atatat
119 1.14 atatat /*
120 1.14 atatat * the nodes under the "user" node
121 1.14 atatat */
122 1.14 atatat static const struct sysctlnode sysctl_usermib[] = {
123 1.14 atatat #if defined(lint)
124 1.14 atatat /*
125 1.14 atatat * lint doesn't like my initializers
126 1.14 atatat */
127 1.14 atatat 0
128 1.14 atatat #else /* !lint */
129 1.14 atatat {
130 1.16 atatat .sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
131 1.16 atatat CTLTYPE_STRING,
132 1.34 martin .sysctl_size = sizeof(_PATH_STDPATH),
133 1.14 atatat .sysctl_name = "cs_path",
134 1.14 atatat .sysctl_num = USER_CS_PATH,
135 1.34 martin .sysctl_data = __UNCONST(_PATH_STDPATH),
136 1.34 martin .sysctl_desc = __UNCONST(
137 1.24 atatat "A value for the PATH environment variable "
138 1.24 atatat "that finds all the standard utilities"),
139 1.14 atatat },
140 1.24 atatat _INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX,
141 1.24 atatat "The maximum ibase/obase values in the bc(1) utility"),
142 1.24 atatat _INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX,
143 1.24 atatat "The maximum array size in the bc(1) utility"),
144 1.24 atatat _INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX,
145 1.24 atatat "The maximum scale value in the bc(1) utility"),
146 1.24 atatat _INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX,
147 1.24 atatat "The maximum string length in the bc(1) utility"),
148 1.14 atatat _INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
149 1.24 atatat COLL_WEIGHTS_MAX, "The maximum number of weights that can "
150 1.24 atatat "be assigned to any entry of the LC_COLLATE order keyword "
151 1.24 atatat "in the locale definition file"),
152 1.24 atatat _INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX,
153 1.24 atatat "The maximum number of expressions that can be nested "
154 1.24 atatat "within parenthesis by the expr(1) utility"),
155 1.24 atatat _INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length "
156 1.24 atatat "in bytes of a text-processing utility's input line"),
157 1.24 atatat _INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum "
158 1.24 atatat "number of repeated occurrences of a regular expression "
159 1.24 atatat "permitted when using interval notation"),
160 1.24 atatat _INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION,
161 1.24 atatat "The version of POSIX 1003.2 with which the system "
162 1.24 atatat "attempts to comply"),
163 1.30 christos #ifdef _POSIX2_C_BIND
164 1.24 atatat _INT("posix2_c_bind", USER_POSIX2_C_BIND, 1,
165 1.25 he "Whether the system's C-language development facilities "
166 1.25 he "support the C-Language Bindings Option"),
167 1.1 cgd #else
168 1.24 atatat _INT("posix2_c_bind", USER_POSIX2_C_BIND, 0,
169 1.24 atatat "Whether the system's C-language development facilities "
170 1.24 atatat "support the C-Language Bindings Option"),
171 1.25 he #endif
172 1.14 atatat #ifdef POSIX2_C_DEV
173 1.24 atatat _INT("posix2_c_dev", USER_POSIX2_C_DEV, 1,
174 1.25 he "Whether the system supports the C-Language Development "
175 1.25 he "Utilities Option"),
176 1.1 cgd #else
177 1.24 atatat _INT("posix2_c_dev", USER_POSIX2_C_DEV, 0,
178 1.24 atatat "Whether the system supports the C-Language Development "
179 1.24 atatat "Utilities Option"),
180 1.25 he #endif
181 1.14 atatat #ifdef POSIX2_CHAR_TERM
182 1.24 atatat _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1,
183 1.25 he "Whether the system supports at least one terminal type "
184 1.25 he "capable of all operations described in POSIX 1003.2"),
185 1.1 cgd #else
186 1.24 atatat _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0,
187 1.24 atatat "Whether the system supports at least one terminal type "
188 1.24 atatat "capable of all operations described in POSIX 1003.2"),
189 1.25 he #endif
190 1.14 atatat #ifdef POSIX2_FORT_DEV
191 1.24 atatat _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1,
192 1.25 he "Whether the system supports the FORTRAN Development "
193 1.25 he "Utilities Option"),
194 1.1 cgd #else
195 1.24 atatat _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0,
196 1.24 atatat "Whether the system supports the FORTRAN Development "
197 1.24 atatat "Utilities Option"),
198 1.25 he #endif
199 1.14 atatat #ifdef POSIX2_FORT_RUN
200 1.24 atatat _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1,
201 1.25 he "Whether the system supports the FORTRAN Runtime "
202 1.25 he "Utilities Option"),
203 1.1 cgd #else
204 1.24 atatat _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0,
205 1.24 atatat "Whether the system supports the FORTRAN Runtime "
206 1.24 atatat "Utilities Option"),
207 1.25 he #endif
208 1.14 atatat #ifdef POSIX2_LOCALEDEF
209 1.24 atatat _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1,
210 1.25 he "Whether the system supports the creation of locales"),
211 1.1 cgd #else
212 1.24 atatat _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0,
213 1.25 he "Whether the system supports the creation of locales"),
214 1.1 cgd #endif
215 1.14 atatat #ifdef POSIX2_SW_DEV
216 1.24 atatat _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1,
217 1.25 he "Whether the system supports the Software Development "
218 1.25 he "Utilities Option"),
219 1.1 cgd #else
220 1.24 atatat _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0,
221 1.24 atatat "Whether the system supports the Software Development "
222 1.24 atatat "Utilities Option"),
223 1.25 he #endif
224 1.14 atatat #ifdef POSIX2_UPE
225 1.24 atatat _INT("posix2_upe", USER_POSIX2_UPE, 1,
226 1.25 he "Whether the system supports the User Portability "
227 1.25 he "Utilities Option"),
228 1.1 cgd #else
229 1.24 atatat _INT("posix2_upe", USER_POSIX2_UPE, 0,
230 1.24 atatat "Whether the system supports the User Portability "
231 1.24 atatat "Utilities Option"),
232 1.25 he #endif
233 1.24 atatat _INT("stream_max", USER_STREAM_MAX, FOPEN_MAX,
234 1.24 atatat "The minimum maximum number of streams that a process "
235 1.24 atatat "may have open at any one time"),
236 1.24 atatat _INT("tzname_max", USER_TZNAME_MAX, NAME_MAX,
237 1.24 atatat "The minimum maximum number of types supported for the "
238 1.24 atatat "name of a timezone"),
239 1.24 atatat _INT("atexit_max", USER_ATEXIT_MAX, -1,
240 1.24 atatat "The maximum number of functions that may be registered "
241 1.24 atatat "with atexit(3)"),
242 1.14 atatat #endif /* !lint */
243 1.14 atatat };
244 1.14 atatat #undef _INT
245 1.14 atatat
246 1.14 atatat static const int clen = sizeof(sysctl_usermib) /
247 1.14 atatat sizeof(sysctl_usermib[0]);
248 1.14 atatat
249 1.14 atatat const struct sysctlnode *node;
250 1.14 atatat int ni;
251 1.14 atatat size_t l, sz;
252 1.14 atatat
253 1.14 atatat /*
254 1.14 atatat * none of these nodes are writable and they're all terminal (for now)
255 1.14 atatat */
256 1.14 atatat if (namelen != 1)
257 1.14 atatat return (EINVAL);
258 1.14 atatat
259 1.14 atatat l = *oldlenp;
260 1.14 atatat if (name[0] == CTL_QUERY) {
261 1.18 atatat uint v;
262 1.18 atatat node = newp;
263 1.26 atatat if (node == NULL)
264 1.26 atatat return (EINVAL);
265 1.19 atatat else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
266 1.18 atatat newlen == sizeof(struct sysctlnode))
267 1.19 atatat v = SYSCTL_VERS_1;
268 1.18 atatat else
269 1.18 atatat return (EINVAL);
270 1.18 atatat
271 1.18 atatat sz = 0;
272 1.18 atatat for (ni = 0; ni < clen; ni++)
273 1.18 atatat sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
274 1.14 atatat *oldlenp = sz;
275 1.1 cgd return (0);
276 1.1 cgd }
277 1.18 atatat
278 1.20 atatat if (name[0] == CTL_DESCRIBE) {
279 1.24 atatat /*
280 1.24 atatat * XXX make sure this is larger than the largest
281 1.24 atatat * "user" description
282 1.24 atatat */
283 1.24 atatat char buf[192];
284 1.20 atatat struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp;
285 1.20 atatat size_t d;
286 1.20 atatat
287 1.20 atatat node = newp;
288 1.20 atatat if (node != NULL &&
289 1.20 atatat (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 ||
290 1.20 atatat newlen != sizeof(struct sysctlnode)))
291 1.20 atatat return (EINVAL);
292 1.20 atatat
293 1.20 atatat sz = 0;
294 1.20 atatat for (ni = 0; ni < clen; ni++) {
295 1.20 atatat memset(&buf[0], 0, sizeof(buf));
296 1.20 atatat if (node != NULL &&
297 1.20 atatat node->sysctl_num != sysctl_usermib[ni].sysctl_num)
298 1.20 atatat continue;
299 1.20 atatat d1->descr_num = sysctl_usermib[ni].sysctl_num;
300 1.20 atatat d1->descr_ver = sysctl_usermib[ni].sysctl_ver;
301 1.20 atatat if (sysctl_usermib[ni].sysctl_desc == NULL)
302 1.20 atatat d1->descr_len = 1;
303 1.20 atatat else {
304 1.31 christos size_t dlen;
305 1.27 jrf (void)strlcpy(d1->descr_str,
306 1.20 atatat sysctl_usermib[ni].sysctl_desc,
307 1.20 atatat sizeof(buf) - sizeof(*d1));
308 1.31 christos dlen = strlen(d1->descr_str) + 1;
309 1.31 christos _DIAGASSERT(__type_fit(uint32_t, dlen));
310 1.31 christos d1->descr_len = (uint32_t)dlen;
311 1.20 atatat }
312 1.20 atatat d = (size_t)__sysc_desc_adv(NULL, d1->descr_len);
313 1.20 atatat if (d2 != NULL)
314 1.20 atatat memcpy(d2, d1, d);
315 1.20 atatat sz += d;
316 1.36 christos d2 = (void *)((char *)(void *)d2 + d);
317 1.20 atatat if (node != NULL)
318 1.20 atatat break;
319 1.20 atatat }
320 1.20 atatat *oldlenp = sz;
321 1.20 atatat if (sz == 0 && node != NULL)
322 1.20 atatat return (ENOENT);
323 1.20 atatat return (0);
324 1.20 atatat
325 1.20 atatat }
326 1.20 atatat
327 1.18 atatat /*
328 1.18 atatat * none of these nodes are writable
329 1.18 atatat */
330 1.18 atatat if (newp != NULL || newlen != 0)
331 1.18 atatat return (EPERM);
332 1.14 atatat
333 1.14 atatat node = &sysctl_usermib[0];
334 1.14 atatat for (ni = 0; ni < clen; ni++)
335 1.14 atatat if (name[0] == node[ni].sysctl_num)
336 1.14 atatat break;
337 1.14 atatat if (ni == clen)
338 1.14 atatat return (EOPNOTSUPP);
339 1.14 atatat
340 1.14 atatat node = &node[ni];
341 1.16 atatat if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
342 1.14 atatat switch (SYSCTL_TYPE(node->sysctl_flags)) {
343 1.14 atatat case CTLTYPE_INT:
344 1.14 atatat newp = &node->sysctl_idata;
345 1.14 atatat break;
346 1.14 atatat case CTLTYPE_QUAD:
347 1.14 atatat newp = &node->sysctl_qdata;
348 1.14 atatat break;
349 1.14 atatat default:
350 1.14 atatat return (EINVAL);
351 1.14 atatat }
352 1.14 atatat }
353 1.14 atatat else
354 1.14 atatat newp = node->sysctl_data;
355 1.14 atatat
356 1.14 atatat l = MIN(l, node->sysctl_size);
357 1.14 atatat if (oldp != NULL)
358 1.14 atatat memcpy(oldp, newp, l);
359 1.14 atatat *oldlenp = node->sysctl_size;
360 1.14 atatat
361 1.14 atatat return (0);
362 1.1 cgd }
363 1.18 atatat
364 1.18 atatat static size_t
365 1.18 atatat __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
366 1.18 atatat {
367 1.18 atatat const void *src = n;
368 1.18 atatat size_t sz;
369 1.18 atatat
370 1.18 atatat switch (v) {
371 1.19 atatat #if (SYSCTL_VERSION != SYSCTL_VERS_1)
372 1.19 atatat #error __cvt_node_out: no support for SYSCTL_VERSION
373 1.19 atatat #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
374 1.19 atatat
375 1.18 atatat case SYSCTL_VERSION:
376 1.18 atatat sz = sizeof(struct sysctlnode);
377 1.18 atatat break;
378 1.26 atatat
379 1.18 atatat default:
380 1.18 atatat sz = 0;
381 1.18 atatat break;
382 1.18 atatat }
383 1.18 atatat
384 1.18 atatat if (sz > 0 && *o != NULL && *l >= sz) {
385 1.18 atatat memcpy(*o, src, sz);
386 1.18 atatat *o = sz + (caddr_t)*o;
387 1.18 atatat *l -= sz;
388 1.18 atatat }
389 1.18 atatat
390 1.18 atatat return(sz);
391 1.18 atatat }
392