source: rtems/cpukit/libnetworking/kern/kern_sysctl.c @ c301570

4.104.114.84.95
Last change on this file since c301570 was c301570, checked in by Ralf Corsepius <ralf.corsepius@…>, on 05/10/07 at 05:12:54

Include <rtems/bsd/sys/queue.h> instead of <sys/queue.h>.

  • Property mode set to 100644
File size: 33.8 KB
Line 
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *      @(#)kern_sysctl.c       8.4 (Berkeley) 4/14/94
36 * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.135 2002/10/27 07:12:34 rwatson Exp $
37 */
38
39#include "opt_compat.h"
40#include "opt_mac.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/sysctl.h>
46#include <sys/malloc.h>
47#include <sys/proc.h>
48#ifndef __rtems__
49#include <sys/mac.h>
50#include <sys/lock.h>
51#include <sys/mutex.h>
52#include <sys/sx.h>
53#include <sys/sysproto.h>
54#else
55#include <rtems/bsd/sys/queue.h>
56
57#include <stdio.h>                                /* for snprintf() */
58size_t   strlcpy(char *, const char *, size_t);
59#endif
60#include <vm/vm.h>
61#include <vm/vm_extern.h>
62
63#ifndef __rtems__
64static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
65static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
66static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
67#else
68/*
69 * Currently these mean nothing on RTEMS.
70 */
71#define M_SYSCTLOID 1
72#define M_SYSCTLTMP 2
73#define M_ZERO      0
74
75#define mtx_lock(l)
76#define mtx_unlock(l)
77
78#endif
79
80#ifndef __rtems__
81/*
82 * Locking - this locks the sysctl tree in memory.
83 */
84static struct sx sysctllock;
85#endif
86
87#ifdef __rtems__
88#define SYSCTL_LOCK()
89#define SYSCTL_UNLOCK()
90#define SYSCTL_INIT()
91#else
92#define SYSCTL_LOCK()           sx_xlock(&sysctllock)
93#define SYSCTL_UNLOCK()         sx_xunlock(&sysctllock)
94#define SYSCTL_INIT()           sx_init(&sysctllock, "sysctl lock")
95#endif
96
97static int sysctl_root(SYSCTL_HANDLER_ARGS);
98
99struct sysctl_oid_list sysctl__children; /* root list */
100
101static struct sysctl_oid *
102sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
103{
104        struct sysctl_oid *oidp;
105
106        SLIST_FOREACH(oidp, list, oid_link) {
107                if (strcmp(oidp->oid_name, name) == 0) {
108                        return (oidp);
109                }
110        }
111        return (NULL);
112}
113
114/*
115 * Initialization of the MIB tree.
116 *
117 * Order by number in each list.
118 */
119
120void
121sysctl_register_oid(struct sysctl_oid *oidp)
122{
123        struct sysctl_oid_list *parent = oidp->oid_parent;
124        struct sysctl_oid *p;
125        struct sysctl_oid *q;
126
127        /*
128         * First check if another oid with the same name already
129         * exists in the parent's list.
130         */
131        p = sysctl_find_oidname(oidp->oid_name, parent);
132        if (p != NULL) {
133                if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
134                        p->oid_refcnt++;
135                        return;
136                } else {
137                        printf("can't re-use a leaf (%s)!\n", p->oid_name);
138                        return;
139                }
140        }
141        /*
142         * If this oid has a number OID_AUTO, give it a number which
143         * is greater than any current oid.
144         * NOTE: DO NOT change the starting value here, change it in
145         * <sys/sysctl.h>, and make sure it is at least 256 to
146         * accomodate e.g. net.inet.raw as a static sysctl node.
147         */
148        if (oidp->oid_number == OID_AUTO) {
149                static int32_t newoid = CTL_AUTO_START;
150
151                oidp->oid_number = newoid++;
152                if (newoid == 0x7fffffff)
153                        panic("out of oids");
154        }
155#if 0
156        else if (oidp->oid_number >= CTL_AUTO_START) {
157                /* do not panic; this happens when unregistering sysctl sets */
158                printf("static sysctl oid too high: %d", oidp->oid_number);
159        }
160#endif
161
162        /*
163         * Insert the oid into the parent's list in order.
164         */
165        q = NULL;
166        SLIST_FOREACH(p, parent, oid_link) {
167                if (oidp->oid_number < p->oid_number)
168                        break;
169                q = p;
170        }
171        if (q)
172                SLIST_INSERT_AFTER(q, oidp, oid_link);
173        else
174                SLIST_INSERT_HEAD(parent, oidp, oid_link);
175}
176
177void
178sysctl_unregister_oid(struct sysctl_oid *oidp)
179{
180        SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
181}
182
183/* Initialize a new context to keep track of dynamically added sysctls. */
184int
185sysctl_ctx_init(struct sysctl_ctx_list *c)
186{
187
188        if (c == NULL) {
189                return (EINVAL);
190        }
191        TAILQ_INIT(c);
192        return (0);
193}
194
195/* Free the context, and destroy all dynamic oids registered in this context */
196int
197sysctl_ctx_free(struct sysctl_ctx_list *clist)
198{
199        struct sysctl_ctx_entry *e, *e1;
200        int error;
201
202        error = 0;
203        /*
204         * First perform a "dry run" to check if it's ok to remove oids.
205         * XXX FIXME
206         * XXX This algorithm is a hack. But I don't know any
207         * XXX better solution for now...
208         */
209        TAILQ_FOREACH(e, clist, link) {
210                error = sysctl_remove_oid(e->entry, 0, 0);
211                if (error)
212                        break;
213        }
214        /*
215         * Restore deregistered entries, either from the end,
216         * or from the place where error occured.
217         * e contains the entry that was not unregistered
218         */
219        if (error)
220                e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
221        else
222                e1 = TAILQ_LAST(clist, sysctl_ctx_list);
223        while (e1 != NULL) {
224                sysctl_register_oid(e1->entry);
225                e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
226        }
227        if (error)
228                return(EBUSY);
229        /* Now really delete the entries */
230        e = TAILQ_FIRST(clist);
231        while (e != NULL) {
232                e1 = TAILQ_NEXT(e, link);
233                error = sysctl_remove_oid(e->entry, 1, 0);
234                if (error)
235                        panic("sysctl_remove_oid: corrupt tree, entry: %s",
236                            e->entry->oid_name);
237                free(e, M_SYSCTLOID);
238                e = e1;
239        }
240        return (error);
241}
242
243/* Add an entry to the context */
244struct sysctl_ctx_entry *
245sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
246{
247        struct sysctl_ctx_entry *e;
248
249        if (clist == NULL || oidp == NULL)
250                return(NULL);
251        e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
252        e->entry = oidp;
253        TAILQ_INSERT_HEAD(clist, e, link);
254        return (e);
255}
256
257/* Find an entry in the context */
258struct sysctl_ctx_entry *
259sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
260{
261        struct sysctl_ctx_entry *e;
262
263        if (clist == NULL || oidp == NULL)
264                return(NULL);
265        TAILQ_FOREACH(e, clist, link) {
266                if(e->entry == oidp)
267                        return(e);
268        }
269        return (e);
270}
271
272/*
273 * Delete an entry from the context.
274 * NOTE: this function doesn't free oidp! You have to remove it
275 * with sysctl_remove_oid().
276 */
277int
278sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
279{
280        struct sysctl_ctx_entry *e;
281
282        if (clist == NULL || oidp == NULL)
283                return (EINVAL);
284        e = sysctl_ctx_entry_find(clist, oidp);
285        if (e != NULL) {
286                TAILQ_REMOVE(clist, e, link);
287                free(e, M_SYSCTLOID);
288                return (0);
289        } else
290                return (ENOENT);
291}
292
293/*
294 * Remove dynamically created sysctl trees.
295 * oidp - top of the tree to be removed
296 * del - if 0 - just deregister, otherwise free up entries as well
297 * recurse - if != 0 traverse the subtree to be deleted
298 */
299int
300sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
301{
302        struct sysctl_oid *p;
303        int error;
304
305        if (oidp == NULL)
306                return(EINVAL);
307        if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
308                printf("can't remove non-dynamic nodes!\n");
309                return (EINVAL);
310        }
311        /*
312         * WARNING: normal method to do this should be through
313         * sysctl_ctx_free(). Use recursing as the last resort
314         * method to purge your sysctl tree of leftovers...
315         * However, if some other code still references these nodes,
316         * it will panic.
317         */
318        if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
319                if (oidp->oid_refcnt == 1) {
320                        SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
321                                if (!recurse)
322                                        return (ENOTEMPTY);
323                                error = sysctl_remove_oid(p, del, recurse);
324                                if (error)
325                                        return (error);
326                        }
327                        if (del)
328                                free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
329                }
330        }
331        if (oidp->oid_refcnt > 1 ) {
332                oidp->oid_refcnt--;
333        } else {
334                if (oidp->oid_refcnt == 0) {
335                        printf("Warning: bad oid_refcnt=%u (%s)!\n",
336                                oidp->oid_refcnt, oidp->oid_name);
337                        return (EINVAL);
338                }
339                sysctl_unregister_oid(oidp);
340                if (del) {
341                        if (oidp->descr)
342                                free((void *)(uintptr_t)(const void *)oidp->descr, M_SYSCTLOID);
343                        free((void *)(uintptr_t)(const void *)oidp->oid_name,
344                             M_SYSCTLOID);
345                        free(oidp, M_SYSCTLOID);
346                }
347        }
348        return (0);
349}
350
351/*
352 * Create new sysctls at run time.
353 * clist may point to a valid context initialized with sysctl_ctx_init().
354 */
355struct sysctl_oid *
356sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
357        int number, const char *name, int kind, void *arg1, int arg2,
358        int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
359{
360        struct sysctl_oid *oidp;
361        ssize_t len;
362        char *newname;
363
364        /* You have to hook up somewhere.. */
365        if (parent == NULL)
366                return(NULL);
367        /* Check if the node already exists, otherwise create it */
368        oidp = sysctl_find_oidname(name, parent);
369        if (oidp != NULL) {
370                if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
371                        oidp->oid_refcnt++;
372                        /* Update the context */
373                        if (clist != NULL)
374                                sysctl_ctx_entry_add(clist, oidp);
375                        return (oidp);
376                } else {
377                        printf("can't re-use a leaf (%s)!\n", name);
378                        return (NULL);
379                }
380        }
381        oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
382        oidp->oid_parent = parent;
383        SLIST_NEXT(oidp, oid_link) = NULL;
384        oidp->oid_number = number;
385        oidp->oid_refcnt = 1;
386        len = strlen(name);
387        newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
388        bcopy(name, newname, len + 1);
389        newname[len] = '\0';
390        oidp->oid_name = newname;
391        oidp->oid_handler = handler;
392        oidp->oid_kind = CTLFLAG_DYN | kind;
393        if ((kind & CTLTYPE) == CTLTYPE_NODE) {
394                /* Allocate space for children */
395                SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
396                    M_SYSCTLOID, M_WAITOK));
397                SLIST_INIT(SYSCTL_CHILDREN(oidp));
398        } else {
399                oidp->oid_arg1 = arg1;
400                oidp->oid_arg2 = arg2;
401        }
402        oidp->oid_fmt = fmt;
403        if (descr) {
404                int len = strlen(descr) + 1;
405                oidp->descr = malloc(len, M_SYSCTLOID, M_WAITOK);
406                if (oidp->descr)
407                        strcpy((char *)(uintptr_t)(const void *)oidp->descr, descr);
408        }
409        /* Update the context, if used */
410        if (clist != NULL)
411                sysctl_ctx_entry_add(clist, oidp);
412        /* Register this oid */
413        sysctl_register_oid(oidp);
414        return (oidp);
415}
416
417/*
418 * Register the kernel's oids on startup.
419 */
420SET_DECLARE(sysctl_set, struct sysctl_oid);
421
422#if defined(__rtems__)
423void
424#else
425static void
426#endif
427sysctl_register_all(void *arg)
428{
429        struct sysctl_oid **oidp;
430
431        SYSCTL_INIT();
432        SET_FOREACH(oidp, sysctl_set)
433                sysctl_register_oid(*oidp);
434}
435SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
436
437/*
438 * "Staff-functions"
439 *
440 * These functions implement a presently undocumented interface
441 * used by the sysctl program to walk the tree, and get the type
442 * so it can print the value.
443 * This interface is under work and consideration, and should probably
444 * be killed with a big axe by the first person who can find the time.
445 * (be aware though, that the proper interface isn't as obvious as it
446 * may seem, there are various conflicting requirements.
447 *
448 * {0,0}        printf the entire MIB-tree.
449 * {0,1,...}    return the name of the "..." OID.
450 * {0,2,...}    return the next OID.
451 * {0,3}        return the OID of the name in "new"
452 * {0,4,...}    return the kind & format info for the "..." OID.
453 * {0,5,...}    return the description the "..." OID.
454 */
455
456static void
457sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
458{
459        int k;
460        struct sysctl_oid *oidp;
461
462        SLIST_FOREACH(oidp, l, oid_link) {
463
464                for (k=0; k<i; k++)
465                        printf(" ");
466
467                printf("%d %s ", oidp->oid_number, oidp->oid_name);
468
469                printf("%c%c",
470                        oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
471                        oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
472
473                if (oidp->oid_handler)
474                        printf(" *Handler");
475
476                switch (oidp->oid_kind & CTLTYPE) {
477                        case CTLTYPE_NODE:
478                                printf(" Node\n");
479                                if (!oidp->oid_handler) {
480                                        sysctl_sysctl_debug_dump_node(
481                                                oidp->oid_arg1, i+2);
482                                }
483                                break;
484                        case CTLTYPE_INT:    printf(" Int\n"); break;
485                        case CTLTYPE_STRING: printf(" String\n"); break;
486                        case CTLTYPE_QUAD:   printf(" Quad\n"); break;
487                        case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
488                        default:             printf("\n");
489                }
490
491        }
492}
493
494static int
495sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
496{
497#ifndef __rtems__
498        int error;
499
500        error = suser(req->td);
501        if (error)
502                return error;
503#endif
504        sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
505        return ENOENT;
506}
507
508SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
509        0, 0, sysctl_sysctl_debug, "-", "");
510
511static int
512sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
513{
514        int *name = (int *) arg1;
515        u_int namelen = arg2;
516        int error = 0;
517        struct sysctl_oid *oid;
518        struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
519        char buf[10];
520
521        while (namelen) {
522                if (!lsp) {
523                        snprintf(buf,sizeof(buf),"%d",*name);
524                        if (req->oldidx)
525                                error = SYSCTL_OUT(req, ".", 1);
526                        if (!error)
527                                error = SYSCTL_OUT(req, buf, strlen(buf));
528                        if (error)
529                                return (error);
530                        namelen--;
531                        name++;
532                        continue;
533                }
534                lsp2 = 0;
535                SLIST_FOREACH(oid, lsp, oid_link) {
536                        if (oid->oid_number != *name)
537                                continue;
538
539                        if (req->oldidx)
540                                error = SYSCTL_OUT(req, ".", 1);
541                        if (!error)
542                                error = SYSCTL_OUT(req, oid->oid_name,
543                                        strlen(oid->oid_name));
544                        if (error)
545                                return (error);
546
547                        namelen--;
548                        name++;
549
550                        if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
551                                break;
552
553                        if (oid->oid_handler)
554                                break;
555
556                        lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
557                        break;
558                }
559                lsp = lsp2;
560        }
561        return (SYSCTL_OUT(req, "", 1));
562}
563
564SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
565
566static int
567sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
568        int *next, int *len, int level, struct sysctl_oid **oidpp)
569{
570        struct sysctl_oid *oidp;
571
572        *len = level;
573        SLIST_FOREACH(oidp, lsp, oid_link) {
574                *next = oidp->oid_number;
575                *oidpp = oidp;
576
577                if (oidp->oid_kind & CTLFLAG_SKIP)
578                        continue;
579
580                if (!namelen) {
581                        if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
582                                return 0;
583                        if (oidp->oid_handler)
584                                /* We really should call the handler here...*/
585                                return 0;
586                        lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
587                        if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
588                                len, level+1, oidpp))
589                                return 0;
590                        goto emptynode;
591                }
592
593                if (oidp->oid_number < *name)
594                        continue;
595
596                if (oidp->oid_number > *name) {
597                        if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
598                                return 0;
599                        if (oidp->oid_handler)
600                                return 0;
601                        lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
602                        if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
603                                next+1, len, level+1, oidpp))
604                                return (0);
605                        goto next;
606                }
607                if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
608                        continue;
609
610                if (oidp->oid_handler)
611                        continue;
612
613                lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
614                if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
615                        len, level+1, oidpp))
616                        return (0);
617        next:
618                namelen = 1;
619        emptynode:
620                *len = level;
621        }
622        return 1;
623}
624
625static int
626sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
627{
628        int *name = (int *) arg1;
629        u_int namelen = arg2;
630        int i, j, error;
631        struct sysctl_oid *oid;
632        struct sysctl_oid_list *lsp = &sysctl__children;
633        int newoid[CTL_MAXNAME];
634
635        i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
636        if (i)
637                return ENOENT;
638        error = SYSCTL_OUT(req, newoid, j * sizeof (int));
639        return (error);
640}
641
642SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
643
644static int
645name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
646{
647        int i;
648        struct sysctl_oid *oidp;
649        struct sysctl_oid_list *lsp = &sysctl__children;
650        char *p;
651
652        if (!*name)
653                return ENOENT;
654
655        p = name + strlen(name) - 1 ;
656        if (*p == '.')
657                *p = '\0';
658
659        *len = 0;
660
661        for (p = name; *p && *p != '.'; p++)
662                ;
663        i = *p;
664        if (i == '.')
665                *p = '\0';
666
667        oidp = SLIST_FIRST(lsp);
668
669        while (oidp && *len < CTL_MAXNAME) {
670                if (strcmp(name, oidp->oid_name)) {
671                        oidp = SLIST_NEXT(oidp, oid_link);
672                        continue;
673                }
674                *oid++ = oidp->oid_number;
675                (*len)++;
676
677                if (!i) {
678                        if (oidpp)
679                                *oidpp = oidp;
680                        return (0);
681                }
682
683                if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
684                        break;
685
686                if (oidp->oid_handler)
687                        break;
688
689                lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
690                oidp = SLIST_FIRST(lsp);
691                name = p+1;
692                for (p = name; *p && *p != '.'; p++)
693                                ;
694                i = *p;
695                if (i == '.')
696                        *p = '\0';
697        }
698        return ENOENT;
699}
700
701static int
702sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
703{
704        char *p;
705        int error, oid[CTL_MAXNAME], len;
706        struct sysctl_oid *op = 0;
707
708        if (!req->newlen)
709                return ENOENT;
710        if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
711                return (ENAMETOOLONG);
712
713        p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
714
715        error = SYSCTL_IN(req, p, req->newlen);
716        if (error) {
717                free(p, M_SYSCTL);
718                return (error);
719        }
720
721        p [req->newlen] = '\0';
722
723        error = name2oid(p, oid, &len, &op);
724
725        free(p, M_SYSCTL);
726
727        if (error)
728                return (error);
729
730        error = SYSCTL_OUT(req, oid, len * sizeof *oid);
731        return (error);
732}
733
734SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
735        sysctl_sysctl_name2oid, "I", "");
736
737static int
738sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
739{
740        struct sysctl_oid *oid;
741        int error;
742
743        error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
744        if (error)
745                return (error);
746
747        if (!oid->oid_fmt)
748                return (ENOENT);
749        error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
750        if (error)
751                return (error);
752        error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
753        return (error);
754}
755
756
757SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
758
759static int
760sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
761{
762        struct sysctl_oid *oid;
763        int error;
764
765        error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
766        if (error)
767                return (error);
768
769        if (!oid->descr)
770                return (ENOENT);
771        error = SYSCTL_OUT(req, oid->descr, strlen(oid->descr) + 1);
772        return (error);
773}
774
775SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
776
777/*
778 * Default "handler" functions.
779 */
780
781/*
782 * Handle an int, signed or unsigned.
783 * Two cases:
784 *     a variable:  point arg1 at it.
785 *     a constant:  pass it in arg2.
786 */
787
788int
789sysctl_handle_int(SYSCTL_HANDLER_ARGS)
790{
791        int tmpout, error = 0;
792
793        /*
794         * Attempt to get a coherent snapshot by making a copy of the data.
795         */
796        if (arg1)
797                tmpout = *(int *)arg1;
798        else
799                tmpout = arg2;
800        error = SYSCTL_OUT(req, &tmpout, sizeof(int));
801
802        if (error || !req->newptr)
803                return (error);
804
805        if (!arg1)
806                error = EPERM;
807        else
808                error = SYSCTL_IN(req, arg1, sizeof(int));
809        return (error);
810}
811
812/*
813 * Handle a long, signed or unsigned.  arg1 points to it.
814 */
815
816int
817sysctl_handle_long(SYSCTL_HANDLER_ARGS)
818{
819        int error = 0;
820        long tmpout;
821
822        /*
823         * Attempt to get a coherent snapshot by making a copy of the data.
824         */
825        if (!arg1)
826                return (EINVAL);
827        tmpout = *(long *)arg1;
828        error = SYSCTL_OUT(req, &tmpout, sizeof(long));
829
830        if (error || !req->newptr)
831                return (error);
832
833        error = SYSCTL_IN(req, arg1, sizeof(long));
834        return (error);
835}
836
837/*
838 * Handle our generic '\0' terminated 'C' string.
839 * Two cases:
840 *      a variable string:  point arg1 at it, arg2 is max length.
841 *      a constant string:  point arg1 at it, arg2 is zero.
842 */
843
844int
845sysctl_handle_string(SYSCTL_HANDLER_ARGS)
846{
847        int error=0;
848        char *tmparg;
849        size_t outlen;
850
851        /*
852         * Attempt to get a coherent snapshot by copying to a
853         * temporary kernel buffer.
854         */
855retry:
856        outlen = strlen((char *)arg1)+1;
857        tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
858
859        if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
860                free(tmparg, M_SYSCTLTMP);
861                goto retry;
862        }
863
864        error = SYSCTL_OUT(req, tmparg, outlen);
865        free(tmparg, M_SYSCTLTMP);
866
867        if (error || !req->newptr)
868                return (error);
869
870        if ((req->newlen - req->newidx) >= arg2) {
871                error = EINVAL;
872        } else {
873                arg2 = (req->newlen - req->newidx);
874                error = SYSCTL_IN(req, arg1, arg2);
875                ((char *)arg1)[arg2] = '\0';
876        }
877
878        return (error);
879}
880
881/*
882 * Handle any kind of opaque data.
883 * arg1 points to it, arg2 is the size.
884 */
885
886int
887sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
888{
889        int error;
890        void *tmparg;
891
892        /*
893         * Attempt to get a coherent snapshot, either by wiring the
894         * user space buffer or copying to a temporary kernel buffer
895         * depending on the size of the data.
896         */
897        if (arg2 > PAGE_SIZE) {
898                sysctl_wire_old_buffer(req, arg2);
899                error = SYSCTL_OUT(req, arg1, arg2);
900        } else {
901                tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
902                bcopy(arg1, tmparg, arg2);
903                error = SYSCTL_OUT(req, tmparg, arg2);
904                free(tmparg, M_SYSCTLTMP);
905        }
906
907        if (error || !req->newptr)
908                return (error);
909
910        error = SYSCTL_IN(req, arg1, arg2);
911
912        return (error);
913}
914
915/*
916 * Transfer functions to/from kernel space.
917 * XXX: rather untested at this point
918 */
919static int
920sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
921{
922        size_t i = 0;
923
924        if (req->oldptr) {
925                i = l;
926                if (req->oldlen <= req->oldidx)
927                        i = 0;
928                else
929                        if (i > req->oldlen - req->oldidx)
930                                i = req->oldlen - req->oldidx;
931                if (i > 0)
932                        bcopy(p, (char *)req->oldptr + req->oldidx, i);
933        }
934        req->oldidx += l;
935        if (req->oldptr && i != l)
936                return (ENOMEM);
937        return (0);
938}
939
940static int
941sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
942{
943        if (!req->newptr)
944                return 0;
945        if (req->newlen - req->newidx < l)
946                return (EINVAL);
947        bcopy((char *)req->newptr + req->newidx, p, l);
948        req->newidx += l;
949        return (0);
950}
951
952int
953kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
954    size_t *oldlenp, void *new, size_t newlen, size_t *retval)
955{
956        int error = 0;
957        struct sysctl_req req;
958
959        bzero(&req, sizeof req);
960
961        req.td = td;
962
963        if (oldlenp) {
964                req.oldlen = *oldlenp;
965        }
966
967        if (old) {
968                req.oldptr= old;
969        }
970
971        if (new != NULL) {
972                req.newlen = newlen;
973                req.newptr = new;
974        }
975
976        req.oldfunc = sysctl_old_kernel;
977        req.newfunc = sysctl_new_kernel;
978        req.lock = REQ_LOCKED;
979
980        SYSCTL_LOCK();
981
982        error = sysctl_root(0, name, namelen, &req);
983
984        if (req.lock == REQ_WIRED)
985#ifdef __rtems__
986    printf ("kern_sysctl: vsunlock needs to be called!\n");
987#else
988                vsunlock(req.oldptr, req.oldlen);
989#endif
990
991        SYSCTL_UNLOCK();
992
993        if (error && error != ENOMEM)
994                return (error);
995
996        if (retval) {
997                if (req.oldptr && req.oldidx > req.oldlen)
998                        *retval = req.oldlen;
999                else
1000                        *retval = req.oldidx;
1001        }
1002        return (error);
1003}
1004
1005int
1006kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1007    void *new, size_t newlen, size_t *retval)
1008{
1009        int oid[CTL_MAXNAME];
1010        size_t oidlen, plen;
1011        int error;
1012
1013        plen = 0; /* RTEMS - to avoid warnings */
1014
1015        oid[0] = 0;             /* sysctl internal magic */
1016        oid[1] = 3;             /* name2oid */
1017        oidlen = sizeof(oid);
1018
1019        error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1020            (void *)name, strlen(name), &plen);
1021        if (error)
1022                return (error);
1023
1024        error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1025            new, newlen, retval);
1026        return (error);
1027}
1028
1029/*
1030 * Transfer function to/from user space.
1031 */
1032static int
1033sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1034{
1035        int error = 0;
1036        size_t i = 0;
1037
1038#ifndef __rtems__
1039        if (req->lock == 1 && req->oldptr)
1040                WITNESS_SLEEP(1, NULL);
1041#endif
1042        if (req->oldptr) {
1043                i = l;
1044                if (req->oldlen <= req->oldidx)
1045                        i = 0;
1046                else
1047                        if (i > req->oldlen - req->oldidx)
1048                                i = req->oldlen - req->oldidx;
1049                if (i > 0)
1050                        error = copyout(p, (char *)req->oldptr + req->oldidx,
1051                                        i);
1052        }
1053        req->oldidx += l;
1054        if (error)
1055                return (error);
1056        if (req->oldptr && i < l)
1057                return (ENOMEM);
1058        return (0);
1059}
1060
1061static int
1062sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1063{
1064        int error;
1065
1066        if (!req->newptr)
1067                return 0;
1068        if (req->newlen - req->newidx < l)
1069                return (EINVAL);
1070        error = copyin((char *)req->newptr + req->newidx, p, l);
1071        req->newidx += l;
1072        return (error);
1073}
1074
1075/*
1076 * Wire the user space destination buffer.  If set to a value greater than
1077 * zero, the len parameter limits the maximum amount of wired memory.
1078 *
1079 * XXX - The len parameter is currently ignored due to the lack of
1080 * a place to save it in the sysctl_req structure so that the matching
1081 * amount of memory can be unwired in the sysctl exit code.
1082 */
1083int
1084sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1085{
1086        if (req->lock == REQ_LOCKED && req->oldptr &&
1087            req->oldfunc == sysctl_old_user) {
1088#ifndef __rtems__
1089                vslock(req->oldptr, req->oldlen);
1090#endif
1091                req->lock = REQ_WIRED;
1092        }
1093        return (0);
1094}
1095
1096int
1097sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1098    int *nindx, struct sysctl_req *req)
1099{
1100        struct sysctl_oid *oid;
1101        int indx;
1102
1103        oid = SLIST_FIRST(&sysctl__children);
1104        indx = 0;
1105        while (oid && indx < CTL_MAXNAME) {
1106                if (oid->oid_number == name[indx]) {
1107                        indx++;
1108                        if (oid->oid_kind & CTLFLAG_NOLOCK)
1109                                req->lock = REQ_UNLOCKED;
1110                        if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1111                                if (oid->oid_handler != NULL ||
1112                                    indx == namelen) {
1113                                        *noid = oid;
1114                                        if (nindx != NULL)
1115                                                *nindx = indx;
1116                                        return (0);
1117                                }
1118                                oid = SLIST_FIRST(
1119                                    (struct sysctl_oid_list *)oid->oid_arg1);
1120                        } else if (indx == namelen) {
1121                                *noid = oid;
1122                                if (nindx != NULL)
1123                                        *nindx = indx;
1124                                return (0);
1125                        } else {
1126                                return (ENOTDIR);
1127                        }
1128                } else {
1129                        oid = SLIST_NEXT(oid, oid_link);
1130                }
1131        }
1132        return (ENOENT);
1133}
1134
1135/*
1136 * Traverse our tree, and find the right node, execute whatever it points
1137 * to, and return the resulting error code.
1138 */
1139
1140static int
1141sysctl_root(SYSCTL_HANDLER_ARGS)
1142{
1143        struct sysctl_oid *oid;
1144        int error, indx;
1145
1146        error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1147        if (error)
1148                return (error);
1149
1150        if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1151                /*
1152                 * You can't call a sysctl when it's a node, but has
1153                 * no handler.  Inform the user that it's a node.
1154                 * The indx may or may not be the same as namelen.
1155                 */
1156                if (oid->oid_handler == NULL)
1157                        return (EISDIR);
1158        }
1159
1160        /* Is this sysctl writable? */
1161        if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1162                return (EPERM);
1163
1164#ifndef __rtems__
1165        KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1166
1167        /* Is this sysctl sensitive to securelevels? */
1168        if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1169                error = securelevel_gt(req->td->td_ucred, 0);
1170                if (error)
1171                        return (error);
1172        }
1173
1174        /* Is this sysctl writable by only privileged users? */
1175        if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1176                int flags;
1177
1178                if (oid->oid_kind & CTLFLAG_PRISON)
1179                        flags = PRISON_ROOT;
1180                else
1181                        flags = 0;
1182                error = suser_cred(req->td->td_ucred, flags);
1183                if (error)
1184                        return (error);
1185        }
1186#endif
1187
1188        if (!oid->oid_handler)
1189                return EINVAL;
1190
1191        if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1192                error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1193                    req);
1194        else
1195                error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1196                    req);
1197        return (error);
1198}
1199
1200#ifndef _SYS_SYSPROTO_H_
1201struct sysctl_args {
1202        int     *name;
1203        u_int   namelen;
1204        void    *old;
1205        size_t  *oldlenp;
1206        void    *new;
1207        size_t  newlen;
1208};
1209#endif
1210
1211/*
1212 * MPSAFE
1213 */
1214int
1215__sysctl(struct thread *td, struct sysctl_args *uap)
1216{
1217        int error, name[CTL_MAXNAME];
1218        size_t j;
1219
1220        if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1221                return (EINVAL);
1222
1223        error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1224        if (error)
1225                return (error);
1226
1227        mtx_lock(&Giant);
1228
1229        error = userland_sysctl(td, name, uap->namelen,
1230                uap->old, uap->oldlenp, 0,
1231                uap->new, uap->newlen, &j);
1232        if (error && error != ENOMEM)
1233                goto done2;
1234        if (uap->oldlenp) {
1235                int i = copyout(&j, uap->oldlenp, sizeof(j));
1236                if (i)
1237                        error = i;
1238        }
1239done2:
1240        mtx_unlock(&Giant);
1241        return (error);
1242}
1243
1244/*
1245 * This is used from various compatibility syscalls too.  That's why name
1246 * must be in kernel space.
1247 */
1248int
1249userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1250    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1251{
1252        int error = 0;
1253        struct sysctl_req req, req2;
1254
1255        bzero(&req, sizeof req);
1256
1257        req.td = td;
1258
1259        if (oldlenp) {
1260                if (inkernel) {
1261                        req.oldlen = *oldlenp;
1262                } else {
1263                        error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1264                        if (error)
1265                                return (error);
1266                }
1267        }
1268
1269        if (old) {
1270#ifndef __rtems__
1271                if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1272                        return (EFAULT);
1273#endif
1274                req.oldptr= old;
1275        }
1276
1277        if (new != NULL) {
1278#ifndef __rtems__
1279                if (!useracc(new, req.newlen, VM_PROT_READ))
1280                        return (EFAULT);
1281#endif
1282                req.newlen = newlen;
1283                req.newptr = new;
1284        }
1285
1286        req.oldfunc = sysctl_old_user;
1287        req.newfunc = sysctl_new_user;
1288        req.lock = REQ_LOCKED;
1289
1290        SYSCTL_LOCK();
1291
1292#ifdef MAC
1293        error = mac_check_system_sysctl(td->td_ucred, name, namelen, old,
1294            oldlenp, inkernel, new, newlen);
1295        if (error) {
1296                SYSCTL_UNLOCK();
1297                return (error);
1298        }
1299#endif
1300
1301        do {
1302            req2 = req;
1303            error = sysctl_root(0, name, namelen, &req2);
1304        } while (error == EAGAIN);
1305
1306        req = req2;
1307#ifndef __rtems__
1308        if (req.lock == REQ_WIRED)
1309                vsunlock(req.oldptr, req.oldlen);
1310#endif
1311
1312        SYSCTL_UNLOCK();
1313
1314        if (error && error != ENOMEM)
1315                return (error);
1316
1317        if (retval) {
1318                if (req.oldptr && req.oldidx > req.oldlen)
1319                        *retval = req.oldlen;
1320                else
1321                        *retval = req.oldidx;
1322        }
1323        return (error);
1324}
1325
1326#ifdef COMPAT_43
1327#include <sys/socket.h>
1328#include <vm/vm_param.h>
1329
1330#define KINFO_PROC              (0<<8)
1331#define KINFO_RT                (1<<8)
1332#define KINFO_VNODE             (2<<8)
1333#define KINFO_FILE              (3<<8)
1334#define KINFO_METER             (4<<8)
1335#define KINFO_LOADAVG           (5<<8)
1336#define KINFO_CLOCKRATE         (6<<8)
1337
1338/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1339#define KINFO_BSDI_SYSINFO      (101<<8)
1340
1341/*
1342 * XXX this is bloat, but I hope it's better here than on the potentially
1343 * limited kernel stack...  -Peter
1344 */
1345
1346static struct {
1347        int     bsdi_machine;           /* "i386" on BSD/386 */
1348/*      ^^^ this is an offset to the string, relative to the struct start */
1349        char    *pad0;
1350        long    pad1;
1351        long    pad2;
1352        long    pad3;
1353        u_long  pad4;
1354        u_long  pad5;
1355        u_long  pad6;
1356
1357        int     bsdi_ostype;            /* "BSD/386" on BSD/386 */
1358        int     bsdi_osrelease;         /* "1.1" on BSD/386 */
1359        long    pad7;
1360        long    pad8;
1361        char    *pad9;
1362
1363        long    pad10;
1364        long    pad11;
1365        int     pad12;
1366        long    pad13;
1367        quad_t  pad14;
1368        long    pad15;
1369
1370        struct  timeval pad16;
1371        /* we dont set this, because BSDI's uname used gethostname() instead */
1372        int     bsdi_hostname;          /* hostname on BSD/386 */
1373
1374        /* the actual string data is appended here */
1375
1376} bsdi_si;
1377/*
1378 * this data is appended to the end of the bsdi_si structure during copyout.
1379 * The "char *" offsets are relative to the base of the bsdi_si struct.
1380 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1381 * should not exceed the length of the buffer here... (or else!! :-)
1382 */
1383static char bsdi_strings[80];   /* It had better be less than this! */
1384
1385#ifndef _SYS_SYSPROTO_H_
1386struct getkerninfo_args {
1387        int     op;
1388        char    *where;
1389        size_t  *size;
1390        int     arg;
1391};
1392#endif
1393
1394/*
1395 * MPSAFE
1396 */
1397int
1398ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1399{
1400        int error, name[6];
1401        size_t size;
1402        u_int needed = 0;
1403
1404        mtx_lock(&Giant);
1405
1406        switch (uap->op & 0xff00) {
1407
1408        case KINFO_RT:
1409                name[0] = CTL_NET;
1410                name[1] = PF_ROUTE;
1411                name[2] = 0;
1412                name[3] = (uap->op & 0xff0000) >> 16;
1413                name[4] = uap->op & 0xff;
1414                name[5] = uap->arg;
1415                error = userland_sysctl(td, name, 6, uap->where, uap->size,
1416                        0, 0, 0, &size);
1417                break;
1418
1419        case KINFO_VNODE:
1420                name[0] = CTL_KERN;
1421                name[1] = KERN_VNODE;
1422                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1423                        0, 0, 0, &size);
1424                break;
1425
1426        case KINFO_PROC:
1427                name[0] = CTL_KERN;
1428                name[1] = KERN_PROC;
1429                name[2] = uap->op & 0xff;
1430                name[3] = uap->arg;
1431                error = userland_sysctl(td, name, 4, uap->where, uap->size,
1432                        0, 0, 0, &size);
1433                break;
1434
1435        case KINFO_FILE:
1436                name[0] = CTL_KERN;
1437                name[1] = KERN_FILE;
1438                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1439                        0, 0, 0, &size);
1440                break;
1441
1442        case KINFO_METER:
1443                name[0] = CTL_VM;
1444                name[1] = VM_TOTAL;
1445                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1446                        0, 0, 0, &size);
1447                break;
1448
1449        case KINFO_LOADAVG:
1450                name[0] = CTL_VM;
1451                name[1] = VM_LOADAVG;
1452                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1453                        0, 0, 0, &size);
1454                break;
1455
1456        case KINFO_CLOCKRATE:
1457                name[0] = CTL_KERN;
1458                name[1] = KERN_CLOCKRATE;
1459                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1460                        0, 0, 0, &size);
1461                break;
1462
1463        case KINFO_BSDI_SYSINFO: {
1464                /*
1465                 * this is pretty crude, but it's just enough for uname()
1466                 * from BSDI's 1.x libc to work.
1467                 *
1468                 * *size gives the size of the buffer before the call, and
1469                 * the amount of data copied after a successful call.
1470                 * If successful, the return value is the amount of data
1471                 * available, which can be larger than *size.
1472                 *
1473                 * BSDI's 2.x product apparently fails with ENOMEM if *size
1474                 * is too small.
1475                 */
1476
1477                u_int left;
1478                char *s;
1479
1480                bzero((char *)&bsdi_si, sizeof(bsdi_si));
1481                bzero(bsdi_strings, sizeof(bsdi_strings));
1482
1483                s = bsdi_strings;
1484
1485                bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1486                strcpy(s, ostype);
1487                s += strlen(s) + 1;
1488
1489                bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1490                strcpy(s, osrelease);
1491                s += strlen(s) + 1;
1492
1493                bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1494                strcpy(s, machine);
1495                s += strlen(s) + 1;
1496
1497                needed = sizeof(bsdi_si) + (s - bsdi_strings);
1498
1499                if ((uap->where == NULL) || (uap->size == NULL)) {
1500                        /* process is asking how much buffer to supply.. */
1501                        size = needed;
1502                        error = 0;
1503                        break;
1504                }
1505
1506                if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1507                        break;
1508
1509                /* if too much buffer supplied, trim it down */
1510                if (size > needed)
1511                        size = needed;
1512
1513                /* how much of the buffer is remaining */
1514                left = size;
1515
1516                if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1517                        break;
1518
1519                /* is there any point in continuing? */
1520                if (left > sizeof(bsdi_si)) {
1521                        left -= sizeof(bsdi_si);
1522                        error = copyout(&bsdi_strings,
1523                                        uap->where + sizeof(bsdi_si), left);
1524                }
1525                break;
1526        }
1527
1528        default:
1529                error = EOPNOTSUPP;
1530                break;
1531        }
1532        if (error == 0) {
1533                td->td_retval[0] = needed ? needed : size;
1534                if (uap->size) {
1535                        error = copyout(&size, uap->size, sizeof(size));
1536                }
1537        }
1538        mtx_unlock(&Giant);
1539        return (error);
1540}
1541#endif /* COMPAT_43 */
Note: See TracBrowser for help on using the repository browser.