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