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