2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
32 * This code is derived from software contributed to Berkeley by
33 * Mike Karels at Berkeley Software Design, Inc.
35 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
36 * project, to make these variables more userfriendly.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
70 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
74 #include <sys/malloc.h>
75 #include <sys/proc_internal.h>
76 #include <sys/systm.h>
78 #include <bsm/audit_kernel.h>
81 struct sysctl_oid_list sysctl__debug_children;
82 struct sysctl_oid_list sysctl__kern_children;
83 struct sysctl_oid_list sysctl__net_children;
84 struct sysctl_oid_list sysctl__sysctl_children;
87 extern struct sysctl_oid
*newsysctl_list
[];
88 extern struct sysctl_oid
*machdep_sysctl_list
[];
89 lck_rw_t
* sysctl_geometry_lock
= NULL
;
92 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list
*l
, int i
);
99 static struct sysctl_lock memlock
;
102 * XXX this does not belong here
105 spl_kernel_funnel(void)
109 cfunnel
= thread_funnel_get();
110 if (cfunnel
!= kernel_flock
) {
112 thread_funnel_set(cfunnel
, FALSE
);
113 thread_funnel_set(kernel_flock
, TRUE
);
119 splx_kernel_funnel(funnel_t
*saved
)
121 if (saved
!= kernel_flock
) {
122 thread_funnel_set(kernel_flock
, FALSE
);
124 thread_funnel_set(saved
, TRUE
);
128 static int sysctl_root SYSCTL_HANDLER_ARGS
;
130 struct sysctl_oid_list sysctl__children
; /* root list */
133 * Initialization of the MIB tree.
135 * Order by number in each list.
138 void sysctl_register_oid(struct sysctl_oid
*oidp
)
140 struct sysctl_oid_list
*parent
= oidp
->oid_parent
;
141 struct sysctl_oid
*p
;
142 struct sysctl_oid
*q
;
146 fnl
= spl_kernel_funnel();
148 if(sysctl_geometry_lock
== NULL
)
150 /* Initialise the geometry lock for reading/modifying the sysctl tree
151 * This is done here because IOKit registers some sysctls before bsd_init()
152 * calls sysctl_register_fixed().
155 lck_grp_t
* lck_grp
= lck_grp_alloc_init("sysctl", NULL
);
156 sysctl_geometry_lock
= lck_rw_alloc_init(lck_grp
, NULL
);
158 /* Get the write lock to modify the geometry */
159 lck_rw_lock_exclusive(sysctl_geometry_lock
);
162 * If this oid has a number OID_AUTO, give it a number which
163 * is greater than any current oid. Make sure it is at least
164 * OID_AUTO_START to leave space for pre-assigned oid numbers.
166 if (oidp
->oid_number
== OID_AUTO
) {
167 /* First, find the highest oid in the parent list >OID_AUTO_START-1 */
169 SLIST_FOREACH(p
, parent
, oid_link
) {
170 if (p
->oid_number
> n
)
173 oidp
->oid_number
= n
+ 1;
177 * Insert the oid into the parent's list in order.
180 SLIST_FOREACH(p
, parent
, oid_link
) {
181 if (oidp
->oid_number
< p
->oid_number
)
186 SLIST_INSERT_AFTER(q
, oidp
, oid_link
);
188 SLIST_INSERT_HEAD(parent
, oidp
, oid_link
);
190 /* Release the write lock */
191 lck_rw_unlock_exclusive(sysctl_geometry_lock
);
193 splx_kernel_funnel(fnl
);
196 void sysctl_unregister_oid(struct sysctl_oid
*oidp
)
200 fnl
= spl_kernel_funnel();
202 /* Get the write lock to modify the geometry */
203 lck_rw_lock_exclusive(sysctl_geometry_lock
);
205 SLIST_REMOVE(oidp
->oid_parent
, oidp
, sysctl_oid
, oid_link
);
207 /* Release the write lock */
208 lck_rw_unlock_exclusive(sysctl_geometry_lock
);
210 splx_kernel_funnel(fnl
);
214 * Bulk-register all the oids in a linker_set.
216 void sysctl_register_set(const char *set
)
218 struct sysctl_oid
**oidpp
, *oidp
;
220 LINKER_SET_FOREACH(oidpp
, set
) {
222 if (!(oidp
->oid_kind
& CTLFLAG_NOAUTO
)) {
223 sysctl_register_oid(oidp
);
228 void sysctl_unregister_set(const char *set
)
230 struct sysctl_oid
**oidpp
, *oidp
;
232 LINKER_SET_FOREACH(oidpp
, set
) {
234 if (!(oidp
->oid_kind
& CTLFLAG_NOAUTO
)) {
235 sysctl_unregister_oid(oidp
);
242 * Register the kernel's oids on startup.
246 sysctl_register_all()
248 sysctl_register_set("__sysctl_set");
252 sysctl_register_fixed(void)
254 sysctl_register_all();
258 * New handler interface
259 * If the sysctl caller (user mode or kernel mode) is interested in the
260 * value (req->oldptr != NULL), we copy the data (bigValue etc.) out,
261 * if the caller wants to set the value (req->newptr), we copy
262 * the data in (*pValue etc.).
266 sysctl_io_number(struct sysctl_req
*req
, long long bigValue
, size_t valueSize
, void *pValue
, int *changed
) {
270 if (changed
) *changed
= 0;
273 * Handle the various combinations of caller buffer size and
274 * data value size. We are generous in the case where the
275 * caller has specified a 32-bit buffer but the value is 64-bit
279 /* 32 bit value expected or 32 bit buffer offered */
280 if ((valueSize
== sizeof(int)) ||
281 ((req
->oldlen
== sizeof(int)) && (valueSize
== sizeof(long long)))) {
282 smallValue
= (int)bigValue
;
283 if ((long long)smallValue
!= bigValue
)
285 error
= SYSCTL_OUT(req
, &smallValue
, sizeof(smallValue
));
287 /* any other case is either size-equal or a bug */
288 error
= SYSCTL_OUT(req
, &bigValue
, valueSize
);
290 /* error or nothing to set */
291 if (error
|| !req
->newptr
)
294 /* set request for constant */
298 /* set request needs to convert? */
299 if ((req
->newlen
== sizeof(int)) && (valueSize
== sizeof(long long))) {
300 /* new value is 32 bits, upconvert to 64 bits */
301 error
= SYSCTL_IN(req
, &smallValue
, sizeof(smallValue
));
303 *(long long *)pValue
= (long long)smallValue
;
304 } else if ((req
->newlen
== sizeof(long long)) && (valueSize
== sizeof(int))) {
305 /* new value is 64 bits, downconvert to 32 bits and range check */
306 error
= SYSCTL_IN(req
, &bigValue
, sizeof(bigValue
));
308 smallValue
= (int)bigValue
;
309 if ((long long)smallValue
!= bigValue
)
311 *(int *)pValue
= smallValue
;
314 /* sizes match, just copy in */
315 error
= SYSCTL_IN(req
, pValue
, valueSize
);
317 if (!error
&& changed
)
323 sysctl_io_string(struct sysctl_req
*req
, char *pValue
, size_t valueSize
, int trunc
, int *changed
)
327 if (changed
) *changed
= 0;
329 if (trunc
&& req
->oldptr
&& req
->oldlen
&& (req
->oldlen
<strlen(pValue
) + 1)) {
330 /* If trunc != 0, if you give it a too small (but larger than
331 * 0 bytes) buffer, instead of returning ENOMEM, it truncates the
332 * returned string to the buffer size. This preserves the semantics
333 * of some library routines implemented via sysctl, which truncate
334 * their returned data, rather than simply returning an error. The
335 * returned string is always NUL terminated. */
336 error
= SYSCTL_OUT(req
, pValue
, req
->oldlen
-1);
339 error
= SYSCTL_OUT(req
, &c
, 1);
342 /* Copy string out */
343 error
= SYSCTL_OUT(req
, pValue
, strlen(pValue
) + 1);
346 /* error or no new value */
347 if (error
|| !req
->newptr
)
350 /* attempt to set read-only value */
354 /* make sure there's room for the new string */
355 if (req
->newlen
>= valueSize
)
358 /* copy the string in and force NUL termination */
359 error
= SYSCTL_IN(req
, pValue
, req
->newlen
);
360 pValue
[req
->newlen
] = '\0';
362 if (!error
&& changed
)
367 int sysctl_io_opaque(struct sysctl_req
*req
,void *pValue
, size_t valueSize
, int *changed
)
371 if (changed
) *changed
= 0;
374 error
= SYSCTL_OUT(req
, pValue
, valueSize
);
376 /* error or nothing to set */
377 if (error
|| !req
->newptr
)
380 error
= SYSCTL_IN(req
, pValue
, valueSize
);
382 if (!error
&& changed
)
390 * These functions implement a presently undocumented interface
391 * used by the sysctl program to walk the tree, and get the type
392 * so it can print the value.
393 * This interface is under work and consideration, and should probably
394 * be killed with a big axe by the first person who can find the time.
395 * (be aware though, that the proper interface isn't as obvious as it
396 * may seem, there are various conflicting requirements.
398 * {0,0} printf the entire MIB-tree.
399 * {0,1,...} return the name of the "..." OID.
400 * {0,2,...} return the next OID.
401 * {0,3} return the OID of the name in "new"
402 * {0,4,...} return the kind & format info for the "..." OID.
406 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list
*l
, int i
)
409 struct sysctl_oid
*oidp
;
411 SLIST_FOREACH(oidp
, l
, oid_link
) {
416 printf("%d %s ", oidp
->oid_number
, oidp
->oid_name
);
419 oidp
->oid_kind
& CTLFLAG_RD
? 'R':' ',
420 oidp
->oid_kind
& CTLFLAG_WR
? 'W':' ');
422 if (oidp
->oid_handler
)
425 switch (oidp
->oid_kind
& CTLTYPE
) {
428 if (!oidp
->oid_handler
) {
429 sysctl_sysctl_debug_dump_node(
430 oidp
->oid_arg1
, i
+2);
433 case CTLTYPE_INT
: printf(" Int\n"); break;
434 case CTLTYPE_STRING
: printf(" String\n"); break;
435 case CTLTYPE_QUAD
: printf(" Quad\n"); break;
436 case CTLTYPE_OPAQUE
: printf(" Opaque/struct\n"); break;
437 default: printf("\n");
444 sysctl_sysctl_debug(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
445 __unused
int arg2
, __unused
struct sysctl_req
*req
)
447 sysctl_sysctl_debug_dump_node(&sysctl__children
, 0);
451 SYSCTL_PROC(_sysctl
, 0, debug
, CTLTYPE_STRING
|CTLFLAG_RD
,
452 0, 0, sysctl_sysctl_debug
, "-", "");
455 sysctl_sysctl_name(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
456 struct sysctl_req
*req
)
458 int *name
= (int *) arg1
;
459 u_int namelen
= arg2
;
461 struct sysctl_oid
*oid
;
462 struct sysctl_oid_list
*lsp
= &sysctl__children
, *lsp2
;
467 snprintf(tempbuf
,sizeof(tempbuf
),"%d",*name
);
469 error
= SYSCTL_OUT(req
, ".", 1);
471 error
= SYSCTL_OUT(req
, tempbuf
, strlen(tempbuf
));
479 SLIST_FOREACH(oid
, lsp
, oid_link
) {
480 if (oid
->oid_number
!= *name
)
484 error
= SYSCTL_OUT(req
, ".", 1);
486 error
= SYSCTL_OUT(req
, oid
->oid_name
,
487 strlen(oid
->oid_name
));
494 if ((oid
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
497 if (oid
->oid_handler
)
500 lsp2
= (struct sysctl_oid_list
*)oid
->oid_arg1
;
505 return (SYSCTL_OUT(req
, "", 1));
508 SYSCTL_NODE(_sysctl
, 1, name
, CTLFLAG_RD
, sysctl_sysctl_name
, "");
511 sysctl_sysctl_next_ls (struct sysctl_oid_list
*lsp
, int *name
, u_int namelen
,
512 int *next
, int *len
, int level
, struct sysctl_oid
**oidpp
)
514 struct sysctl_oid
*oidp
;
517 SLIST_FOREACH(oidp
, lsp
, oid_link
) {
518 *next
= oidp
->oid_number
;
522 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
524 if (oidp
->oid_handler
)
525 /* We really should call the handler here...*/
527 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
529 if (!SLIST_FIRST(lsp
))
530 /* This node had no children - skip it! */
533 if (!sysctl_sysctl_next_ls (lsp
, 0, 0, next
+1,
534 len
, level
+1, oidpp
))
539 if (oidp
->oid_number
< *name
)
542 if (oidp
->oid_number
> *name
) {
543 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
545 if (oidp
->oid_handler
)
547 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
548 if (!sysctl_sysctl_next_ls (lsp
, name
+1, namelen
-1,
549 next
+1, len
, level
+1, oidpp
))
553 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
556 if (oidp
->oid_handler
)
559 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
560 if (!sysctl_sysctl_next_ls (lsp
, name
+1, namelen
-1, next
+1,
561 len
, level
+1, oidpp
))
571 sysctl_sysctl_next(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
572 struct sysctl_req
*req
)
574 int *name
= (int *) arg1
;
575 u_int namelen
= arg2
;
577 struct sysctl_oid
*oid
;
578 struct sysctl_oid_list
*lsp
= &sysctl__children
;
579 int newoid
[CTL_MAXNAME
];
581 i
= sysctl_sysctl_next_ls (lsp
, name
, namelen
, newoid
, &j
, 1, &oid
);
584 error
= SYSCTL_OUT(req
, newoid
, j
* sizeof (int));
588 SYSCTL_NODE(_sysctl
, 2, next
, CTLFLAG_RD
, sysctl_sysctl_next
, "");
591 name2oid (char *name
, int *oid
, int *len
, struct sysctl_oid
**oidpp
)
594 struct sysctl_oid
*oidp
;
595 struct sysctl_oid_list
*lsp
= &sysctl__children
;
601 p
= name
+ strlen(name
) - 1 ;
607 for (p
= name
; *p
&& *p
!= '.'; p
++)
613 oidp
= SLIST_FIRST(lsp
);
615 while (oidp
&& *len
< CTL_MAXNAME
) {
616 if (strcmp(name
, oidp
->oid_name
)) {
617 oidp
= SLIST_NEXT(oidp
, oid_link
);
620 *oid
++ = oidp
->oid_number
;
629 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
632 if (oidp
->oid_handler
)
635 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
636 oidp
= SLIST_FIRST(lsp
);
638 for (p
= name
; *p
&& *p
!= '.'; p
++)
648 sysctl_sysctl_name2oid(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
649 __unused
int arg2
, struct sysctl_req
*req
)
652 int error
, oid
[CTL_MAXNAME
];
653 int len
= 0; /* set by name2oid() */
654 struct sysctl_oid
*op
= 0;
658 if (req
->newlen
>= MAXPATHLEN
) /* XXX arbitrary, undocumented */
659 return (ENAMETOOLONG
);
661 MALLOC(p
, char *,req
->newlen
+1, M_TEMP
, M_WAITOK
);
665 error
= SYSCTL_IN(req
, p
, req
->newlen
);
671 p
[req
->newlen
] = '\0';
673 error
= name2oid(p
, oid
, &len
, &op
);
680 error
= SYSCTL_OUT(req
, oid
, len
* sizeof *oid
);
684 SYSCTL_PROC(_sysctl
, 3, name2oid
, CTLFLAG_RW
|CTLFLAG_ANYBODY
|CTLFLAG_KERN
, 0, 0,
685 sysctl_sysctl_name2oid
, "I", "");
688 sysctl_sysctl_oidfmt(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
689 struct sysctl_req
*req
)
691 int *name
= (int *) arg1
, error
;
692 u_int namelen
= arg2
;
694 struct sysctl_oid
*oid
;
695 struct sysctl_oid_list
*lsp
= &sysctl__children
;
697 oid
= SLIST_FIRST(lsp
);
700 while (oid
&& indx
< CTL_MAXNAME
) {
701 if (oid
->oid_number
== name
[indx
]) {
703 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
704 if (oid
->oid_handler
)
708 lsp
= (struct sysctl_oid_list
*)oid
->oid_arg1
;
709 oid
= SLIST_FIRST(lsp
);
716 oid
= SLIST_NEXT(oid
, oid_link
);
723 error
= SYSCTL_OUT(req
,
724 &oid
->oid_kind
, sizeof(oid
->oid_kind
));
726 error
= SYSCTL_OUT(req
, oid
->oid_fmt
,
727 strlen(oid
->oid_fmt
)+1);
732 SYSCTL_NODE(_sysctl
, 4, oidfmt
, CTLFLAG_RD
, sysctl_sysctl_oidfmt
, "");
735 * Default "handler" functions.
739 * Handle an int, signed or unsigned.
741 * a variable: point arg1 at it.
742 * a constant: pass it in arg2.
746 sysctl_handle_int(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
747 struct sysctl_req
*req
)
749 return sysctl_io_number(req
, arg1
? *(int*)arg1
: arg2
, sizeof(int), arg1
, NULL
);
753 * Handle a long, signed or unsigned. arg1 points to it.
757 sysctl_handle_long(__unused
struct sysctl_oid
*oidp
, void *arg1
,
758 __unused
int arg2
, struct sysctl_req
*req
)
762 return sysctl_io_number(req
, *(long*)arg1
, sizeof(long), arg1
, NULL
);
766 * Handle a quad, signed or unsigned. arg1 points to it.
770 sysctl_handle_quad(__unused
struct sysctl_oid
*oidp
, void *arg1
,
771 __unused
int arg2
, struct sysctl_req
*req
)
775 return sysctl_io_number(req
, *(long long*)arg1
, sizeof(long long), arg1
, NULL
);
779 * Expose an int value as a quad.
781 * This interface allows us to support interfaces defined
782 * as using quad values while the implementation is still
786 sysctl_handle_int2quad(__unused
struct sysctl_oid
*oidp
, void *arg1
,
787 __unused
int arg2
, struct sysctl_req
*req
)
795 val
= (long long)*(int *)arg1
;
796 error
= SYSCTL_OUT(req
, &val
, sizeof(long long));
798 if (error
|| !req
->newptr
)
801 error
= SYSCTL_IN(req
, &val
, sizeof(long long));
804 * Value must be representable; check by
805 * casting and then casting back.
808 if ((long long)newval
!= val
) {
811 *(int *)arg1
= newval
;
818 * Handle our generic '\0' terminated 'C' string.
820 * a variable string: point arg1 at it, arg2 is max length.
821 * a constant string: point arg1 at it, arg2 is zero.
825 sysctl_handle_string( __unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
826 struct sysctl_req
*req
)
828 return sysctl_io_string(req
, arg1
, arg2
, 0, NULL
);
832 * Handle any kind of opaque data.
833 * arg1 points to it, arg2 is the size.
837 sysctl_handle_opaque(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
838 struct sysctl_req
*req
)
840 return sysctl_io_opaque(req
, arg1
, arg2
, NULL
);
844 * Transfer functions to/from kernel space.
847 sysctl_old_kernel(struct sysctl_req
*req
, const void *p
, size_t l
)
853 if (i
> req
->oldlen
- req
->oldidx
)
854 i
= req
->oldlen
- req
->oldidx
;
856 bcopy((const void*)p
, CAST_DOWN(char *, (req
->oldptr
+ req
->oldidx
)), i
);
859 if (req
->oldptr
&& i
!= l
)
865 sysctl_new_kernel(struct sysctl_req
*req
, void *p
, size_t l
)
869 if (req
->newlen
- req
->newidx
< l
)
871 bcopy(CAST_DOWN(char *, (req
->newptr
+ req
->newidx
)), p
, l
);
877 kernel_sysctl(struct proc
*p
, int *name
, u_int namelen
, void *old
, size_t *oldlenp
, void *new, size_t newlen
)
880 struct sysctl_req req
;
885 bzero(&req
, sizeof req
);
888 req
.oldlen
= *oldlenp
;
890 req
.oldptr
= CAST_USER_ADDR_T(old
);
893 req
.newptr
= CAST_USER_ADDR_T(new);
895 req
.oldfunc
= sysctl_old_kernel
;
896 req
.newfunc
= sysctl_new_kernel
;
899 /* make the request */
900 error
= sysctl_root(0, name
, namelen
, &req
);
902 /* unlock memory if required */
904 vsunlock(req
.oldptr
, (user_size_t
)req
.oldlen
, B_WRITE
);
906 if (error
&& error
!= ENOMEM
)
910 *oldlenp
= req
.oldidx
;
916 * Transfer function to/from user space.
919 sysctl_old_user(struct sysctl_req
*req
, const void *p
, size_t l
)
925 if (req
->oldlen
- req
->oldidx
< l
)
928 if (i
> req
->oldlen
- req
->oldidx
)
929 i
= req
->oldlen
- req
->oldidx
;
931 error
= copyout((const void*)p
, (req
->oldptr
+ req
->oldidx
), i
);
936 if (req
->oldptr
&& i
< l
)
942 sysctl_new_user(struct sysctl_req
*req
, void *p
, size_t l
)
948 if (req
->newlen
- req
->newidx
< l
)
950 error
= copyin((req
->newptr
+ req
->newidx
), p
, l
);
956 * Traverse our tree, and find the right node, execute whatever it points
957 * at, and return the resulting error code.
961 sysctl_root(__unused
struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
962 struct sysctl_req
*req
)
964 int *name
= (int *) arg1
;
965 u_int namelen
= arg2
;
968 struct sysctl_oid
*oid
;
969 struct sysctl_oid_list
*lsp
= &sysctl__children
;
971 funnel_t
*fnl
= NULL
;
972 boolean_t funnel_held
= FALSE
;
974 /* Get the read lock on the geometry */
975 lck_rw_lock_shared(sysctl_geometry_lock
);
977 oid
= SLIST_FIRST(lsp
);
980 while (oid
&& indx
< CTL_MAXNAME
) {
981 if (oid
->oid_number
== name
[indx
]) {
983 if (!(oid
->oid_kind
& CTLFLAG_LOCKED
))
987 if (oid
->oid_kind
& CTLFLAG_NOLOCK
)
989 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
990 if (oid
->oid_handler
)
998 lsp
= (struct sysctl_oid_list
*)oid
->oid_arg1
;
999 oid
= SLIST_FIRST(lsp
);
1001 if (indx
!= namelen
)
1009 oid
= SLIST_NEXT(oid
, oid_link
);
1015 /* If writing isn't allowed */
1016 if (req
->newptr
&& (!(oid
->oid_kind
& CTLFLAG_WR
) ||
1017 ((oid
->oid_kind
& CTLFLAG_SECURE
) && securelevel
> 0))) {
1023 * If we're inside the kernel, the OID must be marked as kernel-valid.
1024 * XXX This mechanism for testing is bad.
1026 if ((req
->oldfunc
== sysctl_old_kernel
) && !(oid
->oid_kind
& CTLFLAG_KERN
))
1032 /* Most likely only root can write */
1033 if (!(oid
->oid_kind
& CTLFLAG_ANYBODY
) &&
1034 req
->newptr
&& req
->p
&&
1035 (error
= proc_suser(req
->p
)))
1038 if (!oid
->oid_handler
) {
1045 fnl
= spl_kernel_funnel();
1049 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
1050 i
= (oid
->oid_handler
) (oid
,
1051 name
+ indx
, namelen
- indx
,
1054 i
= (oid
->oid_handler
) (oid
,
1055 oid
->oid_arg1
, oid
->oid_arg2
,
1063 splx_kernel_funnel(fnl
);
1067 lck_rw_done(sysctl_geometry_lock
);
1071 #ifndef _SYS_SYSPROTO_H_
1072 struct sysctl_args
{
1083 /* __sysctl(struct proc *p, struct sysctl_args *uap) */
1084 new_sysctl(struct proc
*p
, struct sysctl_args
*uap
)
1086 int error
, i
, name
[CTL_MAXNAME
];
1089 if (uap
->namelen
> CTL_MAXNAME
|| uap
->namelen
< 2)
1092 error
= copyin(CAST_USER_ADDR_T(uap
->name
), &name
, uap
->namelen
* sizeof(int));
1096 error
= userland_sysctl(p
, name
, uap
->namelen
,
1097 CAST_USER_ADDR_T(uap
->old
), uap
->oldlenp
, 0,
1098 CAST_USER_ADDR_T(uap
->new), uap
->newlen
, &j
);
1099 if (error
&& error
!= ENOMEM
)
1102 i
= copyout(&j
, CAST_USER_ADDR_T(uap
->oldlenp
), sizeof(j
));
1110 * This is used from various compatibility syscalls too. That's why name
1111 * must be in kernel space.
1114 userland_sysctl(struct proc
*p
, int *name
, u_int namelen
, user_addr_t oldp
,
1115 size_t *oldlenp
, int inkernel
, user_addr_t newp
, size_t newlen
,
1119 struct sysctl_req req
, req2
;
1121 bzero(&req
, sizeof req
);
1127 req
.oldlen
= *oldlenp
;
1129 error
= copyin(CAST_USER_ADDR_T(oldlenp
), &req
.oldlen
, sizeof(*oldlenp
));
1140 req
.newlen
= newlen
;
1144 req
.oldfunc
= sysctl_old_user
;
1145 req
.newfunc
= sysctl_new_user
;
1150 error
= sysctl_root(0, name
, namelen
, &req2
);
1151 } while (error
== EAGAIN
);
1155 if (error
&& error
!= ENOMEM
)
1159 if (req
.oldptr
&& req
.oldidx
> req
.oldlen
)
1160 *retval
= req
.oldlen
;
1162 *retval
= req
.oldidx
;
1167 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1168 #define KINFO_BSDI_SYSINFO (101<<8)
1171 * Kernel versions of the userland sysctl helper functions.
1173 * These allow sysctl to be used in the same fashion in both
1174 * userland and the kernel.
1176 * Note that some sysctl handlers use copyin/copyout, which
1177 * may not work correctly.
1181 sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
, void *newp
, size_t newlen
)
1184 return(kernel_sysctl(current_proc(), name
, namelen
, oldp
, oldlenp
, newp
, newlen
));
1188 sysctlnametomib(const char *name
, int *mibp
, size_t *sizep
)
1193 /* magic service node */
1197 /* look up OID for name */
1198 *sizep
*= sizeof(int);
1199 error
= sysctl(oid
, 2, mibp
, sizep
, (void *)name
, strlen(name
));
1200 *sizep
/= sizeof(int);
1205 sysctlbyname(const char *name
, void *oldp
, size_t *oldlenp
, void *newp
, size_t newlen
)
1207 int oid
[CTL_MAXNAME
+ 2];
1211 /* look up the OID */
1212 oidlen
= CTL_MAXNAME
;
1213 error
= sysctlnametomib(name
, oid
, &oidlen
);
1215 /* now use the OID */
1217 error
= sysctl(oid
, oidlen
, oldp
, oldlenp
, newp
, newlen
);