]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_sysctl.c
This commit was generated by cvs2svn to compensate for changes in r170754,
[FreeBSD/FreeBSD.git] / sys / kern / kern_sysctl.c
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  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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/priv.h>
50 #include <sys/proc.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/sx.h>
54 #include <sys/sysproto.h>
55
56 #include <security/mac/mac_framework.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_extern.h>
60
61 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
62 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
63 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
64
65 /*
66  * Locking - this locks the sysctl tree in memory.
67  */
68 static struct sx sysctllock;
69
70 #define SYSCTL_LOCK()           sx_xlock(&sysctllock)
71 #define SYSCTL_UNLOCK()         sx_xunlock(&sysctllock)
72 #define SYSCTL_INIT()           sx_init(&sysctllock, "sysctl lock")
73
74 static int sysctl_root(SYSCTL_HANDLER_ARGS);
75
76 struct sysctl_oid_list sysctl__children; /* root list */
77
78 static struct sysctl_oid *
79 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
80 {
81         struct sysctl_oid *oidp;
82
83         SLIST_FOREACH(oidp, list, oid_link) {
84                 if (strcmp(oidp->oid_name, name) == 0) {
85                         return (oidp);
86                 }
87         }
88         return (NULL);
89 }
90
91 /*
92  * Initialization of the MIB tree.
93  *
94  * Order by number in each list.
95  */
96
97 void
98 sysctl_register_oid(struct sysctl_oid *oidp)
99 {
100         struct sysctl_oid_list *parent = oidp->oid_parent;
101         struct sysctl_oid *p;
102         struct sysctl_oid *q;
103
104         /*
105          * First check if another oid with the same name already
106          * exists in the parent's list.
107          */
108         p = sysctl_find_oidname(oidp->oid_name, parent);
109         if (p != NULL) {
110                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
111                         p->oid_refcnt++;
112                         return;
113                 } else {
114                         printf("can't re-use a leaf (%s)!\n", p->oid_name);
115                         return;
116                 }
117         }
118         /*
119          * If this oid has a number OID_AUTO, give it a number which
120          * is greater than any current oid.
121          * NOTE: DO NOT change the starting value here, change it in
122          * <sys/sysctl.h>, and make sure it is at least 256 to
123          * accomodate e.g. net.inet.raw as a static sysctl node.
124          */
125         if (oidp->oid_number == OID_AUTO) {
126                 static int newoid = CTL_AUTO_START;
127
128                 oidp->oid_number = newoid++;
129                 if (newoid == 0x7fffffff)
130                         panic("out of oids");
131         }
132 #if 0
133         else if (oidp->oid_number >= CTL_AUTO_START) {
134                 /* do not panic; this happens when unregistering sysctl sets */
135                 printf("static sysctl oid too high: %d", oidp->oid_number);
136         }
137 #endif
138
139         /*
140          * Insert the oid into the parent's list in order.
141          */
142         q = NULL;
143         SLIST_FOREACH(p, parent, oid_link) {
144                 if (oidp->oid_number < p->oid_number)
145                         break;
146                 q = p;
147         }
148         if (q)
149                 SLIST_INSERT_AFTER(q, oidp, oid_link);
150         else
151                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
152 }
153
154 void
155 sysctl_unregister_oid(struct sysctl_oid *oidp)
156 {
157         struct sysctl_oid *p;
158         int error;
159
160         error = ENOENT;
161         if (oidp->oid_number == OID_AUTO) {
162                 error = EINVAL;
163         } else {
164                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
165                         if (p == oidp) {
166                                 SLIST_REMOVE(oidp->oid_parent, oidp,
167                                     sysctl_oid, oid_link);
168                                 error = 0;
169                                 break;
170                         }
171                 }
172         }
173
174         /* 
175          * This can happen when a module fails to register and is
176          * being unloaded afterwards.  It should not be a panic()
177          * for normal use.
178          */
179         if (error)
180                 printf("%s: failed to unregister sysctl\n", __func__);
181 }
182
183 /* Initialize a new context to keep track of dynamically added sysctls. */
184 int
185 sysctl_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 */
196 int
197 sysctl_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 */
244 struct sysctl_ctx_entry *
245 sysctl_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 */
258 struct sysctl_ctx_entry *
259 sysctl_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  */
277 int
278 sysctl_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  */
299 int
300 sysctl_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->oid_descr)
342                                 free((void *)(uintptr_t)(const void *)oidp->oid_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  */
355 struct sysctl_oid *
356 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
357         int number, const char *name, int kind, void *arg1, int arg2,
358         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
359 {
360         struct sysctl_oid *oidp;
361         ssize_t len;
362         char *newname;
363
364         /* You have to hook up somewhere.. */
365         if (parent == NULL)
366                 return(NULL);
367         /* Check if the node already exists, otherwise create it */
368         oidp = sysctl_find_oidname(name, parent);
369         if (oidp != NULL) {
370                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
371                         oidp->oid_refcnt++;
372                         /* Update the context */
373                         if (clist != NULL)
374                                 sysctl_ctx_entry_add(clist, oidp);
375                         return (oidp);
376                 } else {
377                         printf("can't re-use a leaf (%s)!\n", name);
378                         return (NULL);
379                 }
380         }
381         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
382         oidp->oid_parent = parent;
383         SLIST_NEXT(oidp, oid_link) = NULL;
384         oidp->oid_number = number;
385         oidp->oid_refcnt = 1;
386         len = strlen(name);
387         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
388         bcopy(name, newname, len + 1);
389         newname[len] = '\0';
390         oidp->oid_name = newname;
391         oidp->oid_handler = handler;
392         oidp->oid_kind = CTLFLAG_DYN | kind;
393         if ((kind & CTLTYPE) == CTLTYPE_NODE) {
394                 /* Allocate space for children */
395                 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
396                     M_SYSCTLOID, M_WAITOK));
397                 SLIST_INIT(SYSCTL_CHILDREN(oidp));
398         } else {
399                 oidp->oid_arg1 = arg1;
400                 oidp->oid_arg2 = arg2;
401         }
402         oidp->oid_fmt = fmt;
403         if (descr) {
404                 int len = strlen(descr) + 1;
405                 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
406                 if (oidp->oid_descr)
407                         strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
408         }
409         /* Update the context, if used */
410         if (clist != NULL)
411                 sysctl_ctx_entry_add(clist, oidp);
412         /* Register this oid */
413         sysctl_register_oid(oidp);
414         return (oidp);
415 }
416
417 /*
418  * Reparent an existing oid.
419  */
420 int
421 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
422 {
423         struct sysctl_oid *oidp;
424
425         if (oid->oid_parent == parent)
426                 return (0);
427         oidp = sysctl_find_oidname(oid->oid_name, parent);
428         if (oidp != NULL)
429                 return (EEXIST);
430         sysctl_unregister_oid(oid);
431         oid->oid_parent = parent;
432         oid->oid_number = OID_AUTO;
433         sysctl_register_oid(oid);
434         return (0);
435 }
436
437 /*
438  * Register the kernel's oids on startup.
439  */
440 SET_DECLARE(sysctl_set, struct sysctl_oid);
441
442 static void
443 sysctl_register_all(void *arg)
444 {
445         struct sysctl_oid **oidp;
446
447         SYSCTL_INIT();
448         SET_FOREACH(oidp, sysctl_set)
449                 sysctl_register_oid(*oidp);
450 }
451 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
452
453 /*
454  * "Staff-functions"
455  *
456  * These functions implement a presently undocumented interface 
457  * used by the sysctl program to walk the tree, and get the type
458  * so it can print the value.
459  * This interface is under work and consideration, and should probably
460  * be killed with a big axe by the first person who can find the time.
461  * (be aware though, that the proper interface isn't as obvious as it
462  * may seem, there are various conflicting requirements.
463  *
464  * {0,0}        printf the entire MIB-tree.
465  * {0,1,...}    return the name of the "..." OID.
466  * {0,2,...}    return the next OID.
467  * {0,3}        return the OID of the name in "new"
468  * {0,4,...}    return the kind & format info for the "..." OID.
469  * {0,5,...}    return the description the "..." OID.
470  */
471
472 #ifdef SYSCTL_DEBUG
473 static void
474 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
475 {
476         int k;
477         struct sysctl_oid *oidp;
478
479         SLIST_FOREACH(oidp, l, oid_link) {
480
481                 for (k=0; k<i; k++)
482                         printf(" ");
483
484                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
485
486                 printf("%c%c",
487                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
488                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
489
490                 if (oidp->oid_handler)
491                         printf(" *Handler");
492
493                 switch (oidp->oid_kind & CTLTYPE) {
494                         case CTLTYPE_NODE:
495                                 printf(" Node\n");
496                                 if (!oidp->oid_handler) {
497                                         sysctl_sysctl_debug_dump_node(
498                                                 oidp->oid_arg1, i+2);
499                                 }
500                                 break;
501                         case CTLTYPE_INT:    printf(" Int\n"); break;
502                         case CTLTYPE_STRING: printf(" String\n"); break;
503                         case CTLTYPE_QUAD:   printf(" Quad\n"); break;
504                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
505                         default:             printf("\n");
506                 }
507
508         }
509 }
510
511 static int
512 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
513 {
514         int error;
515
516         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
517         if (error)
518                 return (error);
519         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
520         return (ENOENT);
521 }
522
523 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
524         0, 0, sysctl_sysctl_debug, "-", "");
525 #endif
526
527 static int
528 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
529 {
530         int *name = (int *) arg1;
531         u_int namelen = arg2;
532         int error = 0;
533         struct sysctl_oid *oid;
534         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
535         char buf[10];
536
537         while (namelen) {
538                 if (!lsp) {
539                         snprintf(buf,sizeof(buf),"%d",*name);
540                         if (req->oldidx)
541                                 error = SYSCTL_OUT(req, ".", 1);
542                         if (!error)
543                                 error = SYSCTL_OUT(req, buf, strlen(buf));
544                         if (error)
545                                 return (error);
546                         namelen--;
547                         name++;
548                         continue;
549                 }
550                 lsp2 = 0;
551                 SLIST_FOREACH(oid, lsp, oid_link) {
552                         if (oid->oid_number != *name)
553                                 continue;
554
555                         if (req->oldidx)
556                                 error = SYSCTL_OUT(req, ".", 1);
557                         if (!error)
558                                 error = SYSCTL_OUT(req, oid->oid_name,
559                                         strlen(oid->oid_name));
560                         if (error)
561                                 return (error);
562
563                         namelen--;
564                         name++;
565
566                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
567                                 break;
568
569                         if (oid->oid_handler)
570                                 break;
571
572                         lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
573                         break;
574                 }
575                 lsp = lsp2;
576         }
577         return (SYSCTL_OUT(req, "", 1));
578 }
579
580 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
581
582 static int
583 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
584         int *next, int *len, int level, struct sysctl_oid **oidpp)
585 {
586         struct sysctl_oid *oidp;
587
588         *len = level;
589         SLIST_FOREACH(oidp, lsp, oid_link) {
590                 *next = oidp->oid_number;
591                 *oidpp = oidp;
592
593                 if (oidp->oid_kind & CTLFLAG_SKIP)
594                         continue;
595
596                 if (!namelen) {
597                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
598                                 return (0);
599                         if (oidp->oid_handler) 
600                                 /* We really should call the handler here...*/
601                                 return (0);
602                         lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
603                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
604                                 len, level+1, oidpp))
605                                 return (0);
606                         goto emptynode;
607                 }
608
609                 if (oidp->oid_number < *name)
610                         continue;
611
612                 if (oidp->oid_number > *name) {
613                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
614                                 return (0);
615                         if (oidp->oid_handler)
616                                 return (0);
617                         lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
618                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
619                                 next+1, len, level+1, oidpp))
620                                 return (0);
621                         goto next;
622                 }
623                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
624                         continue;
625
626                 if (oidp->oid_handler)
627                         continue;
628
629                 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
630                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
631                         len, level+1, oidpp))
632                         return (0);
633         next:
634                 namelen = 1;
635         emptynode:
636                 *len = level;
637         }
638         return (1);
639 }
640
641 static int
642 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
643 {
644         int *name = (int *) arg1;
645         u_int namelen = arg2;
646         int i, j, error;
647         struct sysctl_oid *oid;
648         struct sysctl_oid_list *lsp = &sysctl__children;
649         int newoid[CTL_MAXNAME];
650
651         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
652         if (i)
653                 return (ENOENT);
654         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
655         return (error);
656 }
657
658 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
659
660 static int
661 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
662 {
663         int i;
664         struct sysctl_oid *oidp;
665         struct sysctl_oid_list *lsp = &sysctl__children;
666         char *p;
667
668         if (!*name)
669                 return (ENOENT);
670
671         p = name + strlen(name) - 1 ;
672         if (*p == '.')
673                 *p = '\0';
674
675         *len = 0;
676
677         for (p = name; *p && *p != '.'; p++) 
678                 ;
679         i = *p;
680         if (i == '.')
681                 *p = '\0';
682
683         oidp = SLIST_FIRST(lsp);
684
685         while (oidp && *len < CTL_MAXNAME) {
686                 if (strcmp(name, oidp->oid_name)) {
687                         oidp = SLIST_NEXT(oidp, oid_link);
688                         continue;
689                 }
690                 *oid++ = oidp->oid_number;
691                 (*len)++;
692
693                 if (!i) {
694                         if (oidpp)
695                                 *oidpp = oidp;
696                         return (0);
697                 }
698
699                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
700                         break;
701
702                 if (oidp->oid_handler)
703                         break;
704
705                 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
706                 oidp = SLIST_FIRST(lsp);
707                 name = p+1;
708                 for (p = name; *p && *p != '.'; p++) 
709                                 ;
710                 i = *p;
711                 if (i == '.')
712                         *p = '\0';
713         }
714         return (ENOENT);
715 }
716
717 static int
718 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
719 {
720         char *p;
721         int error, oid[CTL_MAXNAME], len;
722         struct sysctl_oid *op = 0;
723
724         if (!req->newlen) 
725                 return (ENOENT);
726         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
727                 return (ENAMETOOLONG);
728
729         p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
730
731         error = SYSCTL_IN(req, p, req->newlen);
732         if (error) {
733                 free(p, M_SYSCTL);
734                 return (error);
735         }
736
737         p [req->newlen] = '\0';
738
739         error = name2oid(p, oid, &len, &op);
740
741         free(p, M_SYSCTL);
742
743         if (error)
744                 return (error);
745
746         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
747         return (error);
748 }
749
750 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 
751         sysctl_sysctl_name2oid, "I", "");
752
753 static int
754 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
755 {
756         struct sysctl_oid *oid;
757         int error;
758
759         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
760         if (error)
761                 return (error);
762
763         if (!oid->oid_fmt)
764                 return (ENOENT);
765         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
766         if (error)
767                 return (error);
768         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
769         return (error);
770 }
771
772
773 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
774
775 static int
776 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
777 {
778         struct sysctl_oid *oid;
779         int error;
780
781         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
782         if (error)
783                 return (error);
784
785         if (!oid->oid_descr)
786                 return (ENOENT);
787         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
788         return (error);
789 }
790
791 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
792
793 /*
794  * Default "handler" functions.
795  */
796
797 /*
798  * Handle an int, signed or unsigned.
799  * Two cases:
800  *     a variable:  point arg1 at it.
801  *     a constant:  pass it in arg2.
802  */
803
804 int
805 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
806 {
807         int tmpout, error = 0;
808
809         /*
810          * Attempt to get a coherent snapshot by making a copy of the data.
811          */
812         if (arg1)
813                 tmpout = *(int *)arg1;
814         else
815                 tmpout = arg2;
816         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
817
818         if (error || !req->newptr)
819                 return (error);
820
821         if (!arg1)
822                 error = EPERM;
823         else
824                 error = SYSCTL_IN(req, arg1, sizeof(int));
825         return (error);
826 }
827
828
829 /*
830  * Based on on sysctl_handle_int() convert milliseconds into ticks.
831  */
832
833 int
834 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
835 {
836         int error, s, tt;
837
838         tt = *(int *)oidp->oid_arg1;
839         s = (int)((int64_t)tt * 1000 / hz);
840
841         error = sysctl_handle_int(oidp, &s, 0, req);
842         if (error || !req->newptr)
843                 return (error);
844
845         tt = (int)((int64_t)s * hz / 1000);
846         if (tt < 1)
847                 return (EINVAL);
848
849         *(int *)oidp->oid_arg1 = tt;
850         return (0);
851 }
852
853
854 /*
855  * Handle a long, signed or unsigned.  arg1 points to it.
856  */
857
858 int
859 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
860 {
861         int error = 0;
862         long tmplong;
863 #ifdef SCTL_MASK32
864         int tmpint;
865 #endif
866
867         /*
868          * Attempt to get a coherent snapshot by making a copy of the data.
869          */
870         if (!arg1)
871                 return (EINVAL);
872         tmplong = *(long *)arg1;
873 #ifdef SCTL_MASK32
874         if (req->flags & SCTL_MASK32) {
875                 tmpint = tmplong;
876                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
877         } else
878 #endif
879                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
880
881         if (error || !req->newptr)
882                 return (error);
883
884 #ifdef SCTL_MASK32
885         if (req->flags & SCTL_MASK32) {
886                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
887                 *(long *)arg1 = (long)tmpint;
888         } else
889 #endif
890                 error = SYSCTL_IN(req, arg1, sizeof(long));
891         return (error);
892 }
893
894 /*
895  * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
896  */
897
898 int
899 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
900 {
901         int error = 0;
902         uint64_t tmpout;
903
904         /*
905          * Attempt to get a coherent snapshot by making a copy of the data.
906          */
907         if (!arg1)
908                 return (EINVAL);
909         tmpout = *(uint64_t *)arg1;
910         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
911
912         if (error || !req->newptr)
913                 return (error);
914
915         error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
916         return (error);
917 }
918
919 /*
920  * Handle our generic '\0' terminated 'C' string.
921  * Two cases:
922  *      a variable string:  point arg1 at it, arg2 is max length.
923  *      a constant string:  point arg1 at it, arg2 is zero.
924  */
925
926 int
927 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
928 {
929         int error=0;
930         char *tmparg;
931         size_t outlen;
932
933         /*
934          * Attempt to get a coherent snapshot by copying to a
935          * temporary kernel buffer.
936          */
937 retry:
938         outlen = strlen((char *)arg1)+1;
939         tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
940
941         if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
942                 free(tmparg, M_SYSCTLTMP);
943                 goto retry;
944         }
945
946         error = SYSCTL_OUT(req, tmparg, outlen);
947         free(tmparg, M_SYSCTLTMP);
948
949         if (error || !req->newptr)
950                 return (error);
951
952         if ((req->newlen - req->newidx) >= arg2) {
953                 error = EINVAL;
954         } else {
955                 arg2 = (req->newlen - req->newidx);
956                 error = SYSCTL_IN(req, arg1, arg2);
957                 ((char *)arg1)[arg2] = '\0';
958         }
959
960         return (error);
961 }
962
963 /*
964  * Handle any kind of opaque data.
965  * arg1 points to it, arg2 is the size.
966  */
967
968 int
969 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
970 {
971         int error, tries;
972         u_int generation;
973         struct sysctl_req req2;
974
975         /*
976          * Attempt to get a coherent snapshot, by using the thread
977          * pre-emption counter updated from within mi_switch() to
978          * determine if we were pre-empted during a bcopy() or
979          * copyout(). Make 3 attempts at doing this before giving up.
980          * If we encounter an error, stop immediately.
981          */
982         tries = 0;
983         req2 = *req;
984 retry:
985         generation = curthread->td_generation;
986         error = SYSCTL_OUT(req, arg1, arg2);
987         if (error)
988                 return (error);
989         tries++;
990         if (generation != curthread->td_generation && tries < 3) {
991                 *req = req2;
992                 goto retry;
993         }
994
995         error = SYSCTL_IN(req, arg1, arg2);
996
997         return (error);
998 }
999
1000 /*
1001  * Transfer functions to/from kernel space.
1002  * XXX: rather untested at this point
1003  */
1004 static int
1005 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1006 {
1007         size_t i = 0;
1008
1009         if (req->oldptr) {
1010                 i = l;
1011                 if (req->oldlen <= req->oldidx)
1012                         i = 0;
1013                 else
1014                         if (i > req->oldlen - req->oldidx)
1015                                 i = req->oldlen - req->oldidx;
1016                 if (i > 0)
1017                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
1018         }
1019         req->oldidx += l;
1020         if (req->oldptr && i != l)
1021                 return (ENOMEM);
1022         return (0);
1023 }
1024
1025 static int
1026 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1027 {
1028         if (!req->newptr)
1029                 return (0);
1030         if (req->newlen - req->newidx < l)
1031                 return (EINVAL);
1032         bcopy((char *)req->newptr + req->newidx, p, l);
1033         req->newidx += l;
1034         return (0);
1035 }
1036
1037 int
1038 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1039     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1040 {
1041         int error = 0;
1042         struct sysctl_req req;
1043
1044         bzero(&req, sizeof req);
1045
1046         req.td = td;
1047         req.flags = flags;
1048
1049         if (oldlenp) {
1050                 req.oldlen = *oldlenp;
1051         }
1052         req.validlen = req.oldlen;
1053
1054         if (old) {
1055                 req.oldptr= old;
1056         }
1057
1058         if (new != NULL) {
1059                 req.newlen = newlen;
1060                 req.newptr = new;
1061         }
1062
1063         req.oldfunc = sysctl_old_kernel;
1064         req.newfunc = sysctl_new_kernel;
1065         req.lock = REQ_LOCKED;
1066
1067         SYSCTL_LOCK();
1068
1069         error = sysctl_root(0, name, namelen, &req);
1070
1071         if (req.lock == REQ_WIRED && req.validlen > 0)
1072                 vsunlock(req.oldptr, req.validlen);
1073
1074         SYSCTL_UNLOCK();
1075
1076         if (error && error != ENOMEM)
1077                 return (error);
1078
1079         if (retval) {
1080                 if (req.oldptr && req.oldidx > req.validlen)
1081                         *retval = req.validlen;
1082                 else
1083                         *retval = req.oldidx;
1084         }
1085         return (error);
1086 }
1087
1088 int
1089 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1090     void *new, size_t newlen, size_t *retval, int flags)
1091 {
1092         int oid[CTL_MAXNAME];
1093         size_t oidlen, plen;
1094         int error;
1095
1096         oid[0] = 0;             /* sysctl internal magic */
1097         oid[1] = 3;             /* name2oid */
1098         oidlen = sizeof(oid);
1099
1100         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1101             (void *)name, strlen(name), &plen, flags);
1102         if (error)
1103                 return (error);
1104
1105         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1106             new, newlen, retval, flags);
1107         return (error);
1108 }
1109
1110 /*
1111  * Transfer function to/from user space.
1112  */
1113 static int
1114 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1115 {
1116         int error = 0;
1117         size_t i, len, origidx;
1118
1119         origidx = req->oldidx;
1120         req->oldidx += l;
1121         if (req->oldptr == NULL)
1122                 return (0);
1123         /*
1124          * If we have not wired the user supplied buffer and we are currently
1125          * holding locks, drop a witness warning, as it's possible that
1126          * write operations to the user page can sleep.
1127          */
1128         if (req->lock != REQ_WIRED)
1129                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1130                     "sysctl_old_user()");
1131         i = l;
1132         len = req->validlen;
1133         if (len <= origidx)
1134                 i = 0;
1135         else {
1136                 if (i > len - origidx)
1137                         i = len - origidx;
1138                 error = copyout(p, (char *)req->oldptr + origidx, i);
1139         }
1140         if (error)
1141                 return (error);
1142         if (i < l)
1143                 return (ENOMEM);
1144         return (0);
1145 }
1146
1147 static int
1148 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1149 {
1150         int error;
1151
1152         if (!req->newptr)
1153                 return (0);
1154         if (req->newlen - req->newidx < l)
1155                 return (EINVAL);
1156         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1157             "sysctl_new_user()");
1158         error = copyin((char *)req->newptr + req->newidx, p, l);
1159         req->newidx += l;
1160         return (error);
1161 }
1162
1163 /*
1164  * Wire the user space destination buffer.  If set to a value greater than
1165  * zero, the len parameter limits the maximum amount of wired memory.
1166  */
1167 int
1168 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1169 {
1170         int ret;
1171         size_t i, wiredlen;
1172         char *cp, dummy;
1173
1174         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1175         ret = 0;
1176         if (req->lock == REQ_LOCKED && req->oldptr &&
1177             req->oldfunc == sysctl_old_user) {
1178                 if (wiredlen != 0) {
1179                         ret = vslock(req->oldptr, wiredlen);
1180                         if (ret != 0) {
1181                                 if (ret != ENOMEM)
1182                                         return (ret);
1183                                 wiredlen = 0;
1184                         }
1185                         /*
1186                          * Touch all the wired pages to avoid PTE modified
1187                          * bit emulation traps on Alpha while holding locks
1188                          * in the sysctl handler.
1189                          */
1190                         for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE,
1191                             cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) {
1192                                 copyin(cp, &dummy, 1);
1193                                 copyout(&dummy, cp, 1);
1194                         }
1195                 }
1196                 req->lock = REQ_WIRED;
1197                 req->validlen = wiredlen;
1198         }
1199         return (0);
1200 }
1201
1202 int
1203 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1204     int *nindx, struct sysctl_req *req)
1205 {
1206         struct sysctl_oid *oid;
1207         int indx;
1208
1209         oid = SLIST_FIRST(&sysctl__children);
1210         indx = 0;
1211         while (oid && indx < CTL_MAXNAME) {
1212                 if (oid->oid_number == name[indx]) {
1213                         indx++;
1214                         if (oid->oid_kind & CTLFLAG_NOLOCK)
1215                                 req->lock = REQ_UNLOCKED;
1216                         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1217                                 if (oid->oid_handler != NULL ||
1218                                     indx == namelen) {
1219                                         *noid = oid;
1220                                         if (nindx != NULL)
1221                                                 *nindx = indx;
1222                                         return (0);
1223                                 }
1224                                 oid = SLIST_FIRST(
1225                                     (struct sysctl_oid_list *)oid->oid_arg1);
1226                         } else if (indx == namelen) {
1227                                 *noid = oid;
1228                                 if (nindx != NULL)
1229                                         *nindx = indx;
1230                                 return (0);
1231                         } else {
1232                                 return (ENOTDIR);
1233                         }
1234                 } else {
1235                         oid = SLIST_NEXT(oid, oid_link);
1236                 }
1237         }
1238         return (ENOENT);
1239 }
1240
1241 /*
1242  * Traverse our tree, and find the right node, execute whatever it points
1243  * to, and return the resulting error code.
1244  */
1245
1246 static int
1247 sysctl_root(SYSCTL_HANDLER_ARGS)
1248 {
1249         struct sysctl_oid *oid;
1250         int error, indx, lvl;
1251
1252         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1253         if (error)
1254                 return (error);
1255
1256         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1257                 /*
1258                  * You can't call a sysctl when it's a node, but has
1259                  * no handler.  Inform the user that it's a node.
1260                  * The indx may or may not be the same as namelen.
1261                  */
1262                 if (oid->oid_handler == NULL)
1263                         return (EISDIR);
1264         }
1265
1266         /* Is this sysctl writable? */
1267         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1268                 return (EPERM);
1269
1270         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1271
1272         /* Is this sysctl sensitive to securelevels? */
1273         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1274                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1275                 error = securelevel_gt(req->td->td_ucred, lvl);
1276                 if (error)
1277                         return (error);
1278         }
1279
1280         /* Is this sysctl writable by only privileged users? */
1281         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1282                 if (oid->oid_kind & CTLFLAG_PRISON)
1283                         error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1284                 else
1285                         error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1286                 if (error)
1287                         return (error);
1288         }
1289
1290         if (!oid->oid_handler)
1291                 return (EINVAL);
1292
1293         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1294                 arg1 = (int *)arg1 + indx;
1295                 arg2 -= indx;
1296         } else {
1297                 arg1 = oid->oid_arg1;
1298                 arg2 = oid->oid_arg2;
1299         }
1300 #ifdef MAC
1301         error = mac_check_system_sysctl(req->td->td_ucred, oid, arg1, arg2,
1302             req);
1303         if (error != 0)
1304                 return (error);
1305 #endif
1306         error = oid->oid_handler(oid, arg1, arg2, req);
1307
1308         return (error);
1309 }
1310
1311 #ifndef _SYS_SYSPROTO_H_
1312 struct sysctl_args {
1313         int     *name;
1314         u_int   namelen;
1315         void    *old;
1316         size_t  *oldlenp;
1317         void    *new;
1318         size_t  newlen;
1319 };
1320 #endif
1321 int
1322 __sysctl(struct thread *td, struct sysctl_args *uap)
1323 {
1324         int error, name[CTL_MAXNAME];
1325         size_t j;
1326
1327         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1328                 return (EINVAL);
1329
1330         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1331         if (error)
1332                 return (error);
1333
1334         mtx_lock(&Giant);
1335
1336         error = userland_sysctl(td, name, uap->namelen,
1337                 uap->old, uap->oldlenp, 0,
1338                 uap->new, uap->newlen, &j, 0);
1339         if (error && error != ENOMEM)
1340                 goto done2;
1341         if (uap->oldlenp) {
1342                 int i = copyout(&j, uap->oldlenp, sizeof(j));
1343                 if (i)
1344                         error = i;
1345         }
1346 done2:
1347         mtx_unlock(&Giant);
1348         return (error);
1349 }
1350
1351 /*
1352  * This is used from various compatibility syscalls too.  That's why name
1353  * must be in kernel space.
1354  */
1355 int
1356 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1357     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1358     int flags)
1359 {
1360         int error = 0;
1361         struct sysctl_req req;
1362
1363         bzero(&req, sizeof req);
1364
1365         req.td = td;
1366         req.flags = flags;
1367
1368         if (oldlenp) {
1369                 if (inkernel) {
1370                         req.oldlen = *oldlenp;
1371                 } else {
1372                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1373                         if (error)
1374                                 return (error);
1375                 }
1376         }
1377         req.validlen = req.oldlen;
1378
1379         if (old) {
1380                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1381                         return (EFAULT);
1382                 req.oldptr= old;
1383         }
1384
1385         if (new != NULL) {
1386                 if (!useracc(new, req.newlen, VM_PROT_READ))
1387                         return (EFAULT);
1388                 req.newlen = newlen;
1389                 req.newptr = new;
1390         }
1391
1392         req.oldfunc = sysctl_old_user;
1393         req.newfunc = sysctl_new_user;
1394         req.lock = REQ_LOCKED;
1395
1396         SYSCTL_LOCK();
1397
1398         do {
1399                 req.oldidx = 0;
1400                 req.newidx = 0;
1401                 error = sysctl_root(0, name, namelen, &req);
1402         } while (error == EAGAIN);
1403
1404         if (req.lock == REQ_WIRED && req.validlen > 0)
1405                 vsunlock(req.oldptr, req.validlen);
1406
1407         SYSCTL_UNLOCK();
1408
1409         if (error && error != ENOMEM)
1410                 return (error);
1411
1412         if (retval) {
1413                 if (req.oldptr && req.oldidx > req.validlen)
1414                         *retval = req.validlen;
1415                 else
1416                         *retval = req.oldidx;
1417         }
1418         return (error);
1419 }
1420
1421 #ifdef COMPAT_43
1422 #include <sys/socket.h>
1423 #include <vm/vm_param.h>
1424
1425 #define KINFO_PROC              (0<<8)
1426 #define KINFO_RT                (1<<8)
1427 #define KINFO_VNODE             (2<<8)
1428 #define KINFO_FILE              (3<<8)
1429 #define KINFO_METER             (4<<8)
1430 #define KINFO_LOADAVG           (5<<8)
1431 #define KINFO_CLOCKRATE         (6<<8)
1432
1433 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1434 #define KINFO_BSDI_SYSINFO      (101<<8)
1435
1436 /*
1437  * XXX this is bloat, but I hope it's better here than on the potentially
1438  * limited kernel stack...  -Peter
1439  */
1440
1441 static struct {
1442         int     bsdi_machine;           /* "i386" on BSD/386 */
1443 /*      ^^^ this is an offset to the string, relative to the struct start */
1444         char    *pad0;
1445         long    pad1;
1446         long    pad2;
1447         long    pad3;
1448         u_long  pad4;
1449         u_long  pad5;
1450         u_long  pad6;
1451
1452         int     bsdi_ostype;            /* "BSD/386" on BSD/386 */
1453         int     bsdi_osrelease;         /* "1.1" on BSD/386 */
1454         long    pad7;
1455         long    pad8;
1456         char    *pad9;
1457
1458         long    pad10;
1459         long    pad11;
1460         int     pad12;
1461         long    pad13;
1462         quad_t  pad14;
1463         long    pad15;
1464
1465         struct  timeval pad16;
1466         /* we dont set this, because BSDI's uname used gethostname() instead */
1467         int     bsdi_hostname;          /* hostname on BSD/386 */
1468
1469         /* the actual string data is appended here */
1470
1471 } bsdi_si;
1472
1473 /*
1474  * this data is appended to the end of the bsdi_si structure during copyout.
1475  * The "char *" offsets are relative to the base of the bsdi_si struct.
1476  * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1477  * should not exceed the length of the buffer here... (or else!! :-)
1478  */
1479 static char bsdi_strings[80];   /* It had better be less than this! */
1480
1481 #ifndef _SYS_SYSPROTO_H_
1482 struct getkerninfo_args {
1483         int     op;
1484         char    *where;
1485         size_t  *size;
1486         int     arg;
1487 };
1488 #endif
1489 int
1490 ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1491 {
1492         int error, name[6];
1493         size_t size;
1494         u_int needed = 0;
1495
1496         mtx_lock(&Giant);
1497
1498         switch (uap->op & 0xff00) {
1499
1500         case KINFO_RT:
1501                 name[0] = CTL_NET;
1502                 name[1] = PF_ROUTE;
1503                 name[2] = 0;
1504                 name[3] = (uap->op & 0xff0000) >> 16;
1505                 name[4] = uap->op & 0xff;
1506                 name[5] = uap->arg;
1507                 error = userland_sysctl(td, name, 6, uap->where, uap->size,
1508                         0, 0, 0, &size, 0);
1509                 break;
1510
1511         case KINFO_VNODE:
1512                 name[0] = CTL_KERN;
1513                 name[1] = KERN_VNODE;
1514                 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1515                         0, 0, 0, &size, 0);
1516                 break;
1517
1518         case KINFO_PROC:
1519                 name[0] = CTL_KERN;
1520                 name[1] = KERN_PROC;
1521                 name[2] = uap->op & 0xff;
1522                 name[3] = uap->arg;
1523                 error = userland_sysctl(td, name, 4, uap->where, uap->size,
1524                         0, 0, 0, &size, 0);
1525                 break;
1526
1527         case KINFO_FILE:
1528                 name[0] = CTL_KERN;
1529                 name[1] = KERN_FILE;
1530                 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1531                         0, 0, 0, &size, 0);
1532                 break;
1533
1534         case KINFO_METER:
1535                 name[0] = CTL_VM;
1536                 name[1] = VM_TOTAL;
1537                 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1538                         0, 0, 0, &size, 0);
1539                 break;
1540
1541         case KINFO_LOADAVG:
1542                 name[0] = CTL_VM;
1543                 name[1] = VM_LOADAVG;
1544                 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1545                         0, 0, 0, &size, 0);
1546                 break;
1547
1548         case KINFO_CLOCKRATE:
1549                 name[0] = CTL_KERN;
1550                 name[1] = KERN_CLOCKRATE;
1551                 error = userland_sysctl(td, name, 2, uap->where, uap->size,
1552                         0, 0, 0, &size, 0);
1553                 break;
1554
1555         case KINFO_BSDI_SYSINFO: {
1556                 /*
1557                  * this is pretty crude, but it's just enough for uname()
1558                  * from BSDI's 1.x libc to work.
1559                  *
1560                  * *size gives the size of the buffer before the call, and
1561                  * the amount of data copied after a successful call.
1562                  * If successful, the return value is the amount of data
1563                  * available, which can be larger than *size.
1564                  *
1565                  * BSDI's 2.x product apparently fails with ENOMEM if *size
1566                  * is too small.
1567                  */
1568
1569                 u_int left;
1570                 char *s;
1571
1572                 bzero((char *)&bsdi_si, sizeof(bsdi_si));
1573                 bzero(bsdi_strings, sizeof(bsdi_strings));
1574
1575                 s = bsdi_strings;
1576
1577                 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1578                 strcpy(s, ostype);
1579                 s += strlen(s) + 1;
1580
1581                 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1582                 strcpy(s, osrelease);
1583                 s += strlen(s) + 1;
1584
1585                 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1586                 strcpy(s, machine);
1587                 s += strlen(s) + 1;
1588
1589                 needed = sizeof(bsdi_si) + (s - bsdi_strings);
1590
1591                 if ((uap->where == NULL) || (uap->size == NULL)) {
1592                         /* process is asking how much buffer to supply.. */
1593                         size = needed;
1594                         error = 0;
1595                         break;
1596                 }
1597
1598                 if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1599                         break;
1600
1601                 /* if too much buffer supplied, trim it down */
1602                 if (size > needed)
1603                         size = needed;
1604
1605                 /* how much of the buffer is remaining */
1606                 left = size;
1607
1608                 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1609                         break;
1610
1611                 /* is there any point in continuing? */
1612                 if (left > sizeof(bsdi_si)) {
1613                         left -= sizeof(bsdi_si);
1614                         error = copyout(&bsdi_strings,
1615                                         uap->where + sizeof(bsdi_si), left);
1616                 }
1617                 break;
1618         }
1619
1620         default:
1621                 error = EOPNOTSUPP;
1622                 break;
1623         }
1624         if (error == 0) {
1625                 td->td_retval[0] = needed ? needed : size;
1626                 if (uap->size) {
1627                         error = copyout(&size, uap->size, sizeof(size));
1628                 }
1629         }
1630         mtx_unlock(&Giant);
1631         return (error);
1632 }
1633 #endif /* COMPAT_43 */