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