]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_sysctl.c
Use NULL for SYSINIT's last arg, which is a pointer type
[FreeBSD/FreeBSD.git] / sys / kern / kern_sysctl.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Mike Karels at Berkeley Software Design, Inc.
9  *
10  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
11  * project, to make these variables more userfriendly.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)kern_sysctl.c       8.4 (Berkeley) 4/14/94
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include "opt_capsicum.h"
44 #include "opt_ktrace.h"
45
46 #include <sys/param.h>
47 #include <sys/fail.h>
48 #include <sys/systm.h>
49 #include <sys/capsicum.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/jail.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/rmlock.h>
59 #include <sys/sbuf.h>
60 #include <sys/sx.h>
61 #include <sys/sysproto.h>
62 #include <sys/uio.h>
63 #ifdef KTRACE
64 #include <sys/ktrace.h>
65 #endif
66
67 #include <net/vnet.h>
68
69 #include <security/mac/mac_framework.h>
70
71 #include <vm/vm.h>
72 #include <vm/vm_extern.h>
73
74 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
75 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
76 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
77
78 /*
79  * The sysctllock protects the MIB tree.  It also protects sysctl
80  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
81  * sysctl_unregister_oid() routines require the sysctllock to already
82  * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
83  * provided for the few places in the kernel which need to use that
84  * API rather than using the dynamic API.  Use of the dynamic API is
85  * strongly encouraged for most code.
86  *
87  * The sysctlmemlock is used to limit the amount of user memory wired for
88  * sysctl requests.  This is implemented by serializing any userland
89  * sysctl requests larger than a single page via an exclusive lock.
90  */
91 static struct rmlock sysctllock;
92 static struct sx __exclusive_cache_line sysctlmemlock;
93
94 #define SYSCTL_WLOCK()          rm_wlock(&sysctllock)
95 #define SYSCTL_WUNLOCK()        rm_wunlock(&sysctllock)
96 #define SYSCTL_RLOCK(tracker)   rm_rlock(&sysctllock, (tracker))
97 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
98 #define SYSCTL_WLOCKED()        rm_wowned(&sysctllock)
99 #define SYSCTL_ASSERT_LOCKED()  rm_assert(&sysctllock, RA_LOCKED)
100 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
101 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
102 #define SYSCTL_INIT()           rm_init_flags(&sysctllock, "sysctl lock", \
103                                     RM_SLEEPABLE)
104 #define SYSCTL_SLEEP(ch, wmesg, timo)                                   \
105                                 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
106
107 static int sysctl_root(SYSCTL_HANDLER_ARGS);
108
109 /* Root list */
110 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
111
112 static int      sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
113                     int recurse);
114 static int      sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
115 static int      sysctl_new_kernel(struct sysctl_req *, void *, size_t);
116
117 static struct sysctl_oid *
118 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
119 {
120         struct sysctl_oid *oidp;
121
122         SYSCTL_ASSERT_LOCKED();
123         SLIST_FOREACH(oidp, list, oid_link) {
124                 if (strcmp(oidp->oid_name, name) == 0) {
125                         return (oidp);
126                 }
127         }
128         return (NULL);
129 }
130
131 /*
132  * Initialization of the MIB tree.
133  *
134  * Order by number in each list.
135  */
136 void
137 sysctl_wlock(void)
138 {
139
140         SYSCTL_WLOCK();
141 }
142
143 void
144 sysctl_wunlock(void)
145 {
146
147         SYSCTL_WUNLOCK();
148 }
149
150 static int
151 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
152     struct sysctl_req *req, struct rm_priotracker *tracker)
153 {
154         int error;
155
156         if (oid->oid_kind & CTLFLAG_DYN)
157                 atomic_add_int(&oid->oid_running, 1);
158
159         if (tracker != NULL)
160                 SYSCTL_RUNLOCK(tracker);
161         else
162                 SYSCTL_WUNLOCK();
163
164         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
165                 mtx_lock(&Giant);
166         error = oid->oid_handler(oid, arg1, arg2, req);
167         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
168                 mtx_unlock(&Giant);
169
170         KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
171
172         if (tracker != NULL)
173                 SYSCTL_RLOCK(tracker);
174         else
175                 SYSCTL_WLOCK();
176
177         if (oid->oid_kind & CTLFLAG_DYN) {
178                 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
179                     (oid->oid_kind & CTLFLAG_DYING) != 0)
180                         wakeup(&oid->oid_running);
181         }
182
183         return (error);
184 }
185
186 static void
187 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
188 {
189         struct sysctl_req req;
190         struct sysctl_oid *curr;
191         char *penv = NULL;
192         char path[96];
193         ssize_t rem = sizeof(path);
194         ssize_t len;
195         uint8_t val_8;
196         uint16_t val_16;
197         uint32_t val_32;
198         int val_int;
199         long val_long;
200         int64_t val_64;
201         quad_t val_quad;
202         int error;
203
204         path[--rem] = 0;
205
206         for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
207                 len = strlen(curr->oid_name);
208                 rem -= len;
209                 if (curr != oidp)
210                         rem -= 1;
211                 if (rem < 0) {
212                         printf("OID path exceeds %d bytes\n", (int)sizeof(path));
213                         return;
214                 }
215                 memcpy(path + rem, curr->oid_name, len);
216                 if (curr != oidp)
217                         path[rem + len] = '.';
218         }
219
220         memset(&req, 0, sizeof(req));
221
222         req.td = curthread;
223         req.oldfunc = sysctl_old_kernel;
224         req.newfunc = sysctl_new_kernel;
225         req.lock = REQ_UNWIRED;
226
227         switch (oidp->oid_kind & CTLTYPE) {
228         case CTLTYPE_INT:
229                 if (getenv_int(path + rem, &val_int) == 0)
230                         return;
231                 req.newlen = sizeof(val_int);
232                 req.newptr = &val_int;
233                 break;
234         case CTLTYPE_UINT:
235                 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
236                         return;
237                 req.newlen = sizeof(val_int);
238                 req.newptr = &val_int;
239                 break;
240         case CTLTYPE_LONG:
241                 if (getenv_long(path + rem, &val_long) == 0)
242                         return;
243                 req.newlen = sizeof(val_long);
244                 req.newptr = &val_long;
245                 break;
246         case CTLTYPE_ULONG:
247                 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
248                         return;
249                 req.newlen = sizeof(val_long);
250                 req.newptr = &val_long;
251                 break;
252         case CTLTYPE_S8:
253                 if (getenv_int(path + rem, &val_int) == 0)
254                         return;
255                 val_8 = val_int;
256                 req.newlen = sizeof(val_8);
257                 req.newptr = &val_8;
258                 break;
259         case CTLTYPE_S16:
260                 if (getenv_int(path + rem, &val_int) == 0)
261                         return;
262                 val_16 = val_int;
263                 req.newlen = sizeof(val_16);
264                 req.newptr = &val_16;
265                 break;
266         case CTLTYPE_S32:
267                 if (getenv_long(path + rem, &val_long) == 0)
268                         return;
269                 val_32 = val_long;
270                 req.newlen = sizeof(val_32);
271                 req.newptr = &val_32;
272                 break;
273         case CTLTYPE_S64:
274                 if (getenv_quad(path + rem, &val_quad) == 0)
275                         return;
276                 val_64 = val_quad;
277                 req.newlen = sizeof(val_64);
278                 req.newptr = &val_64;
279                 break;
280         case CTLTYPE_U8:
281                 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
282                         return;
283                 val_8 = val_int;
284                 req.newlen = sizeof(val_8);
285                 req.newptr = &val_8;
286                 break;
287         case CTLTYPE_U16:
288                 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
289                         return;
290                 val_16 = val_int;
291                 req.newlen = sizeof(val_16);
292                 req.newptr = &val_16;
293                 break;
294         case CTLTYPE_U32:
295                 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
296                         return;
297                 val_32 = val_long;
298                 req.newlen = sizeof(val_32);
299                 req.newptr = &val_32;
300                 break;
301         case CTLTYPE_U64:
302                 /* XXX there is no getenv_uquad() */
303                 if (getenv_quad(path + rem, &val_quad) == 0)
304                         return;
305                 val_64 = val_quad;
306                 req.newlen = sizeof(val_64);
307                 req.newptr = &val_64;
308                 break;
309         case CTLTYPE_STRING:
310                 penv = kern_getenv(path + rem);
311                 if (penv == NULL)
312                         return;
313                 req.newlen = strlen(penv);
314                 req.newptr = penv;
315                 break;
316         default:
317                 return;
318         }
319         error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
320             oidp->oid_arg2, &req, NULL);
321         if (error != 0)
322                 printf("Setting sysctl %s failed: %d\n", path + rem, error);
323         if (penv != NULL)
324                 freeenv(penv);
325 }
326
327 static int
328 sbuf_printf_drain(void *arg __unused, const char *data, int len)
329 {
330
331         return (printf("%.*s", len, data));
332 }
333
334 /*
335  * Locate the path to a given oid.  Returns the length of the resulting path,
336  * or -1 if the oid was not found.  nodes must have room for CTL_MAXNAME
337  * elements and be NULL initialized.
338  */
339 static int
340 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
341 {
342         int indx;
343
344         SYSCTL_ASSERT_LOCKED();
345         indx = 0;
346         while (indx < CTL_MAXNAME && indx >= 0) {
347                 if (nodes[indx] == NULL && indx == 0)
348                         nodes[indx] = SLIST_FIRST(&sysctl__children);
349                 else if (nodes[indx] == NULL)
350                         nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
351                 else
352                         nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
353
354                 if (nodes[indx] == needle)
355                         return (indx + 1);
356
357                 if (nodes[indx] == NULL) {
358                         indx--;
359                         continue;
360                 }
361
362                 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
363                         indx++;
364                         continue;
365                 }
366         }
367         return (-1);
368 }
369
370 static void
371 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
372 {
373         struct sysctl_oid *nodes[CTL_MAXNAME];
374         char buf[128];
375         struct sbuf sb;
376         int rc, i;
377
378         (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
379         sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
380
381         sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
382
383         memset(nodes, 0, sizeof(nodes));
384         rc = sysctl_search_oid(nodes, leaf);
385         if (rc > 0) {
386                 for (i = 0; i < rc; i++)
387                         sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
388                             i != (rc - 1), ".");
389         } else {
390                 sbuf_printf(&sb, "%s", leaf->oid_name);
391         }
392         sbuf_printf(&sb, ")!\n");
393
394         (void)sbuf_finish(&sb);
395 }
396
397 #ifdef SYSCTL_DEBUG
398 static int
399 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
400 {
401         struct rm_priotracker tracker;
402
403         SYSCTL_RLOCK(&tracker);
404         sysctl_warn_reuse(__func__, oidp);
405         SYSCTL_RUNLOCK(&tracker);
406         return (0);
407 }
408 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
409         0, 0, sysctl_reuse_test, "-", "");
410 #endif
411
412 void
413 sysctl_register_oid(struct sysctl_oid *oidp)
414 {
415         struct sysctl_oid_list *parent = oidp->oid_parent;
416         struct sysctl_oid *p;
417         struct sysctl_oid *q;
418         int oid_number;
419         int timeout = 2;
420
421         /*
422          * First check if another oid with the same name already
423          * exists in the parent's list.
424          */
425         SYSCTL_ASSERT_WLOCKED();
426         p = sysctl_find_oidname(oidp->oid_name, parent);
427         if (p != NULL) {
428                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
429                         p->oid_refcnt++;
430                         return;
431                 } else {
432                         sysctl_warn_reuse(__func__, p);
433                         return;
434                 }
435         }
436         /* get current OID number */
437         oid_number = oidp->oid_number;
438
439 #if (OID_AUTO >= 0)
440 #error "OID_AUTO is expected to be a negative value"
441 #endif  
442         /*
443          * Any negative OID number qualifies as OID_AUTO. Valid OID
444          * numbers should always be positive.
445          *
446          * NOTE: DO NOT change the starting value here, change it in
447          * <sys/sysctl.h>, and make sure it is at least 256 to
448          * accommodate e.g. net.inet.raw as a static sysctl node.
449          */
450         if (oid_number < 0) {
451                 static int newoid;
452
453                 /*
454                  * By decrementing the next OID number we spend less
455                  * time inserting the OIDs into a sorted list.
456                  */
457                 if (--newoid < CTL_AUTO_START)
458                         newoid = 0x7fffffff;
459
460                 oid_number = newoid;
461         }
462
463         /*
464          * Insert the OID into the parent's list sorted by OID number.
465          */
466 retry:
467         q = NULL;
468         SLIST_FOREACH(p, parent, oid_link) {
469                 /* check if the current OID number is in use */
470                 if (oid_number == p->oid_number) {
471                         /* get the next valid OID number */
472                         if (oid_number < CTL_AUTO_START ||
473                             oid_number == 0x7fffffff) {
474                                 /* wraparound - restart */
475                                 oid_number = CTL_AUTO_START;
476                                 /* don't loop forever */
477                                 if (!timeout--)
478                                         panic("sysctl: Out of OID numbers\n");
479                                 goto retry;
480                         } else {
481                                 oid_number++;
482                         }
483                 } else if (oid_number < p->oid_number)
484                         break;
485                 q = p;
486         }
487         /* check for non-auto OID number collision */
488         if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
489             oid_number >= CTL_AUTO_START) {
490                 printf("sysctl: OID number(%d) is already in use for '%s'\n",
491                     oidp->oid_number, oidp->oid_name);
492         }
493         /* update the OID number, if any */
494         oidp->oid_number = oid_number;
495         if (q != NULL)
496                 SLIST_INSERT_AFTER(q, oidp, oid_link);
497         else
498                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
499
500         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
501 #ifdef VIMAGE
502             (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
503 #endif
504             (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
505             (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
506                 /* only fetch value once */
507                 oidp->oid_kind |= CTLFLAG_NOFETCH;
508                 /* try to fetch value from kernel environment */
509                 sysctl_load_tunable_by_oid_locked(oidp);
510         }
511 }
512
513 void
514 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
515 {
516
517         /*
518          * Mark the leaf as dormant if it's not to be immediately enabled.
519          * We do not disable nodes as they can be shared between modules
520          * and it is always safe to access a node.
521          */
522         KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
523             ("internal flag is set in oid_kind"));
524         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
525                 oidp->oid_kind |= CTLFLAG_DORMANT;
526         sysctl_register_oid(oidp);
527 }
528
529 void
530 sysctl_enable_oid(struct sysctl_oid *oidp)
531 {
532
533         SYSCTL_ASSERT_WLOCKED();
534         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
535                 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
536                     ("sysctl node is marked as dormant"));
537                 return;
538         }
539         KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
540             ("enabling already enabled sysctl oid"));
541         oidp->oid_kind &= ~CTLFLAG_DORMANT;
542 }
543
544 void
545 sysctl_unregister_oid(struct sysctl_oid *oidp)
546 {
547         struct sysctl_oid *p;
548         int error;
549
550         SYSCTL_ASSERT_WLOCKED();
551         error = ENOENT;
552         if (oidp->oid_number == OID_AUTO) {
553                 error = EINVAL;
554         } else {
555                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
556                         if (p == oidp) {
557                                 SLIST_REMOVE(oidp->oid_parent, oidp,
558                                     sysctl_oid, oid_link);
559                                 error = 0;
560                                 break;
561                         }
562                 }
563         }
564
565         /* 
566          * This can happen when a module fails to register and is
567          * being unloaded afterwards.  It should not be a panic()
568          * for normal use.
569          */
570         if (error)
571                 printf("%s: failed to unregister sysctl\n", __func__);
572 }
573
574 /* Initialize a new context to keep track of dynamically added sysctls. */
575 int
576 sysctl_ctx_init(struct sysctl_ctx_list *c)
577 {
578
579         if (c == NULL) {
580                 return (EINVAL);
581         }
582
583         /*
584          * No locking here, the caller is responsible for not adding
585          * new nodes to a context until after this function has
586          * returned.
587          */
588         TAILQ_INIT(c);
589         return (0);
590 }
591
592 /* Free the context, and destroy all dynamic oids registered in this context */
593 int
594 sysctl_ctx_free(struct sysctl_ctx_list *clist)
595 {
596         struct sysctl_ctx_entry *e, *e1;
597         int error;
598
599         error = 0;
600         /*
601          * First perform a "dry run" to check if it's ok to remove oids.
602          * XXX FIXME
603          * XXX This algorithm is a hack. But I don't know any
604          * XXX better solution for now...
605          */
606         SYSCTL_WLOCK();
607         TAILQ_FOREACH(e, clist, link) {
608                 error = sysctl_remove_oid_locked(e->entry, 0, 0);
609                 if (error)
610                         break;
611         }
612         /*
613          * Restore deregistered entries, either from the end,
614          * or from the place where error occurred.
615          * e contains the entry that was not unregistered
616          */
617         if (error)
618                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
619         else
620                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
621         while (e1 != NULL) {
622                 sysctl_register_oid(e1->entry);
623                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
624         }
625         if (error) {
626                 SYSCTL_WUNLOCK();
627                 return(EBUSY);
628         }
629         /* Now really delete the entries */
630         e = TAILQ_FIRST(clist);
631         while (e != NULL) {
632                 e1 = TAILQ_NEXT(e, link);
633                 error = sysctl_remove_oid_locked(e->entry, 1, 0);
634                 if (error)
635                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
636                             e->entry->oid_name);
637                 free(e, M_SYSCTLOID);
638                 e = e1;
639         }
640         SYSCTL_WUNLOCK();
641         return (error);
642 }
643
644 /* Add an entry to the context */
645 struct sysctl_ctx_entry *
646 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
647 {
648         struct sysctl_ctx_entry *e;
649
650         SYSCTL_ASSERT_WLOCKED();
651         if (clist == NULL || oidp == NULL)
652                 return(NULL);
653         e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
654         e->entry = oidp;
655         TAILQ_INSERT_HEAD(clist, e, link);
656         return (e);
657 }
658
659 /* Find an entry in the context */
660 struct sysctl_ctx_entry *
661 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
662 {
663         struct sysctl_ctx_entry *e;
664
665         SYSCTL_ASSERT_WLOCKED();
666         if (clist == NULL || oidp == NULL)
667                 return(NULL);
668         TAILQ_FOREACH(e, clist, link) {
669                 if(e->entry == oidp)
670                         return(e);
671         }
672         return (e);
673 }
674
675 /*
676  * Delete an entry from the context.
677  * NOTE: this function doesn't free oidp! You have to remove it
678  * with sysctl_remove_oid().
679  */
680 int
681 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
682 {
683         struct sysctl_ctx_entry *e;
684
685         if (clist == NULL || oidp == NULL)
686                 return (EINVAL);
687         SYSCTL_WLOCK();
688         e = sysctl_ctx_entry_find(clist, oidp);
689         if (e != NULL) {
690                 TAILQ_REMOVE(clist, e, link);
691                 SYSCTL_WUNLOCK();
692                 free(e, M_SYSCTLOID);
693                 return (0);
694         } else {
695                 SYSCTL_WUNLOCK();
696                 return (ENOENT);
697         }
698 }
699
700 /*
701  * Remove dynamically created sysctl trees.
702  * oidp - top of the tree to be removed
703  * del - if 0 - just deregister, otherwise free up entries as well
704  * recurse - if != 0 traverse the subtree to be deleted
705  */
706 int
707 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
708 {
709         int error;
710
711         SYSCTL_WLOCK();
712         error = sysctl_remove_oid_locked(oidp, del, recurse);
713         SYSCTL_WUNLOCK();
714         return (error);
715 }
716
717 int
718 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
719     int del, int recurse)
720 {
721         struct sysctl_oid *p, *tmp;
722         int error;
723
724         error = ENOENT;
725         SYSCTL_WLOCK();
726         SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
727                 if (strcmp(p->oid_name, name) == 0) {
728                         error = sysctl_remove_oid_locked(p, del, recurse);
729                         break;
730                 }
731         }
732         SYSCTL_WUNLOCK();
733
734         return (error);
735 }
736
737
738 static int
739 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
740 {
741         struct sysctl_oid *p, *tmp;
742         int error;
743
744         SYSCTL_ASSERT_WLOCKED();
745         if (oidp == NULL)
746                 return(EINVAL);
747         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
748                 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
749                     oidp->oid_name);
750                 return (EINVAL);
751         }
752         /*
753          * WARNING: normal method to do this should be through
754          * sysctl_ctx_free(). Use recursing as the last resort
755          * method to purge your sysctl tree of leftovers...
756          * However, if some other code still references these nodes,
757          * it will panic.
758          */
759         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
760                 if (oidp->oid_refcnt == 1) {
761                         SLIST_FOREACH_SAFE(p,
762                             SYSCTL_CHILDREN(oidp), oid_link, tmp) {
763                                 if (!recurse) {
764                                         printf("Warning: failed attempt to "
765                                             "remove oid %s with child %s\n",
766                                             oidp->oid_name, p->oid_name);
767                                         return (ENOTEMPTY);
768                                 }
769                                 error = sysctl_remove_oid_locked(p, del,
770                                     recurse);
771                                 if (error)
772                                         return (error);
773                         }
774                 }
775         }
776         if (oidp->oid_refcnt > 1 ) {
777                 oidp->oid_refcnt--;
778         } else {
779                 if (oidp->oid_refcnt == 0) {
780                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
781                                 oidp->oid_refcnt, oidp->oid_name);
782                         return (EINVAL);
783                 }
784                 sysctl_unregister_oid(oidp);
785                 if (del) {
786                         /*
787                          * Wait for all threads running the handler to drain.
788                          * This preserves the previous behavior when the
789                          * sysctl lock was held across a handler invocation,
790                          * and is necessary for module unload correctness.
791                          */
792                         while (oidp->oid_running > 0) {
793                                 oidp->oid_kind |= CTLFLAG_DYING;
794                                 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
795                         }
796                         if (oidp->oid_descr)
797                                 free(__DECONST(char *, oidp->oid_descr),
798                                     M_SYSCTLOID);
799                         if (oidp->oid_label)
800                                 free(__DECONST(char *, oidp->oid_label),
801                                     M_SYSCTLOID);
802                         free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
803                         free(oidp, M_SYSCTLOID);
804                 }
805         }
806         return (0);
807 }
808 /*
809  * Create new sysctls at run time.
810  * clist may point to a valid context initialized with sysctl_ctx_init().
811  */
812 struct sysctl_oid *
813 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
814         int number, const char *name, int kind, void *arg1, intmax_t arg2,
815         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
816         const char *label)
817 {
818         struct sysctl_oid *oidp;
819
820         /* You have to hook up somewhere.. */
821         if (parent == NULL)
822                 return(NULL);
823         /* Check if the node already exists, otherwise create it */
824         SYSCTL_WLOCK();
825         oidp = sysctl_find_oidname(name, parent);
826         if (oidp != NULL) {
827                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
828                         oidp->oid_refcnt++;
829                         /* Update the context */
830                         if (clist != NULL)
831                                 sysctl_ctx_entry_add(clist, oidp);
832                         SYSCTL_WUNLOCK();
833                         return (oidp);
834                 } else {
835                         sysctl_warn_reuse(__func__, oidp);
836                         SYSCTL_WUNLOCK();
837                         return (NULL);
838                 }
839         }
840         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
841         oidp->oid_parent = parent;
842         SLIST_INIT(&oidp->oid_children);
843         oidp->oid_number = number;
844         oidp->oid_refcnt = 1;
845         oidp->oid_name = strdup(name, M_SYSCTLOID);
846         oidp->oid_handler = handler;
847         oidp->oid_kind = CTLFLAG_DYN | kind;
848         oidp->oid_arg1 = arg1;
849         oidp->oid_arg2 = arg2;
850         oidp->oid_fmt = fmt;
851         if (descr != NULL)
852                 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
853         if (label != NULL)
854                 oidp->oid_label = strdup(label, M_SYSCTLOID);
855         /* Update the context, if used */
856         if (clist != NULL)
857                 sysctl_ctx_entry_add(clist, oidp);
858         /* Register this oid */
859         sysctl_register_oid(oidp);
860         SYSCTL_WUNLOCK();
861         return (oidp);
862 }
863
864 /*
865  * Rename an existing oid.
866  */
867 void
868 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
869 {
870         char *newname;
871         char *oldname;
872
873         newname = strdup(name, M_SYSCTLOID);
874         SYSCTL_WLOCK();
875         oldname = __DECONST(char *, oidp->oid_name);
876         oidp->oid_name = newname;
877         SYSCTL_WUNLOCK();
878         free(oldname, M_SYSCTLOID);
879 }
880
881 /*
882  * Reparent an existing oid.
883  */
884 int
885 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
886 {
887         struct sysctl_oid *oidp;
888
889         SYSCTL_WLOCK();
890         if (oid->oid_parent == parent) {
891                 SYSCTL_WUNLOCK();
892                 return (0);
893         }
894         oidp = sysctl_find_oidname(oid->oid_name, parent);
895         if (oidp != NULL) {
896                 SYSCTL_WUNLOCK();
897                 return (EEXIST);
898         }
899         sysctl_unregister_oid(oid);
900         oid->oid_parent = parent;
901         oid->oid_number = OID_AUTO;
902         sysctl_register_oid(oid);
903         SYSCTL_WUNLOCK();
904         return (0);
905 }
906
907 /*
908  * Register the kernel's oids on startup.
909  */
910 SET_DECLARE(sysctl_set, struct sysctl_oid);
911
912 static void
913 sysctl_register_all(void *arg)
914 {
915         struct sysctl_oid **oidp;
916
917         sx_init(&sysctlmemlock, "sysctl mem");
918         SYSCTL_INIT();
919         SYSCTL_WLOCK();
920         SET_FOREACH(oidp, sysctl_set)
921                 sysctl_register_oid(*oidp);
922         SYSCTL_WUNLOCK();
923 }
924 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
925
926 /*
927  * "Staff-functions"
928  *
929  * These functions implement a presently undocumented interface 
930  * used by the sysctl program to walk the tree, and get the type
931  * so it can print the value.
932  * This interface is under work and consideration, and should probably
933  * be killed with a big axe by the first person who can find the time.
934  * (be aware though, that the proper interface isn't as obvious as it
935  * may seem, there are various conflicting requirements.
936  *
937  * {0,0}        printf the entire MIB-tree.
938  * {0,1,...}    return the name of the "..." OID.
939  * {0,2,...}    return the next OID.
940  * {0,3}        return the OID of the name in "new"
941  * {0,4,...}    return the kind & format info for the "..." OID.
942  * {0,5,...}    return the description of the "..." OID.
943  * {0,6,...}    return the aggregation label of the "..." OID.
944  */
945
946 #ifdef SYSCTL_DEBUG
947 static void
948 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
949 {
950         int k;
951         struct sysctl_oid *oidp;
952
953         SYSCTL_ASSERT_LOCKED();
954         SLIST_FOREACH(oidp, l, oid_link) {
955
956                 for (k=0; k<i; k++)
957                         printf(" ");
958
959                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
960
961                 printf("%c%c",
962                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
963                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
964
965                 if (oidp->oid_handler)
966                         printf(" *Handler");
967
968                 switch (oidp->oid_kind & CTLTYPE) {
969                         case CTLTYPE_NODE:
970                                 printf(" Node\n");
971                                 if (!oidp->oid_handler) {
972                                         sysctl_sysctl_debug_dump_node(
973                                             SYSCTL_CHILDREN(oidp), i + 2);
974                                 }
975                                 break;
976                         case CTLTYPE_INT:    printf(" Int\n"); break;
977                         case CTLTYPE_UINT:   printf(" u_int\n"); break;
978                         case CTLTYPE_LONG:   printf(" Long\n"); break;
979                         case CTLTYPE_ULONG:  printf(" u_long\n"); break;
980                         case CTLTYPE_STRING: printf(" String\n"); break;
981                         case CTLTYPE_S8:     printf(" int8_t\n"); break;
982                         case CTLTYPE_S16:    printf(" int16_t\n"); break;
983                         case CTLTYPE_S32:    printf(" int32_t\n"); break;
984                         case CTLTYPE_S64:    printf(" int64_t\n"); break;
985                         case CTLTYPE_U8:     printf(" uint8_t\n"); break;
986                         case CTLTYPE_U16:    printf(" uint16_t\n"); break;
987                         case CTLTYPE_U32:    printf(" uint32_t\n"); break;
988                         case CTLTYPE_U64:    printf(" uint64_t\n"); break;
989                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
990                         default:             printf("\n");
991                 }
992
993         }
994 }
995
996 static int
997 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
998 {
999         struct rm_priotracker tracker;
1000         int error;
1001
1002         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1003         if (error)
1004                 return (error);
1005         SYSCTL_RLOCK(&tracker);
1006         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1007         SYSCTL_RUNLOCK(&tracker);
1008         return (ENOENT);
1009 }
1010
1011 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
1012         0, 0, sysctl_sysctl_debug, "-", "");
1013 #endif
1014
1015 static int
1016 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1017 {
1018         int *name = (int *) arg1;
1019         u_int namelen = arg2;
1020         int error = 0;
1021         struct sysctl_oid *oid;
1022         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1023         struct rm_priotracker tracker;
1024         char buf[10];
1025
1026         SYSCTL_RLOCK(&tracker);
1027         while (namelen) {
1028                 if (!lsp) {
1029                         snprintf(buf,sizeof(buf),"%d",*name);
1030                         if (req->oldidx)
1031                                 error = SYSCTL_OUT(req, ".", 1);
1032                         if (!error)
1033                                 error = SYSCTL_OUT(req, buf, strlen(buf));
1034                         if (error)
1035                                 goto out;
1036                         namelen--;
1037                         name++;
1038                         continue;
1039                 }
1040                 lsp2 = NULL;
1041                 SLIST_FOREACH(oid, lsp, oid_link) {
1042                         if (oid->oid_number != *name)
1043                                 continue;
1044
1045                         if (req->oldidx)
1046                                 error = SYSCTL_OUT(req, ".", 1);
1047                         if (!error)
1048                                 error = SYSCTL_OUT(req, oid->oid_name,
1049                                         strlen(oid->oid_name));
1050                         if (error)
1051                                 goto out;
1052
1053                         namelen--;
1054                         name++;
1055
1056                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
1057                                 break;
1058
1059                         if (oid->oid_handler)
1060                                 break;
1061
1062                         lsp2 = SYSCTL_CHILDREN(oid);
1063                         break;
1064                 }
1065                 lsp = lsp2;
1066         }
1067         error = SYSCTL_OUT(req, "", 1);
1068  out:
1069         SYSCTL_RUNLOCK(&tracker);
1070         return (error);
1071 }
1072
1073 /*
1074  * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1075  * capability mode.
1076  */
1077 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1078     sysctl_sysctl_name, "");
1079
1080 static int
1081 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
1082         int *next, int *len, int level, struct sysctl_oid **oidpp)
1083 {
1084         struct sysctl_oid *oidp;
1085
1086         SYSCTL_ASSERT_LOCKED();
1087         *len = level;
1088         SLIST_FOREACH(oidp, lsp, oid_link) {
1089                 *next = oidp->oid_number;
1090                 *oidpp = oidp;
1091
1092                 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1093                         continue;
1094
1095                 if (!namelen) {
1096                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
1097                                 return (0);
1098                         if (oidp->oid_handler) 
1099                                 /* We really should call the handler here...*/
1100                                 return (0);
1101                         lsp = SYSCTL_CHILDREN(oidp);
1102                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
1103                                 len, level+1, oidpp))
1104                                 return (0);
1105                         goto emptynode;
1106                 }
1107
1108                 if (oidp->oid_number < *name)
1109                         continue;
1110
1111                 if (oidp->oid_number > *name) {
1112                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1113                                 return (0);
1114                         if (oidp->oid_handler)
1115                                 return (0);
1116                         lsp = SYSCTL_CHILDREN(oidp);
1117                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
1118                                 next+1, len, level+1, oidpp))
1119                                 return (0);
1120                         goto next;
1121                 }
1122                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1123                         continue;
1124
1125                 if (oidp->oid_handler)
1126                         continue;
1127
1128                 lsp = SYSCTL_CHILDREN(oidp);
1129                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
1130                         len, level+1, oidpp))
1131                         return (0);
1132         next:
1133                 namelen = 1;
1134         emptynode:
1135                 *len = level;
1136         }
1137         return (1);
1138 }
1139
1140 static int
1141 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1142 {
1143         int *name = (int *) arg1;
1144         u_int namelen = arg2;
1145         int i, j, error;
1146         struct sysctl_oid *oid;
1147         struct sysctl_oid_list *lsp = &sysctl__children;
1148         struct rm_priotracker tracker;
1149         int newoid[CTL_MAXNAME];
1150
1151         SYSCTL_RLOCK(&tracker);
1152         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1153         SYSCTL_RUNLOCK(&tracker);
1154         if (i)
1155                 return (ENOENT);
1156         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1157         return (error);
1158 }
1159
1160 /*
1161  * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1162  * capability mode.
1163  */
1164 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1165     sysctl_sysctl_next, "");
1166
1167 static int
1168 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1169 {
1170         struct sysctl_oid *oidp;
1171         struct sysctl_oid_list *lsp = &sysctl__children;
1172         char *p;
1173
1174         SYSCTL_ASSERT_LOCKED();
1175
1176         for (*len = 0; *len < CTL_MAXNAME;) {
1177                 p = strsep(&name, ".");
1178
1179                 oidp = SLIST_FIRST(lsp);
1180                 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1181                         if (oidp == NULL)
1182                                 return (ENOENT);
1183                         if (strcmp(p, oidp->oid_name) == 0)
1184                                 break;
1185                 }
1186                 *oid++ = oidp->oid_number;
1187                 (*len)++;
1188
1189                 if (name == NULL || *name == '\0') {
1190                         if (oidpp)
1191                                 *oidpp = oidp;
1192                         return (0);
1193                 }
1194
1195                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1196                         break;
1197
1198                 if (oidp->oid_handler)
1199                         break;
1200
1201                 lsp = SYSCTL_CHILDREN(oidp);
1202         }
1203         return (ENOENT);
1204 }
1205
1206 static int
1207 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1208 {
1209         char *p;
1210         int error, oid[CTL_MAXNAME], len = 0;
1211         struct sysctl_oid *op = NULL;
1212         struct rm_priotracker tracker;
1213         char buf[32];
1214
1215         if (!req->newlen) 
1216                 return (ENOENT);
1217         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
1218                 return (ENAMETOOLONG);
1219
1220         p = buf;
1221         if (req->newlen >= sizeof(buf))
1222                 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1223
1224         error = SYSCTL_IN(req, p, req->newlen);
1225         if (error) {
1226                 if (p != buf)
1227                         free(p, M_SYSCTL);
1228                 return (error);
1229         }
1230
1231         p [req->newlen] = '\0';
1232
1233         SYSCTL_RLOCK(&tracker);
1234         error = name2oid(p, oid, &len, &op);
1235         SYSCTL_RUNLOCK(&tracker);
1236
1237         if (p != buf)
1238                 free(p, M_SYSCTL);
1239
1240         if (error)
1241                 return (error);
1242
1243         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1244         return (error);
1245 }
1246
1247 /*
1248  * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1249  * capability mode.
1250  */
1251 SYSCTL_PROC(_sysctl, 3, name2oid,
1252     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1253     | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1254
1255 static int
1256 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1257 {
1258         struct sysctl_oid *oid;
1259         struct rm_priotracker tracker;
1260         int error;
1261
1262         SYSCTL_RLOCK(&tracker);
1263         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1264         if (error)
1265                 goto out;
1266
1267         if (oid->oid_fmt == NULL) {
1268                 error = ENOENT;
1269                 goto out;
1270         }
1271         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1272         if (error)
1273                 goto out;
1274         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1275  out:
1276         SYSCTL_RUNLOCK(&tracker);
1277         return (error);
1278 }
1279
1280
1281 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1282     sysctl_sysctl_oidfmt, "");
1283
1284 static int
1285 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1286 {
1287         struct sysctl_oid *oid;
1288         struct rm_priotracker tracker;
1289         int error;
1290
1291         SYSCTL_RLOCK(&tracker);
1292         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1293         if (error)
1294                 goto out;
1295
1296         if (oid->oid_descr == NULL) {
1297                 error = ENOENT;
1298                 goto out;
1299         }
1300         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1301  out:
1302         SYSCTL_RUNLOCK(&tracker);
1303         return (error);
1304 }
1305
1306 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1307     sysctl_sysctl_oiddescr, "");
1308
1309 static int
1310 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1311 {
1312         struct sysctl_oid *oid;
1313         struct rm_priotracker tracker;
1314         int error;
1315
1316         SYSCTL_RLOCK(&tracker);
1317         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1318         if (error)
1319                 goto out;
1320
1321         if (oid->oid_label == NULL) {
1322                 error = ENOENT;
1323                 goto out;
1324         }
1325         error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1326  out:
1327         SYSCTL_RUNLOCK(&tracker);
1328         return (error);
1329 }
1330
1331 static SYSCTL_NODE(_sysctl, 6, oidlabel,
1332     CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1333
1334 /*
1335  * Default "handler" functions.
1336  */
1337
1338 /*
1339  * Handle a bool.
1340  * Two cases:
1341  *     a variable:  point arg1 at it.
1342  *     a constant:  pass it in arg2.
1343  */
1344
1345 int
1346 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1347 {
1348         uint8_t temp;
1349         int error;
1350
1351         /*
1352          * Attempt to get a coherent snapshot by making a copy of the data.
1353          */
1354         if (arg1)
1355                 temp = *(bool *)arg1 ? 1 : 0;
1356         else
1357                 temp = arg2 ? 1 : 0;
1358
1359         error = SYSCTL_OUT(req, &temp, sizeof(temp));
1360         if (error || !req->newptr)
1361                 return (error);
1362
1363         if (!arg1)
1364                 error = EPERM;
1365         else {
1366                 error = SYSCTL_IN(req, &temp, sizeof(temp));
1367                 if (!error)
1368                         *(bool *)arg1 = temp ? 1 : 0;
1369         }
1370         return (error);
1371 }
1372
1373 /*
1374  * Handle an int8_t, signed or unsigned.
1375  * Two cases:
1376  *     a variable:  point arg1 at it.
1377  *     a constant:  pass it in arg2.
1378  */
1379
1380 int
1381 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1382 {
1383         int8_t tmpout;
1384         int error = 0;
1385
1386         /*
1387          * Attempt to get a coherent snapshot by making a copy of the data.
1388          */
1389         if (arg1)
1390                 tmpout = *(int8_t *)arg1;
1391         else
1392                 tmpout = arg2;
1393         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1394
1395         if (error || !req->newptr)
1396                 return (error);
1397
1398         if (!arg1)
1399                 error = EPERM;
1400         else
1401                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1402         return (error);
1403 }
1404
1405 /*
1406  * Handle an int16_t, signed or unsigned.
1407  * Two cases:
1408  *     a variable:  point arg1 at it.
1409  *     a constant:  pass it in arg2.
1410  */
1411
1412 int
1413 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1414 {
1415         int16_t tmpout;
1416         int error = 0;
1417
1418         /*
1419          * Attempt to get a coherent snapshot by making a copy of the data.
1420          */
1421         if (arg1)
1422                 tmpout = *(int16_t *)arg1;
1423         else
1424                 tmpout = arg2;
1425         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1426
1427         if (error || !req->newptr)
1428                 return (error);
1429
1430         if (!arg1)
1431                 error = EPERM;
1432         else
1433                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1434         return (error);
1435 }
1436
1437 /*
1438  * Handle an int32_t, signed or unsigned.
1439  * Two cases:
1440  *     a variable:  point arg1 at it.
1441  *     a constant:  pass it in arg2.
1442  */
1443
1444 int
1445 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1446 {
1447         int32_t tmpout;
1448         int error = 0;
1449
1450         /*
1451          * Attempt to get a coherent snapshot by making a copy of the data.
1452          */
1453         if (arg1)
1454                 tmpout = *(int32_t *)arg1;
1455         else
1456                 tmpout = arg2;
1457         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1458
1459         if (error || !req->newptr)
1460                 return (error);
1461
1462         if (!arg1)
1463                 error = EPERM;
1464         else
1465                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1466         return (error);
1467 }
1468
1469 /*
1470  * Handle an int, signed or unsigned.
1471  * Two cases:
1472  *     a variable:  point arg1 at it.
1473  *     a constant:  pass it in arg2.
1474  */
1475
1476 int
1477 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1478 {
1479         int tmpout, error = 0;
1480
1481         /*
1482          * Attempt to get a coherent snapshot by making a copy of the data.
1483          */
1484         if (arg1)
1485                 tmpout = *(int *)arg1;
1486         else
1487                 tmpout = arg2;
1488         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1489
1490         if (error || !req->newptr)
1491                 return (error);
1492
1493         if (!arg1)
1494                 error = EPERM;
1495         else
1496                 error = SYSCTL_IN(req, arg1, sizeof(int));
1497         return (error);
1498 }
1499
1500 /*
1501  * Based on on sysctl_handle_int() convert milliseconds into ticks.
1502  * Note: this is used by TCP.
1503  */
1504
1505 int
1506 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1507 {
1508         int error, s, tt;
1509
1510         tt = *(int *)arg1;
1511         s = (int)((int64_t)tt * 1000 / hz);
1512
1513         error = sysctl_handle_int(oidp, &s, 0, req);
1514         if (error || !req->newptr)
1515                 return (error);
1516
1517         tt = (int)((int64_t)s * hz / 1000);
1518         if (tt < 1)
1519                 return (EINVAL);
1520
1521         *(int *)arg1 = tt;
1522         return (0);
1523 }
1524
1525
1526 /*
1527  * Handle a long, signed or unsigned.
1528  * Two cases:
1529  *     a variable:  point arg1 at it.
1530  *     a constant:  pass it in arg2.
1531  */
1532
1533 int
1534 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1535 {
1536         int error = 0;
1537         long tmplong;
1538 #ifdef SCTL_MASK32
1539         int tmpint;
1540 #endif
1541
1542         /*
1543          * Attempt to get a coherent snapshot by making a copy of the data.
1544          */
1545         if (arg1)
1546                 tmplong = *(long *)arg1;
1547         else
1548                 tmplong = arg2;
1549 #ifdef SCTL_MASK32
1550         if (req->flags & SCTL_MASK32) {
1551                 tmpint = tmplong;
1552                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1553         } else
1554 #endif
1555                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1556
1557         if (error || !req->newptr)
1558                 return (error);
1559
1560         if (!arg1)
1561                 error = EPERM;
1562 #ifdef SCTL_MASK32
1563         else if (req->flags & SCTL_MASK32) {
1564                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1565                 *(long *)arg1 = (long)tmpint;
1566         }
1567 #endif
1568         else
1569                 error = SYSCTL_IN(req, arg1, sizeof(long));
1570         return (error);
1571 }
1572
1573 /*
1574  * Handle a 64 bit int, signed or unsigned.
1575  * Two cases:
1576  *     a variable:  point arg1 at it.
1577  *     a constant:  pass it in arg2.
1578  */
1579 int
1580 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1581 {
1582         int error = 0;
1583         uint64_t tmpout;
1584
1585         /*
1586          * Attempt to get a coherent snapshot by making a copy of the data.
1587          */
1588         if (arg1)
1589                 tmpout = *(uint64_t *)arg1;
1590         else
1591                 tmpout = arg2;
1592         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1593
1594         if (error || !req->newptr)
1595                 return (error);
1596
1597         if (!arg1)
1598                 error = EPERM;
1599         else
1600                 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1601         return (error);
1602 }
1603
1604 /*
1605  * Handle our generic '\0' terminated 'C' string.
1606  * Two cases:
1607  *      a variable string:  point arg1 at it, arg2 is max length.
1608  *      a constant string:  point arg1 at it, arg2 is zero.
1609  */
1610
1611 int
1612 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1613 {
1614         size_t outlen;
1615         int error = 0, ro_string = 0;
1616
1617         /*
1618          * A zero-length buffer indicates a fixed size read-only
1619          * string:
1620          */
1621         if (arg2 == 0) {
1622                 arg2 = strlen((char *)arg1) + 1;
1623                 ro_string = 1;
1624         }
1625
1626         if (req->oldptr != NULL) {
1627                 char *tmparg;
1628
1629                 if (ro_string) {
1630                         tmparg = arg1;
1631                 } else {
1632                         /* try to make a coherent snapshot of the string */
1633                         tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1634                         memcpy(tmparg, arg1, arg2);
1635                 }
1636
1637                 outlen = strnlen(tmparg, arg2 - 1) + 1;
1638                 error = SYSCTL_OUT(req, tmparg, outlen);
1639
1640                 if (!ro_string)
1641                         free(tmparg, M_SYSCTLTMP);
1642         } else {
1643                 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1644                 error = SYSCTL_OUT(req, NULL, outlen);
1645         }
1646         if (error || !req->newptr)
1647                 return (error);
1648
1649         if ((req->newlen - req->newidx) >= arg2) {
1650                 error = EINVAL;
1651         } else {
1652                 arg2 = (req->newlen - req->newidx);
1653                 error = SYSCTL_IN(req, arg1, arg2);
1654                 ((char *)arg1)[arg2] = '\0';
1655         }
1656         return (error);
1657 }
1658
1659 /*
1660  * Handle any kind of opaque data.
1661  * arg1 points to it, arg2 is the size.
1662  */
1663
1664 int
1665 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1666 {
1667         int error, tries;
1668         u_int generation;
1669         struct sysctl_req req2;
1670
1671         /*
1672          * Attempt to get a coherent snapshot, by using the thread
1673          * pre-emption counter updated from within mi_switch() to
1674          * determine if we were pre-empted during a bcopy() or
1675          * copyout(). Make 3 attempts at doing this before giving up.
1676          * If we encounter an error, stop immediately.
1677          */
1678         tries = 0;
1679         req2 = *req;
1680 retry:
1681         generation = curthread->td_generation;
1682         error = SYSCTL_OUT(req, arg1, arg2);
1683         if (error)
1684                 return (error);
1685         tries++;
1686         if (generation != curthread->td_generation && tries < 3) {
1687                 *req = req2;
1688                 goto retry;
1689         }
1690
1691         error = SYSCTL_IN(req, arg1, arg2);
1692
1693         return (error);
1694 }
1695
1696 /*
1697  * Transfer functions to/from kernel space.
1698  * XXX: rather untested at this point
1699  */
1700 static int
1701 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1702 {
1703         size_t i = 0;
1704
1705         if (req->oldptr) {
1706                 i = l;
1707                 if (req->oldlen <= req->oldidx)
1708                         i = 0;
1709                 else
1710                         if (i > req->oldlen - req->oldidx)
1711                                 i = req->oldlen - req->oldidx;
1712                 if (i > 0)
1713                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
1714         }
1715         req->oldidx += l;
1716         if (req->oldptr && i != l)
1717                 return (ENOMEM);
1718         return (0);
1719 }
1720
1721 static int
1722 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1723 {
1724         if (!req->newptr)
1725                 return (0);
1726         if (req->newlen - req->newidx < l)
1727                 return (EINVAL);
1728         bcopy((char *)req->newptr + req->newidx, p, l);
1729         req->newidx += l;
1730         return (0);
1731 }
1732
1733 int
1734 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1735     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1736 {
1737         int error = 0;
1738         struct sysctl_req req;
1739
1740         bzero(&req, sizeof req);
1741
1742         req.td = td;
1743         req.flags = flags;
1744
1745         if (oldlenp) {
1746                 req.oldlen = *oldlenp;
1747         }
1748         req.validlen = req.oldlen;
1749
1750         if (old) {
1751                 req.oldptr= old;
1752         }
1753
1754         if (new != NULL) {
1755                 req.newlen = newlen;
1756                 req.newptr = new;
1757         }
1758
1759         req.oldfunc = sysctl_old_kernel;
1760         req.newfunc = sysctl_new_kernel;
1761         req.lock = REQ_UNWIRED;
1762
1763         error = sysctl_root(0, name, namelen, &req);
1764
1765         if (req.lock == REQ_WIRED && req.validlen > 0)
1766                 vsunlock(req.oldptr, req.validlen);
1767
1768         if (error && error != ENOMEM)
1769                 return (error);
1770
1771         if (retval) {
1772                 if (req.oldptr && req.oldidx > req.validlen)
1773                         *retval = req.validlen;
1774                 else
1775                         *retval = req.oldidx;
1776         }
1777         return (error);
1778 }
1779
1780 int
1781 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1782     void *new, size_t newlen, size_t *retval, int flags)
1783 {
1784         int oid[CTL_MAXNAME];
1785         size_t oidlen, plen;
1786         int error;
1787
1788         oid[0] = 0;             /* sysctl internal magic */
1789         oid[1] = 3;             /* name2oid */
1790         oidlen = sizeof(oid);
1791
1792         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1793             (void *)name, strlen(name), &plen, flags);
1794         if (error)
1795                 return (error);
1796
1797         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1798             new, newlen, retval, flags);
1799         return (error);
1800 }
1801
1802 /*
1803  * Transfer function to/from user space.
1804  */
1805 static int
1806 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1807 {
1808         size_t i, len, origidx;
1809         int error;
1810
1811         origidx = req->oldidx;
1812         req->oldidx += l;
1813         if (req->oldptr == NULL)
1814                 return (0);
1815         /*
1816          * If we have not wired the user supplied buffer and we are currently
1817          * holding locks, drop a witness warning, as it's possible that
1818          * write operations to the user page can sleep.
1819          */
1820         if (req->lock != REQ_WIRED)
1821                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1822                     "sysctl_old_user()");
1823         i = l;
1824         len = req->validlen;
1825         if (len <= origidx)
1826                 i = 0;
1827         else {
1828                 if (i > len - origidx)
1829                         i = len - origidx;
1830                 if (req->lock == REQ_WIRED) {
1831                         error = copyout_nofault(p, (char *)req->oldptr +
1832                             origidx, i);
1833                 } else
1834                         error = copyout(p, (char *)req->oldptr + origidx, i);
1835                 if (error != 0)
1836                         return (error);
1837         }
1838         if (i < l)
1839                 return (ENOMEM);
1840         return (0);
1841 }
1842
1843 static int
1844 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1845 {
1846         int error;
1847
1848         if (!req->newptr)
1849                 return (0);
1850         if (req->newlen - req->newidx < l)
1851                 return (EINVAL);
1852         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1853             "sysctl_new_user()");
1854         error = copyin((char *)req->newptr + req->newidx, p, l);
1855         req->newidx += l;
1856         return (error);
1857 }
1858
1859 /*
1860  * Wire the user space destination buffer.  If set to a value greater than
1861  * zero, the len parameter limits the maximum amount of wired memory.
1862  */
1863 int
1864 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1865 {
1866         int ret;
1867         size_t wiredlen;
1868
1869         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1870         ret = 0;
1871         if (req->lock != REQ_WIRED && req->oldptr &&
1872             req->oldfunc == sysctl_old_user) {
1873                 if (wiredlen != 0) {
1874                         ret = vslock(req->oldptr, wiredlen);
1875                         if (ret != 0) {
1876                                 if (ret != ENOMEM)
1877                                         return (ret);
1878                                 wiredlen = 0;
1879                         }
1880                 }
1881                 req->lock = REQ_WIRED;
1882                 req->validlen = wiredlen;
1883         }
1884         return (0);
1885 }
1886
1887 int
1888 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1889     int *nindx, struct sysctl_req *req)
1890 {
1891         struct sysctl_oid_list *lsp;
1892         struct sysctl_oid *oid;
1893         int indx;
1894
1895         SYSCTL_ASSERT_LOCKED();
1896         lsp = &sysctl__children;
1897         indx = 0;
1898         while (indx < CTL_MAXNAME) {
1899                 SLIST_FOREACH(oid, lsp, oid_link) {
1900                         if (oid->oid_number == name[indx])
1901                                 break;
1902                 }
1903                 if (oid == NULL)
1904                         return (ENOENT);
1905
1906                 indx++;
1907                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1908                         if (oid->oid_handler != NULL || indx == namelen) {
1909                                 *noid = oid;
1910                                 if (nindx != NULL)
1911                                         *nindx = indx;
1912                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1913                                     ("%s found DYING node %p", __func__, oid));
1914                                 return (0);
1915                         }
1916                         lsp = SYSCTL_CHILDREN(oid);
1917                 } else if (indx == namelen) {
1918                         if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
1919                                 return (ENOENT);
1920                         *noid = oid;
1921                         if (nindx != NULL)
1922                                 *nindx = indx;
1923                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1924                             ("%s found DYING node %p", __func__, oid));
1925                         return (0);
1926                 } else {
1927                         return (ENOTDIR);
1928                 }
1929         }
1930         return (ENOENT);
1931 }
1932
1933 /*
1934  * Traverse our tree, and find the right node, execute whatever it points
1935  * to, and return the resulting error code.
1936  */
1937
1938 static int
1939 sysctl_root(SYSCTL_HANDLER_ARGS)
1940 {
1941         struct sysctl_oid *oid;
1942         struct rm_priotracker tracker;
1943         int error, indx, lvl;
1944
1945         SYSCTL_RLOCK(&tracker);
1946
1947         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1948         if (error)
1949                 goto out;
1950
1951         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1952                 /*
1953                  * You can't call a sysctl when it's a node, but has
1954                  * no handler.  Inform the user that it's a node.
1955                  * The indx may or may not be the same as namelen.
1956                  */
1957                 if (oid->oid_handler == NULL) {
1958                         error = EISDIR;
1959                         goto out;
1960                 }
1961         }
1962
1963         /* Is this sysctl writable? */
1964         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1965                 error = EPERM;
1966                 goto out;
1967         }
1968
1969         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1970
1971 #ifdef CAPABILITY_MODE
1972         /*
1973          * If the process is in capability mode, then don't permit reading or
1974          * writing unless specifically granted for the node.
1975          */
1976         if (IN_CAPABILITY_MODE(req->td)) {
1977                 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
1978                     (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
1979                         error = EPERM;
1980                         goto out;
1981                 }
1982         }
1983 #endif
1984
1985         /* Is this sysctl sensitive to securelevels? */
1986         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1987                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1988                 error = securelevel_gt(req->td->td_ucred, lvl);
1989                 if (error)
1990                         goto out;
1991         }
1992
1993         /* Is this sysctl writable by only privileged users? */
1994         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1995                 int priv;
1996
1997                 if (oid->oid_kind & CTLFLAG_PRISON)
1998                         priv = PRIV_SYSCTL_WRITEJAIL;
1999 #ifdef VIMAGE
2000                 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2001                      prison_owns_vnet(req->td->td_ucred))
2002                         priv = PRIV_SYSCTL_WRITEJAIL;
2003 #endif
2004                 else
2005                         priv = PRIV_SYSCTL_WRITE;
2006                 error = priv_check(req->td, priv);
2007                 if (error)
2008                         goto out;
2009         }
2010
2011         if (!oid->oid_handler) {
2012                 error = EINVAL;
2013                 goto out;
2014         }
2015
2016         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2017                 arg1 = (int *)arg1 + indx;
2018                 arg2 -= indx;
2019         } else {
2020                 arg1 = oid->oid_arg1;
2021                 arg2 = oid->oid_arg2;
2022         }
2023 #ifdef MAC
2024         error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2025             req);
2026         if (error != 0)
2027                 goto out;
2028 #endif
2029 #ifdef VIMAGE
2030         if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2031                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2032 #endif
2033         error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2034
2035 out:
2036         SYSCTL_RUNLOCK(&tracker);
2037         return (error);
2038 }
2039
2040 #ifndef _SYS_SYSPROTO_H_
2041 struct sysctl_args {
2042         int     *name;
2043         u_int   namelen;
2044         void    *old;
2045         size_t  *oldlenp;
2046         void    *new;
2047         size_t  newlen;
2048 };
2049 #endif
2050 int
2051 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2052 {
2053         int error, i, name[CTL_MAXNAME];
2054         size_t j;
2055
2056         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2057                 return (EINVAL);
2058
2059         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2060         if (error)
2061                 return (error);
2062
2063         error = userland_sysctl(td, name, uap->namelen,
2064                 uap->old, uap->oldlenp, 0,
2065                 uap->new, uap->newlen, &j, 0);
2066         if (error && error != ENOMEM)
2067                 return (error);
2068         if (uap->oldlenp) {
2069                 i = copyout(&j, uap->oldlenp, sizeof(j));
2070                 if (i)
2071                         return (i);
2072         }
2073         return (error);
2074 }
2075
2076 /*
2077  * This is used from various compatibility syscalls too.  That's why name
2078  * must be in kernel space.
2079  */
2080 int
2081 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2082     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
2083     int flags)
2084 {
2085         int error = 0, memlocked;
2086         struct sysctl_req req;
2087
2088         bzero(&req, sizeof req);
2089
2090         req.td = td;
2091         req.flags = flags;
2092
2093         if (oldlenp) {
2094                 if (inkernel) {
2095                         req.oldlen = *oldlenp;
2096                 } else {
2097                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2098                         if (error)
2099                                 return (error);
2100                 }
2101         }
2102         req.validlen = req.oldlen;
2103         req.oldptr = old;
2104
2105         if (new != NULL) {
2106                 req.newlen = newlen;
2107                 req.newptr = new;
2108         }
2109
2110         req.oldfunc = sysctl_old_user;
2111         req.newfunc = sysctl_new_user;
2112         req.lock = REQ_UNWIRED;
2113
2114 #ifdef KTRACE
2115         if (KTRPOINT(curthread, KTR_SYSCTL))
2116                 ktrsysctl(name, namelen);
2117 #endif
2118         memlocked = 0;
2119         if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2120                 memlocked = 1;
2121                 sx_xlock(&sysctlmemlock);
2122         }
2123         CURVNET_SET(TD_TO_VNET(td));
2124
2125         for (;;) {
2126                 req.oldidx = 0;
2127                 req.newidx = 0;
2128                 error = sysctl_root(0, name, namelen, &req);
2129                 if (error != EAGAIN)
2130                         break;
2131                 kern_yield(PRI_USER);
2132         }
2133
2134         CURVNET_RESTORE();
2135
2136         if (req.lock == REQ_WIRED && req.validlen > 0)
2137                 vsunlock(req.oldptr, req.validlen);
2138         if (memlocked)
2139                 sx_xunlock(&sysctlmemlock);
2140
2141         if (error && error != ENOMEM)
2142                 return (error);
2143
2144         if (retval) {
2145                 if (req.oldptr && req.oldidx > req.validlen)
2146                         *retval = req.validlen;
2147                 else
2148                         *retval = req.oldidx;
2149         }
2150         return (error);
2151 }
2152
2153 /*
2154  * Drain into a sysctl struct.  The user buffer should be wired if a page
2155  * fault would cause issue.
2156  */
2157 static int
2158 sbuf_sysctl_drain(void *arg, const char *data, int len)
2159 {
2160         struct sysctl_req *req = arg;
2161         int error;
2162
2163         error = SYSCTL_OUT(req, data, len);
2164         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2165         return (error == 0 ? len : -error);
2166 }
2167
2168 struct sbuf *
2169 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2170     struct sysctl_req *req)
2171 {
2172
2173         /* Supply a default buffer size if none given. */
2174         if (buf == NULL && length == 0)
2175                 length = 64;
2176         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2177         sbuf_set_drain(s, sbuf_sysctl_drain, req);
2178         return (s);
2179 }