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