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