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