]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/kern_sysctl.c
MFC r245457:
[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((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
424                         free((void *)(uintptr_t)(const void *)oidp->oid_name,
425                              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         ssize_t len;
443         char *newname;
444
445         /* You have to hook up somewhere.. */
446         if (parent == NULL)
447                 return(NULL);
448         /* Check if the node already exists, otherwise create it */
449         SYSCTL_XLOCK();
450         oidp = sysctl_find_oidname(name, parent);
451         if (oidp != NULL) {
452                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
453                         oidp->oid_refcnt++;
454                         /* Update the context */
455                         if (clist != NULL)
456                                 sysctl_ctx_entry_add(clist, oidp);
457                         SYSCTL_XUNLOCK();
458                         return (oidp);
459                 } else {
460                         SYSCTL_XUNLOCK();
461                         printf("can't re-use a leaf (%s)!\n", name);
462                         return (NULL);
463                 }
464         }
465         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
466         oidp->oid_parent = parent;
467         SLIST_NEXT(oidp, oid_link) = NULL;
468         oidp->oid_number = number;
469         oidp->oid_refcnt = 1;
470         len = strlen(name);
471         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
472         bcopy(name, newname, len + 1);
473         newname[len] = '\0';
474         oidp->oid_name = newname;
475         oidp->oid_handler = handler;
476         oidp->oid_kind = CTLFLAG_DYN | kind;
477         if ((kind & CTLTYPE) == CTLTYPE_NODE) {
478                 /* Allocate space for children */
479                 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
480                     M_SYSCTLOID, M_WAITOK));
481                 SLIST_INIT(SYSCTL_CHILDREN(oidp));
482         } else {
483                 oidp->oid_arg1 = arg1;
484                 oidp->oid_arg2 = arg2;
485         }
486         oidp->oid_fmt = fmt;
487         if (descr) {
488                 int len = strlen(descr) + 1;
489                 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
490                 if (oidp->oid_descr)
491                         strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
492         }
493         /* Update the context, if used */
494         if (clist != NULL)
495                 sysctl_ctx_entry_add(clist, oidp);
496         /* Register this oid */
497         sysctl_register_oid(oidp);
498         SYSCTL_XUNLOCK();
499         return (oidp);
500 }
501
502 /*
503  * Rename an existing oid.
504  */
505 void
506 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
507 {
508         ssize_t len;
509         char *newname;
510         void *oldname;
511
512         len = strlen(name);
513         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
514         bcopy(name, newname, len + 1);
515         newname[len] = '\0';
516         SYSCTL_XLOCK();
517         oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
518         oidp->oid_name = newname;
519         SYSCTL_XUNLOCK();
520         free(oldname, M_SYSCTLOID);
521 }
522
523 /*
524  * Reparent an existing oid.
525  */
526 int
527 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
528 {
529         struct sysctl_oid *oidp;
530
531         SYSCTL_XLOCK();
532         if (oid->oid_parent == parent) {
533                 SYSCTL_XUNLOCK();
534                 return (0);
535         }
536         oidp = sysctl_find_oidname(oid->oid_name, parent);
537         if (oidp != NULL) {
538                 SYSCTL_XUNLOCK();
539                 return (EEXIST);
540         }
541         sysctl_unregister_oid(oid);
542         oid->oid_parent = parent;
543         oid->oid_number = OID_AUTO;
544         sysctl_register_oid(oid);
545         SYSCTL_XUNLOCK();
546         return (0);
547 }
548
549 /*
550  * Register the kernel's oids on startup.
551  */
552 SET_DECLARE(sysctl_set, struct sysctl_oid);
553
554 static void
555 sysctl_register_all(void *arg)
556 {
557         struct sysctl_oid **oidp;
558
559         sx_init(&sysctlmemlock, "sysctl mem");
560         SYSCTL_INIT();
561         SYSCTL_XLOCK();
562         SET_FOREACH(oidp, sysctl_set)
563                 sysctl_register_oid(*oidp);
564         SYSCTL_XUNLOCK();
565 }
566 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
567
568 /*
569  * "Staff-functions"
570  *
571  * These functions implement a presently undocumented interface 
572  * used by the sysctl program to walk the tree, and get the type
573  * so it can print the value.
574  * This interface is under work and consideration, and should probably
575  * be killed with a big axe by the first person who can find the time.
576  * (be aware though, that the proper interface isn't as obvious as it
577  * may seem, there are various conflicting requirements.
578  *
579  * {0,0}        printf the entire MIB-tree.
580  * {0,1,...}    return the name of the "..." OID.
581  * {0,2,...}    return the next OID.
582  * {0,3}        return the OID of the name in "new"
583  * {0,4,...}    return the kind & format info for the "..." OID.
584  * {0,5,...}    return the description the "..." OID.
585  */
586
587 #ifdef SYSCTL_DEBUG
588 static void
589 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
590 {
591         int k;
592         struct sysctl_oid *oidp;
593
594         SYSCTL_ASSERT_XLOCKED();
595         SLIST_FOREACH(oidp, l, oid_link) {
596
597                 for (k=0; k<i; k++)
598                         printf(" ");
599
600                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
601
602                 printf("%c%c",
603                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
604                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
605
606                 if (oidp->oid_handler)
607                         printf(" *Handler");
608
609                 switch (oidp->oid_kind & CTLTYPE) {
610                         case CTLTYPE_NODE:
611                                 printf(" Node\n");
612                                 if (!oidp->oid_handler) {
613                                         sysctl_sysctl_debug_dump_node(
614                                                 oidp->oid_arg1, i+2);
615                                 }
616                                 break;
617                         case CTLTYPE_INT:    printf(" Int\n"); break;
618                         case CTLTYPE_STRING: printf(" String\n"); break;
619                         case CTLTYPE_QUAD:   printf(" Quad\n"); break;
620                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
621                         default:             printf("\n");
622                 }
623
624         }
625 }
626
627 static int
628 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
629 {
630         int error;
631
632         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
633         if (error)
634                 return (error);
635         SYSCTL_XLOCK();
636         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
637         SYSCTL_XUNLOCK();
638         return (ENOENT);
639 }
640
641 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
642         0, 0, sysctl_sysctl_debug, "-", "");
643 #endif
644
645 static int
646 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
647 {
648         int *name = (int *) arg1;
649         u_int namelen = arg2;
650         int error = 0;
651         struct sysctl_oid *oid;
652         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
653         char buf[10];
654
655         SYSCTL_XLOCK();
656         while (namelen) {
657                 if (!lsp) {
658                         snprintf(buf,sizeof(buf),"%d",*name);
659                         if (req->oldidx)
660                                 error = SYSCTL_OUT(req, ".", 1);
661                         if (!error)
662                                 error = SYSCTL_OUT(req, buf, strlen(buf));
663                         if (error)
664                                 goto out;
665                         namelen--;
666                         name++;
667                         continue;
668                 }
669                 lsp2 = 0;
670                 SLIST_FOREACH(oid, lsp, oid_link) {
671                         if (oid->oid_number != *name)
672                                 continue;
673
674                         if (req->oldidx)
675                                 error = SYSCTL_OUT(req, ".", 1);
676                         if (!error)
677                                 error = SYSCTL_OUT(req, oid->oid_name,
678                                         strlen(oid->oid_name));
679                         if (error)
680                                 goto out;
681
682                         namelen--;
683                         name++;
684
685                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
686                                 break;
687
688                         if (oid->oid_handler)
689                                 break;
690
691                         lsp2 = SYSCTL_CHILDREN(oid);
692                         break;
693                 }
694                 lsp = lsp2;
695         }
696         error = SYSCTL_OUT(req, "", 1);
697  out:
698         SYSCTL_XUNLOCK();
699         return (error);
700 }
701
702 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
703
704 static int
705 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
706         int *next, int *len, int level, struct sysctl_oid **oidpp)
707 {
708         struct sysctl_oid *oidp;
709
710         SYSCTL_ASSERT_XLOCKED();
711         *len = level;
712         SLIST_FOREACH(oidp, lsp, oid_link) {
713                 *next = oidp->oid_number;
714                 *oidpp = oidp;
715
716                 if (oidp->oid_kind & CTLFLAG_SKIP)
717                         continue;
718
719                 if (!namelen) {
720                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
721                                 return (0);
722                         if (oidp->oid_handler) 
723                                 /* We really should call the handler here...*/
724                                 return (0);
725                         lsp = SYSCTL_CHILDREN(oidp);
726                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
727                                 len, level+1, oidpp))
728                                 return (0);
729                         goto emptynode;
730                 }
731
732                 if (oidp->oid_number < *name)
733                         continue;
734
735                 if (oidp->oid_number > *name) {
736                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
737                                 return (0);
738                         if (oidp->oid_handler)
739                                 return (0);
740                         lsp = SYSCTL_CHILDREN(oidp);
741                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
742                                 next+1, len, level+1, oidpp))
743                                 return (0);
744                         goto next;
745                 }
746                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
747                         continue;
748
749                 if (oidp->oid_handler)
750                         continue;
751
752                 lsp = SYSCTL_CHILDREN(oidp);
753                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
754                         len, level+1, oidpp))
755                         return (0);
756         next:
757                 namelen = 1;
758         emptynode:
759                 *len = level;
760         }
761         return (1);
762 }
763
764 static int
765 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
766 {
767         int *name = (int *) arg1;
768         u_int namelen = arg2;
769         int i, j, error;
770         struct sysctl_oid *oid;
771         struct sysctl_oid_list *lsp = &sysctl__children;
772         int newoid[CTL_MAXNAME];
773
774         SYSCTL_XLOCK();
775         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
776         SYSCTL_XUNLOCK();
777         if (i)
778                 return (ENOENT);
779         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
780         return (error);
781 }
782
783 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
784
785 static int
786 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
787 {
788         int i;
789         struct sysctl_oid *oidp;
790         struct sysctl_oid_list *lsp = &sysctl__children;
791         char *p;
792
793         SYSCTL_ASSERT_XLOCKED();
794
795         if (!*name)
796                 return (ENOENT);
797
798         p = name + strlen(name) - 1 ;
799         if (*p == '.')
800                 *p = '\0';
801
802         *len = 0;
803
804         for (p = name; *p && *p != '.'; p++) 
805                 ;
806         i = *p;
807         if (i == '.')
808                 *p = '\0';
809
810         oidp = SLIST_FIRST(lsp);
811
812         while (oidp && *len < CTL_MAXNAME) {
813                 if (strcmp(name, oidp->oid_name)) {
814                         oidp = SLIST_NEXT(oidp, oid_link);
815                         continue;
816                 }
817                 *oid++ = oidp->oid_number;
818                 (*len)++;
819
820                 if (!i) {
821                         if (oidpp)
822                                 *oidpp = oidp;
823                         return (0);
824                 }
825
826                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
827                         break;
828
829                 if (oidp->oid_handler)
830                         break;
831
832                 lsp = SYSCTL_CHILDREN(oidp);
833                 oidp = SLIST_FIRST(lsp);
834                 name = p+1;
835                 for (p = name; *p && *p != '.'; p++) 
836                                 ;
837                 i = *p;
838                 if (i == '.')
839                         *p = '\0';
840         }
841         return (ENOENT);
842 }
843
844 static int
845 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
846 {
847         char *p;
848         int error, oid[CTL_MAXNAME], len;
849         struct sysctl_oid *op = 0;
850
851         if (!req->newlen) 
852                 return (ENOENT);
853         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
854                 return (ENAMETOOLONG);
855
856         p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
857
858         error = SYSCTL_IN(req, p, req->newlen);
859         if (error) {
860                 free(p, M_SYSCTL);
861                 return (error);
862         }
863
864         p [req->newlen] = '\0';
865         len = 0;
866         SYSCTL_XLOCK();
867         error = name2oid(p, oid, &len, &op);
868         SYSCTL_XUNLOCK();
869
870         free(p, M_SYSCTL);
871
872         if (error)
873                 return (error);
874
875         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
876         return (error);
877 }
878
879 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
880     0, 0, sysctl_sysctl_name2oid, "I", "");
881
882 static int
883 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
884 {
885         struct sysctl_oid *oid;
886         int error;
887
888         SYSCTL_XLOCK();
889         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
890         if (error)
891                 goto out;
892
893         if (oid->oid_fmt == NULL) {
894                 error = ENOENT;
895                 goto out;
896         }
897         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
898         if (error)
899                 goto out;
900         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
901  out:
902         SYSCTL_XUNLOCK();
903         return (error);
904 }
905
906
907 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
908     sysctl_sysctl_oidfmt, "");
909
910 static int
911 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
912 {
913         struct sysctl_oid *oid;
914         int error;
915
916         SYSCTL_XLOCK();
917         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
918         if (error)
919                 goto out;
920
921         if (oid->oid_descr == NULL) {
922                 error = ENOENT;
923                 goto out;
924         }
925         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
926  out:
927         SYSCTL_XUNLOCK();
928         return (error);
929 }
930
931 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
932
933 /*
934  * Default "handler" functions.
935  */
936
937 /*
938  * Handle an int, signed or unsigned.
939  * Two cases:
940  *     a variable:  point arg1 at it.
941  *     a constant:  pass it in arg2.
942  */
943
944 int
945 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
946 {
947         int tmpout, error = 0;
948
949         /*
950          * Attempt to get a coherent snapshot by making a copy of the data.
951          */
952         if (arg1)
953                 tmpout = *(int *)arg1;
954         else
955                 tmpout = arg2;
956         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
957
958         if (error || !req->newptr)
959                 return (error);
960
961         if (!arg1)
962                 error = EPERM;
963         else
964                 error = SYSCTL_IN(req, arg1, sizeof(int));
965         return (error);
966 }
967
968 /*
969  * Based on on sysctl_handle_int() convert milliseconds into ticks.
970  * Note: this is used by TCP.
971  */
972
973 int
974 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
975 {
976         int error, s, tt;
977
978         tt = *(int *)arg1;
979         s = (int)((int64_t)tt * 1000 / hz);
980
981         error = sysctl_handle_int(oidp, &s, 0, req);
982         if (error || !req->newptr)
983                 return (error);
984
985         tt = (int)((int64_t)s * hz / 1000);
986         if (tt < 1)
987                 return (EINVAL);
988
989         *(int *)arg1 = tt;
990         return (0);
991 }
992
993
994 /*
995  * Handle a long, signed or unsigned.  arg1 points to it.
996  */
997
998 int
999 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1000 {
1001         int error = 0;
1002         long tmplong;
1003 #ifdef SCTL_MASK32
1004         int tmpint;
1005 #endif
1006
1007         /*
1008          * Attempt to get a coherent snapshot by making a copy of the data.
1009          */
1010         if (!arg1)
1011                 return (EINVAL);
1012         tmplong = *(long *)arg1;
1013 #ifdef SCTL_MASK32
1014         if (req->flags & SCTL_MASK32) {
1015                 tmpint = tmplong;
1016                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1017         } else
1018 #endif
1019                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1020
1021         if (error || !req->newptr)
1022                 return (error);
1023
1024 #ifdef SCTL_MASK32
1025         if (req->flags & SCTL_MASK32) {
1026                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1027                 *(long *)arg1 = (long)tmpint;
1028         } else
1029 #endif
1030                 error = SYSCTL_IN(req, arg1, sizeof(long));
1031         return (error);
1032 }
1033
1034 /*
1035  * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1036  */
1037
1038 int
1039 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1040 {
1041         int error = 0;
1042         uint64_t tmpout;
1043
1044         /*
1045          * Attempt to get a coherent snapshot by making a copy of the data.
1046          */
1047         if (!arg1)
1048                 return (EINVAL);
1049         tmpout = *(uint64_t *)arg1;
1050         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1051
1052         if (error || !req->newptr)
1053                 return (error);
1054
1055         error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1056         return (error);
1057 }
1058
1059 /*
1060  * Handle our generic '\0' terminated 'C' string.
1061  * Two cases:
1062  *      a variable string:  point arg1 at it, arg2 is max length.
1063  *      a constant string:  point arg1 at it, arg2 is zero.
1064  */
1065
1066 int
1067 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1068 {
1069         int error=0;
1070         char *tmparg;
1071         size_t outlen;
1072
1073         /*
1074          * Attempt to get a coherent snapshot by copying to a
1075          * temporary kernel buffer.
1076          */
1077 retry:
1078         outlen = strlen((char *)arg1)+1;
1079         tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1080
1081         if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1082                 free(tmparg, M_SYSCTLTMP);
1083                 goto retry;
1084         }
1085
1086         error = SYSCTL_OUT(req, tmparg, outlen);
1087         free(tmparg, M_SYSCTLTMP);
1088
1089         if (error || !req->newptr)
1090                 return (error);
1091
1092         if ((req->newlen - req->newidx) >= arg2) {
1093                 error = EINVAL;
1094         } else {
1095                 arg2 = (req->newlen - req->newidx);
1096                 error = SYSCTL_IN(req, arg1, arg2);
1097                 ((char *)arg1)[arg2] = '\0';
1098         }
1099
1100         return (error);
1101 }
1102
1103 /*
1104  * Handle any kind of opaque data.
1105  * arg1 points to it, arg2 is the size.
1106  */
1107
1108 int
1109 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1110 {
1111         int error, tries;
1112         u_int generation;
1113         struct sysctl_req req2;
1114
1115         /*
1116          * Attempt to get a coherent snapshot, by using the thread
1117          * pre-emption counter updated from within mi_switch() to
1118          * determine if we were pre-empted during a bcopy() or
1119          * copyout(). Make 3 attempts at doing this before giving up.
1120          * If we encounter an error, stop immediately.
1121          */
1122         tries = 0;
1123         req2 = *req;
1124 retry:
1125         generation = curthread->td_generation;
1126         error = SYSCTL_OUT(req, arg1, arg2);
1127         if (error)
1128                 return (error);
1129         tries++;
1130         if (generation != curthread->td_generation && tries < 3) {
1131                 *req = req2;
1132                 goto retry;
1133         }
1134
1135         error = SYSCTL_IN(req, arg1, arg2);
1136
1137         return (error);
1138 }
1139
1140 /*
1141  * Transfer functions to/from kernel space.
1142  * XXX: rather untested at this point
1143  */
1144 static int
1145 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1146 {
1147         size_t i = 0;
1148
1149         if (req->oldptr) {
1150                 i = l;
1151                 if (req->oldlen <= req->oldidx)
1152                         i = 0;
1153                 else
1154                         if (i > req->oldlen - req->oldidx)
1155                                 i = req->oldlen - req->oldidx;
1156                 if (i > 0)
1157                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
1158         }
1159         req->oldidx += l;
1160         if (req->oldptr && i != l)
1161                 return (ENOMEM);
1162         return (0);
1163 }
1164
1165 static int
1166 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1167 {
1168         if (!req->newptr)
1169                 return (0);
1170         if (req->newlen - req->newidx < l)
1171                 return (EINVAL);
1172         bcopy((char *)req->newptr + req->newidx, p, l);
1173         req->newidx += l;
1174         return (0);
1175 }
1176
1177 int
1178 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1179     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1180 {
1181         int error = 0;
1182         struct sysctl_req req;
1183
1184         bzero(&req, sizeof req);
1185
1186         req.td = td;
1187         req.flags = flags;
1188
1189         if (oldlenp) {
1190                 req.oldlen = *oldlenp;
1191         }
1192         req.validlen = req.oldlen;
1193
1194         if (old) {
1195                 req.oldptr= old;
1196         }
1197
1198         if (new != NULL) {
1199                 req.newlen = newlen;
1200                 req.newptr = new;
1201         }
1202
1203         req.oldfunc = sysctl_old_kernel;
1204         req.newfunc = sysctl_new_kernel;
1205         req.lock = REQ_LOCKED;
1206
1207         SYSCTL_XLOCK();
1208         error = sysctl_root(0, name, namelen, &req);
1209         SYSCTL_XUNLOCK();
1210
1211         if (req.lock == REQ_WIRED && req.validlen > 0)
1212                 vsunlock(req.oldptr, req.validlen);
1213
1214         if (error && error != ENOMEM)
1215                 return (error);
1216
1217         if (retval) {
1218                 if (req.oldptr && req.oldidx > req.validlen)
1219                         *retval = req.validlen;
1220                 else
1221                         *retval = req.oldidx;
1222         }
1223         return (error);
1224 }
1225
1226 int
1227 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1228     void *new, size_t newlen, size_t *retval, int flags)
1229 {
1230         int oid[CTL_MAXNAME];
1231         size_t oidlen, plen;
1232         int error;
1233
1234         oid[0] = 0;             /* sysctl internal magic */
1235         oid[1] = 3;             /* name2oid */
1236         oidlen = sizeof(oid);
1237
1238         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1239             (void *)name, strlen(name), &plen, flags);
1240         if (error)
1241                 return (error);
1242
1243         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1244             new, newlen, retval, flags);
1245         return (error);
1246 }
1247
1248 /*
1249  * Transfer function to/from user space.
1250  */
1251 static int
1252 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1253 {
1254         size_t i, len, origidx;
1255         int error;
1256
1257         origidx = req->oldidx;
1258         req->oldidx += l;
1259         if (req->oldptr == NULL)
1260                 return (0);
1261         /*
1262          * If we have not wired the user supplied buffer and we are currently
1263          * holding locks, drop a witness warning, as it's possible that
1264          * write operations to the user page can sleep.
1265          */
1266         if (req->lock != REQ_WIRED)
1267                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1268                     "sysctl_old_user()");
1269         i = l;
1270         len = req->validlen;
1271         if (len <= origidx)
1272                 i = 0;
1273         else {
1274                 if (i > len - origidx)
1275                         i = len - origidx;
1276                 if (req->lock == REQ_WIRED) {
1277                         error = copyout_nofault(p, (char *)req->oldptr +
1278                             origidx, i);
1279                 } else
1280                         error = copyout(p, (char *)req->oldptr + origidx, i);
1281                 if (error != 0)
1282                         return (error);
1283         }
1284         if (i < l)
1285                 return (ENOMEM);
1286         return (0);
1287 }
1288
1289 static int
1290 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1291 {
1292         int error;
1293
1294         if (!req->newptr)
1295                 return (0);
1296         if (req->newlen - req->newidx < l)
1297                 return (EINVAL);
1298         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1299             "sysctl_new_user()");
1300         error = copyin((char *)req->newptr + req->newidx, p, l);
1301         req->newidx += l;
1302         return (error);
1303 }
1304
1305 /*
1306  * Wire the user space destination buffer.  If set to a value greater than
1307  * zero, the len parameter limits the maximum amount of wired memory.
1308  */
1309 int
1310 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1311 {
1312         int ret;
1313         size_t wiredlen;
1314
1315         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1316         ret = 0;
1317         if (req->lock == REQ_LOCKED && req->oldptr &&
1318             req->oldfunc == sysctl_old_user) {
1319                 if (wiredlen != 0) {
1320                         ret = vslock(req->oldptr, wiredlen);
1321                         if (ret != 0) {
1322                                 if (ret != ENOMEM)
1323                                         return (ret);
1324                                 wiredlen = 0;
1325                         }
1326                 }
1327                 req->lock = REQ_WIRED;
1328                 req->validlen = wiredlen;
1329         }
1330         return (0);
1331 }
1332
1333 int
1334 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1335     int *nindx, struct sysctl_req *req)
1336 {
1337         struct sysctl_oid_list *lsp;
1338         struct sysctl_oid *oid;
1339         int indx;
1340
1341         SYSCTL_ASSERT_XLOCKED();
1342         lsp = &sysctl__children;
1343         indx = 0;
1344         while (indx < CTL_MAXNAME) {
1345                 SLIST_FOREACH(oid, lsp, oid_link) {
1346                         if (oid->oid_number == name[indx])
1347                                 break;
1348                 }
1349                 if (oid == NULL)
1350                         return (ENOENT);
1351
1352                 indx++;
1353                 if (oid->oid_kind & CTLFLAG_NOLOCK)
1354                         req->lock = REQ_UNLOCKED;
1355                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1356                         if (oid->oid_handler != NULL || indx == namelen) {
1357                                 *noid = oid;
1358                                 if (nindx != NULL)
1359                                         *nindx = indx;
1360                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1361                                     ("%s found DYING node %p", __func__, oid));
1362                                 return (0);
1363                         }
1364                         lsp = SYSCTL_CHILDREN(oid);
1365                 } else if (indx == namelen) {
1366                         *noid = oid;
1367                         if (nindx != NULL)
1368                                 *nindx = indx;
1369                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1370                             ("%s found DYING node %p", __func__, oid));
1371                         return (0);
1372                 } else {
1373                         return (ENOTDIR);
1374                 }
1375         }
1376         return (ENOENT);
1377 }
1378
1379 /*
1380  * Traverse our tree, and find the right node, execute whatever it points
1381  * to, and return the resulting error code.
1382  */
1383
1384 static int
1385 sysctl_root(SYSCTL_HANDLER_ARGS)
1386 {
1387         struct sysctl_oid *oid;
1388         int error, indx, lvl;
1389
1390         SYSCTL_ASSERT_XLOCKED();
1391
1392         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1393         if (error)
1394                 return (error);
1395
1396         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1397                 /*
1398                  * You can't call a sysctl when it's a node, but has
1399                  * no handler.  Inform the user that it's a node.
1400                  * The indx may or may not be the same as namelen.
1401                  */
1402                 if (oid->oid_handler == NULL)
1403                         return (EISDIR);
1404         }
1405
1406         /* Is this sysctl writable? */
1407         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1408                 return (EPERM);
1409
1410         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1411
1412         /* Is this sysctl sensitive to securelevels? */
1413         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1414                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1415                 error = securelevel_gt(req->td->td_ucred, lvl);
1416                 if (error)
1417                         return (error);
1418         }
1419
1420         /* Is this sysctl writable by only privileged users? */
1421         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1422                 int priv;
1423
1424                 if (oid->oid_kind & CTLFLAG_PRISON)
1425                         priv = PRIV_SYSCTL_WRITEJAIL;
1426 #ifdef VIMAGE
1427                 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1428                      prison_owns_vnet(req->td->td_ucred))
1429                         priv = PRIV_SYSCTL_WRITEJAIL;
1430 #endif
1431                 else
1432                         priv = PRIV_SYSCTL_WRITE;
1433                 error = priv_check(req->td, priv);
1434                 if (error)
1435                         return (error);
1436         }
1437
1438         if (!oid->oid_handler)
1439                 return (EINVAL);
1440
1441         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1442                 arg1 = (int *)arg1 + indx;
1443                 arg2 -= indx;
1444         } else {
1445                 arg1 = oid->oid_arg1;
1446                 arg2 = oid->oid_arg2;
1447         }
1448 #ifdef MAC
1449         error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1450             req);
1451         if (error != 0)
1452                 return (error);
1453 #endif
1454         oid->oid_running++;
1455         SYSCTL_XUNLOCK();
1456
1457         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1458                 mtx_lock(&Giant);
1459         error = oid->oid_handler(oid, arg1, arg2, req);
1460         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1461                 mtx_unlock(&Giant);
1462
1463         KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1464
1465         SYSCTL_XLOCK();
1466         oid->oid_running--;
1467         if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1468                 wakeup(&oid->oid_running);
1469         return (error);
1470 }
1471
1472 #ifndef _SYS_SYSPROTO_H_
1473 struct sysctl_args {
1474         int     *name;
1475         u_int   namelen;
1476         void    *old;
1477         size_t  *oldlenp;
1478         void    *new;
1479         size_t  newlen;
1480 };
1481 #endif
1482 int
1483 __sysctl(struct thread *td, struct sysctl_args *uap)
1484 {
1485         int error, i, name[CTL_MAXNAME];
1486         size_t j;
1487
1488         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1489                 return (EINVAL);
1490
1491         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1492         if (error)
1493                 return (error);
1494
1495         error = userland_sysctl(td, name, uap->namelen,
1496                 uap->old, uap->oldlenp, 0,
1497                 uap->new, uap->newlen, &j, 0);
1498         if (error && error != ENOMEM)
1499                 return (error);
1500         if (uap->oldlenp) {
1501                 i = copyout(&j, uap->oldlenp, sizeof(j));
1502                 if (i)
1503                         return (i);
1504         }
1505         return (error);
1506 }
1507
1508 /*
1509  * This is used from various compatibility syscalls too.  That's why name
1510  * must be in kernel space.
1511  */
1512 int
1513 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1514     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1515     int flags)
1516 {
1517         int error = 0, memlocked;
1518         struct sysctl_req req;
1519
1520         bzero(&req, sizeof req);
1521
1522         req.td = td;
1523         req.flags = flags;
1524
1525         if (oldlenp) {
1526                 if (inkernel) {
1527                         req.oldlen = *oldlenp;
1528                 } else {
1529                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1530                         if (error)
1531                                 return (error);
1532                 }
1533         }
1534         req.validlen = req.oldlen;
1535
1536         if (old) {
1537                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1538                         return (EFAULT);
1539                 req.oldptr= old;
1540         }
1541
1542         if (new != NULL) {
1543                 if (!useracc(new, newlen, VM_PROT_READ))
1544                         return (EFAULT);
1545                 req.newlen = newlen;
1546                 req.newptr = new;
1547         }
1548
1549         req.oldfunc = sysctl_old_user;
1550         req.newfunc = sysctl_new_user;
1551         req.lock = REQ_LOCKED;
1552
1553 #ifdef KTRACE
1554         if (KTRPOINT(curthread, KTR_SYSCTL))
1555                 ktrsysctl(name, namelen);
1556 #endif
1557
1558         if (req.oldlen > PAGE_SIZE) {
1559                 memlocked = 1;
1560                 sx_xlock(&sysctlmemlock);
1561         } else
1562                 memlocked = 0;
1563         CURVNET_SET(TD_TO_VNET(td));
1564
1565         for (;;) {
1566                 req.oldidx = 0;
1567                 req.newidx = 0;
1568                 SYSCTL_XLOCK();
1569                 error = sysctl_root(0, name, namelen, &req);
1570                 SYSCTL_XUNLOCK();
1571                 if (error != EAGAIN)
1572                         break;
1573                 uio_yield();
1574         }
1575
1576         CURVNET_RESTORE();
1577
1578         if (req.lock == REQ_WIRED && req.validlen > 0)
1579                 vsunlock(req.oldptr, req.validlen);
1580         if (memlocked)
1581                 sx_xunlock(&sysctlmemlock);
1582
1583         if (error && error != ENOMEM)
1584                 return (error);
1585
1586         if (retval) {
1587                 if (req.oldptr && req.oldidx > req.validlen)
1588                         *retval = req.validlen;
1589                 else
1590                         *retval = req.oldidx;
1591         }
1592         return (error);
1593 }
1594
1595 /*
1596  * Drain into a sysctl struct.  The user buffer should be wired if a page
1597  * fault would cause issue.
1598  */
1599 static int
1600 sbuf_sysctl_drain(void *arg, const char *data, int len)
1601 {
1602         struct sysctl_req *req = arg;
1603         int error;
1604
1605         error = SYSCTL_OUT(req, data, len);
1606         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1607         return (error == 0 ? len : -error);
1608 }
1609
1610 struct sbuf *
1611 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1612     struct sysctl_req *req)
1613 {
1614
1615         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1616         sbuf_set_drain(s, sbuf_sysctl_drain, req);
1617         return (s);
1618 }