2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
52 #include <sys/sysproto.h>
54 #include <vm/vm_extern.h>
56 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
61 static struct sysctl_lock {
67 static int sysctl_root SYSCTL_HANDLER_ARGS;
69 struct sysctl_oid_list sysctl__children; /* root list */
72 * Initialization of the MIB tree.
74 * Order by number in each list.
77 void sysctl_register_oid(struct sysctl_oid *oidp)
79 struct sysctl_oid_list *parent = oidp->oid_parent;
85 * If this oid has a number OID_AUTO, give it a number which
86 * is greater than any current oid. Make sure it is at least
87 * 100 to leave space for pre-assigned oid numbers.
89 if (oidp->oid_number == OID_AUTO) {
90 /* First, find the highest oid in the parent list >99 */
92 SLIST_FOREACH(p, parent, oid_link) {
93 if (p->oid_number > n)
96 oidp->oid_number = n + 1;
100 * Insert the oid into the parent's list in order.
103 SLIST_FOREACH(p, parent, oid_link) {
104 if (oidp->oid_number < p->oid_number)
109 SLIST_INSERT_AFTER(q, oidp, oid_link);
111 SLIST_INSERT_HEAD(parent, oidp, oid_link);
114 void sysctl_unregister_oid(struct sysctl_oid *oidp)
116 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
120 * Bulk-register all the oids in a linker_set.
122 void sysctl_register_set(struct linker_set *lsp)
124 int count = lsp->ls_length;
126 for (i = 0; i < count; i++)
127 sysctl_register_oid((struct sysctl_oid *) lsp->ls_items[i]);
130 void sysctl_unregister_set(struct linker_set *lsp)
132 int count = lsp->ls_length;
134 for (i = 0; i < count; i++)
135 sysctl_unregister_oid((struct sysctl_oid *) lsp->ls_items[i]);
139 * Register the kernel's oids on startup.
141 extern struct linker_set sysctl_set;
143 static void sysctl_register_all(void *arg)
145 sysctl_register_set(&sysctl_set);
148 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
153 * These functions implement a presently undocumented interface
154 * used by the sysctl program to walk the tree, and get the type
155 * so it can print the value.
156 * This interface is under work and consideration, and should probably
157 * be killed with a big axe by the first person who can find the time.
158 * (be aware though, that the proper interface isn't as obvious as it
159 * may seem, there are various conflicting requirements.
161 * {0,0} printf the entire MIB-tree.
162 * {0,1,...} return the name of the "..." OID.
163 * {0,2,...} return the next OID.
164 * {0,3} return the OID of the name in "new"
165 * {0,4,...} return the kind & format info for the "..." OID.
169 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
172 struct sysctl_oid *oidp;
174 SLIST_FOREACH(oidp, l, oid_link) {
179 printf("%d %s ", oidp->oid_number, oidp->oid_name);
182 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
183 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
185 if (oidp->oid_handler)
188 switch (oidp->oid_kind & CTLTYPE) {
191 if (!oidp->oid_handler) {
192 sysctl_sysctl_debug_dump_node(
193 oidp->oid_arg1, i+2);
196 case CTLTYPE_INT: printf(" Int\n"); break;
197 case CTLTYPE_STRING: printf(" String\n"); break;
198 case CTLTYPE_QUAD: printf(" Quad\n"); break;
199 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
200 default: printf("\n");
207 sysctl_sysctl_debug SYSCTL_HANDLER_ARGS
209 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
213 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
214 0, 0, sysctl_sysctl_debug, "-", "");
217 sysctl_sysctl_name SYSCTL_HANDLER_ARGS
219 int *name = (int *) arg1;
220 u_int namelen = arg2;
222 struct sysctl_oid *oid;
223 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
228 snprintf(buf,sizeof(buf),"%d",*name);
230 error = SYSCTL_OUT(req, ".", 1);
232 error = SYSCTL_OUT(req, buf, strlen(buf));
240 SLIST_FOREACH(oid, lsp, oid_link) {
241 if (oid->oid_number != *name)
245 error = SYSCTL_OUT(req, ".", 1);
247 error = SYSCTL_OUT(req, oid->oid_name,
248 strlen(oid->oid_name));
255 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
258 if (oid->oid_handler)
261 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
266 return (SYSCTL_OUT(req, "", 1));
269 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
272 sysctl_sysctl_next_ls (struct sysctl_oid_list *lsp, int *name, u_int namelen,
273 int *next, int *len, int level, struct sysctl_oid **oidpp)
275 struct sysctl_oid *oidp;
278 SLIST_FOREACH(oidp, lsp, oid_link) {
279 *next = oidp->oid_number;
283 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
285 if (oidp->oid_handler)
286 /* We really should call the handler here...*/
288 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
289 if (!sysctl_sysctl_next_ls (lsp, 0, 0, next+1,
290 len, level+1, oidpp))
295 if (oidp->oid_number < *name)
298 if (oidp->oid_number > *name) {
299 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
301 if (oidp->oid_handler)
303 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
304 if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1,
305 next+1, len, level+1, oidpp))
309 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
312 if (oidp->oid_handler)
315 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
316 if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1, next+1,
317 len, level+1, oidpp))
327 sysctl_sysctl_next SYSCTL_HANDLER_ARGS
329 int *name = (int *) arg1;
330 u_int namelen = arg2;
332 struct sysctl_oid *oid;
333 struct sysctl_oid_list *lsp = &sysctl__children;
334 int newoid[CTL_MAXNAME];
336 i = sysctl_sysctl_next_ls (lsp, name, namelen, newoid, &j, 1, &oid);
339 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
343 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
346 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
349 struct sysctl_oid *oidp;
350 struct sysctl_oid_list *lsp = &sysctl__children;
356 p = name + strlen(name) - 1 ;
362 for (p = name; *p && *p != '.'; p++)
368 oidp = SLIST_FIRST(lsp);
370 while (oidp && *len < CTL_MAXNAME) {
371 if (strcmp(name, oidp->oid_name)) {
372 oidp = SLIST_NEXT(oidp, oid_link);
375 *oid++ = oidp->oid_number;
384 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
387 if (oidp->oid_handler)
390 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
391 oidp = SLIST_FIRST(lsp);
393 for (p = name; *p && *p != '.'; p++)
403 sysctl_sysctl_name2oid SYSCTL_HANDLER_ARGS
406 int error, oid[CTL_MAXNAME], len;
407 struct sysctl_oid *op = 0;
411 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
412 return (ENAMETOOLONG);
414 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
416 error = SYSCTL_IN(req, p, req->newlen);
422 p [req->newlen] = '\0';
424 error = name2oid(p, oid, &len, &op);
431 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
435 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
436 sysctl_sysctl_name2oid, "I", "");
439 sysctl_sysctl_oidfmt SYSCTL_HANDLER_ARGS
441 int *name = (int *) arg1, error;
442 u_int namelen = arg2;
444 struct sysctl_oid *oid;
445 struct sysctl_oid_list *lsp = &sysctl__children;
447 oid = SLIST_FIRST(lsp);
450 while (oid && indx < CTL_MAXNAME) {
451 if (oid->oid_number == name[indx]) {
453 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
454 if (oid->oid_handler)
458 lsp = (struct sysctl_oid_list *)oid->oid_arg1;
459 oid = SLIST_FIRST(lsp);
466 oid = SLIST_NEXT(oid, oid_link);
473 error = SYSCTL_OUT(req,
474 &oid->oid_kind, sizeof(oid->oid_kind));
476 error = SYSCTL_OUT(req, oid->oid_fmt,
477 strlen(oid->oid_fmt)+1);
482 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
485 * Default "handler" functions.
489 * Handle an int, signed or unsigned.
491 * a variable: point arg1 at it.
492 * a constant: pass it in arg2.
496 sysctl_handle_int SYSCTL_HANDLER_ARGS
501 error = SYSCTL_OUT(req, arg1, sizeof(int));
503 error = SYSCTL_OUT(req, &arg2, sizeof(int));
505 if (error || !req->newptr)
511 error = SYSCTL_IN(req, arg1, sizeof(int));
516 * Handle a long, signed or unsigned. arg1 points to it.
520 sysctl_handle_long SYSCTL_HANDLER_ARGS
526 error = SYSCTL_OUT(req, arg1, sizeof(long));
528 if (error || !req->newptr)
531 error = SYSCTL_IN(req, arg1, sizeof(long));
536 * Handle our generic '\0' terminated 'C' string.
538 * a variable string: point arg1 at it, arg2 is max length.
539 * a constant string: point arg1 at it, arg2 is zero.
543 sysctl_handle_string SYSCTL_HANDLER_ARGS
547 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
549 if (error || !req->newptr)
552 if ((req->newlen - req->newidx) >= arg2) {
555 arg2 = (req->newlen - req->newidx);
556 error = SYSCTL_IN(req, arg1, arg2);
557 ((char *)arg1)[arg2] = '\0';
564 * Handle any kind of opaque data.
565 * arg1 points to it, arg2 is the size.
569 sysctl_handle_opaque SYSCTL_HANDLER_ARGS
573 error = SYSCTL_OUT(req, arg1, arg2);
575 if (error || !req->newptr)
578 error = SYSCTL_IN(req, arg1, arg2);
584 * Transfer functions to/from kernel space.
585 * XXX: rather untested at this point
588 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
594 if (i > req->oldlen - req->oldidx)
595 i = req->oldlen - req->oldidx;
597 bcopy(p, (char *)req->oldptr + req->oldidx, i);
600 if (req->oldptr && i != l)
606 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
610 if (req->newlen - req->newidx < l)
612 bcopy((char *)req->newptr + req->newidx, p, l);
618 kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
621 struct sysctl_req req;
623 bzero(&req, sizeof req);
628 req.oldlen = *oldlenp;
640 req.oldfunc = sysctl_old_kernel;
641 req.newfunc = sysctl_new_kernel;
644 /* XXX this should probably be done in a general way */
645 while (memlock.sl_lock) {
647 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
652 error = sysctl_root(0, name, namelen, &req);
655 vsunlock(req.oldptr, req.oldlen, B_WRITE);
659 if (memlock.sl_want) {
661 wakeup((caddr_t)&memlock);
664 if (error && error != ENOMEM)
668 if (req.oldptr && req.oldidx > req.oldlen)
669 *retval = req.oldlen;
671 *retval = req.oldidx;
677 * Transfer function to/from user space.
680 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
685 if (req->lock == 1 && req->oldptr) {
686 vslock(req->oldptr, req->oldlen);
691 if (i > req->oldlen - req->oldidx)
692 i = req->oldlen - req->oldidx;
694 error = copyout(p, (char *)req->oldptr + req->oldidx,
700 if (req->oldptr && i < l)
706 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
712 if (req->newlen - req->newidx < l)
714 error = copyin((char *)req->newptr + req->newidx, p, l);
720 * Traverse our tree, and find the right node, execute whatever it points
721 * at, and return the resulting error code.
725 sysctl_root SYSCTL_HANDLER_ARGS
727 int *name = (int *) arg1;
728 u_int namelen = arg2;
730 struct sysctl_oid *oid;
731 struct sysctl_oid_list *lsp = &sysctl__children;
733 oid = SLIST_FIRST(lsp);
736 while (oid && indx < CTL_MAXNAME) {
737 if (oid->oid_number == name[indx]) {
739 if (oid->oid_kind & CTLFLAG_NOLOCK)
741 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
742 if (oid->oid_handler)
746 lsp = (struct sysctl_oid_list *)oid->oid_arg1;
747 oid = SLIST_FIRST(lsp);
754 oid = SLIST_NEXT(oid, oid_link);
759 /* If writing isn't allowed */
760 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
761 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
764 /* Most likely only root can write */
765 if (!(oid->oid_kind & CTLFLAG_ANYBODY) &&
766 req->newptr && req->p &&
767 (i = suser_xxx(0, req->p,
768 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
771 if (!oid->oid_handler)
774 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
775 i = (oid->oid_handler) (oid,
776 name + indx, namelen - indx,
779 i = (oid->oid_handler) (oid,
780 oid->oid_arg1, oid->oid_arg2,
786 #ifndef _SYS_SYSPROTO_H_
798 __sysctl(struct proc *p, struct sysctl_args *uap)
800 int error, i, name[CTL_MAXNAME];
803 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
806 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
810 error = userland_sysctl(p, name, uap->namelen,
811 uap->old, uap->oldlenp, 0,
812 uap->new, uap->newlen, &j);
813 if (error && error != ENOMEM)
816 i = copyout(&j, uap->oldlenp, sizeof(j));
824 * This is used from various compatibility syscalls too. That's why name
825 * must be in kernel space.
828 userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
831 struct sysctl_req req, req2;
833 bzero(&req, sizeof req);
839 req.oldlen = *oldlenp;
841 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
848 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
854 if (!useracc(new, req.newlen, VM_PROT_READ))
860 req.oldfunc = sysctl_old_user;
861 req.newfunc = sysctl_new_user;
864 /* XXX this should probably be done in a general way */
865 while (memlock.sl_lock) {
867 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
874 error = sysctl_root(0, name, namelen, &req2);
875 } while (error == EAGAIN);
879 vsunlock(req.oldptr, req.oldlen, B_WRITE);
883 if (memlock.sl_want) {
885 wakeup((caddr_t)&memlock);
888 if (error && error != ENOMEM)
892 if (req.oldptr && req.oldidx > req.oldlen)
893 *retval = req.oldlen;
895 *retval = req.oldidx;
901 #include <sys/socket.h>
902 #include <vm/vm_param.h>
904 #define KINFO_PROC (0<<8)
905 #define KINFO_RT (1<<8)
906 #define KINFO_VNODE (2<<8)
907 #define KINFO_FILE (3<<8)
908 #define KINFO_METER (4<<8)
909 #define KINFO_LOADAVG (5<<8)
910 #define KINFO_CLOCKRATE (6<<8)
912 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
913 #define KINFO_BSDI_SYSINFO (101<<8)
916 * XXX this is bloat, but I hope it's better here than on the potentially
917 * limited kernel stack... -Peter
921 int bsdi_machine; /* "i386" on BSD/386 */
922 /* ^^^ this is an offset to the string, relative to the struct start */
931 int bsdi_ostype; /* "BSD/386" on BSD/386 */
932 int bsdi_osrelease; /* "1.1" on BSD/386 */
944 struct timeval pad16;
945 /* we dont set this, because BSDI's uname used gethostname() instead */
946 int bsdi_hostname; /* hostname on BSD/386 */
948 /* the actual string data is appended here */
952 * this data is appended to the end of the bsdi_si structure during copyout.
953 * The "char *" offsets are relative to the base of the bsdi_si struct.
954 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
955 * should not exceed the length of the buffer here... (or else!! :-)
957 static char bsdi_strings[80]; /* It had better be less than this! */
959 #ifndef _SYS_SYSPROTO_H_
960 struct getkerninfo_args {
969 ogetkerninfo(struct proc *p, struct getkerninfo_args *uap)
974 switch (uap->op & 0xff00) {
980 name[3] = (uap->op & 0xff0000) >> 16;
981 name[4] = uap->op & 0xff;
983 error = userland_sysctl(p, name, 6, uap->where, uap->size,
989 name[1] = KERN_VNODE;
990 error = userland_sysctl(p, name, 2, uap->where, uap->size,
997 name[2] = uap->op & 0xff;
999 error = userland_sysctl(p, name, 4, uap->where, uap->size,
1005 name[1] = KERN_FILE;
1006 error = userland_sysctl(p, name, 2, uap->where, uap->size,
1013 error = userland_sysctl(p, name, 2, uap->where, uap->size,
1019 name[1] = VM_LOADAVG;
1020 error = userland_sysctl(p, name, 2, uap->where, uap->size,
1024 case KINFO_CLOCKRATE:
1026 name[1] = KERN_CLOCKRATE;
1027 error = userland_sysctl(p, name, 2, uap->where, uap->size,
1031 case KINFO_BSDI_SYSINFO: {
1033 * this is pretty crude, but it's just enough for uname()
1034 * from BSDI's 1.x libc to work.
1036 * In particular, it doesn't return the same results when
1037 * the supplied buffer is too small. BSDI's version apparently
1038 * will return the amount copied, and set the *size to how
1039 * much was needed. The emulation framework here isn't capable
1040 * of that, so we just set both to the amount copied.
1041 * BSDI's 2.x product apparently fails with ENOMEM in this
1049 bzero((char *)&bsdi_si, sizeof(bsdi_si));
1050 bzero(bsdi_strings, sizeof(bsdi_strings));
1054 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1058 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1059 strcpy(s, osrelease);
1062 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1066 needed = sizeof(bsdi_si) + (s - bsdi_strings);
1068 if (uap->where == NULL) {
1069 /* process is asking how much buffer to supply.. */
1076 /* if too much buffer supplied, trim it down */
1080 /* how much of the buffer is remaining */
1083 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1086 /* is there any point in continuing? */
1087 if (left > sizeof(bsdi_si)) {
1088 left -= sizeof(bsdi_si);
1089 error = copyout(&bsdi_strings,
1090 uap->where + sizeof(bsdi_si), left);
1096 return (EOPNOTSUPP);
1100 p->p_retval[0] = size;
1102 error = copyout((caddr_t)&size, (caddr_t)uap->size,
1106 #endif /* COMPAT_43 */