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