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