source: rtems-libbsd/freebsd/sys/kern/kern_sysctl.c @ 66659ff

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 66659ff was 66659ff, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 15:20:21

Update to FreeBSD 9.2

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