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