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