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

5
Last change on this file since cb68253 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

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