]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/kern/kern_sysctl.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 #include <sys/uio.h>
56
57 #include <security/mac/mac_framework.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_extern.h>
61
62 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
63 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
64 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
65
66 /*
67  * The sysctllock protects the MIB tree.  It also protects sysctl
68  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
69  * sysctl_unregister_oid() routines require the sysctllock to already
70  * be held, so the sysctl_lock() and sysctl_unlock() routines are
71  * provided for the few places in the kernel which need to use that
72  * API rather than using the dynamic API.  Use of the dynamic API is
73  * strongly encouraged for most code.
74  *
75  * This lock is also used to serialize userland sysctl requests.  Some
76  * sysctls wire user memory, and serializing the requests limits the
77  * amount of wired user memory in use.
78  */
79 static struct sx sysctllock;
80
81 #define SYSCTL_SLOCK()          sx_slock(&sysctllock)
82 #define SYSCTL_SUNLOCK()        sx_sunlock(&sysctllock)
83 #define SYSCTL_XLOCK()          sx_xlock(&sysctllock)
84 #define SYSCTL_XUNLOCK()        sx_xunlock(&sysctllock)
85 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
86 #define SYSCTL_ASSERT_LOCKED()  sx_assert(&sysctllock, SA_LOCKED)
87 #define SYSCTL_INIT()           sx_init(&sysctllock, "sysctl lock")
88
89 static int sysctl_root(SYSCTL_HANDLER_ARGS);
90
91 struct sysctl_oid_list sysctl__children; /* root list */
92
93 static int      sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
94                     int recurse);
95
96 static struct sysctl_oid *
97 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
98 {
99         struct sysctl_oid *oidp;
100
101         SYSCTL_ASSERT_LOCKED();
102         SLIST_FOREACH(oidp, list, oid_link) {
103                 if (strcmp(oidp->oid_name, name) == 0) {
104                         return (oidp);
105                 }
106         }
107         return (NULL);
108 }
109
110 /*
111  * Initialization of the MIB tree.
112  *
113  * Order by number in each list.
114  */
115 void
116 sysctl_lock(void)
117 {
118
119         SYSCTL_XLOCK();
120 }
121
122 void
123 sysctl_unlock(void)
124 {
125
126         SYSCTL_XUNLOCK();
127 }
128
129 void
130 sysctl_register_oid(struct sysctl_oid *oidp)
131 {
132         struct sysctl_oid_list *parent = oidp->oid_parent;
133         struct sysctl_oid *p;
134         struct sysctl_oid *q;
135
136         /*
137          * First check if another oid with the same name already
138          * exists in the parent's list.
139          */
140         SYSCTL_ASSERT_XLOCKED();
141         p = sysctl_find_oidname(oidp->oid_name, parent);
142         if (p != NULL) {
143                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
144                         p->oid_refcnt++;
145                         return;
146                 } else {
147                         printf("can't re-use a leaf (%s)!\n", p->oid_name);
148                         return;
149                 }
150         }
151         /*
152          * If this oid has a number OID_AUTO, give it a number which
153          * is greater than any current oid.
154          * NOTE: DO NOT change the starting value here, change it in
155          * <sys/sysctl.h>, and make sure it is at least 256 to
156          * accomodate e.g. net.inet.raw as a static sysctl node.
157          */
158         if (oidp->oid_number == OID_AUTO) {
159                 static int newoid = CTL_AUTO_START;
160
161                 oidp->oid_number = newoid++;
162                 if (newoid == 0x7fffffff)
163                         panic("out of oids");
164         }
165 #if 0
166         else if (oidp->oid_number >= CTL_AUTO_START) {
167                 /* do not panic; this happens when unregistering sysctl sets */
168                 printf("static sysctl oid too high: %d", oidp->oid_number);
169         }
170 #endif
171
172         /*
173          * Insert the oid into the parent's list in order.
174          */
175         q = NULL;
176         SLIST_FOREACH(p, parent, oid_link) {
177                 if (oidp->oid_number < p->oid_number)
178                         break;
179                 q = p;
180         }
181         if (q)
182                 SLIST_INSERT_AFTER(q, oidp, oid_link);
183         else
184                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
185 }
186
187 void
188 sysctl_unregister_oid(struct sysctl_oid *oidp)
189 {
190         struct sysctl_oid *p;
191         int error;
192
193         SYSCTL_ASSERT_XLOCKED();
194         error = ENOENT;
195         if (oidp->oid_number == OID_AUTO) {
196                 error = EINVAL;
197         } else {
198                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
199                         if (p == oidp) {
200                                 SLIST_REMOVE(oidp->oid_parent, oidp,
201                                     sysctl_oid, oid_link);
202                                 error = 0;
203                                 break;
204                         }
205                 }
206         }
207
208         /* 
209          * This can happen when a module fails to register and is
210          * being unloaded afterwards.  It should not be a panic()
211          * for normal use.
212          */
213         if (error)
214                 printf("%s: failed to unregister sysctl\n", __func__);
215 }
216
217 /* Initialize a new context to keep track of dynamically added sysctls. */
218 int
219 sysctl_ctx_init(struct sysctl_ctx_list *c)
220 {
221
222         if (c == NULL) {
223                 return (EINVAL);
224         }
225
226         /*
227          * No locking here, the caller is responsible for not adding
228          * new nodes to a context until after this function has
229          * returned.
230          */
231         TAILQ_INIT(c);
232         return (0);
233 }
234
235 /* Free the context, and destroy all dynamic oids registered in this context */
236 int
237 sysctl_ctx_free(struct sysctl_ctx_list *clist)
238 {
239         struct sysctl_ctx_entry *e, *e1;
240         int error;
241
242         error = 0;
243         /*
244          * First perform a "dry run" to check if it's ok to remove oids.
245          * XXX FIXME
246          * XXX This algorithm is a hack. But I don't know any
247          * XXX better solution for now...
248          */
249         SYSCTL_XLOCK();
250         TAILQ_FOREACH(e, clist, link) {
251                 error = sysctl_remove_oid_locked(e->entry, 0, 0);
252                 if (error)
253                         break;
254         }
255         /*
256          * Restore deregistered entries, either from the end,
257          * or from the place where error occured.
258          * e contains the entry that was not unregistered
259          */
260         if (error)
261                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
262         else
263                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
264         while (e1 != NULL) {
265                 sysctl_register_oid(e1->entry);
266                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
267         }
268         if (error) {
269                 SYSCTL_XUNLOCK();
270                 return(EBUSY);
271         }
272         /* Now really delete the entries */
273         e = TAILQ_FIRST(clist);
274         while (e != NULL) {
275                 e1 = TAILQ_NEXT(e, link);
276                 error = sysctl_remove_oid_locked(e->entry, 1, 0);
277                 if (error)
278                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
279                             e->entry->oid_name);
280                 free(e, M_SYSCTLOID);
281                 e = e1;
282         }
283         SYSCTL_XUNLOCK();
284         return (error);
285 }
286
287 /* Add an entry to the context */
288 struct sysctl_ctx_entry *
289 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
290 {
291         struct sysctl_ctx_entry *e;
292
293         SYSCTL_ASSERT_XLOCKED();
294         if (clist == NULL || oidp == NULL)
295                 return(NULL);
296         e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
297         e->entry = oidp;
298         TAILQ_INSERT_HEAD(clist, e, link);
299         return (e);
300 }
301
302 /* Find an entry in the context */
303 struct sysctl_ctx_entry *
304 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
305 {
306         struct sysctl_ctx_entry *e;
307
308         SYSCTL_ASSERT_LOCKED();
309         if (clist == NULL || oidp == NULL)
310                 return(NULL);
311         TAILQ_FOREACH(e, clist, link) {
312                 if(e->entry == oidp)
313                         return(e);
314         }
315         return (e);
316 }
317
318 /*
319  * Delete an entry from the context.
320  * NOTE: this function doesn't free oidp! You have to remove it
321  * with sysctl_remove_oid().
322  */
323 int
324 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
325 {
326         struct sysctl_ctx_entry *e;
327
328         if (clist == NULL || oidp == NULL)
329                 return (EINVAL);
330         SYSCTL_XLOCK();
331         e = sysctl_ctx_entry_find(clist, oidp);
332         if (e != NULL) {
333                 TAILQ_REMOVE(clist, e, link);
334                 SYSCTL_XUNLOCK();
335                 free(e, M_SYSCTLOID);
336                 return (0);
337         } else {
338                 SYSCTL_XUNLOCK();
339                 return (ENOENT);
340         }
341 }
342
343 /*
344  * Remove dynamically created sysctl trees.
345  * oidp - top of the tree to be removed
346  * del - if 0 - just deregister, otherwise free up entries as well
347  * recurse - if != 0 traverse the subtree to be deleted
348  */
349 int
350 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
351 {
352         int error;
353
354         SYSCTL_XLOCK();
355         error = sysctl_remove_oid_locked(oidp, del, recurse);
356         SYSCTL_XUNLOCK();
357         return (error);
358 }
359
360 static int
361 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
362 {
363         struct sysctl_oid *p;
364         int error;
365
366         SYSCTL_ASSERT_XLOCKED();
367         if (oidp == NULL)
368                 return(EINVAL);
369         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
370                 printf("can't remove non-dynamic nodes!\n");
371                 return (EINVAL);
372         }
373         /*
374          * WARNING: normal method to do this should be through
375          * sysctl_ctx_free(). Use recursing as the last resort
376          * method to purge your sysctl tree of leftovers...
377          * However, if some other code still references these nodes,
378          * it will panic.
379          */
380         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
381                 if (oidp->oid_refcnt == 1) {
382                         SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
383                                 if (!recurse)
384                                         return (ENOTEMPTY);
385                                 error = sysctl_remove_oid_locked(p, del,
386                                     recurse);
387                                 if (error)
388                                         return (error);
389                         }
390                         if (del)
391                                 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
392                 }
393         }
394         if (oidp->oid_refcnt > 1 ) {
395                 oidp->oid_refcnt--;
396         } else {
397                 if (oidp->oid_refcnt == 0) {
398                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
399                                 oidp->oid_refcnt, oidp->oid_name);
400                         return (EINVAL);
401                 }
402                 sysctl_unregister_oid(oidp);
403                 if (del) {
404                         if (oidp->oid_descr)
405                                 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
406                         free((void *)(uintptr_t)(const void *)oidp->oid_name,
407                              M_SYSCTLOID);
408                         free(oidp, M_SYSCTLOID);
409                 }
410         }
411         return (0);
412 }
413
414 /*
415  * Create new sysctls at run time.
416  * clist may point to a valid context initialized with sysctl_ctx_init().
417  */
418 struct sysctl_oid *
419 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
420         int number, const char *name, int kind, void *arg1, int arg2,
421         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
422 {
423         struct sysctl_oid *oidp;
424         ssize_t len;
425         char *newname;
426
427         /* You have to hook up somewhere.. */
428         if (parent == NULL)
429                 return(NULL);
430         /* Check if the node already exists, otherwise create it */
431         SYSCTL_XLOCK();
432         oidp = sysctl_find_oidname(name, parent);
433         if (oidp != NULL) {
434                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
435                         oidp->oid_refcnt++;
436                         /* Update the context */
437                         if (clist != NULL)
438                                 sysctl_ctx_entry_add(clist, oidp);
439                         SYSCTL_XUNLOCK();
440                         return (oidp);
441                 } else {
442                         SYSCTL_XUNLOCK();
443                         printf("can't re-use a leaf (%s)!\n", name);
444                         return (NULL);
445                 }
446         }
447         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
448         oidp->oid_parent = parent;
449         SLIST_NEXT(oidp, oid_link) = NULL;
450         oidp->oid_number = number;
451         oidp->oid_refcnt = 1;
452         len = strlen(name);
453         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
454         bcopy(name, newname, len + 1);
455         newname[len] = '\0';
456         oidp->oid_name = newname;
457         oidp->oid_handler = handler;
458         oidp->oid_kind = CTLFLAG_DYN | kind;
459         if ((kind & CTLTYPE) == CTLTYPE_NODE) {
460                 /* Allocate space for children */
461                 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
462                     M_SYSCTLOID, M_WAITOK));
463                 SLIST_INIT(SYSCTL_CHILDREN(oidp));
464         } else {
465                 oidp->oid_arg1 = arg1;
466                 oidp->oid_arg2 = arg2;
467         }
468         oidp->oid_fmt = fmt;
469         if (descr) {
470                 int len = strlen(descr) + 1;
471                 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
472                 if (oidp->oid_descr)
473                         strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
474         }
475         /* Update the context, if used */
476         if (clist != NULL)
477                 sysctl_ctx_entry_add(clist, oidp);
478         /* Register this oid */
479         sysctl_register_oid(oidp);
480         SYSCTL_XUNLOCK();
481         return (oidp);
482 }
483
484 /*
485  * Rename an existing oid.
486  */
487 void
488 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
489 {
490         ssize_t len;
491         char *newname;
492         void *oldname;
493
494         len = strlen(name);
495         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
496         bcopy(name, newname, len + 1);
497         newname[len] = '\0';
498         SYSCTL_XLOCK();
499         oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
500         oidp->oid_name = newname;
501         SYSCTL_XUNLOCK();
502         free(oldname, M_SYSCTLOID);
503 }
504
505 /*
506  * Reparent an existing oid.
507  */
508 int
509 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
510 {
511         struct sysctl_oid *oidp;
512
513         SYSCTL_XLOCK();
514         if (oid->oid_parent == parent) {
515                 SYSCTL_XUNLOCK();
516                 return (0);
517         }
518         oidp = sysctl_find_oidname(oid->oid_name, parent);
519         if (oidp != NULL) {
520                 SYSCTL_XUNLOCK();
521                 return (EEXIST);
522         }
523         sysctl_unregister_oid(oid);
524         oid->oid_parent = parent;
525         oid->oid_number = OID_AUTO;
526         sysctl_register_oid(oid);
527         SYSCTL_XUNLOCK();
528         return (0);
529 }
530
531 /*
532  * Register the kernel's oids on startup.
533  */
534 SET_DECLARE(sysctl_set, struct sysctl_oid);
535
536 static void
537 sysctl_register_all(void *arg)
538 {
539         struct sysctl_oid **oidp;
540
541         SYSCTL_INIT();
542         SYSCTL_XLOCK();
543         SET_FOREACH(oidp, sysctl_set)
544                 sysctl_register_oid(*oidp);
545         SYSCTL_XUNLOCK();
546 }
547 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
548
549 /*
550  * "Staff-functions"
551  *
552  * These functions implement a presently undocumented interface 
553  * used by the sysctl program to walk the tree, and get the type
554  * so it can print the value.
555  * This interface is under work and consideration, and should probably
556  * be killed with a big axe by the first person who can find the time.
557  * (be aware though, that the proper interface isn't as obvious as it
558  * may seem, there are various conflicting requirements.
559  *
560  * {0,0}        printf the entire MIB-tree.
561  * {0,1,...}    return the name of the "..." OID.
562  * {0,2,...}    return the next OID.
563  * {0,3}        return the OID of the name in "new"
564  * {0,4,...}    return the kind & format info for the "..." OID.
565  * {0,5,...}    return the description the "..." OID.
566  */
567
568 #ifdef SYSCTL_DEBUG
569 static void
570 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
571 {
572         int k;
573         struct sysctl_oid *oidp;
574
575         SYSCTL_ASSERT_LOCKED();
576         SLIST_FOREACH(oidp, l, oid_link) {
577
578                 for (k=0; k<i; k++)
579                         printf(" ");
580
581                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
582
583                 printf("%c%c",
584                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
585                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
586
587                 if (oidp->oid_handler)
588                         printf(" *Handler");
589
590                 switch (oidp->oid_kind & CTLTYPE) {
591                         case CTLTYPE_NODE:
592                                 printf(" Node\n");
593                                 if (!oidp->oid_handler) {
594                                         sysctl_sysctl_debug_dump_node(
595                                                 oidp->oid_arg1, i+2);
596                                 }
597                                 break;
598                         case CTLTYPE_INT:    printf(" Int\n"); break;
599                         case CTLTYPE_STRING: printf(" String\n"); break;
600                         case CTLTYPE_QUAD:   printf(" Quad\n"); break;
601                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
602                         default:             printf("\n");
603                 }
604
605         }
606 }
607
608 static int
609 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
610 {
611         int error;
612
613         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
614         if (error)
615                 return (error);
616         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
617         return (ENOENT);
618 }
619
620 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
621         0, 0, sysctl_sysctl_debug, "-", "");
622 #endif
623
624 static int
625 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
626 {
627         int *name = (int *) arg1;
628         u_int namelen = arg2;
629         int error = 0;
630         struct sysctl_oid *oid;
631         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
632         char buf[10];
633
634         SYSCTL_ASSERT_LOCKED();
635         while (namelen) {
636                 if (!lsp) {
637                         snprintf(buf,sizeof(buf),"%d",*name);
638                         if (req->oldidx)
639                                 error = SYSCTL_OUT(req, ".", 1);
640                         if (!error)
641                                 error = SYSCTL_OUT(req, buf, strlen(buf));
642                         if (error)
643                                 return (error);
644                         namelen--;
645                         name++;
646                         continue;
647                 }
648                 lsp2 = 0;
649                 SLIST_FOREACH(oid, lsp, oid_link) {
650                         if (oid->oid_number != *name)
651                                 continue;
652
653                         if (req->oldidx)
654                                 error = SYSCTL_OUT(req, ".", 1);
655                         if (!error)
656                                 error = SYSCTL_OUT(req, oid->oid_name,
657                                         strlen(oid->oid_name));
658                         if (error)
659                                 return (error);
660
661                         namelen--;
662                         name++;
663
664                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
665                                 break;
666
667                         if (oid->oid_handler)
668                                 break;
669
670                         lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
671                         break;
672                 }
673                 lsp = lsp2;
674         }
675         return (SYSCTL_OUT(req, "", 1));
676 }
677
678 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
679
680 static int
681 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
682         int *next, int *len, int level, struct sysctl_oid **oidpp)
683 {
684         struct sysctl_oid *oidp;
685
686         SYSCTL_ASSERT_LOCKED();
687         *len = level;
688         SLIST_FOREACH(oidp, lsp, oid_link) {
689                 *next = oidp->oid_number;
690                 *oidpp = oidp;
691
692                 if (oidp->oid_kind & CTLFLAG_SKIP)
693                         continue;
694
695                 if (!namelen) {
696                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
697                                 return (0);
698                         if (oidp->oid_handler) 
699                                 /* We really should call the handler here...*/
700                                 return (0);
701                         lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
702                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
703                                 len, level+1, oidpp))
704                                 return (0);
705                         goto emptynode;
706                 }
707
708                 if (oidp->oid_number < *name)
709                         continue;
710
711                 if (oidp->oid_number > *name) {
712                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
713                                 return (0);
714                         if (oidp->oid_handler)
715                                 return (0);
716                         lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
717                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
718                                 next+1, len, level+1, oidpp))
719                                 return (0);
720                         goto next;
721                 }
722                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
723                         continue;
724
725                 if (oidp->oid_handler)
726                         continue;
727
728                 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
729                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
730                         len, level+1, oidpp))
731                         return (0);
732         next:
733                 namelen = 1;
734         emptynode:
735                 *len = level;
736         }
737         return (1);
738 }
739
740 static int
741 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
742 {
743         int *name = (int *) arg1;
744         u_int namelen = arg2;
745         int i, j, error;
746         struct sysctl_oid *oid;
747         struct sysctl_oid_list *lsp = &sysctl__children;
748         int newoid[CTL_MAXNAME];
749
750         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
751         if (i)
752                 return (ENOENT);
753         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
754         return (error);
755 }
756
757 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
758
759 static int
760 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
761 {
762         int i;
763         struct sysctl_oid *oidp;
764         struct sysctl_oid_list *lsp = &sysctl__children;
765         char *p;
766
767         SYSCTL_ASSERT_LOCKED();
768
769         if (!*name)
770                 return (ENOENT);
771
772         p = name + strlen(name) - 1 ;
773         if (*p == '.')
774                 *p = '\0';
775
776         *len = 0;
777
778         for (p = name; *p && *p != '.'; p++) 
779                 ;
780         i = *p;
781         if (i == '.')
782                 *p = '\0';
783
784         oidp = SLIST_FIRST(lsp);
785
786         while (oidp && *len < CTL_MAXNAME) {
787                 if (strcmp(name, oidp->oid_name)) {
788                         oidp = SLIST_NEXT(oidp, oid_link);
789                         continue;
790                 }
791                 *oid++ = oidp->oid_number;
792                 (*len)++;
793
794                 if (!i) {
795                         if (oidpp)
796                                 *oidpp = oidp;
797                         return (0);
798                 }
799
800                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
801                         break;
802
803                 if (oidp->oid_handler)
804                         break;
805
806                 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
807                 oidp = SLIST_FIRST(lsp);
808                 name = p+1;
809                 for (p = name; *p && *p != '.'; p++) 
810                                 ;
811                 i = *p;
812                 if (i == '.')
813                         *p = '\0';
814         }
815         return (ENOENT);
816 }
817
818 static int
819 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
820 {
821         char *p;
822         int error, oid[CTL_MAXNAME], len;
823         struct sysctl_oid *op = 0;
824
825         SYSCTL_ASSERT_LOCKED();
826
827         if (!req->newlen) 
828                 return (ENOENT);
829         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
830                 return (ENAMETOOLONG);
831
832         p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
833
834         error = SYSCTL_IN(req, p, req->newlen);
835         if (error) {
836                 free(p, M_SYSCTL);
837                 return (error);
838         }
839
840         p [req->newlen] = '\0';
841
842         error = name2oid(p, oid, &len, &op);
843
844         free(p, M_SYSCTL);
845
846         if (error)
847                 return (error);
848
849         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
850         return (error);
851 }
852
853 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
854     0, 0, sysctl_sysctl_name2oid, "I", "");
855
856 static int
857 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
858 {
859         struct sysctl_oid *oid;
860         int error;
861
862         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
863         if (error)
864                 return (error);
865
866         if (!oid->oid_fmt)
867                 return (ENOENT);
868         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
869         if (error)
870                 return (error);
871         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
872         return (error);
873 }
874
875
876 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
877     sysctl_sysctl_oidfmt, "");
878
879 static int
880 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
881 {
882         struct sysctl_oid *oid;
883         int error;
884
885         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
886         if (error)
887                 return (error);
888
889         if (!oid->oid_descr)
890                 return (ENOENT);
891         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
892         return (error);
893 }
894
895 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
896
897 /*
898  * Default "handler" functions.
899  */
900
901 /*
902  * Handle an int, signed or unsigned.
903  * Two cases:
904  *     a variable:  point arg1 at it.
905  *     a constant:  pass it in arg2.
906  */
907
908 int
909 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
910 {
911         int tmpout, error = 0;
912
913         /*
914          * Attempt to get a coherent snapshot by making a copy of the data.
915          */
916         if (arg1)
917                 tmpout = *(int *)arg1;
918         else
919                 tmpout = arg2;
920         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
921
922         if (error || !req->newptr)
923                 return (error);
924
925         if (!arg1)
926                 error = EPERM;
927         else
928                 error = SYSCTL_IN(req, arg1, sizeof(int));
929         return (error);
930 }
931
932
933 /*
934  * Based on on sysctl_handle_int() convert milliseconds into ticks.
935  */
936
937 int
938 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
939 {
940         int error, s, tt;
941
942         tt = *(int *)oidp->oid_arg1;
943         s = (int)((int64_t)tt * 1000 / hz);
944
945         error = sysctl_handle_int(oidp, &s, 0, req);
946         if (error || !req->newptr)
947                 return (error);
948
949         tt = (int)((int64_t)s * hz / 1000);
950         if (tt < 1)
951                 return (EINVAL);
952
953         *(int *)oidp->oid_arg1 = tt;
954         return (0);
955 }
956
957
958 /*
959  * Handle a long, signed or unsigned.  arg1 points to it.
960  */
961
962 int
963 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
964 {
965         int error = 0;
966         long tmplong;
967 #ifdef SCTL_MASK32
968         int tmpint;
969 #endif
970
971         /*
972          * Attempt to get a coherent snapshot by making a copy of the data.
973          */
974         if (!arg1)
975                 return (EINVAL);
976         tmplong = *(long *)arg1;
977 #ifdef SCTL_MASK32
978         if (req->flags & SCTL_MASK32) {
979                 tmpint = tmplong;
980                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
981         } else
982 #endif
983                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
984
985         if (error || !req->newptr)
986                 return (error);
987
988 #ifdef SCTL_MASK32
989         if (req->flags & SCTL_MASK32) {
990                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
991                 *(long *)arg1 = (long)tmpint;
992         } else
993 #endif
994                 error = SYSCTL_IN(req, arg1, sizeof(long));
995         return (error);
996 }
997
998 /*
999  * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1000  */
1001
1002 int
1003 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1004 {
1005         int error = 0;
1006         uint64_t tmpout;
1007
1008         /*
1009          * Attempt to get a coherent snapshot by making a copy of the data.
1010          */
1011         if (!arg1)
1012                 return (EINVAL);
1013         tmpout = *(uint64_t *)arg1;
1014         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1015
1016         if (error || !req->newptr)
1017                 return (error);
1018
1019         error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1020         return (error);
1021 }
1022
1023 /*
1024  * Handle our generic '\0' terminated 'C' string.
1025  * Two cases:
1026  *      a variable string:  point arg1 at it, arg2 is max length.
1027  *      a constant string:  point arg1 at it, arg2 is zero.
1028  */
1029
1030 int
1031 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1032 {
1033         int error=0;
1034         char *tmparg;
1035         size_t outlen;
1036
1037         /*
1038          * Attempt to get a coherent snapshot by copying to a
1039          * temporary kernel buffer.
1040          */
1041 retry:
1042         outlen = strlen((char *)arg1)+1;
1043         tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1044
1045         if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1046                 free(tmparg, M_SYSCTLTMP);
1047                 goto retry;
1048         }
1049
1050         error = SYSCTL_OUT(req, tmparg, outlen);
1051         free(tmparg, M_SYSCTLTMP);
1052
1053         if (error || !req->newptr)
1054                 return (error);
1055
1056         if ((req->newlen - req->newidx) >= arg2) {
1057                 error = EINVAL;
1058         } else {
1059                 arg2 = (req->newlen - req->newidx);
1060                 error = SYSCTL_IN(req, arg1, arg2);
1061                 ((char *)arg1)[arg2] = '\0';
1062         }
1063
1064         return (error);
1065 }
1066
1067 /*
1068  * Handle any kind of opaque data.
1069  * arg1 points to it, arg2 is the size.
1070  */
1071
1072 int
1073 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1074 {
1075         int error, tries;
1076         u_int generation;
1077         struct sysctl_req req2;
1078
1079         /*
1080          * Attempt to get a coherent snapshot, by using the thread
1081          * pre-emption counter updated from within mi_switch() to
1082          * determine if we were pre-empted during a bcopy() or
1083          * copyout(). Make 3 attempts at doing this before giving up.
1084          * If we encounter an error, stop immediately.
1085          */
1086         tries = 0;
1087         req2 = *req;
1088 retry:
1089         generation = curthread->td_generation;
1090         error = SYSCTL_OUT(req, arg1, arg2);
1091         if (error)
1092                 return (error);
1093         tries++;
1094         if (generation != curthread->td_generation && tries < 3) {
1095                 *req = req2;
1096                 goto retry;
1097         }
1098
1099         error = SYSCTL_IN(req, arg1, arg2);
1100
1101         return (error);
1102 }
1103
1104 /*
1105  * Transfer functions to/from kernel space.
1106  * XXX: rather untested at this point
1107  */
1108 static int
1109 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1110 {
1111         size_t i = 0;
1112
1113         if (req->oldptr) {
1114                 i = l;
1115                 if (req->oldlen <= req->oldidx)
1116                         i = 0;
1117                 else
1118                         if (i > req->oldlen - req->oldidx)
1119                                 i = req->oldlen - req->oldidx;
1120                 if (i > 0)
1121                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
1122         }
1123         req->oldidx += l;
1124         if (req->oldptr && i != l)
1125                 return (ENOMEM);
1126         return (0);
1127 }
1128
1129 static int
1130 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1131 {
1132         if (!req->newptr)
1133                 return (0);
1134         if (req->newlen - req->newidx < l)
1135                 return (EINVAL);
1136         bcopy((char *)req->newptr + req->newidx, p, l);
1137         req->newidx += l;
1138         return (0);
1139 }
1140
1141 int
1142 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1143     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1144 {
1145         int error = 0;
1146         struct sysctl_req req;
1147
1148         bzero(&req, sizeof req);
1149
1150         req.td = td;
1151         req.flags = flags;
1152
1153         if (oldlenp) {
1154                 req.oldlen = *oldlenp;
1155         }
1156         req.validlen = req.oldlen;
1157
1158         if (old) {
1159                 req.oldptr= old;
1160         }
1161
1162         if (new != NULL) {
1163                 req.newlen = newlen;
1164                 req.newptr = new;
1165         }
1166
1167         req.oldfunc = sysctl_old_kernel;
1168         req.newfunc = sysctl_new_kernel;
1169         req.lock = REQ_LOCKED;
1170
1171         SYSCTL_SLOCK();
1172         error = sysctl_root(0, name, namelen, &req);
1173         SYSCTL_SUNLOCK();
1174
1175         if (req.lock == REQ_WIRED && req.validlen > 0)
1176                 vsunlock(req.oldptr, req.validlen);
1177
1178         if (error && error != ENOMEM)
1179                 return (error);
1180
1181         if (retval) {
1182                 if (req.oldptr && req.oldidx > req.validlen)
1183                         *retval = req.validlen;
1184                 else
1185                         *retval = req.oldidx;
1186         }
1187         return (error);
1188 }
1189
1190 int
1191 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1192     void *new, size_t newlen, size_t *retval, int flags)
1193 {
1194         int oid[CTL_MAXNAME];
1195         size_t oidlen, plen;
1196         int error;
1197
1198         oid[0] = 0;             /* sysctl internal magic */
1199         oid[1] = 3;             /* name2oid */
1200         oidlen = sizeof(oid);
1201
1202         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1203             (void *)name, strlen(name), &plen, flags);
1204         if (error)
1205                 return (error);
1206
1207         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1208             new, newlen, retval, flags);
1209         return (error);
1210 }
1211
1212 /*
1213  * Transfer function to/from user space.
1214  */
1215 static int
1216 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1217 {
1218         int error = 0;
1219         size_t i, len, origidx;
1220
1221         origidx = req->oldidx;
1222         req->oldidx += l;
1223         if (req->oldptr == NULL)
1224                 return (0);
1225         /*
1226          * If we have not wired the user supplied buffer and we are currently
1227          * holding locks, drop a witness warning, as it's possible that
1228          * write operations to the user page can sleep.
1229          */
1230         if (req->lock != REQ_WIRED)
1231                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1232                     "sysctl_old_user()");
1233         i = l;
1234         len = req->validlen;
1235         if (len <= origidx)
1236                 i = 0;
1237         else {
1238                 if (i > len - origidx)
1239                         i = len - origidx;
1240                 error = copyout(p, (char *)req->oldptr + origidx, i);
1241         }
1242         if (error)
1243                 return (error);
1244         if (i < l)
1245                 return (ENOMEM);
1246         return (0);
1247 }
1248
1249 static int
1250 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1251 {
1252         int error;
1253
1254         if (!req->newptr)
1255                 return (0);
1256         if (req->newlen - req->newidx < l)
1257                 return (EINVAL);
1258         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1259             "sysctl_new_user()");
1260         error = copyin((char *)req->newptr + req->newidx, p, l);
1261         req->newidx += l;
1262         return (error);
1263 }
1264
1265 /*
1266  * Wire the user space destination buffer.  If set to a value greater than
1267  * zero, the len parameter limits the maximum amount of wired memory.
1268  */
1269 int
1270 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1271 {
1272         int ret;
1273         size_t i, wiredlen;
1274         char *cp, dummy;
1275
1276         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1277         ret = 0;
1278         if (req->lock == REQ_LOCKED && req->oldptr &&
1279             req->oldfunc == sysctl_old_user) {
1280                 if (wiredlen != 0) {
1281                         ret = vslock(req->oldptr, wiredlen);
1282                         if (ret != 0) {
1283                                 if (ret != ENOMEM)
1284                                         return (ret);
1285                                 wiredlen = 0;
1286                         }
1287                         /*
1288                          * Touch all the wired pages to avoid PTE modified
1289                          * bit emulation traps on Alpha while holding locks
1290                          * in the sysctl handler.
1291                          */
1292                         for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE,
1293                             cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) {
1294                                 copyin(cp, &dummy, 1);
1295                                 copyout(&dummy, cp, 1);
1296                         }
1297                 }
1298                 req->lock = REQ_WIRED;
1299                 req->validlen = wiredlen;
1300         }
1301         return (0);
1302 }
1303
1304 int
1305 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1306     int *nindx, struct sysctl_req *req)
1307 {
1308         struct sysctl_oid *oid;
1309         int indx;
1310
1311         SYSCTL_ASSERT_LOCKED();
1312         oid = SLIST_FIRST(&sysctl__children);
1313         indx = 0;
1314         while (oid && indx < CTL_MAXNAME) {
1315                 if (oid->oid_number == name[indx]) {
1316                         indx++;
1317                         if (oid->oid_kind & CTLFLAG_NOLOCK)
1318                                 req->lock = REQ_UNLOCKED;
1319                         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1320                                 if (oid->oid_handler != NULL ||
1321                                     indx == namelen) {
1322                                         *noid = oid;
1323                                         if (nindx != NULL)
1324                                                 *nindx = indx;
1325                                         return (0);
1326                                 }
1327                                 oid = SLIST_FIRST(
1328                                     (struct sysctl_oid_list *)oid->oid_arg1);
1329                         } else if (indx == namelen) {
1330                                 *noid = oid;
1331                                 if (nindx != NULL)
1332                                         *nindx = indx;
1333                                 return (0);
1334                         } else {
1335                                 return (ENOTDIR);
1336                         }
1337                 } else {
1338                         oid = SLIST_NEXT(oid, oid_link);
1339                 }
1340         }
1341         return (ENOENT);
1342 }
1343
1344 /*
1345  * Traverse our tree, and find the right node, execute whatever it points
1346  * to, and return the resulting error code.
1347  */
1348
1349 static int
1350 sysctl_root(SYSCTL_HANDLER_ARGS)
1351 {
1352         struct sysctl_oid *oid;
1353         int error, indx, lvl;
1354
1355         SYSCTL_ASSERT_LOCKED();
1356
1357         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1358         if (error)
1359                 return (error);
1360
1361         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1362                 /*
1363                  * You can't call a sysctl when it's a node, but has
1364                  * no handler.  Inform the user that it's a node.
1365                  * The indx may or may not be the same as namelen.
1366                  */
1367                 if (oid->oid_handler == NULL)
1368                         return (EISDIR);
1369         }
1370
1371         /* Is this sysctl writable? */
1372         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1373                 return (EPERM);
1374
1375         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1376
1377         /* Is this sysctl sensitive to securelevels? */
1378         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1379                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1380                 error = securelevel_gt(req->td->td_ucred, lvl);
1381                 if (error)
1382                         return (error);
1383         }
1384
1385         /* Is this sysctl writable by only privileged users? */
1386         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1387                 if (oid->oid_kind & CTLFLAG_PRISON)
1388                         error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1389                 else
1390                         error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1391                 if (error)
1392                         return (error);
1393         }
1394
1395         if (!oid->oid_handler)
1396                 return (EINVAL);
1397
1398         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1399                 arg1 = (int *)arg1 + indx;
1400                 arg2 -= indx;
1401         } else {
1402                 arg1 = oid->oid_arg1;
1403                 arg2 = oid->oid_arg2;
1404         }
1405 #ifdef MAC
1406         error = mac_check_system_sysctl(req->td->td_ucred, oid, arg1, arg2,
1407             req);
1408         if (error != 0)
1409                 return (error);
1410 #endif
1411         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1412                 mtx_lock(&Giant);
1413         error = oid->oid_handler(oid, arg1, arg2, req);
1414         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1415                 mtx_unlock(&Giant);
1416
1417         return (error);
1418 }
1419
1420 #ifndef _SYS_SYSPROTO_H_
1421 struct sysctl_args {
1422         int     *name;
1423         u_int   namelen;
1424         void    *old;
1425         size_t  *oldlenp;
1426         void    *new;
1427         size_t  newlen;
1428 };
1429 #endif
1430 int
1431 __sysctl(struct thread *td, struct sysctl_args *uap)
1432 {
1433         int error, i, name[CTL_MAXNAME];
1434         size_t j;
1435
1436         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1437                 return (EINVAL);
1438
1439         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1440         if (error)
1441                 return (error);
1442
1443         error = userland_sysctl(td, name, uap->namelen,
1444                 uap->old, uap->oldlenp, 0,
1445                 uap->new, uap->newlen, &j, 0);
1446         if (error && error != ENOMEM)
1447                 return (error);
1448         if (uap->oldlenp) {
1449                 i = copyout(&j, uap->oldlenp, sizeof(j));
1450                 if (i)
1451                         return (i);
1452         }
1453         return (error);
1454 }
1455
1456 /*
1457  * This is used from various compatibility syscalls too.  That's why name
1458  * must be in kernel space.
1459  */
1460 int
1461 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1462     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1463     int flags)
1464 {
1465         int error = 0;
1466         struct sysctl_req req;
1467
1468         bzero(&req, sizeof req);
1469
1470         req.td = td;
1471         req.flags = flags;
1472
1473         if (oldlenp) {
1474                 if (inkernel) {
1475                         req.oldlen = *oldlenp;
1476                 } else {
1477                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1478                         if (error)
1479                                 return (error);
1480                 }
1481         }
1482         req.validlen = req.oldlen;
1483
1484         if (old) {
1485                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1486                         return (EFAULT);
1487                 req.oldptr= old;
1488         }
1489
1490         if (new != NULL) {
1491                 if (!useracc(new, newlen, VM_PROT_READ))
1492                         return (EFAULT);
1493                 req.newlen = newlen;
1494                 req.newptr = new;
1495         }
1496
1497         req.oldfunc = sysctl_old_user;
1498         req.newfunc = sysctl_new_user;
1499         req.lock = REQ_LOCKED;
1500
1501         SYSCTL_XLOCK();
1502
1503         for (;;) {
1504                 req.oldidx = 0;
1505                 req.newidx = 0;
1506                 error = sysctl_root(0, name, namelen, &req);
1507                 if (error != EAGAIN)
1508                         break;
1509                 uio_yield();
1510         }
1511
1512
1513         if (req.lock == REQ_WIRED && req.validlen > 0)
1514                 vsunlock(req.oldptr, req.validlen);
1515         SYSCTL_XUNLOCK();
1516
1517         if (error && error != ENOMEM)
1518                 return (error);
1519
1520         if (retval) {
1521                 if (req.oldptr && req.oldidx > req.validlen)
1522                         *retval = req.validlen;
1523                 else
1524                         *retval = req.oldidx;
1525         }
1526         return (error);
1527 }