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