sysctl.c revision 1.16 1 1.16 atatat /* $NetBSD: sysctl.c,v 1.16 2004/03/24 15:34:56 atatat 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.16 atatat __RCSID("$NetBSD: sysctl.c,v 1.16 2004/03/24 15:34:56 atatat 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.1 cgd #include <sys/sysctl.h>
44 1.1 cgd
45 1.1 cgd #include <errno.h>
46 1.1 cgd #include <paths.h>
47 1.1 cgd #include <stdio.h>
48 1.4 jtc #include <string.h>
49 1.1 cgd #include <unistd.h>
50 1.5 christos #include "extern.h"
51 1.6 jtc
52 1.6 jtc #ifdef __weak_alias
53 1.11 mycroft __weak_alias(sysctl,_sysctl)
54 1.6 jtc #endif
55 1.1 cgd
56 1.14 atatat /*
57 1.14 atatat * handles requests off the user subtree
58 1.14 atatat */
59 1.14 atatat static int user_sysctl(int *, u_int, void *, size_t *, const void *, size_t);
60 1.14 atatat
61 1.14 atatat #include <stdlib.h>
62 1.14 atatat
63 1.1 cgd int
64 1.1 cgd sysctl(name, namelen, oldp, oldlenp, newp, newlen)
65 1.1 cgd int *name;
66 1.8 kleink unsigned int namelen;
67 1.7 christos void *oldp;
68 1.7 christos const void *newp;
69 1.1 cgd size_t *oldlenp, newlen;
70 1.1 cgd {
71 1.14 atatat size_t oldlen, savelen;
72 1.14 atatat int error;
73 1.9 lukem
74 1.16 atatat if (getenv("SYSCTL_TRACE") != NULL) {
75 1.16 atatat char trb[128];
76 1.16 atatat uint l, i;
77 1.16 atatat
78 1.16 atatat l = snprintf(trb, sizeof(trb), "sysctl(%p {", name);
79 1.16 atatat write(2, &trb[0], l);
80 1.16 atatat for (i = 0; i < namelen; i++) {
81 1.16 atatat l = snprintf(trb, sizeof(trb), "%s%d", i ? "." : "",
82 1.16 atatat name[i]);
83 1.16 atatat write(2, &trb[0], l);
84 1.16 atatat }
85 1.16 atatat l = snprintf(trb, sizeof(trb), "}, %d, %p, %p (%lu), %p, %lu)\n",
86 1.16 atatat namelen,
87 1.16 atatat oldp,
88 1.16 atatat oldlenp, (ulong)(oldlenp ? *oldlenp : 0),
89 1.16 atatat newp,
90 1.16 atatat (ulong)newlen);
91 1.16 atatat write(2, &trb[0], l);
92 1.16 atatat }
93 1.16 atatat
94 1.1 cgd if (name[0] != CTL_USER)
95 1.7 christos /* LINTED will fix when sysctl interface gets corrected */
96 1.14 atatat /* XXX when will that be? */
97 1.9 lukem return (__sysctl(name, namelen, oldp, oldlenp,
98 1.14 atatat (void *)newp, newlen));
99 1.14 atatat
100 1.14 atatat oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
101 1.14 atatat savelen = oldlen;
102 1.14 atatat error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
103 1.1 cgd
104 1.14 atatat if (error != 0) {
105 1.14 atatat errno = error;
106 1.1 cgd return (-1);
107 1.1 cgd }
108 1.1 cgd
109 1.14 atatat if (oldlenp != NULL) {
110 1.14 atatat *oldlenp = oldlen;
111 1.14 atatat if (oldp != NULL && oldlen > savelen) {
112 1.14 atatat errno = ENOMEM;
113 1.14 atatat return (-1);
114 1.14 atatat }
115 1.1 cgd }
116 1.1 cgd
117 1.14 atatat return (0);
118 1.14 atatat }
119 1.1 cgd
120 1.14 atatat static int
121 1.14 atatat user_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
122 1.14 atatat int *name;
123 1.14 atatat unsigned int namelen;
124 1.14 atatat void *oldp;
125 1.14 atatat const void *newp;
126 1.14 atatat size_t *oldlenp, newlen;
127 1.14 atatat {
128 1.14 atatat #define _INT(s, n, v) { \
129 1.16 atatat .sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT| \
130 1.16 atatat CTLTYPE_INT|SYSCTL_VERSION, \
131 1.14 atatat .sysctl_size = sizeof(int), \
132 1.14 atatat .sysctl_name = (s), \
133 1.14 atatat .sysctl_num = (n), \
134 1.14 atatat .sysctl_un = { .scu_idata = (v), }, }
135 1.14 atatat
136 1.14 atatat /*
137 1.14 atatat * the nodes under the "user" node
138 1.14 atatat */
139 1.14 atatat static const struct sysctlnode sysctl_usermib[] = {
140 1.14 atatat #if defined(lint)
141 1.14 atatat /*
142 1.14 atatat * lint doesn't like my initializers
143 1.14 atatat */
144 1.14 atatat 0
145 1.14 atatat #else /* !lint */
146 1.14 atatat {
147 1.16 atatat .sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
148 1.16 atatat CTLTYPE_STRING,
149 1.14 atatat .sysctl_size = sizeof(_PATH_STDPATH),
150 1.14 atatat .sysctl_name = "cs_path",
151 1.14 atatat .sysctl_num = USER_CS_PATH,
152 1.14 atatat .sysctl_un = { .scu_data = _PATH_STDPATH, },
153 1.14 atatat },
154 1.14 atatat _INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX),
155 1.14 atatat _INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX),
156 1.14 atatat _INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX),
157 1.14 atatat _INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX),
158 1.14 atatat _INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
159 1.14 atatat COLL_WEIGHTS_MAX),
160 1.14 atatat _INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX),
161 1.14 atatat _INT("line_max", USER_LINE_MAX, LINE_MAX),
162 1.14 atatat _INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX),
163 1.14 atatat _INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION),
164 1.1 cgd #ifdef POSIX2_C_BIND
165 1.14 atatat _INT("posix2_c_bind", USER_POSIX2_C_BIND, 1),
166 1.1 cgd #else
167 1.14 atatat _INT("posix2_c_bind", USER_POSIX2_C_BIND, 0),
168 1.1 cgd #endif
169 1.14 atatat #ifdef POSIX2_C_DEV
170 1.14 atatat _INT("posix2_c_dev", USER_POSIX2_C_DEV, 1),
171 1.1 cgd #else
172 1.14 atatat _INT("posix2_c_dev", USER_POSIX2_C_DEV, 0),
173 1.1 cgd #endif
174 1.14 atatat #ifdef POSIX2_CHAR_TERM
175 1.14 atatat _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1),
176 1.1 cgd #else
177 1.14 atatat _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0),
178 1.1 cgd #endif
179 1.14 atatat #ifdef POSIX2_FORT_DEV
180 1.14 atatat _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1),
181 1.1 cgd #else
182 1.14 atatat _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0),
183 1.1 cgd #endif
184 1.14 atatat #ifdef POSIX2_FORT_RUN
185 1.14 atatat _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1),
186 1.1 cgd #else
187 1.14 atatat _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0),
188 1.1 cgd #endif
189 1.14 atatat #ifdef POSIX2_LOCALEDEF
190 1.14 atatat _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1),
191 1.1 cgd #else
192 1.14 atatat _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0),
193 1.1 cgd #endif
194 1.14 atatat #ifdef POSIX2_SW_DEV
195 1.14 atatat _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1),
196 1.1 cgd #else
197 1.14 atatat _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0),
198 1.1 cgd #endif
199 1.14 atatat #ifdef POSIX2_UPE
200 1.14 atatat _INT("posix2_upe", USER_POSIX2_UPE, 1),
201 1.1 cgd #else
202 1.14 atatat _INT("posix2_upe", USER_POSIX2_UPE, 0),
203 1.1 cgd #endif
204 1.14 atatat _INT("stream_max", USER_STREAM_MAX, FOPEN_MAX),
205 1.14 atatat _INT("tzname_max", USER_TZNAME_MAX, NAME_MAX),
206 1.14 atatat _INT("atexit_max", USER_ATEXIT_MAX, -1),
207 1.14 atatat #endif /* !lint */
208 1.14 atatat };
209 1.14 atatat #undef _INT
210 1.14 atatat
211 1.14 atatat static const int clen = sizeof(sysctl_usermib) /
212 1.14 atatat sizeof(sysctl_usermib[0]);
213 1.14 atatat
214 1.14 atatat const struct sysctlnode *node;
215 1.14 atatat int ni;
216 1.14 atatat size_t l, sz;
217 1.14 atatat
218 1.14 atatat /*
219 1.14 atatat * none of these nodes are writable and they're all terminal (for now)
220 1.14 atatat */
221 1.14 atatat if (newp != NULL || newlen != 0)
222 1.14 atatat return (EPERM);
223 1.14 atatat if (namelen != 1)
224 1.14 atatat return (EINVAL);
225 1.14 atatat
226 1.14 atatat l = *oldlenp;
227 1.14 atatat if (name[0] == CTL_QUERY) {
228 1.14 atatat sz = clen * sizeof(struct sysctlnode);
229 1.14 atatat l = MIN(l, sz);
230 1.14 atatat if (oldp != NULL)
231 1.14 atatat memcpy(oldp, &sysctl_usermib[0], l);
232 1.14 atatat *oldlenp = sz;
233 1.1 cgd return (0);
234 1.1 cgd }
235 1.14 atatat
236 1.14 atatat node = &sysctl_usermib[0];
237 1.14 atatat for (ni = 0; ni < clen; ni++)
238 1.14 atatat if (name[0] == node[ni].sysctl_num)
239 1.14 atatat break;
240 1.14 atatat if (ni == clen)
241 1.14 atatat return (EOPNOTSUPP);
242 1.14 atatat
243 1.14 atatat node = &node[ni];
244 1.16 atatat if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
245 1.14 atatat switch (SYSCTL_TYPE(node->sysctl_flags)) {
246 1.14 atatat case CTLTYPE_INT:
247 1.14 atatat newp = &node->sysctl_idata;
248 1.14 atatat break;
249 1.14 atatat case CTLTYPE_QUAD:
250 1.14 atatat newp = &node->sysctl_qdata;
251 1.14 atatat break;
252 1.14 atatat default:
253 1.14 atatat return (EINVAL);
254 1.14 atatat }
255 1.14 atatat }
256 1.14 atatat else
257 1.14 atatat newp = node->sysctl_data;
258 1.14 atatat
259 1.14 atatat l = MIN(l, node->sysctl_size);
260 1.14 atatat if (oldp != NULL)
261 1.14 atatat memcpy(oldp, newp, l);
262 1.14 atatat *oldlenp = node->sysctl_size;
263 1.14 atatat
264 1.14 atatat return (0);
265 1.1 cgd }
266