kern_sysctl.c revision 1.166 1 1.166 atatat /* $NetBSD: kern_sysctl.c,v 1.166 2004/03/24 18:11:09 atatat Exp $ */
2 1.153 atatat
3 1.153 atatat /*-
4 1.153 atatat * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.153 atatat * All rights reserved.
6 1.153 atatat *
7 1.153 atatat * This code is derived from software contributed to The NetBSD Foundation
8 1.153 atatat * by Andrew Brown.
9 1.153 atatat *
10 1.153 atatat * Redistribution and use in source and binary forms, with or without
11 1.153 atatat * modification, are permitted provided that the following conditions
12 1.153 atatat * are met:
13 1.153 atatat * 1. Redistributions of source code must retain the above copyright
14 1.153 atatat * notice, this list of conditions and the following disclaimer.
15 1.153 atatat * 2. Redistributions in binary form must reproduce the above copyright
16 1.153 atatat * notice, this list of conditions and the following disclaimer in the
17 1.153 atatat * documentation and/or other materials provided with the distribution.
18 1.153 atatat * 3. All advertising materials mentioning features or use of this software
19 1.153 atatat * must display the following acknowledgement:
20 1.153 atatat * This product includes software developed by the NetBSD
21 1.153 atatat * Foundation, Inc. and its contributors.
22 1.153 atatat * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.153 atatat * contributors may be used to endorse or promote products derived
24 1.153 atatat * from this software without specific prior written permission.
25 1.153 atatat *
26 1.153 atatat * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.153 atatat * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.153 atatat * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.153 atatat * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.153 atatat * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.153 atatat * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.153 atatat * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.153 atatat * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.153 atatat * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.153 atatat * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.153 atatat * POSSIBILITY OF SUCH DAMAGE.
37 1.153 atatat */
38 1.2 cgd
39 1.1 cgd /*-
40 1.1 cgd * Copyright (c) 1982, 1986, 1989, 1993
41 1.1 cgd * The Regents of the University of California. All rights reserved.
42 1.1 cgd *
43 1.1 cgd * This code is derived from software contributed to Berkeley by
44 1.1 cgd * Mike Karels at Berkeley Software Design, Inc.
45 1.1 cgd *
46 1.1 cgd * Redistribution and use in source and binary forms, with or without
47 1.1 cgd * modification, are permitted provided that the following conditions
48 1.1 cgd * are met:
49 1.1 cgd * 1. Redistributions of source code must retain the above copyright
50 1.1 cgd * notice, this list of conditions and the following disclaimer.
51 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 cgd * notice, this list of conditions and the following disclaimer in the
53 1.1 cgd * documentation and/or other materials provided with the distribution.
54 1.140 agc * 3. Neither the name of the University nor the names of its contributors
55 1.1 cgd * may be used to endorse or promote products derived from this software
56 1.1 cgd * without specific prior written permission.
57 1.1 cgd *
58 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 cgd * SUCH DAMAGE.
69 1.1 cgd *
70 1.34 fvdl * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
71 1.1 cgd */
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * sysctl system call.
75 1.1 cgd */
76 1.97 lukem
77 1.97 lukem #include <sys/cdefs.h>
78 1.166 atatat __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.166 2004/03/24 18:11:09 atatat Exp $");
79 1.30 thorpej
80 1.52 bouyer #include "opt_defcorename.h"
81 1.154 fvdl #include "opt_insecure.h"
82 1.153 atatat #include "ksyms.h"
83 1.1 cgd
84 1.1 cgd #include <sys/param.h>
85 1.164 atatat #define __COMPAT_SYSCTL
86 1.153 atatat #include <sys/sysctl.h>
87 1.1 cgd #include <sys/systm.h>
88 1.62 simonb #include <sys/buf.h>
89 1.153 atatat #include <sys/ksyms.h>
90 1.1 cgd #include <sys/malloc.h>
91 1.62 simonb #include <sys/mount.h>
92 1.123 thorpej #include <sys/sa.h>
93 1.62 simonb #include <sys/syscallargs.h>
94 1.153 atatat #include <machine/stdarg.h>
95 1.153 atatat
96 1.153 atatat MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures");
97 1.153 atatat MALLOC_DEFINE(M_SYSCTLDATA, "sysctldata", "misc sysctl data");
98 1.153 atatat
99 1.153 atatat static int sysctl_mmap(SYSCTLFN_RWPROTO);
100 1.153 atatat static int sysctl_alloc(struct sysctlnode *, int);
101 1.153 atatat static int sysctl_realloc(struct sysctlnode *);
102 1.38 jonathan
103 1.162 atatat static int sysctl_cvt_in(struct lwp *, int *, const void *, size_t,
104 1.162 atatat struct sysctlnode *);
105 1.162 atatat static int sysctl_cvt_out(struct lwp *, int, const struct sysctlnode *,
106 1.162 atatat void *, size_t, size_t *);
107 1.162 atatat
108 1.163 atatat static int sysctl_log_add(struct sysctllog **, struct sysctlnode *);
109 1.163 atatat static int sysctl_log_realloc(struct sysctllog *);
110 1.163 atatat
111 1.163 atatat struct sysctllog {
112 1.163 atatat struct sysctlnode *log_root;
113 1.163 atatat int *log_num;
114 1.163 atatat int log_size, log_left;
115 1.163 atatat };
116 1.163 atatat
117 1.153 atatat /*
118 1.153 atatat * the "root" of the new sysctl tree
119 1.153 atatat */
120 1.153 atatat static struct sysctlnode sysctl_root = {
121 1.161 atatat .sysctl_flags = SYSCTL_VERSION|
122 1.161 atatat CTLFLAG_ROOT|CTLFLAG_READWRITE|
123 1.153 atatat CTLTYPE_NODE,
124 1.153 atatat .sysctl_num = 0,
125 1.153 atatat .sysctl_size = sizeof(struct sysctlnode),
126 1.153 atatat .sysctl_name = "(root)",
127 1.153 atatat };
128 1.69 simonb
129 1.153 atatat /*
130 1.153 atatat * link set of functions that add nodes at boot time (see also
131 1.153 atatat * sysctl_buildtree())
132 1.153 atatat */
133 1.153 atatat __link_set_decl(sysctl_funcs, sysctl_setup_func);
134 1.74 simonb
135 1.153 atatat /*
136 1.153 atatat * The `sysctl_lock' is intended to serialize access to the sysctl
137 1.153 atatat * tree. Given that it is now (a) dynamic, and (b) most consumers of
138 1.153 atatat * sysctl are going to be copying data out, the old `sysctl_memlock'
139 1.153 atatat * has been `upgraded' to simply guard the whole tree.
140 1.153 atatat *
141 1.153 atatat * The two new data here are to keep track of the locked chunk of
142 1.153 atatat * memory, if there is one, so that it can be released more easily
143 1.153 atatat * from anywhere.
144 1.153 atatat */
145 1.153 atatat struct lock sysctl_treelock;
146 1.153 atatat caddr_t sysctl_memaddr;
147 1.153 atatat size_t sysctl_memsize;
148 1.90 jdolecek
149 1.153 atatat /*
150 1.153 atatat * Attributes stored in the kernel.
151 1.153 atatat */
152 1.153 atatat char hostname[MAXHOSTNAMELEN];
153 1.153 atatat int hostnamelen;
154 1.31 mrg
155 1.153 atatat char domainname[MAXHOSTNAMELEN];
156 1.153 atatat int domainnamelen;
157 1.109 itojun
158 1.153 atatat long hostid;
159 1.62 simonb
160 1.153 atatat #ifdef INSECURE
161 1.153 atatat int securelevel = -1;
162 1.153 atatat #else
163 1.153 atatat int securelevel = 0;
164 1.69 simonb #endif
165 1.153 atatat
166 1.153 atatat #ifndef DEFCORENAME
167 1.153 atatat #define DEFCORENAME "%n.core"
168 1.80 bjh21 #endif
169 1.153 atatat char defcorename[MAXPATHLEN] = DEFCORENAME;
170 1.62 simonb
171 1.75 thorpej /*
172 1.153 atatat * ********************************************************************
173 1.153 atatat * Section 0: Some simple glue
174 1.153 atatat * ********************************************************************
175 1.153 atatat * By wrapping copyin(), copyout(), and copyinstr() like this, we can
176 1.153 atatat * stop caring about who's calling us and simplify some code a bunch.
177 1.153 atatat * ********************************************************************
178 1.75 thorpej */
179 1.153 atatat static inline int
180 1.153 atatat sysctl_copyin(const struct lwp *l, const void *uaddr, void *kaddr, size_t len)
181 1.153 atatat {
182 1.153 atatat
183 1.153 atatat if (l != NULL)
184 1.153 atatat return (copyin(uaddr, kaddr, len));
185 1.156 atatat else
186 1.156 atatat return (kcopy(uaddr, kaddr, len));
187 1.153 atatat }
188 1.153 atatat
189 1.153 atatat static inline int
190 1.153 atatat sysctl_copyout(const struct lwp *l, const void *kaddr, void *uaddr, size_t len)
191 1.153 atatat {
192 1.153 atatat
193 1.153 atatat if (l != NULL)
194 1.153 atatat return (copyout(kaddr, uaddr, len));
195 1.156 atatat else
196 1.156 atatat return (kcopy(kaddr, uaddr, len));
197 1.153 atatat }
198 1.153 atatat
199 1.153 atatat static inline int
200 1.153 atatat sysctl_copyinstr(const struct lwp *l, const void *uaddr, void *kaddr,
201 1.153 atatat size_t len, size_t *done)
202 1.153 atatat {
203 1.75 thorpej
204 1.153 atatat if (l != NULL)
205 1.153 atatat return (copyinstr(uaddr, kaddr, len, done));
206 1.153 atatat else
207 1.153 atatat return (copystr(uaddr, kaddr, len, done));
208 1.153 atatat }
209 1.153 atatat
210 1.153 atatat /*
211 1.153 atatat * ********************************************************************
212 1.153 atatat * Initialize sysctl subsystem.
213 1.153 atatat * ********************************************************************
214 1.153 atatat */
215 1.75 thorpej void
216 1.75 thorpej sysctl_init(void)
217 1.75 thorpej {
218 1.153 atatat sysctl_setup_func **sysctl_setup, f;
219 1.153 atatat
220 1.153 atatat lockinit(&sysctl_treelock, PRIBIO|PCATCH, "sysctl", 0, 0);
221 1.153 atatat
222 1.153 atatat /*
223 1.153 atatat * dynamic mib numbers start here
224 1.153 atatat */
225 1.153 atatat sysctl_root.sysctl_num = CREATE_BASE;
226 1.153 atatat
227 1.153 atatat __link_set_foreach(sysctl_setup, sysctl_funcs) {
228 1.153 atatat /*
229 1.153 atatat * XXX - why do i have to coerce the pointers like this?
230 1.153 atatat */
231 1.153 atatat f = (void*)*sysctl_setup;
232 1.161 atatat (*f)(NULL);
233 1.153 atatat }
234 1.153 atatat
235 1.153 atatat /*
236 1.153 atatat * setting this means no more permanent nodes can be added,
237 1.153 atatat * trees that claim to be readonly at the root now are, and if
238 1.153 atatat * the main tree is readonly, *everything* is.
239 1.153 atatat */
240 1.161 atatat sysctl_root.sysctl_flags |= CTLFLAG_PERMANENT;
241 1.75 thorpej
242 1.75 thorpej }
243 1.75 thorpej
244 1.153 atatat /*
245 1.153 atatat * ********************************************************************
246 1.153 atatat * The main native sysctl system call itself.
247 1.153 atatat * ********************************************************************
248 1.153 atatat */
249 1.1 cgd int
250 1.123 thorpej sys___sysctl(struct lwp *l, void *v, register_t *retval)
251 1.11 thorpej {
252 1.60 augustss struct sys___sysctl_args /* {
253 1.5 cgd syscallarg(int *) name;
254 1.5 cgd syscallarg(u_int) namelen;
255 1.5 cgd syscallarg(void *) old;
256 1.5 cgd syscallarg(size_t *) oldlenp;
257 1.5 cgd syscallarg(void *) new;
258 1.5 cgd syscallarg(size_t) newlen;
259 1.11 thorpej } */ *uap = v;
260 1.153 atatat int error, nerror, name[CTL_MAXNAME];
261 1.153 atatat size_t oldlen, savelen, *oldlenp;
262 1.153 atatat
263 1.153 atatat /*
264 1.153 atatat * get oldlen
265 1.153 atatat */
266 1.153 atatat oldlen = 0;
267 1.153 atatat oldlenp = SCARG(uap, oldlenp);
268 1.153 atatat if (oldlenp != NULL) {
269 1.153 atatat error = copyin(oldlenp, &oldlen, sizeof(oldlen));
270 1.153 atatat if (error)
271 1.153 atatat return (error);
272 1.153 atatat }
273 1.153 atatat savelen = oldlen;
274 1.1 cgd
275 1.1 cgd /*
276 1.153 atatat * top-level sysctl names may or may not be non-terminal, but
277 1.153 atatat * we don't care
278 1.1 cgd */
279 1.153 atatat if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
280 1.1 cgd return (EINVAL);
281 1.13 christos error = copyin(SCARG(uap, name), &name,
282 1.153 atatat SCARG(uap, namelen) * sizeof(int));
283 1.13 christos if (error)
284 1.1 cgd return (error);
285 1.1 cgd
286 1.52 bouyer /*
287 1.153 atatat * wire old so that copyout() is less likely to fail?
288 1.52 bouyer */
289 1.153 atatat error = sysctl_lock(l, SCARG(uap, old), savelen);
290 1.153 atatat if (error)
291 1.129 enami return (error);
292 1.52 bouyer
293 1.153 atatat /*
294 1.153 atatat * do sysctl work (NULL means main built-in default tree)
295 1.153 atatat */
296 1.153 atatat error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
297 1.153 atatat SCARG(uap, old), &oldlen,
298 1.153 atatat SCARG(uap, new), SCARG(uap, newlen),
299 1.153 atatat &name[0], l, NULL);
300 1.153 atatat
301 1.153 atatat /*
302 1.153 atatat * release the sysctl lock
303 1.153 atatat */
304 1.153 atatat sysctl_unlock(l);
305 1.104 christos
306 1.153 atatat /*
307 1.153 atatat * set caller's oldlen to new value even in the face of an
308 1.153 atatat * error (if this gets an error and they didn't have one, they
309 1.153 atatat * get this one)
310 1.153 atatat */
311 1.153 atatat if (oldlenp) {
312 1.153 atatat nerror = copyout(&oldlen, oldlenp, sizeof(oldlen));
313 1.153 atatat if (error == 0)
314 1.153 atatat error = nerror;
315 1.1 cgd }
316 1.1 cgd
317 1.75 thorpej /*
318 1.153 atatat * if the only problem is that we weren't given enough space,
319 1.153 atatat * that's an ENOMEM error
320 1.75 thorpej */
321 1.153 atatat if (error == 0 && SCARG(uap, old) != NULL && savelen < oldlen)
322 1.153 atatat error = ENOMEM;
323 1.153 atatat
324 1.153 atatat return (error);
325 1.153 atatat }
326 1.153 atatat
327 1.153 atatat /*
328 1.153 atatat * ********************************************************************
329 1.153 atatat * Section 1: How the tree is used
330 1.153 atatat * ********************************************************************
331 1.153 atatat * Implementations of sysctl for emulations should typically need only
332 1.153 atatat * these three functions in this order: lock the tree, dispatch
333 1.153 atatat * request into it, unlock the tree.
334 1.153 atatat * ********************************************************************
335 1.153 atatat */
336 1.153 atatat int
337 1.153 atatat sysctl_lock(struct lwp *l, void *oldp, size_t savelen)
338 1.153 atatat {
339 1.153 atatat int error = 0;
340 1.75 thorpej
341 1.153 atatat error = lockmgr(&sysctl_treelock, LK_EXCLUSIVE, NULL);
342 1.153 atatat if (error)
343 1.153 atatat return (error);
344 1.153 atatat
345 1.153 atatat if (l != NULL && oldp != NULL && savelen) {
346 1.153 atatat error = uvm_vslock(l->l_proc, oldp, savelen, VM_PROT_WRITE);
347 1.87 chs if (error) {
348 1.153 atatat (void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
349 1.129 enami return (error);
350 1.45 thorpej }
351 1.153 atatat sysctl_memaddr = oldp;
352 1.153 atatat sysctl_memsize = savelen;
353 1.1 cgd }
354 1.153 atatat
355 1.153 atatat return (0);
356 1.1 cgd }
357 1.1 cgd
358 1.1 cgd /*
359 1.153 atatat * ********************************************************************
360 1.153 atatat * the main sysctl dispatch routine. scans the given tree and picks a
361 1.153 atatat * function to call based on what it finds.
362 1.153 atatat * ********************************************************************
363 1.1 cgd */
364 1.153 atatat int
365 1.153 atatat sysctl_dispatch(SYSCTLFN_RWARGS)
366 1.153 atatat {
367 1.153 atatat int error;
368 1.153 atatat sysctlfn fn;
369 1.153 atatat int ni;
370 1.75 thorpej
371 1.161 atatat if (rnode && SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
372 1.161 atatat printf("sysctl_dispatch: rnode %p wrong version\n", rnode);
373 1.161 atatat return (EINVAL);
374 1.161 atatat }
375 1.161 atatat
376 1.153 atatat fn = NULL;
377 1.153 atatat error = sysctl_locate(l, name, namelen, &rnode, &ni);
378 1.75 thorpej
379 1.153 atatat /*
380 1.153 atatat * the node we ended up at has a function, so call it. it can
381 1.153 atatat * hand off to query or create if it wants to.
382 1.153 atatat */
383 1.153 atatat if (rnode->sysctl_func != NULL)
384 1.153 atatat fn = rnode->sysctl_func;
385 1.75 thorpej
386 1.153 atatat /*
387 1.153 atatat * we found the node they were looking for, so do a lookup.
388 1.153 atatat */
389 1.153 atatat else if (error == 0)
390 1.153 atatat fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */
391 1.75 thorpej
392 1.153 atatat /*
393 1.153 atatat * prospective parent node found, but the terminal node was
394 1.153 atatat * not. generic operations associate with the parent.
395 1.153 atatat */
396 1.153 atatat else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) {
397 1.153 atatat switch (name[ni]) {
398 1.153 atatat case CTL_QUERY:
399 1.153 atatat fn = sysctl_query;
400 1.153 atatat break;
401 1.153 atatat case CTL_CREATE:
402 1.153 atatat #if NKSYMS > 0
403 1.153 atatat case CTL_CREATESYM:
404 1.153 atatat #endif /* NKSYMS > 0 */
405 1.153 atatat fn = (sysctlfn)sysctl_create; /* we own the rnode */
406 1.153 atatat break;
407 1.153 atatat case CTL_DESTROY:
408 1.153 atatat fn = (sysctlfn)sysctl_destroy; /* we own the rnode */
409 1.153 atatat break;
410 1.153 atatat case CTL_MMAP:
411 1.153 atatat fn = (sysctlfn)sysctl_mmap; /* we own the rnode */
412 1.153 atatat break;
413 1.165 atatat case CTL_DESCRIBE:
414 1.165 atatat fn = sysctl_describe;
415 1.165 atatat break;
416 1.153 atatat default:
417 1.153 atatat error = EOPNOTSUPP;
418 1.153 atatat break;
419 1.153 atatat }
420 1.153 atatat }
421 1.75 thorpej
422 1.153 atatat /*
423 1.153 atatat * after all of that, maybe we found someone who knows how to
424 1.153 atatat * get us what we want?
425 1.153 atatat */
426 1.153 atatat if (fn != NULL)
427 1.153 atatat error = (*fn)(name + ni, namelen - ni, oldp, oldlenp,
428 1.153 atatat newp, newlen, name, l, rnode);
429 1.1 cgd
430 1.153 atatat else if (error == 0)
431 1.153 atatat error = EOPNOTSUPP;
432 1.84 sommerfe
433 1.153 atatat return (error);
434 1.153 atatat }
435 1.83 sommerfe
436 1.153 atatat /*
437 1.153 atatat * ********************************************************************
438 1.153 atatat * Releases the tree lock. Note that if uvm_vslock() was called when
439 1.153 atatat * the lock was taken, we release that memory now. By keeping track
440 1.153 atatat * of where and how much by ourselves, the lock can be released much
441 1.153 atatat * more easily from anywhere.
442 1.153 atatat * ********************************************************************
443 1.153 atatat */
444 1.153 atatat void
445 1.153 atatat sysctl_unlock(struct lwp *l)
446 1.83 sommerfe {
447 1.84 sommerfe
448 1.153 atatat if (l != NULL && sysctl_memsize != 0) {
449 1.153 atatat uvm_vsunlock(l->l_proc, sysctl_memaddr, sysctl_memsize);
450 1.153 atatat sysctl_memsize = 0;
451 1.153 atatat }
452 1.84 sommerfe
453 1.153 atatat (void) lockmgr(&sysctl_treelock, LK_RELEASE, NULL);
454 1.84 sommerfe }
455 1.84 sommerfe
456 1.153 atatat /*
457 1.153 atatat * ********************************************************************
458 1.153 atatat * Section 2: The main tree interfaces
459 1.153 atatat * ********************************************************************
460 1.153 atatat * This is how sysctl_dispatch() does its work, and you can too, by
461 1.153 atatat * calling these routines from helpers (though typically only
462 1.153 atatat * sysctl_lookup() will be used). The tree MUST BE LOCKED when these
463 1.153 atatat * are called.
464 1.153 atatat * ********************************************************************
465 1.153 atatat */
466 1.83 sommerfe
467 1.1 cgd /*
468 1.153 atatat * sysctl_locate -- Finds the node matching the given mib under the
469 1.153 atatat * given tree (via rv). If no tree is given, we fall back to the
470 1.153 atatat * native tree. The current process (via l) is used for access
471 1.156 atatat * control on the tree (some nodes may be traversable only by root) and
472 1.153 atatat * on return, nip will show how many numbers in the mib were consumed.
473 1.1 cgd */
474 1.13 christos int
475 1.153 atatat sysctl_locate(struct lwp *l, const int *name, u_int namelen,
476 1.158 atatat struct sysctlnode **rnode, int *nip)
477 1.1 cgd {
478 1.153 atatat struct sysctlnode *node, *pnode;
479 1.153 atatat int tn, si, ni, error, alias;
480 1.153 atatat
481 1.153 atatat /*
482 1.153 atatat * basic checks and setup
483 1.153 atatat */
484 1.158 atatat if (*rnode == NULL)
485 1.158 atatat *rnode = &sysctl_root;
486 1.153 atatat if (nip)
487 1.153 atatat *nip = 0;
488 1.153 atatat if (namelen < 0)
489 1.153 atatat return (EINVAL);
490 1.153 atatat if (namelen == 0)
491 1.153 atatat return (0);
492 1.1 cgd
493 1.153 atatat /*
494 1.153 atatat * search starts from "root"
495 1.153 atatat */
496 1.158 atatat pnode = *rnode;
497 1.161 atatat if (SYSCTL_VERS(pnode->sysctl_flags) != SYSCTL_VERSION) {
498 1.161 atatat printf("sysctl_locate: pnode %p wrong version\n", pnode);
499 1.161 atatat return (EINVAL);
500 1.161 atatat }
501 1.153 atatat node = pnode->sysctl_child;
502 1.153 atatat error = 0;
503 1.1 cgd
504 1.153 atatat /*
505 1.153 atatat * scan for node to which new node should be attached
506 1.153 atatat */
507 1.153 atatat for (ni = 0; ni < namelen; ni++) {
508 1.153 atatat /*
509 1.153 atatat * walked off bottom of tree
510 1.153 atatat */
511 1.153 atatat if (node == NULL) {
512 1.153 atatat if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
513 1.153 atatat error = ENOENT;
514 1.153 atatat else
515 1.153 atatat error = ENOTDIR;
516 1.153 atatat break;
517 1.153 atatat }
518 1.153 atatat /*
519 1.153 atatat * can anyone traverse this node or only root?
520 1.153 atatat */
521 1.161 atatat if (l != NULL && (pnode->sysctl_flags & CTLFLAG_PRIVATE) &&
522 1.153 atatat (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag))
523 1.153 atatat != 0)
524 1.153 atatat return (error);
525 1.153 atatat /*
526 1.153 atatat * find a child node with the right number
527 1.153 atatat */
528 1.153 atatat tn = name[ni];
529 1.153 atatat alias = 0;
530 1.159 atatat
531 1.159 atatat si = 0;
532 1.159 atatat /*
533 1.159 atatat * Note: ANYNUMBER only matches positive integers.
534 1.159 atatat * Since ANYNUMBER is only permitted on single-node
535 1.159 atatat * sub-trees (eg proc), check before the loop and skip
536 1.159 atatat * it if we can.
537 1.159 atatat */
538 1.161 atatat if ((node[si].sysctl_flags & CTLFLAG_ANYNUMBER) && (tn >= 0))
539 1.159 atatat goto foundit;
540 1.159 atatat for (; si < pnode->sysctl_clen; si++) {
541 1.159 atatat if (node[si].sysctl_num == tn) {
542 1.161 atatat if (node[si].sysctl_flags & CTLFLAG_ALIAS) {
543 1.153 atatat if (alias++ == 4)
544 1.159 atatat break;
545 1.153 atatat else {
546 1.153 atatat tn = node[si].sysctl_alias;
547 1.153 atatat si = -1;
548 1.153 atatat }
549 1.139 yamt }
550 1.153 atatat else
551 1.159 atatat goto foundit;
552 1.99 lukem }
553 1.29 sommerfe }
554 1.153 atatat /*
555 1.153 atatat * if we ran off the end, it obviously doesn't exist
556 1.153 atatat */
557 1.159 atatat error = ENOENT;
558 1.159 atatat break;
559 1.159 atatat
560 1.153 atatat /*
561 1.153 atatat * so far so good, move on down the line
562 1.153 atatat */
563 1.159 atatat foundit:
564 1.153 atatat pnode = &node[si];
565 1.153 atatat if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE)
566 1.153 atatat node = node[si].sysctl_child;
567 1.153 atatat else
568 1.153 atatat node = NULL;
569 1.153 atatat }
570 1.153 atatat
571 1.158 atatat *rnode = pnode;
572 1.153 atatat if (nip)
573 1.153 atatat *nip = ni;
574 1.153 atatat
575 1.153 atatat return (error);
576 1.153 atatat }
577 1.153 atatat
578 1.153 atatat /*
579 1.165 atatat * sysctl_query -- The auto-discovery engine. Copies out the structs
580 1.165 atatat * describing nodes under the given node and handles overlay trees.
581 1.153 atatat */
582 1.153 atatat int
583 1.153 atatat sysctl_query(SYSCTLFN_ARGS)
584 1.153 atatat {
585 1.162 atatat int error, ni, elim, v;
586 1.153 atatat size_t out, left, t;
587 1.162 atatat struct sysctlnode *enode, *onode, qnode;
588 1.121 jdolecek
589 1.161 atatat if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
590 1.161 atatat printf("sysctl_query: rnode %p wrong version\n", rnode);
591 1.161 atatat return (EINVAL);
592 1.161 atatat }
593 1.161 atatat
594 1.153 atatat if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
595 1.153 atatat return (ENOTDIR);
596 1.153 atatat if (namelen != 1 || name[0] != CTL_QUERY)
597 1.153 atatat return (EINVAL);
598 1.121 jdolecek
599 1.153 atatat error = 0;
600 1.153 atatat out = 0;
601 1.153 atatat left = *oldlenp;
602 1.153 atatat elim = 0;
603 1.153 atatat enode = NULL;
604 1.121 jdolecek
605 1.153 atatat /*
606 1.162 atatat * translate the given request to a current node
607 1.162 atatat */
608 1.162 atatat error = sysctl_cvt_in(l, &v, newp, newlen, &qnode);
609 1.162 atatat if (error)
610 1.162 atatat return (error);
611 1.162 atatat
612 1.162 atatat /*
613 1.162 atatat * if the request specifies a version, check it
614 1.162 atatat */
615 1.162 atatat if (qnode.sysctl_ver != 0) {
616 1.162 atatat enode = (struct sysctlnode *)rnode; /* discard const */
617 1.162 atatat if (qnode.sysctl_ver != enode->sysctl_ver &&
618 1.162 atatat qnode.sysctl_ver != sysctl_rootof(enode)->sysctl_ver)
619 1.162 atatat return (EINVAL);
620 1.162 atatat }
621 1.162 atatat
622 1.162 atatat /*
623 1.153 atatat * process has overlay tree
624 1.153 atatat */
625 1.153 atatat if (l && l->l_proc->p_emul->e_sysctlovly) {
626 1.161 atatat enode = l->l_proc->p_emul->e_sysctlovly;
627 1.153 atatat elim = (name - oname);
628 1.153 atatat error = sysctl_locate(l, oname, elim, &enode, NULL);
629 1.153 atatat if (error == 0) {
630 1.153 atatat /* ah, found parent in overlay */
631 1.153 atatat elim = enode->sysctl_clen;
632 1.153 atatat enode = enode->sysctl_child;
633 1.153 atatat }
634 1.153 atatat else {
635 1.153 atatat error = 0;
636 1.153 atatat elim = 0;
637 1.153 atatat enode = NULL;
638 1.121 jdolecek }
639 1.153 atatat }
640 1.121 jdolecek
641 1.153 atatat for (ni = 0; ni < rnode->sysctl_clen; ni++) {
642 1.153 atatat onode = &rnode->sysctl_child[ni];
643 1.153 atatat if (enode && enode->sysctl_num == onode->sysctl_num) {
644 1.161 atatat if (SYSCTL_TYPE(enode->sysctl_flags) != CTLTYPE_NODE)
645 1.153 atatat onode = enode;
646 1.153 atatat if (--elim > 0)
647 1.153 atatat enode++;
648 1.153 atatat else
649 1.153 atatat enode = NULL;
650 1.153 atatat }
651 1.162 atatat error = sysctl_cvt_out(l, v, onode, oldp, left, &t);
652 1.153 atatat if (error)
653 1.1 cgd return (error);
654 1.162 atatat if (oldp != NULL)
655 1.162 atatat oldp = (char*)oldp + t;
656 1.162 atatat out += t;
657 1.162 atatat left -= MIN(left, t);
658 1.153 atatat }
659 1.153 atatat
660 1.153 atatat /*
661 1.153 atatat * overlay trees *MUST* be entirely consumed
662 1.153 atatat */
663 1.153 atatat KASSERT(enode == NULL);
664 1.153 atatat
665 1.153 atatat *oldlenp = out;
666 1.153 atatat
667 1.153 atatat return (error);
668 1.153 atatat }
669 1.153 atatat
670 1.153 atatat #ifdef SYSCTL_DEBUG_CREATE
671 1.153 atatat #undef sysctl_create
672 1.153 atatat #endif /* SYSCTL_DEBUG_CREATE */
673 1.153 atatat
674 1.153 atatat /*
675 1.153 atatat * sysctl_create -- Adds a node (the description of which is taken
676 1.153 atatat * from newp) to the tree, returning a copy of it in the space pointed
677 1.153 atatat * to by oldp. In the event that the requested slot is already taken
678 1.153 atatat * (either by name or by number), the offending node is returned
679 1.153 atatat * instead. Yes, this is complex, but we want to make sure everything
680 1.153 atatat * is proper.
681 1.153 atatat */
682 1.153 atatat int
683 1.153 atatat sysctl_create(SYSCTLFN_RWARGS)
684 1.153 atatat {
685 1.153 atatat struct sysctlnode nnode, *node, *pnode;
686 1.162 atatat int error, ni, at, nm, type, sz, flags, rw, anum, v;
687 1.153 atatat void *own;
688 1.153 atatat
689 1.153 atatat error = 0;
690 1.153 atatat own = NULL;
691 1.153 atatat anum = -1;
692 1.153 atatat
693 1.161 atatat if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
694 1.161 atatat printf("sysctl_create: rnode %p wrong version\n", rnode);
695 1.161 atatat return (EINVAL);
696 1.161 atatat }
697 1.161 atatat
698 1.153 atatat if (namelen != 1 || (name[namelen - 1] != CTL_CREATE
699 1.153 atatat #if NKSYMS > 0
700 1.153 atatat && name[namelen - 1] != CTL_CREATESYM
701 1.153 atatat #endif /* NKSYMS > 0 */
702 1.153 atatat ))
703 1.153 atatat return (EINVAL);
704 1.153 atatat
705 1.153 atatat /*
706 1.153 atatat * processes can only add nodes at securelevel 0, must be
707 1.153 atatat * root, and can't add nodes to a parent that's not writeable
708 1.153 atatat */
709 1.153 atatat if (l != NULL) {
710 1.156 atatat #ifndef SYSCTL_DISALLOW_CREATE
711 1.153 atatat if (securelevel > 0)
712 1.153 atatat return (EPERM);
713 1.153 atatat error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
714 1.153 atatat if (error)
715 1.153 atatat return (error);
716 1.161 atatat if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
717 1.153 atatat #endif /* SYSCTL_DISALLOW_CREATE */
718 1.1 cgd return (EPERM);
719 1.153 atatat }
720 1.153 atatat
721 1.153 atatat /*
722 1.153 atatat * nothing can add a node if:
723 1.153 atatat * we've finished initial set up and
724 1.153 atatat * the tree itself is not writeable or
725 1.153 atatat * the entire sysctl system is not writeable
726 1.153 atatat */
727 1.161 atatat if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) &&
728 1.161 atatat (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
729 1.161 atatat !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE)))
730 1.153 atatat return (EPERM);
731 1.120 jdolecek
732 1.153 atatat /*
733 1.153 atatat * it must be a "node", not a "int" or something
734 1.153 atatat */
735 1.153 atatat if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
736 1.153 atatat return (ENOTDIR);
737 1.153 atatat pnode = rnode;
738 1.120 jdolecek
739 1.162 atatat if (newp == NULL)
740 1.153 atatat return (EINVAL);
741 1.162 atatat error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
742 1.153 atatat if (error)
743 1.120 jdolecek return (error);
744 1.120 jdolecek
745 1.153 atatat /*
746 1.153 atatat * nodes passed in don't *have* parents
747 1.153 atatat */
748 1.153 atatat if (nnode.sysctl_parent != NULL)
749 1.153 atatat return (EINVAL);
750 1.145 dsl
751 1.153 atatat /*
752 1.153 atatat * if we are indeed adding it, it should be a "good" name and
753 1.153 atatat * number
754 1.153 atatat */
755 1.153 atatat nm = nnode.sysctl_num;
756 1.153 atatat #if NKSYMS > 0
757 1.153 atatat if (nm == CTL_CREATESYM)
758 1.153 atatat nm = CTL_CREATE;
759 1.153 atatat #endif /* NKSYMS > 0 */
760 1.153 atatat if (nm < 0 && nm != CTL_CREATE)
761 1.153 atatat return (EINVAL);
762 1.153 atatat sz = 0;
763 1.1 cgd
764 1.153 atatat /*
765 1.153 atatat * the name can't start with a digit
766 1.153 atatat */
767 1.153 atatat if (nnode.sysctl_name[sz] >= '0' &&
768 1.153 atatat nnode.sysctl_name[sz] <= '9')
769 1.153 atatat return (EINVAL);
770 1.1 cgd
771 1.153 atatat /*
772 1.153 atatat * the name must be only alphanumerics or - or _, longer than
773 1.153 atatat * 0 bytes and less that SYSCTL_NAMELEN
774 1.153 atatat */
775 1.153 atatat while (sz < SYSCTL_NAMELEN && nnode.sysctl_name[sz] != '\0') {
776 1.153 atatat if ((nnode.sysctl_name[sz] >= '0' &&
777 1.153 atatat nnode.sysctl_name[sz] <= '9') ||
778 1.153 atatat (nnode.sysctl_name[sz] >= 'A' &&
779 1.153 atatat nnode.sysctl_name[sz] <= 'Z') ||
780 1.153 atatat (nnode.sysctl_name[sz] >= 'a' &&
781 1.153 atatat nnode.sysctl_name[sz] <= 'z') ||
782 1.153 atatat nnode.sysctl_name[sz] == '-' ||
783 1.153 atatat nnode.sysctl_name[sz] == '_')
784 1.153 atatat sz++;
785 1.153 atatat else
786 1.153 atatat return (EINVAL);
787 1.98 simonb }
788 1.153 atatat if (sz == 0 || sz == SYSCTL_NAMELEN)
789 1.153 atatat return (EINVAL);
790 1.1 cgd
791 1.153 atatat /*
792 1.153 atatat * various checks revolve around size vs type, etc
793 1.153 atatat */
794 1.153 atatat type = SYSCTL_TYPE(nnode.sysctl_flags);
795 1.153 atatat flags = SYSCTL_FLAGS(nnode.sysctl_flags);
796 1.161 atatat rw = (flags & CTLFLAG_READWRITE) ? B_WRITE : B_READ;
797 1.153 atatat sz = nnode.sysctl_size;
798 1.131 thorpej
799 1.153 atatat /*
800 1.153 atatat * find out if there's a collision, and if so, let the caller
801 1.153 atatat * know what they collided with
802 1.153 atatat */
803 1.153 atatat node = pnode->sysctl_child;
804 1.161 atatat if (((flags & CTLFLAG_ANYNUMBER) && node) ||
805 1.161 atatat (node && node->sysctl_flags & CTLFLAG_ANYNUMBER))
806 1.153 atatat return (EINVAL);
807 1.153 atatat for (ni = at = 0; ni < pnode->sysctl_clen; ni++) {
808 1.153 atatat if (nm == node[ni].sysctl_num ||
809 1.153 atatat strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) {
810 1.162 atatat /*
811 1.162 atatat * ignore error here, since we
812 1.162 atatat * are already fixed on EEXIST
813 1.162 atatat */
814 1.162 atatat (void)sysctl_cvt_out(l, v, &node[ni], oldp,
815 1.162 atatat *oldlenp, oldlenp);
816 1.153 atatat return (EEXIST);
817 1.82 eeh }
818 1.153 atatat if (nm > node[ni].sysctl_num)
819 1.153 atatat at++;
820 1.1 cgd }
821 1.1 cgd
822 1.153 atatat /*
823 1.153 atatat * use sysctl_ver to add to the tree iff it hasn't changed
824 1.153 atatat */
825 1.153 atatat if (nnode.sysctl_ver != 0) {
826 1.153 atatat /*
827 1.153 atatat * a specified value must match either the parent
828 1.153 atatat * node's version or the root node's version
829 1.153 atatat */
830 1.153 atatat if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver &&
831 1.153 atatat nnode.sysctl_ver != rnode->sysctl_ver) {
832 1.153 atatat return (EINVAL);
833 1.153 atatat }
834 1.1 cgd }
835 1.1 cgd
836 1.153 atatat /*
837 1.153 atatat * only the kernel can assign functions to entries
838 1.153 atatat */
839 1.153 atatat if (l != NULL && nnode.sysctl_func != NULL)
840 1.153 atatat return (EPERM);
841 1.52 bouyer
842 1.153 atatat /*
843 1.153 atatat * only the kernel can create permanent entries, and only then
844 1.153 atatat * before the kernel is finished setting itself up
845 1.153 atatat */
846 1.153 atatat if (l != NULL && (flags & ~SYSCTL_USERFLAGS))
847 1.153 atatat return (EPERM);
848 1.161 atatat if ((flags & CTLFLAG_PERMANENT) &
849 1.161 atatat (sysctl_root.sysctl_flags & CTLFLAG_PERMANENT))
850 1.153 atatat return (EPERM);
851 1.161 atatat if ((flags & (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) ==
852 1.161 atatat (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE))
853 1.153 atatat return (EINVAL);
854 1.161 atatat if ((flags & CTLFLAG_IMMEDIATE) &&
855 1.153 atatat type != CTLTYPE_INT && type != CTLTYPE_QUAD)
856 1.129 enami return (EINVAL);
857 1.52 bouyer
858 1.153 atatat /*
859 1.153 atatat * check size, or set it if unset and we can figure it out.
860 1.153 atatat * kernel created nodes are allowed to have a function instead
861 1.153 atatat * of a size (or a data pointer).
862 1.153 atatat */
863 1.153 atatat switch (type) {
864 1.153 atatat case CTLTYPE_NODE:
865 1.153 atatat /*
866 1.153 atatat * only *i* can assert the size of a node
867 1.153 atatat */
868 1.161 atatat if (flags & CTLFLAG_ALIAS) {
869 1.153 atatat anum = nnode.sysctl_alias;
870 1.153 atatat if (anum < 0)
871 1.153 atatat return (EINVAL);
872 1.153 atatat nnode.sysctl_alias = 0;
873 1.52 bouyer }
874 1.153 atatat if (sz != 0 || nnode.sysctl_data != NULL)
875 1.153 atatat return (EINVAL);
876 1.153 atatat if (nnode.sysctl_csize != 0 ||
877 1.153 atatat nnode.sysctl_clen != 0 ||
878 1.153 atatat nnode.sysctl_child != 0)
879 1.129 enami return (EINVAL);
880 1.161 atatat if (flags & CTLFLAG_OWNDATA)
881 1.129 enami return (EINVAL);
882 1.153 atatat sz = sizeof(struct sysctlnode);
883 1.153 atatat break;
884 1.153 atatat case CTLTYPE_INT:
885 1.153 atatat /*
886 1.153 atatat * since an int is an int, if the size is not given or
887 1.153 atatat * is wrong, we can "int-uit" it.
888 1.153 atatat */
889 1.153 atatat if (sz != 0 && sz != sizeof(int))
890 1.129 enami return (EINVAL);
891 1.153 atatat sz = sizeof(int);
892 1.153 atatat break;
893 1.153 atatat case CTLTYPE_STRING:
894 1.52 bouyer /*
895 1.153 atatat * strings are a little more tricky
896 1.52 bouyer */
897 1.153 atatat if (sz == 0) {
898 1.153 atatat if (l == NULL) {
899 1.153 atatat if (nnode.sysctl_func == NULL) {
900 1.153 atatat if (nnode.sysctl_data == NULL)
901 1.153 atatat return (EINVAL);
902 1.153 atatat else
903 1.153 atatat sz = strlen(nnode.sysctl_data) +
904 1.153 atatat 1;
905 1.52 bouyer }
906 1.52 bouyer }
907 1.153 atatat else if (nnode.sysctl_data == NULL &&
908 1.161 atatat flags & CTLFLAG_OWNDATA) {
909 1.153 atatat return (EINVAL);
910 1.52 bouyer }
911 1.153 atatat else {
912 1.156 atatat char v[PAGE_SIZE], *e;
913 1.153 atatat size_t s;
914 1.153 atatat
915 1.153 atatat /*
916 1.156 atatat * we want a rough idea of what the
917 1.156 atatat * size is now
918 1.153 atatat */
919 1.156 atatat e = nnode.sysctl_data;
920 1.156 atatat do {
921 1.160 atatat error = copyinstr(e, &v[0], sizeof(v),
922 1.160 atatat &s);
923 1.156 atatat if (error) {
924 1.156 atatat if (error != ENAMETOOLONG)
925 1.156 atatat return (error);
926 1.156 atatat e += PAGE_SIZE;
927 1.156 atatat if ((e - 32 * PAGE_SIZE) >
928 1.156 atatat (char*)nnode.sysctl_data)
929 1.156 atatat return (ERANGE);
930 1.156 atatat }
931 1.156 atatat } while (error != 0);
932 1.156 atatat sz = s + (e - (char*)nnode.sysctl_data);
933 1.96 bouyer }
934 1.52 bouyer }
935 1.153 atatat break;
936 1.153 atatat case CTLTYPE_QUAD:
937 1.153 atatat if (sz != 0 && sz != sizeof(u_quad_t))
938 1.129 enami return (EINVAL);
939 1.153 atatat sz = sizeof(u_quad_t);
940 1.153 atatat break;
941 1.153 atatat case CTLTYPE_STRUCT:
942 1.153 atatat if (sz == 0) {
943 1.153 atatat if (l != NULL || nnode.sysctl_func == NULL)
944 1.153 atatat return (EINVAL);
945 1.161 atatat if (flags & CTLFLAG_OWNDATA)
946 1.153 atatat return (EINVAL);
947 1.153 atatat }
948 1.153 atatat break;
949 1.115 manu default:
950 1.115 manu return (EINVAL);
951 1.52 bouyer }
952 1.52 bouyer
953 1.153 atatat /*
954 1.153 atatat * at this point, if sz is zero, we *must* have a
955 1.153 atatat * function to go with it and we can't own it.
956 1.153 atatat */
957 1.104 christos
958 1.153 atatat /*
959 1.153 atatat * l ptr own
960 1.153 atatat * 0 0 0 -> EINVAL (if no func)
961 1.153 atatat * 0 0 1 -> own
962 1.153 atatat * 0 1 0 -> kptr
963 1.153 atatat * 0 1 1 -> kptr
964 1.153 atatat * 1 0 0 -> EINVAL
965 1.153 atatat * 1 0 1 -> own
966 1.156 atatat * 1 1 0 -> kptr, no own (fault on lookup)
967 1.153 atatat * 1 1 1 -> uptr, own
968 1.153 atatat */
969 1.153 atatat if (type != CTLTYPE_NODE) {
970 1.153 atatat if (sz != 0) {
971 1.161 atatat if (flags & CTLFLAG_OWNDATA) {
972 1.153 atatat own = malloc(sz, M_SYSCTLDATA,
973 1.153 atatat M_WAITOK|M_CANFAIL);
974 1.153 atatat if (nnode.sysctl_data == NULL)
975 1.153 atatat memset(own, 0, sz);
976 1.153 atatat else {
977 1.153 atatat error = sysctl_copyin(l,
978 1.153 atatat nnode.sysctl_data, own, sz);
979 1.153 atatat if (error != 0) {
980 1.153 atatat FREE(own, M_SYSCTLDATA);
981 1.153 atatat return (error);
982 1.153 atatat }
983 1.153 atatat }
984 1.153 atatat }
985 1.153 atatat else if ((nnode.sysctl_data != NULL) &&
986 1.161 atatat !(flags & CTLFLAG_IMMEDIATE)) {
987 1.153 atatat #if NKSYMS > 0
988 1.153 atatat if (name[namelen - 1] == CTL_CREATESYM) {
989 1.153 atatat char symname[128]; /* XXX enough? */
990 1.153 atatat u_long symaddr;
991 1.153 atatat size_t symlen;
992 1.153 atatat
993 1.153 atatat error = sysctl_copyinstr(l,
994 1.153 atatat nnode.sysctl_data, symname,
995 1.153 atatat sizeof(symname), &symlen);
996 1.153 atatat if (error)
997 1.153 atatat return (error);
998 1.153 atatat error = ksyms_getval_from_kernel(NULL,
999 1.153 atatat symname, &symaddr, KSYMS_EXTERN);
1000 1.153 atatat if (error)
1001 1.153 atatat return (error); /* EINVAL? */
1002 1.153 atatat nnode.sysctl_data = (void*)symaddr;
1003 1.153 atatat }
1004 1.153 atatat #endif /* NKSYMS > 0 */
1005 1.156 atatat /*
1006 1.156 atatat * Ideally, we'd like to verify here
1007 1.156 atatat * that this address is acceptable,
1008 1.156 atatat * but...
1009 1.156 atatat *
1010 1.156 atatat * - it might be valid now, only to
1011 1.156 atatat * become invalid later
1012 1.156 atatat *
1013 1.156 atatat * - it might be invalid only for the
1014 1.156 atatat * moment and valid later
1015 1.156 atatat *
1016 1.156 atatat * - or something else.
1017 1.156 atatat *
1018 1.156 atatat * Since we can't get a good answer,
1019 1.156 atatat * we'll just accept the address as
1020 1.156 atatat * given, and fault on individual
1021 1.156 atatat * lookups.
1022 1.156 atatat */
1023 1.153 atatat }
1024 1.153 atatat }
1025 1.153 atatat else if (nnode.sysctl_func == NULL)
1026 1.153 atatat return (EINVAL);
1027 1.153 atatat }
1028 1.104 christos
1029 1.153 atatat /*
1030 1.153 atatat * a process can't assign a function to a node, and the kernel
1031 1.153 atatat * can't create a node that has no function or data.
1032 1.153 atatat * (XXX somewhat redundant check)
1033 1.153 atatat */
1034 1.153 atatat if (l != NULL || nnode.sysctl_func == NULL) {
1035 1.153 atatat if (type != CTLTYPE_NODE &&
1036 1.153 atatat nnode.sysctl_data == NULL &&
1037 1.161 atatat !(flags & CTLFLAG_IMMEDIATE) &&
1038 1.153 atatat own == NULL)
1039 1.153 atatat return (EINVAL);
1040 1.153 atatat }
1041 1.104 christos
1042 1.153 atatat #ifdef SYSCTL_DISALLOW_KWRITE
1043 1.153 atatat /*
1044 1.153 atatat * a process can't create a writable node unless it refers to
1045 1.153 atatat * new data.
1046 1.153 atatat */
1047 1.153 atatat if (l != NULL && own == NULL && type != CTLTYPE_NODE &&
1048 1.161 atatat (flags & CTLFLAG_READWRITE) != CTLFLAG_READONLY &&
1049 1.161 atatat !(flags & CTLFLAG_IMMEDIATE))
1050 1.153 atatat return (EPERM);
1051 1.153 atatat #endif /* SYSCTL_DISALLOW_KWRITE */
1052 1.104 christos
1053 1.153 atatat /*
1054 1.153 atatat * make sure there's somewhere to put the new stuff.
1055 1.153 atatat */
1056 1.153 atatat if (pnode->sysctl_child == NULL) {
1057 1.161 atatat if (flags & CTLFLAG_ANYNUMBER)
1058 1.153 atatat error = sysctl_alloc(pnode, 1);
1059 1.153 atatat else
1060 1.153 atatat error = sysctl_alloc(pnode, 0);
1061 1.153 atatat if (error)
1062 1.153 atatat return (error);
1063 1.153 atatat }
1064 1.153 atatat node = pnode->sysctl_child;
1065 1.105 jdolecek
1066 1.153 atatat /*
1067 1.153 atatat * no collisions, so pick a good dynamic number if we need to.
1068 1.153 atatat */
1069 1.153 atatat if (nm == CTL_CREATE) {
1070 1.153 atatat nm = ++sysctl_root.sysctl_num;
1071 1.153 atatat for (ni = 0; ni < pnode->sysctl_clen; ni++) {
1072 1.153 atatat if (nm == node[ni].sysctl_num) {
1073 1.153 atatat nm++;
1074 1.153 atatat ni = -1;
1075 1.153 atatat }
1076 1.153 atatat else if (nm > node[ni].sysctl_num)
1077 1.153 atatat at = ni + 1;
1078 1.153 atatat }
1079 1.104 christos }
1080 1.104 christos
1081 1.153 atatat /*
1082 1.153 atatat * oops...ran out of space
1083 1.153 atatat */
1084 1.153 atatat if (pnode->sysctl_clen == pnode->sysctl_csize) {
1085 1.153 atatat error = sysctl_realloc(pnode);
1086 1.153 atatat if (error)
1087 1.153 atatat return (error);
1088 1.153 atatat node = pnode->sysctl_child;
1089 1.55 is }
1090 1.55 is
1091 1.153 atatat /*
1092 1.153 atatat * insert new node data
1093 1.153 atatat */
1094 1.153 atatat if (at < pnode->sysctl_clen) {
1095 1.153 atatat int t;
1096 1.153 atatat
1097 1.153 atatat /*
1098 1.153 atatat * move the nodes that should come after the new one
1099 1.153 atatat */
1100 1.153 atatat memmove(&node[at + 1], &node[at],
1101 1.153 atatat (pnode->sysctl_clen - at) * sizeof(struct sysctlnode));
1102 1.153 atatat memset(&node[at], 0, sizeof(struct sysctlnode));
1103 1.153 atatat node[at].sysctl_parent = pnode;
1104 1.153 atatat /*
1105 1.153 atatat * and...reparent any children of any moved nodes
1106 1.153 atatat */
1107 1.153 atatat for (ni = at; ni <= pnode->sysctl_clen; ni++)
1108 1.153 atatat if (SYSCTL_TYPE(node[ni].sysctl_flags) == CTLTYPE_NODE)
1109 1.153 atatat for (t = 0; t < node[ni].sysctl_clen; t++)
1110 1.153 atatat node[ni].sysctl_child[t].sysctl_parent =
1111 1.153 atatat &node[ni];
1112 1.153 atatat }
1113 1.153 atatat node = &node[at];
1114 1.153 atatat pnode->sysctl_clen++;
1115 1.153 atatat
1116 1.153 atatat strlcpy(node->sysctl_name, nnode.sysctl_name,
1117 1.153 atatat sizeof(node->sysctl_name));
1118 1.153 atatat node->sysctl_num = nm;
1119 1.153 atatat node->sysctl_size = sz;
1120 1.161 atatat node->sysctl_flags = SYSCTL_VERSION|type|flags; /* XXX other trees */
1121 1.153 atatat node->sysctl_csize = 0;
1122 1.153 atatat node->sysctl_clen = 0;
1123 1.153 atatat if (own) {
1124 1.153 atatat node->sysctl_data = own;
1125 1.161 atatat node->sysctl_flags |= CTLFLAG_OWNDATA;
1126 1.153 atatat }
1127 1.161 atatat else if (flags & CTLFLAG_ALIAS) {
1128 1.153 atatat node->sysctl_alias = anum;
1129 1.153 atatat }
1130 1.161 atatat else if (flags & CTLFLAG_IMMEDIATE) {
1131 1.153 atatat switch (type) {
1132 1.153 atatat case CTLTYPE_INT:
1133 1.153 atatat node->sysctl_idata = nnode.sysctl_idata;
1134 1.153 atatat break;
1135 1.153 atatat case CTLTYPE_QUAD:
1136 1.153 atatat node->sysctl_qdata = nnode.sysctl_qdata;
1137 1.153 atatat break;
1138 1.153 atatat }
1139 1.55 is }
1140 1.153 atatat else {
1141 1.153 atatat node->sysctl_data = nnode.sysctl_data;
1142 1.161 atatat node->sysctl_flags &= ~CTLFLAG_OWNDATA;
1143 1.153 atatat }
1144 1.153 atatat node->sysctl_func = nnode.sysctl_func;
1145 1.153 atatat node->sysctl_child = NULL;
1146 1.153 atatat /* node->sysctl_parent should already be done */
1147 1.55 is
1148 1.153 atatat /*
1149 1.153 atatat * update "version" on path to "root"
1150 1.153 atatat */
1151 1.153 atatat for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1152 1.153 atatat ;
1153 1.153 atatat pnode = node;
1154 1.153 atatat for (nm = rnode->sysctl_ver + 1; pnode != NULL;
1155 1.153 atatat pnode = pnode->sysctl_parent)
1156 1.153 atatat pnode->sysctl_ver = nm;
1157 1.153 atatat
1158 1.162 atatat error = sysctl_cvt_out(l, v, node, oldp, *oldlenp, oldlenp);
1159 1.55 is
1160 1.1 cgd return (error);
1161 1.1 cgd }
1162 1.1 cgd
1163 1.1 cgd /*
1164 1.153 atatat * ********************************************************************
1165 1.153 atatat * A wrapper around sysctl_create() that prints the thing we're trying
1166 1.153 atatat * to add.
1167 1.153 atatat * ********************************************************************
1168 1.1 cgd */
1169 1.153 atatat #ifdef SYSCTL_DEBUG_CREATE
1170 1.153 atatat int _sysctl_create(SYSCTLFN_RWPROTO);
1171 1.13 christos int
1172 1.153 atatat _sysctl_create(SYSCTLFN_RWARGS)
1173 1.1 cgd {
1174 1.153 atatat const struct sysctlnode *node;
1175 1.153 atatat int k, rc, ni, nl = namelen + (name - oname);
1176 1.153 atatat
1177 1.153 atatat node = newp;
1178 1.153 atatat
1179 1.153 atatat printf("namelen %d (", nl);
1180 1.153 atatat for (ni = 0; ni < nl - 1; ni++)
1181 1.153 atatat printf(" %d", oname[ni]);
1182 1.153 atatat printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
1183 1.153 atatat k = node->sysctl_num,
1184 1.153 atatat node->sysctl_name,
1185 1.153 atatat node->sysctl_flags,
1186 1.153 atatat SYSCTL_FLAGS(node->sysctl_flags),
1187 1.153 atatat SYSCTL_TYPE(node->sysctl_flags),
1188 1.153 atatat node->sysctl_size);
1189 1.1 cgd
1190 1.153 atatat node = rnode;
1191 1.153 atatat rc = sysctl_create(SYSCTLFN_CALL(rnode));
1192 1.55 is
1193 1.153 atatat printf("sysctl_create(");
1194 1.153 atatat for (ni = 0; ni < nl - 1; ni++)
1195 1.153 atatat printf(" %d", oname[ni]);
1196 1.153 atatat printf(" %d ) returned %d\n", k, rc);
1197 1.55 is
1198 1.153 atatat return (rc);
1199 1.1 cgd }
1200 1.153 atatat #define sysctl_create _sysctl_create
1201 1.153 atatat #endif /* SYSCTL_DEBUG_CREATE */
1202 1.1 cgd
1203 1.1 cgd /*
1204 1.153 atatat * sysctl_destroy -- Removes a node (as described by newp) from the
1205 1.153 atatat * given tree, returning (if successful) a copy of the dead node in
1206 1.153 atatat * oldp. Since we're removing stuff, there's not much to check.
1207 1.52 bouyer */
1208 1.52 bouyer int
1209 1.153 atatat sysctl_destroy(SYSCTLFN_RWARGS)
1210 1.52 bouyer {
1211 1.153 atatat struct sysctlnode *node, *pnode, onode, nnode;
1212 1.162 atatat int ni, error, v;
1213 1.52 bouyer
1214 1.161 atatat if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1215 1.161 atatat printf("sysctl_destroy: rnode %p wrong version\n", rnode);
1216 1.161 atatat return (EINVAL);
1217 1.161 atatat }
1218 1.161 atatat
1219 1.153 atatat error = 0;
1220 1.55 is
1221 1.153 atatat if (namelen != 1 || name[namelen - 1] != CTL_DESTROY)
1222 1.153 atatat return (EINVAL);
1223 1.52 bouyer
1224 1.153 atatat /*
1225 1.153 atatat * processes can only destroy nodes at securelevel 0, must be
1226 1.153 atatat * root, and can't remove nodes from a parent that's not
1227 1.153 atatat * writeable
1228 1.153 atatat */
1229 1.153 atatat if (l != NULL) {
1230 1.156 atatat #ifndef SYSCTL_DISALLOW_CREATE
1231 1.153 atatat if (securelevel > 0)
1232 1.153 atatat return (EPERM);
1233 1.153 atatat error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
1234 1.153 atatat if (error)
1235 1.153 atatat return (error);
1236 1.161 atatat if (!(rnode->sysctl_flags & CTLFLAG_READWRITE))
1237 1.156 atatat #endif /* SYSCTL_DISALLOW_CREATE */
1238 1.153 atatat return (EPERM);
1239 1.153 atatat }
1240 1.52 bouyer
1241 1.153 atatat /*
1242 1.153 atatat * nothing can remove a node if:
1243 1.153 atatat * the node is permanent (checked later) or
1244 1.153 atatat * the tree itself is not writeable or
1245 1.153 atatat * the entire sysctl system is not writeable
1246 1.153 atatat */
1247 1.161 atatat if (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) ||
1248 1.161 atatat !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))
1249 1.52 bouyer return (EPERM);
1250 1.55 is
1251 1.161 atatat if (newp == NULL)
1252 1.153 atatat return (EINVAL);
1253 1.162 atatat error = sysctl_cvt_in(l, &v, newp, newlen, &nnode);
1254 1.153 atatat if (error)
1255 1.153 atatat return (error);
1256 1.153 atatat memset(&onode, 0, sizeof(struct sysctlnode));
1257 1.55 is
1258 1.153 atatat node = rnode->sysctl_child;
1259 1.153 atatat for (ni = 0; ni < rnode->sysctl_clen; ni++) {
1260 1.153 atatat if (nnode.sysctl_num == node[ni].sysctl_num) {
1261 1.153 atatat /*
1262 1.153 atatat * if name specified, must match
1263 1.153 atatat */
1264 1.153 atatat if (nnode.sysctl_name[0] != '\0' &&
1265 1.153 atatat strcmp(nnode.sysctl_name, node[ni].sysctl_name))
1266 1.153 atatat continue;
1267 1.153 atatat /*
1268 1.153 atatat * if version specified, must match
1269 1.153 atatat */
1270 1.153 atatat if (nnode.sysctl_ver != 0 &&
1271 1.153 atatat nnode.sysctl_ver != node[ni].sysctl_ver)
1272 1.153 atatat continue;
1273 1.153 atatat /*
1274 1.153 atatat * this must be the one
1275 1.153 atatat */
1276 1.153 atatat break;
1277 1.153 atatat }
1278 1.153 atatat }
1279 1.153 atatat if (ni == rnode->sysctl_clen)
1280 1.153 atatat return (ENOENT);
1281 1.153 atatat node = &node[ni];
1282 1.153 atatat pnode = node->sysctl_parent;
1283 1.52 bouyer
1284 1.153 atatat /*
1285 1.153 atatat * if the kernel says permanent, it is, so there. nyah.
1286 1.153 atatat */
1287 1.161 atatat if (SYSCTL_FLAGS(node->sysctl_flags) & CTLFLAG_PERMANENT)
1288 1.153 atatat return (EPERM);
1289 1.1 cgd
1290 1.153 atatat /*
1291 1.153 atatat * can't delete non-empty nodes
1292 1.153 atatat */
1293 1.153 atatat if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
1294 1.153 atatat node->sysctl_clen != 0)
1295 1.153 atatat return (ENOTEMPTY);
1296 1.55 is
1297 1.153 atatat /*
1298 1.153 atatat * if the node "owns" data, release it now
1299 1.153 atatat */
1300 1.161 atatat if (node->sysctl_flags & CTLFLAG_OWNDATA) {
1301 1.153 atatat if (node->sysctl_data != NULL)
1302 1.153 atatat FREE(node->sysctl_data, M_SYSCTLDATA);
1303 1.153 atatat node->sysctl_data = NULL;
1304 1.153 atatat }
1305 1.166 atatat if (node->sysctl_flags & CTLFLAG_OWNDESC) {
1306 1.166 atatat if (node->sysctl_desc != NULL)
1307 1.166 atatat FREE(node->sysctl_desc, M_SYSCTLDATA);
1308 1.166 atatat node->sysctl_desc = NULL;
1309 1.166 atatat }
1310 1.55 is
1311 1.153 atatat /*
1312 1.153 atatat * if the node to be removed is not the last one on the list,
1313 1.153 atatat * move the remaining nodes up, and reparent any grandchildren
1314 1.153 atatat */
1315 1.153 atatat onode = *node;
1316 1.153 atatat if (ni < pnode->sysctl_clen - 1) {
1317 1.153 atatat int t;
1318 1.153 atatat
1319 1.153 atatat memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1],
1320 1.153 atatat (pnode->sysctl_clen - ni - 1) *
1321 1.153 atatat sizeof(struct sysctlnode));
1322 1.153 atatat for (; ni < pnode->sysctl_clen - 1; ni++)
1323 1.153 atatat if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) ==
1324 1.153 atatat CTLTYPE_NODE)
1325 1.161 atatat for (t = 0;
1326 1.161 atatat t < pnode->sysctl_child[ni].sysctl_clen;
1327 1.153 atatat t++)
1328 1.153 atatat pnode->sysctl_child[ni].sysctl_child[t].
1329 1.153 atatat sysctl_parent =
1330 1.153 atatat &pnode->sysctl_child[ni];
1331 1.153 atatat ni = pnode->sysctl_clen - 1;
1332 1.153 atatat node = &pnode->sysctl_child[ni];
1333 1.1 cgd }
1334 1.1 cgd
1335 1.153 atatat /*
1336 1.153 atatat * reset the space we just vacated
1337 1.153 atatat */
1338 1.153 atatat memset(node, 0, sizeof(struct sysctlnode));
1339 1.153 atatat node->sysctl_parent = pnode;
1340 1.153 atatat pnode->sysctl_clen--;
1341 1.1 cgd
1342 1.153 atatat /*
1343 1.153 atatat * if this parent just lost its last child, nuke the creche
1344 1.153 atatat */
1345 1.153 atatat if (pnode->sysctl_clen == 0) {
1346 1.153 atatat FREE(pnode->sysctl_child, M_SYSCTLNODE);
1347 1.153 atatat pnode->sysctl_csize = 0;
1348 1.153 atatat pnode->sysctl_child = NULL;
1349 1.153 atatat }
1350 1.55 is
1351 1.153 atatat /*
1352 1.153 atatat * update "version" on path to "root"
1353 1.153 atatat */
1354 1.153 atatat for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent)
1355 1.153 atatat ;
1356 1.153 atatat for (ni = rnode->sysctl_ver + 1; pnode != NULL;
1357 1.153 atatat pnode = pnode->sysctl_parent)
1358 1.153 atatat pnode->sysctl_ver = ni;
1359 1.153 atatat
1360 1.162 atatat error = sysctl_cvt_out(l, v, &onode, oldp, *oldlenp, oldlenp);
1361 1.55 is
1362 1.1 cgd return (error);
1363 1.1 cgd }
1364 1.1 cgd
1365 1.1 cgd /*
1366 1.153 atatat * sysctl_lookup -- Handles copyin/copyout of new and old values.
1367 1.153 atatat * Partial reads are globally allowed. Only root can write to things
1368 1.153 atatat * unless the node says otherwise.
1369 1.1 cgd */
1370 1.13 christos int
1371 1.153 atatat sysctl_lookup(SYSCTLFN_RWARGS)
1372 1.1 cgd {
1373 1.153 atatat int error, rw;
1374 1.153 atatat size_t sz, len;
1375 1.153 atatat void *d;
1376 1.153 atatat
1377 1.161 atatat if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1378 1.161 atatat printf("sysctl_lookup: rnode %p wrong version\n", rnode);
1379 1.161 atatat return (EINVAL);
1380 1.161 atatat }
1381 1.161 atatat
1382 1.153 atatat error = 0;
1383 1.153 atatat
1384 1.153 atatat /*
1385 1.153 atatat * you can't "look up" a node. you can "query" it, but you
1386 1.153 atatat * can't "look it up".
1387 1.153 atatat */
1388 1.153 atatat if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0)
1389 1.153 atatat return (EINVAL);
1390 1.1 cgd
1391 1.153 atatat /*
1392 1.153 atatat * some nodes are private, so only root can look into them.
1393 1.153 atatat */
1394 1.161 atatat if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) &&
1395 1.157 atatat (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
1396 1.153 atatat return (error);
1397 1.55 is
1398 1.153 atatat /*
1399 1.153 atatat * if a node wants to be writable according to different rules
1400 1.153 atatat * other than "only root can write to stuff unless a flag is
1401 1.153 atatat * set", then it needs its own function which should have been
1402 1.153 atatat * called and not us.
1403 1.153 atatat */
1404 1.153 atatat if (l != NULL && newp != NULL &&
1405 1.161 atatat !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) &&
1406 1.153 atatat (error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
1407 1.153 atatat return (error);
1408 1.1 cgd
1409 1.153 atatat /*
1410 1.153 atatat * is this node supposedly writable?
1411 1.153 atatat */
1412 1.153 atatat rw = 0;
1413 1.161 atatat switch (rnode->sysctl_flags & CTLFLAG_READWRITE) {
1414 1.161 atatat case CTLFLAG_READONLY1:
1415 1.153 atatat rw = (securelevel < 1) ? 1 : 0;
1416 1.153 atatat break;
1417 1.161 atatat case CTLFLAG_READONLY2:
1418 1.153 atatat rw = (securelevel < 2) ? 1 : 0;
1419 1.153 atatat break;
1420 1.161 atatat case CTLFLAG_READWRITE:
1421 1.153 atatat rw = 1;
1422 1.153 atatat break;
1423 1.153 atatat }
1424 1.1 cgd
1425 1.153 atatat /*
1426 1.153 atatat * it appears not to be writable at this time, so if someone
1427 1.153 atatat * tried to write to it, we must tell them to go away
1428 1.153 atatat */
1429 1.153 atatat if (!rw && newp != NULL)
1430 1.1 cgd return (EPERM);
1431 1.55 is
1432 1.153 atatat /*
1433 1.153 atatat * step one, copy out the stuff we have presently
1434 1.153 atatat */
1435 1.161 atatat if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) {
1436 1.153 atatat switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1437 1.153 atatat case CTLTYPE_INT:
1438 1.153 atatat d = &rnode->sysctl_idata;
1439 1.153 atatat break;
1440 1.153 atatat case CTLTYPE_QUAD:
1441 1.153 atatat d = &rnode->sysctl_qdata;
1442 1.153 atatat break;
1443 1.153 atatat default:
1444 1.153 atatat return (EINVAL);
1445 1.153 atatat }
1446 1.153 atatat }
1447 1.153 atatat else
1448 1.153 atatat d = rnode->sysctl_data;
1449 1.153 atatat if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING)
1450 1.157 atatat sz = strlen(d) + 1; /* XXX@@@ possible fault here */
1451 1.153 atatat else
1452 1.153 atatat sz = rnode->sysctl_size;
1453 1.153 atatat if (oldp != NULL)
1454 1.153 atatat error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp));
1455 1.153 atatat if (error)
1456 1.153 atatat return (error);
1457 1.153 atatat *oldlenp = sz;
1458 1.153 atatat
1459 1.153 atatat /*
1460 1.153 atatat * are we done?
1461 1.153 atatat */
1462 1.153 atatat if (newp == NULL || newlen == 0)
1463 1.153 atatat return (0);
1464 1.153 atatat
1465 1.153 atatat /*
1466 1.153 atatat * hmm...not done. must now "copy in" new value. re-adjust
1467 1.153 atatat * sz to maximum value (strings are "weird").
1468 1.153 atatat */
1469 1.153 atatat sz = rnode->sysctl_size;
1470 1.153 atatat switch (SYSCTL_TYPE(rnode->sysctl_flags)) {
1471 1.153 atatat case CTLTYPE_INT:
1472 1.153 atatat case CTLTYPE_QUAD:
1473 1.153 atatat case CTLTYPE_STRUCT:
1474 1.153 atatat /*
1475 1.153 atatat * these data must be *exactly* the same size coming
1476 1.153 atatat * in.
1477 1.153 atatat */
1478 1.153 atatat if (newlen != sz)
1479 1.153 atatat return (EINVAL);
1480 1.153 atatat error = sysctl_copyin(l, newp, d, sz);
1481 1.153 atatat break;
1482 1.153 atatat case CTLTYPE_STRING: {
1483 1.153 atatat /*
1484 1.153 atatat * strings, on the other hand, can be shorter, and we
1485 1.153 atatat * let userland be sloppy about the trailing nul.
1486 1.153 atatat */
1487 1.153 atatat char *newbuf;
1488 1.85 simonb
1489 1.153 atatat /*
1490 1.153 atatat * too much new string?
1491 1.153 atatat */
1492 1.153 atatat if (newlen > sz)
1493 1.153 atatat return (EINVAL);
1494 1.85 simonb
1495 1.153 atatat /*
1496 1.153 atatat * temporary copy of new inbound string
1497 1.153 atatat */
1498 1.153 atatat len = MIN(sz, newlen);
1499 1.153 atatat newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
1500 1.153 atatat if (newbuf == NULL)
1501 1.153 atatat return (ENOMEM);
1502 1.153 atatat error = sysctl_copyin(l, newp, newbuf, len);
1503 1.153 atatat if (error) {
1504 1.153 atatat FREE(newbuf, M_SYSCTLDATA);
1505 1.153 atatat return (error);
1506 1.153 atatat }
1507 1.85 simonb
1508 1.153 atatat /*
1509 1.153 atatat * did they null terminate it, or do we have space
1510 1.153 atatat * left to do it ourselves?
1511 1.153 atatat */
1512 1.153 atatat if (newbuf[len - 1] != '\0' && len == sz) {
1513 1.153 atatat FREE(newbuf, M_SYSCTLDATA);
1514 1.153 atatat return (EINVAL);
1515 1.153 atatat }
1516 1.85 simonb
1517 1.153 atatat /*
1518 1.153 atatat * looks good, so pop it into place and zero the rest.
1519 1.153 atatat */
1520 1.153 atatat if (len > 0)
1521 1.153 atatat memcpy(rnode->sysctl_data, newbuf, len);
1522 1.153 atatat if (sz != len)
1523 1.153 atatat memset((char*)rnode->sysctl_data + len, 0, sz - len);
1524 1.153 atatat FREE(newbuf, M_SYSCTLDATA);
1525 1.153 atatat break;
1526 1.153 atatat }
1527 1.153 atatat default:
1528 1.153 atatat return (EINVAL);
1529 1.153 atatat }
1530 1.55 is
1531 1.1 cgd return (error);
1532 1.1 cgd }
1533 1.1 cgd
1534 1.1 cgd /*
1535 1.153 atatat * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
1536 1.153 atatat * purport to handle it. This interface isn't fully fleshed out yet,
1537 1.153 atatat * unfortunately.
1538 1.1 cgd */
1539 1.69 simonb static int
1540 1.153 atatat sysctl_mmap(SYSCTLFN_RWARGS)
1541 1.1 cgd {
1542 1.153 atatat struct sysctlnode nnode, *node;
1543 1.111 thorpej int error;
1544 1.1 cgd
1545 1.161 atatat if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1546 1.161 atatat printf("sysctl_mmap: rnode %p wrong version\n", rnode);
1547 1.161 atatat return (EINVAL);
1548 1.161 atatat }
1549 1.161 atatat
1550 1.153 atatat /*
1551 1.153 atatat * let's just pretend that didn't happen, m'kay?
1552 1.153 atatat */
1553 1.153 atatat if (l == NULL)
1554 1.153 atatat return (EPERM);
1555 1.153 atatat
1556 1.153 atatat /*
1557 1.153 atatat * is this a sysctlnode description of an mmap request?
1558 1.153 atatat */
1559 1.153 atatat if (newp == NULL || newlen != sizeof(struct sysctlnode))
1560 1.153 atatat return (EINVAL);
1561 1.161 atatat error = sysctl_copyin(l, newp, &nnode, sizeof(nnode));
1562 1.153 atatat if (error)
1563 1.153 atatat return (error);
1564 1.1 cgd
1565 1.1 cgd /*
1566 1.153 atatat * does the node they asked for exist?
1567 1.1 cgd */
1568 1.153 atatat if (namelen != 1)
1569 1.153 atatat return (EOPNOTSUPP);
1570 1.153 atatat node = rnode;
1571 1.153 atatat error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL);
1572 1.13 christos if (error)
1573 1.1 cgd return (error);
1574 1.1 cgd
1575 1.1 cgd /*
1576 1.153 atatat * does this node that we have found purport to handle mmap?
1577 1.153 atatat */
1578 1.153 atatat if (node->sysctl_func == NULL ||
1579 1.161 atatat !(node->sysctl_flags & CTLFLAG_MMAP))
1580 1.153 atatat return (EOPNOTSUPP);
1581 1.153 atatat
1582 1.153 atatat /*
1583 1.153 atatat * well...okay, they asked for it.
1584 1.1 cgd */
1585 1.153 atatat return ((*node->sysctl_func)(SYSCTLFN_CALL(node)));
1586 1.1 cgd }
1587 1.1 cgd
1588 1.165 atatat int
1589 1.165 atatat sysctl_describe(SYSCTLFN_ARGS)
1590 1.165 atatat {
1591 1.166 atatat struct sysctldesc *d;
1592 1.166 atatat char buf[1024];
1593 1.166 atatat size_t sz, left, tot;
1594 1.166 atatat int i, error, v = -1;
1595 1.166 atatat struct sysctlnode *node;
1596 1.166 atatat struct sysctlnode dnode;
1597 1.165 atatat
1598 1.165 atatat if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
1599 1.165 atatat printf("sysctl_query: rnode %p wrong version\n", rnode);
1600 1.165 atatat return (EINVAL);
1601 1.165 atatat }
1602 1.165 atatat
1603 1.165 atatat if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE)
1604 1.165 atatat return (ENOTDIR);
1605 1.165 atatat if (namelen != 1 || name[0] != CTL_DESCRIBE)
1606 1.165 atatat return (EINVAL);
1607 1.165 atatat
1608 1.165 atatat /*
1609 1.166 atatat * get ready...
1610 1.166 atatat */
1611 1.166 atatat error = 0;
1612 1.166 atatat d = (void*)&buf[0];
1613 1.166 atatat tot = 0;
1614 1.166 atatat node = rnode->sysctl_child;
1615 1.166 atatat left = *oldlenp;
1616 1.166 atatat
1617 1.166 atatat /*
1618 1.166 atatat * no request -> all descriptions at this level
1619 1.166 atatat * request with desc unset -> just this node
1620 1.166 atatat * request with desc set -> set descr for this node
1621 1.165 atatat */
1622 1.166 atatat if (newp != NULL) {
1623 1.166 atatat error = sysctl_cvt_in(l, &v, newp, newlen, &dnode);
1624 1.166 atatat if (error)
1625 1.166 atatat return (error);
1626 1.166 atatat if (dnode.sysctl_desc != NULL) {
1627 1.166 atatat /*
1628 1.166 atatat * processes cannot set descriptions above
1629 1.166 atatat * securelevel 0. and must be root. blah
1630 1.166 atatat * blah blah. a couple more checks are made
1631 1.166 atatat * once we find the node we want.
1632 1.166 atatat */
1633 1.166 atatat if (l != NULL) {
1634 1.166 atatat #ifndef SYSCTL_DISALLOW_CREATE
1635 1.166 atatat if (securelevel > 0)
1636 1.166 atatat return (EPERM);
1637 1.166 atatat error = suser(l->l_proc->p_ucred,
1638 1.166 atatat &l->l_proc->p_acflag);
1639 1.166 atatat if (error)
1640 1.166 atatat return (error);
1641 1.166 atatat #else /* SYSCTL_DISALLOW_CREATE */
1642 1.166 atatat return (EPERM);
1643 1.166 atatat #endif /* SYSCTL_DISALLOW_CREATE */
1644 1.166 atatat }
1645 1.166 atatat
1646 1.166 atatat /*
1647 1.166 atatat * find node and try to set the description on it
1648 1.166 atatat */
1649 1.166 atatat for (i = 0; i < rnode->sysctl_clen; i++)
1650 1.166 atatat if (node[i].sysctl_num == dnode.sysctl_num)
1651 1.166 atatat break;
1652 1.166 atatat if (i == rnode->sysctl_clen)
1653 1.166 atatat return (ENOENT);
1654 1.166 atatat node = &node[i];
1655 1.166 atatat
1656 1.166 atatat /*
1657 1.166 atatat * did the caller specify a node version?
1658 1.166 atatat */
1659 1.166 atatat if (dnode.sysctl_ver != 0 &&
1660 1.166 atatat dnode.sysctl_ver != node->sysctl_ver)
1661 1.166 atatat return (EINVAL);
1662 1.166 atatat
1663 1.166 atatat /*
1664 1.166 atatat * okay...some rules:
1665 1.166 atatat * (1) no one can set a description on a
1666 1.166 atatat * permanent node (it must be set when
1667 1.166 atatat * using createv)
1668 1.166 atatat * (2) processes cannot *change* a description
1669 1.166 atatat * (3) processes *can*, however, set a
1670 1.166 atatat * description on a read-only node so that
1671 1.166 atatat * one can be created and then described
1672 1.166 atatat * in two steps
1673 1.166 atatat * anything else come to mind?
1674 1.166 atatat */
1675 1.166 atatat if (node->sysctl_flags & CTLFLAG_PERMANENT)
1676 1.166 atatat return (EPERM);
1677 1.166 atatat if (l != NULL && node->sysctl_desc != NULL)
1678 1.166 atatat return (EPERM);
1679 1.166 atatat
1680 1.166 atatat /*
1681 1.166 atatat * right, let's go ahead. the first step is
1682 1.166 atatat * making the description into something the
1683 1.166 atatat * node can "own", if need be.
1684 1.166 atatat */
1685 1.166 atatat if (l != NULL ||
1686 1.166 atatat dnode.sysctl_flags & CTLFLAG_OWNDESC) {
1687 1.166 atatat char *nd, k[1024];
1688 1.166 atatat
1689 1.166 atatat error = sysctl_copyinstr(l, dnode.sysctl_desc,
1690 1.166 atatat &k[0], sizeof(k), &sz);
1691 1.166 atatat if (error)
1692 1.166 atatat return (error);
1693 1.166 atatat nd = malloc(sz, M_SYSCTLDATA,
1694 1.166 atatat M_WAITOK|M_CANFAIL);
1695 1.166 atatat if (nd == NULL)
1696 1.166 atatat return (ENOMEM);
1697 1.166 atatat memcpy(nd, k, sz);
1698 1.166 atatat dnode.sysctl_flags |= CTLFLAG_OWNDESC;
1699 1.166 atatat dnode.sysctl_desc = nd;
1700 1.166 atatat }
1701 1.166 atatat
1702 1.166 atatat /*
1703 1.166 atatat * now "release" the old description and
1704 1.166 atatat * attach the new one. ta-da.
1705 1.166 atatat */
1706 1.166 atatat if ((node->sysctl_flags & CTLFLAG_OWNDESC) &&
1707 1.166 atatat node->sysctl_desc != NULL)
1708 1.166 atatat free((void*)node->sysctl_desc, M_SYSCTLDATA);
1709 1.166 atatat node->sysctl_desc = dnode.sysctl_desc;
1710 1.166 atatat node->sysctl_flags |=
1711 1.166 atatat (dnode.sysctl_flags & CTLFLAG_OWNDESC);
1712 1.166 atatat
1713 1.166 atatat /*
1714 1.166 atatat * now we "fall out" and into the loop which
1715 1.166 atatat * will copy the new description back out for
1716 1.166 atatat * those interested parties
1717 1.166 atatat */
1718 1.166 atatat }
1719 1.166 atatat }
1720 1.166 atatat
1721 1.166 atatat /*
1722 1.166 atatat * scan for one description or just retrieve all descriptions
1723 1.166 atatat */
1724 1.166 atatat for (i = 0; i < rnode->sysctl_clen; i++) {
1725 1.166 atatat /*
1726 1.166 atatat * did they ask for the description of only one node?
1727 1.166 atatat */
1728 1.166 atatat if (v != -1 && node[i].sysctl_num != dnode.sysctl_num)
1729 1.166 atatat continue;
1730 1.166 atatat
1731 1.166 atatat /*
1732 1.166 atatat * don't describe "private" nodes to non-suser users
1733 1.166 atatat */
1734 1.166 atatat if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) &&
1735 1.166 atatat !(suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)))
1736 1.166 atatat continue;
1737 1.166 atatat
1738 1.166 atatat /*
1739 1.166 atatat * is this description "valid"?
1740 1.166 atatat */
1741 1.166 atatat memset(&buf[0], 0, sizeof(buf));
1742 1.166 atatat if (node[i].sysctl_desc == NULL)
1743 1.166 atatat sz = 1;
1744 1.166 atatat else if (copystr(node[i].sysctl_desc, &d->descr_str[0],
1745 1.166 atatat sizeof(buf) - sizeof(*d), &sz) != 0) {
1746 1.166 atatat /*
1747 1.166 atatat * erase possible partial description
1748 1.166 atatat */
1749 1.166 atatat memset(&buf[0], 0, sizeof(buf));
1750 1.166 atatat sz = 1;
1751 1.166 atatat }
1752 1.166 atatat
1753 1.166 atatat /*
1754 1.166 atatat * we've got it, stuff it into the caller's buffer
1755 1.166 atatat */
1756 1.166 atatat d->descr_num = node[i].sysctl_num;
1757 1.166 atatat d->descr_ver = node[i].sysctl_ver;
1758 1.166 atatat d->descr_len = sz; /* includes trailing nul */
1759 1.166 atatat sz = (caddr_t)NEXT_DESCR(d) - (caddr_t)d;
1760 1.166 atatat if (oldp != NULL && left >= sz) {
1761 1.166 atatat error = sysctl_copyout(l, d, oldp, sz);
1762 1.166 atatat if (error)
1763 1.166 atatat return (error);
1764 1.166 atatat left -= sz;
1765 1.166 atatat oldp = (void*)__sysc_desc_adv(oldp, d->descr_len);
1766 1.166 atatat }
1767 1.166 atatat tot += sz;
1768 1.165 atatat
1769 1.166 atatat /*
1770 1.166 atatat * if we get this far with v not "unset", they asked
1771 1.166 atatat * for a specific node and we found it
1772 1.166 atatat */
1773 1.166 atatat if (v != -1)
1774 1.166 atatat break;
1775 1.166 atatat }
1776 1.166 atatat
1777 1.166 atatat /*
1778 1.166 atatat * did we find it after all?
1779 1.166 atatat */
1780 1.166 atatat if (v != -1 && tot == 0)
1781 1.166 atatat error = ENOENT;
1782 1.166 atatat else
1783 1.166 atatat *oldlenp = tot;
1784 1.165 atatat
1785 1.166 atatat return (error);
1786 1.165 atatat }
1787 1.165 atatat
1788 1.145 dsl /*
1789 1.153 atatat * ********************************************************************
1790 1.153 atatat * Section 3: Create and destroy from inside the kernel
1791 1.153 atatat * ********************************************************************
1792 1.153 atatat * sysctl_createv() and sysctl_destroyv() are simpler-to-use
1793 1.153 atatat * interfaces for the kernel to fling new entries into the mib and rip
1794 1.153 atatat * them out later. In the case of sysctl_createv(), the returned copy
1795 1.153 atatat * of the node (see sysctl_create()) will be translated back into a
1796 1.153 atatat * pointer to the actual node.
1797 1.153 atatat *
1798 1.153 atatat * Note that sysctl_createv() will return 0 if the create request
1799 1.153 atatat * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
1800 1.153 atatat * will return 0 if the node to be destroyed already does not exist
1801 1.153 atatat * (aka rm -f) or if it is a parent of other nodes.
1802 1.153 atatat *
1803 1.153 atatat * This allows two (or more) different subsystems to assert sub-tree
1804 1.153 atatat * existence before populating their own nodes, and to remove their
1805 1.153 atatat * own nodes without orphaning the others when they are done.
1806 1.153 atatat * ********************************************************************
1807 1.145 dsl */
1808 1.153 atatat int
1809 1.161 atatat sysctl_createv(struct sysctllog **log, int cflags,
1810 1.161 atatat struct sysctlnode **rnode, struct sysctlnode **cnode,
1811 1.161 atatat int flags, int type, const char *namep, const char *descr,
1812 1.153 atatat sysctlfn func, u_quad_t qv, void *newp, size_t newlen,
1813 1.153 atatat ...)
1814 1.153 atatat {
1815 1.153 atatat va_list ap;
1816 1.153 atatat int error, ni, namelen, name[CTL_MAXNAME];
1817 1.153 atatat struct sysctlnode *pnode, nnode, onode;
1818 1.153 atatat size_t sz;
1819 1.145 dsl
1820 1.161 atatat if (cnode != NULL)
1821 1.161 atatat *cnode = NULL;
1822 1.161 atatat if (cflags != 0)
1823 1.161 atatat return (EINVAL);
1824 1.161 atatat
1825 1.161 atatat /*
1826 1.161 atatat * where are we putting this?
1827 1.161 atatat */
1828 1.161 atatat if (rnode != NULL && *rnode == NULL) {
1829 1.161 atatat printf("sysctl_createv: rnode NULL\n");
1830 1.161 atatat return (EINVAL);
1831 1.161 atatat }
1832 1.161 atatat
1833 1.153 atatat /*
1834 1.153 atatat * what is it?
1835 1.153 atatat */
1836 1.161 atatat flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags);
1837 1.163 atatat if (log != NULL)
1838 1.163 atatat flags &= ~CTLFLAG_PERMANENT;
1839 1.145 dsl
1840 1.145 dsl /*
1841 1.153 atatat * where do we put it?
1842 1.145 dsl */
1843 1.153 atatat va_start(ap, newlen);
1844 1.153 atatat namelen = 0;
1845 1.153 atatat ni = -1;
1846 1.153 atatat do {
1847 1.153 atatat if (++ni == CTL_MAXNAME)
1848 1.153 atatat return (ENAMETOOLONG);
1849 1.153 atatat name[ni] = va_arg(ap, int);
1850 1.153 atatat /*
1851 1.153 atatat * sorry, this is not supported from here
1852 1.153 atatat */
1853 1.153 atatat if (name[ni] == CTL_CREATESYM)
1854 1.153 atatat return (EINVAL);
1855 1.153 atatat } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE);
1856 1.153 atatat namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0);
1857 1.153 atatat va_end(ap);
1858 1.69 simonb
1859 1.69 simonb /*
1860 1.153 atatat * what's it called
1861 1.69 simonb */
1862 1.153 atatat if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >
1863 1.153 atatat sizeof(nnode.sysctl_name))
1864 1.153 atatat return (ENAMETOOLONG);
1865 1.69 simonb
1866 1.153 atatat /*
1867 1.153 atatat * cons up the description of the new node
1868 1.153 atatat */
1869 1.153 atatat nnode.sysctl_num = name[namelen - 1];
1870 1.153 atatat name[namelen - 1] = CTL_CREATE;
1871 1.153 atatat nnode.sysctl_size = newlen;
1872 1.153 atatat nnode.sysctl_flags = flags;
1873 1.153 atatat if (type == CTLTYPE_NODE) {
1874 1.153 atatat nnode.sysctl_csize = 0;
1875 1.153 atatat nnode.sysctl_clen = 0;
1876 1.153 atatat nnode.sysctl_child = NULL;
1877 1.161 atatat if (flags & CTLFLAG_ALIAS)
1878 1.153 atatat nnode.sysctl_alias = qv;
1879 1.153 atatat }
1880 1.161 atatat else if (flags & CTLFLAG_IMMEDIATE) {
1881 1.153 atatat switch (type) {
1882 1.153 atatat case CTLTYPE_INT:
1883 1.153 atatat nnode.sysctl_idata = qv;
1884 1.153 atatat break;
1885 1.153 atatat case CTLTYPE_QUAD:
1886 1.153 atatat nnode.sysctl_qdata = qv;
1887 1.153 atatat break;
1888 1.153 atatat default:
1889 1.153 atatat return (EINVAL);
1890 1.153 atatat }
1891 1.69 simonb }
1892 1.153 atatat else {
1893 1.153 atatat nnode.sysctl_data = newp;
1894 1.69 simonb }
1895 1.153 atatat nnode.sysctl_func = func;
1896 1.153 atatat nnode.sysctl_parent = NULL;
1897 1.153 atatat nnode.sysctl_ver = 0;
1898 1.69 simonb
1899 1.153 atatat /*
1900 1.153 atatat * initialize lock state -- we need locks if the main tree has
1901 1.153 atatat * been marked as complete, but since we could be called from
1902 1.153 atatat * either there, or from a device driver (say, at device
1903 1.153 atatat * insertion), or from an lkm (at lkm load time, say), we
1904 1.153 atatat * don't really want to "wait"...
1905 1.153 atatat */
1906 1.153 atatat error = sysctl_lock(NULL, NULL, 0);
1907 1.153 atatat if (error)
1908 1.153 atatat return (error);
1909 1.153 atatat
1910 1.153 atatat /*
1911 1.153 atatat * locate the prospective parent of the new node, and if we
1912 1.153 atatat * find it, add the new node.
1913 1.153 atatat */
1914 1.153 atatat sz = sizeof(onode);
1915 1.160 atatat pnode = rnode ? *rnode : NULL;
1916 1.153 atatat error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
1917 1.161 atatat if (error) {
1918 1.161 atatat sysctl_unlock(NULL);
1919 1.161 atatat return (error);
1920 1.161 atatat }
1921 1.161 atatat error = sysctl_create(&name[ni], namelen - ni, &onode, &sz,
1922 1.161 atatat &nnode, sizeof(nnode), &name[0], NULL,
1923 1.161 atatat pnode);
1924 1.72 simonb
1925 1.72 simonb /*
1926 1.153 atatat * unfortunately the node we wanted to create is already
1927 1.153 atatat * there. if the node that's already there is a reasonable
1928 1.153 atatat * facsimile of the node we wanted to create, just pretend
1929 1.153 atatat * (for the caller's benefit) that we managed to create the
1930 1.153 atatat * node they wanted.
1931 1.72 simonb */
1932 1.153 atatat if (error == EEXIST) {
1933 1.153 atatat /* name is the same as requested... */
1934 1.153 atatat if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 &&
1935 1.153 atatat /* they want the same function... */
1936 1.153 atatat nnode.sysctl_func == onode.sysctl_func &&
1937 1.153 atatat /* number is the same as requested, or... */
1938 1.153 atatat (nnode.sysctl_num == onode.sysctl_num ||
1939 1.153 atatat /* they didn't pick a number... */
1940 1.153 atatat nnode.sysctl_num == CTL_CREATE)) {
1941 1.153 atatat /*
1942 1.153 atatat * collision here from trying to create
1943 1.153 atatat * something that already existed; let's give
1944 1.153 atatat * our customers a hand and tell them they got
1945 1.153 atatat * what they wanted.
1946 1.153 atatat */
1947 1.153 atatat #ifdef SYSCTL_DEBUG_CREATE
1948 1.153 atatat printf("cleared\n");
1949 1.153 atatat #endif /* SYSCTL_DEBUG_CREATE */
1950 1.153 atatat error = 0;
1951 1.153 atatat }
1952 1.72 simonb }
1953 1.72 simonb
1954 1.163 atatat if (error == 0 &&
1955 1.166 atatat (cnode != NULL || log != NULL || descr != NULL)) {
1956 1.153 atatat /*
1957 1.153 atatat * sysctl_create() gave us back a copy of the node,
1958 1.153 atatat * but we need to know where it actually is...
1959 1.153 atatat */
1960 1.161 atatat pnode = rnode ? *rnode : NULL;
1961 1.160 atatat error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni);
1962 1.160 atatat
1963 1.160 atatat /*
1964 1.160 atatat * manual scan of last layer so that aliased nodes
1965 1.160 atatat * aren't followed.
1966 1.160 atatat */
1967 1.160 atatat if (error == 0) {
1968 1.160 atatat for (ni = 0; ni < pnode->sysctl_clen; ni++)
1969 1.160 atatat if (pnode->sysctl_child[ni].sysctl_num ==
1970 1.160 atatat onode.sysctl_num)
1971 1.160 atatat break;
1972 1.160 atatat if (ni < pnode->sysctl_clen)
1973 1.160 atatat pnode = &pnode->sysctl_child[ni];
1974 1.160 atatat else
1975 1.160 atatat error = ENOENT;
1976 1.160 atatat }
1977 1.160 atatat
1978 1.153 atatat /*
1979 1.153 atatat * not expecting an error here, but...
1980 1.153 atatat */
1981 1.161 atatat if (error == 0) {
1982 1.163 atatat if (log != NULL)
1983 1.163 atatat sysctl_log_add(log, pnode);
1984 1.161 atatat if (cnode != NULL)
1985 1.161 atatat *cnode = pnode;
1986 1.166 atatat if (descr != NULL) {
1987 1.166 atatat if (flags & CTLFLAG_OWNDESC) {
1988 1.166 atatat size_t l = strlen(descr) + 1;
1989 1.166 atatat char *d = malloc(l, M_SYSCTLDATA,
1990 1.166 atatat M_WAITOK|M_CANFAIL);
1991 1.166 atatat if (d != NULL) {
1992 1.166 atatat memcpy(d, descr, l);
1993 1.166 atatat pnode->sysctl_desc = d;
1994 1.166 atatat }
1995 1.166 atatat }
1996 1.166 atatat else
1997 1.166 atatat pnode->sysctl_desc = descr;
1998 1.166 atatat }
1999 1.161 atatat }
2000 1.161 atatat else {
2001 1.161 atatat printf("sysctl_create succeeded but node not found?!\n");
2002 1.161 atatat /*
2003 1.161 atatat * confusing, but the create said it
2004 1.161 atatat * succeeded, so...
2005 1.161 atatat */
2006 1.161 atatat error = 0;
2007 1.161 atatat }
2008 1.72 simonb }
2009 1.72 simonb
2010 1.88 enami /*
2011 1.161 atatat * now it should be safe to release the lock state. note that
2012 1.161 atatat * the pointer to the newly created node being passed back may
2013 1.161 atatat * not be "good" for very long.
2014 1.88 enami */
2015 1.153 atatat sysctl_unlock(NULL);
2016 1.88 enami
2017 1.153 atatat if (error != 0) {
2018 1.153 atatat printf("sysctl_createv: sysctl_create(%s) returned %d\n",
2019 1.153 atatat nnode.sysctl_name, error);
2020 1.153 atatat #if 0
2021 1.153 atatat if (error != ENOENT)
2022 1.153 atatat sysctl_dump(&onode);
2023 1.153 atatat #endif
2024 1.72 simonb }
2025 1.153 atatat
2026 1.72 simonb return (error);
2027 1.72 simonb }
2028 1.69 simonb
2029 1.153 atatat int
2030 1.153 atatat sysctl_destroyv(struct sysctlnode *rnode, ...)
2031 1.1 cgd {
2032 1.153 atatat va_list ap;
2033 1.153 atatat int error, name[CTL_MAXNAME], namelen, ni;
2034 1.158 atatat struct sysctlnode *pnode, *node, dnode;
2035 1.158 atatat size_t sz;
2036 1.153 atatat
2037 1.153 atatat va_start(ap, rnode);
2038 1.153 atatat namelen = 0;
2039 1.153 atatat ni = 0;
2040 1.153 atatat do {
2041 1.153 atatat if (ni == CTL_MAXNAME)
2042 1.153 atatat return (ENAMETOOLONG);
2043 1.153 atatat name[ni] = va_arg(ap, int);
2044 1.153 atatat } while (name[ni++] != CTL_EOL);
2045 1.153 atatat namelen = ni - 1;
2046 1.153 atatat va_end(ap);
2047 1.62 simonb
2048 1.153 atatat /*
2049 1.153 atatat * i can't imagine why we'd be destroying a node when the tree
2050 1.153 atatat * wasn't complete, but who knows?
2051 1.153 atatat */
2052 1.153 atatat error = sysctl_lock(NULL, NULL, 0);
2053 1.153 atatat if (error)
2054 1.153 atatat return (error);
2055 1.1 cgd
2056 1.153 atatat /*
2057 1.153 atatat * where is it?
2058 1.153 atatat */
2059 1.153 atatat node = rnode;
2060 1.160 atatat error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni);
2061 1.153 atatat if (error) {
2062 1.153 atatat /* they want it gone and it's not there, so... */
2063 1.153 atatat sysctl_unlock(NULL);
2064 1.153 atatat return (error == ENOENT ? 0 : error);
2065 1.62 simonb }
2066 1.41 thorpej
2067 1.153 atatat /*
2068 1.160 atatat * set up the deletion
2069 1.158 atatat */
2070 1.160 atatat pnode = node;
2071 1.160 atatat node = &dnode;
2072 1.160 atatat memset(&dnode, 0, sizeof(dnode));
2073 1.160 atatat dnode.sysctl_num = name[namelen - 1];
2074 1.158 atatat
2075 1.158 atatat /*
2076 1.153 atatat * we found it, now let's nuke it
2077 1.153 atatat */
2078 1.153 atatat name[namelen - 1] = CTL_DESTROY;
2079 1.158 atatat sz = 0;
2080 1.158 atatat error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz,
2081 1.153 atatat node, sizeof(*node), &name[0], NULL,
2082 1.153 atatat pnode);
2083 1.166 atatat if (error == ENOTEMPTY) {
2084 1.1 cgd /*
2085 1.153 atatat * think of trying to delete "foo" when "foo.bar"
2086 1.153 atatat * (which someone else put there) is still in
2087 1.153 atatat * existence
2088 1.1 cgd */
2089 1.153 atatat error = 0;
2090 1.1 cgd
2091 1.166 atatat /*
2092 1.166 atatat * dunno who put the description there, but if this
2093 1.166 atatat * node can ever be removed, we need to make sure the
2094 1.166 atatat * string doesn't go out of context. that means we
2095 1.166 atatat * need to find the node that's still there (don't use
2096 1.166 atatat * sysctl_locate() because that follows aliasing).
2097 1.166 atatat */
2098 1.166 atatat node = pnode->sysctl_child;
2099 1.166 atatat for (ni = 0; ni < pnode->sysctl_clen; ni++)
2100 1.166 atatat if (node[ni].sysctl_num == dnode.sysctl_num)
2101 1.166 atatat break;
2102 1.166 atatat node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL;
2103 1.166 atatat
2104 1.166 atatat /*
2105 1.166 atatat * if we found it, and this node has a description,
2106 1.166 atatat * and this node can be released, and it doesn't
2107 1.166 atatat * already own its own description...sigh. :)
2108 1.166 atatat */
2109 1.166 atatat if (node != NULL && node->sysctl_desc != NULL &&
2110 1.166 atatat !(node->sysctl_flags & CTLFLAG_PERMANENT) &&
2111 1.166 atatat !(node->sysctl_flags & CTLFLAG_OWNDESC)) {
2112 1.166 atatat char *d;
2113 1.166 atatat
2114 1.166 atatat sz = strlen(node->sysctl_desc) + 1;
2115 1.166 atatat d = malloc(sz, M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2116 1.166 atatat if (d != NULL) {
2117 1.166 atatat memcpy(d, node->sysctl_desc, sz);
2118 1.166 atatat node->sysctl_desc = d;
2119 1.166 atatat node->sysctl_flags |= CTLFLAG_OWNDESC;
2120 1.166 atatat }
2121 1.166 atatat else {
2122 1.166 atatat /*
2123 1.166 atatat * XXX drop the description? be
2124 1.166 atatat * afraid? don't care?
2125 1.166 atatat */
2126 1.166 atatat }
2127 1.166 atatat }
2128 1.166 atatat }
2129 1.166 atatat
2130 1.153 atatat sysctl_unlock(NULL);
2131 1.1 cgd
2132 1.153 atatat return (error);
2133 1.153 atatat }
2134 1.1 cgd
2135 1.153 atatat #if 0
2136 1.153 atatat /*
2137 1.153 atatat * ********************************************************************
2138 1.153 atatat * the dump routine. i haven't yet decided how (if at all) i'll call
2139 1.153 atatat * this from userland when it's in the kernel.
2140 1.153 atatat * ********************************************************************
2141 1.153 atatat */
2142 1.153 atatat static const char *
2143 1.153 atatat sf(int f)
2144 1.153 atatat {
2145 1.153 atatat static char s[256];
2146 1.153 atatat char *c;
2147 1.153 atatat
2148 1.153 atatat s[0] = '\0';
2149 1.153 atatat c = "";
2150 1.153 atatat
2151 1.153 atatat #define print_flag(_f, _s, _c, _q, _x) \
2152 1.161 atatat if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \
2153 1.153 atatat strlcat((_s), (_c), sizeof(_s)); \
2154 1.153 atatat strlcat((_s), __STRING(_q), sizeof(_s)); \
2155 1.153 atatat (_c) = ","; \
2156 1.161 atatat (_f) &= ~__CONCAT(CTLFLAG_,_x); \
2157 1.153 atatat }
2158 1.161 atatat
2159 1.161 atatat print_flag(f, s, c, READONLY, READWRITE);
2160 1.161 atatat print_flag(f, s, c, READONLY1, READWRITE);
2161 1.161 atatat print_flag(f, s, c, READONLY2, READWRITE);
2162 1.161 atatat print_flag(f, s, c, READWRITE, READWRITE);
2163 1.161 atatat print_flag(f, s, c, ANYWRITE, ANYWRITE);
2164 1.161 atatat print_flag(f, s, c, PRIVATE, PRIVATE);
2165 1.161 atatat print_flag(f, s, c, PERMANENT, PERMANENT);
2166 1.161 atatat print_flag(f, s, c, OWNDATA, OWNDATA);
2167 1.161 atatat print_flag(f, s, c, IMMEDIATE, IMMEDIATE);
2168 1.161 atatat print_flag(f, s, c, HEX, HEX);
2169 1.161 atatat print_flag(f, s, c, ROOT, ROOT);
2170 1.161 atatat print_flag(f, s, c, ANYNUMBER, ANYNUMBER);
2171 1.161 atatat print_flag(f, s, c, HIDDEN, HIDDEN);
2172 1.161 atatat print_flag(f, s, c, ALIAS, ALIAS);
2173 1.153 atatat #undef print_flag
2174 1.153 atatat
2175 1.153 atatat if (f) {
2176 1.153 atatat char foo[9];
2177 1.153 atatat snprintf(foo, sizeof(foo), "%x", f);
2178 1.153 atatat strlcat(s, c, sizeof(s));
2179 1.153 atatat strlcat(s, foo, sizeof(s));
2180 1.153 atatat }
2181 1.153 atatat
2182 1.153 atatat return (s);
2183 1.153 atatat }
2184 1.153 atatat
2185 1.153 atatat static const char *
2186 1.153 atatat st(int t)
2187 1.153 atatat {
2188 1.153 atatat
2189 1.153 atatat switch (t) {
2190 1.153 atatat case CTLTYPE_NODE:
2191 1.153 atatat return "NODE";
2192 1.153 atatat case CTLTYPE_INT:
2193 1.153 atatat return "INT";
2194 1.153 atatat case CTLTYPE_STRING:
2195 1.153 atatat return "STRING";
2196 1.153 atatat case CTLTYPE_QUAD:
2197 1.153 atatat return "QUAD";
2198 1.153 atatat case CTLTYPE_STRUCT:
2199 1.153 atatat return "STRUCT";
2200 1.153 atatat }
2201 1.71 simonb
2202 1.153 atatat return "???";
2203 1.153 atatat }
2204 1.1 cgd
2205 1.153 atatat void
2206 1.153 atatat sysctl_dump(const struct sysctlnode *d)
2207 1.153 atatat {
2208 1.153 atatat static char nmib[64], smib[256];
2209 1.153 atatat static int indent;
2210 1.153 atatat struct sysctlnode *n;
2211 1.153 atatat char *np, *sp, tmp[20];
2212 1.153 atatat int i;
2213 1.1 cgd
2214 1.153 atatat if (d == NULL)
2215 1.153 atatat return;
2216 1.71 simonb
2217 1.153 atatat np = &nmib[strlen(nmib)];
2218 1.153 atatat sp = &smib[strlen(smib)];
2219 1.71 simonb
2220 1.161 atatat if (!(d->sysctl_flags & CTLFLAG_ROOT)) {
2221 1.153 atatat snprintf(tmp, sizeof(tmp), "%d", d->sysctl_num);
2222 1.153 atatat strcat(nmib, ".");
2223 1.153 atatat strcat(smib, ".");
2224 1.153 atatat strcat(nmib, tmp);
2225 1.153 atatat strcat(smib, d->sysctl_name);
2226 1.153 atatat printf("%s -> %s (%d)\n", &nmib[1], &smib[1],
2227 1.153 atatat SYSCTL_TYPE(d->sysctl_flags));
2228 1.153 atatat }
2229 1.153 atatat
2230 1.153 atatat if (1) {
2231 1.153 atatat printf("%*s%p:\tsysctl_name [%s]\n", indent, "",
2232 1.153 atatat d, d->sysctl_name);
2233 1.153 atatat printf("%*s\t\tsysctl_num %d\n", indent, "",
2234 1.153 atatat d->sysctl_num);
2235 1.153 atatat printf("%*s\t\tsysctl_flags %x (flags=%x<%s> type=%d<%s> "
2236 1.153 atatat "size=%zu)\n",
2237 1.153 atatat indent, "", d->sysctl_flags,
2238 1.153 atatat SYSCTL_FLAGS(d->sysctl_flags),
2239 1.153 atatat sf(SYSCTL_FLAGS(d->sysctl_flags)),
2240 1.153 atatat SYSCTL_TYPE(d->sysctl_flags),
2241 1.153 atatat st(SYSCTL_TYPE(d->sysctl_flags)),
2242 1.153 atatat d->sysctl_size);
2243 1.153 atatat if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
2244 1.153 atatat printf("%*s\t\tsysctl_csize %d\n", indent, "",
2245 1.153 atatat d->sysctl_csize);
2246 1.153 atatat printf("%*s\t\tsysctl_clen %d\n", indent, "",
2247 1.153 atatat d->sysctl_clen);
2248 1.153 atatat printf("%*s\t\tsysctl_child %p\n", indent, "",
2249 1.153 atatat d->sysctl_child);
2250 1.153 atatat }
2251 1.153 atatat else
2252 1.153 atatat printf("%*s\t\tsysctl_data %p\n", indent, "",
2253 1.153 atatat d->sysctl_data);
2254 1.153 atatat printf("%*s\t\tsysctl_func %p\n", indent, "",
2255 1.153 atatat d->sysctl_func);
2256 1.153 atatat printf("%*s\t\tsysctl_parent %p\n", indent, "",
2257 1.153 atatat d->sysctl_parent);
2258 1.153 atatat printf("%*s\t\tsysctl_ver %d\n", indent, "",
2259 1.153 atatat d->sysctl_ver);
2260 1.153 atatat }
2261 1.71 simonb
2262 1.153 atatat if (SYSCTL_TYPE(d->sysctl_flags) == CTLTYPE_NODE) {
2263 1.153 atatat indent += 8;
2264 1.153 atatat n = d->sysctl_child;
2265 1.153 atatat for (i = 0; i < d->sysctl_clen; i++) {
2266 1.153 atatat sysctl_dump(&n[i]);
2267 1.1 cgd }
2268 1.153 atatat indent -= 8;
2269 1.1 cgd }
2270 1.41 thorpej
2271 1.153 atatat np[0] = '\0';
2272 1.153 atatat sp[0] = '\0';
2273 1.1 cgd }
2274 1.153 atatat #endif /* 0 */
2275 1.123 thorpej
2276 1.123 thorpej /*
2277 1.153 atatat * ********************************************************************
2278 1.153 atatat * Deletes an entire n-ary tree. Not recommended unless you know why
2279 1.153 atatat * you're doing it. Personally, I don't know why you'd even think
2280 1.153 atatat * about it.
2281 1.153 atatat * ********************************************************************
2282 1.123 thorpej */
2283 1.153 atatat void
2284 1.153 atatat sysctl_free(struct sysctlnode *rnode)
2285 1.123 thorpej {
2286 1.153 atatat struct sysctlnode *node, *pnode;
2287 1.123 thorpej
2288 1.161 atatat if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) {
2289 1.161 atatat printf("sysctl_free: rnode %p wrong version\n", rnode);
2290 1.161 atatat return;
2291 1.161 atatat }
2292 1.161 atatat
2293 1.153 atatat if (rnode == NULL)
2294 1.153 atatat rnode = &sysctl_root;
2295 1.153 atatat pnode = rnode;
2296 1.153 atatat
2297 1.153 atatat node = pnode->sysctl_child;
2298 1.153 atatat do {
2299 1.153 atatat while (node != NULL && pnode->sysctl_csize > 0) {
2300 1.153 atatat while (node <
2301 1.153 atatat &pnode->sysctl_child[pnode->sysctl_clen] &&
2302 1.153 atatat (SYSCTL_TYPE(node->sysctl_flags) !=
2303 1.153 atatat CTLTYPE_NODE ||
2304 1.153 atatat node->sysctl_csize == 0)) {
2305 1.153 atatat if (SYSCTL_FLAGS(node->sysctl_flags) &
2306 1.161 atatat CTLFLAG_OWNDATA) {
2307 1.153 atatat if (node->sysctl_data != NULL) {
2308 1.153 atatat FREE(node->sysctl_data,
2309 1.153 atatat M_SYSCTLDATA);
2310 1.153 atatat node->sysctl_data = NULL;
2311 1.153 atatat }
2312 1.153 atatat }
2313 1.166 atatat if (SYSCTL_FLAGS(node->sysctl_flags) &
2314 1.166 atatat CTLFLAG_OWNDESC) {
2315 1.166 atatat if (node->sysctl_desc != NULL) {
2316 1.166 atatat FREE(node->sysctl_desc,
2317 1.166 atatat M_SYSCTLDATA);
2318 1.166 atatat node->sysctl_desc = NULL;
2319 1.166 atatat }
2320 1.166 atatat }
2321 1.153 atatat node++;
2322 1.153 atatat }
2323 1.153 atatat if (node < &pnode->sysctl_child[pnode->sysctl_clen]) {
2324 1.153 atatat pnode = node;
2325 1.153 atatat node = node->sysctl_child;
2326 1.153 atatat }
2327 1.153 atatat else
2328 1.153 atatat break;
2329 1.153 atatat }
2330 1.153 atatat if (pnode->sysctl_child != NULL)
2331 1.153 atatat FREE(pnode->sysctl_child, M_SYSCTLNODE);
2332 1.153 atatat pnode->sysctl_clen = 0;
2333 1.153 atatat pnode->sysctl_csize = 0;
2334 1.153 atatat pnode->sysctl_child = NULL;
2335 1.153 atatat node = pnode;
2336 1.153 atatat pnode = node->sysctl_parent;
2337 1.158 atatat } while (pnode != NULL && node != rnode);
2338 1.123 thorpej }
2339 1.123 thorpej
2340 1.163 atatat int
2341 1.163 atatat sysctl_log_add(struct sysctllog **logp, struct sysctlnode *node)
2342 1.163 atatat {
2343 1.163 atatat int name[CTL_MAXNAME], namelen, i;
2344 1.163 atatat struct sysctlnode *pnode;
2345 1.163 atatat struct sysctllog *log;
2346 1.163 atatat
2347 1.163 atatat if (node->sysctl_flags & CTLFLAG_PERMANENT)
2348 1.163 atatat return (0);
2349 1.163 atatat
2350 1.163 atatat if (logp == NULL)
2351 1.163 atatat return (0);
2352 1.163 atatat
2353 1.163 atatat if (*logp == NULL) {
2354 1.163 atatat MALLOC(log, struct sysctllog *, sizeof(struct sysctllog),
2355 1.163 atatat M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2356 1.163 atatat if (log == NULL) {
2357 1.163 atatat /* XXX print error message? */
2358 1.163 atatat return (-1);
2359 1.163 atatat }
2360 1.163 atatat MALLOC(log->log_num, int *, 16 * sizeof(int),
2361 1.163 atatat M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2362 1.163 atatat if (log->log_num == NULL) {
2363 1.163 atatat /* XXX print error message? */
2364 1.163 atatat free(log, M_SYSCTLDATA);
2365 1.163 atatat return (-1);
2366 1.163 atatat }
2367 1.163 atatat memset(log->log_num, 0, 16 * sizeof(int));
2368 1.163 atatat log->log_root = NULL;
2369 1.163 atatat log->log_size = 16;
2370 1.163 atatat log->log_left = 16;
2371 1.163 atatat *logp = log;
2372 1.163 atatat }
2373 1.163 atatat else
2374 1.163 atatat log = *logp;
2375 1.163 atatat
2376 1.163 atatat /*
2377 1.163 atatat * check that the root is proper. it's okay to record the
2378 1.163 atatat * address of the root of a tree. it's the only thing that's
2379 1.163 atatat * guaranteed not to shift around as nodes come and go.
2380 1.163 atatat */
2381 1.163 atatat if (log->log_root == NULL)
2382 1.163 atatat log->log_root = sysctl_rootof(node);
2383 1.163 atatat else if (log->log_root != sysctl_rootof(node)) {
2384 1.163 atatat printf("sysctl: log %p root mismatch (%p)\n",
2385 1.163 atatat log->log_root, sysctl_rootof(node));
2386 1.163 atatat return (-1);
2387 1.163 atatat }
2388 1.163 atatat
2389 1.163 atatat /*
2390 1.163 atatat * we will copy out name in reverse order
2391 1.163 atatat */
2392 1.163 atatat for (pnode = node, namelen = 0;
2393 1.163 atatat pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT);
2394 1.163 atatat pnode = pnode->sysctl_parent)
2395 1.163 atatat name[namelen++] = pnode->sysctl_num;
2396 1.163 atatat
2397 1.163 atatat /*
2398 1.163 atatat * do we have space?
2399 1.163 atatat */
2400 1.163 atatat if (log->log_left < (namelen + 3))
2401 1.163 atatat sysctl_log_realloc(log);
2402 1.163 atatat if (log->log_left < (namelen + 3))
2403 1.163 atatat return (-1);
2404 1.163 atatat
2405 1.163 atatat /*
2406 1.163 atatat * stuff name in, then namelen, then node type, and finally,
2407 1.163 atatat * the version for non-node nodes.
2408 1.163 atatat */
2409 1.163 atatat for (i = 0; i < namelen; i++)
2410 1.163 atatat log->log_num[--log->log_left] = name[i];
2411 1.163 atatat log->log_num[--log->log_left] = namelen;
2412 1.163 atatat log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags);
2413 1.163 atatat if (log->log_num[log->log_left] != CTLTYPE_NODE)
2414 1.163 atatat log->log_num[--log->log_left] = node->sysctl_ver;
2415 1.163 atatat else
2416 1.163 atatat log->log_num[--log->log_left] = 0;
2417 1.163 atatat
2418 1.163 atatat return (0);
2419 1.163 atatat }
2420 1.163 atatat
2421 1.163 atatat void
2422 1.163 atatat sysctl_teardown(struct sysctllog **logp)
2423 1.163 atatat {
2424 1.163 atatat struct sysctlnode node, *rnode;
2425 1.163 atatat struct sysctllog *log;
2426 1.163 atatat uint namelen;
2427 1.163 atatat int *name, t, v, error, ni;
2428 1.163 atatat size_t sz;
2429 1.163 atatat
2430 1.163 atatat if (logp == NULL || *logp == NULL)
2431 1.163 atatat return;
2432 1.163 atatat log = *logp;
2433 1.163 atatat
2434 1.163 atatat error = sysctl_lock(NULL, NULL, 0);
2435 1.163 atatat if (error)
2436 1.163 atatat return;
2437 1.163 atatat
2438 1.163 atatat memset(&node, 0, sizeof(node));
2439 1.163 atatat
2440 1.163 atatat while (log->log_left < log->log_size) {
2441 1.163 atatat KASSERT((log->log_left + 3 < log->log_size) &&
2442 1.163 atatat (log->log_left + log->log_num[log->log_left + 2] <=
2443 1.163 atatat log->log_size));
2444 1.163 atatat v = log->log_num[log->log_left++];
2445 1.163 atatat t = log->log_num[log->log_left++];
2446 1.163 atatat namelen = log->log_num[log->log_left++];
2447 1.163 atatat name = &log->log_num[log->log_left];
2448 1.163 atatat
2449 1.163 atatat node.sysctl_num = name[namelen - 1];
2450 1.163 atatat node.sysctl_flags = t;
2451 1.163 atatat node.sysctl_ver = v;
2452 1.163 atatat
2453 1.163 atatat rnode = log->log_root;
2454 1.163 atatat error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni);
2455 1.163 atatat if (error == 0) {
2456 1.163 atatat name[namelen - 1] = CTL_DESTROY;
2457 1.163 atatat rnode = rnode->sysctl_parent;
2458 1.163 atatat sz = 0;
2459 1.163 atatat (void)sysctl_destroy(&name[namelen - 1], 1, NULL,
2460 1.163 atatat &sz, &node, sizeof(node),
2461 1.163 atatat &name[0], NULL, rnode);
2462 1.163 atatat }
2463 1.163 atatat
2464 1.163 atatat log->log_left += namelen;
2465 1.163 atatat }
2466 1.163 atatat
2467 1.163 atatat KASSERT(log->log_size == log->log_left);
2468 1.163 atatat free(log->log_num, M_SYSCTLDATA);
2469 1.163 atatat free(log, M_SYSCTLDATA);
2470 1.163 atatat *logp = NULL;
2471 1.163 atatat
2472 1.163 atatat sysctl_unlock(NULL);
2473 1.163 atatat }
2474 1.163 atatat
2475 1.1 cgd /*
2476 1.153 atatat * ********************************************************************
2477 1.153 atatat * old_sysctl -- A routine to bridge old-style internal calls to the
2478 1.153 atatat * new infrastructure.
2479 1.153 atatat * ********************************************************************
2480 1.1 cgd */
2481 1.153 atatat int
2482 1.153 atatat old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2483 1.153 atatat void *newp, size_t newlen, struct lwp *l)
2484 1.1 cgd {
2485 1.153 atatat int error;
2486 1.153 atatat size_t savelen = *oldlenp;
2487 1.153 atatat
2488 1.153 atatat error = sysctl_lock(l, oldp, savelen);
2489 1.153 atatat if (error)
2490 1.153 atatat return (error);
2491 1.153 atatat error = sysctl_dispatch(name, namelen, oldp, oldlenp,
2492 1.153 atatat newp, newlen, name, l, NULL);
2493 1.153 atatat sysctl_unlock(l);
2494 1.153 atatat if (error == 0 && oldp != NULL && savelen < *oldlenp)
2495 1.153 atatat error = ENOMEM;
2496 1.1 cgd
2497 1.153 atatat return (error);
2498 1.62 simonb }
2499 1.62 simonb
2500 1.62 simonb /*
2501 1.153 atatat * ********************************************************************
2502 1.153 atatat * Section 4: Generic helper routines
2503 1.153 atatat * ********************************************************************
2504 1.153 atatat * "helper" routines that can do more finely grained access control,
2505 1.153 atatat * construct structures from disparate information, create the
2506 1.153 atatat * appearance of more nodes and sub-trees, etc. for example, if
2507 1.153 atatat * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
2508 1.153 atatat * with a dynamically created list of nodes that represented the
2509 1.153 atatat * currently running processes at that instant.
2510 1.153 atatat * ********************************************************************
2511 1.153 atatat */
2512 1.123 thorpej
2513 1.123 thorpej /*
2514 1.153 atatat * first, a few generic helpers that provide:
2515 1.153 atatat *
2516 1.153 atatat * sysctl_needfunc() a readonly interface that emits a warning
2517 1.153 atatat * sysctl_notavail() returns EOPNOTSUPP (generic error)
2518 1.153 atatat * sysctl_null() an empty return buffer with no error
2519 1.123 thorpej */
2520 1.62 simonb int
2521 1.153 atatat sysctl_needfunc(SYSCTLFN_ARGS)
2522 1.62 simonb {
2523 1.153 atatat int error;
2524 1.153 atatat
2525 1.153 atatat printf("!!SYSCTL_NEEDFUNC!!\n");
2526 1.62 simonb
2527 1.153 atatat if (newp != NULL || namelen != 0)
2528 1.153 atatat return (EOPNOTSUPP);
2529 1.62 simonb
2530 1.153 atatat error = 0;
2531 1.153 atatat if (oldp != NULL)
2532 1.153 atatat error = sysctl_copyout(l, rnode->sysctl_data, oldp,
2533 1.153 atatat MIN(rnode->sysctl_size, *oldlenp));
2534 1.153 atatat *oldlenp = rnode->sysctl_size;
2535 1.62 simonb
2536 1.153 atatat return (error);
2537 1.153 atatat }
2538 1.62 simonb
2539 1.153 atatat int
2540 1.153 atatat sysctl_notavail(SYSCTLFN_ARGS)
2541 1.153 atatat {
2542 1.62 simonb
2543 1.155 atatat if (namelen == 1 && name[0] == CTL_QUERY)
2544 1.155 atatat return (sysctl_query(SYSCTLFN_CALL(rnode)));
2545 1.155 atatat
2546 1.153 atatat return (EOPNOTSUPP);
2547 1.153 atatat }
2548 1.62 simonb
2549 1.153 atatat int
2550 1.153 atatat sysctl_null(SYSCTLFN_ARGS)
2551 1.153 atatat {
2552 1.62 simonb
2553 1.153 atatat *oldlenp = 0;
2554 1.123 thorpej
2555 1.153 atatat return (0);
2556 1.153 atatat }
2557 1.67 simonb
2558 1.153 atatat /*
2559 1.153 atatat * ********************************************************************
2560 1.153 atatat * Section 5: The machinery that makes it all go
2561 1.153 atatat * ********************************************************************
2562 1.153 atatat * Memory "manglement" routines. Not much to this, eh?
2563 1.153 atatat * ********************************************************************
2564 1.153 atatat */
2565 1.153 atatat static int
2566 1.153 atatat sysctl_alloc(struct sysctlnode *p, int x)
2567 1.153 atatat {
2568 1.153 atatat int i;
2569 1.153 atatat struct sysctlnode *n;
2570 1.62 simonb
2571 1.153 atatat assert(p->sysctl_child == NULL);
2572 1.62 simonb
2573 1.153 atatat if (x == 1)
2574 1.153 atatat MALLOC(n, struct sysctlnode *,
2575 1.153 atatat sizeof(struct sysctlnode),
2576 1.153 atatat M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2577 1.62 simonb else
2578 1.153 atatat MALLOC(n, struct sysctlnode *,
2579 1.153 atatat SYSCTL_DEFSIZE * sizeof(struct sysctlnode),
2580 1.153 atatat M_SYSCTLNODE, M_WAITOK|M_CANFAIL);
2581 1.153 atatat if (n == NULL)
2582 1.153 atatat return (ENOMEM);
2583 1.153 atatat
2584 1.153 atatat if (x == 1) {
2585 1.153 atatat memset(n, 0, sizeof(struct sysctlnode));
2586 1.153 atatat p->sysctl_csize = 1;
2587 1.62 simonb }
2588 1.153 atatat else {
2589 1.153 atatat memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode));
2590 1.153 atatat p->sysctl_csize = SYSCTL_DEFSIZE;
2591 1.62 simonb }
2592 1.153 atatat p->sysctl_clen = 0;
2593 1.62 simonb
2594 1.153 atatat for (i = 0; i < p->sysctl_csize; i++)
2595 1.153 atatat n[i].sysctl_parent = p;
2596 1.62 simonb
2597 1.153 atatat p->sysctl_child = n;
2598 1.153 atatat return (0);
2599 1.1 cgd }
2600 1.78 jdolecek
2601 1.78 jdolecek static int
2602 1.153 atatat sysctl_realloc(struct sysctlnode *p)
2603 1.78 jdolecek {
2604 1.153 atatat int i, j;
2605 1.153 atatat struct sysctlnode *n;
2606 1.78 jdolecek
2607 1.153 atatat assert(p->sysctl_csize == p->sysctl_clen);
2608 1.78 jdolecek
2609 1.153 atatat /*
2610 1.153 atatat * how many do we have...how many should we make?
2611 1.153 atatat */
2612 1.153 atatat i = p->sysctl_clen;
2613 1.153 atatat n = malloc(2 * i * sizeof(struct sysctlnode), M_SYSCTLNODE,
2614 1.153 atatat M_WAITOK|M_CANFAIL);
2615 1.153 atatat if (n == NULL)
2616 1.153 atatat return (ENOMEM);
2617 1.78 jdolecek
2618 1.153 atatat /*
2619 1.153 atatat * move old children over...initialize new children
2620 1.153 atatat */
2621 1.153 atatat memcpy(n, p->sysctl_child, i * sizeof(struct sysctlnode));
2622 1.153 atatat memset(&n[i], 0, i * sizeof(struct sysctlnode));
2623 1.153 atatat p->sysctl_csize = 2 * i;
2624 1.78 jdolecek
2625 1.153 atatat /*
2626 1.153 atatat * reattach moved (and new) children to parent; if a moved
2627 1.153 atatat * child node has children, reattach the parent pointers of
2628 1.153 atatat * grandchildren
2629 1.153 atatat */
2630 1.153 atatat for (i = 0; i < p->sysctl_csize; i++) {
2631 1.153 atatat n[i].sysctl_parent = p;
2632 1.153 atatat if (n[i].sysctl_child != NULL) {
2633 1.153 atatat for (j = 0; j < n[i].sysctl_csize; j++)
2634 1.153 atatat n[i].sysctl_child[j].sysctl_parent = &n[i];
2635 1.153 atatat }
2636 1.78 jdolecek }
2637 1.78 jdolecek
2638 1.153 atatat /*
2639 1.153 atatat * get out with the old and in with the new
2640 1.153 atatat */
2641 1.153 atatat FREE(p->sysctl_child, M_SYSCTLNODE);
2642 1.153 atatat p->sysctl_child = n;
2643 1.100 simonb
2644 1.153 atatat return (0);
2645 1.100 simonb }
2646 1.162 atatat
2647 1.163 atatat static int
2648 1.163 atatat sysctl_log_realloc(struct sysctllog *log)
2649 1.163 atatat {
2650 1.163 atatat int *n, s, d;
2651 1.163 atatat
2652 1.163 atatat s = log->log_size * 2;
2653 1.163 atatat d = log->log_size;
2654 1.163 atatat
2655 1.163 atatat n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL);
2656 1.163 atatat if (n == NULL)
2657 1.163 atatat return (-1);
2658 1.163 atatat
2659 1.163 atatat memset(n, 0, s * sizeof(int));
2660 1.163 atatat memcpy(&n[d], log->log_num, d * sizeof(int));
2661 1.163 atatat free(log->log_num, M_SYSCTLDATA);
2662 1.163 atatat log->log_num = n;
2663 1.163 atatat if (d)
2664 1.163 atatat log->log_left += d;
2665 1.163 atatat else
2666 1.163 atatat log->log_left = s;
2667 1.163 atatat log->log_size = s;
2668 1.163 atatat
2669 1.163 atatat return (0);
2670 1.163 atatat }
2671 1.163 atatat
2672 1.162 atatat /*
2673 1.162 atatat * ********************************************************************
2674 1.162 atatat * Section 6: Conversion between API versions wrt the sysctlnode
2675 1.162 atatat * ********************************************************************
2676 1.162 atatat */
2677 1.162 atatat static int
2678 1.162 atatat sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz,
2679 1.162 atatat struct sysctlnode *node)
2680 1.162 atatat {
2681 1.164 atatat struct sysctlnode0 inode0;
2682 1.162 atatat int error, flags;
2683 1.162 atatat
2684 1.162 atatat if (i == NULL) {
2685 1.162 atatat memset(node, 0, sizeof(*node));
2686 1.162 atatat *vp = SYSCTL_VERS_0;
2687 1.162 atatat return (0);
2688 1.162 atatat }
2689 1.162 atatat
2690 1.162 atatat if (sz < sizeof(flags))
2691 1.162 atatat return (EINVAL);
2692 1.162 atatat
2693 1.162 atatat error = sysctl_copyin(l, i, &flags, sizeof(flags));
2694 1.162 atatat if (error)
2695 1.162 atatat return (error);
2696 1.162 atatat
2697 1.164 atatat if (sz == sizeof(inode0) &&
2698 1.164 atatat SYSCTL_VERS(flags) == SYSCTL_VERS_0) {
2699 1.164 atatat error = sysctl_copyin(l, i, &inode0, sizeof(inode0));
2700 1.164 atatat if (error)
2701 1.164 atatat return (error);
2702 1.164 atatat
2703 1.164 atatat node->sysctl_flags = inode0.sysctl0_flags;
2704 1.164 atatat node->sysctl_num = inode0.sysctl0_num;
2705 1.164 atatat memcpy(node->sysctl_name, inode0.sysctl0_name, SYSCTL_NAMELEN);
2706 1.164 atatat node->sysctl_ver = inode0.sysctl0_ver;
2707 1.164 atatat node->sysctl_csize = inode0.sysctl0_csize;
2708 1.164 atatat node->sysctl_clen = inode0.sysctl0_clen;
2709 1.164 atatat node->sysctl_child = NULL; /* inode.sysctl0_child; */
2710 1.164 atatat node->sysctl_data = inode0.sysctl0_data;
2711 1.164 atatat node->sysctl_offset = 0;
2712 1.164 atatat node->sysctl_alias = inode0.sysctl0_alias;
2713 1.164 atatat node->sysctl_idata = inode0.sysctl0_idata;
2714 1.164 atatat node->sysctl_qdata = inode0.sysctl0_qdata;
2715 1.164 atatat node->sysctl_size = inode0.sysctl0_size;
2716 1.164 atatat node->sysctl_func = inode0.sysctl0_func;
2717 1.164 atatat node->sysctl_parent = NULL; /* inode.sysctl0_parent; */
2718 1.164 atatat node->sysctl_desc = NULL;
2719 1.164 atatat
2720 1.164 atatat node->sysctl_flags &= ~SYSCTL_VERS_MASK;
2721 1.164 atatat node->sysctl_flags |= SYSCTL_VERSION;
2722 1.164 atatat if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE &&
2723 1.164 atatat node->sysctl_size == sizeof(inode0))
2724 1.164 atatat node->sysctl_size = sizeof(*node);
2725 1.164 atatat
2726 1.164 atatat *vp = SYSCTL_VERS_0;
2727 1.164 atatat return (0);
2728 1.164 atatat }
2729 1.164 atatat
2730 1.164 atatat #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2731 1.164 atatat #error sysctl_cvt_in: no support for SYSCTL_VERSION
2732 1.164 atatat #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2733 1.164 atatat
2734 1.162 atatat if (sz == sizeof(*node) &&
2735 1.162 atatat SYSCTL_VERS(flags) == SYSCTL_VERSION) {
2736 1.162 atatat error = sysctl_copyin(l, i, node, sizeof(*node));
2737 1.162 atatat if (error)
2738 1.162 atatat return (error);
2739 1.162 atatat *vp = SYSCTL_VERSION;
2740 1.162 atatat return (0);
2741 1.162 atatat }
2742 1.162 atatat
2743 1.162 atatat return (EINVAL);
2744 1.162 atatat }
2745 1.162 atatat
2746 1.162 atatat static int
2747 1.162 atatat sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i,
2748 1.162 atatat void *ovp, size_t left, size_t *szp)
2749 1.162 atatat {
2750 1.164 atatat struct sysctlnode0 onode0;
2751 1.162 atatat size_t sz = sizeof(*i);
2752 1.162 atatat const void *src = i;
2753 1.162 atatat int error;
2754 1.162 atatat
2755 1.162 atatat switch (v) {
2756 1.164 atatat case SYSCTL_VERS_0:
2757 1.164 atatat sz = sizeof(onode0);
2758 1.164 atatat src = &onode0;
2759 1.164 atatat memset(&onode0, 0, sz);
2760 1.164 atatat onode0.sysctl0_flags = i->sysctl_flags;
2761 1.164 atatat onode0.sysctl0_num = i->sysctl_num;
2762 1.164 atatat onode0.sysctl0_size = i->sysctl_size;
2763 1.164 atatat memcpy(onode0.sysctl0_name, i->sysctl_name, SYSCTL_NAMELEN);
2764 1.164 atatat onode0.sysctl0_csize = i->sysctl_csize;
2765 1.164 atatat onode0.sysctl0_clen = i->sysctl_clen;
2766 1.164 atatat onode0.sysctl0_child = NULL; /* i->sysctl_child; */
2767 1.164 atatat onode0.sysctl0_alias = i->sysctl_alias;
2768 1.164 atatat onode0.sysctl0_data = i->sysctl_data;
2769 1.164 atatat onode0.sysctl0_idata = i->sysctl_idata;
2770 1.164 atatat onode0.sysctl0_qdata = i->sysctl_qdata;
2771 1.164 atatat onode0.sysctl0_func = i->sysctl_func;
2772 1.164 atatat onode0.sysctl0_parent = NULL; /* i->syctl_parent; */
2773 1.164 atatat onode0.sysctl0_ver = i->sysctl_ver;
2774 1.164 atatat
2775 1.164 atatat onode0.sysctl0_flags &= ~SYSCTL_VERS_MASK;
2776 1.164 atatat onode0.sysctl0_flags |= SYSCTL_VERS_0;
2777 1.164 atatat if (SYSCTL_TYPE(onode0.sysctl0_flags) == CTLTYPE_NODE &&
2778 1.164 atatat onode0.sysctl0_size == sizeof(*i))
2779 1.164 atatat onode0.sysctl0_size = sizeof(onode0);
2780 1.164 atatat
2781 1.164 atatat break;
2782 1.164 atatat
2783 1.164 atatat #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2784 1.164 atatat #error sysctl_cvt_out: no support for SYSCTL_VERSION
2785 1.164 atatat #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2786 1.164 atatat
2787 1.162 atatat case SYSCTL_VERSION:
2788 1.162 atatat /* nothing more to do here */
2789 1.162 atatat break;
2790 1.162 atatat }
2791 1.162 atatat
2792 1.162 atatat if (ovp != NULL && left >= sz) {
2793 1.162 atatat error = sysctl_copyout(l, src, ovp, sz);
2794 1.162 atatat if (error)
2795 1.162 atatat return (error);
2796 1.162 atatat }
2797 1.162 atatat
2798 1.162 atatat if (szp != NULL)
2799 1.162 atatat *szp = sz;
2800 1.162 atatat
2801 1.162 atatat return (0);
2802 1.162 atatat }
2803