Remove bogus (void *) casts.
[dragonfly.git] / sys / kern / kern_sysctl.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
10 *
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.
26 *
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.
38 *
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 */
42
984263bc
MD
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/buf.h>
47#include <sys/sysctl.h>
48#include <sys/malloc.h>
49#include <sys/proc.h>
895c1f85 50#include <sys/priv.h>
984263bc 51#include <sys/sysproto.h>
853af6eb 52#include <sys/lock.h>
684a93c4
MD
53
54#include <sys/mplock2.h>
55
984263bc
MD
56#include <vm/vm.h>
57#include <vm/vm_extern.h>
58
59static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
60static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
61
853af6eb
JS
62static struct lock sysctl_lkp;
63static struct lock sysctl_ctx_lkp;
64
65static void sysctl_lock(int type);
66static void sysctl_unlock(void);
67static void sysctl_ctx_lock(int type);
68static void sysctl_ctx_unlock(void);
984263bc 69
853af6eb
JS
70static int sysctl_root(SYSCTL_HANDLER_ARGS);
71static void sysctl_register_oid_int(struct sysctl_oid *oipd);
72static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
73static struct sysctl_ctx_entry* sysctl_ctx_entry_find_int
74 (struct sysctl_ctx_list *, struct sysctl_oid *oidp);
984263bc
MD
75
76struct sysctl_oid_list sysctl__children; /* root list */
77
78static struct sysctl_oid *
853af6eb 79sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
984263bc
MD
80{
81 struct sysctl_oid *oidp;
82
83 SLIST_FOREACH(oidp, list, oid_link) {
84 if (strcmp(oidp->oid_name, name) == 0) {
853af6eb 85 break;
984263bc
MD
86 }
87 }
853af6eb 88 return (oidp);
984263bc
MD
89}
90
91/*
92 * Initialization of the MIB tree.
93 *
94 * Order by number in each list.
95 */
96
853af6eb
JS
97void
98sysctl_register_oid(struct sysctl_oid *oidp)
99{
100 sysctl_lock(LK_EXCLUSIVE);
101 sysctl_register_oid_int(oidp);
102 sysctl_unlock();
103}
104
105static void
106sysctl_register_oid_int(struct sysctl_oid *oidp)
984263bc
MD
107{
108 struct sysctl_oid_list *parent = oidp->oid_parent;
109 struct sysctl_oid *p;
110 struct sysctl_oid *q;
984263bc
MD
111
112 /*
113 * First check if another oid with the same name already
114 * exists in the parent's list.
115 */
853af6eb 116 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
984263bc 117 if (p != NULL) {
853af6eb 118 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
984263bc 119 p->oid_refcnt++;
853af6eb 120 else
6ea70f76 121 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
853af6eb 122 return;
984263bc 123 }
853af6eb 124
984263bc
MD
125 /*
126 * If this oid has a number OID_AUTO, give it a number which
127 * is greater than any current oid. Make sure it is at least
1e32b918 128 * 256 to leave space for pre-assigned oid numbers.
984263bc
MD
129 */
130 if (oidp->oid_number == OID_AUTO) {
1e32b918
MD
131 int newoid = 0x100; /* minimum AUTO oid */
132
133 /*
134 * Adjust based on highest oid in parent list
135 */
136 SLIST_FOREACH(p, parent, oid_link) {
137 if (newoid <= p->oid_number)
138 newoid = p->oid_number + 1;
139 }
140 oidp->oid_number = newoid;
984263bc
MD
141 }
142
143 /*
144 * Insert the oid into the parent's list in order.
145 */
146 q = NULL;
147 SLIST_FOREACH(p, parent, oid_link) {
148 if (oidp->oid_number < p->oid_number)
149 break;
150 q = p;
151 }
152 if (q)
153 SLIST_INSERT_AFTER(q, oidp, oid_link);
154 else
155 SLIST_INSERT_HEAD(parent, oidp, oid_link);
156}
157
853af6eb
JS
158void
159sysctl_unregister_oid(struct sysctl_oid *oidp)
160{
161 sysctl_lock(LK_EXCLUSIVE);
162 sysctl_unregister_oid_int(oidp);
163 sysctl_unlock();
164}
165
166static void
167sysctl_unregister_oid_int(struct sysctl_oid *oidp)
984263bc 168{
fef0fdf2 169 struct sysctl_oid *p;
984263bc 170
853af6eb 171 if (oidp->oid_number == OID_AUTO)
fc92d4aa 172 panic("Trying to unregister OID_AUTO entry: %p", oidp);
853af6eb
JS
173
174 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
175 if (p != oidp)
176 continue;
177 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
178 return;
fef0fdf2
MD
179 }
180
181 /*
182 * This can happen when a module fails to register and is
183 * being unloaded afterwards. It should not be a panic()
184 * for normal use.
185 */
6ea70f76 186 kprintf("%s: failed to unregister sysctl\n", __func__);
984263bc
MD
187}
188
189/* Initialize a new context to keep track of dynamically added sysctls. */
190int
191sysctl_ctx_init(struct sysctl_ctx_list *c)
192{
853af6eb
JS
193 if (c == NULL)
194 return(EINVAL);
984263bc 195 TAILQ_INIT(c);
853af6eb 196 return(0);
984263bc
MD
197}
198
199/* Free the context, and destroy all dynamic oids registered in this context */
200int
201sysctl_ctx_free(struct sysctl_ctx_list *clist)
202{
203 struct sysctl_ctx_entry *e, *e1;
204 int error;
205
206 error = 0;
853af6eb 207 sysctl_ctx_lock(LK_EXCLUSIVE);
984263bc
MD
208 /*
209 * First perform a "dry run" to check if it's ok to remove oids.
210 * XXX FIXME
211 * XXX This algorithm is a hack. But I don't know any
212 * XXX better solution for now...
213 */
214 TAILQ_FOREACH(e, clist, link) {
215 error = sysctl_remove_oid(e->entry, 0, 0);
216 if (error)
217 break;
218 }
219 /*
220 * Restore deregistered entries, either from the end,
221 * or from the place where error occured.
222 * e contains the entry that was not unregistered
223 */
224 if (error)
225 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
226 else
227 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
228 while (e1 != NULL) {
229 sysctl_register_oid(e1->entry);
230 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
231 }
853af6eb
JS
232 if (error) {
233 sysctl_ctx_unlock();
984263bc 234 return(EBUSY);
853af6eb 235 }
984263bc
MD
236 /* Now really delete the entries */
237 e = TAILQ_FIRST(clist);
238 while (e != NULL) {
239 e1 = TAILQ_NEXT(e, link);
240 error = sysctl_remove_oid(e->entry, 1, 0);
241 if (error)
242 panic("sysctl_remove_oid: corrupt tree, entry: %s",
243 e->entry->oid_name);
efda3bd0 244 kfree(e, M_SYSCTLOID);
984263bc
MD
245 e = e1;
246 }
853af6eb 247 sysctl_ctx_unlock();
984263bc
MD
248 return (error);
249}
250
251/* Add an entry to the context */
252struct sysctl_ctx_entry *
253sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
254{
255 struct sysctl_ctx_entry *e;
256
257 if (clist == NULL || oidp == NULL)
258 return(NULL);
efda3bd0 259 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
984263bc 260 e->entry = oidp;
853af6eb 261 sysctl_ctx_lock(LK_EXCLUSIVE);
984263bc 262 TAILQ_INSERT_HEAD(clist, e, link);
853af6eb 263 sysctl_ctx_unlock();
984263bc
MD
264 return (e);
265}
266
267/* Find an entry in the context */
268struct sysctl_ctx_entry *
269sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
270{
271 struct sysctl_ctx_entry *e;
272
273 if (clist == NULL || oidp == NULL)
274 return(NULL);
853af6eb
JS
275
276 sysctl_ctx_lock(LK_SHARED);
277 e = sysctl_ctx_entry_find_int(clist, oidp);
278 sysctl_ctx_unlock();
279
280 return(e);
281}
282
283struct sysctl_ctx_entry *
284sysctl_ctx_entry_find_int(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
285{
286 struct sysctl_ctx_entry *e;
287
288 KKASSERT(clist != NULL && oidp != NULL);
289
984263bc
MD
290 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
291 if(e->entry == oidp)
853af6eb 292 break;
984263bc 293 }
853af6eb 294
984263bc
MD
295 return (e);
296}
297
298/*
299 * Delete an entry from the context.
300 * NOTE: this function doesn't free oidp! You have to remove it
301 * with sysctl_remove_oid().
302 */
303int
304sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
305{
306 struct sysctl_ctx_entry *e;
307
308 if (clist == NULL || oidp == NULL)
309 return (EINVAL);
853af6eb
JS
310
311 sysctl_ctx_lock(LK_EXCLUSIVE);
312 e = sysctl_ctx_entry_find_int(clist, oidp);
313 if (e == NULL) {
314 sysctl_ctx_unlock();
984263bc 315 return (ENOENT);
853af6eb
JS
316 }
317 TAILQ_REMOVE(clist, e, link);
efda3bd0 318 kfree(e, M_SYSCTLOID);
853af6eb
JS
319 sysctl_ctx_unlock();
320
321 return(0);
984263bc
MD
322}
323
324/*
325 * Remove dynamically created sysctl trees.
326 * oidp - top of the tree to be removed
327 * del - if 0 - just deregister, otherwise free up entries as well
328 * recurse - if != 0 traverse the subtree to be deleted
329 */
330int
331sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
332{
333 struct sysctl_oid *p;
334 int error;
335
336 if (oidp == NULL)
337 return(EINVAL);
338 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
6ea70f76 339 kprintf("can't remove non-dynamic nodes!\n");
984263bc
MD
340 return (EINVAL);
341 }
853af6eb 342 sysctl_lock(LK_EXCLUSIVE | LK_CANRECURSE);
984263bc
MD
343 /*
344 * WARNING: normal method to do this should be through
345 * sysctl_ctx_free(). Use recursing as the last resort
346 * method to purge your sysctl tree of leftovers...
347 * However, if some other code still references these nodes,
348 * it will panic.
349 */
350 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
351 if (oidp->oid_refcnt == 1) {
352 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
853af6eb
JS
353 if (!recurse) {
354 sysctl_unlock();
355 return(ENOTEMPTY);
356 }
984263bc 357 error = sysctl_remove_oid(p, del, recurse);
853af6eb
JS
358 if (error) {
359 sysctl_unlock();
360 return(error);
361 }
984263bc
MD
362 }
363 if (del)
efda3bd0 364 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
984263bc
MD
365 }
366 }
367 if (oidp->oid_refcnt > 1 ) {
368 oidp->oid_refcnt--;
369 } else {
370 if (oidp->oid_refcnt == 0) {
6ea70f76 371 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
853af6eb
JS
372 oidp->oid_refcnt, oidp->oid_name);
373 sysctl_unlock();
374 return(EINVAL);
984263bc 375 }
853af6eb 376 sysctl_unregister_oid_int(oidp);
984263bc
MD
377 if (del) {
378 if (oidp->oid_descr)
efda3bd0 379 kfree(__DECONST(char *,oidp->oid_descr),
853af6eb 380 M_SYSCTLOID);
efda3bd0
MD
381 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
382 kfree(oidp, M_SYSCTLOID);
984263bc
MD
383 }
384 }
853af6eb
JS
385 sysctl_unlock();
386 return(0);
984263bc
MD
387}
388
389/*
390 * Create new sysctls at run time.
391 * clist may point to a valid context initialized with sysctl_ctx_init().
392 */
393struct sysctl_oid *
394sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
395 int number, const char *name, int kind, void *arg1, int arg2,
396 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
397{
398 struct sysctl_oid *oidp;
399 ssize_t len;
400 char *newname;
401
402 /* You have to hook up somewhere.. */
403 if (parent == NULL)
404 return(NULL);
853af6eb 405 sysctl_lock(LK_EXCLUSIVE);
984263bc 406 /* Check if the node already exists, otherwise create it */
853af6eb 407 oidp = sysctl_find_oidname(name, parent, 0);
984263bc
MD
408 if (oidp != NULL) {
409 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
410 oidp->oid_refcnt++;
411 /* Update the context */
412 if (clist != NULL)
413 sysctl_ctx_entry_add(clist, oidp);
853af6eb 414 sysctl_unlock();
984263bc
MD
415 return (oidp);
416 } else {
6ea70f76 417 kprintf("can't re-use a leaf (%s)!\n", name);
853af6eb 418 sysctl_unlock();
984263bc
MD
419 return (NULL);
420 }
421 }
efda3bd0 422 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
984263bc
MD
423 oidp->oid_parent = parent;
424 SLIST_NEXT(oidp, oid_link) = NULL;
425 oidp->oid_number = number;
426 oidp->oid_refcnt = 1;
427 len = strlen(name);
efda3bd0 428 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
984263bc
MD
429 bcopy(name, newname, len + 1);
430 newname[len] = '\0';
431 oidp->oid_name = newname;
432 oidp->oid_handler = handler;
433 oidp->oid_kind = CTLFLAG_DYN | kind;
434 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
6d9b622e
JS
435 struct sysctl_oid_list *children;
436
984263bc 437 /* Allocate space for children */
efda3bd0 438 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
6d9b622e
JS
439 SYSCTL_SET_CHILDREN(oidp, children);
440 SLIST_INIT(children);
984263bc
MD
441 } else {
442 oidp->oid_arg1 = arg1;
443 oidp->oid_arg2 = arg2;
444 }
445 oidp->oid_fmt = fmt;
446 if (descr) {
447 int len = strlen(descr) + 1;
efda3bd0 448 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
978400d3 449 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
984263bc
MD
450 };
451 /* Update the context, if used */
452 if (clist != NULL)
453 sysctl_ctx_entry_add(clist, oidp);
454 /* Register this oid */
853af6eb
JS
455 sysctl_register_oid_int(oidp);
456 sysctl_unlock();
984263bc
MD
457 return (oidp);
458}
459
984263bc
MD
460/*
461 * Register the kernel's oids on startup.
462 */
dc62b251 463SET_DECLARE(sysctl_set, struct sysctl_oid);
984263bc 464
c972a82f
SW
465static void
466sysctl_register_all(void *arg)
984263bc 467{
dc62b251 468 struct sysctl_oid **oidp;
984263bc 469
f2770c70
MD
470 lockinit(&sysctl_lkp, "sysctl", 0, 0);
471 lockinit(&sysctl_ctx_lkp, "sysctl ctx", 0, 0);
dc62b251 472 SET_FOREACH(oidp, sysctl_set)
853af6eb 473 sysctl_register_oid_int(*oidp);
984263bc
MD
474}
475
ba39e2e0 476SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
984263bc
MD
477
478/*
479 * "Staff-functions"
480 *
481 * These functions implement a presently undocumented interface
482 * used by the sysctl program to walk the tree, and get the type
483 * so it can print the value.
484 * This interface is under work and consideration, and should probably
485 * be killed with a big axe by the first person who can find the time.
486 * (be aware though, that the proper interface isn't as obvious as it
487 * may seem, there are various conflicting requirements.
488 *
6ea70f76 489 * {0,0} kprintf the entire MIB-tree.
984263bc
MD
490 * {0,1,...} return the name of the "..." OID.
491 * {0,2,...} return the next OID.
492 * {0,3} return the OID of the name in "new"
493 * {0,4,...} return the kind & format info for the "..." OID.
494 */
495
496static void
497sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
498{
499 int k;
500 struct sysctl_oid *oidp;
501
853af6eb 502 sysctl_lock(LK_SHARED);
984263bc
MD
503 SLIST_FOREACH(oidp, l, oid_link) {
504
505 for (k=0; k<i; k++)
6ea70f76 506 kprintf(" ");
984263bc 507
6ea70f76 508 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
984263bc 509
6ea70f76 510 kprintf("%c%c",
984263bc
MD
511 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
512 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
513
514 if (oidp->oid_handler)
6ea70f76 515 kprintf(" *Handler");
984263bc
MD
516
517 switch (oidp->oid_kind & CTLTYPE) {
518 case CTLTYPE_NODE:
6ea70f76 519 kprintf(" Node\n");
984263bc
MD
520 if (!oidp->oid_handler) {
521 sysctl_sysctl_debug_dump_node(
522 oidp->oid_arg1, i+2);
523 }
524 break;
6ea70f76
SW
525 case CTLTYPE_INT: kprintf(" Int\n"); break;
526 case CTLTYPE_STRING: kprintf(" String\n"); break;
527 case CTLTYPE_QUAD: kprintf(" Quad\n"); break;
528 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
529 default: kprintf("\n");
984263bc
MD
530 }
531
532 }
853af6eb 533 sysctl_unlock();
984263bc
MD
534}
535
536static int
537sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
538{
539 int error;
540
3b1d99e9 541 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
984263bc
MD
542 if (error)
543 return error;
544 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
545 return ENOENT;
546}
547
548SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
549 0, 0, sysctl_sysctl_debug, "-", "");
550
551static int
552sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
553{
554 int *name = (int *) arg1;
555 u_int namelen = arg2;
556 int error = 0;
557 struct sysctl_oid *oid;
558 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
871e6aa8 559 char buf[16];
984263bc 560
853af6eb 561 sysctl_lock(LK_SHARED);
984263bc
MD
562 while (namelen) {
563 if (!lsp) {
871e6aa8 564 ksnprintf(buf, sizeof(buf), "%d", *name);
984263bc
MD
565 if (req->oldidx)
566 error = SYSCTL_OUT(req, ".", 1);
567 if (!error)
568 error = SYSCTL_OUT(req, buf, strlen(buf));
853af6eb
JS
569 if (error) {
570 sysctl_unlock();
984263bc 571 return (error);
853af6eb 572 }
984263bc
MD
573 namelen--;
574 name++;
575 continue;
576 }
4090d6ff 577 lsp2 = NULL;
984263bc
MD
578 SLIST_FOREACH(oid, lsp, oid_link) {
579 if (oid->oid_number != *name)
580 continue;
581
582 if (req->oldidx)
583 error = SYSCTL_OUT(req, ".", 1);
584 if (!error)
585 error = SYSCTL_OUT(req, oid->oid_name,
586 strlen(oid->oid_name));
853af6eb
JS
587 if (error) {
588 sysctl_unlock();
984263bc 589 return (error);
853af6eb 590 }
984263bc
MD
591
592 namelen--;
593 name++;
594
595 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
596 break;
597
598 if (oid->oid_handler)
599 break;
600
601 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
602 break;
603 }
604 lsp = lsp2;
605 }
853af6eb 606 sysctl_unlock();
984263bc
MD
607 return (SYSCTL_OUT(req, "", 1));
608}
609
610SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
611
612static int
613sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
614 int *next, int *len, int level, struct sysctl_oid **oidpp)
615{
616 struct sysctl_oid *oidp;
617
618 *len = level;
853af6eb 619 sysctl_lock(LK_SHARED);
984263bc
MD
620 SLIST_FOREACH(oidp, lsp, oid_link) {
621 *next = oidp->oid_number;
622 *oidpp = oidp;
623
624 if (!namelen) {
853af6eb
JS
625 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
626 sysctl_unlock();
984263bc 627 return 0;
853af6eb
JS
628 }
629 if (oidp->oid_handler) {
984263bc 630 /* We really should call the handler here...*/
853af6eb 631 sysctl_unlock();
984263bc 632 return 0;
853af6eb 633 }
984263bc
MD
634 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
635 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
853af6eb
JS
636 len, level+1, oidpp)) {
637 sysctl_unlock();
984263bc 638 return 0;
853af6eb 639 }
fef0fdf2 640 goto emptynode;
984263bc
MD
641 }
642
643 if (oidp->oid_number < *name)
644 continue;
645
646 if (oidp->oid_number > *name) {
853af6eb
JS
647 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
648 sysctl_unlock();
984263bc 649 return 0;
853af6eb
JS
650 }
651 if (oidp->oid_handler) {
652 sysctl_unlock();
984263bc 653 return 0;
853af6eb 654 }
984263bc
MD
655 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
656 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
853af6eb
JS
657 next+1, len, level+1, oidpp)) {
658 sysctl_unlock();
984263bc 659 return (0);
853af6eb 660 }
984263bc
MD
661 goto next;
662 }
663 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
664 continue;
665
666 if (oidp->oid_handler)
667 continue;
668
669 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
670 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
853af6eb
JS
671 len, level+1, oidpp)) {
672 sysctl_unlock();
984263bc 673 return (0);
853af6eb 674 }
984263bc
MD
675 next:
676 namelen = 1;
677 *len = level;
fef0fdf2
MD
678 emptynode:
679 *len = level;
984263bc 680 }
853af6eb 681 sysctl_unlock();
984263bc
MD
682 return 1;
683}
684
685static int
686sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
687{
688 int *name = (int *) arg1;
689 u_int namelen = arg2;
690 int i, j, error;
691 struct sysctl_oid *oid;
692 struct sysctl_oid_list *lsp = &sysctl__children;
693 int newoid[CTL_MAXNAME];
694
695 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
696 if (i)
697 return ENOENT;
698 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
699 return (error);
700}
701
702SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
703
704static int
705name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
706{
707 int i;
708 struct sysctl_oid *oidp;
709 struct sysctl_oid_list *lsp = &sysctl__children;
710 char *p;
711
712 if (!*name)
713 return ENOENT;
714
715 p = name + strlen(name) - 1 ;
716 if (*p == '.')
717 *p = '\0';
718
719 *len = 0;
720
721 for (p = name; *p && *p != '.'; p++)
722 ;
723 i = *p;
724 if (i == '.')
725 *p = '\0';
726
853af6eb 727 sysctl_lock(LK_SHARED);
984263bc
MD
728 oidp = SLIST_FIRST(lsp);
729
730 while (oidp && *len < CTL_MAXNAME) {
731 if (strcmp(name, oidp->oid_name)) {
732 oidp = SLIST_NEXT(oidp, oid_link);
733 continue;
734 }
735 *oid++ = oidp->oid_number;
736 (*len)++;
737
738 if (!i) {
739 if (oidpp)
740 *oidpp = oidp;
853af6eb 741 sysctl_unlock();
984263bc
MD
742 return (0);
743 }
744
745 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
746 break;
747
748 if (oidp->oid_handler)
749 break;
750
751 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
752 oidp = SLIST_FIRST(lsp);
753 name = p+1;
754 for (p = name; *p && *p != '.'; p++)
755 ;
756 i = *p;
757 if (i == '.')
758 *p = '\0';
759 }
853af6eb 760 sysctl_unlock();
984263bc
MD
761 return ENOENT;
762}
763
764static int
765sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
766{
767 char *p;
768 int error, oid[CTL_MAXNAME], len;
4090d6ff 769 struct sysctl_oid *op = NULL;
984263bc
MD
770
771 if (!req->newlen)
772 return ENOENT;
773 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
774 return (ENAMETOOLONG);
775
efda3bd0 776 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
984263bc
MD
777
778 error = SYSCTL_IN(req, p, req->newlen);
779 if (error) {
efda3bd0 780 kfree(p, M_SYSCTL);
984263bc
MD
781 return (error);
782 }
783
784 p [req->newlen] = '\0';
785
786 error = name2oid(p, oid, &len, &op);
787
efda3bd0 788 kfree(p, M_SYSCTL);
984263bc
MD
789
790 if (error)
791 return (error);
792
793 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
794 return (error);
795}
796
797SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
798 sysctl_sysctl_name2oid, "I", "");
799
800static int
801sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
802{
803 struct sysctl_oid *oid;
804 int error;
805
806 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
807 if (error)
808 return (error);
809
810 if (!oid->oid_fmt)
811 return (ENOENT);
812 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
813 if (error)
814 return (error);
815 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
816 return (error);
817}
818
819
820SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
821
822static int
823sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
824{
825 struct sysctl_oid *oid;
826 int error;
827
828 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
829 if (error)
830 return (error);
831
832 if (!oid->oid_descr)
833 return (ENOENT);
834 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
835 return (error);
836}
837
838SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
839
840/*
841 * Default "handler" functions.
842 */
843
844/*
845 * Handle an int, signed or unsigned.
846 * Two cases:
847 * a variable: point arg1 at it.
848 * a constant: pass it in arg2.
849 */
850
851int
852sysctl_handle_int(SYSCTL_HANDLER_ARGS)
853{
854 int error = 0;
855
856 if (arg1)
857 error = SYSCTL_OUT(req, arg1, sizeof(int));
858 else
859 error = SYSCTL_OUT(req, &arg2, sizeof(int));
860
861 if (error || !req->newptr)
862 return (error);
863
864 if (!arg1)
865 error = EPERM;
866 else
867 error = SYSCTL_IN(req, arg1, sizeof(int));
868 return (error);
869}
870
871/*
872 * Handle a long, signed or unsigned. arg1 points to it.
873 */
874
875int
876sysctl_handle_long(SYSCTL_HANDLER_ARGS)
877{
878 int error = 0;
879
880 if (!arg1)
881 return (EINVAL);
882 error = SYSCTL_OUT(req, arg1, sizeof(long));
883
884 if (error || !req->newptr)
885 return (error);
886
887 error = SYSCTL_IN(req, arg1, sizeof(long));
888 return (error);
889}
890
5f2e2a8b
MD
891/*
892 * Handle a quad, signed or unsigned. arg1 points to it.
893 */
894
895int
896sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
897{
898 int error = 0;
899
900 if (!arg1)
901 return (EINVAL);
902 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
903
904 if (error || !req->newptr)
905 return (error);
906
907 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
908 return (error);
909}
910
984263bc
MD
911/*
912 * Handle our generic '\0' terminated 'C' string.
913 * Two cases:
914 * a variable string: point arg1 at it, arg2 is max length.
915 * a constant string: point arg1 at it, arg2 is zero.
916 */
917
918int
919sysctl_handle_string(SYSCTL_HANDLER_ARGS)
920{
921 int error=0;
922
923 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
924
925 if (error || !req->newptr)
926 return (error);
927
928 if ((req->newlen - req->newidx) >= arg2) {
929 error = EINVAL;
930 } else {
931 arg2 = (req->newlen - req->newidx);
932 error = SYSCTL_IN(req, arg1, arg2);
933 ((char *)arg1)[arg2] = '\0';
934 }
935
936 return (error);
937}
938
939/*
940 * Handle any kind of opaque data.
941 * arg1 points to it, arg2 is the size.
942 */
943
944int
945sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
946{
947 int error;
948
949 error = SYSCTL_OUT(req, arg1, arg2);
950
951 if (error || !req->newptr)
952 return (error);
953
954 error = SYSCTL_IN(req, arg1, arg2);
955
956 return (error);
957}
958
959/*
960 * Transfer functions to/from kernel space.
961 * XXX: rather untested at this point
962 */
963static int
964sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
965{
966 size_t i = 0;
967
968 if (req->oldptr) {
969 i = l;
970 if (i > req->oldlen - req->oldidx)
971 i = req->oldlen - req->oldidx;
972 if (i > 0)
973 bcopy(p, (char *)req->oldptr + req->oldidx, i);
974 }
975 req->oldidx += l;
976 if (req->oldptr && i != l)
977 return (ENOMEM);
978 return (0);
979}
980
981static int
982sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
983{
984
985 if (!req->newptr)
986 return 0;
987 if (req->newlen - req->newidx < l)
988 return (EINVAL);
989 bcopy((char *)req->newptr + req->newidx, p, l);
990 req->newidx += l;
991 return (0);
992}
993
994int
41c20dac 995kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
984263bc
MD
996{
997 int error = 0;
998 struct sysctl_req req;
999
1000 bzero(&req, sizeof req);
1001
dadab5e9 1002 req.td = curthread;
984263bc
MD
1003
1004 if (oldlenp) {
1005 req.oldlen = *oldlenp;
1006 }
1007
1008 if (old) {
41c20dac 1009 req.oldptr = old;
984263bc
MD
1010 }
1011
1012 if (new != NULL) {
1013 req.newlen = newlen;
1014 req.newptr = new;
1015 }
1016
1017 req.oldfunc = sysctl_old_kernel;
1018 req.newfunc = sysctl_new_kernel;
2c8c434f 1019#if 0
984263bc 1020 req.lock = 1;
2c8c434f 1021#endif
984263bc 1022
853af6eb 1023 sysctl_lock(LK_SHARED);
984263bc
MD
1024
1025 error = sysctl_root(0, name, namelen, &req);
1026
2c8c434f 1027#if 0
984263bc
MD
1028 if (req.lock == 2)
1029 vsunlock(req.oldptr, req.oldlen);
2c8c434f 1030#endif
984263bc 1031
853af6eb 1032 sysctl_unlock();
984263bc
MD
1033
1034 if (error && error != ENOMEM)
1035 return (error);
1036
1037 if (retval) {
1038 if (req.oldptr && req.oldidx > req.oldlen)
1039 *retval = req.oldlen;
1040 else
1041 *retval = req.oldidx;
1042 }
1043 return (error);
1044}
1045
1046int
41c20dac 1047kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
984263bc
MD
1048 void *new, size_t newlen, size_t *retval)
1049{
1050 int oid[CTL_MAXNAME];
1051 size_t oidlen, plen;
1052 int error;
1053
1054 oid[0] = 0; /* sysctl internal magic */
1055 oid[1] = 3; /* name2oid */
1056 oidlen = sizeof(oid);
1057
4e1af74f 1058 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
984263bc
MD
1059 if (error)
1060 return (error);
1061
41c20dac 1062 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
984263bc
MD
1063 new, newlen, retval);
1064 return (error);
1065}
1066
1067/*
1068 * Transfer function to/from user space.
1069 */
1070static int
1071sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1072{
1073 int error = 0;
1074 size_t i = 0;
1075
2c8c434f 1076#if 0
984263bc
MD
1077 if (req->lock == 1 && req->oldptr) {
1078 vslock(req->oldptr, req->oldlen);
1079 req->lock = 2;
1080 }
2c8c434f 1081#endif
984263bc
MD
1082 if (req->oldptr) {
1083 i = l;
1084 if (i > req->oldlen - req->oldidx)
1085 i = req->oldlen - req->oldidx;
1086 if (i > 0)
1087 error = copyout(p, (char *)req->oldptr + req->oldidx,
1088 i);
1089 }
1090 req->oldidx += l;
1091 if (error)
1092 return (error);
1093 if (req->oldptr && i < l)
1094 return (ENOMEM);
1095 return (0);
1096}
1097
1098static int
1099sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1100{
1101 int error;
1102
1103 if (!req->newptr)
1104 return 0;
1105 if (req->newlen - req->newidx < l)
1106 return (EINVAL);
1107 error = copyin((char *)req->newptr + req->newidx, p, l);
1108 req->newidx += l;
1109 return (error);
1110}
1111
1112int
1113sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1114 int *nindx, struct sysctl_req *req)
1115{
1116 struct sysctl_oid *oid;
1117 int indx;
1118
853af6eb 1119 sysctl_lock(LK_SHARED);
984263bc
MD
1120 oid = SLIST_FIRST(&sysctl__children);
1121 indx = 0;
1122 while (oid && indx < CTL_MAXNAME) {
1123 if (oid->oid_number == name[indx]) {
1124 indx++;
984263bc
MD
1125 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1126 if (oid->oid_handler != NULL ||
1127 indx == namelen) {
1128 *noid = oid;
1129 if (nindx != NULL)
1130 *nindx = indx;
853af6eb 1131 sysctl_unlock();
984263bc
MD
1132 return (0);
1133 }
1134 oid = SLIST_FIRST(
1135 (struct sysctl_oid_list *)oid->oid_arg1);
1136 } else if (indx == namelen) {
1137 *noid = oid;
1138 if (nindx != NULL)
1139 *nindx = indx;
853af6eb 1140 sysctl_unlock();
984263bc
MD
1141 return (0);
1142 } else {
853af6eb 1143 sysctl_unlock();
984263bc
MD
1144 return (ENOTDIR);
1145 }
1146 } else {
1147 oid = SLIST_NEXT(oid, oid_link);
1148 }
1149 }
853af6eb 1150 sysctl_unlock();
984263bc
MD
1151 return (ENOENT);
1152}
1153
1154/*
1155 * Traverse our tree, and find the right node, execute whatever it points
1156 * to, and return the resulting error code.
1157 */
1158
1159int
1160sysctl_root(SYSCTL_HANDLER_ARGS)
1161{
dadab5e9
MD
1162 struct thread *td = req->td;
1163 struct proc *p = td ? td->td_proc : NULL;
984263bc
MD
1164 struct sysctl_oid *oid;
1165 int error, indx;
1166
1167 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1168 if (error)
1169 return (error);
1170
1171 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1172 /*
1173 * You can't call a sysctl when it's a node, but has
1174 * no handler. Inform the user that it's a node.
1175 * The indx may or may not be the same as namelen.
1176 */
1177 if (oid->oid_handler == NULL)
1178 return (EISDIR);
1179 }
1180
1181 /* If writing isn't allowed */
1182 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1183 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1184 return (EPERM);
1185
1186 /* Most likely only root can write */
26a0694b 1187 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
9910d07b 1188 (error = priv_check_cred(td->td_ucred,
d4a89085 1189 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
3a591c90 1190 PRIV_SYSCTL_WRITE, 0)))
984263bc
MD
1191 return (error);
1192
1193 if (!oid->oid_handler)
1194 return EINVAL;
1195
1196 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1197 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1198 req);
1199 else
1200 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1201 req);
1202 return (error);
1203}
1204
3919ced0
MD
1205/*
1206 * MPALMOSTSAFE
1207 */
984263bc 1208int
753fd850 1209sys___sysctl(struct sysctl_args *uap)
984263bc
MD
1210{
1211 int error, i, name[CTL_MAXNAME];
1212 size_t j;
1213
1214 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1215 return (EINVAL);
1216
8f706258 1217 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
984263bc
MD
1218 if (error)
1219 return (error);
1220
3919ced0 1221 get_mplock();
41c20dac 1222 error = userland_sysctl(name, uap->namelen,
984263bc
MD
1223 uap->old, uap->oldlenp, 0,
1224 uap->new, uap->newlen, &j);
3919ced0 1225 rel_mplock();
984263bc
MD
1226 if (error && error != ENOMEM)
1227 return (error);
1228 if (uap->oldlenp) {
1229 i = copyout(&j, uap->oldlenp, sizeof(j));
1230 if (i)
1231 return (i);
1232 }
1233 return (error);
1234}
1235
1236/*
1237 * This is used from various compatibility syscalls too. That's why name
1238 * must be in kernel space.
1239 */
1240int
41c20dac 1241userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
984263bc
MD
1242{
1243 int error = 0;
1244 struct sysctl_req req, req2;
1245
1246 bzero(&req, sizeof req);
1247
984263bc
MD
1248 if (oldlenp) {
1249 if (inkernel) {
1250 req.oldlen = *oldlenp;
1251 } else {
1252 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1253 if (error)
1254 return (error);
1255 }
1256 }
1257
1258 if (old) {
1259 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1260 return (EFAULT);
1261 req.oldptr= old;
1262 }
1263
1264 if (new != NULL) {
9328e781 1265 if (!useracc(new, newlen, VM_PROT_READ))
984263bc
MD
1266 return (EFAULT);
1267 req.newlen = newlen;
1268 req.newptr = new;
1269 }
1270
1271 req.oldfunc = sysctl_old_user;
1272 req.newfunc = sysctl_new_user;
2c8c434f 1273#if 0
984263bc 1274 req.lock = 1;
2c8c434f 1275#endif
5f2e2a8b 1276 req.td = curthread;
984263bc 1277
853af6eb 1278 sysctl_lock(LK_SHARED);
984263bc
MD
1279
1280 do {
1281 req2 = req;
1282 error = sysctl_root(0, name, namelen, &req2);
1283 } while (error == EAGAIN);
1284
1285 req = req2;
2c8c434f 1286#if 0
984263bc
MD
1287 if (req.lock == 2)
1288 vsunlock(req.oldptr, req.oldlen);
2c8c434f 1289#endif
984263bc 1290
853af6eb 1291 sysctl_unlock();
984263bc
MD
1292
1293 if (error && error != ENOMEM)
1294 return (error);
1295
1296 if (retval) {
1297 if (req.oldptr && req.oldidx > req.oldlen)
1298 *retval = req.oldlen;
1299 else
1300 *retval = req.oldidx;
1301 }
1302 return (error);
1303}
853af6eb
JS
1304
1305static void
1306sysctl_lock(int flag)
1307{
df4f70a6 1308 lockmgr(&sysctl_lkp, flag);
853af6eb
JS
1309}
1310
1311static void
1312sysctl_unlock(void)
1313{
df4f70a6 1314 lockmgr(&sysctl_lkp, LK_RELEASE);
853af6eb
JS
1315}
1316
1317static void
1318sysctl_ctx_lock(int flag)
1319{
df4f70a6 1320 lockmgr(&sysctl_ctx_lkp, flag);
853af6eb
JS
1321}
1322
1323static void
1324sysctl_ctx_unlock(void)
1325{
df4f70a6 1326 lockmgr(&sysctl_ctx_lkp, LK_RELEASE);
853af6eb 1327}
df4f70a6 1328
7efc589a
SZ
1329int
1330sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1331{
1332 int error, value;
1333
1334 value = *(int *)arg1;
1335 error = sysctl_handle_int(oidp, &value, 0, req);
1336 if (error || !req->newptr)
1337 return (error);
1338 if (value < low || value > high)
1339 return (EINVAL);
1340 *(int *)arg1 = value;
1341 return (0);
1342}