nlookup - introduce nlookup_init_root
[dragonfly.git] / sys / kern / kern_sysctl.c
blob24bf124abf64689c3d41cff592f025894fc8acfc
1 /*-
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
13 * are met:
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
37 * SUCH DAMAGE.
39 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
40 * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.92.2.9 2003/05/01 22:48:09 trhodes Exp $
41 * $DragonFly: src/sys/kern/kern_sysctl.c,v 1.30 2008/08/03 11:00:32 sephe Exp $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/buf.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/priv.h>
52 #include <sys/sysproto.h>
53 #include <sys/lock.h>
55 #include <sys/mplock2.h>
57 #include <vm/vm.h>
58 #include <vm/vm_extern.h>
60 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
61 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
63 static struct lock sysctl_lkp;
64 static struct lock sysctl_ctx_lkp;
66 static void sysctl_lock(int type);
67 static void sysctl_unlock(void);
68 static void sysctl_ctx_lock(int type);
69 static void sysctl_ctx_unlock(void);
71 static int sysctl_root(SYSCTL_HANDLER_ARGS);
72 static void sysctl_register_oid_int(struct sysctl_oid *oipd);
73 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
74 static struct sysctl_ctx_entry* sysctl_ctx_entry_find_int
75 (struct sysctl_ctx_list *, struct sysctl_oid *oidp);
77 struct sysctl_oid_list sysctl__children; /* root list */
79 static struct sysctl_oid *
80 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
82 struct sysctl_oid *oidp;
84 SLIST_FOREACH(oidp, list, oid_link) {
85 if (strcmp(oidp->oid_name, name) == 0) {
86 break;
89 return (oidp);
93 * Initialization of the MIB tree.
95 * Order by number in each list.
98 void
99 sysctl_register_oid(struct sysctl_oid *oidp)
101 sysctl_lock(LK_EXCLUSIVE);
102 sysctl_register_oid_int(oidp);
103 sysctl_unlock();
106 static void
107 sysctl_register_oid_int(struct sysctl_oid *oidp)
109 struct sysctl_oid_list *parent = oidp->oid_parent;
110 struct sysctl_oid *p;
111 struct sysctl_oid *q;
114 * First check if another oid with the same name already
115 * exists in the parent's list.
117 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
118 if (p != NULL) {
119 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
120 p->oid_refcnt++;
121 else
122 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
123 return;
127 * If this oid has a number OID_AUTO, give it a number which
128 * is greater than any current oid. Make sure it is at least
129 * 256 to leave space for pre-assigned oid numbers.
131 if (oidp->oid_number == OID_AUTO) {
132 int newoid = 0x100; /* minimum AUTO oid */
135 * Adjust based on highest oid in parent list
137 SLIST_FOREACH(p, parent, oid_link) {
138 if (newoid <= p->oid_number)
139 newoid = p->oid_number + 1;
141 oidp->oid_number = newoid;
145 * Insert the oid into the parent's list in order.
147 q = NULL;
148 SLIST_FOREACH(p, parent, oid_link) {
149 if (oidp->oid_number < p->oid_number)
150 break;
151 q = p;
153 if (q)
154 SLIST_INSERT_AFTER(q, oidp, oid_link);
155 else
156 SLIST_INSERT_HEAD(parent, oidp, oid_link);
159 void
160 sysctl_unregister_oid(struct sysctl_oid *oidp)
162 sysctl_lock(LK_EXCLUSIVE);
163 sysctl_unregister_oid_int(oidp);
164 sysctl_unlock();
167 static void
168 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
170 struct sysctl_oid *p;
172 if (oidp->oid_number == OID_AUTO)
173 panic("Trying to unregister OID_AUTO entry: %p", oidp);
175 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
176 if (p != oidp)
177 continue;
178 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
179 return;
183 * This can happen when a module fails to register and is
184 * being unloaded afterwards. It should not be a panic()
185 * for normal use.
187 kprintf("%s: failed to unregister sysctl\n", __func__);
190 /* Initialize a new context to keep track of dynamically added sysctls. */
192 sysctl_ctx_init(struct sysctl_ctx_list *c)
194 if (c == NULL)
195 return(EINVAL);
196 TAILQ_INIT(c);
197 return(0);
200 /* Free the context, and destroy all dynamic oids registered in this context */
202 sysctl_ctx_free(struct sysctl_ctx_list *clist)
204 struct sysctl_ctx_entry *e, *e1;
205 int error;
207 error = 0;
208 sysctl_ctx_lock(LK_EXCLUSIVE);
210 * First perform a "dry run" to check if it's ok to remove oids.
211 * XXX FIXME
212 * XXX This algorithm is a hack. But I don't know any
213 * XXX better solution for now...
215 TAILQ_FOREACH(e, clist, link) {
216 error = sysctl_remove_oid(e->entry, 0, 0);
217 if (error)
218 break;
221 * Restore deregistered entries, either from the end,
222 * or from the place where error occured.
223 * e contains the entry that was not unregistered
225 if (error)
226 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
227 else
228 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
229 while (e1 != NULL) {
230 sysctl_register_oid(e1->entry);
231 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
233 if (error) {
234 sysctl_ctx_unlock();
235 return(EBUSY);
237 /* Now really delete the entries */
238 e = TAILQ_FIRST(clist);
239 while (e != NULL) {
240 e1 = TAILQ_NEXT(e, link);
241 error = sysctl_remove_oid(e->entry, 1, 0);
242 if (error)
243 panic("sysctl_remove_oid: corrupt tree, entry: %s",
244 e->entry->oid_name);
245 kfree(e, M_SYSCTLOID);
246 e = e1;
248 sysctl_ctx_unlock();
249 return (error);
252 /* Add an entry to the context */
253 struct sysctl_ctx_entry *
254 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
256 struct sysctl_ctx_entry *e;
258 if (clist == NULL || oidp == NULL)
259 return(NULL);
260 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
261 e->entry = oidp;
262 sysctl_ctx_lock(LK_EXCLUSIVE);
263 TAILQ_INSERT_HEAD(clist, e, link);
264 sysctl_ctx_unlock();
265 return (e);
268 /* Find an entry in the context */
269 struct sysctl_ctx_entry *
270 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
272 struct sysctl_ctx_entry *e;
274 if (clist == NULL || oidp == NULL)
275 return(NULL);
277 sysctl_ctx_lock(LK_SHARED);
278 e = sysctl_ctx_entry_find_int(clist, oidp);
279 sysctl_ctx_unlock();
281 return(e);
284 struct sysctl_ctx_entry *
285 sysctl_ctx_entry_find_int(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
287 struct sysctl_ctx_entry *e;
289 KKASSERT(clist != NULL && oidp != NULL);
291 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
292 if(e->entry == oidp)
293 break;
296 return (e);
300 * Delete an entry from the context.
301 * NOTE: this function doesn't free oidp! You have to remove it
302 * with sysctl_remove_oid().
305 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
307 struct sysctl_ctx_entry *e;
309 if (clist == NULL || oidp == NULL)
310 return (EINVAL);
312 sysctl_ctx_lock(LK_EXCLUSIVE);
313 e = sysctl_ctx_entry_find_int(clist, oidp);
314 if (e == NULL) {
315 sysctl_ctx_unlock();
316 return (ENOENT);
318 TAILQ_REMOVE(clist, e, link);
319 kfree(e, M_SYSCTLOID);
320 sysctl_ctx_unlock();
322 return(0);
326 * Remove dynamically created sysctl trees.
327 * oidp - top of the tree to be removed
328 * del - if 0 - just deregister, otherwise free up entries as well
329 * recurse - if != 0 traverse the subtree to be deleted
332 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
334 struct sysctl_oid *p;
335 int error;
337 if (oidp == NULL)
338 return(EINVAL);
339 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
340 kprintf("can't remove non-dynamic nodes!\n");
341 return (EINVAL);
343 sysctl_lock(LK_EXCLUSIVE | LK_CANRECURSE);
345 * WARNING: normal method to do this should be through
346 * sysctl_ctx_free(). Use recursing as the last resort
347 * method to purge your sysctl tree of leftovers...
348 * However, if some other code still references these nodes,
349 * it will panic.
351 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
352 if (oidp->oid_refcnt == 1) {
353 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
354 if (!recurse) {
355 sysctl_unlock();
356 return(ENOTEMPTY);
358 error = sysctl_remove_oid(p, del, recurse);
359 if (error) {
360 sysctl_unlock();
361 return(error);
364 if (del)
365 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
368 if (oidp->oid_refcnt > 1 ) {
369 oidp->oid_refcnt--;
370 } else {
371 if (oidp->oid_refcnt == 0) {
372 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
373 oidp->oid_refcnt, oidp->oid_name);
374 sysctl_unlock();
375 return(EINVAL);
377 sysctl_unregister_oid_int(oidp);
378 if (del) {
379 if (oidp->oid_descr)
380 kfree(__DECONST(char *,oidp->oid_descr),
381 M_SYSCTLOID);
382 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
383 kfree(oidp, M_SYSCTLOID);
386 sysctl_unlock();
387 return(0);
391 * Create new sysctls at run time.
392 * clist may point to a valid context initialized with sysctl_ctx_init().
394 struct sysctl_oid *
395 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
396 int number, const char *name, int kind, void *arg1, int arg2,
397 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
399 struct sysctl_oid *oidp;
400 ssize_t len;
401 char *newname;
403 /* You have to hook up somewhere.. */
404 if (parent == NULL)
405 return(NULL);
406 sysctl_lock(LK_EXCLUSIVE);
407 /* Check if the node already exists, otherwise create it */
408 oidp = sysctl_find_oidname(name, parent, 0);
409 if (oidp != NULL) {
410 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
411 oidp->oid_refcnt++;
412 /* Update the context */
413 if (clist != NULL)
414 sysctl_ctx_entry_add(clist, oidp);
415 sysctl_unlock();
416 return (oidp);
417 } else {
418 kprintf("can't re-use a leaf (%s)!\n", name);
419 sysctl_unlock();
420 return (NULL);
423 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
424 oidp->oid_parent = parent;
425 SLIST_NEXT(oidp, oid_link) = NULL;
426 oidp->oid_number = number;
427 oidp->oid_refcnt = 1;
428 len = strlen(name);
429 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
430 bcopy(name, newname, len + 1);
431 newname[len] = '\0';
432 oidp->oid_name = newname;
433 oidp->oid_handler = handler;
434 oidp->oid_kind = CTLFLAG_DYN | kind;
435 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
436 struct sysctl_oid_list *children;
438 /* Allocate space for children */
439 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
440 SYSCTL_SET_CHILDREN(oidp, children);
441 SLIST_INIT(children);
442 } else {
443 oidp->oid_arg1 = arg1;
444 oidp->oid_arg2 = arg2;
446 oidp->oid_fmt = fmt;
447 if (descr) {
448 int len = strlen(descr) + 1;
449 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
450 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
452 /* Update the context, if used */
453 if (clist != NULL)
454 sysctl_ctx_entry_add(clist, oidp);
455 /* Register this oid */
456 sysctl_register_oid_int(oidp);
457 sysctl_unlock();
458 return (oidp);
462 * Register the kernel's oids on startup.
464 SET_DECLARE(sysctl_set, struct sysctl_oid);
466 static void
467 sysctl_register_all(void *arg)
469 struct sysctl_oid **oidp;
471 lockinit(&sysctl_lkp, "sysctl", 0, 0);
472 lockinit(&sysctl_ctx_lkp, "sysctl ctx", 0, 0);
473 SET_FOREACH(oidp, sysctl_set)
474 sysctl_register_oid_int(*oidp);
477 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
480 * "Staff-functions"
482 * These functions implement a presently undocumented interface
483 * used by the sysctl program to walk the tree, and get the type
484 * so it can print the value.
485 * This interface is under work and consideration, and should probably
486 * be killed with a big axe by the first person who can find the time.
487 * (be aware though, that the proper interface isn't as obvious as it
488 * may seem, there are various conflicting requirements.
490 * {0,0} kprintf the entire MIB-tree.
491 * {0,1,...} return the name of the "..." OID.
492 * {0,2,...} return the next OID.
493 * {0,3} return the OID of the name in "new"
494 * {0,4,...} return the kind & format info for the "..." OID.
497 static void
498 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
500 int k;
501 struct sysctl_oid *oidp;
503 sysctl_lock(LK_SHARED);
504 SLIST_FOREACH(oidp, l, oid_link) {
506 for (k=0; k<i; k++)
507 kprintf(" ");
509 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
511 kprintf("%c%c",
512 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
513 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
515 if (oidp->oid_handler)
516 kprintf(" *Handler");
518 switch (oidp->oid_kind & CTLTYPE) {
519 case CTLTYPE_NODE:
520 kprintf(" Node\n");
521 if (!oidp->oid_handler) {
522 sysctl_sysctl_debug_dump_node(
523 oidp->oid_arg1, i+2);
525 break;
526 case CTLTYPE_INT: kprintf(" Int\n"); break;
527 case CTLTYPE_STRING: kprintf(" String\n"); break;
528 case CTLTYPE_QUAD: kprintf(" Quad\n"); break;
529 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
530 default: kprintf("\n");
534 sysctl_unlock();
537 static int
538 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
540 int error;
542 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
543 if (error)
544 return error;
545 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
546 return ENOENT;
549 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
550 0, 0, sysctl_sysctl_debug, "-", "");
552 static int
553 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
555 int *name = (int *) arg1;
556 u_int namelen = arg2;
557 int error = 0;
558 struct sysctl_oid *oid;
559 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
560 char buf[10];
562 sysctl_lock(LK_SHARED);
563 while (namelen) {
564 if (!lsp) {
565 ksnprintf(buf,sizeof(buf),"%d",*name);
566 if (req->oldidx)
567 error = SYSCTL_OUT(req, ".", 1);
568 if (!error)
569 error = SYSCTL_OUT(req, buf, strlen(buf));
570 if (error) {
571 sysctl_unlock();
572 return (error);
574 namelen--;
575 name++;
576 continue;
578 lsp2 = 0;
579 SLIST_FOREACH(oid, lsp, oid_link) {
580 if (oid->oid_number != *name)
581 continue;
583 if (req->oldidx)
584 error = SYSCTL_OUT(req, ".", 1);
585 if (!error)
586 error = SYSCTL_OUT(req, oid->oid_name,
587 strlen(oid->oid_name));
588 if (error) {
589 sysctl_unlock();
590 return (error);
593 namelen--;
594 name++;
596 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
597 break;
599 if (oid->oid_handler)
600 break;
602 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
603 break;
605 lsp = lsp2;
607 sysctl_unlock();
608 return (SYSCTL_OUT(req, "", 1));
611 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
613 static int
614 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
615 int *next, int *len, int level, struct sysctl_oid **oidpp)
617 struct sysctl_oid *oidp;
619 *len = level;
620 sysctl_lock(LK_SHARED);
621 SLIST_FOREACH(oidp, lsp, oid_link) {
622 *next = oidp->oid_number;
623 *oidpp = oidp;
625 if (!namelen) {
626 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
627 sysctl_unlock();
628 return 0;
630 if (oidp->oid_handler) {
631 /* We really should call the handler here...*/
632 sysctl_unlock();
633 return 0;
635 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
636 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
637 len, level+1, oidpp)) {
638 sysctl_unlock();
639 return 0;
641 goto emptynode;
644 if (oidp->oid_number < *name)
645 continue;
647 if (oidp->oid_number > *name) {
648 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
649 sysctl_unlock();
650 return 0;
652 if (oidp->oid_handler) {
653 sysctl_unlock();
654 return 0;
656 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
657 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
658 next+1, len, level+1, oidpp)) {
659 sysctl_unlock();
660 return (0);
662 goto next;
664 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
665 continue;
667 if (oidp->oid_handler)
668 continue;
670 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
671 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
672 len, level+1, oidpp)) {
673 sysctl_unlock();
674 return (0);
676 next:
677 namelen = 1;
678 *len = level;
679 emptynode:
680 *len = level;
682 sysctl_unlock();
683 return 1;
686 static int
687 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
689 int *name = (int *) arg1;
690 u_int namelen = arg2;
691 int i, j, error;
692 struct sysctl_oid *oid;
693 struct sysctl_oid_list *lsp = &sysctl__children;
694 int newoid[CTL_MAXNAME];
696 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
697 if (i)
698 return ENOENT;
699 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
700 return (error);
703 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
705 static int
706 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
708 int i;
709 struct sysctl_oid *oidp;
710 struct sysctl_oid_list *lsp = &sysctl__children;
711 char *p;
713 if (!*name)
714 return ENOENT;
716 p = name + strlen(name) - 1 ;
717 if (*p == '.')
718 *p = '\0';
720 *len = 0;
722 for (p = name; *p && *p != '.'; p++)
724 i = *p;
725 if (i == '.')
726 *p = '\0';
728 sysctl_lock(LK_SHARED);
729 oidp = SLIST_FIRST(lsp);
731 while (oidp && *len < CTL_MAXNAME) {
732 if (strcmp(name, oidp->oid_name)) {
733 oidp = SLIST_NEXT(oidp, oid_link);
734 continue;
736 *oid++ = oidp->oid_number;
737 (*len)++;
739 if (!i) {
740 if (oidpp)
741 *oidpp = oidp;
742 sysctl_unlock();
743 return (0);
746 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
747 break;
749 if (oidp->oid_handler)
750 break;
752 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
753 oidp = SLIST_FIRST(lsp);
754 name = p+1;
755 for (p = name; *p && *p != '.'; p++)
757 i = *p;
758 if (i == '.')
759 *p = '\0';
761 sysctl_unlock();
762 return ENOENT;
765 static int
766 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
768 char *p;
769 int error, oid[CTL_MAXNAME], len;
770 struct sysctl_oid *op = 0;
772 if (!req->newlen)
773 return ENOENT;
774 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
775 return (ENAMETOOLONG);
777 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
779 error = SYSCTL_IN(req, p, req->newlen);
780 if (error) {
781 kfree(p, M_SYSCTL);
782 return (error);
785 p [req->newlen] = '\0';
787 error = name2oid(p, oid, &len, &op);
789 kfree(p, M_SYSCTL);
791 if (error)
792 return (error);
794 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
795 return (error);
798 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
799 sysctl_sysctl_name2oid, "I", "");
801 static int
802 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
804 struct sysctl_oid *oid;
805 int error;
807 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
808 if (error)
809 return (error);
811 if (!oid->oid_fmt)
812 return (ENOENT);
813 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
814 if (error)
815 return (error);
816 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
817 return (error);
821 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
823 static int
824 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
826 struct sysctl_oid *oid;
827 int error;
829 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
830 if (error)
831 return (error);
833 if (!oid->oid_descr)
834 return (ENOENT);
835 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
836 return (error);
839 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
842 * Default "handler" functions.
846 * Handle an int, signed or unsigned.
847 * Two cases:
848 * a variable: point arg1 at it.
849 * a constant: pass it in arg2.
853 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
855 int error = 0;
857 if (arg1)
858 error = SYSCTL_OUT(req, arg1, sizeof(int));
859 else
860 error = SYSCTL_OUT(req, &arg2, sizeof(int));
862 if (error || !req->newptr)
863 return (error);
865 if (!arg1)
866 error = EPERM;
867 else
868 error = SYSCTL_IN(req, arg1, sizeof(int));
869 return (error);
873 * Handle a long, signed or unsigned. arg1 points to it.
877 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
879 int error = 0;
881 if (!arg1)
882 return (EINVAL);
883 error = SYSCTL_OUT(req, arg1, sizeof(long));
885 if (error || !req->newptr)
886 return (error);
888 error = SYSCTL_IN(req, arg1, sizeof(long));
889 return (error);
893 * Handle a quad, signed or unsigned. arg1 points to it.
897 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
899 int error = 0;
901 if (!arg1)
902 return (EINVAL);
903 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
905 if (error || !req->newptr)
906 return (error);
908 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
909 return (error);
913 * Handle our generic '\0' terminated 'C' string.
914 * Two cases:
915 * a variable string: point arg1 at it, arg2 is max length.
916 * a constant string: point arg1 at it, arg2 is zero.
920 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
922 int error=0;
924 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
926 if (error || !req->newptr)
927 return (error);
929 if ((req->newlen - req->newidx) >= arg2) {
930 error = EINVAL;
931 } else {
932 arg2 = (req->newlen - req->newidx);
933 error = SYSCTL_IN(req, arg1, arg2);
934 ((char *)arg1)[arg2] = '\0';
937 return (error);
941 * Handle any kind of opaque data.
942 * arg1 points to it, arg2 is the size.
946 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
948 int error;
950 error = SYSCTL_OUT(req, arg1, arg2);
952 if (error || !req->newptr)
953 return (error);
955 error = SYSCTL_IN(req, arg1, arg2);
957 return (error);
961 * Transfer functions to/from kernel space.
962 * XXX: rather untested at this point
964 static int
965 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
967 size_t i = 0;
969 if (req->oldptr) {
970 i = l;
971 if (i > req->oldlen - req->oldidx)
972 i = req->oldlen - req->oldidx;
973 if (i > 0)
974 bcopy(p, (char *)req->oldptr + req->oldidx, i);
976 req->oldidx += l;
977 if (req->oldptr && i != l)
978 return (ENOMEM);
979 return (0);
982 static int
983 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
986 if (!req->newptr)
987 return 0;
988 if (req->newlen - req->newidx < l)
989 return (EINVAL);
990 bcopy((char *)req->newptr + req->newidx, p, l);
991 req->newidx += l;
992 return (0);
996 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
998 int error = 0;
999 struct sysctl_req req;
1001 bzero(&req, sizeof req);
1003 req.td = curthread;
1005 if (oldlenp) {
1006 req.oldlen = *oldlenp;
1009 if (old) {
1010 req.oldptr = old;
1013 if (new != NULL) {
1014 req.newlen = newlen;
1015 req.newptr = new;
1018 req.oldfunc = sysctl_old_kernel;
1019 req.newfunc = sysctl_new_kernel;
1020 #if 0
1021 req.lock = 1;
1022 #endif
1024 sysctl_lock(LK_SHARED);
1026 error = sysctl_root(0, name, namelen, &req);
1028 #if 0
1029 if (req.lock == 2)
1030 vsunlock(req.oldptr, req.oldlen);
1031 #endif
1033 sysctl_unlock();
1035 if (error && error != ENOMEM)
1036 return (error);
1038 if (retval) {
1039 if (req.oldptr && req.oldidx > req.oldlen)
1040 *retval = req.oldlen;
1041 else
1042 *retval = req.oldidx;
1044 return (error);
1048 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1049 void *new, size_t newlen, size_t *retval)
1051 int oid[CTL_MAXNAME];
1052 size_t oidlen, plen;
1053 int error;
1055 oid[0] = 0; /* sysctl internal magic */
1056 oid[1] = 3; /* name2oid */
1057 oidlen = sizeof(oid);
1059 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
1060 strlen(name), &plen);
1061 if (error)
1062 return (error);
1064 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1065 new, newlen, retval);
1066 return (error);
1070 * Transfer function to/from user space.
1072 static int
1073 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1075 int error = 0;
1076 size_t i = 0;
1078 #if 0
1079 if (req->lock == 1 && req->oldptr) {
1080 vslock(req->oldptr, req->oldlen);
1081 req->lock = 2;
1083 #endif
1084 if (req->oldptr) {
1085 i = l;
1086 if (i > req->oldlen - req->oldidx)
1087 i = req->oldlen - req->oldidx;
1088 if (i > 0)
1089 error = copyout(p, (char *)req->oldptr + req->oldidx,
1092 req->oldidx += l;
1093 if (error)
1094 return (error);
1095 if (req->oldptr && i < l)
1096 return (ENOMEM);
1097 return (0);
1100 static int
1101 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1103 int error;
1105 if (!req->newptr)
1106 return 0;
1107 if (req->newlen - req->newidx < l)
1108 return (EINVAL);
1109 error = copyin((char *)req->newptr + req->newidx, p, l);
1110 req->newidx += l;
1111 return (error);
1115 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1116 int *nindx, struct sysctl_req *req)
1118 struct sysctl_oid *oid;
1119 int indx;
1121 sysctl_lock(LK_SHARED);
1122 oid = SLIST_FIRST(&sysctl__children);
1123 indx = 0;
1124 while (oid && indx < CTL_MAXNAME) {
1125 if (oid->oid_number == name[indx]) {
1126 indx++;
1127 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1128 if (oid->oid_handler != NULL ||
1129 indx == namelen) {
1130 *noid = oid;
1131 if (nindx != NULL)
1132 *nindx = indx;
1133 sysctl_unlock();
1134 return (0);
1136 oid = SLIST_FIRST(
1137 (struct sysctl_oid_list *)oid->oid_arg1);
1138 } else if (indx == namelen) {
1139 *noid = oid;
1140 if (nindx != NULL)
1141 *nindx = indx;
1142 sysctl_unlock();
1143 return (0);
1144 } else {
1145 sysctl_unlock();
1146 return (ENOTDIR);
1148 } else {
1149 oid = SLIST_NEXT(oid, oid_link);
1152 sysctl_unlock();
1153 return (ENOENT);
1157 * Traverse our tree, and find the right node, execute whatever it points
1158 * to, and return the resulting error code.
1162 sysctl_root(SYSCTL_HANDLER_ARGS)
1164 struct thread *td = req->td;
1165 struct proc *p = td ? td->td_proc : NULL;
1166 struct sysctl_oid *oid;
1167 int error, indx;
1169 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1170 if (error)
1171 return (error);
1173 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1175 * You can't call a sysctl when it's a node, but has
1176 * no handler. Inform the user that it's a node.
1177 * The indx may or may not be the same as namelen.
1179 if (oid->oid_handler == NULL)
1180 return (EISDIR);
1183 /* If writing isn't allowed */
1184 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1185 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1186 return (EPERM);
1188 /* Most likely only root can write */
1189 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1190 (error = priv_check_cred(td->td_ucred,
1191 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1192 PRIV_SYSCTL_WRITE, 0)))
1193 return (error);
1195 if (!oid->oid_handler)
1196 return EINVAL;
1198 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1199 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1200 req);
1201 else
1202 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1203 req);
1204 return (error);
1208 * MPALMOSTSAFE
1211 sys___sysctl(struct sysctl_args *uap)
1213 int error, i, name[CTL_MAXNAME];
1214 size_t j;
1216 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1217 return (EINVAL);
1219 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1220 if (error)
1221 return (error);
1223 get_mplock();
1224 error = userland_sysctl(name, uap->namelen,
1225 uap->old, uap->oldlenp, 0,
1226 uap->new, uap->newlen, &j);
1227 rel_mplock();
1228 if (error && error != ENOMEM)
1229 return (error);
1230 if (uap->oldlenp) {
1231 i = copyout(&j, uap->oldlenp, sizeof(j));
1232 if (i)
1233 return (i);
1235 return (error);
1239 * This is used from various compatibility syscalls too. That's why name
1240 * must be in kernel space.
1243 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1245 int error = 0;
1246 struct sysctl_req req, req2;
1248 bzero(&req, sizeof req);
1250 if (oldlenp) {
1251 if (inkernel) {
1252 req.oldlen = *oldlenp;
1253 } else {
1254 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1255 if (error)
1256 return (error);
1260 if (old) {
1261 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1262 return (EFAULT);
1263 req.oldptr= old;
1266 if (new != NULL) {
1267 if (!useracc(new, newlen, VM_PROT_READ))
1268 return (EFAULT);
1269 req.newlen = newlen;
1270 req.newptr = new;
1273 req.oldfunc = sysctl_old_user;
1274 req.newfunc = sysctl_new_user;
1275 #if 0
1276 req.lock = 1;
1277 #endif
1278 req.td = curthread;
1280 sysctl_lock(LK_SHARED);
1282 do {
1283 req2 = req;
1284 error = sysctl_root(0, name, namelen, &req2);
1285 } while (error == EAGAIN);
1287 req = req2;
1288 #if 0
1289 if (req.lock == 2)
1290 vsunlock(req.oldptr, req.oldlen);
1291 #endif
1293 sysctl_unlock();
1295 if (error && error != ENOMEM)
1296 return (error);
1298 if (retval) {
1299 if (req.oldptr && req.oldidx > req.oldlen)
1300 *retval = req.oldlen;
1301 else
1302 *retval = req.oldidx;
1304 return (error);
1307 static void
1308 sysctl_lock(int flag)
1310 lockmgr(&sysctl_lkp, flag);
1313 static void
1314 sysctl_unlock(void)
1316 lockmgr(&sysctl_lkp, LK_RELEASE);
1319 static void
1320 sysctl_ctx_lock(int flag)
1322 lockmgr(&sysctl_ctx_lkp, flag);
1325 static void
1326 sysctl_ctx_unlock(void)
1328 lockmgr(&sysctl_ctx_lkp, LK_RELEASE);
1332 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1334 int error, value;
1336 value = *(int *)arg1;
1337 error = sysctl_handle_int(oidp, &value, 0, req);
1338 if (error || !req->newptr)
1339 return (error);
1340 if (value < low || value > high)
1341 return (EINVAL);
1342 *(int *)arg1 = value;
1343 return (0);