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