Move ogetkerninfo() to the compat tree without any changes.
[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.13 2003/11/14 02:54:52 daver Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/buf.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/sysproto.h>
52 #include <vm/vm.h>
53 #include <vm/vm_extern.h>
54
55 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
56 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
57
58 /*
59  * Locking and stats
60  */
61 static struct sysctl_lock {
62         int     sl_lock;
63         int     sl_want;
64         int     sl_locked;
65 } memlock;
66
67 static int sysctl_root(SYSCTL_HANDLER_ARGS);
68
69 struct sysctl_oid_list sysctl__children; /* root list */
70
71 static struct sysctl_oid *
72 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
73 {
74         struct sysctl_oid *oidp;
75
76         SLIST_FOREACH(oidp, list, oid_link) {
77                 if (strcmp(oidp->oid_name, name) == 0) {
78                         return (oidp);
79                 }
80         }
81         return (NULL);
82 }
83
84 /*
85  * Initialization of the MIB tree.
86  *
87  * Order by number in each list.
88  */
89
90 void sysctl_register_oid(struct sysctl_oid *oidp)
91 {
92         struct sysctl_oid_list *parent = oidp->oid_parent;
93         struct sysctl_oid *p;
94         struct sysctl_oid *q;
95
96         /*
97          * First check if another oid with the same name already
98          * exists in the parent's list.
99          */
100         p = sysctl_find_oidname(oidp->oid_name, parent);
101         if (p != NULL) {
102                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
103                         p->oid_refcnt++;
104                         return;
105                 } else {
106                         printf("can't re-use a leaf (%s)!\n", p->oid_name);
107                         return;
108                 }
109         }
110         /*
111          * If this oid has a number OID_AUTO, give it a number which
112          * is greater than any current oid.  Make sure it is at least
113          * 100 to leave space for pre-assigned oid numbers.
114          */
115         if (oidp->oid_number == OID_AUTO) {
116                 static int newoid = 100;
117                 oidp->oid_number = newoid++;
118                 if (newoid == 0x7fffffff)
119                         panic("out of oids");
120         }
121
122         /*
123          * Insert the oid into the parent's list in order.
124          */
125         q = NULL;
126         SLIST_FOREACH(p, parent, oid_link) {
127                 if (oidp->oid_number < p->oid_number)
128                         break;
129                 q = p;
130         }
131         if (q)
132                 SLIST_INSERT_AFTER(q, oidp, oid_link);
133         else
134                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
135 }
136
137 void sysctl_unregister_oid(struct sysctl_oid *oidp)
138 {
139         struct sysctl_oid *p;
140         int error;
141
142         error = ENOENT;
143         if (oidp->oid_number == OID_AUTO) {
144                 error = EINVAL;
145         } else {
146                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
147                         if (p == oidp) {
148                                 SLIST_REMOVE(oidp->oid_parent, oidp,
149                                                 sysctl_oid, oid_link);
150                                 error = 0;
151                                 break;
152                         }
153                 }
154         }
155
156         /*
157          * This can happen when a module fails to register and is
158          * being unloaded afterwards.  It should not be a panic()
159          * for normal use.
160          */
161         if (error)
162                 printf("%s: failed to unregister sysctl\n", __func__);
163 }
164
165 /* Initialize a new context to keep track of dynamically added sysctls. */
166 int
167 sysctl_ctx_init(struct sysctl_ctx_list *c)
168 {
169
170         if (c == NULL) {
171                 return (EINVAL);
172         }
173         TAILQ_INIT(c);
174         return (0);
175 }
176
177 /* Free the context, and destroy all dynamic oids registered in this context */
178 int
179 sysctl_ctx_free(struct sysctl_ctx_list *clist)
180 {
181         struct sysctl_ctx_entry *e, *e1;
182         int error;
183
184         error = 0;
185         /*
186          * First perform a "dry run" to check if it's ok to remove oids.
187          * XXX FIXME
188          * XXX This algorithm is a hack. But I don't know any
189          * XXX better solution for now...
190          */
191         TAILQ_FOREACH(e, clist, link) {
192                 error = sysctl_remove_oid(e->entry, 0, 0);
193                 if (error)
194                         break;
195         }
196         /*
197          * Restore deregistered entries, either from the end,
198          * or from the place where error occured.
199          * e contains the entry that was not unregistered
200          */
201         if (error)
202                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
203         else
204                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
205         while (e1 != NULL) {
206                 sysctl_register_oid(e1->entry);
207                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
208         }
209         if (error)
210                 return(EBUSY);
211         /* Now really delete the entries */
212         e = TAILQ_FIRST(clist);
213         while (e != NULL) {
214                 e1 = TAILQ_NEXT(e, link);
215                 error = sysctl_remove_oid(e->entry, 1, 0);
216                 if (error)
217                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
218                             e->entry->oid_name);
219                 free(e, M_SYSCTLOID);
220                 e = e1;
221         }
222         return (error);
223 }
224
225 /* Add an entry to the context */
226 struct sysctl_ctx_entry *
227 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
228 {
229         struct sysctl_ctx_entry *e;
230
231         if (clist == NULL || oidp == NULL)
232                 return(NULL);
233         e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
234         e->entry = oidp;
235         TAILQ_INSERT_HEAD(clist, e, link);
236         return (e);
237 }
238
239 /* Find an entry in the context */
240 struct sysctl_ctx_entry *
241 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
242 {
243         struct sysctl_ctx_entry *e;
244
245         if (clist == NULL || oidp == NULL)
246                 return(NULL);
247         for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
248                 if(e->entry == oidp)
249                         return(e);
250         }
251         return (e);
252 }
253
254 /*
255  * Delete an entry from the context.
256  * NOTE: this function doesn't free oidp! You have to remove it
257  * with sysctl_remove_oid().
258  */
259 int
260 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
261 {
262         struct sysctl_ctx_entry *e;
263
264         if (clist == NULL || oidp == NULL)
265                 return (EINVAL);
266         e = sysctl_ctx_entry_find(clist, oidp);
267         if (e != NULL) {
268                 TAILQ_REMOVE(clist, e, link);
269                 free(e, M_SYSCTLOID);
270                 return (0);
271         } else
272                 return (ENOENT);
273 }
274
275 /*
276  * Remove dynamically created sysctl trees.
277  * oidp - top of the tree to be removed
278  * del - if 0 - just deregister, otherwise free up entries as well
279  * recurse - if != 0 traverse the subtree to be deleted
280  */
281 int
282 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
283 {
284         struct sysctl_oid *p;
285         int error;
286
287         if (oidp == NULL)
288                 return(EINVAL);
289         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
290                 printf("can't remove non-dynamic nodes!\n");
291                 return (EINVAL);
292         }
293         /*
294          * WARNING: normal method to do this should be through
295          * sysctl_ctx_free(). Use recursing as the last resort
296          * method to purge your sysctl tree of leftovers...
297          * However, if some other code still references these nodes,
298          * it will panic.
299          */
300         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
301                 if (oidp->oid_refcnt == 1) {
302                         SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
303                                 if (!recurse)
304                                         return (ENOTEMPTY);
305                                 error = sysctl_remove_oid(p, del, recurse);
306                                 if (error)
307                                         return (error);
308                         }
309                         if (del)
310                                 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
311                 }
312         }
313         if (oidp->oid_refcnt > 1 ) {
314                 oidp->oid_refcnt--;
315         } else {
316                 if (oidp->oid_refcnt == 0) {
317                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
318                                 oidp->oid_refcnt, oidp->oid_name);
319                         return (EINVAL);
320                 }
321                 sysctl_unregister_oid(oidp);
322                 if (del) {
323                         if (oidp->oid_descr)
324                                 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
325                         free((void *)(uintptr_t)(const void *)oidp->oid_name,
326                              M_SYSCTLOID);
327                         free(oidp, M_SYSCTLOID);
328                 }
329         }
330         return (0);
331 }
332
333 /*
334  * Create new sysctls at run time.
335  * clist may point to a valid context initialized with sysctl_ctx_init().
336  */
337 struct sysctl_oid *
338 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
339         int number, const char *name, int kind, void *arg1, int arg2,
340         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
341 {
342         struct sysctl_oid *oidp;
343         ssize_t len;
344         char *newname;
345
346         /* You have to hook up somewhere.. */
347         if (parent == NULL)
348                 return(NULL);
349         /* Check if the node already exists, otherwise create it */
350         oidp = sysctl_find_oidname(name, parent);
351         if (oidp != NULL) {
352                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
353                         oidp->oid_refcnt++;
354                         /* Update the context */
355                         if (clist != NULL)
356                                 sysctl_ctx_entry_add(clist, oidp);
357                         return (oidp);
358                 } else {
359                         printf("can't re-use a leaf (%s)!\n", name);
360                         return (NULL);
361                 }
362         }
363         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK);
364         bzero(oidp, sizeof(struct sysctl_oid));
365         oidp->oid_parent = parent;
366         SLIST_NEXT(oidp, oid_link) = NULL;
367         oidp->oid_number = number;
368         oidp->oid_refcnt = 1;
369         len = strlen(name);
370         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
371         bcopy(name, newname, len + 1);
372         newname[len] = '\0';
373         oidp->oid_name = newname;
374         oidp->oid_handler = handler;
375         oidp->oid_kind = CTLFLAG_DYN | kind;
376         if ((kind & CTLTYPE) == CTLTYPE_NODE) {
377                 /* Allocate space for children */
378                 SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
379                     M_SYSCTLOID, M_WAITOK);
380                 SLIST_INIT(SYSCTL_CHILDREN(oidp));
381         } else {
382                 oidp->oid_arg1 = arg1;
383                 oidp->oid_arg2 = arg2;
384         }
385         oidp->oid_fmt = fmt;
386         if (descr) {
387                 int len = strlen(descr) + 1;
388                 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
389                 if (oidp->oid_descr)
390                         strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
391         };
392         /* Update the context, if used */
393         if (clist != NULL)
394                 sysctl_ctx_entry_add(clist, oidp);
395         /* Register this oid */
396         sysctl_register_oid(oidp);
397         return (oidp);
398 }
399
400 /*
401  * Register the kernel's oids on startup.
402  */
403 SET_DECLARE(sysctl_set, struct sysctl_oid);
404
405 static void sysctl_register_all(void *arg)
406 {
407         struct sysctl_oid **oidp;
408
409         SET_FOREACH(oidp, sysctl_set)
410                 sysctl_register_oid(*oidp);
411 }
412
413 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
414
415 /*
416  * "Staff-functions"
417  *
418  * These functions implement a presently undocumented interface 
419  * used by the sysctl program to walk the tree, and get the type
420  * so it can print the value.
421  * This interface is under work and consideration, and should probably
422  * be killed with a big axe by the first person who can find the time.
423  * (be aware though, that the proper interface isn't as obvious as it
424  * may seem, there are various conflicting requirements.
425  *
426  * {0,0}        printf the entire MIB-tree.
427  * {0,1,...}    return the name of the "..." OID.
428  * {0,2,...}    return the next OID.
429  * {0,3}        return the OID of the name in "new"
430  * {0,4,...}    return the kind & format info for the "..." OID.
431  */
432
433 static void
434 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
435 {
436         int k;
437         struct sysctl_oid *oidp;
438
439         SLIST_FOREACH(oidp, l, oid_link) {
440
441                 for (k=0; k<i; k++)
442                         printf(" ");
443
444                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
445
446                 printf("%c%c",
447                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
448                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
449
450                 if (oidp->oid_handler)
451                         printf(" *Handler");
452
453                 switch (oidp->oid_kind & CTLTYPE) {
454                         case CTLTYPE_NODE:
455                                 printf(" Node\n");
456                                 if (!oidp->oid_handler) {
457                                         sysctl_sysctl_debug_dump_node(
458                                                 oidp->oid_arg1, i+2);
459                                 }
460                                 break;
461                         case CTLTYPE_INT:    printf(" Int\n"); break;
462                         case CTLTYPE_STRING: printf(" String\n"); break;
463                         case CTLTYPE_QUAD:   printf(" Quad\n"); break;
464                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
465                         default:             printf("\n");
466                 }
467
468         }
469 }
470
471 static int
472 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
473 {
474         int error;
475
476         error = suser(req->td);
477         if (error)
478                 return error;
479         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
480         return ENOENT;
481 }
482
483 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
484         0, 0, sysctl_sysctl_debug, "-", "");
485
486 static int
487 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
488 {
489         int *name = (int *) arg1;
490         u_int namelen = arg2;
491         int error = 0;
492         struct sysctl_oid *oid;
493         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
494         char buf[10];
495
496         while (namelen) {
497                 if (!lsp) {
498                         snprintf(buf,sizeof(buf),"%d",*name);
499                         if (req->oldidx)
500                                 error = SYSCTL_OUT(req, ".", 1);
501                         if (!error)
502                                 error = SYSCTL_OUT(req, buf, strlen(buf));
503                         if (error)
504                                 return (error);
505                         namelen--;
506                         name++;
507                         continue;
508                 }
509                 lsp2 = 0;
510                 SLIST_FOREACH(oid, lsp, oid_link) {
511                         if (oid->oid_number != *name)
512                                 continue;
513
514                         if (req->oldidx)
515                                 error = SYSCTL_OUT(req, ".", 1);
516                         if (!error)
517                                 error = SYSCTL_OUT(req, oid->oid_name,
518                                         strlen(oid->oid_name));
519                         if (error)
520                                 return (error);
521
522                         namelen--;
523                         name++;
524
525                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
526                                 break;
527
528                         if (oid->oid_handler)
529                                 break;
530
531                         lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
532                         break;
533                 }
534                 lsp = lsp2;
535         }
536         return (SYSCTL_OUT(req, "", 1));
537 }
538
539 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
540
541 static int
542 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
543         int *next, int *len, int level, struct sysctl_oid **oidpp)
544 {
545         struct sysctl_oid *oidp;
546
547         *len = level;
548         SLIST_FOREACH(oidp, lsp, oid_link) {
549                 *next = oidp->oid_number;
550                 *oidpp = oidp;
551
552                 if (!namelen) {
553                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
554                                 return 0;
555                         if (oidp->oid_handler) 
556                                 /* We really should call the handler here...*/
557                                 return 0;
558                         lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
559                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
560                                 len, level+1, oidpp))
561                                 return 0;
562                         goto emptynode;
563                 }
564
565                 if (oidp->oid_number < *name)
566                         continue;
567
568                 if (oidp->oid_number > *name) {
569                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
570                                 return 0;
571                         if (oidp->oid_handler)
572                                 return 0;
573                         lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
574                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
575                                 next+1, len, level+1, oidpp))
576                                 return (0);
577                         goto next;
578                 }
579                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
580                         continue;
581
582                 if (oidp->oid_handler)
583                         continue;
584
585                 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
586                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
587                         len, level+1, oidpp))
588                         return (0);
589         next:
590                 namelen = 1;
591                 *len = level;
592         emptynode:
593                 *len = level;
594         }
595         return 1;
596 }
597
598 static int
599 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
600 {
601         int *name = (int *) arg1;
602         u_int namelen = arg2;
603         int i, j, error;
604         struct sysctl_oid *oid;
605         struct sysctl_oid_list *lsp = &sysctl__children;
606         int newoid[CTL_MAXNAME];
607
608         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
609         if (i)
610                 return ENOENT;
611         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
612         return (error);
613 }
614
615 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
616
617 static int
618 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
619 {
620         int i;
621         struct sysctl_oid *oidp;
622         struct sysctl_oid_list *lsp = &sysctl__children;
623         char *p;
624
625         if (!*name)
626                 return ENOENT;
627
628         p = name + strlen(name) - 1 ;
629         if (*p == '.')
630                 *p = '\0';
631
632         *len = 0;
633
634         for (p = name; *p && *p != '.'; p++) 
635                 ;
636         i = *p;
637         if (i == '.')
638                 *p = '\0';
639
640         oidp = SLIST_FIRST(lsp);
641
642         while (oidp && *len < CTL_MAXNAME) {
643                 if (strcmp(name, oidp->oid_name)) {
644                         oidp = SLIST_NEXT(oidp, oid_link);
645                         continue;
646                 }
647                 *oid++ = oidp->oid_number;
648                 (*len)++;
649
650                 if (!i) {
651                         if (oidpp)
652                                 *oidpp = oidp;
653                         return (0);
654                 }
655
656                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
657                         break;
658
659                 if (oidp->oid_handler)
660                         break;
661
662                 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
663                 oidp = SLIST_FIRST(lsp);
664                 name = p+1;
665                 for (p = name; *p && *p != '.'; p++) 
666                                 ;
667                 i = *p;
668                 if (i == '.')
669                         *p = '\0';
670         }
671         return ENOENT;
672 }
673
674 static int
675 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
676 {
677         char *p;
678         int error, oid[CTL_MAXNAME], len;
679         struct sysctl_oid *op = 0;
680
681         if (!req->newlen) 
682                 return ENOENT;
683         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
684                 return (ENAMETOOLONG);
685
686         p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
687
688         error = SYSCTL_IN(req, p, req->newlen);
689         if (error) {
690                 free(p, M_SYSCTL);
691                 return (error);
692         }
693
694         p [req->newlen] = '\0';
695
696         error = name2oid(p, oid, &len, &op);
697
698         free(p, M_SYSCTL);
699
700         if (error)
701                 return (error);
702
703         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
704         return (error);
705 }
706
707 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 
708         sysctl_sysctl_name2oid, "I", "");
709
710 static int
711 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
712 {
713         struct sysctl_oid *oid;
714         int error;
715
716         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
717         if (error)
718                 return (error);
719
720         if (!oid->oid_fmt)
721                 return (ENOENT);
722         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
723         if (error)
724                 return (error);
725         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
726         return (error);
727 }
728
729
730 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
731
732 static int
733 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
734 {
735         struct sysctl_oid *oid;
736         int error;
737
738         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
739         if (error)
740                 return (error);
741         
742         if (!oid->oid_descr)
743                 return (ENOENT);
744         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
745         return (error);
746 }
747
748 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
749
750 /*
751  * Default "handler" functions.
752  */
753
754 /*
755  * Handle an int, signed or unsigned.
756  * Two cases:
757  *     a variable:  point arg1 at it.
758  *     a constant:  pass it in arg2.
759  */
760
761 int
762 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
763 {
764         int error = 0;
765
766         if (arg1)
767                 error = SYSCTL_OUT(req, arg1, sizeof(int));
768         else
769                 error = SYSCTL_OUT(req, &arg2, sizeof(int));
770
771         if (error || !req->newptr)
772                 return (error);
773
774         if (!arg1)
775                 error = EPERM;
776         else
777                 error = SYSCTL_IN(req, arg1, sizeof(int));
778         return (error);
779 }
780
781 /*
782  * Handle a long, signed or unsigned.  arg1 points to it.
783  */
784
785 int
786 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
787 {
788         int error = 0;
789
790         if (!arg1)
791                 return (EINVAL);
792         error = SYSCTL_OUT(req, arg1, sizeof(long));
793
794         if (error || !req->newptr)
795                 return (error);
796
797         error = SYSCTL_IN(req, arg1, sizeof(long));
798         return (error);
799 }
800
801 /*
802  * Handle a quad, signed or unsigned.  arg1 points to it.
803  */
804
805 int
806 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
807 {
808         int error = 0;
809
810         if (!arg1)
811                 return (EINVAL);
812         error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
813
814         if (error || !req->newptr)
815                 return (error);
816
817         error = SYSCTL_IN(req, arg1, sizeof(quad_t));
818         return (error);
819 }
820
821 /*
822  * Handle our generic '\0' terminated 'C' string.
823  * Two cases:
824  *      a variable string:  point arg1 at it, arg2 is max length.
825  *      a constant string:  point arg1 at it, arg2 is zero.
826  */
827
828 int
829 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
830 {
831         int error=0;
832
833         error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
834
835         if (error || !req->newptr)
836                 return (error);
837
838         if ((req->newlen - req->newidx) >= arg2) {
839                 error = EINVAL;
840         } else {
841                 arg2 = (req->newlen - req->newidx);
842                 error = SYSCTL_IN(req, arg1, arg2);
843                 ((char *)arg1)[arg2] = '\0';
844         }
845
846         return (error);
847 }
848
849 /*
850  * Handle any kind of opaque data.
851  * arg1 points to it, arg2 is the size.
852  */
853
854 int
855 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
856 {
857         int error;
858
859         error = SYSCTL_OUT(req, arg1, arg2);
860
861         if (error || !req->newptr)
862                 return (error);
863
864         error = SYSCTL_IN(req, arg1, arg2);
865
866         return (error);
867 }
868
869 /*
870  * Transfer functions to/from kernel space.
871  * XXX: rather untested at this point
872  */
873 static int
874 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
875 {
876         size_t i = 0;
877
878         if (req->oldptr) {
879                 i = l;
880                 if (i > req->oldlen - req->oldidx)
881                         i = req->oldlen - req->oldidx;
882                 if (i > 0)
883                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
884         }
885         req->oldidx += l;
886         if (req->oldptr && i != l)
887                 return (ENOMEM);
888         return (0);
889 }
890
891 static int
892 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
893 {
894
895         if (!req->newptr)
896                 return 0;
897         if (req->newlen - req->newidx < l)
898                 return (EINVAL);
899         bcopy((char *)req->newptr + req->newidx, p, l);
900         req->newidx += l;
901         return (0);
902 }
903
904 int
905 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
906 {
907         int error = 0;
908         struct sysctl_req req;
909
910         bzero(&req, sizeof req);
911
912         req.td = curthread;
913
914         if (oldlenp) {
915                 req.oldlen = *oldlenp;
916         }
917
918         if (old) {
919                 req.oldptr = old;
920         }
921
922         if (new != NULL) {
923                 req.newlen = newlen;
924                 req.newptr = new;
925         }
926
927         req.oldfunc = sysctl_old_kernel;
928         req.newfunc = sysctl_new_kernel;
929         req.lock = 1;
930
931         /* XXX this should probably be done in a general way */
932         while (memlock.sl_lock) {
933                 memlock.sl_want = 1;
934                 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
935                 memlock.sl_locked++;
936         }
937         memlock.sl_lock = 1;
938
939         error = sysctl_root(0, name, namelen, &req);
940
941         if (req.lock == 2)
942                 vsunlock(req.oldptr, req.oldlen);
943
944         memlock.sl_lock = 0;
945
946         if (memlock.sl_want) {
947                 memlock.sl_want = 0;
948                 wakeup((caddr_t)&memlock);
949         }
950
951         if (error && error != ENOMEM)
952                 return (error);
953
954         if (retval) {
955                 if (req.oldptr && req.oldidx > req.oldlen)
956                         *retval = req.oldlen;
957                 else
958                         *retval = req.oldidx;
959         }
960         return (error);
961 }
962
963 int
964 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
965     void *new, size_t newlen, size_t *retval)
966 {
967         int oid[CTL_MAXNAME];
968         size_t oidlen, plen;
969         int error;
970
971         oid[0] = 0;             /* sysctl internal magic */
972         oid[1] = 3;             /* name2oid */
973         oidlen = sizeof(oid);
974
975         error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
976                     strlen(name), &plen);
977         if (error)
978                 return (error);
979
980         error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
981             new, newlen, retval);
982         return (error);
983 }
984
985 /*
986  * Transfer function to/from user space.
987  */
988 static int
989 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
990 {
991         int error = 0;
992         size_t i = 0;
993
994         if (req->lock == 1 && req->oldptr) {
995                 vslock(req->oldptr, req->oldlen);
996                 req->lock = 2;
997         }
998         if (req->oldptr) {
999                 i = l;
1000                 if (i > req->oldlen - req->oldidx)
1001                         i = req->oldlen - req->oldidx;
1002                 if (i > 0)
1003                         error = copyout(p, (char *)req->oldptr + req->oldidx,
1004                                         i);
1005         }
1006         req->oldidx += l;
1007         if (error)
1008                 return (error);
1009         if (req->oldptr && i < l)
1010                 return (ENOMEM);
1011         return (0);
1012 }
1013
1014 static int
1015 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1016 {
1017         int error;
1018
1019         if (!req->newptr)
1020                 return 0;
1021         if (req->newlen - req->newidx < l)
1022                 return (EINVAL);
1023         error = copyin((char *)req->newptr + req->newidx, p, l);
1024         req->newidx += l;
1025         return (error);
1026 }
1027
1028 int
1029 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1030     int *nindx, struct sysctl_req *req)
1031 {
1032         struct sysctl_oid *oid;
1033         int indx;
1034
1035         oid = SLIST_FIRST(&sysctl__children);
1036         indx = 0;
1037         while (oid && indx < CTL_MAXNAME) {
1038                 if (oid->oid_number == name[indx]) {
1039                         indx++;
1040                         if (oid->oid_kind & CTLFLAG_NOLOCK)
1041                                 req->lock = 0;
1042                         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1043                                 if (oid->oid_handler != NULL ||
1044                                     indx == namelen) {
1045                                         *noid = oid;
1046                                         if (nindx != NULL)
1047                                                 *nindx = indx;
1048                                         return (0);
1049                                 }
1050                                 oid = SLIST_FIRST(
1051                                     (struct sysctl_oid_list *)oid->oid_arg1);
1052                         } else if (indx == namelen) {
1053                                 *noid = oid;
1054                                 if (nindx != NULL)
1055                                         *nindx = indx;
1056                                 return (0);
1057                         } else {
1058                                 return (ENOTDIR);
1059                         }
1060                 } else {
1061                         oid = SLIST_NEXT(oid, oid_link);
1062                 }
1063         }
1064         return (ENOENT);
1065 }
1066
1067 /*
1068  * Traverse our tree, and find the right node, execute whatever it points
1069  * to, and return the resulting error code.
1070  */
1071
1072 int
1073 sysctl_root(SYSCTL_HANDLER_ARGS)
1074 {
1075         struct thread *td = req->td;
1076         struct proc *p = td ? td->td_proc : NULL;
1077         struct sysctl_oid *oid;
1078         int error, indx;
1079
1080         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1081         if (error)
1082                 return (error);
1083
1084         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1085                 /*
1086                  * You can't call a sysctl when it's a node, but has
1087                  * no handler.  Inform the user that it's a node.
1088                  * The indx may or may not be the same as namelen.
1089                  */
1090                 if (oid->oid_handler == NULL)
1091                         return (EISDIR);
1092         }
1093
1094         /* If writing isn't allowed */
1095         if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1096             ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1097                 return (EPERM);
1098
1099         /* Most likely only root can write */
1100         if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1101             (error = suser_cred(p->p_ucred, 
1102              (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1103                 return (error);
1104
1105         if (!oid->oid_handler)
1106                 return EINVAL;
1107
1108         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1109                 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1110                     req);
1111         else
1112                 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1113                     req);
1114         return (error);
1115 }
1116
1117 int
1118 __sysctl(struct sysctl_args *uap)
1119 {
1120         int error, i, name[CTL_MAXNAME];
1121         size_t j;
1122
1123         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1124                 return (EINVAL);
1125
1126         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1127         if (error)
1128                 return (error);
1129
1130         error = userland_sysctl(name, uap->namelen,
1131                 uap->old, uap->oldlenp, 0,
1132                 uap->new, uap->newlen, &j);
1133         if (error && error != ENOMEM)
1134                 return (error);
1135         if (uap->oldlenp) {
1136                 i = copyout(&j, uap->oldlenp, sizeof(j));
1137                 if (i)
1138                         return (i);
1139         }
1140         return (error);
1141 }
1142
1143 /*
1144  * This is used from various compatibility syscalls too.  That's why name
1145  * must be in kernel space.
1146  */
1147 int
1148 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1149 {
1150         int error = 0;
1151         struct sysctl_req req, req2;
1152
1153         bzero(&req, sizeof req);
1154
1155         if (oldlenp) {
1156                 if (inkernel) {
1157                         req.oldlen = *oldlenp;
1158                 } else {
1159                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1160                         if (error)
1161                                 return (error);
1162                 }
1163         }
1164
1165         if (old) {
1166                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1167                         return (EFAULT);
1168                 req.oldptr= old;
1169         }
1170
1171         if (new != NULL) {
1172                 if (!useracc(new, req.newlen, VM_PROT_READ))
1173                         return (EFAULT);
1174                 req.newlen = newlen;
1175                 req.newptr = new;
1176         }
1177
1178         req.oldfunc = sysctl_old_user;
1179         req.newfunc = sysctl_new_user;
1180         req.lock = 1;
1181         req.td = curthread;
1182
1183         /* XXX this should probably be done in a general way */
1184         while (memlock.sl_lock) {
1185                 memlock.sl_want = 1;
1186                 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
1187                 memlock.sl_locked++;
1188         }
1189         memlock.sl_lock = 1;
1190
1191         do {
1192             req2 = req;
1193             error = sysctl_root(0, name, namelen, &req2);
1194         } while (error == EAGAIN);
1195
1196         req = req2;
1197         if (req.lock == 2)
1198                 vsunlock(req.oldptr, req.oldlen);
1199
1200         memlock.sl_lock = 0;
1201
1202         if (memlock.sl_want) {
1203                 memlock.sl_want = 0;
1204                 wakeup((caddr_t)&memlock);
1205         }
1206
1207         if (error && error != ENOMEM)
1208                 return (error);
1209
1210         if (retval) {
1211                 if (req.oldptr && req.oldidx > req.oldlen)
1212                         *retval = req.oldlen;
1213                 else
1214                         *retval = req.oldidx;
1215         }
1216         return (error);
1217 }