]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_sysctl.c
Merge ^/vendor/llvm/dist up to its last change, and resolve conflicts.
[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
741 static int
742 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
743 {
744         struct sysctl_oid *p, *tmp;
745         int error;
746
747         SYSCTL_ASSERT_WLOCKED();
748         if (oidp == NULL)
749                 return(EINVAL);
750         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
751                 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
752                     oidp->oid_name);
753                 return (EINVAL);
754         }
755         /*
756          * WARNING: normal method to do this should be through
757          * sysctl_ctx_free(). Use recursing as the last resort
758          * method to purge your sysctl tree of leftovers...
759          * However, if some other code still references these nodes,
760          * it will panic.
761          */
762         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
763                 if (oidp->oid_refcnt == 1) {
764                         SLIST_FOREACH_SAFE(p,
765                             SYSCTL_CHILDREN(oidp), oid_link, tmp) {
766                                 if (!recurse) {
767                                         printf("Warning: failed attempt to "
768                                             "remove oid %s with child %s\n",
769                                             oidp->oid_name, p->oid_name);
770                                         return (ENOTEMPTY);
771                                 }
772                                 error = sysctl_remove_oid_locked(p, del,
773                                     recurse);
774                                 if (error)
775                                         return (error);
776                         }
777                 }
778         }
779         if (oidp->oid_refcnt > 1 ) {
780                 oidp->oid_refcnt--;
781         } else {
782                 if (oidp->oid_refcnt == 0) {
783                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
784                                 oidp->oid_refcnt, oidp->oid_name);
785                         return (EINVAL);
786                 }
787                 sysctl_unregister_oid(oidp);
788                 if (del) {
789                         /*
790                          * Wait for all threads running the handler to drain.
791                          * This preserves the previous behavior when the
792                          * sysctl lock was held across a handler invocation,
793                          * and is necessary for module unload correctness.
794                          */
795                         while (oidp->oid_running > 0) {
796                                 oidp->oid_kind |= CTLFLAG_DYING;
797                                 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
798                         }
799                         if (oidp->oid_descr)
800                                 free(__DECONST(char *, oidp->oid_descr),
801                                     M_SYSCTLOID);
802                         if (oidp->oid_label)
803                                 free(__DECONST(char *, oidp->oid_label),
804                                     M_SYSCTLOID);
805                         free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
806                         free(oidp, M_SYSCTLOID);
807                 }
808         }
809         return (0);
810 }
811 /*
812  * Create new sysctls at run time.
813  * clist may point to a valid context initialized with sysctl_ctx_init().
814  */
815 struct sysctl_oid *
816 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
817         int number, const char *name, int kind, void *arg1, intmax_t arg2,
818         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
819         const char *label)
820 {
821         struct sysctl_oid *oidp;
822
823         /* You have to hook up somewhere.. */
824         if (parent == NULL)
825                 return(NULL);
826         /* Check if the node already exists, otherwise create it */
827         SYSCTL_WLOCK();
828         oidp = sysctl_find_oidname(name, parent);
829         if (oidp != NULL) {
830                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
831                         oidp->oid_refcnt++;
832                         /* Update the context */
833                         if (clist != NULL)
834                                 sysctl_ctx_entry_add(clist, oidp);
835                         SYSCTL_WUNLOCK();
836                         return (oidp);
837                 } else {
838                         sysctl_warn_reuse(__func__, oidp);
839                         SYSCTL_WUNLOCK();
840                         return (NULL);
841                 }
842         }
843         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
844         oidp->oid_parent = parent;
845         SLIST_INIT(&oidp->oid_children);
846         oidp->oid_number = number;
847         oidp->oid_refcnt = 1;
848         oidp->oid_name = strdup(name, M_SYSCTLOID);
849         oidp->oid_handler = handler;
850         oidp->oid_kind = CTLFLAG_DYN | kind;
851         oidp->oid_arg1 = arg1;
852         oidp->oid_arg2 = arg2;
853         oidp->oid_fmt = fmt;
854         if (descr != NULL)
855                 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
856         if (label != NULL)
857                 oidp->oid_label = strdup(label, M_SYSCTLOID);
858         /* Update the context, if used */
859         if (clist != NULL)
860                 sysctl_ctx_entry_add(clist, oidp);
861         /* Register this oid */
862         sysctl_register_oid(oidp);
863         SYSCTL_WUNLOCK();
864         return (oidp);
865 }
866
867 /*
868  * Rename an existing oid.
869  */
870 void
871 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
872 {
873         char *newname;
874         char *oldname;
875
876         newname = strdup(name, M_SYSCTLOID);
877         SYSCTL_WLOCK();
878         oldname = __DECONST(char *, oidp->oid_name);
879         oidp->oid_name = newname;
880         SYSCTL_WUNLOCK();
881         free(oldname, M_SYSCTLOID);
882 }
883
884 /*
885  * Reparent an existing oid.
886  */
887 int
888 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
889 {
890         struct sysctl_oid *oidp;
891
892         SYSCTL_WLOCK();
893         if (oid->oid_parent == parent) {
894                 SYSCTL_WUNLOCK();
895                 return (0);
896         }
897         oidp = sysctl_find_oidname(oid->oid_name, parent);
898         if (oidp != NULL) {
899                 SYSCTL_WUNLOCK();
900                 return (EEXIST);
901         }
902         sysctl_unregister_oid(oid);
903         oid->oid_parent = parent;
904         oid->oid_number = OID_AUTO;
905         sysctl_register_oid(oid);
906         SYSCTL_WUNLOCK();
907         return (0);
908 }
909
910 /*
911  * Register the kernel's oids on startup.
912  */
913 SET_DECLARE(sysctl_set, struct sysctl_oid);
914
915 static void
916 sysctl_register_all(void *arg)
917 {
918         struct sysctl_oid **oidp;
919
920         sx_init(&sysctlmemlock, "sysctl mem");
921         SYSCTL_INIT();
922         SYSCTL_WLOCK();
923         SET_FOREACH(oidp, sysctl_set)
924                 sysctl_register_oid(*oidp);
925         SYSCTL_WUNLOCK();
926 }
927 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
928
929 /*
930  * "Staff-functions"
931  *
932  * These functions implement a presently undocumented interface 
933  * used by the sysctl program to walk the tree, and get the type
934  * so it can print the value.
935  * This interface is under work and consideration, and should probably
936  * be killed with a big axe by the first person who can find the time.
937  * (be aware though, that the proper interface isn't as obvious as it
938  * may seem, there are various conflicting requirements.
939  *
940  * {CTL_SYSCTL, CTL_SYSCTL_DEBUG}               printf the entire MIB-tree.
941  * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...}           return the name of the "..."
942  *                                              OID.
943  * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...}           return the next OID.
944  * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID}            return the OID of the name in
945  *                                              "new"
946  * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...}         return the kind & format info
947  *                                              for the "..." OID.
948  * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...}       return the description of the
949  *                                              "..." OID.
950  * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...}       return the aggregation label of
951  *                                              the "..." OID.
952  */
953
954 #ifdef SYSCTL_DEBUG
955 static void
956 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
957 {
958         int k;
959         struct sysctl_oid *oidp;
960
961         SYSCTL_ASSERT_LOCKED();
962         SLIST_FOREACH(oidp, l, oid_link) {
963
964                 for (k=0; k<i; k++)
965                         printf(" ");
966
967                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
968
969                 printf("%c%c",
970                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
971                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
972
973                 if (oidp->oid_handler)
974                         printf(" *Handler");
975
976                 switch (oidp->oid_kind & CTLTYPE) {
977                         case CTLTYPE_NODE:
978                                 printf(" Node\n");
979                                 if (!oidp->oid_handler) {
980                                         sysctl_sysctl_debug_dump_node(
981                                             SYSCTL_CHILDREN(oidp), i + 2);
982                                 }
983                                 break;
984                         case CTLTYPE_INT:    printf(" Int\n"); break;
985                         case CTLTYPE_UINT:   printf(" u_int\n"); break;
986                         case CTLTYPE_LONG:   printf(" Long\n"); break;
987                         case CTLTYPE_ULONG:  printf(" u_long\n"); break;
988                         case CTLTYPE_STRING: printf(" String\n"); break;
989                         case CTLTYPE_S8:     printf(" int8_t\n"); break;
990                         case CTLTYPE_S16:    printf(" int16_t\n"); break;
991                         case CTLTYPE_S32:    printf(" int32_t\n"); break;
992                         case CTLTYPE_S64:    printf(" int64_t\n"); break;
993                         case CTLTYPE_U8:     printf(" uint8_t\n"); break;
994                         case CTLTYPE_U16:    printf(" uint16_t\n"); break;
995                         case CTLTYPE_U32:    printf(" uint32_t\n"); break;
996                         case CTLTYPE_U64:    printf(" uint64_t\n"); break;
997                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
998                         default:             printf("\n");
999                 }
1000
1001         }
1002 }
1003
1004 static int
1005 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1006 {
1007         struct rm_priotracker tracker;
1008         int error;
1009
1010         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1011         if (error)
1012                 return (error);
1013         SYSCTL_RLOCK(&tracker);
1014         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1015         SYSCTL_RUNLOCK(&tracker);
1016         return (ENOENT);
1017 }
1018
1019 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1020     CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1021 #endif
1022
1023 static int
1024 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1025 {
1026         int *name = (int *) arg1;
1027         u_int namelen = arg2;
1028         int error;
1029         struct sysctl_oid *oid;
1030         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1031         struct rm_priotracker tracker;
1032         char buf[10];
1033
1034         error = sysctl_wire_old_buffer(req, 0);
1035         if (error)
1036                 return (error);
1037
1038         SYSCTL_RLOCK(&tracker);
1039         while (namelen) {
1040                 if (!lsp) {
1041                         snprintf(buf,sizeof(buf),"%d",*name);
1042                         if (req->oldidx)
1043                                 error = SYSCTL_OUT(req, ".", 1);
1044                         if (!error)
1045                                 error = SYSCTL_OUT(req, buf, strlen(buf));
1046                         if (error)
1047                                 goto out;
1048                         namelen--;
1049                         name++;
1050                         continue;
1051                 }
1052                 lsp2 = NULL;
1053                 SLIST_FOREACH(oid, lsp, oid_link) {
1054                         if (oid->oid_number != *name)
1055                                 continue;
1056
1057                         if (req->oldidx)
1058                                 error = SYSCTL_OUT(req, ".", 1);
1059                         if (!error)
1060                                 error = SYSCTL_OUT(req, oid->oid_name,
1061                                         strlen(oid->oid_name));
1062                         if (error)
1063                                 goto out;
1064
1065                         namelen--;
1066                         name++;
1067
1068                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
1069                                 break;
1070
1071                         if (oid->oid_handler)
1072                                 break;
1073
1074                         lsp2 = SYSCTL_CHILDREN(oid);
1075                         break;
1076                 }
1077                 lsp = lsp2;
1078         }
1079         error = SYSCTL_OUT(req, "", 1);
1080  out:
1081         SYSCTL_RUNLOCK(&tracker);
1082         return (error);
1083 }
1084
1085 /*
1086  * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1087  * capability mode.
1088  */
1089 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1090     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1091
1092 static int
1093 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
1094         int *next, int *len, int level, struct sysctl_oid **oidpp)
1095 {
1096         struct sysctl_oid *oidp;
1097
1098         SYSCTL_ASSERT_LOCKED();
1099         *len = level;
1100         SLIST_FOREACH(oidp, lsp, oid_link) {
1101                 *next = oidp->oid_number;
1102                 *oidpp = oidp;
1103
1104                 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1105                         continue;
1106
1107                 if (!namelen) {
1108                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
1109                                 return (0);
1110                         if (oidp->oid_handler) 
1111                                 /* We really should call the handler here...*/
1112                                 return (0);
1113                         lsp = SYSCTL_CHILDREN(oidp);
1114                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
1115                                 len, level+1, oidpp))
1116                                 return (0);
1117                         goto emptynode;
1118                 }
1119
1120                 if (oidp->oid_number < *name)
1121                         continue;
1122
1123                 if (oidp->oid_number > *name) {
1124                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1125                                 return (0);
1126                         if (oidp->oid_handler)
1127                                 return (0);
1128                         lsp = SYSCTL_CHILDREN(oidp);
1129                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
1130                                 next+1, len, level+1, oidpp))
1131                                 return (0);
1132                         goto next;
1133                 }
1134                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1135                         continue;
1136
1137                 if (oidp->oid_handler)
1138                         continue;
1139
1140                 lsp = SYSCTL_CHILDREN(oidp);
1141                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
1142                         len, level+1, oidpp))
1143                         return (0);
1144         next:
1145                 namelen = 1;
1146         emptynode:
1147                 *len = level;
1148         }
1149         return (1);
1150 }
1151
1152 static int
1153 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1154 {
1155         int *name = (int *) arg1;
1156         u_int namelen = arg2;
1157         int i, j, error;
1158         struct sysctl_oid *oid;
1159         struct sysctl_oid_list *lsp = &sysctl__children;
1160         struct rm_priotracker tracker;
1161         int newoid[CTL_MAXNAME];
1162
1163         SYSCTL_RLOCK(&tracker);
1164         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1165         SYSCTL_RUNLOCK(&tracker);
1166         if (i)
1167                 return (ENOENT);
1168         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1169         return (error);
1170 }
1171
1172 /*
1173  * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1174  * capability mode.
1175  */
1176 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1177     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1178
1179 static int
1180 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1181 {
1182         struct sysctl_oid *oidp;
1183         struct sysctl_oid_list *lsp = &sysctl__children;
1184         char *p;
1185
1186         SYSCTL_ASSERT_LOCKED();
1187
1188         for (*len = 0; *len < CTL_MAXNAME;) {
1189                 p = strsep(&name, ".");
1190
1191                 oidp = SLIST_FIRST(lsp);
1192                 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1193                         if (oidp == NULL)
1194                                 return (ENOENT);
1195                         if (strcmp(p, oidp->oid_name) == 0)
1196                                 break;
1197                 }
1198                 *oid++ = oidp->oid_number;
1199                 (*len)++;
1200
1201                 if (name == NULL || *name == '\0') {
1202                         if (oidpp)
1203                                 *oidpp = oidp;
1204                         return (0);
1205                 }
1206
1207                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1208                         break;
1209
1210                 if (oidp->oid_handler)
1211                         break;
1212
1213                 lsp = SYSCTL_CHILDREN(oidp);
1214         }
1215         return (ENOENT);
1216 }
1217
1218 static int
1219 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1220 {
1221         char *p;
1222         int error, oid[CTL_MAXNAME], len = 0;
1223         struct sysctl_oid *op = NULL;
1224         struct rm_priotracker tracker;
1225         char buf[32];
1226
1227         if (!req->newlen) 
1228                 return (ENOENT);
1229         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
1230                 return (ENAMETOOLONG);
1231
1232         p = buf;
1233         if (req->newlen >= sizeof(buf))
1234                 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1235
1236         error = SYSCTL_IN(req, p, req->newlen);
1237         if (error) {
1238                 if (p != buf)
1239                         free(p, M_SYSCTL);
1240                 return (error);
1241         }
1242
1243         p [req->newlen] = '\0';
1244
1245         SYSCTL_RLOCK(&tracker);
1246         error = name2oid(p, oid, &len, &op);
1247         SYSCTL_RUNLOCK(&tracker);
1248
1249         if (p != buf)
1250                 free(p, M_SYSCTL);
1251
1252         if (error)
1253                 return (error);
1254
1255         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1256         return (error);
1257 }
1258
1259 /*
1260  * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1261  * capability mode.
1262  */
1263 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1264     CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1265     sysctl_sysctl_name2oid, "I", "");
1266
1267 static int
1268 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1269 {
1270         struct sysctl_oid *oid;
1271         struct rm_priotracker tracker;
1272         int error;
1273
1274         error = sysctl_wire_old_buffer(req, 0);
1275         if (error)
1276                 return (error);
1277
1278         SYSCTL_RLOCK(&tracker);
1279         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1280         if (error)
1281                 goto out;
1282
1283         if (oid->oid_fmt == NULL) {
1284                 error = ENOENT;
1285                 goto out;
1286         }
1287         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1288         if (error)
1289                 goto out;
1290         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1291  out:
1292         SYSCTL_RUNLOCK(&tracker);
1293         return (error);
1294 }
1295
1296
1297 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1298     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1299
1300 static int
1301 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1302 {
1303         struct sysctl_oid *oid;
1304         struct rm_priotracker tracker;
1305         int error;
1306
1307         error = sysctl_wire_old_buffer(req, 0);
1308         if (error)
1309                 return (error);
1310
1311         SYSCTL_RLOCK(&tracker);
1312         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1313         if (error)
1314                 goto out;
1315
1316         if (oid->oid_descr == NULL) {
1317                 error = ENOENT;
1318                 goto out;
1319         }
1320         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1321  out:
1322         SYSCTL_RUNLOCK(&tracker);
1323         return (error);
1324 }
1325
1326 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1327     CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1328
1329 static int
1330 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1331 {
1332         struct sysctl_oid *oid;
1333         struct rm_priotracker tracker;
1334         int error;
1335
1336         error = sysctl_wire_old_buffer(req, 0);
1337         if (error)
1338                 return (error);
1339
1340         SYSCTL_RLOCK(&tracker);
1341         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1342         if (error)
1343                 goto out;
1344
1345         if (oid->oid_label == NULL) {
1346                 error = ENOENT;
1347                 goto out;
1348         }
1349         error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1350  out:
1351         SYSCTL_RUNLOCK(&tracker);
1352         return (error);
1353 }
1354
1355 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1356     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1357
1358 /*
1359  * Default "handler" functions.
1360  */
1361
1362 /*
1363  * Handle a bool.
1364  * Two cases:
1365  *     a variable:  point arg1 at it.
1366  *     a constant:  pass it in arg2.
1367  */
1368
1369 int
1370 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1371 {
1372         uint8_t temp;
1373         int error;
1374
1375         /*
1376          * Attempt to get a coherent snapshot by making a copy of the data.
1377          */
1378         if (arg1)
1379                 temp = *(bool *)arg1 ? 1 : 0;
1380         else
1381                 temp = arg2 ? 1 : 0;
1382
1383         error = SYSCTL_OUT(req, &temp, sizeof(temp));
1384         if (error || !req->newptr)
1385                 return (error);
1386
1387         if (!arg1)
1388                 error = EPERM;
1389         else {
1390                 error = SYSCTL_IN(req, &temp, sizeof(temp));
1391                 if (!error)
1392                         *(bool *)arg1 = temp ? 1 : 0;
1393         }
1394         return (error);
1395 }
1396
1397 /*
1398  * Handle an int8_t, signed or unsigned.
1399  * Two cases:
1400  *     a variable:  point arg1 at it.
1401  *     a constant:  pass it in arg2.
1402  */
1403
1404 int
1405 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1406 {
1407         int8_t tmpout;
1408         int error = 0;
1409
1410         /*
1411          * Attempt to get a coherent snapshot by making a copy of the data.
1412          */
1413         if (arg1)
1414                 tmpout = *(int8_t *)arg1;
1415         else
1416                 tmpout = arg2;
1417         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1418
1419         if (error || !req->newptr)
1420                 return (error);
1421
1422         if (!arg1)
1423                 error = EPERM;
1424         else
1425                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1426         return (error);
1427 }
1428
1429 /*
1430  * Handle an int16_t, signed or unsigned.
1431  * Two cases:
1432  *     a variable:  point arg1 at it.
1433  *     a constant:  pass it in arg2.
1434  */
1435
1436 int
1437 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1438 {
1439         int16_t tmpout;
1440         int error = 0;
1441
1442         /*
1443          * Attempt to get a coherent snapshot by making a copy of the data.
1444          */
1445         if (arg1)
1446                 tmpout = *(int16_t *)arg1;
1447         else
1448                 tmpout = arg2;
1449         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1450
1451         if (error || !req->newptr)
1452                 return (error);
1453
1454         if (!arg1)
1455                 error = EPERM;
1456         else
1457                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1458         return (error);
1459 }
1460
1461 /*
1462  * Handle an int32_t, signed or unsigned.
1463  * Two cases:
1464  *     a variable:  point arg1 at it.
1465  *     a constant:  pass it in arg2.
1466  */
1467
1468 int
1469 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1470 {
1471         int32_t tmpout;
1472         int error = 0;
1473
1474         /*
1475          * Attempt to get a coherent snapshot by making a copy of the data.
1476          */
1477         if (arg1)
1478                 tmpout = *(int32_t *)arg1;
1479         else
1480                 tmpout = arg2;
1481         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1482
1483         if (error || !req->newptr)
1484                 return (error);
1485
1486         if (!arg1)
1487                 error = EPERM;
1488         else
1489                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1490         return (error);
1491 }
1492
1493 /*
1494  * Handle an int, signed or unsigned.
1495  * Two cases:
1496  *     a variable:  point arg1 at it.
1497  *     a constant:  pass it in arg2.
1498  */
1499
1500 int
1501 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1502 {
1503         int tmpout, error = 0;
1504
1505         /*
1506          * Attempt to get a coherent snapshot by making a copy of the data.
1507          */
1508         if (arg1)
1509                 tmpout = *(int *)arg1;
1510         else
1511                 tmpout = arg2;
1512         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1513
1514         if (error || !req->newptr)
1515                 return (error);
1516
1517         if (!arg1)
1518                 error = EPERM;
1519         else
1520                 error = SYSCTL_IN(req, arg1, sizeof(int));
1521         return (error);
1522 }
1523
1524 /*
1525  * Based on on sysctl_handle_int() convert milliseconds into ticks.
1526  * Note: this is used by TCP.
1527  */
1528
1529 int
1530 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1531 {
1532         int error, s, tt;
1533
1534         tt = *(int *)arg1;
1535         s = (int)((int64_t)tt * 1000 / hz);
1536
1537         error = sysctl_handle_int(oidp, &s, 0, req);
1538         if (error || !req->newptr)
1539                 return (error);
1540
1541         tt = (int)((int64_t)s * hz / 1000);
1542         if (tt < 1)
1543                 return (EINVAL);
1544
1545         *(int *)arg1 = tt;
1546         return (0);
1547 }
1548
1549
1550 /*
1551  * Handle a long, signed or unsigned.
1552  * Two cases:
1553  *     a variable:  point arg1 at it.
1554  *     a constant:  pass it in arg2.
1555  */
1556
1557 int
1558 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1559 {
1560         int error = 0;
1561         long tmplong;
1562 #ifdef SCTL_MASK32
1563         int tmpint;
1564 #endif
1565
1566         /*
1567          * Attempt to get a coherent snapshot by making a copy of the data.
1568          */
1569         if (arg1)
1570                 tmplong = *(long *)arg1;
1571         else
1572                 tmplong = arg2;
1573 #ifdef SCTL_MASK32
1574         if (req->flags & SCTL_MASK32) {
1575                 tmpint = tmplong;
1576                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1577         } else
1578 #endif
1579                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1580
1581         if (error || !req->newptr)
1582                 return (error);
1583
1584         if (!arg1)
1585                 error = EPERM;
1586 #ifdef SCTL_MASK32
1587         else if (req->flags & SCTL_MASK32) {
1588                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1589                 *(long *)arg1 = (long)tmpint;
1590         }
1591 #endif
1592         else
1593                 error = SYSCTL_IN(req, arg1, sizeof(long));
1594         return (error);
1595 }
1596
1597 /*
1598  * Handle a 64 bit int, signed or unsigned.
1599  * Two cases:
1600  *     a variable:  point arg1 at it.
1601  *     a constant:  pass it in arg2.
1602  */
1603 int
1604 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1605 {
1606         int error = 0;
1607         uint64_t tmpout;
1608
1609         /*
1610          * Attempt to get a coherent snapshot by making a copy of the data.
1611          */
1612         if (arg1)
1613                 tmpout = *(uint64_t *)arg1;
1614         else
1615                 tmpout = arg2;
1616         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1617
1618         if (error || !req->newptr)
1619                 return (error);
1620
1621         if (!arg1)
1622                 error = EPERM;
1623         else
1624                 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1625         return (error);
1626 }
1627
1628 /*
1629  * Handle our generic '\0' terminated 'C' string.
1630  * Two cases:
1631  *      a variable string:  point arg1 at it, arg2 is max length.
1632  *      a constant string:  point arg1 at it, arg2 is zero.
1633  */
1634
1635 int
1636 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1637 {
1638         size_t outlen;
1639         int error = 0, ro_string = 0;
1640
1641         /*
1642          * A zero-length buffer indicates a fixed size read-only
1643          * string.  In ddb, don't worry about trying to make a malloced
1644          * snapshot.
1645          */
1646         if (arg2 == 0 || kdb_active) {
1647                 arg2 = strlen((char *)arg1) + 1;
1648                 ro_string = 1;
1649         }
1650
1651         if (req->oldptr != NULL) {
1652                 char *tmparg;
1653
1654                 if (ro_string) {
1655                         tmparg = arg1;
1656                 } else {
1657                         /* try to make a coherent snapshot of the string */
1658                         tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1659                         memcpy(tmparg, arg1, arg2);
1660                 }
1661
1662                 outlen = strnlen(tmparg, arg2 - 1) + 1;
1663                 error = SYSCTL_OUT(req, tmparg, outlen);
1664
1665                 if (!ro_string)
1666                         free(tmparg, M_SYSCTLTMP);
1667         } else {
1668                 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1669                 error = SYSCTL_OUT(req, NULL, outlen);
1670         }
1671         if (error || !req->newptr)
1672                 return (error);
1673
1674         if ((req->newlen - req->newidx) >= arg2) {
1675                 error = EINVAL;
1676         } else {
1677                 arg2 = (req->newlen - req->newidx);
1678                 error = SYSCTL_IN(req, arg1, arg2);
1679                 ((char *)arg1)[arg2] = '\0';
1680         }
1681         return (error);
1682 }
1683
1684 /*
1685  * Handle any kind of opaque data.
1686  * arg1 points to it, arg2 is the size.
1687  */
1688
1689 int
1690 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1691 {
1692         int error, tries;
1693         u_int generation;
1694         struct sysctl_req req2;
1695
1696         /*
1697          * Attempt to get a coherent snapshot, by using the thread
1698          * pre-emption counter updated from within mi_switch() to
1699          * determine if we were pre-empted during a bcopy() or
1700          * copyout(). Make 3 attempts at doing this before giving up.
1701          * If we encounter an error, stop immediately.
1702          */
1703         tries = 0;
1704         req2 = *req;
1705 retry:
1706         generation = curthread->td_generation;
1707         error = SYSCTL_OUT(req, arg1, arg2);
1708         if (error)
1709                 return (error);
1710         tries++;
1711         if (generation != curthread->td_generation && tries < 3) {
1712                 *req = req2;
1713                 goto retry;
1714         }
1715
1716         error = SYSCTL_IN(req, arg1, arg2);
1717
1718         return (error);
1719 }
1720
1721 /*
1722  * Based on on sysctl_handle_int() convert microseconds to a sbintime.
1723  */
1724 int
1725 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
1726 {
1727         int error;
1728         int64_t tt;
1729         sbintime_t sb;
1730
1731         tt = *(int64_t *)arg1;
1732         sb = sbttous(tt);
1733
1734         error = sysctl_handle_64(oidp, &sb, 0, req);
1735         if (error || !req->newptr)
1736                 return (error);
1737
1738         tt = ustosbt(sb);
1739         *(int64_t *)arg1 = tt;
1740
1741         return (0);
1742 }
1743
1744 /*
1745  * Based on on sysctl_handle_int() convert milliseconds to a sbintime.
1746  */
1747 int
1748 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
1749 {
1750         int error;
1751         int64_t tt;
1752         sbintime_t sb;
1753
1754         tt = *(int64_t *)arg1;
1755         sb = sbttoms(tt);
1756
1757         error = sysctl_handle_64(oidp, &sb, 0, req);
1758         if (error || !req->newptr)
1759                 return (error);
1760
1761         tt = mstosbt(sb);
1762         *(int64_t *)arg1 = tt;
1763
1764         return (0);
1765 }
1766
1767 /*
1768  * Convert seconds to a struct timeval.  Intended for use with
1769  * intervals and thus does not permit negative seconds.
1770  */
1771 int
1772 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1773 {
1774         struct timeval *tv;
1775         int error, secs;
1776
1777         tv = arg1;
1778         secs = tv->tv_sec;
1779
1780         error = sysctl_handle_int(oidp, &secs, 0, req);
1781         if (error || req->newptr == NULL)
1782                 return (error);
1783
1784         if (secs < 0)
1785                 return (EINVAL);
1786         tv->tv_sec = secs;
1787
1788         return (0);
1789 }
1790
1791 /*
1792  * Transfer functions to/from kernel space.
1793  * XXX: rather untested at this point
1794  */
1795 static int
1796 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1797 {
1798         size_t i = 0;
1799
1800         if (req->oldptr) {
1801                 i = l;
1802                 if (req->oldlen <= req->oldidx)
1803                         i = 0;
1804                 else
1805                         if (i > req->oldlen - req->oldidx)
1806                                 i = req->oldlen - req->oldidx;
1807                 if (i > 0)
1808                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
1809         }
1810         req->oldidx += l;
1811         if (req->oldptr && i != l)
1812                 return (ENOMEM);
1813         return (0);
1814 }
1815
1816 static int
1817 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1818 {
1819         if (!req->newptr)
1820                 return (0);
1821         if (req->newlen - req->newidx < l)
1822                 return (EINVAL);
1823         bcopy((const char *)req->newptr + req->newidx, p, l);
1824         req->newidx += l;
1825         return (0);
1826 }
1827
1828 int
1829 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1830     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1831 {
1832         int error = 0;
1833         struct sysctl_req req;
1834
1835         bzero(&req, sizeof req);
1836
1837         req.td = td;
1838         req.flags = flags;
1839
1840         if (oldlenp) {
1841                 req.oldlen = *oldlenp;
1842         }
1843         req.validlen = req.oldlen;
1844
1845         if (old) {
1846                 req.oldptr= old;
1847         }
1848
1849         if (new != NULL) {
1850                 req.newlen = newlen;
1851                 req.newptr = new;
1852         }
1853
1854         req.oldfunc = sysctl_old_kernel;
1855         req.newfunc = sysctl_new_kernel;
1856         req.lock = REQ_UNWIRED;
1857
1858         error = sysctl_root(0, name, namelen, &req);
1859
1860         if (req.lock == REQ_WIRED && req.validlen > 0)
1861                 vsunlock(req.oldptr, req.validlen);
1862
1863         if (error && error != ENOMEM)
1864                 return (error);
1865
1866         if (retval) {
1867                 if (req.oldptr && req.oldidx > req.validlen)
1868                         *retval = req.validlen;
1869                 else
1870                         *retval = req.oldidx;
1871         }
1872         return (error);
1873 }
1874
1875 int
1876 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1877     void *new, size_t newlen, size_t *retval, int flags)
1878 {
1879         int oid[CTL_MAXNAME];
1880         size_t oidlen, plen;
1881         int error;
1882
1883         oid[0] = CTL_SYSCTL;
1884         oid[1] = CTL_SYSCTL_NAME2OID;
1885         oidlen = sizeof(oid);
1886
1887         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1888             (void *)name, strlen(name), &plen, flags);
1889         if (error)
1890                 return (error);
1891
1892         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1893             new, newlen, retval, flags);
1894         return (error);
1895 }
1896
1897 /*
1898  * Transfer function to/from user space.
1899  */
1900 static int
1901 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1902 {
1903         size_t i, len, origidx;
1904         int error;
1905
1906         origidx = req->oldidx;
1907         req->oldidx += l;
1908         if (req->oldptr == NULL)
1909                 return (0);
1910         /*
1911          * If we have not wired the user supplied buffer and we are currently
1912          * holding locks, drop a witness warning, as it's possible that
1913          * write operations to the user page can sleep.
1914          */
1915         if (req->lock != REQ_WIRED)
1916                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1917                     "sysctl_old_user()");
1918         i = l;
1919         len = req->validlen;
1920         if (len <= origidx)
1921                 i = 0;
1922         else {
1923                 if (i > len - origidx)
1924                         i = len - origidx;
1925                 if (req->lock == REQ_WIRED) {
1926                         error = copyout_nofault(p, (char *)req->oldptr +
1927                             origidx, i);
1928                 } else
1929                         error = copyout(p, (char *)req->oldptr + origidx, i);
1930                 if (error != 0)
1931                         return (error);
1932         }
1933         if (i < l)
1934                 return (ENOMEM);
1935         return (0);
1936 }
1937
1938 static int
1939 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1940 {
1941         int error;
1942
1943         if (!req->newptr)
1944                 return (0);
1945         if (req->newlen - req->newidx < l)
1946                 return (EINVAL);
1947         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1948             "sysctl_new_user()");
1949         error = copyin((const char *)req->newptr + req->newidx, p, l);
1950         req->newidx += l;
1951         return (error);
1952 }
1953
1954 /*
1955  * Wire the user space destination buffer.  If set to a value greater than
1956  * zero, the len parameter limits the maximum amount of wired memory.
1957  */
1958 int
1959 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1960 {
1961         int ret;
1962         size_t wiredlen;
1963
1964         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1965         ret = 0;
1966         if (req->lock != REQ_WIRED && req->oldptr &&
1967             req->oldfunc == sysctl_old_user) {
1968                 if (wiredlen != 0) {
1969                         ret = vslock(req->oldptr, wiredlen);
1970                         if (ret != 0) {
1971                                 if (ret != ENOMEM)
1972                                         return (ret);
1973                                 wiredlen = 0;
1974                         }
1975                 }
1976                 req->lock = REQ_WIRED;
1977                 req->validlen = wiredlen;
1978         }
1979         return (0);
1980 }
1981
1982 int
1983 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1984     int *nindx, struct sysctl_req *req)
1985 {
1986         struct sysctl_oid_list *lsp;
1987         struct sysctl_oid *oid;
1988         int indx;
1989
1990         SYSCTL_ASSERT_LOCKED();
1991         lsp = &sysctl__children;
1992         indx = 0;
1993         while (indx < CTL_MAXNAME) {
1994                 SLIST_FOREACH(oid, lsp, oid_link) {
1995                         if (oid->oid_number == name[indx])
1996                                 break;
1997                 }
1998                 if (oid == NULL)
1999                         return (ENOENT);
2000
2001                 indx++;
2002                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2003                         if (oid->oid_handler != NULL || indx == namelen) {
2004                                 *noid = oid;
2005                                 if (nindx != NULL)
2006                                         *nindx = indx;
2007                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2008                                     ("%s found DYING node %p", __func__, oid));
2009                                 return (0);
2010                         }
2011                         lsp = SYSCTL_CHILDREN(oid);
2012                 } else if (indx == namelen) {
2013                         if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2014                                 return (ENOENT);
2015                         *noid = oid;
2016                         if (nindx != NULL)
2017                                 *nindx = indx;
2018                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2019                             ("%s found DYING node %p", __func__, oid));
2020                         return (0);
2021                 } else {
2022                         return (ENOTDIR);
2023                 }
2024         }
2025         return (ENOENT);
2026 }
2027
2028 /*
2029  * Traverse our tree, and find the right node, execute whatever it points
2030  * to, and return the resulting error code.
2031  */
2032
2033 static int
2034 sysctl_root(SYSCTL_HANDLER_ARGS)
2035 {
2036         struct sysctl_oid *oid;
2037         struct rm_priotracker tracker;
2038         int error, indx, lvl;
2039
2040         SYSCTL_RLOCK(&tracker);
2041
2042         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2043         if (error)
2044                 goto out;
2045
2046         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2047                 /*
2048                  * You can't call a sysctl when it's a node, but has
2049                  * no handler.  Inform the user that it's a node.
2050                  * The indx may or may not be the same as namelen.
2051                  */
2052                 if (oid->oid_handler == NULL) {
2053                         error = EISDIR;
2054                         goto out;
2055                 }
2056         }
2057
2058         /* Is this sysctl writable? */
2059         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2060                 error = EPERM;
2061                 goto out;
2062         }
2063
2064         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2065
2066 #ifdef CAPABILITY_MODE
2067         /*
2068          * If the process is in capability mode, then don't permit reading or
2069          * writing unless specifically granted for the node.
2070          */
2071         if (IN_CAPABILITY_MODE(req->td)) {
2072                 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2073                     (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2074                         error = EPERM;
2075                         goto out;
2076                 }
2077         }
2078 #endif
2079
2080         /* Is this sysctl sensitive to securelevels? */
2081         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2082                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2083                 error = securelevel_gt(req->td->td_ucred, lvl);
2084                 if (error)
2085                         goto out;
2086         }
2087
2088         /* Is this sysctl writable by only privileged users? */
2089         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2090                 int priv;
2091
2092                 if (oid->oid_kind & CTLFLAG_PRISON)
2093                         priv = PRIV_SYSCTL_WRITEJAIL;
2094 #ifdef VIMAGE
2095                 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2096                      prison_owns_vnet(req->td->td_ucred))
2097                         priv = PRIV_SYSCTL_WRITEJAIL;
2098 #endif
2099                 else
2100                         priv = PRIV_SYSCTL_WRITE;
2101                 error = priv_check(req->td, priv);
2102                 if (error)
2103                         goto out;
2104         }
2105
2106         if (!oid->oid_handler) {
2107                 error = EINVAL;
2108                 goto out;
2109         }
2110
2111         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2112                 arg1 = (int *)arg1 + indx;
2113                 arg2 -= indx;
2114         } else {
2115                 arg1 = oid->oid_arg1;
2116                 arg2 = oid->oid_arg2;
2117         }
2118 #ifdef MAC
2119         error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2120             req);
2121         if (error != 0)
2122                 goto out;
2123 #endif
2124 #ifdef VIMAGE
2125         if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2126                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2127 #endif
2128         error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2129
2130 out:
2131         SYSCTL_RUNLOCK(&tracker);
2132         return (error);
2133 }
2134
2135 #ifndef _SYS_SYSPROTO_H_
2136 struct sysctl_args {
2137         int     *name;
2138         u_int   namelen;
2139         void    *old;
2140         size_t  *oldlenp;
2141         void    *new;
2142         size_t  newlen;
2143 };
2144 #endif
2145 int
2146 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2147 {
2148         int error, i, name[CTL_MAXNAME];
2149         size_t j;
2150
2151         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2152                 return (EINVAL);
2153
2154         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2155         if (error)
2156                 return (error);
2157
2158         error = userland_sysctl(td, name, uap->namelen,
2159                 uap->old, uap->oldlenp, 0,
2160                 uap->new, uap->newlen, &j, 0);
2161         if (error && error != ENOMEM)
2162                 return (error);
2163         if (uap->oldlenp) {
2164                 i = copyout(&j, uap->oldlenp, sizeof(j));
2165                 if (i)
2166                         return (i);
2167         }
2168         return (error);
2169 }
2170
2171 int
2172 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2173     void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2174     int flags, bool inkernel)
2175 {
2176         int oid[CTL_MAXNAME];
2177         char namebuf[16];
2178         char *name;
2179         size_t oidlen;
2180         int error;
2181
2182         if (namelen > MAXPATHLEN || namelen == 0)
2183                 return (EINVAL);
2184         name = namebuf;
2185         if (namelen > sizeof(namebuf))
2186                 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2187         error = copyin(oname, name, namelen);
2188         if (error != 0)
2189                 goto out;
2190
2191         oid[0] = CTL_SYSCTL;
2192         oid[1] = CTL_SYSCTL_NAME2OID;
2193         oidlen = sizeof(oid);
2194         error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2195             retval, flags);
2196         if (error != 0)
2197                 goto out;
2198         error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2199             inkernel, new, newlen, retval, flags);
2200
2201 out:
2202         if (namelen > sizeof(namebuf))
2203                 free(name, M_SYSCTL);
2204         return (error);
2205 }
2206
2207 #ifndef _SYS_SYSPROTO_H_
2208 struct __sysctlbyname_args {
2209         const char      *name;
2210         size_t  namelen;
2211         void    *old;
2212         size_t  *oldlenp;
2213         void    *new;
2214         size_t  newlen;
2215 };
2216 #endif
2217 int
2218 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2219 {
2220         size_t rv;
2221         int error;
2222
2223         error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2224             uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2225         if (error != 0)
2226                 return (error);
2227         if (uap->oldlenp != NULL)
2228                 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2229
2230         return (error);
2231 }
2232
2233 /*
2234  * This is used from various compatibility syscalls too.  That's why name
2235  * must be in kernel space.
2236  */
2237 int
2238 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2239     size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2240     size_t *retval, int flags)
2241 {
2242         int error = 0, memlocked;
2243         struct sysctl_req req;
2244
2245         bzero(&req, sizeof req);
2246
2247         req.td = td;
2248         req.flags = flags;
2249
2250         if (oldlenp) {
2251                 if (inkernel) {
2252                         req.oldlen = *oldlenp;
2253                 } else {
2254                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2255                         if (error)
2256                                 return (error);
2257                 }
2258         }
2259         req.validlen = req.oldlen;
2260         req.oldptr = old;
2261
2262         if (new != NULL) {
2263                 req.newlen = newlen;
2264                 req.newptr = new;
2265         }
2266
2267         req.oldfunc = sysctl_old_user;
2268         req.newfunc = sysctl_new_user;
2269         req.lock = REQ_UNWIRED;
2270
2271 #ifdef KTRACE
2272         if (KTRPOINT(curthread, KTR_SYSCTL))
2273                 ktrsysctl(name, namelen);
2274 #endif
2275         memlocked = 0;
2276         if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2277                 memlocked = 1;
2278                 sx_xlock(&sysctlmemlock);
2279         }
2280         CURVNET_SET(TD_TO_VNET(td));
2281
2282         for (;;) {
2283                 req.oldidx = 0;
2284                 req.newidx = 0;
2285                 error = sysctl_root(0, name, namelen, &req);
2286                 if (error != EAGAIN)
2287                         break;
2288                 kern_yield(PRI_USER);
2289         }
2290
2291         CURVNET_RESTORE();
2292
2293         if (req.lock == REQ_WIRED && req.validlen > 0)
2294                 vsunlock(req.oldptr, req.validlen);
2295         if (memlocked)
2296                 sx_xunlock(&sysctlmemlock);
2297
2298         if (error && error != ENOMEM)
2299                 return (error);
2300
2301         if (retval) {
2302                 if (req.oldptr && req.oldidx > req.validlen)
2303                         *retval = req.validlen;
2304                 else
2305                         *retval = req.oldidx;
2306         }
2307         return (error);
2308 }
2309
2310 /*
2311  * Drain into a sysctl struct.  The user buffer should be wired if a page
2312  * fault would cause issue.
2313  */
2314 static int
2315 sbuf_sysctl_drain(void *arg, const char *data, int len)
2316 {
2317         struct sysctl_req *req = arg;
2318         int error;
2319
2320         error = SYSCTL_OUT(req, data, len);
2321         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2322         return (error == 0 ? len : -error);
2323 }
2324
2325 struct sbuf *
2326 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2327     struct sysctl_req *req)
2328 {
2329
2330         /* Supply a default buffer size if none given. */
2331         if (buf == NULL && length == 0)
2332                 length = 64;
2333         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2334         sbuf_set_drain(s, sbuf_sysctl_drain, req);
2335         return (s);
2336 }
2337
2338 #ifdef DDB
2339
2340 /* The current OID the debugger is working with */
2341 static struct sysctl_oid *g_ddb_oid;
2342
2343 /* The current flags specified by the user */
2344 static int g_ddb_sysctl_flags;
2345
2346 /* Check to see if the last sysctl printed */
2347 static int g_ddb_sysctl_printed;
2348
2349 static const int ctl_sign[CTLTYPE+1] = {
2350         [CTLTYPE_INT] = 1,
2351         [CTLTYPE_LONG] = 1,
2352         [CTLTYPE_S8] = 1,
2353         [CTLTYPE_S16] = 1,
2354         [CTLTYPE_S32] = 1,
2355         [CTLTYPE_S64] = 1,
2356 };
2357
2358 static const int ctl_size[CTLTYPE+1] = {
2359         [CTLTYPE_INT] = sizeof(int),
2360         [CTLTYPE_UINT] = sizeof(u_int),
2361         [CTLTYPE_LONG] = sizeof(long),
2362         [CTLTYPE_ULONG] = sizeof(u_long),
2363         [CTLTYPE_S8] = sizeof(int8_t),
2364         [CTLTYPE_S16] = sizeof(int16_t),
2365         [CTLTYPE_S32] = sizeof(int32_t),
2366         [CTLTYPE_S64] = sizeof(int64_t),
2367         [CTLTYPE_U8] = sizeof(uint8_t),
2368         [CTLTYPE_U16] = sizeof(uint16_t),
2369         [CTLTYPE_U32] = sizeof(uint32_t),
2370         [CTLTYPE_U64] = sizeof(uint64_t),
2371 };
2372
2373 #define DB_SYSCTL_NAME_ONLY     0x001   /* Compare with -N */
2374 #define DB_SYSCTL_VALUE_ONLY    0x002   /* Compare with -n */
2375 #define DB_SYSCTL_OPAQUE        0x004   /* Compare with -o */
2376 #define DB_SYSCTL_HEX           0x008   /* Compare with -x */
2377
2378 #define DB_SYSCTL_SAFE_ONLY     0x100   /* Only simple types */
2379
2380 static const char db_sysctl_modifs[] = {
2381         'N', 'n', 'o', 'x',
2382 };
2383
2384 static const int db_sysctl_modif_values[] = {
2385         DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2386         DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2387 };
2388
2389 /* Handlers considered safe to print while recursing */
2390 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2391         sysctl_handle_bool,
2392         sysctl_handle_8,
2393         sysctl_handle_16,
2394         sysctl_handle_32,
2395         sysctl_handle_64,
2396         sysctl_handle_int,
2397         sysctl_handle_long,
2398         sysctl_handle_string,
2399         sysctl_handle_opaque,
2400 };
2401
2402 /*
2403  * Use in place of sysctl_old_kernel to print sysctl values.
2404  *
2405  * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2406  */
2407 static int
2408 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2409 {
2410         const u_char *val, *p;
2411         const char *sep1;
2412         size_t intlen, slen;
2413         uintmax_t umv;
2414         intmax_t mv;
2415         int sign, ctltype, hexlen, xflag, error;
2416
2417         /* Suppress false-positive GCC uninitialized variable warnings */
2418         mv = 0;
2419         umv = 0;
2420
2421         slen = len;
2422         val = p = ptr;
2423
2424         if (ptr == NULL) {
2425                 error = 0;
2426                 goto out;
2427         }
2428
2429         /* We are going to print */
2430         g_ddb_sysctl_printed = 1;
2431
2432         xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2433
2434         ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2435         sign = ctl_sign[ctltype];
2436         intlen = ctl_size[ctltype];
2437
2438         switch (ctltype) {
2439         case CTLTYPE_NODE:
2440         case CTLTYPE_STRING:
2441                 db_printf("%.*s", (int) len, (const char *) p);
2442                 error = 0;
2443                 goto out;
2444
2445         case CTLTYPE_INT:
2446         case CTLTYPE_UINT:
2447         case CTLTYPE_LONG:
2448         case CTLTYPE_ULONG:
2449         case CTLTYPE_S8:
2450         case CTLTYPE_S16:
2451         case CTLTYPE_S32:
2452         case CTLTYPE_S64:
2453         case CTLTYPE_U8:
2454         case CTLTYPE_U16:
2455         case CTLTYPE_U32:
2456         case CTLTYPE_U64:
2457                 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2458                 sep1 = "";
2459                 while (len >= intlen) {
2460                         switch (ctltype) {
2461                         case CTLTYPE_INT:
2462                         case CTLTYPE_UINT:
2463                                 umv = *(const u_int *)p;
2464                                 mv = *(const int *)p;
2465                                 break;
2466                         case CTLTYPE_LONG:
2467                         case CTLTYPE_ULONG:
2468                                 umv = *(const u_long *)p;
2469                                 mv = *(const long *)p;
2470                                 break;
2471                         case CTLTYPE_S8:
2472                         case CTLTYPE_U8:
2473                                 umv = *(const uint8_t *)p;
2474                                 mv = *(const int8_t *)p;
2475                                 break;
2476                         case CTLTYPE_S16:
2477                         case CTLTYPE_U16:
2478                                 umv = *(const uint16_t *)p;
2479                                 mv = *(const int16_t *)p;
2480                                 break;
2481                         case CTLTYPE_S32:
2482                         case CTLTYPE_U32:
2483                                 umv = *(const uint32_t *)p;
2484                                 mv = *(const int32_t *)p;
2485                                 break;
2486                         case CTLTYPE_S64:
2487                         case CTLTYPE_U64:
2488                                 umv = *(const uint64_t *)p;
2489                                 mv = *(const int64_t *)p;
2490                                 break;
2491                         }
2492
2493                         db_printf("%s", sep1);
2494                         if (xflag)
2495                                 db_printf("%#0*jx", hexlen, umv);
2496                         else if (!sign)
2497                                 db_printf("%ju", umv);
2498                         else if (g_ddb_oid->oid_fmt[1] == 'K') {
2499                                 /* Kelvins are currently unsupported. */
2500                                 error = EOPNOTSUPP;
2501                                 goto out;
2502                         } else
2503                                 db_printf("%jd", mv);
2504
2505                         sep1 = " ";
2506                         len -= intlen;
2507                         p += intlen;
2508                 }
2509                 error = 0;
2510                 goto out;
2511
2512         case CTLTYPE_OPAQUE:
2513                 /* TODO: Support struct functions. */
2514
2515                 /* FALLTHROUGH */
2516         default:
2517                 db_printf("Format:%s Length:%zu Dump:0x",
2518                     g_ddb_oid->oid_fmt, len);
2519                 while (len-- && (xflag || p < val + 16))
2520                         db_printf("%02x", *p++);
2521                 if (!xflag && len > 16)
2522                         db_printf("...");
2523                 error = 0;
2524                 goto out;
2525         }
2526
2527 out:
2528         req->oldidx += slen;
2529         return (error);
2530 }
2531
2532 /*
2533  * Avoid setting new sysctl values from the debugger
2534  */
2535 static int
2536 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2537 {
2538
2539         if (!req->newptr)
2540                 return (0);
2541
2542         /* Changing sysctls from the debugger is currently unsupported */
2543         return (EPERM);
2544 }
2545
2546 /*
2547  * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2548  * Instead of copying any output to a buffer we'll dump it right to
2549  * the console.
2550  */
2551 static int
2552 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2553     void *old, size_t *oldlenp, size_t *retval, int flags)
2554 {
2555         struct sysctl_req req;
2556         int error;
2557
2558         /* Setup the request */
2559         bzero(&req, sizeof req);
2560         req.td = kdb_thread;
2561         req.oldfunc = sysctl_old_ddb;
2562         req.newfunc = sysctl_new_ddb;
2563         req.lock = REQ_UNWIRED;
2564         if (oldlenp) {
2565                 req.oldlen = *oldlenp;
2566         }
2567         req.validlen = req.oldlen;
2568         if (old) {
2569                 req.oldptr = old;
2570         }
2571
2572         /* Setup our globals for sysctl_old_ddb */
2573         g_ddb_oid = oidp;
2574         g_ddb_sysctl_flags = flags;
2575         g_ddb_sysctl_printed = 0;
2576
2577         error = sysctl_root(0, name, namelen, &req);
2578
2579         /* Reset globals */
2580         g_ddb_oid = NULL;
2581         g_ddb_sysctl_flags = 0;
2582
2583         if (retval) {
2584                 if (req.oldptr && req.oldidx > req.validlen)
2585                         *retval = req.validlen;
2586                 else
2587                         *retval = req.oldidx;
2588         }
2589         return (error);
2590 }
2591
2592 /*
2593  * Show a sysctl's name
2594  */
2595 static void
2596 db_show_oid_name(int *oid, size_t nlen)
2597 {
2598         struct sysctl_oid *oidp;
2599         int qoid[CTL_MAXNAME+2];
2600         int error;
2601
2602         qoid[0] = 0;
2603         memcpy(qoid + 2, oid, nlen * sizeof(int));
2604         qoid[1] = 1;
2605
2606         error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2607         if (error)
2608                 db_error("sysctl name oid");
2609
2610         error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2611         if (error)
2612                 db_error("sysctl name");
2613 }
2614
2615 /*
2616  * Check to see if an OID is safe to print from ddb.
2617  */
2618 static bool
2619 db_oid_safe(const struct sysctl_oid *oidp)
2620 {
2621         for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2622                 if (oidp->oid_handler == db_safe_handlers[i])
2623                         return (true);
2624         }
2625
2626         return (false);
2627 }
2628
2629 /*
2630  * Show a sysctl at a specific OID
2631  * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2632  */
2633 static int
2634 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2635 {
2636         int error, xflag, oflag, Nflag, nflag;
2637         size_t len;
2638
2639         xflag = flags & DB_SYSCTL_HEX;
2640         oflag = flags & DB_SYSCTL_OPAQUE;
2641         nflag = flags & DB_SYSCTL_VALUE_ONLY;
2642         Nflag = flags & DB_SYSCTL_NAME_ONLY;
2643
2644         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2645             (!xflag && !oflag))
2646                 return (0);
2647
2648         if (Nflag) {
2649                 db_show_oid_name(oid, nlen);
2650                 error = 0;
2651                 goto out;
2652         }
2653
2654         if (!nflag) {
2655                 db_show_oid_name(oid, nlen);
2656                 db_printf(": ");
2657         }
2658
2659         if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2660                 db_printf("Skipping, unsafe to print while recursing.");
2661                 error = 0;
2662                 goto out;
2663         }
2664
2665         /* Try once, and ask about the size */
2666         len = 0;
2667         error = db_sysctl(oidp, oid, nlen,
2668             NULL, NULL, &len, flags);
2669         if (error)
2670                 goto out;
2671
2672         if (!g_ddb_sysctl_printed)
2673                 /* Lie about the size */
2674                 error = db_sysctl(oidp, oid, nlen,
2675                     (void *) 1, &len, NULL, flags);
2676
2677 out:
2678         db_printf("\n");
2679         return (error);
2680 }
2681
2682 /*
2683  * Show all sysctls under a specific OID
2684  * Compare to sysctl_all from sbin/sysctl/sysctl.c
2685  */
2686 static int
2687 db_show_sysctl_all(int *oid, size_t len, int flags)
2688 {
2689         struct sysctl_oid *oidp;
2690         int name1[CTL_MAXNAME + 2], name2[CTL_MAXNAME + 2];
2691         size_t l1, l2;
2692
2693         name1[0] = CTL_SYSCTL;
2694         name1[1] = CTL_SYSCTL_NEXT;
2695         l1 = 2;
2696         if (len) {
2697                 memcpy(name1+2, oid, len * sizeof(int));
2698                 l1 +=len;
2699         } else {
2700                 name1[2] = 1;
2701                 l1++;
2702         }
2703         for (;;) {
2704                 int i, error;
2705
2706                 l2 = sizeof(name2);
2707                 error = kernel_sysctl(kdb_thread, name1, l1,
2708                     name2, &l2, NULL, 0, &l2, 0);
2709                 if (error != 0) {
2710                         if (error == ENOENT)
2711                                 return (0);
2712                         else
2713                                 db_error("sysctl(getnext)");
2714                 }
2715
2716                 l2 /= sizeof(int);
2717
2718                 if (l2 < (unsigned int)len)
2719                         return (0);
2720
2721                 for (i = 0; i < len; i++)
2722                         if (name2[i] != oid[i])
2723                                 return (0);
2724
2725                 /* Find the OID in question */
2726                 error = sysctl_find_oid(name2, l2, &oidp, NULL, NULL);
2727                 if (error)
2728                         return (error);
2729
2730                 i = db_show_oid(oidp, name2, l2, flags | DB_SYSCTL_SAFE_ONLY);
2731
2732                 if (db_pager_quit)
2733                         return (0);
2734
2735                 memcpy(name1+2, name2, l2 * sizeof(int));
2736                 l1 = 2 + l2;
2737         }
2738 }
2739
2740 /*
2741  * Show a sysctl by its user facing string
2742  */
2743 static int
2744 db_sysctlbyname(char *name, int flags)
2745 {
2746         struct sysctl_oid *oidp;
2747         int oid[CTL_MAXNAME];
2748         int error, nlen;
2749
2750         error = name2oid(name, oid, &nlen, &oidp);
2751         if (error) {
2752                 return (error);
2753         }
2754
2755         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2756                 db_show_sysctl_all(oid, nlen, flags);
2757         } else {
2758                 error = db_show_oid(oidp, oid, nlen, flags);
2759         }
2760
2761         return (error);
2762 }
2763
2764 static void
2765 db_sysctl_cmd_usage(void)
2766 {
2767         db_printf(
2768             " sysctl [/Nnox] <sysctl>                                       \n"
2769             "                                                               \n"
2770             " <sysctl> The name of the sysctl to show.                      \n"
2771             "                                                               \n"
2772             " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT.       \n"
2773             " This will work for most sysctls, but should not be used       \n"
2774             " with sysctls that are known to malloc.                        \n"
2775             "                                                               \n"
2776             " While recursing any \"unsafe\" sysctls will be skipped.       \n"
2777             " Call sysctl directly on the sysctl to try printing the        \n"
2778             " skipped sysctl. This is unsafe and may make the ddb           \n"
2779             " session unusable.                                             \n"
2780             "                                                               \n"
2781             " Arguments:                                                    \n"
2782             "   /N      Display only the name of the sysctl.                \n"
2783             "   /n      Display only the value of the sysctl.               \n"
2784             "   /o      Display opaque values.                              \n"
2785             "   /x      Display the sysctl in hex.                          \n"
2786             "                                                               \n"
2787             "For example:                                                   \n"
2788             "sysctl vm.v_free_min                                           \n"
2789             "vn.v_free_min: 12669                                           \n"
2790             );
2791 }
2792
2793 /*
2794  * Show a specific sysctl similar to sysctl (8).
2795  */
2796 DB_FUNC(sysctl, db_sysctl_cmd, db_cmd_table, CS_OWN, NULL)
2797 {
2798         char name[TOK_STRING_SIZE];
2799         int error, i, t, flags;
2800
2801         /* Parse the modifiers */
2802         t = db_read_token();
2803         if (t == tSLASH || t == tMINUS) {
2804                 t = db_read_token();
2805                 if (t != tIDENT) {
2806                         db_printf("Bad modifier\n");
2807                         error = EINVAL;
2808                         goto out;
2809                 }
2810                 db_strcpy(modif, db_tok_string);
2811         }
2812         else {
2813                 db_unread_token(t);
2814                 modif[0] = '\0';
2815         }
2816
2817         flags = 0;
2818         for (i = 0; i < nitems(db_sysctl_modifs); i++) {
2819                 if (strchr(modif, db_sysctl_modifs[i])) {
2820                         flags |= db_sysctl_modif_values[i];
2821                 }
2822         }
2823
2824         /* Parse the sysctl names */
2825         t = db_read_token();
2826         if (t != tIDENT) {
2827                 db_printf("Need sysctl name\n");
2828                 error = EINVAL;
2829                 goto out;
2830         }
2831
2832         /* Copy the name into a temporary buffer */
2833         db_strcpy(name, db_tok_string);
2834
2835         /* Ensure there is no trailing cruft */
2836         t = db_read_token();
2837         if (t != tEOL) {
2838                 db_printf("Unexpected sysctl argument\n");
2839                 error = EINVAL;
2840                 goto out;
2841         }
2842
2843         error = db_sysctlbyname(name, flags);
2844         if (error == ENOENT) {
2845                 db_printf("unknown oid: '%s'\n", db_tok_string);
2846                 goto out;
2847         } else if (error) {
2848                 db_printf("%s: error: %d\n", db_tok_string, error);
2849                 goto out;
2850         }
2851
2852 out:
2853         /* Ensure we eat all of our text */
2854         db_flush_lex();
2855
2856         if (error == EINVAL) {
2857                 db_sysctl_cmd_usage();
2858         }
2859 }
2860
2861 #endif /* DDB */