]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/kern_sysctl.c
MFC r309748 (by glebius):
[FreeBSD/stable/8.git] / sys / kern / kern_sysctl.c
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  * 4. Neither the name of the University nor the names of its contributors
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  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
43
44 #include <sys/param.h>
45 #include <sys/fail.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/jail.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/sbuf.h>
56 #include <sys/sx.h>
57 #include <sys/sysproto.h>
58 #include <sys/uio.h>
59 #ifdef KTRACE
60 #include <sys/ktrace.h>
61 #endif
62
63 #include <net/vnet.h>
64
65 #include <security/mac/mac_framework.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69
70 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
71 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
72 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
73
74 /*
75  * The sysctllock protects the MIB tree.  It also protects sysctl
76  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
77  * sysctl_unregister_oid() routines require the sysctllock to already
78  * be held, so the sysctl_lock() and sysctl_unlock() routines are
79  * provided for the few places in the kernel which need to use that
80  * API rather than using the dynamic API.  Use of the dynamic API is
81  * strongly encouraged for most code.
82  *
83  * The sysctlmemlock is used to limit the amount of user memory wired for
84  * sysctl requests.  This is implemented by serializing any userland
85  * sysctl requests larger than a single page via an exclusive lock.
86  */
87 static struct sx sysctllock;
88 static struct sx sysctlmemlock;
89
90 #define SYSCTL_XLOCK()          sx_xlock(&sysctllock)
91 #define SYSCTL_XUNLOCK()        sx_xunlock(&sysctllock)
92 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
93 #define SYSCTL_INIT()           sx_init(&sysctllock, "sysctl lock")
94 #define SYSCTL_SLEEP(ch, wmesg, timo)                                   \
95                                 sx_sleep(ch, &sysctllock, 0, wmesg, timo)
96
97 static int sysctl_root(SYSCTL_HANDLER_ARGS);
98
99 struct sysctl_oid_list sysctl__children; /* root list */
100
101 static int      sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
102                     int recurse);
103
104 static struct sysctl_oid *
105 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
106 {
107         struct sysctl_oid *oidp;
108
109         SYSCTL_ASSERT_XLOCKED();
110         SLIST_FOREACH(oidp, list, oid_link) {
111                 if (strcmp(oidp->oid_name, name) == 0) {
112                         return (oidp);
113                 }
114         }
115         return (NULL);
116 }
117
118 /*
119  * Initialization of the MIB tree.
120  *
121  * Order by number in each list.
122  */
123 void
124 sysctl_lock(void)
125 {
126
127         SYSCTL_XLOCK();
128 }
129
130 void
131 sysctl_unlock(void)
132 {
133
134         SYSCTL_XUNLOCK();
135 }
136
137 void
138 sysctl_register_oid(struct sysctl_oid *oidp)
139 {
140         struct sysctl_oid_list *parent = oidp->oid_parent;
141         struct sysctl_oid *p;
142         struct sysctl_oid *q;
143
144         /*
145          * First check if another oid with the same name already
146          * exists in the parent's list.
147          */
148         SYSCTL_ASSERT_XLOCKED();
149         p = sysctl_find_oidname(oidp->oid_name, parent);
150         if (p != NULL) {
151                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
152                         p->oid_refcnt++;
153                         return;
154                 } else {
155                         printf("can't re-use a leaf (%s)!\n", p->oid_name);
156                         return;
157                 }
158         }
159         /*
160          * If this oid has a number OID_AUTO, give it a number which
161          * is greater than any current oid.
162          * NOTE: DO NOT change the starting value here, change it in
163          * <sys/sysctl.h>, and make sure it is at least 256 to
164          * accomodate e.g. net.inet.raw as a static sysctl node.
165          */
166         if (oidp->oid_number == OID_AUTO) {
167                 static int newoid = CTL_AUTO_START;
168
169                 oidp->oid_number = newoid++;
170                 if (newoid == 0x7fffffff)
171                         panic("out of oids");
172         }
173 #if 0
174         else if (oidp->oid_number >= CTL_AUTO_START) {
175                 /* do not panic; this happens when unregistering sysctl sets */
176                 printf("static sysctl oid too high: %d", oidp->oid_number);
177         }
178 #endif
179
180         /*
181          * Insert the oid into the parent's list in order.
182          */
183         q = NULL;
184         SLIST_FOREACH(p, parent, oid_link) {
185                 if (oidp->oid_number < p->oid_number)
186                         break;
187                 q = p;
188         }
189         if (q)
190                 SLIST_INSERT_AFTER(q, oidp, oid_link);
191         else
192                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
193 }
194
195 void
196 sysctl_unregister_oid(struct sysctl_oid *oidp)
197 {
198         struct sysctl_oid *p;
199         int error;
200
201         SYSCTL_ASSERT_XLOCKED();
202         error = ENOENT;
203         if (oidp->oid_number == OID_AUTO) {
204                 error = EINVAL;
205         } else {
206                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
207                         if (p == oidp) {
208                                 SLIST_REMOVE(oidp->oid_parent, oidp,
209                                     sysctl_oid, oid_link);
210                                 error = 0;
211                                 break;
212                         }
213                 }
214         }
215
216         /* 
217          * This can happen when a module fails to register and is
218          * being unloaded afterwards.  It should not be a panic()
219          * for normal use.
220          */
221         if (error)
222                 printf("%s: failed to unregister sysctl\n", __func__);
223 }
224
225 /* Initialize a new context to keep track of dynamically added sysctls. */
226 int
227 sysctl_ctx_init(struct sysctl_ctx_list *c)
228 {
229
230         if (c == NULL) {
231                 return (EINVAL);
232         }
233
234         /*
235          * No locking here, the caller is responsible for not adding
236          * new nodes to a context until after this function has
237          * returned.
238          */
239         TAILQ_INIT(c);
240         return (0);
241 }
242
243 /* Free the context, and destroy all dynamic oids registered in this context */
244 int
245 sysctl_ctx_free(struct sysctl_ctx_list *clist)
246 {
247         struct sysctl_ctx_entry *e, *e1;
248         int error;
249
250         error = 0;
251         /*
252          * First perform a "dry run" to check if it's ok to remove oids.
253          * XXX FIXME
254          * XXX This algorithm is a hack. But I don't know any
255          * XXX better solution for now...
256          */
257         SYSCTL_XLOCK();
258         TAILQ_FOREACH(e, clist, link) {
259                 error = sysctl_remove_oid_locked(e->entry, 0, 0);
260                 if (error)
261                         break;
262         }
263         /*
264          * Restore deregistered entries, either from the end,
265          * or from the place where error occured.
266          * e contains the entry that was not unregistered
267          */
268         if (error)
269                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
270         else
271                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
272         while (e1 != NULL) {
273                 sysctl_register_oid(e1->entry);
274                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
275         }
276         if (error) {
277                 SYSCTL_XUNLOCK();
278                 return(EBUSY);
279         }
280         /* Now really delete the entries */
281         e = TAILQ_FIRST(clist);
282         while (e != NULL) {
283                 e1 = TAILQ_NEXT(e, link);
284                 error = sysctl_remove_oid_locked(e->entry, 1, 0);
285                 if (error)
286                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
287                             e->entry->oid_name);
288                 free(e, M_SYSCTLOID);
289                 e = e1;
290         }
291         SYSCTL_XUNLOCK();
292         return (error);
293 }
294
295 /* Add an entry to the context */
296 struct sysctl_ctx_entry *
297 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
298 {
299         struct sysctl_ctx_entry *e;
300
301         SYSCTL_ASSERT_XLOCKED();
302         if (clist == NULL || oidp == NULL)
303                 return(NULL);
304         e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
305         e->entry = oidp;
306         TAILQ_INSERT_HEAD(clist, e, link);
307         return (e);
308 }
309
310 /* Find an entry in the context */
311 struct sysctl_ctx_entry *
312 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
313 {
314         struct sysctl_ctx_entry *e;
315
316         SYSCTL_ASSERT_XLOCKED();
317         if (clist == NULL || oidp == NULL)
318                 return(NULL);
319         TAILQ_FOREACH(e, clist, link) {
320                 if(e->entry == oidp)
321                         return(e);
322         }
323         return (e);
324 }
325
326 /*
327  * Delete an entry from the context.
328  * NOTE: this function doesn't free oidp! You have to remove it
329  * with sysctl_remove_oid().
330  */
331 int
332 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
333 {
334         struct sysctl_ctx_entry *e;
335
336         if (clist == NULL || oidp == NULL)
337                 return (EINVAL);
338         SYSCTL_XLOCK();
339         e = sysctl_ctx_entry_find(clist, oidp);
340         if (e != NULL) {
341                 TAILQ_REMOVE(clist, e, link);
342                 SYSCTL_XUNLOCK();
343                 free(e, M_SYSCTLOID);
344                 return (0);
345         } else {
346                 SYSCTL_XUNLOCK();
347                 return (ENOENT);
348         }
349 }
350
351 /*
352  * Remove dynamically created sysctl trees.
353  * oidp - top of the tree to be removed
354  * del - if 0 - just deregister, otherwise free up entries as well
355  * recurse - if != 0 traverse the subtree to be deleted
356  */
357 int
358 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
359 {
360         int error;
361
362         SYSCTL_XLOCK();
363         error = sysctl_remove_oid_locked(oidp, del, recurse);
364         SYSCTL_XUNLOCK();
365         return (error);
366 }
367
368 static int
369 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
370 {
371         struct sysctl_oid *p;
372         int error;
373
374         SYSCTL_ASSERT_XLOCKED();
375         if (oidp == NULL)
376                 return(EINVAL);
377         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
378                 printf("can't remove non-dynamic nodes!\n");
379                 return (EINVAL);
380         }
381         /*
382          * WARNING: normal method to do this should be through
383          * sysctl_ctx_free(). Use recursing as the last resort
384          * method to purge your sysctl tree of leftovers...
385          * However, if some other code still references these nodes,
386          * it will panic.
387          */
388         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
389                 if (oidp->oid_refcnt == 1) {
390                         SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
391                                 if (!recurse)
392                                         return (ENOTEMPTY);
393                                 error = sysctl_remove_oid_locked(p, del,
394                                     recurse);
395                                 if (error)
396                                         return (error);
397                         }
398                         if (del)
399                                 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
400                 }
401         }
402         if (oidp->oid_refcnt > 1 ) {
403                 oidp->oid_refcnt--;
404         } else {
405                 if (oidp->oid_refcnt == 0) {
406                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
407                                 oidp->oid_refcnt, oidp->oid_name);
408                         return (EINVAL);
409                 }
410                 sysctl_unregister_oid(oidp);
411                 if (del) {
412                         /*
413                          * Wait for all threads running the handler to drain.
414                          * This preserves the previous behavior when the
415                          * sysctl lock was held across a handler invocation,
416                          * and is necessary for module unload correctness.
417                          */
418                         while (oidp->oid_running > 0) {
419                                 oidp->oid_kind |= CTLFLAG_DYING;
420                                 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
421                         }
422                         if (oidp->oid_descr)
423                                 free(__DECONST(char *, oidp->oid_descr),
424                                     M_SYSCTLOID);
425                         free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
426                         free(oidp, M_SYSCTLOID);
427                 }
428         }
429         return (0);
430 }
431
432 /*
433  * Create new sysctls at run time.
434  * clist may point to a valid context initialized with sysctl_ctx_init().
435  */
436 struct sysctl_oid *
437 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
438         int number, const char *name, int kind, void *arg1, int arg2,
439         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
440 {
441         struct sysctl_oid *oidp;
442
443         /* You have to hook up somewhere.. */
444         if (parent == NULL)
445                 return(NULL);
446         /* Check if the node already exists, otherwise create it */
447         SYSCTL_XLOCK();
448         oidp = sysctl_find_oidname(name, parent);
449         if (oidp != NULL) {
450                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
451                         oidp->oid_refcnt++;
452                         /* Update the context */
453                         if (clist != NULL)
454                                 sysctl_ctx_entry_add(clist, oidp);
455                         SYSCTL_XUNLOCK();
456                         return (oidp);
457                 } else {
458                         SYSCTL_XUNLOCK();
459                         printf("can't re-use a leaf (%s)!\n", name);
460                         return (NULL);
461                 }
462         }
463         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
464         oidp->oid_parent = parent;
465         SLIST_NEXT(oidp, oid_link) = NULL;
466         oidp->oid_number = number;
467         oidp->oid_refcnt = 1;
468         oidp->oid_name = strdup(name, M_SYSCTLOID);
469         oidp->oid_handler = handler;
470         oidp->oid_kind = CTLFLAG_DYN | kind;
471         if ((kind & CTLTYPE) == CTLTYPE_NODE) {
472                 /* Allocate space for children */
473                 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
474                     M_SYSCTLOID, M_WAITOK));
475                 SLIST_INIT(SYSCTL_CHILDREN(oidp));
476         } else {
477                 oidp->oid_arg1 = arg1;
478                 oidp->oid_arg2 = arg2;
479         }
480         oidp->oid_fmt = fmt;
481         if (descr)
482                 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
483         /* Update the context, if used */
484         if (clist != NULL)
485                 sysctl_ctx_entry_add(clist, oidp);
486         /* Register this oid */
487         sysctl_register_oid(oidp);
488         SYSCTL_XUNLOCK();
489         return (oidp);
490 }
491
492 /*
493  * Rename an existing oid.
494  */
495 void
496 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
497 {
498         char *newname;
499         char *oldname;
500
501         newname = strdup(name, M_SYSCTLOID);
502         SYSCTL_XLOCK();
503         oldname = __DECONST(char *, oidp->oid_name);
504         oidp->oid_name = newname;
505         SYSCTL_XUNLOCK();
506         free(oldname, M_SYSCTLOID);
507 }
508
509 /*
510  * Reparent an existing oid.
511  */
512 int
513 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
514 {
515         struct sysctl_oid *oidp;
516
517         SYSCTL_XLOCK();
518         if (oid->oid_parent == parent) {
519                 SYSCTL_XUNLOCK();
520                 return (0);
521         }
522         oidp = sysctl_find_oidname(oid->oid_name, parent);
523         if (oidp != NULL) {
524                 SYSCTL_XUNLOCK();
525                 return (EEXIST);
526         }
527         sysctl_unregister_oid(oid);
528         oid->oid_parent = parent;
529         oid->oid_number = OID_AUTO;
530         sysctl_register_oid(oid);
531         SYSCTL_XUNLOCK();
532         return (0);
533 }
534
535 /*
536  * Register the kernel's oids on startup.
537  */
538 SET_DECLARE(sysctl_set, struct sysctl_oid);
539
540 static void
541 sysctl_register_all(void *arg)
542 {
543         struct sysctl_oid **oidp;
544
545         sx_init(&sysctlmemlock, "sysctl mem");
546         SYSCTL_INIT();
547         SYSCTL_XLOCK();
548         SET_FOREACH(oidp, sysctl_set)
549                 sysctl_register_oid(*oidp);
550         SYSCTL_XUNLOCK();
551 }
552 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
553
554 /*
555  * "Staff-functions"
556  *
557  * These functions implement a presently undocumented interface 
558  * used by the sysctl program to walk the tree, and get the type
559  * so it can print the value.
560  * This interface is under work and consideration, and should probably
561  * be killed with a big axe by the first person who can find the time.
562  * (be aware though, that the proper interface isn't as obvious as it
563  * may seem, there are various conflicting requirements.
564  *
565  * {0,0}        printf the entire MIB-tree.
566  * {0,1,...}    return the name of the "..." OID.
567  * {0,2,...}    return the next OID.
568  * {0,3}        return the OID of the name in "new"
569  * {0,4,...}    return the kind & format info for the "..." OID.
570  * {0,5,...}    return the description the "..." OID.
571  */
572
573 #ifdef SYSCTL_DEBUG
574 static void
575 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
576 {
577         int k;
578         struct sysctl_oid *oidp;
579
580         SYSCTL_ASSERT_XLOCKED();
581         SLIST_FOREACH(oidp, l, oid_link) {
582
583                 for (k=0; k<i; k++)
584                         printf(" ");
585
586                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
587
588                 printf("%c%c",
589                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
590                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
591
592                 if (oidp->oid_handler)
593                         printf(" *Handler");
594
595                 switch (oidp->oid_kind & CTLTYPE) {
596                         case CTLTYPE_NODE:
597                                 printf(" Node\n");
598                                 if (!oidp->oid_handler) {
599                                         sysctl_sysctl_debug_dump_node(
600                                                 oidp->oid_arg1, i+2);
601                                 }
602                                 break;
603                         case CTLTYPE_INT:    printf(" Int\n"); break;
604                         case CTLTYPE_STRING: printf(" String\n"); break;
605                         case CTLTYPE_QUAD:   printf(" Quad\n"); break;
606                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
607                         default:             printf("\n");
608                 }
609
610         }
611 }
612
613 static int
614 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
615 {
616         int error;
617
618         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
619         if (error)
620                 return (error);
621         SYSCTL_XLOCK();
622         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
623         SYSCTL_XUNLOCK();
624         return (ENOENT);
625 }
626
627 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
628         0, 0, sysctl_sysctl_debug, "-", "");
629 #endif
630
631 static int
632 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
633 {
634         int *name = (int *) arg1;
635         u_int namelen = arg2;
636         int error = 0;
637         struct sysctl_oid *oid;
638         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
639         char buf[10];
640
641         SYSCTL_XLOCK();
642         while (namelen) {
643                 if (!lsp) {
644                         snprintf(buf,sizeof(buf),"%d",*name);
645                         if (req->oldidx)
646                                 error = SYSCTL_OUT(req, ".", 1);
647                         if (!error)
648                                 error = SYSCTL_OUT(req, buf, strlen(buf));
649                         if (error)
650                                 goto out;
651                         namelen--;
652                         name++;
653                         continue;
654                 }
655                 lsp2 = 0;
656                 SLIST_FOREACH(oid, lsp, oid_link) {
657                         if (oid->oid_number != *name)
658                                 continue;
659
660                         if (req->oldidx)
661                                 error = SYSCTL_OUT(req, ".", 1);
662                         if (!error)
663                                 error = SYSCTL_OUT(req, oid->oid_name,
664                                         strlen(oid->oid_name));
665                         if (error)
666                                 goto out;
667
668                         namelen--;
669                         name++;
670
671                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
672                                 break;
673
674                         if (oid->oid_handler)
675                                 break;
676
677                         lsp2 = SYSCTL_CHILDREN(oid);
678                         break;
679                 }
680                 lsp = lsp2;
681         }
682         error = SYSCTL_OUT(req, "", 1);
683  out:
684         SYSCTL_XUNLOCK();
685         return (error);
686 }
687
688 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
689
690 static int
691 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
692         int *next, int *len, int level, struct sysctl_oid **oidpp)
693 {
694         struct sysctl_oid *oidp;
695
696         SYSCTL_ASSERT_XLOCKED();
697         *len = level;
698         SLIST_FOREACH(oidp, lsp, oid_link) {
699                 *next = oidp->oid_number;
700                 *oidpp = oidp;
701
702                 if (oidp->oid_kind & CTLFLAG_SKIP)
703                         continue;
704
705                 if (!namelen) {
706                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
707                                 return (0);
708                         if (oidp->oid_handler) 
709                                 /* We really should call the handler here...*/
710                                 return (0);
711                         lsp = SYSCTL_CHILDREN(oidp);
712                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
713                                 len, level+1, oidpp))
714                                 return (0);
715                         goto emptynode;
716                 }
717
718                 if (oidp->oid_number < *name)
719                         continue;
720
721                 if (oidp->oid_number > *name) {
722                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
723                                 return (0);
724                         if (oidp->oid_handler)
725                                 return (0);
726                         lsp = SYSCTL_CHILDREN(oidp);
727                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
728                                 next+1, len, level+1, oidpp))
729                                 return (0);
730                         goto next;
731                 }
732                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
733                         continue;
734
735                 if (oidp->oid_handler)
736                         continue;
737
738                 lsp = SYSCTL_CHILDREN(oidp);
739                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
740                         len, level+1, oidpp))
741                         return (0);
742         next:
743                 namelen = 1;
744         emptynode:
745                 *len = level;
746         }
747         return (1);
748 }
749
750 static int
751 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
752 {
753         int *name = (int *) arg1;
754         u_int namelen = arg2;
755         int i, j, error;
756         struct sysctl_oid *oid;
757         struct sysctl_oid_list *lsp = &sysctl__children;
758         int newoid[CTL_MAXNAME];
759
760         SYSCTL_XLOCK();
761         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
762         SYSCTL_XUNLOCK();
763         if (i)
764                 return (ENOENT);
765         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
766         return (error);
767 }
768
769 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
770
771 static int
772 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
773 {
774         struct sysctl_oid *oidp;
775         struct sysctl_oid_list *lsp = &sysctl__children;
776         char *p;
777
778         SYSCTL_ASSERT_XLOCKED();
779
780         for (*len = 0; *len < CTL_MAXNAME;) {
781                 p = strsep(&name, ".");
782
783                 oidp = SLIST_FIRST(lsp);
784                 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
785                         if (oidp == NULL)
786                                 return (ENOENT);
787                         if (strcmp(p, oidp->oid_name) == 0)
788                                 break;
789                 }
790                 *oid++ = oidp->oid_number;
791                 (*len)++;
792
793                 if (name == NULL || *name == '\0') {
794                         if (oidpp)
795                                 *oidpp = oidp;
796                         return (0);
797                 }
798
799                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
800                         break;
801
802                 if (oidp->oid_handler)
803                         break;
804
805                 lsp = SYSCTL_CHILDREN(oidp);
806         }
807         return (ENOENT);
808 }
809
810 static int
811 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
812 {
813         char *p;
814         int error, oid[CTL_MAXNAME], len;
815         struct sysctl_oid *op = 0;
816
817         if (!req->newlen) 
818                 return (ENOENT);
819         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
820                 return (ENAMETOOLONG);
821
822         p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
823
824         error = SYSCTL_IN(req, p, req->newlen);
825         if (error) {
826                 free(p, M_SYSCTL);
827                 return (error);
828         }
829
830         p [req->newlen] = '\0';
831         len = 0;
832         SYSCTL_XLOCK();
833         error = name2oid(p, oid, &len, &op);
834         SYSCTL_XUNLOCK();
835
836         free(p, M_SYSCTL);
837
838         if (error)
839                 return (error);
840
841         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
842         return (error);
843 }
844
845 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
846     0, 0, sysctl_sysctl_name2oid, "I", "");
847
848 static int
849 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
850 {
851         struct sysctl_oid *oid;
852         int error;
853
854         SYSCTL_XLOCK();
855         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
856         if (error)
857                 goto out;
858
859         if (oid->oid_fmt == NULL) {
860                 error = ENOENT;
861                 goto out;
862         }
863         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
864         if (error)
865                 goto out;
866         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
867  out:
868         SYSCTL_XUNLOCK();
869         return (error);
870 }
871
872
873 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
874     sysctl_sysctl_oidfmt, "");
875
876 static int
877 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
878 {
879         struct sysctl_oid *oid;
880         int error;
881
882         SYSCTL_XLOCK();
883         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
884         if (error)
885                 goto out;
886
887         if (oid->oid_descr == NULL) {
888                 error = ENOENT;
889                 goto out;
890         }
891         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
892  out:
893         SYSCTL_XUNLOCK();
894         return (error);
895 }
896
897 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
898
899 /*
900  * Default "handler" functions.
901  */
902
903 /*
904  * Handle an int, signed or unsigned.
905  * Two cases:
906  *     a variable:  point arg1 at it.
907  *     a constant:  pass it in arg2.
908  */
909
910 int
911 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
912 {
913         int tmpout, error = 0;
914
915         /*
916          * Attempt to get a coherent snapshot by making a copy of the data.
917          */
918         if (arg1)
919                 tmpout = *(int *)arg1;
920         else
921                 tmpout = arg2;
922         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
923
924         if (error || !req->newptr)
925                 return (error);
926
927         if (!arg1)
928                 error = EPERM;
929         else
930                 error = SYSCTL_IN(req, arg1, sizeof(int));
931         return (error);
932 }
933
934 /*
935  * Based on on sysctl_handle_int() convert milliseconds into ticks.
936  * Note: this is used by TCP.
937  */
938
939 int
940 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
941 {
942         int error, s, tt;
943
944         tt = *(int *)arg1;
945         s = (int)((int64_t)tt * 1000 / hz);
946
947         error = sysctl_handle_int(oidp, &s, 0, req);
948         if (error || !req->newptr)
949                 return (error);
950
951         tt = (int)((int64_t)s * hz / 1000);
952         if (tt < 1)
953                 return (EINVAL);
954
955         *(int *)arg1 = tt;
956         return (0);
957 }
958
959
960 /*
961  * Handle a long, signed or unsigned.
962  * Two cases:
963  *     a variable:  point arg1 at it.
964  *     a constant:  pass it in arg2.
965  */
966
967 int
968 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
969 {
970         int error = 0;
971         long tmplong;
972 #ifdef SCTL_MASK32
973         int tmpint;
974 #endif
975
976         /*
977          * Attempt to get a coherent snapshot by making a copy of the data.
978          */
979         if (arg1)
980                 tmplong = *(long *)arg1;
981         else
982                 tmplong = arg2;
983 #ifdef SCTL_MASK32
984         if (req->flags & SCTL_MASK32) {
985                 tmpint = tmplong;
986                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
987         } else
988 #endif
989                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
990
991         if (error || !req->newptr)
992                 return (error);
993
994         if (!arg1)
995                 error = EPERM;
996 #ifdef SCTL_MASK32
997         else if (req->flags & SCTL_MASK32) {
998                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
999                 *(long *)arg1 = (long)tmpint;
1000         }
1001 #endif
1002         else
1003                 error = SYSCTL_IN(req, arg1, sizeof(long));
1004         return (error);
1005 }
1006
1007 /*
1008  * Handle a 64 bit int, signed or unsigned.
1009  * Two cases:
1010  *     a variable:  point arg1 at it.
1011  *     a constant:  pass it in arg2.
1012  */
1013
1014 int
1015 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1016 {
1017         int error = 0;
1018         uint64_t tmpout;
1019
1020         /*
1021          * Attempt to get a coherent snapshot by making a copy of the data.
1022          */
1023         if (arg1)
1024                 tmpout = *(uint64_t *)arg1;
1025         else
1026                 tmpout = arg2;
1027         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1028
1029         if (error || !req->newptr)
1030                 return (error);
1031
1032         if (!arg1)
1033                 error = EPERM;
1034         else
1035                 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1036         return (error);
1037 }
1038
1039 /*
1040  * Handle our generic '\0' terminated 'C' string.
1041  * Two cases:
1042  *      a variable string:  point arg1 at it, arg2 is max length.
1043  *      a constant string:  point arg1 at it, arg2 is zero.
1044  */
1045
1046 int
1047 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1048 {
1049         int error=0;
1050         char *tmparg;
1051         size_t outlen;
1052
1053         /*
1054          * Attempt to get a coherent snapshot by copying to a
1055          * temporary kernel buffer.
1056          */
1057 retry:
1058         outlen = strlen((char *)arg1)+1;
1059         tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1060
1061         if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1062                 free(tmparg, M_SYSCTLTMP);
1063                 goto retry;
1064         }
1065
1066         error = SYSCTL_OUT(req, tmparg, outlen);
1067         free(tmparg, M_SYSCTLTMP);
1068
1069         if (error || !req->newptr)
1070                 return (error);
1071
1072         if ((req->newlen - req->newidx) >= arg2) {
1073                 error = EINVAL;
1074         } else {
1075                 arg2 = (req->newlen - req->newidx);
1076                 error = SYSCTL_IN(req, arg1, arg2);
1077                 ((char *)arg1)[arg2] = '\0';
1078         }
1079
1080         return (error);
1081 }
1082
1083 /*
1084  * Handle any kind of opaque data.
1085  * arg1 points to it, arg2 is the size.
1086  */
1087
1088 int
1089 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1090 {
1091         int error, tries;
1092         u_int generation;
1093         struct sysctl_req req2;
1094
1095         /*
1096          * Attempt to get a coherent snapshot, by using the thread
1097          * pre-emption counter updated from within mi_switch() to
1098          * determine if we were pre-empted during a bcopy() or
1099          * copyout(). Make 3 attempts at doing this before giving up.
1100          * If we encounter an error, stop immediately.
1101          */
1102         tries = 0;
1103         req2 = *req;
1104 retry:
1105         generation = curthread->td_generation;
1106         error = SYSCTL_OUT(req, arg1, arg2);
1107         if (error)
1108                 return (error);
1109         tries++;
1110         if (generation != curthread->td_generation && tries < 3) {
1111                 *req = req2;
1112                 goto retry;
1113         }
1114
1115         error = SYSCTL_IN(req, arg1, arg2);
1116
1117         return (error);
1118 }
1119
1120 /*
1121  * Transfer functions to/from kernel space.
1122  * XXX: rather untested at this point
1123  */
1124 static int
1125 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1126 {
1127         size_t i = 0;
1128
1129         if (req->oldptr) {
1130                 i = l;
1131                 if (req->oldlen <= req->oldidx)
1132                         i = 0;
1133                 else
1134                         if (i > req->oldlen - req->oldidx)
1135                                 i = req->oldlen - req->oldidx;
1136                 if (i > 0)
1137                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
1138         }
1139         req->oldidx += l;
1140         if (req->oldptr && i != l)
1141                 return (ENOMEM);
1142         return (0);
1143 }
1144
1145 static int
1146 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1147 {
1148         if (!req->newptr)
1149                 return (0);
1150         if (req->newlen - req->newidx < l)
1151                 return (EINVAL);
1152         bcopy((char *)req->newptr + req->newidx, p, l);
1153         req->newidx += l;
1154         return (0);
1155 }
1156
1157 int
1158 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1159     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1160 {
1161         int error = 0;
1162         struct sysctl_req req;
1163
1164         bzero(&req, sizeof req);
1165
1166         req.td = td;
1167         req.flags = flags;
1168
1169         if (oldlenp) {
1170                 req.oldlen = *oldlenp;
1171         }
1172         req.validlen = req.oldlen;
1173
1174         if (old) {
1175                 req.oldptr= old;
1176         }
1177
1178         if (new != NULL) {
1179                 req.newlen = newlen;
1180                 req.newptr = new;
1181         }
1182
1183         req.oldfunc = sysctl_old_kernel;
1184         req.newfunc = sysctl_new_kernel;
1185         req.lock = REQ_LOCKED;
1186
1187         SYSCTL_XLOCK();
1188         error = sysctl_root(0, name, namelen, &req);
1189         SYSCTL_XUNLOCK();
1190
1191         if (req.lock == REQ_WIRED && req.validlen > 0)
1192                 vsunlock(req.oldptr, req.validlen);
1193
1194         if (error && error != ENOMEM)
1195                 return (error);
1196
1197         if (retval) {
1198                 if (req.oldptr && req.oldidx > req.validlen)
1199                         *retval = req.validlen;
1200                 else
1201                         *retval = req.oldidx;
1202         }
1203         return (error);
1204 }
1205
1206 int
1207 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1208     void *new, size_t newlen, size_t *retval, int flags)
1209 {
1210         int oid[CTL_MAXNAME];
1211         size_t oidlen, plen;
1212         int error;
1213
1214         oid[0] = 0;             /* sysctl internal magic */
1215         oid[1] = 3;             /* name2oid */
1216         oidlen = sizeof(oid);
1217
1218         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1219             (void *)name, strlen(name), &plen, flags);
1220         if (error)
1221                 return (error);
1222
1223         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1224             new, newlen, retval, flags);
1225         return (error);
1226 }
1227
1228 /*
1229  * Transfer function to/from user space.
1230  */
1231 static int
1232 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1233 {
1234         size_t i, len, origidx;
1235         int error;
1236
1237         origidx = req->oldidx;
1238         req->oldidx += l;
1239         if (req->oldptr == NULL)
1240                 return (0);
1241         /*
1242          * If we have not wired the user supplied buffer and we are currently
1243          * holding locks, drop a witness warning, as it's possible that
1244          * write operations to the user page can sleep.
1245          */
1246         if (req->lock != REQ_WIRED)
1247                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1248                     "sysctl_old_user()");
1249         i = l;
1250         len = req->validlen;
1251         if (len <= origidx)
1252                 i = 0;
1253         else {
1254                 if (i > len - origidx)
1255                         i = len - origidx;
1256                 if (req->lock == REQ_WIRED) {
1257                         error = copyout_nofault(p, (char *)req->oldptr +
1258                             origidx, i);
1259                 } else
1260                         error = copyout(p, (char *)req->oldptr + origidx, i);
1261                 if (error != 0)
1262                         return (error);
1263         }
1264         if (i < l)
1265                 return (ENOMEM);
1266         return (0);
1267 }
1268
1269 static int
1270 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1271 {
1272         int error;
1273
1274         if (!req->newptr)
1275                 return (0);
1276         if (req->newlen - req->newidx < l)
1277                 return (EINVAL);
1278         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1279             "sysctl_new_user()");
1280         error = copyin((char *)req->newptr + req->newidx, p, l);
1281         req->newidx += l;
1282         return (error);
1283 }
1284
1285 /*
1286  * Wire the user space destination buffer.  If set to a value greater than
1287  * zero, the len parameter limits the maximum amount of wired memory.
1288  */
1289 int
1290 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1291 {
1292         int ret;
1293         size_t wiredlen;
1294
1295         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1296         ret = 0;
1297         if (req->lock == REQ_LOCKED && req->oldptr &&
1298             req->oldfunc == sysctl_old_user) {
1299                 if (wiredlen != 0) {
1300                         ret = vslock(req->oldptr, wiredlen);
1301                         if (ret != 0) {
1302                                 if (ret != ENOMEM)
1303                                         return (ret);
1304                                 wiredlen = 0;
1305                         }
1306                 }
1307                 req->lock = REQ_WIRED;
1308                 req->validlen = wiredlen;
1309         }
1310         return (0);
1311 }
1312
1313 int
1314 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1315     int *nindx, struct sysctl_req *req)
1316 {
1317         struct sysctl_oid_list *lsp;
1318         struct sysctl_oid *oid;
1319         int indx;
1320
1321         SYSCTL_ASSERT_XLOCKED();
1322         lsp = &sysctl__children;
1323         indx = 0;
1324         while (indx < CTL_MAXNAME) {
1325                 SLIST_FOREACH(oid, lsp, oid_link) {
1326                         if (oid->oid_number == name[indx])
1327                                 break;
1328                 }
1329                 if (oid == NULL)
1330                         return (ENOENT);
1331
1332                 indx++;
1333                 if (oid->oid_kind & CTLFLAG_NOLOCK)
1334                         req->lock = REQ_UNLOCKED;
1335                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1336                         if (oid->oid_handler != NULL || indx == namelen) {
1337                                 *noid = oid;
1338                                 if (nindx != NULL)
1339                                         *nindx = indx;
1340                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1341                                     ("%s found DYING node %p", __func__, oid));
1342                                 return (0);
1343                         }
1344                         lsp = SYSCTL_CHILDREN(oid);
1345                 } else if (indx == namelen) {
1346                         *noid = oid;
1347                         if (nindx != NULL)
1348                                 *nindx = indx;
1349                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1350                             ("%s found DYING node %p", __func__, oid));
1351                         return (0);
1352                 } else {
1353                         return (ENOTDIR);
1354                 }
1355         }
1356         return (ENOENT);
1357 }
1358
1359 /*
1360  * Traverse our tree, and find the right node, execute whatever it points
1361  * to, and return the resulting error code.
1362  */
1363
1364 static int
1365 sysctl_root(SYSCTL_HANDLER_ARGS)
1366 {
1367         struct sysctl_oid *oid;
1368         int error, indx, lvl;
1369
1370         SYSCTL_ASSERT_XLOCKED();
1371
1372         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1373         if (error)
1374                 return (error);
1375
1376         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1377                 /*
1378                  * You can't call a sysctl when it's a node, but has
1379                  * no handler.  Inform the user that it's a node.
1380                  * The indx may or may not be the same as namelen.
1381                  */
1382                 if (oid->oid_handler == NULL)
1383                         return (EISDIR);
1384         }
1385
1386         /* Is this sysctl writable? */
1387         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1388                 return (EPERM);
1389
1390         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1391
1392         /* Is this sysctl sensitive to securelevels? */
1393         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1394                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1395                 error = securelevel_gt(req->td->td_ucred, lvl);
1396                 if (error)
1397                         return (error);
1398         }
1399
1400         /* Is this sysctl writable by only privileged users? */
1401         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1402                 int priv;
1403
1404                 if (oid->oid_kind & CTLFLAG_PRISON)
1405                         priv = PRIV_SYSCTL_WRITEJAIL;
1406 #ifdef VIMAGE
1407                 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1408                      prison_owns_vnet(req->td->td_ucred))
1409                         priv = PRIV_SYSCTL_WRITEJAIL;
1410 #endif
1411                 else
1412                         priv = PRIV_SYSCTL_WRITE;
1413                 error = priv_check(req->td, priv);
1414                 if (error)
1415                         return (error);
1416         }
1417
1418         if (!oid->oid_handler)
1419                 return (EINVAL);
1420
1421         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1422                 arg1 = (int *)arg1 + indx;
1423                 arg2 -= indx;
1424         } else {
1425                 arg1 = oid->oid_arg1;
1426                 arg2 = oid->oid_arg2;
1427         }
1428 #ifdef MAC
1429         error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1430             req);
1431         if (error != 0)
1432                 return (error);
1433 #endif
1434         oid->oid_running++;
1435         SYSCTL_XUNLOCK();
1436
1437         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1438                 mtx_lock(&Giant);
1439         error = oid->oid_handler(oid, arg1, arg2, req);
1440         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1441                 mtx_unlock(&Giant);
1442
1443         KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1444
1445         SYSCTL_XLOCK();
1446         oid->oid_running--;
1447         if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1448                 wakeup(&oid->oid_running);
1449         return (error);
1450 }
1451
1452 #ifndef _SYS_SYSPROTO_H_
1453 struct sysctl_args {
1454         int     *name;
1455         u_int   namelen;
1456         void    *old;
1457         size_t  *oldlenp;
1458         void    *new;
1459         size_t  newlen;
1460 };
1461 #endif
1462 int
1463 __sysctl(struct thread *td, struct sysctl_args *uap)
1464 {
1465         int error, i, name[CTL_MAXNAME];
1466         size_t j;
1467
1468         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1469                 return (EINVAL);
1470
1471         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1472         if (error)
1473                 return (error);
1474
1475         error = userland_sysctl(td, name, uap->namelen,
1476                 uap->old, uap->oldlenp, 0,
1477                 uap->new, uap->newlen, &j, 0);
1478         if (error && error != ENOMEM)
1479                 return (error);
1480         if (uap->oldlenp) {
1481                 i = copyout(&j, uap->oldlenp, sizeof(j));
1482                 if (i)
1483                         return (i);
1484         }
1485         return (error);
1486 }
1487
1488 /*
1489  * This is used from various compatibility syscalls too.  That's why name
1490  * must be in kernel space.
1491  */
1492 int
1493 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1494     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1495     int flags)
1496 {
1497         int error = 0, memlocked;
1498         struct sysctl_req req;
1499
1500         bzero(&req, sizeof req);
1501
1502         req.td = td;
1503         req.flags = flags;
1504
1505         if (oldlenp) {
1506                 if (inkernel) {
1507                         req.oldlen = *oldlenp;
1508                 } else {
1509                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1510                         if (error)
1511                                 return (error);
1512                 }
1513         }
1514         req.validlen = req.oldlen;
1515
1516         if (old) {
1517                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1518                         return (EFAULT);
1519                 req.oldptr= old;
1520         }
1521
1522         if (new != NULL) {
1523                 if (!useracc(new, newlen, VM_PROT_READ))
1524                         return (EFAULT);
1525                 req.newlen = newlen;
1526                 req.newptr = new;
1527         }
1528
1529         req.oldfunc = sysctl_old_user;
1530         req.newfunc = sysctl_new_user;
1531         req.lock = REQ_LOCKED;
1532
1533 #ifdef KTRACE
1534         if (KTRPOINT(curthread, KTR_SYSCTL))
1535                 ktrsysctl(name, namelen);
1536 #endif
1537
1538         if (req.oldlen > PAGE_SIZE) {
1539                 memlocked = 1;
1540                 sx_xlock(&sysctlmemlock);
1541         } else
1542                 memlocked = 0;
1543         CURVNET_SET(TD_TO_VNET(td));
1544
1545         for (;;) {
1546                 req.oldidx = 0;
1547                 req.newidx = 0;
1548                 SYSCTL_XLOCK();
1549                 error = sysctl_root(0, name, namelen, &req);
1550                 SYSCTL_XUNLOCK();
1551                 if (error != EAGAIN)
1552                         break;
1553                 uio_yield();
1554         }
1555
1556         CURVNET_RESTORE();
1557
1558         if (req.lock == REQ_WIRED && req.validlen > 0)
1559                 vsunlock(req.oldptr, req.validlen);
1560         if (memlocked)
1561                 sx_xunlock(&sysctlmemlock);
1562
1563         if (error && error != ENOMEM)
1564                 return (error);
1565
1566         if (retval) {
1567                 if (req.oldptr && req.oldidx > req.validlen)
1568                         *retval = req.validlen;
1569                 else
1570                         *retval = req.oldidx;
1571         }
1572         return (error);
1573 }
1574
1575 /*
1576  * Drain into a sysctl struct.  The user buffer should be wired if a page
1577  * fault would cause issue.
1578  */
1579 static int
1580 sbuf_sysctl_drain(void *arg, const char *data, int len)
1581 {
1582         struct sysctl_req *req = arg;
1583         int error;
1584
1585         error = SYSCTL_OUT(req, data, len);
1586         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1587         return (error == 0 ? len : -error);
1588 }
1589
1590 struct sbuf *
1591 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1592     struct sysctl_req *req)
1593 {
1594
1595         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1596         sbuf_set_drain(s, sbuf_sysctl_drain, req);
1597         return (s);
1598 }