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