source: rtems-libbsd/freebsd/sys/kern/kern_sysctl.c @ 1bbb359

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 1bbb359 was 1bbb359, checked in by Sebastian Huber <sebastian.huber@…>, on 10/23/13 at 07:47:26

Avoid NULL pointer access

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