1 /* $NetBSD: kern_sysctl.c,v 1.226 2009/09/16 15:23:04 pooka Exp $ */
4 * Copyright (c) 2003, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * Mike Karels at Berkeley Software Design, Inc.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.226 2009/09/16 15:23:04 pooka Exp $");
73 #include "opt_defcorename.h"
76 #include <sys/param.h>
77 #define __COMPAT_SYSCTL
78 #include <sys/sysctl.h>
79 #include <sys/systm.h>
81 #include <sys/ksyms.h>
82 #include <sys/malloc.h>
83 #include <sys/mount.h>
84 #include <sys/syscallargs.h>
85 #include <sys/kauth.h>
86 #include <sys/ktrace.h>
87 #include <machine/stdarg.h>
89 #define MAXDESCLEN 1024
90 MALLOC_DEFINE(M_SYSCTLNODE
, "sysctlnode", "sysctl node structures");
91 MALLOC_DEFINE(M_SYSCTLDATA
, "sysctldata", "misc sysctl data");
93 static int sysctl_mmap(SYSCTLFN_PROTO
);
94 static int sysctl_alloc(struct sysctlnode
*, int);
95 static int sysctl_realloc(struct sysctlnode
*);
97 static int sysctl_cvt_in(struct lwp
*, int *, const void *, size_t,
99 static int sysctl_cvt_out(struct lwp
*, int, const struct sysctlnode
*,
100 void *, size_t, size_t *);
102 static int sysctl_log_add(struct sysctllog
**, const struct sysctlnode
*);
103 static int sysctl_log_realloc(struct sysctllog
*);
105 typedef void (*sysctl_setup_func
)(struct sysctllog
**);
108 const struct sysctlnode
*log_root
;
110 int log_size
, log_left
;
114 * the "root" of the new sysctl tree
116 struct sysctlnode sysctl_root
= {
117 .sysctl_flags
= SYSCTL_VERSION
|
118 CTLFLAG_ROOT
|CTLFLAG_READWRITE
|
122 * XXX once all ports are on gcc3, we can get rid of this
123 * ugliness and simply make it into
125 * .sysctl_size = sizeof(struct sysctlnode),
127 sysc_init_field(_sysctl_size
, sizeof(struct sysctlnode
)),
128 .sysctl_name
= "(root)",
132 * link set of functions that add nodes at boot time (see also
133 * sysctl_buildtree())
135 __link_set_decl(sysctl_funcs
, sysctl_setup_func
);
138 * The `sysctl_treelock' is intended to serialize access to the sysctl
139 * tree. XXX This has serious problems; allocating memory and
140 * copying data out with the lock held is insane.
142 krwlock_t sysctl_treelock
;
145 * Attributes stored in the kernel.
147 char hostname
[MAXHOSTNAMELEN
];
150 char domainname
[MAXHOSTNAMELEN
];
156 #define DEFCORENAME "%n.core"
158 char defcorename
[MAXPATHLEN
] = DEFCORENAME
;
161 * ********************************************************************
162 * Section 0: Some simple glue
163 * ********************************************************************
164 * By wrapping copyin(), copyout(), and copyinstr() like this, we can
165 * stop caring about who's calling us and simplify some code a bunch.
166 * ********************************************************************
169 sysctl_copyin(struct lwp
*l
, const void *uaddr
, void *kaddr
, size_t len
)
174 error
= copyin(uaddr
, kaddr
, len
);
175 ktrmibio(-1, UIO_WRITE
, uaddr
, len
, error
);
177 error
= kcopy(uaddr
, kaddr
, len
);
184 sysctl_copyout(struct lwp
*l
, const void *kaddr
, void *uaddr
, size_t len
)
189 error
= copyout(kaddr
, uaddr
, len
);
190 ktrmibio(-1, UIO_READ
, uaddr
, len
, error
);
192 error
= kcopy(kaddr
, uaddr
, len
);
199 sysctl_copyinstr(struct lwp
*l
, const void *uaddr
, void *kaddr
,
200 size_t len
, size_t *done
)
205 error
= copyinstr(uaddr
, kaddr
, len
, done
);
206 ktrmibio(-1, UIO_WRITE
, uaddr
, len
, error
);
208 error
= copystr(uaddr
, kaddr
, len
, done
);
215 * ********************************************************************
216 * Initialize sysctl subsystem.
217 * ********************************************************************
222 sysctl_setup_func
* const *sysctl_setup
, f
;
223 extern kmutex_t sysctl_file_marker_lock
; /* in init_sysctl.c */
225 rw_init(&sysctl_treelock
);
228 * dynamic mib numbers start here
230 sysctl_root
.sysctl_num
= CREATE_BASE
;
232 __link_set_foreach(sysctl_setup
, sysctl_funcs
) {
234 * XXX - why do i have to coerce the pointers like this?
236 f
= (void*)*sysctl_setup
;
240 mutex_init(&sysctl_file_marker_lock
, MUTEX_DEFAULT
, IPL_NONE
);
244 * Setting this means no more permanent nodes can be added,
245 * trees that claim to be readonly at the root now are, and if
246 * the main tree is readonly, *everything* is.
248 * Call this at the end of kernel init.
251 sysctl_finalize(void)
254 sysctl_root
.sysctl_flags
|= CTLFLAG_PERMANENT
;
258 * ********************************************************************
259 * The main native sysctl system call itself.
260 * ********************************************************************
263 sys___sysctl(struct lwp
*l
, const struct sys___sysctl_args
*uap
, register_t
*retval
)
266 syscallarg(const int *) name;
267 syscallarg(u_int) namelen;
268 syscallarg(void *) old;
269 syscallarg(size_t *) oldlenp;
270 syscallarg(const void *) new;
271 syscallarg(size_t) newlen;
273 int error
, nerror
, name
[CTL_MAXNAME
];
274 size_t oldlen
, savelen
, *oldlenp
;
280 oldlenp
= SCARG(uap
, oldlenp
);
281 if (oldlenp
!= NULL
) {
282 error
= copyin(oldlenp
, &oldlen
, sizeof(oldlen
));
289 * top-level sysctl names may or may not be non-terminal, but
292 if (SCARG(uap
, namelen
) > CTL_MAXNAME
|| SCARG(uap
, namelen
) < 1)
294 error
= copyin(SCARG(uap
, name
), &name
,
295 SCARG(uap
, namelen
) * sizeof(int));
299 ktrmib(name
, SCARG(uap
, namelen
));
301 sysctl_lock(SCARG(uap
, new) != NULL
);
304 * do sysctl work (NULL means main built-in default tree)
306 error
= sysctl_dispatch(&name
[0], SCARG(uap
, namelen
),
307 SCARG(uap
, old
), &oldlen
,
308 SCARG(uap
, new), SCARG(uap
, newlen
),
312 * release the sysctl lock
317 * set caller's oldlen to new value even in the face of an
318 * error (if this gets an error and they didn't have one, they
322 nerror
= copyout(&oldlen
, oldlenp
, sizeof(oldlen
));
328 * if the only problem is that we weren't given enough space,
329 * that's an ENOMEM error
331 if (error
== 0 && SCARG(uap
, old
) != NULL
&& savelen
< oldlen
)
338 * ********************************************************************
339 * Section 1: How the tree is used
340 * ********************************************************************
341 * Implementations of sysctl for emulations should typically need only
342 * these three functions in this order: lock the tree, dispatch
343 * request into it, unlock the tree.
344 * ********************************************************************
347 sysctl_lock(bool write
)
351 rw_enter(&sysctl_treelock
, RW_WRITER
);
352 curlwp
->l_pflag
|= LP_SYSCTLWRITE
;
354 rw_enter(&sysctl_treelock
, RW_READER
);
355 curlwp
->l_pflag
&= ~LP_SYSCTLWRITE
;
363 if ((curlwp
->l_pflag
& LP_SYSCTLWRITE
) != 0) {
364 rw_enter(&sysctl_treelock
, RW_WRITER
);
366 rw_enter(&sysctl_treelock
, RW_READER
);
371 * ********************************************************************
372 * the main sysctl dispatch routine. scans the given tree and picks a
373 * function to call based on what it finds.
374 * ********************************************************************
377 sysctl_dispatch(SYSCTLFN_ARGS
)
383 KASSERT(rw_lock_held(&sysctl_treelock
));
385 if (rnode
&& SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
386 printf("sysctl_dispatch: rnode %p wrong version\n", rnode
);
392 error
= sysctl_locate(l
, name
, namelen
, &rnode
, &ni
);
394 if (rnode
->sysctl_func
!= NULL
) {
396 * the node we ended up at has a function, so call it. it can
397 * hand off to query or create if it wants to.
399 fn
= rnode
->sysctl_func
;
400 } else if (error
== 0) {
402 * we found the node they were looking for, so do a lookup.
404 fn
= (sysctlfn
)sysctl_lookup
; /* XXX may write to rnode */
405 } else if (error
== ENOENT
&& (ni
+ 1) == namelen
&& name
[ni
] < 0) {
407 * prospective parent node found, but the terminal node was
408 * not. generic operations associate with the parent.
417 #endif /* NKSYMS > 0 */
422 KASSERT(rw_write_held(&sysctl_treelock
));
423 fn
= (sysctlfn
)sysctl_create
; /* we own the rnode */
430 KASSERT(rw_write_held(&sysctl_treelock
));
431 fn
= (sysctlfn
)sysctl_destroy
; /* we own the rnode */
434 fn
= (sysctlfn
)sysctl_mmap
; /* we own the rnode */
437 fn
= sysctl_describe
;
446 * after all of that, maybe we found someone who knows how to
447 * get us what we want?
450 error
= (*fn
)(name
+ ni
, namelen
- ni
, oldp
, oldlenp
,
451 newp
, newlen
, name
, l
, rnode
);
460 * ********************************************************************
461 * Releases the tree lock.
462 * ********************************************************************
468 rw_exit(&sysctl_treelock
);
472 * ********************************************************************
473 * Section 2: The main tree interfaces
474 * ********************************************************************
475 * This is how sysctl_dispatch() does its work, and you can too, by
476 * calling these routines from helpers (though typically only
477 * sysctl_lookup() will be used). The tree MUST BE LOCKED when these
479 * ********************************************************************
483 * sysctl_locate -- Finds the node matching the given mib under the
484 * given tree (via rv). If no tree is given, we fall back to the
485 * native tree. The current process (via l) is used for access
486 * control on the tree (some nodes may be traversable only by root) and
487 * on return, nip will show how many numbers in the mib were consumed.
490 sysctl_locate(struct lwp
*l
, const int *name
, u_int namelen
,
491 const struct sysctlnode
**rnode
, int *nip
)
493 const struct sysctlnode
*node
, *pnode
;
494 int tn
, si
, ni
, error
, alias
;
496 KASSERT(rw_lock_held(&sysctl_treelock
));
499 * basic checks and setup
502 *rnode
= &sysctl_root
;
509 * search starts from "root"
512 if (SYSCTL_VERS(pnode
->sysctl_flags
) != SYSCTL_VERSION
) {
513 printf("sysctl_locate: pnode %p wrong version\n", pnode
);
516 node
= pnode
->sysctl_child
;
520 * scan for node to which new node should be attached
522 for (ni
= 0; ni
< namelen
; ni
++) {
524 * walked off bottom of tree
527 if (SYSCTL_TYPE(pnode
->sysctl_flags
) == CTLTYPE_NODE
)
534 * can anyone traverse this node or only root?
536 if (l
!= NULL
&& (pnode
->sysctl_flags
& CTLFLAG_PRIVATE
) &&
537 (error
= kauth_authorize_system(l
->l_cred
,
538 KAUTH_SYSTEM_SYSCTL
, KAUTH_REQ_SYSTEM_SYSCTL_PRVT
,
539 NULL
, NULL
, NULL
)) != 0)
542 * find a child node with the right number
549 * Note: ANYNUMBER only matches positive integers.
550 * Since ANYNUMBER is only permitted on single-node
551 * sub-trees (eg proc), check before the loop and skip
554 if ((node
[si
].sysctl_flags
& CTLFLAG_ANYNUMBER
) && (tn
>= 0))
556 for (; si
< pnode
->sysctl_clen
; si
++) {
557 if (node
[si
].sysctl_num
== tn
) {
558 if (node
[si
].sysctl_flags
& CTLFLAG_ALIAS
) {
562 tn
= node
[si
].sysctl_alias
;
570 * if we ran off the end, it obviously doesn't exist
576 * so far so good, move on down the line
580 if (SYSCTL_TYPE(pnode
->sysctl_flags
) == CTLTYPE_NODE
)
581 node
= node
[si
].sysctl_child
;
594 * sysctl_query -- The auto-discovery engine. Copies out the structs
595 * describing nodes under the given node and handles overlay trees.
598 sysctl_query(SYSCTLFN_ARGS
)
600 int error
, ni
, elim
, v
;
602 const struct sysctlnode
*enode
, *onode
;
603 struct sysctlnode qnode
;
605 KASSERT(rw_lock_held(&sysctl_treelock
));
607 if (SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
608 printf("sysctl_query: rnode %p wrong version\n", rnode
);
612 if (SYSCTL_TYPE(rnode
->sysctl_flags
) != CTLTYPE_NODE
)
614 if (namelen
!= 1 || name
[0] != CTL_QUERY
)
624 * translate the given request to a current node
626 error
= sysctl_cvt_in(l
, &v
, newp
, newlen
, &qnode
);
631 * if the request specifies a version, check it
633 if (qnode
.sysctl_ver
!= 0) {
635 if (qnode
.sysctl_ver
!= enode
->sysctl_ver
&&
636 qnode
.sysctl_ver
!= sysctl_rootof(enode
)->sysctl_ver
)
641 * process has overlay tree
643 if (l
&& l
->l_proc
->p_emul
->e_sysctlovly
) {
644 enode
= l
->l_proc
->p_emul
->e_sysctlovly
;
645 elim
= (name
- oname
);
646 error
= sysctl_locate(l
, oname
, elim
, &enode
, NULL
);
648 /* ah, found parent in overlay */
649 elim
= enode
->sysctl_clen
;
650 enode
= enode
->sysctl_child
;
658 for (ni
= 0; ni
< rnode
->sysctl_clen
; ni
++) {
659 onode
= &rnode
->sysctl_child
[ni
];
660 if (enode
&& enode
->sysctl_num
== onode
->sysctl_num
) {
661 if (SYSCTL_TYPE(enode
->sysctl_flags
) != CTLTYPE_NODE
)
668 error
= sysctl_cvt_out(l
, v
, onode
, oldp
, left
, &t
);
672 oldp
= (char*)oldp
+ t
;
674 left
-= MIN(left
, t
);
678 * overlay trees *MUST* be entirely consumed
680 KASSERT(enode
== NULL
);
688 * sysctl_create -- Adds a node (the description of which is taken
689 * from newp) to the tree, returning a copy of it in the space pointed
690 * to by oldp. In the event that the requested slot is already taken
691 * (either by name or by number), the offending node is returned
692 * instead. Yes, this is complex, but we want to make sure everything
695 #ifdef SYSCTL_DEBUG_CREATE
696 int _sysctl_create(SYSCTLFN_ARGS
);
698 _sysctl_create(SYSCTLFN_ARGS
)
701 sysctl_create(SYSCTLFN_ARGS
)
704 struct sysctlnode nnode
, *node
, *pnode
;
705 int error
, ni
, at
, nm
, type
, nsz
, sz
, flags
, anum
, v
;
708 KASSERT(rw_write_held(&sysctl_treelock
));
714 if (SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
715 printf("sysctl_create: rnode %p wrong version\n", rnode
);
719 if (namelen
!= 1 || (name
[namelen
- 1] != CTL_CREATE
721 && name
[namelen
- 1] != CTL_CREATESYM
722 #endif /* NKSYMS > 0 */
727 * processes can only add nodes at securelevel 0, must be
728 * root, and can't add nodes to a parent that's not writeable
731 #ifndef SYSCTL_DISALLOW_CREATE
732 error
= kauth_authorize_system(l
->l_cred
, KAUTH_SYSTEM_SYSCTL
,
733 KAUTH_REQ_SYSTEM_SYSCTL_ADD
, NULL
, NULL
, NULL
);
736 if (!(rnode
->sysctl_flags
& CTLFLAG_READWRITE
))
737 #endif /* SYSCTL_DISALLOW_CREATE */
742 * nothing can add a node if:
743 * we've finished initial set up of this tree and
744 * (the tree itself is not writeable or
745 * the entire sysctl system is not writeable)
747 if ((sysctl_rootof(rnode
)->sysctl_flags
& CTLFLAG_PERMANENT
) &&
748 (!(sysctl_rootof(rnode
)->sysctl_flags
& CTLFLAG_READWRITE
) ||
749 !(sysctl_root
.sysctl_flags
& CTLFLAG_READWRITE
)))
753 * it must be a "node", not a "int" or something
755 if (SYSCTL_TYPE(rnode
->sysctl_flags
) != CTLTYPE_NODE
)
757 if (rnode
->sysctl_flags
& CTLFLAG_ALIAS
) {
758 printf("sysctl_create: attempt to add node to aliased "
762 pnode
= __UNCONST(rnode
); /* we are adding children to this node */
766 error
= sysctl_cvt_in(l
, &v
, newp
, newlen
, &nnode
);
771 * nodes passed in don't *have* parents
773 if (nnode
.sysctl_parent
!= NULL
)
777 * if we are indeed adding it, it should be a "good" name and
780 nm
= nnode
.sysctl_num
;
782 if (nm
== CTL_CREATESYM
)
784 #endif /* NKSYMS > 0 */
785 if (nm
< 0 && nm
!= CTL_CREATE
)
789 * the name can't start with a digit
791 if (nnode
.sysctl_name
[0] >= '0' &&
792 nnode
.sysctl_name
[0] <= '9')
796 * the name must be only alphanumerics or - or _, longer than
797 * 0 bytes and less that SYSCTL_NAMELEN
800 while (nsz
< SYSCTL_NAMELEN
&& nnode
.sysctl_name
[nsz
] != '\0') {
801 if ((nnode
.sysctl_name
[nsz
] >= '0' &&
802 nnode
.sysctl_name
[nsz
] <= '9') ||
803 (nnode
.sysctl_name
[nsz
] >= 'A' &&
804 nnode
.sysctl_name
[nsz
] <= 'Z') ||
805 (nnode
.sysctl_name
[nsz
] >= 'a' &&
806 nnode
.sysctl_name
[nsz
] <= 'z') ||
807 nnode
.sysctl_name
[nsz
] == '-' ||
808 nnode
.sysctl_name
[nsz
] == '_')
813 if (nsz
== 0 || nsz
== SYSCTL_NAMELEN
)
817 * various checks revolve around size vs type, etc
819 type
= SYSCTL_TYPE(nnode
.sysctl_flags
);
820 flags
= SYSCTL_FLAGS(nnode
.sysctl_flags
);
821 sz
= nnode
.sysctl_size
;
824 * find out if there's a collision, and if so, let the caller
825 * know what they collided with
827 node
= pnode
->sysctl_child
;
830 if ((flags
| node
->sysctl_flags
) & CTLFLAG_ANYNUMBER
)
831 /* No siblings for a CTLFLAG_ANYNUMBER node */
833 for (ni
= 0; ni
< pnode
->sysctl_clen
; ni
++) {
834 if (nm
== node
[ni
].sysctl_num
||
835 strcmp(nnode
.sysctl_name
, node
[ni
].sysctl_name
) == 0) {
837 * ignore error here, since we
838 * are already fixed on EEXIST
840 (void)sysctl_cvt_out(l
, v
, &node
[ni
], oldp
,
844 if (nm
> node
[ni
].sysctl_num
)
850 * use sysctl_ver to add to the tree iff it hasn't changed
852 if (nnode
.sysctl_ver
!= 0) {
854 * a specified value must match either the parent
855 * node's version or the root node's version
857 if (nnode
.sysctl_ver
!= sysctl_rootof(rnode
)->sysctl_ver
&&
858 nnode
.sysctl_ver
!= rnode
->sysctl_ver
) {
864 * only the kernel can assign functions to entries
866 if (l
!= NULL
&& nnode
.sysctl_func
!= NULL
)
870 * only the kernel can create permanent entries, and only then
871 * before the kernel is finished setting itself up
873 if (l
!= NULL
&& (flags
& ~SYSCTL_USERFLAGS
))
875 if ((flags
& CTLFLAG_PERMANENT
) &
876 (sysctl_root
.sysctl_flags
& CTLFLAG_PERMANENT
))
878 if ((flags
& (CTLFLAG_OWNDATA
| CTLFLAG_IMMEDIATE
)) ==
879 (CTLFLAG_OWNDATA
| CTLFLAG_IMMEDIATE
))
881 if ((flags
& CTLFLAG_IMMEDIATE
) &&
882 type
!= CTLTYPE_INT
&& type
!= CTLTYPE_QUAD
&& type
!= CTLTYPE_BOOL
)
886 * check size, or set it if unset and we can figure it out.
887 * kernel created nodes are allowed to have a function instead
888 * of a size (or a data pointer).
893 * only *i* can assert the size of a node
895 if (flags
& CTLFLAG_ALIAS
) {
896 anum
= nnode
.sysctl_alias
;
899 nnode
.sysctl_alias
= 0;
901 if (sz
!= 0 || nnode
.sysctl_data
!= NULL
)
903 if (nnode
.sysctl_csize
!= 0 ||
904 nnode
.sysctl_clen
!= 0 ||
905 nnode
.sysctl_child
!= 0)
907 if (flags
& CTLFLAG_OWNDATA
)
909 sz
= sizeof(struct sysctlnode
);
913 * since an int is an int, if the size is not given or
914 * is wrong, we can "int-uit" it.
916 if (sz
!= 0 && sz
!= sizeof(int))
922 * strings are a little more tricky
926 if (nnode
.sysctl_func
== NULL
) {
927 if (nnode
.sysctl_data
== NULL
)
930 sz
= strlen(nnode
.sysctl_data
) +
933 } else if (nnode
.sysctl_data
== NULL
&&
934 flags
& CTLFLAG_OWNDATA
) {
941 * we want a rough idea of what the
944 vp
= malloc(PAGE_SIZE
, M_SYSCTLDATA
,
948 e
= nnode
.sysctl_data
;
950 error
= copyinstr(e
, vp
, PAGE_SIZE
, &s
);
952 if (error
!= ENAMETOOLONG
) {
953 free(vp
, M_SYSCTLDATA
);
957 if ((e
- 32 * PAGE_SIZE
) >
958 (char*)nnode
.sysctl_data
) {
959 free(vp
, M_SYSCTLDATA
);
963 } while (error
!= 0);
964 sz
= s
+ (e
- (char*)nnode
.sysctl_data
);
965 free(vp
, M_SYSCTLDATA
);
970 if (sz
!= 0 && sz
!= sizeof(u_quad_t
))
972 sz
= sizeof(u_quad_t
);
976 * since an bool is an bool, if the size is not given or
977 * is wrong, we can "intuit" it.
979 if (sz
!= 0 && sz
!= sizeof(bool))
985 if (l
!= NULL
|| nnode
.sysctl_func
== NULL
)
987 if (flags
& CTLFLAG_OWNDATA
)
996 * at this point, if sz is zero, we *must* have a
997 * function to go with it and we can't own it.
1002 * 0 0 0 -> EINVAL (if no func)
1008 * 1 1 0 -> kptr, no own (fault on lookup)
1009 * 1 1 1 -> uptr, own
1011 if (type
!= CTLTYPE_NODE
) {
1013 if (flags
& CTLFLAG_OWNDATA
) {
1014 own
= malloc(sz
, M_SYSCTLDATA
,
1015 M_WAITOK
|M_CANFAIL
);
1018 if (nnode
.sysctl_data
== NULL
)
1021 error
= sysctl_copyin(l
,
1022 nnode
.sysctl_data
, own
, sz
);
1024 free(own
, M_SYSCTLDATA
);
1028 } else if ((nnode
.sysctl_data
!= NULL
) &&
1029 !(flags
& CTLFLAG_IMMEDIATE
)) {
1031 if (name
[namelen
- 1] == CTL_CREATESYM
) {
1032 char symname
[128]; /* XXX enough? */
1036 error
= sysctl_copyinstr(l
,
1037 nnode
.sysctl_data
, symname
,
1038 sizeof(symname
), &symlen
);
1041 error
= ksyms_getval(NULL
, symname
,
1042 &symaddr
, KSYMS_EXTERN
);
1044 return (error
); /* EINVAL? */
1045 nnode
.sysctl_data
= (void*)symaddr
;
1047 #endif /* NKSYMS > 0 */
1049 * Ideally, we'd like to verify here
1050 * that this address is acceptable,
1053 * - it might be valid now, only to
1054 * become invalid later
1056 * - it might be invalid only for the
1057 * moment and valid later
1059 * - or something else.
1061 * Since we can't get a good answer,
1062 * we'll just accept the address as
1063 * given, and fault on individual
1067 } else if (nnode
.sysctl_func
== NULL
)
1072 * a process can't assign a function to a node, and the kernel
1073 * can't create a node that has no function or data.
1074 * (XXX somewhat redundant check)
1076 if (l
!= NULL
|| nnode
.sysctl_func
== NULL
) {
1077 if (type
!= CTLTYPE_NODE
&&
1078 nnode
.sysctl_data
== NULL
&&
1079 !(flags
& CTLFLAG_IMMEDIATE
) &&
1084 #ifdef SYSCTL_DISALLOW_KWRITE
1086 * a process can't create a writable node unless it refers to
1089 if (l
!= NULL
&& own
== NULL
&& type
!= CTLTYPE_NODE
&&
1090 (flags
& CTLFLAG_READWRITE
) != CTLFLAG_READONLY
&&
1091 !(flags
& CTLFLAG_IMMEDIATE
))
1093 #endif /* SYSCTL_DISALLOW_KWRITE */
1096 * make sure there's somewhere to put the new stuff.
1098 if (pnode
->sysctl_child
== NULL
) {
1099 if (flags
& CTLFLAG_ANYNUMBER
)
1100 error
= sysctl_alloc(pnode
, 1);
1102 error
= sysctl_alloc(pnode
, 0);
1105 free(own
, M_SYSCTLDATA
);
1109 node
= pnode
->sysctl_child
;
1112 * no collisions, so pick a good dynamic number if we need to.
1114 if (nm
== CTL_CREATE
) {
1115 nm
= ++sysctl_root
.sysctl_num
;
1116 for (ni
= 0; ni
< pnode
->sysctl_clen
; ni
++) {
1117 if (nm
== node
[ni
].sysctl_num
) {
1120 } else if (nm
> node
[ni
].sysctl_num
)
1126 * oops...ran out of space
1128 if (pnode
->sysctl_clen
== pnode
->sysctl_csize
) {
1129 error
= sysctl_realloc(pnode
);
1132 free(own
, M_SYSCTLDATA
);
1135 node
= pnode
->sysctl_child
;
1139 * insert new node data
1141 if (at
< pnode
->sysctl_clen
) {
1145 * move the nodes that should come after the new one
1147 memmove(&node
[at
+ 1], &node
[at
],
1148 (pnode
->sysctl_clen
- at
) * sizeof(struct sysctlnode
));
1149 memset(&node
[at
], 0, sizeof(struct sysctlnode
));
1150 node
[at
].sysctl_parent
= pnode
;
1152 * and...reparent any children of any moved nodes
1154 for (ni
= at
; ni
<= pnode
->sysctl_clen
; ni
++)
1155 if (node
[ni
].sysctl_child
!= NULL
)
1156 for (t
= 0; t
< node
[ni
].sysctl_csize
; t
++)
1157 node
[ni
].sysctl_child
[t
].sysctl_parent
=
1161 pnode
->sysctl_clen
++;
1163 strlcpy(node
->sysctl_name
, nnode
.sysctl_name
,
1164 sizeof(node
->sysctl_name
));
1165 node
->sysctl_num
= nm
;
1166 node
->sysctl_size
= sz
;
1167 node
->sysctl_flags
= SYSCTL_VERSION
|type
|flags
; /* XXX other trees */
1168 node
->sysctl_csize
= 0;
1169 node
->sysctl_clen
= 0;
1171 node
->sysctl_data
= own
;
1172 node
->sysctl_flags
|= CTLFLAG_OWNDATA
;
1173 } else if (flags
& CTLFLAG_ALIAS
) {
1174 node
->sysctl_alias
= anum
;
1175 } else if (flags
& CTLFLAG_IMMEDIATE
) {
1178 node
->sysctl_idata
= nnode
.sysctl_bdata
;
1181 node
->sysctl_idata
= nnode
.sysctl_idata
;
1184 node
->sysctl_qdata
= nnode
.sysctl_qdata
;
1188 node
->sysctl_data
= nnode
.sysctl_data
;
1189 node
->sysctl_flags
&= ~CTLFLAG_OWNDATA
;
1191 node
->sysctl_func
= nnode
.sysctl_func
;
1192 node
->sysctl_child
= NULL
;
1193 /* node->sysctl_parent should already be done */
1196 * update "version" on path to "root"
1198 for (; rnode
->sysctl_parent
!= NULL
; rnode
= rnode
->sysctl_parent
)
1201 for (nm
= rnode
->sysctl_ver
+ 1; pnode
!= NULL
;
1202 pnode
= pnode
->sysctl_parent
)
1203 pnode
->sysctl_ver
= nm
;
1205 /* If this fails, the node is already added - the user won't know! */
1206 error
= sysctl_cvt_out(l
, v
, node
, oldp
, *oldlenp
, oldlenp
);
1212 * ********************************************************************
1213 * A wrapper around sysctl_create() that prints the thing we're trying
1215 * ********************************************************************
1217 #ifdef SYSCTL_DEBUG_CREATE
1219 sysctl_create(SYSCTLFN_ARGS
)
1221 const struct sysctlnode
*node
;
1222 int k
, rc
, ni
, nl
= namelen
+ (name
- oname
);
1226 printf("namelen %d (", nl
);
1227 for (ni
= 0; ni
< nl
- 1; ni
++)
1228 printf(" %d", oname
[ni
]);
1229 printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n",
1230 k
= node
->sysctl_num
,
1233 SYSCTL_FLAGS(node
->sysctl_flags
),
1234 SYSCTL_TYPE(node
->sysctl_flags
),
1238 rc
= _sysctl_create(SYSCTLFN_CALL(rnode
));
1240 printf("sysctl_create(");
1241 for (ni
= 0; ni
< nl
- 1; ni
++)
1242 printf(" %d", oname
[ni
]);
1243 printf(" %d ) returned %d\n", k
, rc
);
1247 #endif /* SYSCTL_DEBUG_CREATE */
1250 * sysctl_destroy -- Removes a node (as described by newp) from the
1251 * given tree, returning (if successful) a copy of the dead node in
1252 * oldp. Since we're removing stuff, there's not much to check.
1255 sysctl_destroy(SYSCTLFN_ARGS
)
1257 struct sysctlnode
*node
, *pnode
, onode
, nnode
;
1260 KASSERT(rw_write_held(&sysctl_treelock
));
1262 if (SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
1263 printf("sysctl_destroy: rnode %p wrong version\n", rnode
);
1269 if (namelen
!= 1 || name
[namelen
- 1] != CTL_DESTROY
)
1273 * processes can only destroy nodes at securelevel 0, must be
1274 * root, and can't remove nodes from a parent that's not
1278 #ifndef SYSCTL_DISALLOW_CREATE
1279 error
= kauth_authorize_system(l
->l_cred
, KAUTH_SYSTEM_SYSCTL
,
1280 KAUTH_REQ_SYSTEM_SYSCTL_DELETE
, NULL
, NULL
, NULL
);
1283 if (!(rnode
->sysctl_flags
& CTLFLAG_READWRITE
))
1284 #endif /* SYSCTL_DISALLOW_CREATE */
1289 * nothing can remove a node if:
1290 * the node is permanent (checked later) or
1291 * the tree itself is not writeable or
1292 * the entire sysctl system is not writeable
1294 * note that we ignore whether setup is complete or not,
1295 * because these rules always apply.
1297 if (!(sysctl_rootof(rnode
)->sysctl_flags
& CTLFLAG_READWRITE
) ||
1298 !(sysctl_root
.sysctl_flags
& CTLFLAG_READWRITE
))
1303 error
= sysctl_cvt_in(l
, &v
, newp
, newlen
, &nnode
);
1306 memset(&onode
, 0, sizeof(struct sysctlnode
));
1308 node
= rnode
->sysctl_child
;
1309 for (ni
= 0; ni
< rnode
->sysctl_clen
; ni
++) {
1310 if (nnode
.sysctl_num
== node
[ni
].sysctl_num
) {
1312 * if name specified, must match
1314 if (nnode
.sysctl_name
[0] != '\0' &&
1315 strcmp(nnode
.sysctl_name
, node
[ni
].sysctl_name
))
1318 * if version specified, must match
1320 if (nnode
.sysctl_ver
!= 0 &&
1321 nnode
.sysctl_ver
!= node
[ni
].sysctl_ver
)
1324 * this must be the one
1329 if (ni
== rnode
->sysctl_clen
)
1332 pnode
= node
->sysctl_parent
;
1335 * if the kernel says permanent, it is, so there. nyah.
1337 if (SYSCTL_FLAGS(node
->sysctl_flags
) & CTLFLAG_PERMANENT
)
1341 * can't delete non-empty nodes
1343 if (SYSCTL_TYPE(node
->sysctl_flags
) == CTLTYPE_NODE
&&
1344 node
->sysctl_clen
!= 0)
1348 * if the node "owns" data, release it now
1350 if (node
->sysctl_flags
& CTLFLAG_OWNDATA
) {
1351 if (node
->sysctl_data
!= NULL
)
1352 free(node
->sysctl_data
, M_SYSCTLDATA
);
1353 node
->sysctl_data
= NULL
;
1355 if (node
->sysctl_flags
& CTLFLAG_OWNDESC
) {
1356 if (node
->sysctl_desc
!= NULL
)
1358 free(__UNCONST(node
->sysctl_desc
), M_SYSCTLDATA
);
1359 node
->sysctl_desc
= NULL
;
1363 * if the node to be removed is not the last one on the list,
1364 * move the remaining nodes up, and reparent any grandchildren
1367 if (ni
< pnode
->sysctl_clen
- 1) {
1370 memmove(&pnode
->sysctl_child
[ni
], &pnode
->sysctl_child
[ni
+ 1],
1371 (pnode
->sysctl_clen
- ni
- 1) *
1372 sizeof(struct sysctlnode
));
1373 for (; ni
< pnode
->sysctl_clen
- 1; ni
++)
1374 if (SYSCTL_TYPE(pnode
->sysctl_child
[ni
].sysctl_flags
) ==
1377 t
< pnode
->sysctl_child
[ni
].sysctl_clen
;
1379 pnode
->sysctl_child
[ni
].sysctl_child
[t
].
1381 &pnode
->sysctl_child
[ni
];
1382 ni
= pnode
->sysctl_clen
- 1;
1383 node
= &pnode
->sysctl_child
[ni
];
1387 * reset the space we just vacated
1389 memset(node
, 0, sizeof(struct sysctlnode
));
1390 node
->sysctl_parent
= pnode
;
1391 pnode
->sysctl_clen
--;
1394 * if this parent just lost its last child, nuke the creche
1396 if (pnode
->sysctl_clen
== 0) {
1397 free(pnode
->sysctl_child
, M_SYSCTLNODE
);
1398 pnode
->sysctl_csize
= 0;
1399 pnode
->sysctl_child
= NULL
;
1403 * update "version" on path to "root"
1405 for (; rnode
->sysctl_parent
!= NULL
; rnode
= rnode
->sysctl_parent
)
1407 for (ni
= rnode
->sysctl_ver
+ 1; pnode
!= NULL
;
1408 pnode
= pnode
->sysctl_parent
)
1409 pnode
->sysctl_ver
= ni
;
1411 error
= sysctl_cvt_out(l
, v
, &onode
, oldp
, *oldlenp
, oldlenp
);
1417 * sysctl_lookup -- Handles copyin/copyout of new and old values.
1418 * Partial reads are globally allowed. Only root can write to things
1419 * unless the node says otherwise.
1422 sysctl_lookup(SYSCTLFN_ARGS
)
1428 KASSERT(rw_lock_held(&sysctl_treelock
));
1430 if (SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
1431 printf("sysctl_lookup: rnode %p wrong version\n", rnode
);
1438 * you can't "look up" a node. you can "query" it, but you
1439 * can't "look it up".
1441 if (SYSCTL_TYPE(rnode
->sysctl_flags
) == CTLTYPE_NODE
|| namelen
!= 0)
1445 * some nodes are private, so only root can look into them.
1447 if (l
!= NULL
&& (rnode
->sysctl_flags
& CTLFLAG_PRIVATE
) &&
1448 (error
= kauth_authorize_system(l
->l_cred
, KAUTH_SYSTEM_SYSCTL
,
1449 KAUTH_REQ_SYSTEM_SYSCTL_PRVT
, NULL
, NULL
, NULL
)) != 0)
1453 * if a node wants to be writable according to different rules
1454 * other than "only root can write to stuff unless a flag is
1455 * set", then it needs its own function which should have been
1456 * called and not us.
1458 if (l
!= NULL
&& newp
!= NULL
&&
1459 !(rnode
->sysctl_flags
& CTLFLAG_ANYWRITE
) &&
1460 (error
= kauth_authorize_system(l
->l_cred
,
1461 KAUTH_SYSTEM_SYSCTL
, KAUTH_REQ_SYSTEM_SYSCTL_MODIFY
, NULL
, NULL
,
1466 * is this node supposedly writable?
1468 rw
= (rnode
->sysctl_flags
& CTLFLAG_READWRITE
) ? 1 : 0;
1471 * it appears not to be writable at this time, so if someone
1472 * tried to write to it, we must tell them to go away
1474 if (!rw
&& newp
!= NULL
)
1478 * step one, copy out the stuff we have presently
1480 if (rnode
->sysctl_flags
& CTLFLAG_IMMEDIATE
) {
1482 * note that we discard const here because we are
1483 * modifying the contents of the node (which is okay
1484 * because it's ours)
1486 switch (SYSCTL_TYPE(rnode
->sysctl_flags
)) {
1488 d
= __UNCONST(&rnode
->sysctl_bdata
);
1491 d
= __UNCONST(&rnode
->sysctl_idata
);
1494 d
= __UNCONST(&rnode
->sysctl_qdata
);
1500 d
= rnode
->sysctl_data
;
1501 if (SYSCTL_TYPE(rnode
->sysctl_flags
) == CTLTYPE_STRING
)
1502 sz
= strlen(d
) + 1; /* XXX@@@ possible fault here */
1504 sz
= rnode
->sysctl_size
;
1506 error
= sysctl_copyout(l
, d
, oldp
, MIN(sz
, *oldlenp
));
1514 if (newp
== NULL
|| newlen
== 0)
1518 * hmm...not done. must now "copy in" new value. re-adjust
1519 * sz to maximum value (strings are "weird").
1521 sz
= rnode
->sysctl_size
;
1522 switch (SYSCTL_TYPE(rnode
->sysctl_flags
)) {
1523 case CTLTYPE_BOOL
: {
1526 * these data must be *exactly* the same size coming
1527 * in. bool may only be true or false.
1531 error
= sysctl_copyin(l
, newp
, &tmp
, sz
);
1539 case CTLTYPE_STRUCT
:
1541 * these data must be *exactly* the same size coming
1546 error
= sysctl_copyin(l
, newp
, d
, sz
);
1548 case CTLTYPE_STRING
: {
1550 * strings, on the other hand, can be shorter, and we
1551 * let userland be sloppy about the trailing nul.
1556 * too much new string?
1562 * temporary copy of new inbound string
1564 len
= MIN(sz
, newlen
);
1565 newbuf
= malloc(len
, M_SYSCTLDATA
, M_WAITOK
|M_CANFAIL
);
1568 error
= sysctl_copyin(l
, newp
, newbuf
, len
);
1570 free(newbuf
, M_SYSCTLDATA
);
1575 * did they null terminate it, or do we have space
1576 * left to do it ourselves?
1578 if (newbuf
[len
- 1] != '\0' && len
== sz
) {
1579 free(newbuf
, M_SYSCTLDATA
);
1584 * looks good, so pop it into place and zero the rest.
1587 memcpy(d
, newbuf
, len
);
1589 memset((char*)d
+ len
, 0, sz
- len
);
1590 free(newbuf
, M_SYSCTLDATA
);
1601 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that
1602 * purport to handle it. This interface isn't fully fleshed out yet,
1606 sysctl_mmap(SYSCTLFN_ARGS
)
1608 const struct sysctlnode
*node
;
1609 struct sysctlnode nnode
;
1612 if (SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
1613 printf("sysctl_mmap: rnode %p wrong version\n", rnode
);
1618 * let's just pretend that didn't happen, m'kay?
1624 * is this a sysctlnode description of an mmap request?
1626 if (newp
== NULL
|| newlen
!= sizeof(struct sysctlnode
))
1628 error
= sysctl_copyin(l
, newp
, &nnode
, sizeof(nnode
));
1633 * does the node they asked for exist?
1636 return (EOPNOTSUPP
);
1638 error
= sysctl_locate(l
, &nnode
.sysctl_num
, 1, &node
, NULL
);
1643 * does this node that we have found purport to handle mmap?
1645 if (node
->sysctl_func
== NULL
||
1646 !(node
->sysctl_flags
& CTLFLAG_MMAP
))
1647 return (EOPNOTSUPP
);
1650 * well...okay, they asked for it.
1652 return ((*node
->sysctl_func
)(SYSCTLFN_CALL(node
)));
1656 sysctl_describe(SYSCTLFN_ARGS
)
1658 struct sysctldesc
*d
;
1660 size_t sz
, left
, tot
;
1661 int i
, error
, v
= -1;
1662 struct sysctlnode
*node
;
1663 struct sysctlnode dnode
;
1665 if (SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
1666 printf("sysctl_query: rnode %p wrong version\n", rnode
);
1670 if (SYSCTL_TYPE(rnode
->sysctl_flags
) != CTLTYPE_NODE
)
1672 if (namelen
!= 1 || name
[0] != CTL_DESCRIBE
)
1679 d
= bf
= malloc(MAXDESCLEN
, M_TEMP
, M_WAITOK
|M_CANFAIL
);
1683 node
= rnode
->sysctl_child
;
1687 * no request -> all descriptions at this level
1688 * request with desc unset -> just this node
1689 * request with desc set -> set descr for this node
1692 error
= sysctl_cvt_in(l
, &v
, newp
, newlen
, &dnode
);
1695 if (dnode
.sysctl_desc
!= NULL
) {
1697 * processes cannot set descriptions above
1698 * securelevel 0. and must be root. blah
1699 * blah blah. a couple more checks are made
1700 * once we find the node we want.
1703 #ifndef SYSCTL_DISALLOW_CREATE
1704 error
= kauth_authorize_system(l
->l_cred
,
1705 KAUTH_SYSTEM_SYSCTL
,
1706 KAUTH_REQ_SYSTEM_SYSCTL_DESC
, NULL
,
1710 #else /* SYSCTL_DISALLOW_CREATE */
1713 #endif /* SYSCTL_DISALLOW_CREATE */
1717 * find node and try to set the description on it
1719 for (i
= 0; i
< rnode
->sysctl_clen
; i
++)
1720 if (node
[i
].sysctl_num
== dnode
.sysctl_num
)
1722 if (i
== rnode
->sysctl_clen
) {
1729 * did the caller specify a node version?
1731 if (dnode
.sysctl_ver
!= 0 &&
1732 dnode
.sysctl_ver
!= node
->sysctl_ver
) {
1738 * okay...some rules:
1739 * (1) if setup is done and the tree is
1740 * read-only or the whole system is
1742 * (2) no one can set a description on a
1743 * permanent node (it must be set when
1745 * (3) processes cannot *change* a description
1746 * (4) processes *can*, however, set a
1747 * description on a read-only node so that
1748 * one can be created and then described
1750 * anything else come to mind?
1752 if ((sysctl_root
.sysctl_flags
& CTLFLAG_PERMANENT
) &&
1753 (!(sysctl_rootof(node
)->sysctl_flags
&
1754 CTLFLAG_READWRITE
) ||
1755 !(sysctl_root
.sysctl_flags
& CTLFLAG_READWRITE
))) {
1759 if (node
->sysctl_flags
& CTLFLAG_PERMANENT
) {
1763 if (l
!= NULL
&& node
->sysctl_desc
!= NULL
) {
1769 * right, let's go ahead. the first step is
1770 * making the description into something the
1771 * node can "own", if need be.
1774 dnode
.sysctl_flags
& CTLFLAG_OWNDESC
) {
1777 k
= malloc(MAXDESCLEN
, M_TEMP
,
1778 M_WAITOK
|M_CANFAIL
);
1783 error
= sysctl_copyinstr(l
, dnode
.sysctl_desc
,
1784 k
, MAXDESCLEN
, &sz
);
1789 nd
= malloc(sz
, M_SYSCTLDATA
,
1790 M_WAITOK
|M_CANFAIL
);
1797 dnode
.sysctl_flags
|= CTLFLAG_OWNDESC
;
1798 dnode
.sysctl_desc
= nd
;
1803 * now "release" the old description and
1804 * attach the new one. ta-da.
1806 if ((node
->sysctl_flags
& CTLFLAG_OWNDESC
) &&
1807 node
->sysctl_desc
!= NULL
)
1809 free(__UNCONST(node
->sysctl_desc
), M_SYSCTLDATA
);
1810 node
->sysctl_desc
= dnode
.sysctl_desc
;
1811 node
->sysctl_flags
|=
1812 (dnode
.sysctl_flags
& CTLFLAG_OWNDESC
);
1815 * now we "fall out" and into the loop which
1816 * will copy the new description back out for
1817 * those interested parties
1823 * scan for one description or just retrieve all descriptions
1825 for (i
= 0; i
< rnode
->sysctl_clen
; i
++) {
1827 * did they ask for the description of only one node?
1829 if (v
!= -1 && node
[i
].sysctl_num
!= dnode
.sysctl_num
)
1833 * don't describe "private" nodes to non-suser users
1835 if ((node
[i
].sysctl_flags
& CTLFLAG_PRIVATE
) && (l
!= NULL
) &&
1836 !(kauth_authorize_system(l
->l_cred
, KAUTH_SYSTEM_SYSCTL
,
1837 KAUTH_REQ_SYSTEM_SYSCTL_PRVT
, NULL
, NULL
, NULL
)))
1841 * is this description "valid"?
1843 memset(bf
, 0, MAXDESCLEN
);
1844 if (node
[i
].sysctl_desc
== NULL
)
1846 else if (copystr(node
[i
].sysctl_desc
, &d
->descr_str
[0],
1847 MAXDESCLEN
- sizeof(*d
), &sz
) != 0) {
1849 * erase possible partial description
1851 memset(bf
, 0, MAXDESCLEN
);
1856 * we've got it, stuff it into the caller's buffer
1858 d
->descr_num
= node
[i
].sysctl_num
;
1859 d
->descr_ver
= node
[i
].sysctl_ver
;
1860 d
->descr_len
= sz
; /* includes trailing nul */
1861 sz
= (char *)NEXT_DESCR(d
) - (char *)d
;
1862 if (oldp
!= NULL
&& left
>= sz
) {
1863 error
= sysctl_copyout(l
, d
, oldp
, sz
);
1867 oldp
= (void *)__sysc_desc_adv(oldp
, d
->descr_len
);
1872 * if we get this far with v not "unset", they asked
1873 * for a specific node and we found it
1880 * did we find it after all?
1882 if (v
!= -1 && tot
== 0)
1893 * ********************************************************************
1894 * Section 3: Create and destroy from inside the kernel
1895 * ********************************************************************
1896 * sysctl_createv() and sysctl_destroyv() are simpler-to-use
1897 * interfaces for the kernel to fling new entries into the mib and rip
1898 * them out later. In the case of sysctl_createv(), the returned copy
1899 * of the node (see sysctl_create()) will be translated back into a
1900 * pointer to the actual node.
1902 * Note that sysctl_createv() will return 0 if the create request
1903 * matches an existing node (ala mkdir -p), and that sysctl_destroyv()
1904 * will return 0 if the node to be destroyed already does not exist
1905 * (aka rm -f) or if it is a parent of other nodes.
1907 * This allows two (or more) different subsystems to assert sub-tree
1908 * existence before populating their own nodes, and to remove their
1909 * own nodes without orphaning the others when they are done.
1910 * ********************************************************************
1913 sysctl_createv(struct sysctllog
**log
, int cflags
,
1914 const struct sysctlnode
**rnode
, const struct sysctlnode
**cnode
,
1915 int flags
, int type
, const char *namep
, const char *descr
,
1916 sysctlfn func
, u_quad_t qv
, void *newp
, size_t newlen
,
1920 int error
, ni
, namelen
, name
[CTL_MAXNAME
];
1921 const struct sysctlnode
*root
, *pnode
;
1922 struct sysctlnode nnode
, onode
, *dnode
;
1926 * where are we putting this?
1928 if (rnode
!= NULL
&& *rnode
== NULL
) {
1929 printf("sysctl_createv: rnode NULL\n");
1932 root
= rnode
? *rnode
: NULL
;
1941 flags
= SYSCTL_VERSION
|SYSCTL_TYPE(type
)|SYSCTL_FLAGS(flags
);
1943 flags
&= ~CTLFLAG_PERMANENT
;
1946 * where do we put it?
1948 va_start(ap
, newlen
);
1952 if (++ni
== CTL_MAXNAME
)
1953 return (ENAMETOOLONG
);
1954 name
[ni
] = va_arg(ap
, int);
1956 * sorry, this is not supported from here
1958 if (name
[ni
] == CTL_CREATESYM
)
1960 } while (name
[ni
] != CTL_EOL
&& name
[ni
] != CTL_CREATE
);
1961 namelen
= ni
+ (name
[ni
] == CTL_CREATE
? 1 : 0);
1967 if (strlcpy(nnode
.sysctl_name
, namep
, sizeof(nnode
.sysctl_name
)) >=
1968 sizeof(nnode
.sysctl_name
))
1969 return (ENAMETOOLONG
);
1972 * cons up the description of the new node
1974 nnode
.sysctl_num
= name
[namelen
- 1];
1975 name
[namelen
- 1] = CTL_CREATE
;
1976 nnode
.sysctl_size
= newlen
;
1977 nnode
.sysctl_flags
= flags
;
1978 if (type
== CTLTYPE_NODE
) {
1979 nnode
.sysctl_csize
= 0;
1980 nnode
.sysctl_clen
= 0;
1981 nnode
.sysctl_child
= NULL
;
1982 if (flags
& CTLFLAG_ALIAS
)
1983 nnode
.sysctl_alias
= qv
;
1984 } else if (flags
& CTLFLAG_IMMEDIATE
) {
1987 nnode
.sysctl_bdata
= qv
;
1990 nnode
.sysctl_idata
= qv
;
1993 nnode
.sysctl_qdata
= qv
;
1999 nnode
.sysctl_data
= newp
;
2001 nnode
.sysctl_func
= func
;
2002 nnode
.sysctl_parent
= NULL
;
2003 nnode
.sysctl_ver
= 0;
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 a module (at module load time, say), we
2010 * don't really want to "wait"...
2015 * locate the prospective parent of the new node, and if we
2016 * find it, add the new node.
2020 error
= sysctl_locate(NULL
, &name
[0], namelen
- 1, &pnode
, &ni
);
2022 printf("sysctl_createv: sysctl_locate(%s) returned %d\n",
2023 nnode
.sysctl_name
, error
);
2027 error
= sysctl_create(&name
[ni
], namelen
- ni
, &onode
, &sz
,
2028 &nnode
, sizeof(nnode
), &name
[0], NULL
,
2032 * unfortunately the node we wanted to create is already
2033 * there. if the node that's already there is a reasonable
2034 * facsimile of the node we wanted to create, just pretend
2035 * (for the caller's benefit) that we managed to create the
2038 if (error
== EEXIST
) {
2039 /* name is the same as requested... */
2040 if (strcmp(nnode
.sysctl_name
, onode
.sysctl_name
) == 0 &&
2041 /* they want the same function... */
2042 nnode
.sysctl_func
== onode
.sysctl_func
&&
2043 /* number is the same as requested, or... */
2044 (nnode
.sysctl_num
== onode
.sysctl_num
||
2045 /* they didn't pick a number... */
2046 nnode
.sysctl_num
== CTL_CREATE
)) {
2048 * collision here from trying to create
2049 * something that already existed; let's give
2050 * our customers a hand and tell them they got
2053 #ifdef SYSCTL_DEBUG_CREATE
2054 printf("cleared\n");
2055 #endif /* SYSCTL_DEBUG_CREATE */
2061 (cnode
!= NULL
|| log
!= NULL
|| descr
!= NULL
)) {
2063 * sysctl_create() gave us back a copy of the node,
2064 * but we need to know where it actually is...
2067 error
= sysctl_locate(NULL
, &name
[0], namelen
- 1, &pnode
, &ni
);
2070 * manual scan of last layer so that aliased nodes
2074 for (ni
= 0; ni
< pnode
->sysctl_clen
; ni
++)
2075 if (pnode
->sysctl_child
[ni
].sysctl_num
==
2078 if (ni
< pnode
->sysctl_clen
)
2079 pnode
= &pnode
->sysctl_child
[ni
];
2085 * not expecting an error here, but...
2089 sysctl_log_add(log
, pnode
);
2092 if (descr
!= NULL
) {
2094 * allow first caller to *set* a
2095 * description actually to set it
2097 * discard const here so we can attach
2100 dnode
= __UNCONST(pnode
);
2101 if (pnode
->sysctl_desc
!= NULL
)
2102 /* skip it...we've got one */;
2103 else if (flags
& CTLFLAG_OWNDESC
) {
2104 size_t l
= strlen(descr
) + 1;
2105 char *d
= malloc(l
, M_SYSCTLDATA
,
2106 M_WAITOK
|M_CANFAIL
);
2108 memcpy(d
, descr
, l
);
2109 dnode
->sysctl_desc
= d
;
2110 dnode
->sysctl_flags
|=
2114 dnode
->sysctl_desc
= descr
;
2117 printf("sysctl_create succeeded but node not found?!\n");
2119 * confusing, but the create said it
2127 * now it should be safe to release the lock state. note that
2128 * the pointer to the newly created node being passed back may
2129 * not be "good" for very long.
2134 printf("sysctl_createv: sysctl_create(%s) returned %d\n",
2135 nnode
.sysctl_name
, error
);
2137 if (error
!= ENOENT
)
2138 sysctl_dump(&onode
);
2146 sysctl_destroyv(struct sysctlnode
*rnode
, ...)
2149 int error
, name
[CTL_MAXNAME
], namelen
, ni
;
2150 const struct sysctlnode
*pnode
, *node
;
2151 struct sysctlnode dnode
, *onode
;
2154 va_start(ap
, rnode
);
2158 if (ni
== CTL_MAXNAME
)
2159 return (ENAMETOOLONG
);
2160 name
[ni
] = va_arg(ap
, int);
2161 } while (name
[ni
++] != CTL_EOL
);
2166 * i can't imagine why we'd be destroying a node when the tree
2167 * wasn't complete, but who knows?
2175 error
= sysctl_locate(NULL
, &name
[0], namelen
- 1, &node
, &ni
);
2177 /* they want it gone and it's not there, so... */
2179 return (error
== ENOENT
? 0 : error
);
2183 * set up the deletion
2187 memset(&dnode
, 0, sizeof(dnode
));
2188 dnode
.sysctl_flags
= SYSCTL_VERSION
;
2189 dnode
.sysctl_num
= name
[namelen
- 1];
2192 * we found it, now let's nuke it
2194 name
[namelen
- 1] = CTL_DESTROY
;
2196 error
= sysctl_destroy(&name
[namelen
- 1], 1, NULL
, &sz
,
2197 node
, sizeof(*node
), &name
[0], NULL
,
2199 if (error
== ENOTEMPTY
) {
2201 * think of trying to delete "foo" when "foo.bar"
2202 * (which someone else put there) is still in
2208 * dunno who put the description there, but if this
2209 * node can ever be removed, we need to make sure the
2210 * string doesn't go out of context. that means we
2211 * need to find the node that's still there (don't use
2212 * sysctl_locate() because that follows aliasing).
2214 node
= pnode
->sysctl_child
;
2215 for (ni
= 0; ni
< pnode
->sysctl_clen
; ni
++)
2216 if (node
[ni
].sysctl_num
== dnode
.sysctl_num
)
2218 node
= (ni
< pnode
->sysctl_clen
) ? &node
[ni
] : NULL
;
2221 * if we found it, and this node has a description,
2222 * and this node can be released, and it doesn't
2223 * already own its own description...sigh. :)
2225 if (node
!= NULL
&& node
->sysctl_desc
!= NULL
&&
2226 !(node
->sysctl_flags
& CTLFLAG_PERMANENT
) &&
2227 !(node
->sysctl_flags
& CTLFLAG_OWNDESC
)) {
2230 sz
= strlen(node
->sysctl_desc
) + 1;
2231 d
= malloc(sz
, M_SYSCTLDATA
, M_WAITOK
|M_CANFAIL
);
2234 * discard const so that we can
2235 * re-attach the description
2237 memcpy(d
, node
->sysctl_desc
, sz
);
2238 onode
= __UNCONST(node
);
2239 onode
->sysctl_desc
= d
;
2240 onode
->sysctl_flags
|= CTLFLAG_OWNDESC
;
2243 * XXX drop the description? be
2244 * afraid? don't care?
2256 * ********************************************************************
2257 * Deletes an entire n-ary tree. Not recommended unless you know why
2258 * you're doing it. Personally, I don't know why you'd even think
2260 * ********************************************************************
2263 sysctl_free(struct sysctlnode
*rnode
)
2265 struct sysctlnode
*node
, *pnode
;
2267 rw_enter(&sysctl_treelock
, RW_WRITER
);
2270 rnode
= &sysctl_root
;
2272 if (SYSCTL_VERS(rnode
->sysctl_flags
) != SYSCTL_VERSION
) {
2273 printf("sysctl_free: rnode %p wrong version\n", rnode
);
2274 rw_exit(&sysctl_treelock
);
2280 node
= pnode
->sysctl_child
;
2282 while (node
!= NULL
&& pnode
->sysctl_csize
> 0) {
2284 &pnode
->sysctl_child
[pnode
->sysctl_clen
] &&
2285 (SYSCTL_TYPE(node
->sysctl_flags
) !=
2287 node
->sysctl_csize
== 0)) {
2288 if (SYSCTL_FLAGS(node
->sysctl_flags
) &
2290 if (node
->sysctl_data
!= NULL
) {
2291 free(node
->sysctl_data
,
2293 node
->sysctl_data
= NULL
;
2296 if (SYSCTL_FLAGS(node
->sysctl_flags
) &
2298 if (node
->sysctl_desc
!= NULL
) {
2300 free(__UNCONST(node
->sysctl_desc
),
2302 node
->sysctl_desc
= NULL
;
2307 if (node
< &pnode
->sysctl_child
[pnode
->sysctl_clen
]) {
2309 node
= node
->sysctl_child
;
2313 if (pnode
->sysctl_child
!= NULL
)
2314 free(pnode
->sysctl_child
, M_SYSCTLNODE
);
2315 pnode
->sysctl_clen
= 0;
2316 pnode
->sysctl_csize
= 0;
2317 pnode
->sysctl_child
= NULL
;
2319 pnode
= node
->sysctl_parent
;
2320 } while (pnode
!= NULL
&& node
!= rnode
);
2322 rw_exit(&sysctl_treelock
);
2326 sysctl_log_print(const struct sysctllog
*slog
)
2330 printf("root %p left %d size %d content", (const void *)slog
->log_root
,
2331 slog
->log_left
, slog
->log_size
);
2333 for (len
= 0, i
= slog
->log_left
; i
< slog
->log_size
; i
++) {
2337 printf(" version %d", slog
->log_num
[i
]);
2341 printf(" type %d", slog
->log_num
[i
]);
2344 len
= slog
->log_num
[i
];
2345 printf(" len %d:", slog
->log_num
[i
]);
2351 printf(" %d", slog
->log_num
[i
]);
2359 sysctl_log_add(struct sysctllog
**logp
, const struct sysctlnode
*node
)
2361 const int size0
= 16;
2362 int name
[CTL_MAXNAME
], namelen
, i
;
2363 const struct sysctlnode
*pnode
;
2364 struct sysctllog
*log
;
2366 if (node
->sysctl_flags
& CTLFLAG_PERMANENT
)
2372 if (*logp
== NULL
) {
2373 log
= malloc(sizeof(struct sysctllog
),
2374 M_SYSCTLDATA
, M_WAITOK
|M_CANFAIL
);
2376 /* XXX print error message? */
2379 log
->log_num
= malloc(size0
* sizeof(int),
2380 M_SYSCTLDATA
, M_WAITOK
|M_CANFAIL
);
2381 if (log
->log_num
== NULL
) {
2382 /* XXX print error message? */
2383 free(log
, M_SYSCTLDATA
);
2386 memset(log
->log_num
, 0, size0
* sizeof(int));
2387 log
->log_root
= NULL
;
2388 log
->log_size
= size0
;
2389 log
->log_left
= size0
;
2395 * check that the root is proper. it's okay to record the
2396 * address of the root of a tree. it's the only thing that's
2397 * guaranteed not to shift around as nodes come and go.
2399 if (log
->log_root
== NULL
)
2400 log
->log_root
= sysctl_rootof(node
);
2401 else if (log
->log_root
!= sysctl_rootof(node
)) {
2402 printf("sysctl: log %p root mismatch (%p)\n",
2403 log
->log_root
, sysctl_rootof(node
));
2408 * we will copy out name in reverse order
2410 for (pnode
= node
, namelen
= 0;
2411 pnode
!= NULL
&& !(pnode
->sysctl_flags
& CTLFLAG_ROOT
);
2412 pnode
= pnode
->sysctl_parent
)
2413 name
[namelen
++] = pnode
->sysctl_num
;
2418 if (log
->log_left
< (namelen
+ 3))
2419 sysctl_log_realloc(log
);
2420 if (log
->log_left
< (namelen
+ 3))
2424 * stuff name in, then namelen, then node type, and finally,
2425 * the version for non-node nodes.
2427 for (i
= 0; i
< namelen
; i
++)
2428 log
->log_num
[--log
->log_left
] = name
[i
];
2429 log
->log_num
[--log
->log_left
] = namelen
;
2430 log
->log_num
[--log
->log_left
] = SYSCTL_TYPE(node
->sysctl_flags
);
2431 if (log
->log_num
[log
->log_left
] != CTLTYPE_NODE
)
2432 log
->log_num
[--log
->log_left
] = node
->sysctl_ver
;
2434 log
->log_num
[--log
->log_left
] = 0;
2440 sysctl_teardown(struct sysctllog
**logp
)
2442 const struct sysctlnode
*rnode
;
2443 struct sysctlnode node
;
2444 struct sysctllog
*log
;
2446 int *name
, t
, v
, error
, ni
;
2449 if (logp
== NULL
|| *logp
== NULL
)
2453 rw_enter(&sysctl_treelock
, RW_WRITER
);
2454 memset(&node
, 0, sizeof(node
));
2456 while (log
->log_left
< log
->log_size
) {
2457 KASSERT((log
->log_left
+ 3 < log
->log_size
) &&
2458 (log
->log_left
+ log
->log_num
[log
->log_left
+ 2] <=
2460 v
= log
->log_num
[log
->log_left
++];
2461 t
= log
->log_num
[log
->log_left
++];
2462 namelen
= log
->log_num
[log
->log_left
++];
2463 name
= &log
->log_num
[log
->log_left
];
2465 node
.sysctl_num
= name
[namelen
- 1];
2466 node
.sysctl_flags
= SYSCTL_VERSION
|t
;
2467 node
.sysctl_ver
= v
;
2469 rnode
= log
->log_root
;
2470 error
= sysctl_locate(NULL
, &name
[0], namelen
, &rnode
, &ni
);
2472 name
[namelen
- 1] = CTL_DESTROY
;
2473 rnode
= rnode
->sysctl_parent
;
2475 (void)sysctl_destroy(&name
[namelen
- 1], 1, NULL
,
2476 &sz
, &node
, sizeof(node
),
2477 &name
[0], NULL
, rnode
);
2480 log
->log_left
+= namelen
;
2483 KASSERT(log
->log_size
== log
->log_left
);
2484 free(log
->log_num
, M_SYSCTLDATA
);
2485 free(log
, M_SYSCTLDATA
);
2488 rw_exit(&sysctl_treelock
);
2492 * ********************************************************************
2493 * old_sysctl -- A routine to bridge old-style internal calls to the
2494 * new infrastructure.
2495 * ********************************************************************
2498 old_sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
,
2499 void *newp
, size_t newlen
, struct lwp
*l
)
2510 sysctl_lock(newp
!= NULL
);
2511 error
= sysctl_dispatch(name
, namelen
, oldp
, &oldlen
,
2512 newp
, newlen
, name
, l
, NULL
);
2514 if (error
== 0 && oldp
!= NULL
&& savelen
< oldlen
)
2524 * ********************************************************************
2525 * Section 4: Generic helper routines
2526 * ********************************************************************
2527 * "helper" routines that can do more finely grained access control,
2528 * construct structures from disparate information, create the
2529 * appearance of more nodes and sub-trees, etc. for example, if
2530 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY
2531 * with a dynamically created list of nodes that represented the
2532 * currently running processes at that instant.
2533 * ********************************************************************
2537 * first, a few generic helpers that provide:
2539 * sysctl_needfunc() a readonly interface that emits a warning
2540 * sysctl_notavail() returns EOPNOTSUPP (generic error)
2541 * sysctl_null() an empty return buffer with no error
2544 sysctl_needfunc(SYSCTLFN_ARGS
)
2548 printf("!!SYSCTL_NEEDFUNC!!\n");
2550 if (newp
!= NULL
|| namelen
!= 0)
2551 return (EOPNOTSUPP
);
2555 error
= sysctl_copyout(l
, rnode
->sysctl_data
, oldp
,
2556 MIN(rnode
->sysctl_size
, *oldlenp
));
2557 *oldlenp
= rnode
->sysctl_size
;
2563 sysctl_notavail(SYSCTLFN_ARGS
)
2566 if (namelen
== 1 && name
[0] == CTL_QUERY
)
2567 return (sysctl_query(SYSCTLFN_CALL(rnode
)));
2569 return (EOPNOTSUPP
);
2573 sysctl_null(SYSCTLFN_ARGS
)
2582 * ********************************************************************
2583 * Section 5: The machinery that makes it all go
2584 * ********************************************************************
2585 * Memory "manglement" routines. Not much to this, eh?
2586 * ********************************************************************
2589 sysctl_alloc(struct sysctlnode
*p
, int x
)
2592 struct sysctlnode
*n
;
2594 assert(p
->sysctl_child
== NULL
);
2597 n
= malloc(sizeof(struct sysctlnode
),
2598 M_SYSCTLNODE
, M_WAITOK
|M_CANFAIL
);
2600 n
= malloc(SYSCTL_DEFSIZE
* sizeof(struct sysctlnode
),
2601 M_SYSCTLNODE
, M_WAITOK
|M_CANFAIL
);
2606 memset(n
, 0, sizeof(struct sysctlnode
));
2607 p
->sysctl_csize
= 1;
2609 memset(n
, 0, SYSCTL_DEFSIZE
* sizeof(struct sysctlnode
));
2610 p
->sysctl_csize
= SYSCTL_DEFSIZE
;
2614 for (i
= 0; i
< p
->sysctl_csize
; i
++)
2615 n
[i
].sysctl_parent
= p
;
2617 p
->sysctl_child
= n
;
2622 sysctl_realloc(struct sysctlnode
*p
)
2625 struct sysctlnode
*n
;
2627 assert(p
->sysctl_csize
== p
->sysctl_clen
);
2630 * how many do we have...how many should we make?
2632 olen
= p
->sysctl_clen
;
2633 n
= malloc(2 * olen
* sizeof(struct sysctlnode
), M_SYSCTLNODE
,
2634 M_WAITOK
|M_CANFAIL
);
2639 * move old children over...initialize new children
2641 memcpy(n
, p
->sysctl_child
, olen
* sizeof(struct sysctlnode
));
2642 memset(&n
[olen
], 0, olen
* sizeof(struct sysctlnode
));
2643 p
->sysctl_csize
= 2 * olen
;
2646 * reattach moved (and new) children to parent; if a moved
2647 * child node has children, reattach the parent pointers of
2650 for (i
= 0; i
< p
->sysctl_csize
; i
++) {
2651 n
[i
].sysctl_parent
= p
;
2652 if (n
[i
].sysctl_child
!= NULL
) {
2653 for (j
= 0; j
< n
[i
].sysctl_csize
; j
++)
2654 n
[i
].sysctl_child
[j
].sysctl_parent
= &n
[i
];
2659 * get out with the old and in with the new
2661 free(p
->sysctl_child
, M_SYSCTLNODE
);
2662 p
->sysctl_child
= n
;
2668 sysctl_log_realloc(struct sysctllog
*log
)
2672 s
= log
->log_size
* 2;
2675 n
= malloc(s
* sizeof(int), M_SYSCTLDATA
, M_WAITOK
|M_CANFAIL
);
2679 memset(n
, 0, s
* sizeof(int));
2680 memcpy(&n
[d
], log
->log_num
, d
* sizeof(int));
2681 free(log
->log_num
, M_SYSCTLDATA
);
2693 * ********************************************************************
2694 * Section 6: Conversion between API versions wrt the sysctlnode
2695 * ********************************************************************
2698 sysctl_cvt_in(struct lwp
*l
, int *vp
, const void *i
, size_t sz
,
2699 struct sysctlnode
*node
)
2703 if (i
== NULL
|| sz
< sizeof(flags
))
2706 error
= sysctl_copyin(l
, i
, &flags
, sizeof(flags
));
2710 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2711 #error sysctl_cvt_in: no support for SYSCTL_VERSION
2712 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2714 if (sz
== sizeof(*node
) &&
2715 SYSCTL_VERS(flags
) == SYSCTL_VERSION
) {
2716 error
= sysctl_copyin(l
, i
, node
, sizeof(*node
));
2719 *vp
= SYSCTL_VERSION
;
2727 sysctl_cvt_out(struct lwp
*l
, int v
, const struct sysctlnode
*i
,
2728 void *ovp
, size_t left
, size_t *szp
)
2730 size_t sz
= sizeof(*i
);
2731 const void *src
= i
;
2738 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
2739 #error sysctl_cvt_out: no support for SYSCTL_VERSION
2740 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
2742 case SYSCTL_VERSION
:
2743 /* nothing more to do here */
2747 if (ovp
!= NULL
&& left
>= sz
) {
2748 error
= sysctl_copyout(l
, src
, ovp
, sz
);