]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_sysctl.c
Remove duplicated empty lines from kern/*.c
[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_ddb.h"
45 #include "opt_ktrace.h"
46 #include "opt_sysctl.h"
47
48 #include <sys/param.h>
49 #include <sys/fail.h>
50 #include <sys/systm.h>
51 #include <sys/capsicum.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/sysctl.h>
55 #include <sys/malloc.h>
56 #include <sys/priv.h>
57 #include <sys/proc.h>
58 #include <sys/jail.h>
59 #include <sys/kdb.h>
60 #include <sys/lock.h>
61 #include <sys/mutex.h>
62 #include <sys/rmlock.h>
63 #include <sys/sbuf.h>
64 #include <sys/sx.h>
65 #include <sys/sysproto.h>
66 #include <sys/uio.h>
67 #ifdef KTRACE
68 #include <sys/ktrace.h>
69 #endif
70
71 #ifdef DDB
72 #include <ddb/ddb.h>
73 #include <ddb/db_lex.h>
74 #endif
75
76 #include <net/vnet.h>
77
78 #include <security/mac/mac_framework.h>
79
80 #include <vm/vm.h>
81 #include <vm/vm_extern.h>
82
83 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
84 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
85 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
86
87 /*
88  * The sysctllock protects the MIB tree.  It also protects sysctl
89  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
90  * sysctl_unregister_oid() routines require the sysctllock to already
91  * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
92  * provided for the few places in the kernel which need to use that
93  * API rather than using the dynamic API.  Use of the dynamic API is
94  * strongly encouraged for most code.
95  *
96  * The sysctlmemlock is used to limit the amount of user memory wired for
97  * sysctl requests.  This is implemented by serializing any userland
98  * sysctl requests larger than a single page via an exclusive lock.
99  */
100 static struct rmlock sysctllock;
101 static struct sx __exclusive_cache_line sysctlmemlock;
102
103 #define SYSCTL_WLOCK()          rm_wlock(&sysctllock)
104 #define SYSCTL_WUNLOCK()        rm_wunlock(&sysctllock)
105 #define SYSCTL_RLOCK(tracker)   rm_rlock(&sysctllock, (tracker))
106 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
107 #define SYSCTL_WLOCKED()        rm_wowned(&sysctllock)
108 #define SYSCTL_ASSERT_LOCKED()  rm_assert(&sysctllock, RA_LOCKED)
109 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
110 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
111 #define SYSCTL_INIT()           rm_init_flags(&sysctllock, "sysctl lock", \
112                                     RM_SLEEPABLE)
113 #define SYSCTL_SLEEP(ch, wmesg, timo)                                   \
114                                 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
115
116 static int sysctl_root(SYSCTL_HANDLER_ARGS);
117
118 /* Root list */
119 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
120
121 static int      sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
122                     int recurse);
123 static int      sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
124 static int      sysctl_new_kernel(struct sysctl_req *, void *, size_t);
125
126 static struct sysctl_oid *
127 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
128 {
129         struct sysctl_oid *oidp;
130
131         SYSCTL_ASSERT_LOCKED();
132         SLIST_FOREACH(oidp, list, oid_link) {
133                 if (strcmp(oidp->oid_name, name) == 0) {
134                         return (oidp);
135                 }
136         }
137         return (NULL);
138 }
139
140 /*
141  * Initialization of the MIB tree.
142  *
143  * Order by number in each list.
144  */
145 void
146 sysctl_wlock(void)
147 {
148
149         SYSCTL_WLOCK();
150 }
151
152 void
153 sysctl_wunlock(void)
154 {
155
156         SYSCTL_WUNLOCK();
157 }
158
159 static int
160 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
161     struct sysctl_req *req, struct rm_priotracker *tracker)
162 {
163         int error;
164
165         if (oid->oid_kind & CTLFLAG_DYN)
166                 atomic_add_int(&oid->oid_running, 1);
167
168         if (tracker != NULL)
169                 SYSCTL_RUNLOCK(tracker);
170         else
171                 SYSCTL_WUNLOCK();
172
173         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
174                 mtx_lock(&Giant);
175         error = oid->oid_handler(oid, arg1, arg2, req);
176         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
177                 mtx_unlock(&Giant);
178
179         KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
180
181         if (tracker != NULL)
182                 SYSCTL_RLOCK(tracker);
183         else
184                 SYSCTL_WLOCK();
185
186         if (oid->oid_kind & CTLFLAG_DYN) {
187                 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
188                     (oid->oid_kind & CTLFLAG_DYING) != 0)
189                         wakeup(&oid->oid_running);
190         }
191
192         return (error);
193 }
194
195 static void
196 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
197 {
198         struct sysctl_req req;
199         struct sysctl_oid *curr;
200         char *penv = NULL;
201         char path[96];
202         ssize_t rem = sizeof(path);
203         ssize_t len;
204         uint8_t data[512] __aligned(sizeof(uint64_t));
205         int size;
206         int error;
207
208         path[--rem] = 0;
209
210         for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
211                 len = strlen(curr->oid_name);
212                 rem -= len;
213                 if (curr != oidp)
214                         rem -= 1;
215                 if (rem < 0) {
216                         printf("OID path exceeds %d bytes\n", (int)sizeof(path));
217                         return;
218                 }
219                 memcpy(path + rem, curr->oid_name, len);
220                 if (curr != oidp)
221                         path[rem + len] = '.';
222         }
223
224         memset(&req, 0, sizeof(req));
225
226         req.td = curthread;
227         req.oldfunc = sysctl_old_kernel;
228         req.newfunc = sysctl_new_kernel;
229         req.lock = REQ_UNWIRED;
230
231         switch (oidp->oid_kind & CTLTYPE) {
232         case CTLTYPE_INT:
233                 if (getenv_array(path + rem, data, sizeof(data), &size,
234                     sizeof(int), GETENV_SIGNED) == 0)
235                         return;
236                 req.newlen = size;
237                 req.newptr = data;
238                 break;
239         case CTLTYPE_UINT:
240                 if (getenv_array(path + rem, data, sizeof(data), &size,
241                     sizeof(int), GETENV_UNSIGNED) == 0)
242                         return;
243                 req.newlen = size;
244                 req.newptr = data;
245                 break;
246         case CTLTYPE_LONG:
247                 if (getenv_array(path + rem, data, sizeof(data), &size,
248                     sizeof(long), GETENV_SIGNED) == 0)
249                         return;
250                 req.newlen = size;
251                 req.newptr = data;
252                 break;
253         case CTLTYPE_ULONG:
254                 if (getenv_array(path + rem, data, sizeof(data), &size,
255                     sizeof(long), GETENV_UNSIGNED) == 0)
256                         return;
257                 req.newlen = size;
258                 req.newptr = data;
259                 break;
260         case CTLTYPE_S8:
261                 if (getenv_array(path + rem, data, sizeof(data), &size,
262                     sizeof(int8_t), GETENV_SIGNED) == 0)
263                         return;
264                 req.newlen = size;
265                 req.newptr = data;
266                 break;
267         case CTLTYPE_S16:
268                 if (getenv_array(path + rem, data, sizeof(data), &size,
269                     sizeof(int16_t), GETENV_SIGNED) == 0)
270                         return;
271                 req.newlen = size;
272                 req.newptr = data;
273                 break;
274         case CTLTYPE_S32:
275                 if (getenv_array(path + rem, data, sizeof(data), &size,
276                     sizeof(int32_t), GETENV_SIGNED) == 0)
277                         return;
278                 req.newlen = size;
279                 req.newptr = data;
280                 break;
281         case CTLTYPE_S64:
282                 if (getenv_array(path + rem, data, sizeof(data), &size,
283                     sizeof(int64_t), GETENV_SIGNED) == 0)
284                         return;
285                 req.newlen = size;
286                 req.newptr = data;
287                 break;
288         case CTLTYPE_U8:
289                 if (getenv_array(path + rem, data, sizeof(data), &size,
290                     sizeof(uint8_t), GETENV_UNSIGNED) == 0)
291                         return;
292                 req.newlen = size;
293                 req.newptr = data;
294                 break;
295         case CTLTYPE_U16:
296                 if (getenv_array(path + rem, data, sizeof(data), &size,
297                     sizeof(uint16_t), GETENV_UNSIGNED) == 0)
298                         return;
299                 req.newlen = size;
300                 req.newptr = data;
301                 break;
302         case CTLTYPE_U32:
303                 if (getenv_array(path + rem, data, sizeof(data), &size,
304                     sizeof(uint32_t), GETENV_UNSIGNED) == 0)
305                         return;
306                 req.newlen = size;
307                 req.newptr = data;
308                 break;
309         case CTLTYPE_U64:
310                 if (getenv_array(path + rem, data, sizeof(data), &size,
311                     sizeof(uint64_t), GETENV_UNSIGNED) == 0)
312                         return;
313                 req.newlen = size;
314                 req.newptr = data;
315                 break;
316         case CTLTYPE_STRING:
317                 penv = kern_getenv(path + rem);
318                 if (penv == NULL)
319                         return;
320                 req.newlen = strlen(penv);
321                 req.newptr = penv;
322                 break;
323         default:
324                 return;
325         }
326         error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
327             oidp->oid_arg2, &req, NULL);
328         if (error != 0)
329                 printf("Setting sysctl %s failed: %d\n", path + rem, error);
330         if (penv != NULL)
331                 freeenv(penv);
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, OID_AUTO, reuse_test,
409     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-",
410     "");
411 #endif
412
413 void
414 sysctl_register_oid(struct sysctl_oid *oidp)
415 {
416         struct sysctl_oid_list *parent = oidp->oid_parent;
417         struct sysctl_oid *p;
418         struct sysctl_oid *q;
419         int oid_number;
420         int timeout = 2;
421
422         /*
423          * First check if another oid with the same name already
424          * exists in the parent's list.
425          */
426         SYSCTL_ASSERT_WLOCKED();
427         p = sysctl_find_oidname(oidp->oid_name, parent);
428         if (p != NULL) {
429                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
430                         p->oid_refcnt++;
431                         return;
432                 } else {
433                         sysctl_warn_reuse(__func__, p);
434                         return;
435                 }
436         }
437         /* get current OID number */
438         oid_number = oidp->oid_number;
439
440 #if (OID_AUTO >= 0)
441 #error "OID_AUTO is expected to be a negative value"
442 #endif  
443         /*
444          * Any negative OID number qualifies as OID_AUTO. Valid OID
445          * numbers should always be positive.
446          *
447          * NOTE: DO NOT change the starting value here, change it in
448          * <sys/sysctl.h>, and make sure it is at least 256 to
449          * accommodate e.g. net.inet.raw as a static sysctl node.
450          */
451         if (oid_number < 0) {
452                 static int newoid;
453
454                 /*
455                  * By decrementing the next OID number we spend less
456                  * time inserting the OIDs into a sorted list.
457                  */
458                 if (--newoid < CTL_AUTO_START)
459                         newoid = 0x7fffffff;
460
461                 oid_number = newoid;
462         }
463
464         /*
465          * Insert the OID into the parent's list sorted by OID number.
466          */
467 retry:
468         q = NULL;
469         SLIST_FOREACH(p, parent, oid_link) {
470                 /* check if the current OID number is in use */
471                 if (oid_number == p->oid_number) {
472                         /* get the next valid OID number */
473                         if (oid_number < CTL_AUTO_START ||
474                             oid_number == 0x7fffffff) {
475                                 /* wraparound - restart */
476                                 oid_number = CTL_AUTO_START;
477                                 /* don't loop forever */
478                                 if (!timeout--)
479                                         panic("sysctl: Out of OID numbers\n");
480                                 goto retry;
481                         } else {
482                                 oid_number++;
483                         }
484                 } else if (oid_number < p->oid_number)
485                         break;
486                 q = p;
487         }
488         /* check for non-auto OID number collision */
489         if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
490             oid_number >= CTL_AUTO_START) {
491                 printf("sysctl: OID number(%d) is already in use for '%s'\n",
492                     oidp->oid_number, oidp->oid_name);
493         }
494         /* update the OID number, if any */
495         oidp->oid_number = oid_number;
496         if (q != NULL)
497                 SLIST_INSERT_AFTER(q, oidp, oid_link);
498         else
499                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
500
501         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
502 #ifdef VIMAGE
503             (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
504 #endif
505             (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
506             (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
507                 /* only fetch value once */
508                 oidp->oid_kind |= CTLFLAG_NOFETCH;
509                 /* try to fetch value from kernel environment */
510                 sysctl_load_tunable_by_oid_locked(oidp);
511         }
512 }
513
514 void
515 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
516 {
517
518         /*
519          * Mark the leaf as dormant if it's not to be immediately enabled.
520          * We do not disable nodes as they can be shared between modules
521          * and it is always safe to access a node.
522          */
523         KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
524             ("internal flag is set in oid_kind"));
525         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
526                 oidp->oid_kind |= CTLFLAG_DORMANT;
527         sysctl_register_oid(oidp);
528 }
529
530 void
531 sysctl_enable_oid(struct sysctl_oid *oidp)
532 {
533
534         SYSCTL_ASSERT_WLOCKED();
535         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
536                 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
537                     ("sysctl node is marked as dormant"));
538                 return;
539         }
540         KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
541             ("enabling already enabled sysctl oid"));
542         oidp->oid_kind &= ~CTLFLAG_DORMANT;
543 }
544
545 void
546 sysctl_unregister_oid(struct sysctl_oid *oidp)
547 {
548         struct sysctl_oid *p;
549         int error;
550
551         SYSCTL_ASSERT_WLOCKED();
552         if (oidp->oid_number == OID_AUTO) {
553                 error = EINVAL;
554         } else {
555                 error = ENOENT;
556                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
557                         if (p == oidp) {
558                                 SLIST_REMOVE(oidp->oid_parent, oidp,
559                                     sysctl_oid, oid_link);
560                                 error = 0;
561                                 break;
562                         }
563                 }
564         }
565
566         /* 
567          * This can happen when a module fails to register and is
568          * being unloaded afterwards.  It should not be a panic()
569          * for normal use.
570          */
571         if (error) {
572                 printf("%s: failed(%d) to unregister sysctl(%s)\n",
573                     __func__, error, oidp->oid_name);
574         }
575 }
576
577 /* Initialize a new context to keep track of dynamically added sysctls. */
578 int
579 sysctl_ctx_init(struct sysctl_ctx_list *c)
580 {
581
582         if (c == NULL) {
583                 return (EINVAL);
584         }
585
586         /*
587          * No locking here, the caller is responsible for not adding
588          * new nodes to a context until after this function has
589          * returned.
590          */
591         TAILQ_INIT(c);
592         return (0);
593 }
594
595 /* Free the context, and destroy all dynamic oids registered in this context */
596 int
597 sysctl_ctx_free(struct sysctl_ctx_list *clist)
598 {
599         struct sysctl_ctx_entry *e, *e1;
600         int error;
601
602         error = 0;
603         /*
604          * First perform a "dry run" to check if it's ok to remove oids.
605          * XXX FIXME
606          * XXX This algorithm is a hack. But I don't know any
607          * XXX better solution for now...
608          */
609         SYSCTL_WLOCK();
610         TAILQ_FOREACH(e, clist, link) {
611                 error = sysctl_remove_oid_locked(e->entry, 0, 0);
612                 if (error)
613                         break;
614         }
615         /*
616          * Restore deregistered entries, either from the end,
617          * or from the place where error occurred.
618          * e contains the entry that was not unregistered
619          */
620         if (error)
621                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
622         else
623                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
624         while (e1 != NULL) {
625                 sysctl_register_oid(e1->entry);
626                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
627         }
628         if (error) {
629                 SYSCTL_WUNLOCK();
630                 return(EBUSY);
631         }
632         /* Now really delete the entries */
633         e = TAILQ_FIRST(clist);
634         while (e != NULL) {
635                 e1 = TAILQ_NEXT(e, link);
636                 error = sysctl_remove_oid_locked(e->entry, 1, 0);
637                 if (error)
638                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
639                             e->entry->oid_name);
640                 free(e, M_SYSCTLOID);
641                 e = e1;
642         }
643         SYSCTL_WUNLOCK();
644         return (error);
645 }
646
647 /* Add an entry to the context */
648 struct sysctl_ctx_entry *
649 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
650 {
651         struct sysctl_ctx_entry *e;
652
653         SYSCTL_ASSERT_WLOCKED();
654         if (clist == NULL || oidp == NULL)
655                 return(NULL);
656         e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
657         e->entry = oidp;
658         TAILQ_INSERT_HEAD(clist, e, link);
659         return (e);
660 }
661
662 /* Find an entry in the context */
663 struct sysctl_ctx_entry *
664 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
665 {
666         struct sysctl_ctx_entry *e;
667
668         SYSCTL_ASSERT_WLOCKED();
669         if (clist == NULL || oidp == NULL)
670                 return(NULL);
671         TAILQ_FOREACH(e, clist, link) {
672                 if(e->entry == oidp)
673                         return(e);
674         }
675         return (e);
676 }
677
678 /*
679  * Delete an entry from the context.
680  * NOTE: this function doesn't free oidp! You have to remove it
681  * with sysctl_remove_oid().
682  */
683 int
684 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
685 {
686         struct sysctl_ctx_entry *e;
687
688         if (clist == NULL || oidp == NULL)
689                 return (EINVAL);
690         SYSCTL_WLOCK();
691         e = sysctl_ctx_entry_find(clist, oidp);
692         if (e != NULL) {
693                 TAILQ_REMOVE(clist, e, link);
694                 SYSCTL_WUNLOCK();
695                 free(e, M_SYSCTLOID);
696                 return (0);
697         } else {
698                 SYSCTL_WUNLOCK();
699                 return (ENOENT);
700         }
701 }
702
703 /*
704  * Remove dynamically created sysctl trees.
705  * oidp - top of the tree to be removed
706  * del - if 0 - just deregister, otherwise free up entries as well
707  * recurse - if != 0 traverse the subtree to be deleted
708  */
709 int
710 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
711 {
712         int error;
713
714         SYSCTL_WLOCK();
715         error = sysctl_remove_oid_locked(oidp, del, recurse);
716         SYSCTL_WUNLOCK();
717         return (error);
718 }
719
720 int
721 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
722     int del, int recurse)
723 {
724         struct sysctl_oid *p, *tmp;
725         int error;
726
727         error = ENOENT;
728         SYSCTL_WLOCK();
729         SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
730                 if (strcmp(p->oid_name, name) == 0) {
731                         error = sysctl_remove_oid_locked(p, del, recurse);
732                         break;
733                 }
734         }
735         SYSCTL_WUNLOCK();
736
737         return (error);
738 }
739
740 static int
741 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
742 {
743         struct sysctl_oid *p, *tmp;
744         int error;
745
746         SYSCTL_ASSERT_WLOCKED();
747         if (oidp == NULL)
748                 return(EINVAL);
749         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
750                 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
751                     oidp->oid_name);
752                 return (EINVAL);
753         }
754         /*
755          * WARNING: normal method to do this should be through
756          * sysctl_ctx_free(). Use recursing as the last resort
757          * method to purge your sysctl tree of leftovers...
758          * However, if some other code still references these nodes,
759          * it will panic.
760          */
761         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
762                 if (oidp->oid_refcnt == 1) {
763                         SLIST_FOREACH_SAFE(p,
764                             SYSCTL_CHILDREN(oidp), oid_link, tmp) {
765                                 if (!recurse) {
766                                         printf("Warning: failed attempt to "
767                                             "remove oid %s with child %s\n",
768                                             oidp->oid_name, p->oid_name);
769                                         return (ENOTEMPTY);
770                                 }
771                                 error = sysctl_remove_oid_locked(p, del,
772                                     recurse);
773                                 if (error)
774                                         return (error);
775                         }
776                 }
777         }
778         if (oidp->oid_refcnt > 1 ) {
779                 oidp->oid_refcnt--;
780         } else {
781                 if (oidp->oid_refcnt == 0) {
782                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
783                                 oidp->oid_refcnt, oidp->oid_name);
784                         return (EINVAL);
785                 }
786                 sysctl_unregister_oid(oidp);
787                 if (del) {
788                         /*
789                          * Wait for all threads running the handler to drain.
790                          * This preserves the previous behavior when the
791                          * sysctl lock was held across a handler invocation,
792                          * and is necessary for module unload correctness.
793                          */
794                         while (oidp->oid_running > 0) {
795                                 oidp->oid_kind |= CTLFLAG_DYING;
796                                 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
797                         }
798                         if (oidp->oid_descr)
799                                 free(__DECONST(char *, oidp->oid_descr),
800                                     M_SYSCTLOID);
801                         if (oidp->oid_label)
802                                 free(__DECONST(char *, oidp->oid_label),
803                                     M_SYSCTLOID);
804                         free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
805                         free(oidp, M_SYSCTLOID);
806                 }
807         }
808         return (0);
809 }
810 /*
811  * Create new sysctls at run time.
812  * clist may point to a valid context initialized with sysctl_ctx_init().
813  */
814 struct sysctl_oid *
815 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
816         int number, const char *name, int kind, void *arg1, intmax_t arg2,
817         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
818         const char *label)
819 {
820         struct sysctl_oid *oidp;
821
822         /* You have to hook up somewhere.. */
823         if (parent == NULL)
824                 return(NULL);
825         /* Check if the node already exists, otherwise create it */
826         SYSCTL_WLOCK();
827         oidp = sysctl_find_oidname(name, parent);
828         if (oidp != NULL) {
829                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
830                         oidp->oid_refcnt++;
831                         /* Update the context */
832                         if (clist != NULL)
833                                 sysctl_ctx_entry_add(clist, oidp);
834                         SYSCTL_WUNLOCK();
835                         return (oidp);
836                 } else {
837                         sysctl_warn_reuse(__func__, oidp);
838                         SYSCTL_WUNLOCK();
839                         return (NULL);
840                 }
841         }
842         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
843         oidp->oid_parent = parent;
844         SLIST_INIT(&oidp->oid_children);
845         oidp->oid_number = number;
846         oidp->oid_refcnt = 1;
847         oidp->oid_name = strdup(name, M_SYSCTLOID);
848         oidp->oid_handler = handler;
849         oidp->oid_kind = CTLFLAG_DYN | kind;
850         oidp->oid_arg1 = arg1;
851         oidp->oid_arg2 = arg2;
852         oidp->oid_fmt = fmt;
853         if (descr != NULL)
854                 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
855         if (label != NULL)
856                 oidp->oid_label = strdup(label, M_SYSCTLOID);
857         /* Update the context, if used */
858         if (clist != NULL)
859                 sysctl_ctx_entry_add(clist, oidp);
860         /* Register this oid */
861         sysctl_register_oid(oidp);
862         SYSCTL_WUNLOCK();
863         return (oidp);
864 }
865
866 /*
867  * Rename an existing oid.
868  */
869 void
870 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
871 {
872         char *newname;
873         char *oldname;
874
875         newname = strdup(name, M_SYSCTLOID);
876         SYSCTL_WLOCK();
877         oldname = __DECONST(char *, oidp->oid_name);
878         oidp->oid_name = newname;
879         SYSCTL_WUNLOCK();
880         free(oldname, M_SYSCTLOID);
881 }
882
883 /*
884  * Reparent an existing oid.
885  */
886 int
887 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
888 {
889         struct sysctl_oid *oidp;
890
891         SYSCTL_WLOCK();
892         if (oid->oid_parent == parent) {
893                 SYSCTL_WUNLOCK();
894                 return (0);
895         }
896         oidp = sysctl_find_oidname(oid->oid_name, parent);
897         if (oidp != NULL) {
898                 SYSCTL_WUNLOCK();
899                 return (EEXIST);
900         }
901         sysctl_unregister_oid(oid);
902         oid->oid_parent = parent;
903         oid->oid_number = OID_AUTO;
904         sysctl_register_oid(oid);
905         SYSCTL_WUNLOCK();
906         return (0);
907 }
908
909 /*
910  * Register the kernel's oids on startup.
911  */
912 SET_DECLARE(sysctl_set, struct sysctl_oid);
913
914 static void
915 sysctl_register_all(void *arg)
916 {
917         struct sysctl_oid **oidp;
918
919         sx_init(&sysctlmemlock, "sysctl mem");
920         SYSCTL_INIT();
921         SYSCTL_WLOCK();
922         SET_FOREACH(oidp, sysctl_set)
923                 sysctl_register_oid(*oidp);
924         SYSCTL_WUNLOCK();
925 }
926 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
927
928 /*
929  * "Staff-functions"
930  *
931  * These functions implement a presently undocumented interface 
932  * used by the sysctl program to walk the tree, and get the type
933  * so it can print the value.
934  * This interface is under work and consideration, and should probably
935  * be killed with a big axe by the first person who can find the time.
936  * (be aware though, that the proper interface isn't as obvious as it
937  * may seem, there are various conflicting requirements.
938  *
939  * {CTL_SYSCTL, CTL_SYSCTL_DEBUG}               printf the entire MIB-tree.
940  * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...}           return the name of the "..."
941  *                                              OID.
942  * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...}           return the next OID.
943  * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID}            return the OID of the name in
944  *                                              "new"
945  * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...}         return the kind & format info
946  *                                              for the "..." OID.
947  * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...}       return the description of the
948  *                                              "..." OID.
949  * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...}       return the aggregation label of
950  *                                              the "..." OID.
951  */
952
953 #ifdef SYSCTL_DEBUG
954 static void
955 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
956 {
957         int k;
958         struct sysctl_oid *oidp;
959
960         SYSCTL_ASSERT_LOCKED();
961         SLIST_FOREACH(oidp, l, oid_link) {
962
963                 for (k=0; k<i; k++)
964                         printf(" ");
965
966                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
967
968                 printf("%c%c",
969                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
970                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
971
972                 if (oidp->oid_handler)
973                         printf(" *Handler");
974
975                 switch (oidp->oid_kind & CTLTYPE) {
976                         case CTLTYPE_NODE:
977                                 printf(" Node\n");
978                                 if (!oidp->oid_handler) {
979                                         sysctl_sysctl_debug_dump_node(
980                                             SYSCTL_CHILDREN(oidp), i + 2);
981                                 }
982                                 break;
983                         case CTLTYPE_INT:    printf(" Int\n"); break;
984                         case CTLTYPE_UINT:   printf(" u_int\n"); break;
985                         case CTLTYPE_LONG:   printf(" Long\n"); break;
986                         case CTLTYPE_ULONG:  printf(" u_long\n"); break;
987                         case CTLTYPE_STRING: printf(" String\n"); break;
988                         case CTLTYPE_S8:     printf(" int8_t\n"); break;
989                         case CTLTYPE_S16:    printf(" int16_t\n"); break;
990                         case CTLTYPE_S32:    printf(" int32_t\n"); break;
991                         case CTLTYPE_S64:    printf(" int64_t\n"); break;
992                         case CTLTYPE_U8:     printf(" uint8_t\n"); break;
993                         case CTLTYPE_U16:    printf(" uint16_t\n"); break;
994                         case CTLTYPE_U32:    printf(" uint32_t\n"); break;
995                         case CTLTYPE_U64:    printf(" uint64_t\n"); break;
996                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
997                         default:             printf("\n");
998                 }
999
1000         }
1001 }
1002
1003 static int
1004 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1005 {
1006         struct rm_priotracker tracker;
1007         int error;
1008
1009         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1010         if (error)
1011                 return (error);
1012         SYSCTL_RLOCK(&tracker);
1013         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1014         SYSCTL_RUNLOCK(&tracker);
1015         return (ENOENT);
1016 }
1017
1018 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1019     CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1020 #endif
1021
1022 static int
1023 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1024 {
1025         int *name = (int *) arg1;
1026         u_int namelen = arg2;
1027         int error;
1028         struct sysctl_oid *oid;
1029         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1030         struct rm_priotracker tracker;
1031         char buf[10];
1032
1033         error = sysctl_wire_old_buffer(req, 0);
1034         if (error)
1035                 return (error);
1036
1037         SYSCTL_RLOCK(&tracker);
1038         while (namelen) {
1039                 if (!lsp) {
1040                         snprintf(buf,sizeof(buf),"%d",*name);
1041                         if (req->oldidx)
1042                                 error = SYSCTL_OUT(req, ".", 1);
1043                         if (!error)
1044                                 error = SYSCTL_OUT(req, buf, strlen(buf));
1045                         if (error)
1046                                 goto out;
1047                         namelen--;
1048                         name++;
1049                         continue;
1050                 }
1051                 lsp2 = NULL;
1052                 SLIST_FOREACH(oid, lsp, oid_link) {
1053                         if (oid->oid_number != *name)
1054                                 continue;
1055
1056                         if (req->oldidx)
1057                                 error = SYSCTL_OUT(req, ".", 1);
1058                         if (!error)
1059                                 error = SYSCTL_OUT(req, oid->oid_name,
1060                                         strlen(oid->oid_name));
1061                         if (error)
1062                                 goto out;
1063
1064                         namelen--;
1065                         name++;
1066
1067                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
1068                                 break;
1069
1070                         if (oid->oid_handler)
1071                                 break;
1072
1073                         lsp2 = SYSCTL_CHILDREN(oid);
1074                         break;
1075                 }
1076                 lsp = lsp2;
1077         }
1078         error = SYSCTL_OUT(req, "", 1);
1079  out:
1080         SYSCTL_RUNLOCK(&tracker);
1081         return (error);
1082 }
1083
1084 /*
1085  * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1086  * capability mode.
1087  */
1088 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1089     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1090
1091 static int
1092 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
1093         int *next, int *len, int level, struct sysctl_oid **oidpp)
1094 {
1095         struct sysctl_oid *oidp;
1096
1097         SYSCTL_ASSERT_LOCKED();
1098         *len = level;
1099         SLIST_FOREACH(oidp, lsp, oid_link) {
1100                 *next = oidp->oid_number;
1101                 *oidpp = oidp;
1102
1103                 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1104                         continue;
1105
1106                 if (!namelen) {
1107                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
1108                                 return (0);
1109                         if (oidp->oid_handler) 
1110                                 /* We really should call the handler here...*/
1111                                 return (0);
1112                         lsp = SYSCTL_CHILDREN(oidp);
1113                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
1114                                 len, level+1, oidpp))
1115                                 return (0);
1116                         goto emptynode;
1117                 }
1118
1119                 if (oidp->oid_number < *name)
1120                         continue;
1121
1122                 if (oidp->oid_number > *name) {
1123                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1124                                 return (0);
1125                         if (oidp->oid_handler)
1126                                 return (0);
1127                         lsp = SYSCTL_CHILDREN(oidp);
1128                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
1129                                 next+1, len, level+1, oidpp))
1130                                 return (0);
1131                         goto next;
1132                 }
1133                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1134                         continue;
1135
1136                 if (oidp->oid_handler)
1137                         continue;
1138
1139                 lsp = SYSCTL_CHILDREN(oidp);
1140                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
1141                         len, level+1, oidpp))
1142                         return (0);
1143         next:
1144                 namelen = 1;
1145         emptynode:
1146                 *len = level;
1147         }
1148         return (1);
1149 }
1150
1151 static int
1152 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1153 {
1154         int *name = (int *) arg1;
1155         u_int namelen = arg2;
1156         int i, j, error;
1157         struct sysctl_oid *oid;
1158         struct sysctl_oid_list *lsp = &sysctl__children;
1159         struct rm_priotracker tracker;
1160         int newoid[CTL_MAXNAME];
1161
1162         SYSCTL_RLOCK(&tracker);
1163         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1164         SYSCTL_RUNLOCK(&tracker);
1165         if (i)
1166                 return (ENOENT);
1167         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1168         return (error);
1169 }
1170
1171 /*
1172  * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1173  * capability mode.
1174  */
1175 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1176     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1177
1178 static int
1179 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1180 {
1181         struct sysctl_oid *oidp;
1182         struct sysctl_oid_list *lsp = &sysctl__children;
1183         char *p;
1184
1185         SYSCTL_ASSERT_LOCKED();
1186
1187         for (*len = 0; *len < CTL_MAXNAME;) {
1188                 p = strsep(&name, ".");
1189
1190                 oidp = SLIST_FIRST(lsp);
1191                 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1192                         if (oidp == NULL)
1193                                 return (ENOENT);
1194                         if (strcmp(p, oidp->oid_name) == 0)
1195                                 break;
1196                 }
1197                 *oid++ = oidp->oid_number;
1198                 (*len)++;
1199
1200                 if (name == NULL || *name == '\0') {
1201                         if (oidpp)
1202                                 *oidpp = oidp;
1203                         return (0);
1204                 }
1205
1206                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1207                         break;
1208
1209                 if (oidp->oid_handler)
1210                         break;
1211
1212                 lsp = SYSCTL_CHILDREN(oidp);
1213         }
1214         return (ENOENT);
1215 }
1216
1217 static int
1218 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1219 {
1220         char *p;
1221         int error, oid[CTL_MAXNAME], len = 0;
1222         struct sysctl_oid *op = NULL;
1223         struct rm_priotracker tracker;
1224         char buf[32];
1225
1226         if (!req->newlen) 
1227                 return (ENOENT);
1228         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
1229                 return (ENAMETOOLONG);
1230
1231         p = buf;
1232         if (req->newlen >= sizeof(buf))
1233                 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1234
1235         error = SYSCTL_IN(req, p, req->newlen);
1236         if (error) {
1237                 if (p != buf)
1238                         free(p, M_SYSCTL);
1239                 return (error);
1240         }
1241
1242         p [req->newlen] = '\0';
1243
1244         SYSCTL_RLOCK(&tracker);
1245         error = name2oid(p, oid, &len, &op);
1246         SYSCTL_RUNLOCK(&tracker);
1247
1248         if (p != buf)
1249                 free(p, M_SYSCTL);
1250
1251         if (error)
1252                 return (error);
1253
1254         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1255         return (error);
1256 }
1257
1258 /*
1259  * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1260  * capability mode.
1261  */
1262 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1263     CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1264     sysctl_sysctl_name2oid, "I", "");
1265
1266 static int
1267 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1268 {
1269         struct sysctl_oid *oid;
1270         struct rm_priotracker tracker;
1271         int error;
1272
1273         error = sysctl_wire_old_buffer(req, 0);
1274         if (error)
1275                 return (error);
1276
1277         SYSCTL_RLOCK(&tracker);
1278         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1279         if (error)
1280                 goto out;
1281
1282         if (oid->oid_fmt == NULL) {
1283                 error = ENOENT;
1284                 goto out;
1285         }
1286         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1287         if (error)
1288                 goto out;
1289         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1290  out:
1291         SYSCTL_RUNLOCK(&tracker);
1292         return (error);
1293 }
1294
1295 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1296     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1297
1298 static int
1299 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1300 {
1301         struct sysctl_oid *oid;
1302         struct rm_priotracker tracker;
1303         int error;
1304
1305         error = sysctl_wire_old_buffer(req, 0);
1306         if (error)
1307                 return (error);
1308
1309         SYSCTL_RLOCK(&tracker);
1310         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1311         if (error)
1312                 goto out;
1313
1314         if (oid->oid_descr == NULL) {
1315                 error = ENOENT;
1316                 goto out;
1317         }
1318         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1319  out:
1320         SYSCTL_RUNLOCK(&tracker);
1321         return (error);
1322 }
1323
1324 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1325     CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1326
1327 static int
1328 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1329 {
1330         struct sysctl_oid *oid;
1331         struct rm_priotracker tracker;
1332         int error;
1333
1334         error = sysctl_wire_old_buffer(req, 0);
1335         if (error)
1336                 return (error);
1337
1338         SYSCTL_RLOCK(&tracker);
1339         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1340         if (error)
1341                 goto out;
1342
1343         if (oid->oid_label == NULL) {
1344                 error = ENOENT;
1345                 goto out;
1346         }
1347         error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1348  out:
1349         SYSCTL_RUNLOCK(&tracker);
1350         return (error);
1351 }
1352
1353 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1354     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1355
1356 /*
1357  * Default "handler" functions.
1358  */
1359
1360 /*
1361  * Handle a bool.
1362  * Two cases:
1363  *     a variable:  point arg1 at it.
1364  *     a constant:  pass it in arg2.
1365  */
1366
1367 int
1368 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1369 {
1370         uint8_t temp;
1371         int error;
1372
1373         /*
1374          * Attempt to get a coherent snapshot by making a copy of the data.
1375          */
1376         if (arg1)
1377                 temp = *(bool *)arg1 ? 1 : 0;
1378         else
1379                 temp = arg2 ? 1 : 0;
1380
1381         error = SYSCTL_OUT(req, &temp, sizeof(temp));
1382         if (error || !req->newptr)
1383                 return (error);
1384
1385         if (!arg1)
1386                 error = EPERM;
1387         else {
1388                 error = SYSCTL_IN(req, &temp, sizeof(temp));
1389                 if (!error)
1390                         *(bool *)arg1 = temp ? 1 : 0;
1391         }
1392         return (error);
1393 }
1394
1395 /*
1396  * Handle an int8_t, signed or unsigned.
1397  * Two cases:
1398  *     a variable:  point arg1 at it.
1399  *     a constant:  pass it in arg2.
1400  */
1401
1402 int
1403 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1404 {
1405         int8_t tmpout;
1406         int error = 0;
1407
1408         /*
1409          * Attempt to get a coherent snapshot by making a copy of the data.
1410          */
1411         if (arg1)
1412                 tmpout = *(int8_t *)arg1;
1413         else
1414                 tmpout = arg2;
1415         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1416
1417         if (error || !req->newptr)
1418                 return (error);
1419
1420         if (!arg1)
1421                 error = EPERM;
1422         else
1423                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1424         return (error);
1425 }
1426
1427 /*
1428  * Handle an int16_t, signed or unsigned.
1429  * Two cases:
1430  *     a variable:  point arg1 at it.
1431  *     a constant:  pass it in arg2.
1432  */
1433
1434 int
1435 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1436 {
1437         int16_t tmpout;
1438         int error = 0;
1439
1440         /*
1441          * Attempt to get a coherent snapshot by making a copy of the data.
1442          */
1443         if (arg1)
1444                 tmpout = *(int16_t *)arg1;
1445         else
1446                 tmpout = arg2;
1447         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1448
1449         if (error || !req->newptr)
1450                 return (error);
1451
1452         if (!arg1)
1453                 error = EPERM;
1454         else
1455                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1456         return (error);
1457 }
1458
1459 /*
1460  * Handle an int32_t, signed or unsigned.
1461  * Two cases:
1462  *     a variable:  point arg1 at it.
1463  *     a constant:  pass it in arg2.
1464  */
1465
1466 int
1467 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1468 {
1469         int32_t tmpout;
1470         int error = 0;
1471
1472         /*
1473          * Attempt to get a coherent snapshot by making a copy of the data.
1474          */
1475         if (arg1)
1476                 tmpout = *(int32_t *)arg1;
1477         else
1478                 tmpout = arg2;
1479         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1480
1481         if (error || !req->newptr)
1482                 return (error);
1483
1484         if (!arg1)
1485                 error = EPERM;
1486         else
1487                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1488         return (error);
1489 }
1490
1491 /*
1492  * Handle an int, signed or unsigned.
1493  * Two cases:
1494  *     a variable:  point arg1 at it.
1495  *     a constant:  pass it in arg2.
1496  */
1497
1498 int
1499 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1500 {
1501         int tmpout, error = 0;
1502
1503         /*
1504          * Attempt to get a coherent snapshot by making a copy of the data.
1505          */
1506         if (arg1)
1507                 tmpout = *(int *)arg1;
1508         else
1509                 tmpout = arg2;
1510         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1511
1512         if (error || !req->newptr)
1513                 return (error);
1514
1515         if (!arg1)
1516                 error = EPERM;
1517         else
1518                 error = SYSCTL_IN(req, arg1, sizeof(int));
1519         return (error);
1520 }
1521
1522 /*
1523  * Based on on sysctl_handle_int() convert milliseconds into ticks.
1524  * Note: this is used by TCP.
1525  */
1526
1527 int
1528 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1529 {
1530         int error, s, tt;
1531
1532         tt = *(int *)arg1;
1533         s = (int)((int64_t)tt * 1000 / hz);
1534
1535         error = sysctl_handle_int(oidp, &s, 0, req);
1536         if (error || !req->newptr)
1537                 return (error);
1538
1539         tt = (int)((int64_t)s * hz / 1000);
1540         if (tt < 1)
1541                 return (EINVAL);
1542
1543         *(int *)arg1 = tt;
1544         return (0);
1545 }
1546
1547 /*
1548  * Handle a long, signed or unsigned.
1549  * Two cases:
1550  *     a variable:  point arg1 at it.
1551  *     a constant:  pass it in arg2.
1552  */
1553
1554 int
1555 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1556 {
1557         int error = 0;
1558         long tmplong;
1559 #ifdef SCTL_MASK32
1560         int tmpint;
1561 #endif
1562
1563         /*
1564          * Attempt to get a coherent snapshot by making a copy of the data.
1565          */
1566         if (arg1)
1567                 tmplong = *(long *)arg1;
1568         else
1569                 tmplong = arg2;
1570 #ifdef SCTL_MASK32
1571         if (req->flags & SCTL_MASK32) {
1572                 tmpint = tmplong;
1573                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1574         } else
1575 #endif
1576                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1577
1578         if (error || !req->newptr)
1579                 return (error);
1580
1581         if (!arg1)
1582                 error = EPERM;
1583 #ifdef SCTL_MASK32
1584         else if (req->flags & SCTL_MASK32) {
1585                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1586                 *(long *)arg1 = (long)tmpint;
1587         }
1588 #endif
1589         else
1590                 error = SYSCTL_IN(req, arg1, sizeof(long));
1591         return (error);
1592 }
1593
1594 /*
1595  * Handle a 64 bit int, signed or unsigned.
1596  * Two cases:
1597  *     a variable:  point arg1 at it.
1598  *     a constant:  pass it in arg2.
1599  */
1600 int
1601 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1602 {
1603         int error = 0;
1604         uint64_t tmpout;
1605
1606         /*
1607          * Attempt to get a coherent snapshot by making a copy of the data.
1608          */
1609         if (arg1)
1610                 tmpout = *(uint64_t *)arg1;
1611         else
1612                 tmpout = arg2;
1613         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1614
1615         if (error || !req->newptr)
1616                 return (error);
1617
1618         if (!arg1)
1619                 error = EPERM;
1620         else
1621                 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1622         return (error);
1623 }
1624
1625 /*
1626  * Handle our generic '\0' terminated 'C' string.
1627  * Two cases:
1628  *      a variable string:  point arg1 at it, arg2 is max length.
1629  *      a constant string:  point arg1 at it, arg2 is zero.
1630  */
1631
1632 int
1633 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1634 {
1635         size_t outlen;
1636         int error = 0, ro_string = 0;
1637
1638         /*
1639          * A zero-length buffer indicates a fixed size read-only
1640          * string.  In ddb, don't worry about trying to make a malloced
1641          * snapshot.
1642          */
1643         if (arg2 == 0 || kdb_active) {
1644                 arg2 = strlen((char *)arg1) + 1;
1645                 ro_string = 1;
1646         }
1647
1648         if (req->oldptr != NULL) {
1649                 char *tmparg;
1650
1651                 if (ro_string) {
1652                         tmparg = arg1;
1653                 } else {
1654                         /* try to make a coherent snapshot of the string */
1655                         tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1656                         memcpy(tmparg, arg1, arg2);
1657                 }
1658
1659                 outlen = strnlen(tmparg, arg2 - 1) + 1;
1660                 error = SYSCTL_OUT(req, tmparg, outlen);
1661
1662                 if (!ro_string)
1663                         free(tmparg, M_SYSCTLTMP);
1664         } else {
1665                 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1666                 error = SYSCTL_OUT(req, NULL, outlen);
1667         }
1668         if (error || !req->newptr)
1669                 return (error);
1670
1671         if ((req->newlen - req->newidx) >= arg2) {
1672                 error = EINVAL;
1673         } else {
1674                 arg2 = (req->newlen - req->newidx);
1675                 error = SYSCTL_IN(req, arg1, arg2);
1676                 ((char *)arg1)[arg2] = '\0';
1677         }
1678         return (error);
1679 }
1680
1681 /*
1682  * Handle any kind of opaque data.
1683  * arg1 points to it, arg2 is the size.
1684  */
1685
1686 int
1687 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1688 {
1689         int error, tries;
1690         u_int generation;
1691         struct sysctl_req req2;
1692
1693         /*
1694          * Attempt to get a coherent snapshot, by using the thread
1695          * pre-emption counter updated from within mi_switch() to
1696          * determine if we were pre-empted during a bcopy() or
1697          * copyout(). Make 3 attempts at doing this before giving up.
1698          * If we encounter an error, stop immediately.
1699          */
1700         tries = 0;
1701         req2 = *req;
1702 retry:
1703         generation = curthread->td_generation;
1704         error = SYSCTL_OUT(req, arg1, arg2);
1705         if (error)
1706                 return (error);
1707         tries++;
1708         if (generation != curthread->td_generation && tries < 3) {
1709                 *req = req2;
1710                 goto retry;
1711         }
1712
1713         error = SYSCTL_IN(req, arg1, arg2);
1714
1715         return (error);
1716 }
1717
1718 /*
1719  * Based on on sysctl_handle_int() convert microseconds to a sbintime.
1720  */
1721 int
1722 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
1723 {
1724         int error;
1725         int64_t tt;
1726         sbintime_t sb;
1727
1728         tt = *(int64_t *)arg1;
1729         sb = sbttous(tt);
1730
1731         error = sysctl_handle_64(oidp, &sb, 0, req);
1732         if (error || !req->newptr)
1733                 return (error);
1734
1735         tt = ustosbt(sb);
1736         *(int64_t *)arg1 = tt;
1737
1738         return (0);
1739 }
1740
1741 /*
1742  * Based on on sysctl_handle_int() convert milliseconds to a sbintime.
1743  */
1744 int
1745 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
1746 {
1747         int error;
1748         int64_t tt;
1749         sbintime_t sb;
1750
1751         tt = *(int64_t *)arg1;
1752         sb = sbttoms(tt);
1753
1754         error = sysctl_handle_64(oidp, &sb, 0, req);
1755         if (error || !req->newptr)
1756                 return (error);
1757
1758         tt = mstosbt(sb);
1759         *(int64_t *)arg1 = tt;
1760
1761         return (0);
1762 }
1763
1764 /*
1765  * Convert seconds to a struct timeval.  Intended for use with
1766  * intervals and thus does not permit negative seconds.
1767  */
1768 int
1769 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1770 {
1771         struct timeval *tv;
1772         int error, secs;
1773
1774         tv = arg1;
1775         secs = tv->tv_sec;
1776
1777         error = sysctl_handle_int(oidp, &secs, 0, req);
1778         if (error || req->newptr == NULL)
1779                 return (error);
1780
1781         if (secs < 0)
1782                 return (EINVAL);
1783         tv->tv_sec = secs;
1784
1785         return (0);
1786 }
1787
1788 /*
1789  * Transfer functions to/from kernel space.
1790  * XXX: rather untested at this point
1791  */
1792 static int
1793 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1794 {
1795         size_t i = 0;
1796
1797         if (req->oldptr) {
1798                 i = l;
1799                 if (req->oldlen <= req->oldidx)
1800                         i = 0;
1801                 else
1802                         if (i > req->oldlen - req->oldidx)
1803                                 i = req->oldlen - req->oldidx;
1804                 if (i > 0)
1805                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
1806         }
1807         req->oldidx += l;
1808         if (req->oldptr && i != l)
1809                 return (ENOMEM);
1810         return (0);
1811 }
1812
1813 static int
1814 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1815 {
1816         if (!req->newptr)
1817                 return (0);
1818         if (req->newlen - req->newidx < l)
1819                 return (EINVAL);
1820         bcopy((const char *)req->newptr + req->newidx, p, l);
1821         req->newidx += l;
1822         return (0);
1823 }
1824
1825 int
1826 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1827     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1828 {
1829         int error = 0;
1830         struct sysctl_req req;
1831
1832         bzero(&req, sizeof req);
1833
1834         req.td = td;
1835         req.flags = flags;
1836
1837         if (oldlenp) {
1838                 req.oldlen = *oldlenp;
1839         }
1840         req.validlen = req.oldlen;
1841
1842         if (old) {
1843                 req.oldptr= old;
1844         }
1845
1846         if (new != NULL) {
1847                 req.newlen = newlen;
1848                 req.newptr = new;
1849         }
1850
1851         req.oldfunc = sysctl_old_kernel;
1852         req.newfunc = sysctl_new_kernel;
1853         req.lock = REQ_UNWIRED;
1854
1855         error = sysctl_root(0, name, namelen, &req);
1856
1857         if (req.lock == REQ_WIRED && req.validlen > 0)
1858                 vsunlock(req.oldptr, req.validlen);
1859
1860         if (error && error != ENOMEM)
1861                 return (error);
1862
1863         if (retval) {
1864                 if (req.oldptr && req.oldidx > req.validlen)
1865                         *retval = req.validlen;
1866                 else
1867                         *retval = req.oldidx;
1868         }
1869         return (error);
1870 }
1871
1872 int
1873 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1874     void *new, size_t newlen, size_t *retval, int flags)
1875 {
1876         int oid[CTL_MAXNAME];
1877         size_t oidlen, plen;
1878         int error;
1879
1880         oid[0] = CTL_SYSCTL;
1881         oid[1] = CTL_SYSCTL_NAME2OID;
1882         oidlen = sizeof(oid);
1883
1884         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1885             (void *)name, strlen(name), &plen, flags);
1886         if (error)
1887                 return (error);
1888
1889         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1890             new, newlen, retval, flags);
1891         return (error);
1892 }
1893
1894 /*
1895  * Transfer function to/from user space.
1896  */
1897 static int
1898 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1899 {
1900         size_t i, len, origidx;
1901         int error;
1902
1903         origidx = req->oldidx;
1904         req->oldidx += l;
1905         if (req->oldptr == NULL)
1906                 return (0);
1907         /*
1908          * If we have not wired the user supplied buffer and we are currently
1909          * holding locks, drop a witness warning, as it's possible that
1910          * write operations to the user page can sleep.
1911          */
1912         if (req->lock != REQ_WIRED)
1913                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1914                     "sysctl_old_user()");
1915         i = l;
1916         len = req->validlen;
1917         if (len <= origidx)
1918                 i = 0;
1919         else {
1920                 if (i > len - origidx)
1921                         i = len - origidx;
1922                 if (req->lock == REQ_WIRED) {
1923                         error = copyout_nofault(p, (char *)req->oldptr +
1924                             origidx, i);
1925                 } else
1926                         error = copyout(p, (char *)req->oldptr + origidx, i);
1927                 if (error != 0)
1928                         return (error);
1929         }
1930         if (i < l)
1931                 return (ENOMEM);
1932         return (0);
1933 }
1934
1935 static int
1936 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1937 {
1938         int error;
1939
1940         if (!req->newptr)
1941                 return (0);
1942         if (req->newlen - req->newidx < l)
1943                 return (EINVAL);
1944         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1945             "sysctl_new_user()");
1946         error = copyin((const char *)req->newptr + req->newidx, p, l);
1947         req->newidx += l;
1948         return (error);
1949 }
1950
1951 /*
1952  * Wire the user space destination buffer.  If set to a value greater than
1953  * zero, the len parameter limits the maximum amount of wired memory.
1954  */
1955 int
1956 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1957 {
1958         int ret;
1959         size_t wiredlen;
1960
1961         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1962         ret = 0;
1963         if (req->lock != REQ_WIRED && req->oldptr &&
1964             req->oldfunc == sysctl_old_user) {
1965                 if (wiredlen != 0) {
1966                         ret = vslock(req->oldptr, wiredlen);
1967                         if (ret != 0) {
1968                                 if (ret != ENOMEM)
1969                                         return (ret);
1970                                 wiredlen = 0;
1971                         }
1972                 }
1973                 req->lock = REQ_WIRED;
1974                 req->validlen = wiredlen;
1975         }
1976         return (0);
1977 }
1978
1979 int
1980 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1981     int *nindx, struct sysctl_req *req)
1982 {
1983         struct sysctl_oid_list *lsp;
1984         struct sysctl_oid *oid;
1985         int indx;
1986
1987         SYSCTL_ASSERT_LOCKED();
1988         lsp = &sysctl__children;
1989         indx = 0;
1990         while (indx < CTL_MAXNAME) {
1991                 SLIST_FOREACH(oid, lsp, oid_link) {
1992                         if (oid->oid_number == name[indx])
1993                                 break;
1994                 }
1995                 if (oid == NULL)
1996                         return (ENOENT);
1997
1998                 indx++;
1999                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2000                         if (oid->oid_handler != NULL || indx == namelen) {
2001                                 *noid = oid;
2002                                 if (nindx != NULL)
2003                                         *nindx = indx;
2004                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2005                                     ("%s found DYING node %p", __func__, oid));
2006                                 return (0);
2007                         }
2008                         lsp = SYSCTL_CHILDREN(oid);
2009                 } else if (indx == namelen) {
2010                         if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2011                                 return (ENOENT);
2012                         *noid = oid;
2013                         if (nindx != NULL)
2014                                 *nindx = indx;
2015                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2016                             ("%s found DYING node %p", __func__, oid));
2017                         return (0);
2018                 } else {
2019                         return (ENOTDIR);
2020                 }
2021         }
2022         return (ENOENT);
2023 }
2024
2025 /*
2026  * Traverse our tree, and find the right node, execute whatever it points
2027  * to, and return the resulting error code.
2028  */
2029
2030 static int
2031 sysctl_root(SYSCTL_HANDLER_ARGS)
2032 {
2033         struct sysctl_oid *oid;
2034         struct rm_priotracker tracker;
2035         int error, indx, lvl;
2036
2037         SYSCTL_RLOCK(&tracker);
2038
2039         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2040         if (error)
2041                 goto out;
2042
2043         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2044                 /*
2045                  * You can't call a sysctl when it's a node, but has
2046                  * no handler.  Inform the user that it's a node.
2047                  * The indx may or may not be the same as namelen.
2048                  */
2049                 if (oid->oid_handler == NULL) {
2050                         error = EISDIR;
2051                         goto out;
2052                 }
2053         }
2054
2055         /* Is this sysctl writable? */
2056         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2057                 error = EPERM;
2058                 goto out;
2059         }
2060
2061         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2062
2063 #ifdef CAPABILITY_MODE
2064         /*
2065          * If the process is in capability mode, then don't permit reading or
2066          * writing unless specifically granted for the node.
2067          */
2068         if (IN_CAPABILITY_MODE(req->td)) {
2069                 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2070                     (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2071                         error = EPERM;
2072                         goto out;
2073                 }
2074         }
2075 #endif
2076
2077         /* Is this sysctl sensitive to securelevels? */
2078         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2079                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2080                 error = securelevel_gt(req->td->td_ucred, lvl);
2081                 if (error)
2082                         goto out;
2083         }
2084
2085         /* Is this sysctl writable by only privileged users? */
2086         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2087                 int priv;
2088
2089                 if (oid->oid_kind & CTLFLAG_PRISON)
2090                         priv = PRIV_SYSCTL_WRITEJAIL;
2091 #ifdef VIMAGE
2092                 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2093                      prison_owns_vnet(req->td->td_ucred))
2094                         priv = PRIV_SYSCTL_WRITEJAIL;
2095 #endif
2096                 else
2097                         priv = PRIV_SYSCTL_WRITE;
2098                 error = priv_check(req->td, priv);
2099                 if (error)
2100                         goto out;
2101         }
2102
2103         if (!oid->oid_handler) {
2104                 error = EINVAL;
2105                 goto out;
2106         }
2107
2108         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2109                 arg1 = (int *)arg1 + indx;
2110                 arg2 -= indx;
2111         } else {
2112                 arg1 = oid->oid_arg1;
2113                 arg2 = oid->oid_arg2;
2114         }
2115 #ifdef MAC
2116         error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2117             req);
2118         if (error != 0)
2119                 goto out;
2120 #endif
2121 #ifdef VIMAGE
2122         if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2123                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2124 #endif
2125         error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2126
2127 out:
2128         SYSCTL_RUNLOCK(&tracker);
2129         return (error);
2130 }
2131
2132 #ifndef _SYS_SYSPROTO_H_
2133 struct sysctl_args {
2134         int     *name;
2135         u_int   namelen;
2136         void    *old;
2137         size_t  *oldlenp;
2138         void    *new;
2139         size_t  newlen;
2140 };
2141 #endif
2142 int
2143 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2144 {
2145         int error, i, name[CTL_MAXNAME];
2146         size_t j;
2147
2148         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2149                 return (EINVAL);
2150
2151         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2152         if (error)
2153                 return (error);
2154
2155         error = userland_sysctl(td, name, uap->namelen,
2156                 uap->old, uap->oldlenp, 0,
2157                 uap->new, uap->newlen, &j, 0);
2158         if (error && error != ENOMEM)
2159                 return (error);
2160         if (uap->oldlenp) {
2161                 i = copyout(&j, uap->oldlenp, sizeof(j));
2162                 if (i)
2163                         return (i);
2164         }
2165         return (error);
2166 }
2167
2168 int
2169 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2170     void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2171     int flags, bool inkernel)
2172 {
2173         int oid[CTL_MAXNAME];
2174         char namebuf[16];
2175         char *name;
2176         size_t oidlen;
2177         int error;
2178
2179         if (namelen > MAXPATHLEN || namelen == 0)
2180                 return (EINVAL);
2181         name = namebuf;
2182         if (namelen > sizeof(namebuf))
2183                 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2184         error = copyin(oname, name, namelen);
2185         if (error != 0)
2186                 goto out;
2187
2188         oid[0] = CTL_SYSCTL;
2189         oid[1] = CTL_SYSCTL_NAME2OID;
2190         oidlen = sizeof(oid);
2191         error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2192             retval, flags);
2193         if (error != 0)
2194                 goto out;
2195         error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2196             inkernel, new, newlen, retval, flags);
2197
2198 out:
2199         if (namelen > sizeof(namebuf))
2200                 free(name, M_SYSCTL);
2201         return (error);
2202 }
2203
2204 #ifndef _SYS_SYSPROTO_H_
2205 struct __sysctlbyname_args {
2206         const char      *name;
2207         size_t  namelen;
2208         void    *old;
2209         size_t  *oldlenp;
2210         void    *new;
2211         size_t  newlen;
2212 };
2213 #endif
2214 int
2215 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2216 {
2217         size_t rv;
2218         int error;
2219
2220         error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2221             uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2222         if (error != 0)
2223                 return (error);
2224         if (uap->oldlenp != NULL)
2225                 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2226
2227         return (error);
2228 }
2229
2230 /*
2231  * This is used from various compatibility syscalls too.  That's why name
2232  * must be in kernel space.
2233  */
2234 int
2235 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2236     size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2237     size_t *retval, int flags)
2238 {
2239         int error = 0, memlocked;
2240         struct sysctl_req req;
2241
2242         bzero(&req, sizeof req);
2243
2244         req.td = td;
2245         req.flags = flags;
2246
2247         if (oldlenp) {
2248                 if (inkernel) {
2249                         req.oldlen = *oldlenp;
2250                 } else {
2251                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2252                         if (error)
2253                                 return (error);
2254                 }
2255         }
2256         req.validlen = req.oldlen;
2257         req.oldptr = old;
2258
2259         if (new != NULL) {
2260                 req.newlen = newlen;
2261                 req.newptr = new;
2262         }
2263
2264         req.oldfunc = sysctl_old_user;
2265         req.newfunc = sysctl_new_user;
2266         req.lock = REQ_UNWIRED;
2267
2268 #ifdef KTRACE
2269         if (KTRPOINT(curthread, KTR_SYSCTL))
2270                 ktrsysctl(name, namelen);
2271 #endif
2272         memlocked = 0;
2273         if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2274                 memlocked = 1;
2275                 sx_xlock(&sysctlmemlock);
2276         }
2277         CURVNET_SET(TD_TO_VNET(td));
2278
2279         for (;;) {
2280                 req.oldidx = 0;
2281                 req.newidx = 0;
2282                 error = sysctl_root(0, name, namelen, &req);
2283                 if (error != EAGAIN)
2284                         break;
2285                 kern_yield(PRI_USER);
2286         }
2287
2288         CURVNET_RESTORE();
2289
2290         if (req.lock == REQ_WIRED && req.validlen > 0)
2291                 vsunlock(req.oldptr, req.validlen);
2292         if (memlocked)
2293                 sx_xunlock(&sysctlmemlock);
2294
2295         if (error && error != ENOMEM)
2296                 return (error);
2297
2298         if (retval) {
2299                 if (req.oldptr && req.oldidx > req.validlen)
2300                         *retval = req.validlen;
2301                 else
2302                         *retval = req.oldidx;
2303         }
2304         return (error);
2305 }
2306
2307 /*
2308  * Drain into a sysctl struct.  The user buffer should be wired if a page
2309  * fault would cause issue.
2310  */
2311 static int
2312 sbuf_sysctl_drain(void *arg, const char *data, int len)
2313 {
2314         struct sysctl_req *req = arg;
2315         int error;
2316
2317         error = SYSCTL_OUT(req, data, len);
2318         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2319         return (error == 0 ? len : -error);
2320 }
2321
2322 struct sbuf *
2323 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2324     struct sysctl_req *req)
2325 {
2326
2327         /* Supply a default buffer size if none given. */
2328         if (buf == NULL && length == 0)
2329                 length = 64;
2330         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2331         sbuf_set_drain(s, sbuf_sysctl_drain, req);
2332         return (s);
2333 }
2334
2335 #ifdef DDB
2336
2337 /* The current OID the debugger is working with */
2338 static struct sysctl_oid *g_ddb_oid;
2339
2340 /* The current flags specified by the user */
2341 static int g_ddb_sysctl_flags;
2342
2343 /* Check to see if the last sysctl printed */
2344 static int g_ddb_sysctl_printed;
2345
2346 static const int ctl_sign[CTLTYPE+1] = {
2347         [CTLTYPE_INT] = 1,
2348         [CTLTYPE_LONG] = 1,
2349         [CTLTYPE_S8] = 1,
2350         [CTLTYPE_S16] = 1,
2351         [CTLTYPE_S32] = 1,
2352         [CTLTYPE_S64] = 1,
2353 };
2354
2355 static const int ctl_size[CTLTYPE+1] = {
2356         [CTLTYPE_INT] = sizeof(int),
2357         [CTLTYPE_UINT] = sizeof(u_int),
2358         [CTLTYPE_LONG] = sizeof(long),
2359         [CTLTYPE_ULONG] = sizeof(u_long),
2360         [CTLTYPE_S8] = sizeof(int8_t),
2361         [CTLTYPE_S16] = sizeof(int16_t),
2362         [CTLTYPE_S32] = sizeof(int32_t),
2363         [CTLTYPE_S64] = sizeof(int64_t),
2364         [CTLTYPE_U8] = sizeof(uint8_t),
2365         [CTLTYPE_U16] = sizeof(uint16_t),
2366         [CTLTYPE_U32] = sizeof(uint32_t),
2367         [CTLTYPE_U64] = sizeof(uint64_t),
2368 };
2369
2370 #define DB_SYSCTL_NAME_ONLY     0x001   /* Compare with -N */
2371 #define DB_SYSCTL_VALUE_ONLY    0x002   /* Compare with -n */
2372 #define DB_SYSCTL_OPAQUE        0x004   /* Compare with -o */
2373 #define DB_SYSCTL_HEX           0x008   /* Compare with -x */
2374
2375 #define DB_SYSCTL_SAFE_ONLY     0x100   /* Only simple types */
2376
2377 static const char db_sysctl_modifs[] = {
2378         'N', 'n', 'o', 'x',
2379 };
2380
2381 static const int db_sysctl_modif_values[] = {
2382         DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2383         DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2384 };
2385
2386 /* Handlers considered safe to print while recursing */
2387 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2388         sysctl_handle_bool,
2389         sysctl_handle_8,
2390         sysctl_handle_16,
2391         sysctl_handle_32,
2392         sysctl_handle_64,
2393         sysctl_handle_int,
2394         sysctl_handle_long,
2395         sysctl_handle_string,
2396         sysctl_handle_opaque,
2397 };
2398
2399 /*
2400  * Use in place of sysctl_old_kernel to print sysctl values.
2401  *
2402  * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2403  */
2404 static int
2405 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2406 {
2407         const u_char *val, *p;
2408         const char *sep1;
2409         size_t intlen, slen;
2410         uintmax_t umv;
2411         intmax_t mv;
2412         int sign, ctltype, hexlen, xflag, error;
2413
2414         /* Suppress false-positive GCC uninitialized variable warnings */
2415         mv = 0;
2416         umv = 0;
2417
2418         slen = len;
2419         val = p = ptr;
2420
2421         if (ptr == NULL) {
2422                 error = 0;
2423                 goto out;
2424         }
2425
2426         /* We are going to print */
2427         g_ddb_sysctl_printed = 1;
2428
2429         xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2430
2431         ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2432         sign = ctl_sign[ctltype];
2433         intlen = ctl_size[ctltype];
2434
2435         switch (ctltype) {
2436         case CTLTYPE_NODE:
2437         case CTLTYPE_STRING:
2438                 db_printf("%.*s", (int) len, (const char *) p);
2439                 error = 0;
2440                 goto out;
2441
2442         case CTLTYPE_INT:
2443         case CTLTYPE_UINT:
2444         case CTLTYPE_LONG:
2445         case CTLTYPE_ULONG:
2446         case CTLTYPE_S8:
2447         case CTLTYPE_S16:
2448         case CTLTYPE_S32:
2449         case CTLTYPE_S64:
2450         case CTLTYPE_U8:
2451         case CTLTYPE_U16:
2452         case CTLTYPE_U32:
2453         case CTLTYPE_U64:
2454                 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2455                 sep1 = "";
2456                 while (len >= intlen) {
2457                         switch (ctltype) {
2458                         case CTLTYPE_INT:
2459                         case CTLTYPE_UINT:
2460                                 umv = *(const u_int *)p;
2461                                 mv = *(const int *)p;
2462                                 break;
2463                         case CTLTYPE_LONG:
2464                         case CTLTYPE_ULONG:
2465                                 umv = *(const u_long *)p;
2466                                 mv = *(const long *)p;
2467                                 break;
2468                         case CTLTYPE_S8:
2469                         case CTLTYPE_U8:
2470                                 umv = *(const uint8_t *)p;
2471                                 mv = *(const int8_t *)p;
2472                                 break;
2473                         case CTLTYPE_S16:
2474                         case CTLTYPE_U16:
2475                                 umv = *(const uint16_t *)p;
2476                                 mv = *(const int16_t *)p;
2477                                 break;
2478                         case CTLTYPE_S32:
2479                         case CTLTYPE_U32:
2480                                 umv = *(const uint32_t *)p;
2481                                 mv = *(const int32_t *)p;
2482                                 break;
2483                         case CTLTYPE_S64:
2484                         case CTLTYPE_U64:
2485                                 umv = *(const uint64_t *)p;
2486                                 mv = *(const int64_t *)p;
2487                                 break;
2488                         }
2489
2490                         db_printf("%s", sep1);
2491                         if (xflag)
2492                                 db_printf("%#0*jx", hexlen, umv);
2493                         else if (!sign)
2494                                 db_printf("%ju", umv);
2495                         else if (g_ddb_oid->oid_fmt[1] == 'K') {
2496                                 /* Kelvins are currently unsupported. */
2497                                 error = EOPNOTSUPP;
2498                                 goto out;
2499                         } else
2500                                 db_printf("%jd", mv);
2501
2502                         sep1 = " ";
2503                         len -= intlen;
2504                         p += intlen;
2505                 }
2506                 error = 0;
2507                 goto out;
2508
2509         case CTLTYPE_OPAQUE:
2510                 /* TODO: Support struct functions. */
2511
2512                 /* FALLTHROUGH */
2513         default:
2514                 db_printf("Format:%s Length:%zu Dump:0x",
2515                     g_ddb_oid->oid_fmt, len);
2516                 while (len-- && (xflag || p < val + 16))
2517                         db_printf("%02x", *p++);
2518                 if (!xflag && len > 16)
2519                         db_printf("...");
2520                 error = 0;
2521                 goto out;
2522         }
2523
2524 out:
2525         req->oldidx += slen;
2526         return (error);
2527 }
2528
2529 /*
2530  * Avoid setting new sysctl values from the debugger
2531  */
2532 static int
2533 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2534 {
2535
2536         if (!req->newptr)
2537                 return (0);
2538
2539         /* Changing sysctls from the debugger is currently unsupported */
2540         return (EPERM);
2541 }
2542
2543 /*
2544  * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2545  * Instead of copying any output to a buffer we'll dump it right to
2546  * the console.
2547  */
2548 static int
2549 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2550     void *old, size_t *oldlenp, size_t *retval, int flags)
2551 {
2552         struct sysctl_req req;
2553         int error;
2554
2555         /* Setup the request */
2556         bzero(&req, sizeof req);
2557         req.td = kdb_thread;
2558         req.oldfunc = sysctl_old_ddb;
2559         req.newfunc = sysctl_new_ddb;
2560         req.lock = REQ_UNWIRED;
2561         if (oldlenp) {
2562                 req.oldlen = *oldlenp;
2563         }
2564         req.validlen = req.oldlen;
2565         if (old) {
2566                 req.oldptr = old;
2567         }
2568
2569         /* Setup our globals for sysctl_old_ddb */
2570         g_ddb_oid = oidp;
2571         g_ddb_sysctl_flags = flags;
2572         g_ddb_sysctl_printed = 0;
2573
2574         error = sysctl_root(0, name, namelen, &req);
2575
2576         /* Reset globals */
2577         g_ddb_oid = NULL;
2578         g_ddb_sysctl_flags = 0;
2579
2580         if (retval) {
2581                 if (req.oldptr && req.oldidx > req.validlen)
2582                         *retval = req.validlen;
2583                 else
2584                         *retval = req.oldidx;
2585         }
2586         return (error);
2587 }
2588
2589 /*
2590  * Show a sysctl's name
2591  */
2592 static void
2593 db_show_oid_name(int *oid, size_t nlen)
2594 {
2595         struct sysctl_oid *oidp;
2596         int qoid[CTL_MAXNAME+2];
2597         int error;
2598
2599         qoid[0] = 0;
2600         memcpy(qoid + 2, oid, nlen * sizeof(int));
2601         qoid[1] = 1;
2602
2603         error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2604         if (error)
2605                 db_error("sysctl name oid");
2606
2607         error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2608         if (error)
2609                 db_error("sysctl name");
2610 }
2611
2612 /*
2613  * Check to see if an OID is safe to print from ddb.
2614  */
2615 static bool
2616 db_oid_safe(const struct sysctl_oid *oidp)
2617 {
2618         for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2619                 if (oidp->oid_handler == db_safe_handlers[i])
2620                         return (true);
2621         }
2622
2623         return (false);
2624 }
2625
2626 /*
2627  * Show a sysctl at a specific OID
2628  * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2629  */
2630 static int
2631 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2632 {
2633         int error, xflag, oflag, Nflag, nflag;
2634         size_t len;
2635
2636         xflag = flags & DB_SYSCTL_HEX;
2637         oflag = flags & DB_SYSCTL_OPAQUE;
2638         nflag = flags & DB_SYSCTL_VALUE_ONLY;
2639         Nflag = flags & DB_SYSCTL_NAME_ONLY;
2640
2641         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2642             (!xflag && !oflag))
2643                 return (0);
2644
2645         if (Nflag) {
2646                 db_show_oid_name(oid, nlen);
2647                 error = 0;
2648                 goto out;
2649         }
2650
2651         if (!nflag) {
2652                 db_show_oid_name(oid, nlen);
2653                 db_printf(": ");
2654         }
2655
2656         if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2657                 db_printf("Skipping, unsafe to print while recursing.");
2658                 error = 0;
2659                 goto out;
2660         }
2661
2662         /* Try once, and ask about the size */
2663         len = 0;
2664         error = db_sysctl(oidp, oid, nlen,
2665             NULL, NULL, &len, flags);
2666         if (error)
2667                 goto out;
2668
2669         if (!g_ddb_sysctl_printed)
2670                 /* Lie about the size */
2671                 error = db_sysctl(oidp, oid, nlen,
2672                     (void *) 1, &len, NULL, flags);
2673
2674 out:
2675         db_printf("\n");
2676         return (error);
2677 }
2678
2679 /*
2680  * Show all sysctls under a specific OID
2681  * Compare to sysctl_all from sbin/sysctl/sysctl.c
2682  */
2683 static int
2684 db_show_sysctl_all(int *oid, size_t len, int flags)
2685 {
2686         struct sysctl_oid *oidp;
2687         int name1[CTL_MAXNAME + 2], name2[CTL_MAXNAME + 2];
2688         size_t l1, l2;
2689
2690         name1[0] = CTL_SYSCTL;
2691         name1[1] = CTL_SYSCTL_NEXT;
2692         l1 = 2;
2693         if (len) {
2694                 memcpy(name1+2, oid, len * sizeof(int));
2695                 l1 +=len;
2696         } else {
2697                 name1[2] = 1;
2698                 l1++;
2699         }
2700         for (;;) {
2701                 int i, error;
2702
2703                 l2 = sizeof(name2);
2704                 error = kernel_sysctl(kdb_thread, name1, l1,
2705                     name2, &l2, NULL, 0, &l2, 0);
2706                 if (error != 0) {
2707                         if (error == ENOENT)
2708                                 return (0);
2709                         else
2710                                 db_error("sysctl(getnext)");
2711                 }
2712
2713                 l2 /= sizeof(int);
2714
2715                 if (l2 < (unsigned int)len)
2716                         return (0);
2717
2718                 for (i = 0; i < len; i++)
2719                         if (name2[i] != oid[i])
2720                                 return (0);
2721
2722                 /* Find the OID in question */
2723                 error = sysctl_find_oid(name2, l2, &oidp, NULL, NULL);
2724                 if (error)
2725                         return (error);
2726
2727                 i = db_show_oid(oidp, name2, l2, flags | DB_SYSCTL_SAFE_ONLY);
2728
2729                 if (db_pager_quit)
2730                         return (0);
2731
2732                 memcpy(name1+2, name2, l2 * sizeof(int));
2733                 l1 = 2 + l2;
2734         }
2735 }
2736
2737 /*
2738  * Show a sysctl by its user facing string
2739  */
2740 static int
2741 db_sysctlbyname(char *name, int flags)
2742 {
2743         struct sysctl_oid *oidp;
2744         int oid[CTL_MAXNAME];
2745         int error, nlen;
2746
2747         error = name2oid(name, oid, &nlen, &oidp);
2748         if (error) {
2749                 return (error);
2750         }
2751
2752         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2753                 db_show_sysctl_all(oid, nlen, flags);
2754         } else {
2755                 error = db_show_oid(oidp, oid, nlen, flags);
2756         }
2757
2758         return (error);
2759 }
2760
2761 static void
2762 db_sysctl_cmd_usage(void)
2763 {
2764         db_printf(
2765             " sysctl [/Nnox] <sysctl>                                       \n"
2766             "                                                               \n"
2767             " <sysctl> The name of the sysctl to show.                      \n"
2768             "                                                               \n"
2769             " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT.       \n"
2770             " This will work for most sysctls, but should not be used       \n"
2771             " with sysctls that are known to malloc.                        \n"
2772             "                                                               \n"
2773             " While recursing any \"unsafe\" sysctls will be skipped.       \n"
2774             " Call sysctl directly on the sysctl to try printing the        \n"
2775             " skipped sysctl. This is unsafe and may make the ddb           \n"
2776             " session unusable.                                             \n"
2777             "                                                               \n"
2778             " Arguments:                                                    \n"
2779             "   /N      Display only the name of the sysctl.                \n"
2780             "   /n      Display only the value of the sysctl.               \n"
2781             "   /o      Display opaque values.                              \n"
2782             "   /x      Display the sysctl in hex.                          \n"
2783             "                                                               \n"
2784             "For example:                                                   \n"
2785             "sysctl vm.v_free_min                                           \n"
2786             "vn.v_free_min: 12669                                           \n"
2787             );
2788 }
2789
2790 /*
2791  * Show a specific sysctl similar to sysctl (8).
2792  */
2793 DB_FUNC(sysctl, db_sysctl_cmd, db_cmd_table, CS_OWN, NULL)
2794 {
2795         char name[TOK_STRING_SIZE];
2796         int error, i, t, flags;
2797
2798         /* Parse the modifiers */
2799         t = db_read_token();
2800         if (t == tSLASH || t == tMINUS) {
2801                 t = db_read_token();
2802                 if (t != tIDENT) {
2803                         db_printf("Bad modifier\n");
2804                         error = EINVAL;
2805                         goto out;
2806                 }
2807                 db_strcpy(modif, db_tok_string);
2808         }
2809         else {
2810                 db_unread_token(t);
2811                 modif[0] = '\0';
2812         }
2813
2814         flags = 0;
2815         for (i = 0; i < nitems(db_sysctl_modifs); i++) {
2816                 if (strchr(modif, db_sysctl_modifs[i])) {
2817                         flags |= db_sysctl_modif_values[i];
2818                 }
2819         }
2820
2821         /* Parse the sysctl names */
2822         t = db_read_token();
2823         if (t != tIDENT) {
2824                 db_printf("Need sysctl name\n");
2825                 error = EINVAL;
2826                 goto out;
2827         }
2828
2829         /* Copy the name into a temporary buffer */
2830         db_strcpy(name, db_tok_string);
2831
2832         /* Ensure there is no trailing cruft */
2833         t = db_read_token();
2834         if (t != tEOL) {
2835                 db_printf("Unexpected sysctl argument\n");
2836                 error = EINVAL;
2837                 goto out;
2838         }
2839
2840         error = db_sysctlbyname(name, flags);
2841         if (error == ENOENT) {
2842                 db_printf("unknown oid: '%s'\n", db_tok_string);
2843                 goto out;
2844         } else if (error) {
2845                 db_printf("%s: error: %d\n", db_tok_string, error);
2846                 goto out;
2847         }
2848
2849 out:
2850         /* Ensure we eat all of our text */
2851         db_flush_lex();
2852
2853         if (error == EINVAL) {
2854                 db_sysctl_cmd_usage();
2855         }
2856 }
2857
2858 #endif /* DDB */