source: rtems/cpukit/libnetworking/kern/kern_sysctl.c @ 78219ad1

4.104.114.84.95
Last change on this file since 78219ad1 was 78219ad1, checked in by Joel Sherrill <joel.sherrill@…>, on 12/02/04 at 20:24:30

2004-12-02 Joel Sherrill <joel@…>

  • libnetworking/kern/kern_sysctl.c, libnetworking/netinet/ip_mroute.c, libnetworking/sys/socketvar.h: Remove warnings.
  • 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 <sys/buf.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 int 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                /* Later accessed by macro SYSCTL_CHILDREN(oidp) */
396                oidp->oid_arg1 = malloc(sizeof(struct sysctl_oid_list),
397                    M_SYSCTLOID, M_WAITOK);
398                SLIST_INIT(SYSCTL_CHILDREN(oidp));
399        } else {
400                oidp->oid_arg1 = arg1;
401                oidp->oid_arg2 = arg2;
402        }
403        oidp->oid_fmt = fmt;
404        if (descr) {
405                int len = strlen(descr) + 1;
406                oidp->descr = malloc(len, M_SYSCTLOID, M_WAITOK);
407                if (oidp->descr)
408                        strcpy((char *)(uintptr_t)(const void *)oidp->descr, descr);
409        }
410        /* Update the context, if used */
411        if (clist != NULL)
412                sysctl_ctx_entry_add(clist, oidp);
413        /* Register this oid */
414        sysctl_register_oid(oidp);
415        return (oidp);
416}
417
418/*
419 * Register the kernel's oids on startup.
420 */
421SET_DECLARE(sysctl_set, struct sysctl_oid);
422
423#if defined(__rtems__)
424void
425#else
426static void
427#endif
428sysctl_register_all(void *arg)
429{
430        struct sysctl_oid **oidp;
431
432        SYSCTL_INIT();
433        SET_FOREACH(oidp, sysctl_set)
434                sysctl_register_oid(*oidp);
435}
436SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
437
438/*
439 * "Staff-functions"
440 *
441 * These functions implement a presently undocumented interface
442 * used by the sysctl program to walk the tree, and get the type
443 * so it can print the value.
444 * This interface is under work and consideration, and should probably
445 * be killed with a big axe by the first person who can find the time.
446 * (be aware though, that the proper interface isn't as obvious as it
447 * may seem, there are various conflicting requirements.
448 *
449 * {0,0}        printf the entire MIB-tree.
450 * {0,1,...}    return the name of the "..." OID.
451 * {0,2,...}    return the next OID.
452 * {0,3}        return the OID of the name in "new"
453 * {0,4,...}    return the kind & format info for the "..." OID.
454 * {0,5,...}    return the description the "..." OID.
455 */
456
457static void
458sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
459{
460        int k;
461        struct sysctl_oid *oidp;
462
463        SLIST_FOREACH(oidp, l, oid_link) {
464
465                for (k=0; k<i; k++)
466                        printf(" ");
467
468                printf("%d %s ", oidp->oid_number, oidp->oid_name);
469
470                printf("%c%c",
471                        oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
472                        oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
473
474                if (oidp->oid_handler)
475                        printf(" *Handler");
476
477                switch (oidp->oid_kind & CTLTYPE) {
478                        case CTLTYPE_NODE:
479                                printf(" Node\n");
480                                if (!oidp->oid_handler) {
481                                        sysctl_sysctl_debug_dump_node(
482                                                oidp->oid_arg1, i+2);
483                                }
484                                break;
485                        case CTLTYPE_INT:    printf(" Int\n"); break;
486                        case CTLTYPE_STRING: printf(" String\n"); break;
487                        case CTLTYPE_QUAD:   printf(" Quad\n"); break;
488                        case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
489                        default:             printf("\n");
490                }
491
492        }
493}
494
495static int
496sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
497{
498#ifndef __rtems__
499        int error;
500
501        error = suser(req->td);
502        if (error)
503                return error;
504#endif
505        sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
506        return ENOENT;
507}
508
509SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
510        0, 0, sysctl_sysctl_debug, "-", "");
511
512static int
513sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
514{
515        int *name = (int *) arg1;
516        u_int namelen = arg2;
517        int error = 0;
518        struct sysctl_oid *oid;
519        struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
520        char buf[10];
521
522        while (namelen) {
523                if (!lsp) {
524                        snprintf(buf,sizeof(buf),"%d",*name);
525                        if (req->oldidx)
526                                error = SYSCTL_OUT(req, ".", 1);
527                        if (!error)
528                                error = SYSCTL_OUT(req, buf, strlen(buf));
529                        if (error)
530                                return (error);
531                        namelen--;
532                        name++;
533                        continue;
534                }
535                lsp2 = 0;
536                SLIST_FOREACH(oid, lsp, oid_link) {
537                        if (oid->oid_number != *name)
538                                continue;
539
540                        if (req->oldidx)
541                                error = SYSCTL_OUT(req, ".", 1);
542                        if (!error)
543                                error = SYSCTL_OUT(req, oid->oid_name,
544                                        strlen(oid->oid_name));
545                        if (error)
546                                return (error);
547
548                        namelen--;
549                        name++;
550
551                        if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
552                                break;
553
554                        if (oid->oid_handler)
555                                break;
556
557                        lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
558                        break;
559                }
560                lsp = lsp2;
561        }
562        return (SYSCTL_OUT(req, "", 1));
563}
564
565SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
566
567static int
568sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
569        int *next, int *len, int level, struct sysctl_oid **oidpp)
570{
571        struct sysctl_oid *oidp;
572
573        *len = level;
574        SLIST_FOREACH(oidp, lsp, oid_link) {
575                *next = oidp->oid_number;
576                *oidpp = oidp;
577
578                if (oidp->oid_kind & CTLFLAG_SKIP)
579                        continue;
580
581                if (!namelen) {
582                        if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
583                                return 0;
584                        if (oidp->oid_handler)
585                                /* We really should call the handler here...*/
586                                return 0;
587                        lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
588                        if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
589                                len, level+1, oidpp))
590                                return 0;
591                        goto emptynode;
592                }
593
594                if (oidp->oid_number < *name)
595                        continue;
596
597                if (oidp->oid_number > *name) {
598                        if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
599                                return 0;
600                        if (oidp->oid_handler)
601                                return 0;
602                        lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
603                        if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
604                                next+1, len, level+1, oidpp))
605                                return (0);
606                        goto next;
607                }
608                if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
609                        continue;
610
611                if (oidp->oid_handler)
612                        continue;
613
614                lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
615                if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
616                        len, level+1, oidpp))
617                        return (0);
618        next:
619                namelen = 1;
620        emptynode:
621                *len = level;
622        }
623        return 1;
624}
625
626static int
627sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
628{
629        int *name = (int *) arg1;
630        u_int namelen = arg2;
631        int i, j, error;
632        struct sysctl_oid *oid;
633        struct sysctl_oid_list *lsp = &sysctl__children;
634        int newoid[CTL_MAXNAME];
635
636        i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
637        if (i)
638                return ENOENT;
639        error = SYSCTL_OUT(req, newoid, j * sizeof (int));
640        return (error);
641}
642
643SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
644
645static int
646name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
647{
648        int i;
649        struct sysctl_oid *oidp;
650        struct sysctl_oid_list *lsp = &sysctl__children;
651        char *p;
652
653        if (!*name)
654                return ENOENT;
655
656        p = name + strlen(name) - 1 ;
657        if (*p == '.')
658                *p = '\0';
659
660        *len = 0;
661
662        for (p = name; *p && *p != '.'; p++)
663                ;
664        i = *p;
665        if (i == '.')
666                *p = '\0';
667
668        oidp = SLIST_FIRST(lsp);
669
670        while (oidp && *len < CTL_MAXNAME) {
671                if (strcmp(name, oidp->oid_name)) {
672                        oidp = SLIST_NEXT(oidp, oid_link);
673                        continue;
674                }
675                *oid++ = oidp->oid_number;
676                (*len)++;
677
678                if (!i) {
679                        if (oidpp)
680                                *oidpp = oidp;
681                        return (0);
682                }
683
684                if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
685                        break;
686
687                if (oidp->oid_handler)
688                        break;
689
690                lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
691                oidp = SLIST_FIRST(lsp);
692                name = p+1;
693                for (p = name; *p && *p != '.'; p++)
694                                ;
695                i = *p;
696                if (i == '.')
697                        *p = '\0';
698        }
699        return ENOENT;
700}
701
702static int
703sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
704{
705        char *p;
706        int error, oid[CTL_MAXNAME], len;
707        struct sysctl_oid *op = 0;
708
709        if (!req->newlen)
710                return ENOENT;
711        if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
712                return (ENAMETOOLONG);
713
714        p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
715
716        error = SYSCTL_IN(req, p, req->newlen);
717        if (error) {
718                free(p, M_SYSCTL);
719                return (error);
720        }
721
722        p [req->newlen] = '\0';
723
724        error = name2oid(p, oid, &len, &op);
725
726        free(p, M_SYSCTL);
727
728        if (error)
729                return (error);
730
731        error = SYSCTL_OUT(req, oid, len * sizeof *oid);
732        return (error);
733}
734
735SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
736        sysctl_sysctl_name2oid, "I", "");
737
738static int
739sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
740{
741        struct sysctl_oid *oid;
742        int error;
743
744        error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
745        if (error)
746                return (error);
747
748        if (!oid->oid_fmt)
749                return (ENOENT);
750        error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
751        if (error)
752                return (error);
753        error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
754        return (error);
755}
756
757
758SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
759
760static int
761sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
762{
763        struct sysctl_oid *oid;
764        int error;
765
766        error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
767        if (error)
768                return (error);
769
770        if (!oid->descr)
771                return (ENOENT);
772        error = SYSCTL_OUT(req, oid->descr, strlen(oid->descr) + 1);
773        return (error);
774}
775
776SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
777
778/*
779 * Default "handler" functions.
780 */
781
782/*
783 * Handle an int, signed or unsigned.
784 * Two cases:
785 *     a variable:  point arg1 at it.
786 *     a constant:  pass it in arg2.
787 */
788
789int
790sysctl_handle_int(SYSCTL_HANDLER_ARGS)
791{
792        int tmpout, error = 0;
793
794        /*
795         * Attempt to get a coherent snapshot by making a copy of the data.
796         */
797        if (arg1)
798                tmpout = *(int *)arg1;
799        else
800                tmpout = arg2;
801        error = SYSCTL_OUT(req, &tmpout, sizeof(int));
802
803        if (error || !req->newptr)
804                return (error);
805
806        if (!arg1)
807                error = EPERM;
808        else
809                error = SYSCTL_IN(req, arg1, sizeof(int));
810        return (error);
811}
812
813/*
814 * Handle a long, signed or unsigned.  arg1 points to it.
815 */
816
817int
818sysctl_handle_long(SYSCTL_HANDLER_ARGS)
819{
820        int error = 0;
821        long tmpout;
822
823        /*
824         * Attempt to get a coherent snapshot by making a copy of the data.
825         */
826        if (!arg1)
827                return (EINVAL);
828        tmpout = *(long *)arg1;
829        error = SYSCTL_OUT(req, &tmpout, sizeof(long));
830
831        if (error || !req->newptr)
832                return (error);
833
834        error = SYSCTL_IN(req, arg1, sizeof(long));
835        return (error);
836}
837
838/*
839 * Handle our generic '\0' terminated 'C' string.
840 * Two cases:
841 *      a variable string:  point arg1 at it, arg2 is max length.
842 *      a constant string:  point arg1 at it, arg2 is zero.
843 */
844
845int
846sysctl_handle_string(SYSCTL_HANDLER_ARGS)
847{
848        int error=0;
849        char *tmparg;
850        size_t outlen;
851
852        /*
853         * Attempt to get a coherent snapshot by copying to a
854         * temporary kernel buffer.
855         */
856retry:
857        outlen = strlen((char *)arg1)+1;
858        tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
859
860        if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
861                free(tmparg, M_SYSCTLTMP);
862                goto retry;
863        }
864
865        error = SYSCTL_OUT(req, tmparg, outlen);
866        free(tmparg, M_SYSCTLTMP);
867
868        if (error || !req->newptr)
869                return (error);
870
871        if ((req->newlen - req->newidx) >= arg2) {
872                error = EINVAL;
873        } else {
874                arg2 = (req->newlen - req->newidx);
875                error = SYSCTL_IN(req, arg1, arg2);
876                ((char *)arg1)[arg2] = '\0';
877        }
878
879        return (error);
880}
881
882/*
883 * Handle any kind of opaque data.
884 * arg1 points to it, arg2 is the size.
885 */
886
887int
888sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
889{
890        int error;
891        void *tmparg;
892
893        /*
894         * Attempt to get a coherent snapshot, either by wiring the
895         * user space buffer or copying to a temporary kernel buffer
896         * depending on the size of the data.
897         */
898        if (arg2 > PAGE_SIZE) {
899                sysctl_wire_old_buffer(req, arg2);
900                error = SYSCTL_OUT(req, arg1, arg2);
901        } else {
902                tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
903                bcopy(arg1, tmparg, arg2);
904                error = SYSCTL_OUT(req, tmparg, arg2);
905                free(tmparg, M_SYSCTLTMP);
906        }
907
908        if (error || !req->newptr)
909                return (error);
910
911        error = SYSCTL_IN(req, arg1, arg2);
912
913        return (error);
914}
915
916/*
917 * Transfer functions to/from kernel space.
918 * XXX: rather untested at this point
919 */
920static int
921sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
922{
923        size_t i = 0;
924
925        if (req->oldptr) {
926                i = l;
927                if (req->oldlen <= req->oldidx)
928                        i = 0;
929                else
930                        if (i > req->oldlen - req->oldidx)
931                                i = req->oldlen - req->oldidx;
932                if (i > 0)
933                        bcopy(p, (char *)req->oldptr + req->oldidx, i);
934        }
935        req->oldidx += l;
936        if (req->oldptr && i != l)
937                return (ENOMEM);
938        return (0);
939}
940
941static int
942sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
943{
944        if (!req->newptr)
945                return 0;
946        if (req->newlen - req->newidx < l)
947                return (EINVAL);
948        bcopy((char *)req->newptr + req->newidx, p, l);
949        req->newidx += l;
950        return (0);
951}
952
953int
954kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
955    size_t *oldlenp, void *new, size_t newlen, size_t *retval)
956{
957        int error = 0;
958        struct sysctl_req req;
959
960        bzero(&req, sizeof req);
961
962        req.td = td;
963
964        if (oldlenp) {
965                req.oldlen = *oldlenp;
966        }
967
968        if (old) {
969                req.oldptr= old;
970        }
971
972        if (new != NULL) {
973                req.newlen = newlen;
974                req.newptr = new;
975        }
976
977        req.oldfunc = sysctl_old_kernel;
978        req.newfunc = sysctl_new_kernel;
979        req.lock = REQ_LOCKED;
980
981        SYSCTL_LOCK();
982
983        error = sysctl_root(0, name, namelen, &req);
984
985        if (req.lock == REQ_WIRED)
986#ifdef __rtems__
987    printf ("kern_sysctl: vsunlock needs to be called!\n");
988#else
989                vsunlock(req.oldptr, req.oldlen);
990#endif
991
992        SYSCTL_UNLOCK();
993
994        if (error && error != ENOMEM)
995                return (error);
996
997        if (retval) {
998                if (req.oldptr && req.oldidx > req.oldlen)
999                        *retval = req.oldlen;
1000                else
1001                        *retval = req.oldidx;
1002        }
1003        return (error);
1004}
1005
1006int
1007kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1008    void *new, size_t newlen, size_t *retval)
1009{
1010        int oid[CTL_MAXNAME];
1011        size_t oidlen, plen;
1012        int error;
1013
1014        oid[0] = 0;             /* sysctl internal magic */
1015        oid[1] = 3;             /* name2oid */
1016        oidlen = sizeof(oid);
1017
1018        error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1019            (void *)name, strlen(name), &plen);
1020        if (error)
1021                return (error);
1022
1023        error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1024            new, newlen, retval);
1025        return (error);
1026}
1027
1028/*
1029 * Transfer function to/from user space.
1030 */
1031static int
1032sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1033{
1034        int error = 0;
1035        size_t i = 0;
1036
1037#ifndef __rtems__
1038        if (req->lock == 1 && req->oldptr)
1039                WITNESS_SLEEP(1, NULL);
1040#endif
1041        if (req->oldptr) {
1042                i = l;
1043                if (req->oldlen <= req->oldidx)
1044                        i = 0;
1045                else
1046                        if (i > req->oldlen - req->oldidx)
1047                                i = req->oldlen - req->oldidx;
1048                if (i > 0)
1049                        error = copyout(p, (char *)req->oldptr + req->oldidx,
1050                                        i);
1051        }
1052        req->oldidx += l;
1053        if (error)
1054                return (error);
1055        if (req->oldptr && i < l)
1056                return (ENOMEM);
1057        return (0);
1058}
1059
1060static int
1061sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1062{
1063        int error;
1064
1065        if (!req->newptr)
1066                return 0;
1067        if (req->newlen - req->newidx < l)
1068                return (EINVAL);
1069        error = copyin((char *)req->newptr + req->newidx, p, l);
1070        req->newidx += l;
1071        return (error);
1072}
1073
1074/*
1075 * Wire the user space destination buffer.  If set to a value greater than
1076 * zero, the len parameter limits the maximum amount of wired memory.
1077 *
1078 * XXX - The len parameter is currently ignored due to the lack of
1079 * a place to save it in the sysctl_req structure so that the matching
1080 * amount of memory can be unwired in the sysctl exit code.
1081 */
1082int
1083sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1084{
1085        if (req->lock == REQ_LOCKED && req->oldptr &&
1086            req->oldfunc == sysctl_old_user) {
1087#ifndef __rtems__
1088                vslock(req->oldptr, req->oldlen);
1089#endif
1090                req->lock = REQ_WIRED;
1091        }
1092        return (0);
1093}
1094
1095int
1096sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1097    int *nindx, struct sysctl_req *req)
1098{
1099        struct sysctl_oid *oid;
1100        int indx;
1101
1102        oid = SLIST_FIRST(&sysctl__children);
1103        indx = 0;
1104        while (oid && indx < CTL_MAXNAME) {
1105                if (oid->oid_number == name[indx]) {
1106                        indx++;
1107                        if (oid->oid_kind & CTLFLAG_NOLOCK)
1108                                req->lock = REQ_UNLOCKED;
1109                        if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1110                                if (oid->oid_handler != NULL ||
1111                                    indx == namelen) {
1112                                        *noid = oid;
1113                                        if (nindx != NULL)
1114                                                *nindx = indx;
1115                                        return (0);
1116                                }
1117                                oid = SLIST_FIRST(
1118                                    (struct sysctl_oid_list *)oid->oid_arg1);
1119                        } else if (indx == namelen) {
1120                                *noid = oid;
1121                                if (nindx != NULL)
1122                                        *nindx = indx;
1123                                return (0);
1124                        } else {
1125                                return (ENOTDIR);
1126                        }
1127                } else {
1128                        oid = SLIST_NEXT(oid, oid_link);
1129                }
1130        }
1131        return (ENOENT);
1132}
1133
1134/*
1135 * Traverse our tree, and find the right node, execute whatever it points
1136 * to, and return the resulting error code.
1137 */
1138
1139static int
1140sysctl_root(SYSCTL_HANDLER_ARGS)
1141{
1142        struct sysctl_oid *oid;
1143        int error, indx;
1144
1145        error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1146        if (error)
1147                return (error);
1148
1149        if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1150                /*
1151                 * You can't call a sysctl when it's a node, but has
1152                 * no handler.  Inform the user that it's a node.
1153                 * The indx may or may not be the same as namelen.
1154                 */
1155                if (oid->oid_handler == NULL)
1156                        return (EISDIR);
1157        }
1158
1159        /* Is this sysctl writable? */
1160        if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1161                return (EPERM);
1162
1163#ifndef __rtems__
1164        KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1165
1166        /* Is this sysctl sensitive to securelevels? */
1167        if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1168                error = securelevel_gt(req->td->td_ucred, 0);
1169                if (error)
1170                        return (error);
1171        }
1172
1173        /* Is this sysctl writable by only privileged users? */
1174        if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1175                int flags;
1176
1177                if (oid->oid_kind & CTLFLAG_PRISON)
1178                        flags = PRISON_ROOT;
1179                else
1180                        flags = 0;
1181                error = suser_cred(req->td->td_ucred, flags);
1182                if (error)
1183                        return (error);
1184        }
1185#endif
1186
1187        if (!oid->oid_handler)
1188                return EINVAL;
1189
1190        if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1191                error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1192                    req);
1193        else
1194                error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1195                    req);
1196        return (error);
1197}
1198
1199#ifndef _SYS_SYSPROTO_H_
1200struct sysctl_args {
1201        int     *name;
1202        u_int   namelen;
1203        void    *old;
1204        size_t  *oldlenp;
1205        void    *new;
1206        size_t  newlen;
1207};
1208#endif
1209
1210/*
1211 * MPSAFE
1212 */
1213int
1214__sysctl(struct thread *td, struct sysctl_args *uap)
1215{
1216        int error, name[CTL_MAXNAME];
1217        size_t j;
1218
1219        if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1220                return (EINVAL);
1221
1222        error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1223        if (error)
1224                return (error);
1225
1226        mtx_lock(&Giant);
1227
1228        error = userland_sysctl(td, name, uap->namelen,
1229                uap->old, uap->oldlenp, 0,
1230                uap->new, uap->newlen, &j);
1231        if (error && error != ENOMEM)
1232                goto done2;
1233        if (uap->oldlenp) {
1234                int i = copyout(&j, uap->oldlenp, sizeof(j));
1235                if (i)
1236                        error = i;
1237        }
1238done2:
1239        mtx_unlock(&Giant);
1240        return (error);
1241}
1242
1243/*
1244 * This is used from various compatibility syscalls too.  That's why name
1245 * must be in kernel space.
1246 */
1247int
1248userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1249    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1250{
1251        int error = 0;
1252        struct sysctl_req req, req2;
1253
1254        bzero(&req, sizeof req);
1255
1256        req.td = td;
1257
1258        if (oldlenp) {
1259                if (inkernel) {
1260                        req.oldlen = *oldlenp;
1261                } else {
1262                        error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1263                        if (error)
1264                                return (error);
1265                }
1266        }
1267
1268        if (old) {
1269#ifndef __rtems__
1270                if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1271                        return (EFAULT);
1272#endif
1273                req.oldptr= old;
1274        }
1275
1276        if (new != NULL) {
1277#ifndef __rtems__
1278                if (!useracc(new, req.newlen, VM_PROT_READ))
1279                        return (EFAULT);
1280#endif
1281                req.newlen = newlen;
1282                req.newptr = new;
1283        }
1284
1285        req.oldfunc = sysctl_old_user;
1286        req.newfunc = sysctl_new_user;
1287        req.lock = REQ_LOCKED;
1288
1289        SYSCTL_LOCK();
1290
1291#ifdef MAC
1292        error = mac_check_system_sysctl(td->td_ucred, name, namelen, old,
1293            oldlenp, inkernel, new, newlen);
1294        if (error) {
1295                SYSCTL_UNLOCK();
1296                return (error);
1297        }
1298#endif
1299
1300        do {
1301            req2 = req;
1302            error = sysctl_root(0, name, namelen, &req2);
1303        } while (error == EAGAIN);
1304
1305        req = req2;
1306#ifndef __rtems__
1307        if (req.lock == REQ_WIRED)
1308                vsunlock(req.oldptr, req.oldlen);
1309#endif
1310
1311        SYSCTL_UNLOCK();
1312
1313        if (error && error != ENOMEM)
1314                return (error);
1315
1316        if (retval) {
1317                if (req.oldptr && req.oldidx > req.oldlen)
1318                        *retval = req.oldlen;
1319                else
1320                        *retval = req.oldidx;
1321        }
1322        return (error);
1323}
1324
1325#ifdef COMPAT_43
1326#include <sys/socket.h>
1327#include <vm/vm_param.h>
1328
1329#define KINFO_PROC              (0<<8)
1330#define KINFO_RT                (1<<8)
1331#define KINFO_VNODE             (2<<8)
1332#define KINFO_FILE              (3<<8)
1333#define KINFO_METER             (4<<8)
1334#define KINFO_LOADAVG           (5<<8)
1335#define KINFO_CLOCKRATE         (6<<8)
1336
1337/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1338#define KINFO_BSDI_SYSINFO      (101<<8)
1339
1340/*
1341 * XXX this is bloat, but I hope it's better here than on the potentially
1342 * limited kernel stack...  -Peter
1343 */
1344
1345static struct {
1346        int     bsdi_machine;           /* "i386" on BSD/386 */
1347/*      ^^^ this is an offset to the string, relative to the struct start */
1348        char    *pad0;
1349        long    pad1;
1350        long    pad2;
1351        long    pad3;
1352        u_long  pad4;
1353        u_long  pad5;
1354        u_long  pad6;
1355
1356        int     bsdi_ostype;            /* "BSD/386" on BSD/386 */
1357        int     bsdi_osrelease;         /* "1.1" on BSD/386 */
1358        long    pad7;
1359        long    pad8;
1360        char    *pad9;
1361
1362        long    pad10;
1363        long    pad11;
1364        int     pad12;
1365        long    pad13;
1366        quad_t  pad14;
1367        long    pad15;
1368
1369        struct  timeval pad16;
1370        /* we dont set this, because BSDI's uname used gethostname() instead */
1371        int     bsdi_hostname;          /* hostname on BSD/386 */
1372
1373        /* the actual string data is appended here */
1374
1375} bsdi_si;
1376/*
1377 * this data is appended to the end of the bsdi_si structure during copyout.
1378 * The "char *" offsets are relative to the base of the bsdi_si struct.
1379 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1380 * should not exceed the length of the buffer here... (or else!! :-)
1381 */
1382static char bsdi_strings[80];   /* It had better be less than this! */
1383
1384#ifndef _SYS_SYSPROTO_H_
1385struct getkerninfo_args {
1386        int     op;
1387        char    *where;
1388        size_t  *size;
1389        int     arg;
1390};
1391#endif
1392
1393/*
1394 * MPSAFE
1395 */
1396int
1397ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1398{
1399        int error, name[6];
1400        size_t size;
1401        u_int needed = 0;
1402
1403        mtx_lock(&Giant);
1404
1405        switch (uap->op & 0xff00) {
1406
1407        case KINFO_RT:
1408                name[0] = CTL_NET;
1409                name[1] = PF_ROUTE;
1410                name[2] = 0;
1411                name[3] = (uap->op & 0xff0000) >> 16;
1412                name[4] = uap->op & 0xff;
1413                name[5] = uap->arg;
1414                error = userland_sysctl(td, name, 6, uap->where, uap->size,
1415                        0, 0, 0, &size);
1416                break;
1417
1418        case KINFO_VNODE:
1419                name[0] = CTL_KERN;
1420                name[1] = KERN_VNODE;
1421                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1422                        0, 0, 0, &size);
1423                break;
1424
1425        case KINFO_PROC:
1426                name[0] = CTL_KERN;
1427                name[1] = KERN_PROC;
1428                name[2] = uap->op & 0xff;
1429                name[3] = uap->arg;
1430                error = userland_sysctl(td, name, 4, uap->where, uap->size,
1431                        0, 0, 0, &size);
1432                break;
1433
1434        case KINFO_FILE:
1435                name[0] = CTL_KERN;
1436                name[1] = KERN_FILE;
1437                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1438                        0, 0, 0, &size);
1439                break;
1440
1441        case KINFO_METER:
1442                name[0] = CTL_VM;
1443                name[1] = VM_TOTAL;
1444                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1445                        0, 0, 0, &size);
1446                break;
1447
1448        case KINFO_LOADAVG:
1449                name[0] = CTL_VM;
1450                name[1] = VM_LOADAVG;
1451                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1452                        0, 0, 0, &size);
1453                break;
1454
1455        case KINFO_CLOCKRATE:
1456                name[0] = CTL_KERN;
1457                name[1] = KERN_CLOCKRATE;
1458                error = userland_sysctl(td, name, 2, uap->where, uap->size,
1459                        0, 0, 0, &size);
1460                break;
1461
1462        case KINFO_BSDI_SYSINFO: {
1463                /*
1464                 * this is pretty crude, but it's just enough for uname()
1465                 * from BSDI's 1.x libc to work.
1466                 *
1467                 * *size gives the size of the buffer before the call, and
1468                 * the amount of data copied after a successful call.
1469                 * If successful, the return value is the amount of data
1470                 * available, which can be larger than *size.
1471                 *
1472                 * BSDI's 2.x product apparently fails with ENOMEM if *size
1473                 * is too small.
1474                 */
1475
1476                u_int left;
1477                char *s;
1478
1479                bzero((char *)&bsdi_si, sizeof(bsdi_si));
1480                bzero(bsdi_strings, sizeof(bsdi_strings));
1481
1482                s = bsdi_strings;
1483
1484                bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1485                strcpy(s, ostype);
1486                s += strlen(s) + 1;
1487
1488                bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1489                strcpy(s, osrelease);
1490                s += strlen(s) + 1;
1491
1492                bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1493                strcpy(s, machine);
1494                s += strlen(s) + 1;
1495
1496                needed = sizeof(bsdi_si) + (s - bsdi_strings);
1497
1498                if ((uap->where == NULL) || (uap->size == NULL)) {
1499                        /* process is asking how much buffer to supply.. */
1500                        size = needed;
1501                        error = 0;
1502                        break;
1503                }
1504
1505                if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1506                        break;
1507
1508                /* if too much buffer supplied, trim it down */
1509                if (size > needed)
1510                        size = needed;
1511
1512                /* how much of the buffer is remaining */
1513                left = size;
1514
1515                if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1516                        break;
1517
1518                /* is there any point in continuing? */
1519                if (left > sizeof(bsdi_si)) {
1520                        left -= sizeof(bsdi_si);
1521                        error = copyout(&bsdi_strings,
1522                                        uap->where + sizeof(bsdi_si), left);
1523                }
1524                break;
1525        }
1526
1527        default:
1528                error = EOPNOTSUPP;
1529                break;
1530        }
1531        if (error == 0) {
1532                td->td_retval[0] = needed ? needed : size;
1533                if (uap->size) {
1534                        error = copyout(&size, uap->size, sizeof(size));
1535                }
1536        }
1537        mtx_unlock(&Giant);
1538        return (error);
1539}
1540#endif /* COMPAT_43 */
Note: See TracBrowser for help on using the repository browser.