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