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 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_capsicum.h" 42 #include "opt_compat.h" 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/fail.h> 47 #include <sys/systm.h> 48 #include <sys/capsicum.h> 49 #include <sys/kernel.h> 50 #include <sys/sysctl.h> 51 #include <sys/malloc.h> 52 #include <sys/priv.h> 53 #include <sys/proc.h> 54 #include <sys/jail.h> 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <sys/rmlock.h> 58 #include <sys/sbuf.h> 59 #include <sys/sx.h> 60 #include <sys/sysproto.h> 61 #include <sys/uio.h> 62 #ifdef KTRACE 63 #include <sys/ktrace.h> 64 #endif 65 66 #include <net/vnet.h> 67 68 #include <security/mac/mac_framework.h> 69 70 #include <vm/vm.h> 71 #include <vm/vm_extern.h> 72 73 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 74 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 75 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 76 77 /* 78 * The sysctllock protects the MIB tree. It also protects sysctl 79 * contexts used with dynamic sysctls. The sysctl_register_oid() and 80 * sysctl_unregister_oid() routines require the sysctllock to already 81 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are 82 * provided for the few places in the kernel which need to use that 83 * API rather than using the dynamic API. Use of the dynamic API is 84 * strongly encouraged for most code. 85 * 86 * The sysctlmemlock is used to limit the amount of user memory wired for 87 * sysctl requests. This is implemented by serializing any userland 88 * sysctl requests larger than a single page via an exclusive lock. 89 */ 90 static struct rmlock sysctllock; 91 static struct sx __exclusive_cache_line sysctlmemlock; 92 93 #define SYSCTL_WLOCK() rm_wlock(&sysctllock) 94 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock) 95 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker)) 96 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker)) 97 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock) 98 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED) 99 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED) 100 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED) 101 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \ 102 RM_SLEEPABLE) 103 #define SYSCTL_SLEEP(ch, wmesg, timo) \ 104 rm_sleep(ch, &sysctllock, 0, wmesg, timo) 105 106 static int sysctl_root(SYSCTL_HANDLER_ARGS); 107 108 /* Root list */ 109 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children); 110 111 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 112 int recurse); 113 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t); 114 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); 115 116 static struct sysctl_oid * 117 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 118 { 119 struct sysctl_oid *oidp; 120 121 SYSCTL_ASSERT_LOCKED(); 122 SLIST_FOREACH(oidp, list, oid_link) { 123 if (strcmp(oidp->oid_name, name) == 0) { 124 return (oidp); 125 } 126 } 127 return (NULL); 128 } 129 130 /* 131 * Initialization of the MIB tree. 132 * 133 * Order by number in each list. 134 */ 135 void 136 sysctl_wlock(void) 137 { 138 139 SYSCTL_WLOCK(); 140 } 141 142 void 143 sysctl_wunlock(void) 144 { 145 146 SYSCTL_WUNLOCK(); 147 } 148 149 static int 150 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2, 151 struct sysctl_req *req, struct rm_priotracker *tracker) 152 { 153 int error; 154 155 if (oid->oid_kind & CTLFLAG_DYN) 156 atomic_add_int(&oid->oid_running, 1); 157 158 if (tracker != NULL) 159 SYSCTL_RUNLOCK(tracker); 160 else 161 SYSCTL_WUNLOCK(); 162 163 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 164 mtx_lock(&Giant); 165 error = oid->oid_handler(oid, arg1, arg2, req); 166 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 167 mtx_unlock(&Giant); 168 169 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error); 170 171 if (tracker != NULL) 172 SYSCTL_RLOCK(tracker); 173 else 174 SYSCTL_WLOCK(); 175 176 if (oid->oid_kind & CTLFLAG_DYN) { 177 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 && 178 (oid->oid_kind & CTLFLAG_DYING) != 0) 179 wakeup(&oid->oid_running); 180 } 181 182 return (error); 183 } 184 185 static void 186 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp) 187 { 188 struct sysctl_req req; 189 struct sysctl_oid *curr; 190 char *penv = NULL; 191 char path[64]; 192 ssize_t rem = sizeof(path); 193 ssize_t len; 194 uint8_t val_8; 195 uint16_t val_16; 196 uint32_t val_32; 197 int val_int; 198 long val_long; 199 int64_t val_64; 200 quad_t val_quad; 201 int error; 202 203 path[--rem] = 0; 204 205 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) { 206 len = strlen(curr->oid_name); 207 rem -= len; 208 if (curr != oidp) 209 rem -= 1; 210 if (rem < 0) { 211 printf("OID path exceeds %d bytes\n", (int)sizeof(path)); 212 return; 213 } 214 memcpy(path + rem, curr->oid_name, len); 215 if (curr != oidp) 216 path[rem + len] = '.'; 217 } 218 219 memset(&req, 0, sizeof(req)); 220 221 req.td = curthread; 222 req.oldfunc = sysctl_old_kernel; 223 req.newfunc = sysctl_new_kernel; 224 req.lock = REQ_UNWIRED; 225 226 switch (oidp->oid_kind & CTLTYPE) { 227 case CTLTYPE_INT: 228 if (getenv_int(path + rem, &val_int) == 0) 229 return; 230 req.newlen = sizeof(val_int); 231 req.newptr = &val_int; 232 break; 233 case CTLTYPE_UINT: 234 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0) 235 return; 236 req.newlen = sizeof(val_int); 237 req.newptr = &val_int; 238 break; 239 case CTLTYPE_LONG: 240 if (getenv_long(path + rem, &val_long) == 0) 241 return; 242 req.newlen = sizeof(val_long); 243 req.newptr = &val_long; 244 break; 245 case CTLTYPE_ULONG: 246 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0) 247 return; 248 req.newlen = sizeof(val_long); 249 req.newptr = &val_long; 250 break; 251 case CTLTYPE_S8: 252 if (getenv_int(path + rem, &val_int) == 0) 253 return; 254 val_8 = val_int; 255 req.newlen = sizeof(val_8); 256 req.newptr = &val_8; 257 break; 258 case CTLTYPE_S16: 259 if (getenv_int(path + rem, &val_int) == 0) 260 return; 261 val_16 = val_int; 262 req.newlen = sizeof(val_16); 263 req.newptr = &val_16; 264 break; 265 case CTLTYPE_S32: 266 if (getenv_long(path + rem, &val_long) == 0) 267 return; 268 val_32 = val_long; 269 req.newlen = sizeof(val_32); 270 req.newptr = &val_32; 271 break; 272 case CTLTYPE_S64: 273 if (getenv_quad(path + rem, &val_quad) == 0) 274 return; 275 val_64 = val_quad; 276 req.newlen = sizeof(val_64); 277 req.newptr = &val_64; 278 break; 279 case CTLTYPE_U8: 280 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0) 281 return; 282 val_8 = val_int; 283 req.newlen = sizeof(val_8); 284 req.newptr = &val_8; 285 break; 286 case CTLTYPE_U16: 287 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0) 288 return; 289 val_16 = val_int; 290 req.newlen = sizeof(val_16); 291 req.newptr = &val_16; 292 break; 293 case CTLTYPE_U32: 294 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0) 295 return; 296 val_32 = val_long; 297 req.newlen = sizeof(val_32); 298 req.newptr = &val_32; 299 break; 300 case CTLTYPE_U64: 301 /* XXX there is no getenv_uquad() */ 302 if (getenv_quad(path + rem, &val_quad) == 0) 303 return; 304 val_64 = val_quad; 305 req.newlen = sizeof(val_64); 306 req.newptr = &val_64; 307 break; 308 case CTLTYPE_STRING: 309 penv = kern_getenv(path + rem); 310 if (penv == NULL) 311 return; 312 req.newlen = strlen(penv); 313 req.newptr = penv; 314 break; 315 default: 316 return; 317 } 318 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1, 319 oidp->oid_arg2, &req, NULL); 320 if (error != 0) 321 printf("Setting sysctl %s failed: %d\n", path + rem, error); 322 if (penv != NULL) 323 freeenv(penv); 324 } 325 326 static int 327 sbuf_printf_drain(void *arg __unused, const char *data, int len) 328 { 329 330 return (printf("%.*s", len, data)); 331 } 332 333 /* 334 * Locate the path to a given oid. Returns the length of the resulting path, 335 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME 336 * elements and be NULL initialized. 337 */ 338 static int 339 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle) 340 { 341 int indx; 342 343 SYSCTL_ASSERT_LOCKED(); 344 indx = 0; 345 while (indx < CTL_MAXNAME && indx >= 0) { 346 if (nodes[indx] == NULL && indx == 0) 347 nodes[indx] = SLIST_FIRST(&sysctl__children); 348 else if (nodes[indx] == NULL) 349 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children); 350 else 351 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link); 352 353 if (nodes[indx] == needle) 354 return (indx + 1); 355 356 if (nodes[indx] == NULL) { 357 indx--; 358 continue; 359 } 360 361 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 362 indx++; 363 continue; 364 } 365 } 366 return (-1); 367 } 368 369 static void 370 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf) 371 { 372 struct sysctl_oid *nodes[CTL_MAXNAME]; 373 char buf[128]; 374 struct sbuf sb; 375 int rc, i; 376 377 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL); 378 sbuf_set_drain(&sb, sbuf_printf_drain, NULL); 379 380 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__); 381 382 memset(nodes, 0, sizeof(nodes)); 383 rc = sysctl_search_oid(nodes, leaf); 384 if (rc > 0) { 385 for (i = 0; i < rc; i++) 386 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name, 387 i != (rc - 1), "."); 388 } else { 389 sbuf_printf(&sb, "%s", leaf->oid_name); 390 } 391 sbuf_printf(&sb, ")!\n"); 392 393 (void)sbuf_finish(&sb); 394 } 395 396 #ifdef SYSCTL_DEBUG 397 static int 398 sysctl_reuse_test(SYSCTL_HANDLER_ARGS) 399 { 400 struct rm_priotracker tracker; 401 402 SYSCTL_RLOCK(&tracker); 403 sysctl_warn_reuse(__func__, oidp); 404 SYSCTL_RUNLOCK(&tracker); 405 return (0); 406 } 407 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE, 408 0, 0, sysctl_reuse_test, "-", ""); 409 #endif 410 411 void 412 sysctl_register_oid(struct sysctl_oid *oidp) 413 { 414 struct sysctl_oid_list *parent = oidp->oid_parent; 415 struct sysctl_oid *p; 416 struct sysctl_oid *q; 417 int oid_number; 418 int timeout = 2; 419 420 /* 421 * First check if another oid with the same name already 422 * exists in the parent's list. 423 */ 424 SYSCTL_ASSERT_WLOCKED(); 425 p = sysctl_find_oidname(oidp->oid_name, parent); 426 if (p != NULL) { 427 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 428 p->oid_refcnt++; 429 return; 430 } else { 431 sysctl_warn_reuse(__func__, p); 432 return; 433 } 434 } 435 /* get current OID number */ 436 oid_number = oidp->oid_number; 437 438 #if (OID_AUTO >= 0) 439 #error "OID_AUTO is expected to be a negative value" 440 #endif 441 /* 442 * Any negative OID number qualifies as OID_AUTO. Valid OID 443 * numbers should always be positive. 444 * 445 * NOTE: DO NOT change the starting value here, change it in 446 * <sys/sysctl.h>, and make sure it is at least 256 to 447 * accommodate e.g. net.inet.raw as a static sysctl node. 448 */ 449 if (oid_number < 0) { 450 static int newoid; 451 452 /* 453 * By decrementing the next OID number we spend less 454 * time inserting the OIDs into a sorted list. 455 */ 456 if (--newoid < CTL_AUTO_START) 457 newoid = 0x7fffffff; 458 459 oid_number = newoid; 460 } 461 462 /* 463 * Insert the OID into the parent's list sorted by OID number. 464 */ 465 retry: 466 q = NULL; 467 SLIST_FOREACH(p, parent, oid_link) { 468 /* check if the current OID number is in use */ 469 if (oid_number == p->oid_number) { 470 /* get the next valid OID number */ 471 if (oid_number < CTL_AUTO_START || 472 oid_number == 0x7fffffff) { 473 /* wraparound - restart */ 474 oid_number = CTL_AUTO_START; 475 /* don't loop forever */ 476 if (!timeout--) 477 panic("sysctl: Out of OID numbers\n"); 478 goto retry; 479 } else { 480 oid_number++; 481 } 482 } else if (oid_number < p->oid_number) 483 break; 484 q = p; 485 } 486 /* check for non-auto OID number collision */ 487 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START && 488 oid_number >= CTL_AUTO_START) { 489 printf("sysctl: OID number(%d) is already in use for '%s'\n", 490 oidp->oid_number, oidp->oid_name); 491 } 492 /* update the OID number, if any */ 493 oidp->oid_number = oid_number; 494 if (q != NULL) 495 SLIST_INSERT_AFTER(q, oidp, oid_link); 496 else 497 SLIST_INSERT_HEAD(parent, oidp, oid_link); 498 499 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE && 500 #ifdef VIMAGE 501 (oidp->oid_kind & CTLFLAG_VNET) == 0 && 502 #endif 503 (oidp->oid_kind & CTLFLAG_TUN) != 0 && 504 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) { 505 /* only fetch value once */ 506 oidp->oid_kind |= CTLFLAG_NOFETCH; 507 /* try to fetch value from kernel environment */ 508 sysctl_load_tunable_by_oid_locked(oidp); 509 } 510 } 511 512 void 513 sysctl_register_disabled_oid(struct sysctl_oid *oidp) 514 { 515 516 /* 517 * Mark the leaf as dormant if it's not to be immediately enabled. 518 * We do not disable nodes as they can be shared between modules 519 * and it is always safe to access a node. 520 */ 521 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0, 522 ("internal flag is set in oid_kind")); 523 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 524 oidp->oid_kind |= CTLFLAG_DORMANT; 525 sysctl_register_oid(oidp); 526 } 527 528 void 529 sysctl_enable_oid(struct sysctl_oid *oidp) 530 { 531 532 SYSCTL_ASSERT_WLOCKED(); 533 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 534 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0, 535 ("sysctl node is marked as dormant")); 536 return; 537 } 538 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0, 539 ("enabling already enabled sysctl oid")); 540 oidp->oid_kind &= ~CTLFLAG_DORMANT; 541 } 542 543 void 544 sysctl_unregister_oid(struct sysctl_oid *oidp) 545 { 546 struct sysctl_oid *p; 547 int error; 548 549 SYSCTL_ASSERT_WLOCKED(); 550 error = ENOENT; 551 if (oidp->oid_number == OID_AUTO) { 552 error = EINVAL; 553 } else { 554 SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 555 if (p == oidp) { 556 SLIST_REMOVE(oidp->oid_parent, oidp, 557 sysctl_oid, oid_link); 558 error = 0; 559 break; 560 } 561 } 562 } 563 564 /* 565 * This can happen when a module fails to register and is 566 * being unloaded afterwards. It should not be a panic() 567 * for normal use. 568 */ 569 if (error) 570 printf("%s: failed to unregister sysctl\n", __func__); 571 } 572 573 /* Initialize a new context to keep track of dynamically added sysctls. */ 574 int 575 sysctl_ctx_init(struct sysctl_ctx_list *c) 576 { 577 578 if (c == NULL) { 579 return (EINVAL); 580 } 581 582 /* 583 * No locking here, the caller is responsible for not adding 584 * new nodes to a context until after this function has 585 * returned. 586 */ 587 TAILQ_INIT(c); 588 return (0); 589 } 590 591 /* Free the context, and destroy all dynamic oids registered in this context */ 592 int 593 sysctl_ctx_free(struct sysctl_ctx_list *clist) 594 { 595 struct sysctl_ctx_entry *e, *e1; 596 int error; 597 598 error = 0; 599 /* 600 * First perform a "dry run" to check if it's ok to remove oids. 601 * XXX FIXME 602 * XXX This algorithm is a hack. But I don't know any 603 * XXX better solution for now... 604 */ 605 SYSCTL_WLOCK(); 606 TAILQ_FOREACH(e, clist, link) { 607 error = sysctl_remove_oid_locked(e->entry, 0, 0); 608 if (error) 609 break; 610 } 611 /* 612 * Restore deregistered entries, either from the end, 613 * or from the place where error occurred. 614 * e contains the entry that was not unregistered 615 */ 616 if (error) 617 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 618 else 619 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 620 while (e1 != NULL) { 621 sysctl_register_oid(e1->entry); 622 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 623 } 624 if (error) { 625 SYSCTL_WUNLOCK(); 626 return(EBUSY); 627 } 628 /* Now really delete the entries */ 629 e = TAILQ_FIRST(clist); 630 while (e != NULL) { 631 e1 = TAILQ_NEXT(e, link); 632 error = sysctl_remove_oid_locked(e->entry, 1, 0); 633 if (error) 634 panic("sysctl_remove_oid: corrupt tree, entry: %s", 635 e->entry->oid_name); 636 free(e, M_SYSCTLOID); 637 e = e1; 638 } 639 SYSCTL_WUNLOCK(); 640 return (error); 641 } 642 643 /* Add an entry to the context */ 644 struct sysctl_ctx_entry * 645 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 646 { 647 struct sysctl_ctx_entry *e; 648 649 SYSCTL_ASSERT_WLOCKED(); 650 if (clist == NULL || oidp == NULL) 651 return(NULL); 652 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 653 e->entry = oidp; 654 TAILQ_INSERT_HEAD(clist, e, link); 655 return (e); 656 } 657 658 /* Find an entry in the context */ 659 struct sysctl_ctx_entry * 660 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 661 { 662 struct sysctl_ctx_entry *e; 663 664 SYSCTL_ASSERT_WLOCKED(); 665 if (clist == NULL || oidp == NULL) 666 return(NULL); 667 TAILQ_FOREACH(e, clist, link) { 668 if(e->entry == oidp) 669 return(e); 670 } 671 return (e); 672 } 673 674 /* 675 * Delete an entry from the context. 676 * NOTE: this function doesn't free oidp! You have to remove it 677 * with sysctl_remove_oid(). 678 */ 679 int 680 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 681 { 682 struct sysctl_ctx_entry *e; 683 684 if (clist == NULL || oidp == NULL) 685 return (EINVAL); 686 SYSCTL_WLOCK(); 687 e = sysctl_ctx_entry_find(clist, oidp); 688 if (e != NULL) { 689 TAILQ_REMOVE(clist, e, link); 690 SYSCTL_WUNLOCK(); 691 free(e, M_SYSCTLOID); 692 return (0); 693 } else { 694 SYSCTL_WUNLOCK(); 695 return (ENOENT); 696 } 697 } 698 699 /* 700 * Remove dynamically created sysctl trees. 701 * oidp - top of the tree to be removed 702 * del - if 0 - just deregister, otherwise free up entries as well 703 * recurse - if != 0 traverse the subtree to be deleted 704 */ 705 int 706 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 707 { 708 int error; 709 710 SYSCTL_WLOCK(); 711 error = sysctl_remove_oid_locked(oidp, del, recurse); 712 SYSCTL_WUNLOCK(); 713 return (error); 714 } 715 716 int 717 sysctl_remove_name(struct sysctl_oid *parent, const char *name, 718 int del, int recurse) 719 { 720 struct sysctl_oid *p, *tmp; 721 int error; 722 723 error = ENOENT; 724 SYSCTL_WLOCK(); 725 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) { 726 if (strcmp(p->oid_name, name) == 0) { 727 error = sysctl_remove_oid_locked(p, del, recurse); 728 break; 729 } 730 } 731 SYSCTL_WUNLOCK(); 732 733 return (error); 734 } 735 736 737 static int 738 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 739 { 740 struct sysctl_oid *p, *tmp; 741 int error; 742 743 SYSCTL_ASSERT_WLOCKED(); 744 if (oidp == NULL) 745 return(EINVAL); 746 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 747 printf("Warning: can't remove non-dynamic nodes (%s)!\n", 748 oidp->oid_name); 749 return (EINVAL); 750 } 751 /* 752 * WARNING: normal method to do this should be through 753 * sysctl_ctx_free(). Use recursing as the last resort 754 * method to purge your sysctl tree of leftovers... 755 * However, if some other code still references these nodes, 756 * it will panic. 757 */ 758 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 759 if (oidp->oid_refcnt == 1) { 760 SLIST_FOREACH_SAFE(p, 761 SYSCTL_CHILDREN(oidp), oid_link, tmp) { 762 if (!recurse) { 763 printf("Warning: failed attempt to " 764 "remove oid %s with child %s\n", 765 oidp->oid_name, p->oid_name); 766 return (ENOTEMPTY); 767 } 768 error = sysctl_remove_oid_locked(p, del, 769 recurse); 770 if (error) 771 return (error); 772 } 773 } 774 } 775 if (oidp->oid_refcnt > 1 ) { 776 oidp->oid_refcnt--; 777 } else { 778 if (oidp->oid_refcnt == 0) { 779 printf("Warning: bad oid_refcnt=%u (%s)!\n", 780 oidp->oid_refcnt, oidp->oid_name); 781 return (EINVAL); 782 } 783 sysctl_unregister_oid(oidp); 784 if (del) { 785 /* 786 * Wait for all threads running the handler to drain. 787 * This preserves the previous behavior when the 788 * sysctl lock was held across a handler invocation, 789 * and is necessary for module unload correctness. 790 */ 791 while (oidp->oid_running > 0) { 792 oidp->oid_kind |= CTLFLAG_DYING; 793 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0); 794 } 795 if (oidp->oid_descr) 796 free(__DECONST(char *, oidp->oid_descr), 797 M_SYSCTLOID); 798 if (oidp->oid_label) 799 free(__DECONST(char *, oidp->oid_label), 800 M_SYSCTLOID); 801 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID); 802 free(oidp, M_SYSCTLOID); 803 } 804 } 805 return (0); 806 } 807 /* 808 * Create new sysctls at run time. 809 * clist may point to a valid context initialized with sysctl_ctx_init(). 810 */ 811 struct sysctl_oid * 812 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 813 int number, const char *name, int kind, void *arg1, intmax_t arg2, 814 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr, 815 const char *label) 816 { 817 struct sysctl_oid *oidp; 818 819 /* You have to hook up somewhere.. */ 820 if (parent == NULL) 821 return(NULL); 822 /* Check if the node already exists, otherwise create it */ 823 SYSCTL_WLOCK(); 824 oidp = sysctl_find_oidname(name, parent); 825 if (oidp != NULL) { 826 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 827 oidp->oid_refcnt++; 828 /* Update the context */ 829 if (clist != NULL) 830 sysctl_ctx_entry_add(clist, oidp); 831 SYSCTL_WUNLOCK(); 832 return (oidp); 833 } else { 834 sysctl_warn_reuse(__func__, oidp); 835 SYSCTL_WUNLOCK(); 836 return (NULL); 837 } 838 } 839 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 840 oidp->oid_parent = parent; 841 SLIST_INIT(&oidp->oid_children); 842 oidp->oid_number = number; 843 oidp->oid_refcnt = 1; 844 oidp->oid_name = strdup(name, M_SYSCTLOID); 845 oidp->oid_handler = handler; 846 oidp->oid_kind = CTLFLAG_DYN | kind; 847 oidp->oid_arg1 = arg1; 848 oidp->oid_arg2 = arg2; 849 oidp->oid_fmt = fmt; 850 if (descr != NULL) 851 oidp->oid_descr = strdup(descr, M_SYSCTLOID); 852 if (label != NULL) 853 oidp->oid_label = strdup(label, M_SYSCTLOID); 854 /* Update the context, if used */ 855 if (clist != NULL) 856 sysctl_ctx_entry_add(clist, oidp); 857 /* Register this oid */ 858 sysctl_register_oid(oidp); 859 SYSCTL_WUNLOCK(); 860 return (oidp); 861 } 862 863 /* 864 * Rename an existing oid. 865 */ 866 void 867 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 868 { 869 char *newname; 870 char *oldname; 871 872 newname = strdup(name, M_SYSCTLOID); 873 SYSCTL_WLOCK(); 874 oldname = __DECONST(char *, oidp->oid_name); 875 oidp->oid_name = newname; 876 SYSCTL_WUNLOCK(); 877 free(oldname, M_SYSCTLOID); 878 } 879 880 /* 881 * Reparent an existing oid. 882 */ 883 int 884 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 885 { 886 struct sysctl_oid *oidp; 887 888 SYSCTL_WLOCK(); 889 if (oid->oid_parent == parent) { 890 SYSCTL_WUNLOCK(); 891 return (0); 892 } 893 oidp = sysctl_find_oidname(oid->oid_name, parent); 894 if (oidp != NULL) { 895 SYSCTL_WUNLOCK(); 896 return (EEXIST); 897 } 898 sysctl_unregister_oid(oid); 899 oid->oid_parent = parent; 900 oid->oid_number = OID_AUTO; 901 sysctl_register_oid(oid); 902 SYSCTL_WUNLOCK(); 903 return (0); 904 } 905 906 /* 907 * Register the kernel's oids on startup. 908 */ 909 SET_DECLARE(sysctl_set, struct sysctl_oid); 910 911 static void 912 sysctl_register_all(void *arg) 913 { 914 struct sysctl_oid **oidp; 915 916 sx_init(&sysctlmemlock, "sysctl mem"); 917 SYSCTL_INIT(); 918 SYSCTL_WLOCK(); 919 SET_FOREACH(oidp, sysctl_set) 920 sysctl_register_oid(*oidp); 921 SYSCTL_WUNLOCK(); 922 } 923 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0); 924 925 /* 926 * "Staff-functions" 927 * 928 * These functions implement a presently undocumented interface 929 * used by the sysctl program to walk the tree, and get the type 930 * so it can print the value. 931 * This interface is under work and consideration, and should probably 932 * be killed with a big axe by the first person who can find the time. 933 * (be aware though, that the proper interface isn't as obvious as it 934 * may seem, there are various conflicting requirements. 935 * 936 * {0,0} printf the entire MIB-tree. 937 * {0,1,...} return the name of the "..." OID. 938 * {0,2,...} return the next OID. 939 * {0,3} return the OID of the name in "new" 940 * {0,4,...} return the kind & format info for the "..." OID. 941 * {0,5,...} return the description of the "..." OID. 942 * {0,6,...} return the aggregation label of the "..." OID. 943 */ 944 945 #ifdef SYSCTL_DEBUG 946 static void 947 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 948 { 949 int k; 950 struct sysctl_oid *oidp; 951 952 SYSCTL_ASSERT_LOCKED(); 953 SLIST_FOREACH(oidp, l, oid_link) { 954 955 for (k=0; k<i; k++) 956 printf(" "); 957 958 printf("%d %s ", oidp->oid_number, oidp->oid_name); 959 960 printf("%c%c", 961 oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 962 oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 963 964 if (oidp->oid_handler) 965 printf(" *Handler"); 966 967 switch (oidp->oid_kind & CTLTYPE) { 968 case CTLTYPE_NODE: 969 printf(" Node\n"); 970 if (!oidp->oid_handler) { 971 sysctl_sysctl_debug_dump_node( 972 SYSCTL_CHILDREN(oidp), i + 2); 973 } 974 break; 975 case CTLTYPE_INT: printf(" Int\n"); break; 976 case CTLTYPE_UINT: printf(" u_int\n"); break; 977 case CTLTYPE_LONG: printf(" Long\n"); break; 978 case CTLTYPE_ULONG: printf(" u_long\n"); break; 979 case CTLTYPE_STRING: printf(" String\n"); break; 980 case CTLTYPE_S8: printf(" int8_t\n"); break; 981 case CTLTYPE_S16: printf(" int16_t\n"); break; 982 case CTLTYPE_S32: printf(" int32_t\n"); break; 983 case CTLTYPE_S64: printf(" int64_t\n"); break; 984 case CTLTYPE_U8: printf(" uint8_t\n"); break; 985 case CTLTYPE_U16: printf(" uint16_t\n"); break; 986 case CTLTYPE_U32: printf(" uint32_t\n"); break; 987 case CTLTYPE_U64: printf(" uint64_t\n"); break; 988 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 989 default: printf("\n"); 990 } 991 992 } 993 } 994 995 static int 996 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 997 { 998 struct rm_priotracker tracker; 999 int error; 1000 1001 error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 1002 if (error) 1003 return (error); 1004 SYSCTL_RLOCK(&tracker); 1005 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 1006 SYSCTL_RUNLOCK(&tracker); 1007 return (ENOENT); 1008 } 1009 1010 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE, 1011 0, 0, sysctl_sysctl_debug, "-", ""); 1012 #endif 1013 1014 static int 1015 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 1016 { 1017 int *name = (int *) arg1; 1018 u_int namelen = arg2; 1019 int error = 0; 1020 struct sysctl_oid *oid; 1021 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 1022 struct rm_priotracker tracker; 1023 char buf[10]; 1024 1025 SYSCTL_RLOCK(&tracker); 1026 while (namelen) { 1027 if (!lsp) { 1028 snprintf(buf,sizeof(buf),"%d",*name); 1029 if (req->oldidx) 1030 error = SYSCTL_OUT(req, ".", 1); 1031 if (!error) 1032 error = SYSCTL_OUT(req, buf, strlen(buf)); 1033 if (error) 1034 goto out; 1035 namelen--; 1036 name++; 1037 continue; 1038 } 1039 lsp2 = NULL; 1040 SLIST_FOREACH(oid, lsp, oid_link) { 1041 if (oid->oid_number != *name) 1042 continue; 1043 1044 if (req->oldidx) 1045 error = SYSCTL_OUT(req, ".", 1); 1046 if (!error) 1047 error = SYSCTL_OUT(req, oid->oid_name, 1048 strlen(oid->oid_name)); 1049 if (error) 1050 goto out; 1051 1052 namelen--; 1053 name++; 1054 1055 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1056 break; 1057 1058 if (oid->oid_handler) 1059 break; 1060 1061 lsp2 = SYSCTL_CHILDREN(oid); 1062 break; 1063 } 1064 lsp = lsp2; 1065 } 1066 error = SYSCTL_OUT(req, "", 1); 1067 out: 1068 SYSCTL_RUNLOCK(&tracker); 1069 return (error); 1070 } 1071 1072 /* 1073 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in 1074 * capability mode. 1075 */ 1076 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, 1077 sysctl_sysctl_name, ""); 1078 1079 static int 1080 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 1081 int *next, int *len, int level, struct sysctl_oid **oidpp) 1082 { 1083 struct sysctl_oid *oidp; 1084 1085 SYSCTL_ASSERT_LOCKED(); 1086 *len = level; 1087 SLIST_FOREACH(oidp, lsp, oid_link) { 1088 *next = oidp->oid_number; 1089 *oidpp = oidp; 1090 1091 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0) 1092 continue; 1093 1094 if (!namelen) { 1095 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1096 return (0); 1097 if (oidp->oid_handler) 1098 /* We really should call the handler here...*/ 1099 return (0); 1100 lsp = SYSCTL_CHILDREN(oidp); 1101 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 1102 len, level+1, oidpp)) 1103 return (0); 1104 goto emptynode; 1105 } 1106 1107 if (oidp->oid_number < *name) 1108 continue; 1109 1110 if (oidp->oid_number > *name) { 1111 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1112 return (0); 1113 if (oidp->oid_handler) 1114 return (0); 1115 lsp = SYSCTL_CHILDREN(oidp); 1116 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 1117 next+1, len, level+1, oidpp)) 1118 return (0); 1119 goto next; 1120 } 1121 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1122 continue; 1123 1124 if (oidp->oid_handler) 1125 continue; 1126 1127 lsp = SYSCTL_CHILDREN(oidp); 1128 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 1129 len, level+1, oidpp)) 1130 return (0); 1131 next: 1132 namelen = 1; 1133 emptynode: 1134 *len = level; 1135 } 1136 return (1); 1137 } 1138 1139 static int 1140 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 1141 { 1142 int *name = (int *) arg1; 1143 u_int namelen = arg2; 1144 int i, j, error; 1145 struct sysctl_oid *oid; 1146 struct sysctl_oid_list *lsp = &sysctl__children; 1147 struct rm_priotracker tracker; 1148 int newoid[CTL_MAXNAME]; 1149 1150 SYSCTL_RLOCK(&tracker); 1151 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 1152 SYSCTL_RUNLOCK(&tracker); 1153 if (i) 1154 return (ENOENT); 1155 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 1156 return (error); 1157 } 1158 1159 /* 1160 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in 1161 * capability mode. 1162 */ 1163 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, 1164 sysctl_sysctl_next, ""); 1165 1166 static int 1167 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) 1168 { 1169 struct sysctl_oid *oidp; 1170 struct sysctl_oid_list *lsp = &sysctl__children; 1171 char *p; 1172 1173 SYSCTL_ASSERT_LOCKED(); 1174 1175 for (*len = 0; *len < CTL_MAXNAME;) { 1176 p = strsep(&name, "."); 1177 1178 oidp = SLIST_FIRST(lsp); 1179 for (;; oidp = SLIST_NEXT(oidp, oid_link)) { 1180 if (oidp == NULL) 1181 return (ENOENT); 1182 if (strcmp(p, oidp->oid_name) == 0) 1183 break; 1184 } 1185 *oid++ = oidp->oid_number; 1186 (*len)++; 1187 1188 if (name == NULL || *name == '\0') { 1189 if (oidpp) 1190 *oidpp = oidp; 1191 return (0); 1192 } 1193 1194 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1195 break; 1196 1197 if (oidp->oid_handler) 1198 break; 1199 1200 lsp = SYSCTL_CHILDREN(oidp); 1201 } 1202 return (ENOENT); 1203 } 1204 1205 static int 1206 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 1207 { 1208 char *p; 1209 int error, oid[CTL_MAXNAME], len = 0; 1210 struct sysctl_oid *op = NULL; 1211 struct rm_priotracker tracker; 1212 char buf[32]; 1213 1214 if (!req->newlen) 1215 return (ENOENT); 1216 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 1217 return (ENAMETOOLONG); 1218 1219 p = buf; 1220 if (req->newlen >= sizeof(buf)) 1221 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 1222 1223 error = SYSCTL_IN(req, p, req->newlen); 1224 if (error) { 1225 if (p != buf) 1226 free(p, M_SYSCTL); 1227 return (error); 1228 } 1229 1230 p [req->newlen] = '\0'; 1231 1232 SYSCTL_RLOCK(&tracker); 1233 error = name2oid(p, oid, &len, &op); 1234 SYSCTL_RUNLOCK(&tracker); 1235 1236 if (p != buf) 1237 free(p, M_SYSCTL); 1238 1239 if (error) 1240 return (error); 1241 1242 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 1243 return (error); 1244 } 1245 1246 /* 1247 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in 1248 * capability mode. 1249 */ 1250 SYSCTL_PROC(_sysctl, 3, name2oid, 1251 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE 1252 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", ""); 1253 1254 static int 1255 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 1256 { 1257 struct sysctl_oid *oid; 1258 struct rm_priotracker tracker; 1259 int error; 1260 1261 SYSCTL_RLOCK(&tracker); 1262 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1263 if (error) 1264 goto out; 1265 1266 if (oid->oid_fmt == NULL) { 1267 error = ENOENT; 1268 goto out; 1269 } 1270 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 1271 if (error) 1272 goto out; 1273 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 1274 out: 1275 SYSCTL_RUNLOCK(&tracker); 1276 return (error); 1277 } 1278 1279 1280 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1281 sysctl_sysctl_oidfmt, ""); 1282 1283 static int 1284 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 1285 { 1286 struct sysctl_oid *oid; 1287 struct rm_priotracker tracker; 1288 int error; 1289 1290 SYSCTL_RLOCK(&tracker); 1291 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1292 if (error) 1293 goto out; 1294 1295 if (oid->oid_descr == NULL) { 1296 error = ENOENT; 1297 goto out; 1298 } 1299 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 1300 out: 1301 SYSCTL_RUNLOCK(&tracker); 1302 return (error); 1303 } 1304 1305 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1306 sysctl_sysctl_oiddescr, ""); 1307 1308 static int 1309 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS) 1310 { 1311 struct sysctl_oid *oid; 1312 struct rm_priotracker tracker; 1313 int error; 1314 1315 SYSCTL_RLOCK(&tracker); 1316 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1317 if (error) 1318 goto out; 1319 1320 if (oid->oid_label == NULL) { 1321 error = ENOENT; 1322 goto out; 1323 } 1324 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1); 1325 out: 1326 SYSCTL_RUNLOCK(&tracker); 1327 return (error); 1328 } 1329 1330 static SYSCTL_NODE(_sysctl, 6, oidlabel, 1331 CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, ""); 1332 1333 /* 1334 * Default "handler" functions. 1335 */ 1336 1337 /* 1338 * Handle a bool. 1339 * Two cases: 1340 * a variable: point arg1 at it. 1341 * a constant: pass it in arg2. 1342 */ 1343 1344 int 1345 sysctl_handle_bool(SYSCTL_HANDLER_ARGS) 1346 { 1347 uint8_t temp; 1348 int error; 1349 1350 /* 1351 * Attempt to get a coherent snapshot by making a copy of the data. 1352 */ 1353 if (arg1) 1354 temp = *(bool *)arg1 ? 1 : 0; 1355 else 1356 temp = arg2 ? 1 : 0; 1357 1358 error = SYSCTL_OUT(req, &temp, sizeof(temp)); 1359 if (error || !req->newptr) 1360 return (error); 1361 1362 if (!arg1) 1363 error = EPERM; 1364 else { 1365 error = SYSCTL_IN(req, &temp, sizeof(temp)); 1366 if (!error) 1367 *(bool *)arg1 = temp ? 1 : 0; 1368 } 1369 return (error); 1370 } 1371 1372 /* 1373 * Handle an int8_t, signed or unsigned. 1374 * Two cases: 1375 * a variable: point arg1 at it. 1376 * a constant: pass it in arg2. 1377 */ 1378 1379 int 1380 sysctl_handle_8(SYSCTL_HANDLER_ARGS) 1381 { 1382 int8_t tmpout; 1383 int error = 0; 1384 1385 /* 1386 * Attempt to get a coherent snapshot by making a copy of the data. 1387 */ 1388 if (arg1) 1389 tmpout = *(int8_t *)arg1; 1390 else 1391 tmpout = arg2; 1392 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); 1393 1394 if (error || !req->newptr) 1395 return (error); 1396 1397 if (!arg1) 1398 error = EPERM; 1399 else 1400 error = SYSCTL_IN(req, arg1, sizeof(tmpout)); 1401 return (error); 1402 } 1403 1404 /* 1405 * Handle an int16_t, signed or unsigned. 1406 * Two cases: 1407 * a variable: point arg1 at it. 1408 * a constant: pass it in arg2. 1409 */ 1410 1411 int 1412 sysctl_handle_16(SYSCTL_HANDLER_ARGS) 1413 { 1414 int16_t tmpout; 1415 int error = 0; 1416 1417 /* 1418 * Attempt to get a coherent snapshot by making a copy of the data. 1419 */ 1420 if (arg1) 1421 tmpout = *(int16_t *)arg1; 1422 else 1423 tmpout = arg2; 1424 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); 1425 1426 if (error || !req->newptr) 1427 return (error); 1428 1429 if (!arg1) 1430 error = EPERM; 1431 else 1432 error = SYSCTL_IN(req, arg1, sizeof(tmpout)); 1433 return (error); 1434 } 1435 1436 /* 1437 * Handle an int32_t, signed or unsigned. 1438 * Two cases: 1439 * a variable: point arg1 at it. 1440 * a constant: pass it in arg2. 1441 */ 1442 1443 int 1444 sysctl_handle_32(SYSCTL_HANDLER_ARGS) 1445 { 1446 int32_t tmpout; 1447 int error = 0; 1448 1449 /* 1450 * Attempt to get a coherent snapshot by making a copy of the data. 1451 */ 1452 if (arg1) 1453 tmpout = *(int32_t *)arg1; 1454 else 1455 tmpout = arg2; 1456 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); 1457 1458 if (error || !req->newptr) 1459 return (error); 1460 1461 if (!arg1) 1462 error = EPERM; 1463 else 1464 error = SYSCTL_IN(req, arg1, sizeof(tmpout)); 1465 return (error); 1466 } 1467 1468 /* 1469 * Handle an int, signed or unsigned. 1470 * Two cases: 1471 * a variable: point arg1 at it. 1472 * a constant: pass it in arg2. 1473 */ 1474 1475 int 1476 sysctl_handle_int(SYSCTL_HANDLER_ARGS) 1477 { 1478 int tmpout, error = 0; 1479 1480 /* 1481 * Attempt to get a coherent snapshot by making a copy of the data. 1482 */ 1483 if (arg1) 1484 tmpout = *(int *)arg1; 1485 else 1486 tmpout = arg2; 1487 error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 1488 1489 if (error || !req->newptr) 1490 return (error); 1491 1492 if (!arg1) 1493 error = EPERM; 1494 else 1495 error = SYSCTL_IN(req, arg1, sizeof(int)); 1496 return (error); 1497 } 1498 1499 /* 1500 * Based on on sysctl_handle_int() convert milliseconds into ticks. 1501 * Note: this is used by TCP. 1502 */ 1503 1504 int 1505 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 1506 { 1507 int error, s, tt; 1508 1509 tt = *(int *)arg1; 1510 s = (int)((int64_t)tt * 1000 / hz); 1511 1512 error = sysctl_handle_int(oidp, &s, 0, req); 1513 if (error || !req->newptr) 1514 return (error); 1515 1516 tt = (int)((int64_t)s * hz / 1000); 1517 if (tt < 1) 1518 return (EINVAL); 1519 1520 *(int *)arg1 = tt; 1521 return (0); 1522 } 1523 1524 1525 /* 1526 * Handle a long, signed or unsigned. 1527 * Two cases: 1528 * a variable: point arg1 at it. 1529 * a constant: pass it in arg2. 1530 */ 1531 1532 int 1533 sysctl_handle_long(SYSCTL_HANDLER_ARGS) 1534 { 1535 int error = 0; 1536 long tmplong; 1537 #ifdef SCTL_MASK32 1538 int tmpint; 1539 #endif 1540 1541 /* 1542 * Attempt to get a coherent snapshot by making a copy of the data. 1543 */ 1544 if (arg1) 1545 tmplong = *(long *)arg1; 1546 else 1547 tmplong = arg2; 1548 #ifdef SCTL_MASK32 1549 if (req->flags & SCTL_MASK32) { 1550 tmpint = tmplong; 1551 error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 1552 } else 1553 #endif 1554 error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 1555 1556 if (error || !req->newptr) 1557 return (error); 1558 1559 if (!arg1) 1560 error = EPERM; 1561 #ifdef SCTL_MASK32 1562 else if (req->flags & SCTL_MASK32) { 1563 error = SYSCTL_IN(req, &tmpint, sizeof(int)); 1564 *(long *)arg1 = (long)tmpint; 1565 } 1566 #endif 1567 else 1568 error = SYSCTL_IN(req, arg1, sizeof(long)); 1569 return (error); 1570 } 1571 1572 /* 1573 * Handle a 64 bit int, signed or unsigned. 1574 * Two cases: 1575 * a variable: point arg1 at it. 1576 * a constant: pass it in arg2. 1577 */ 1578 int 1579 sysctl_handle_64(SYSCTL_HANDLER_ARGS) 1580 { 1581 int error = 0; 1582 uint64_t tmpout; 1583 1584 /* 1585 * Attempt to get a coherent snapshot by making a copy of the data. 1586 */ 1587 if (arg1) 1588 tmpout = *(uint64_t *)arg1; 1589 else 1590 tmpout = arg2; 1591 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 1592 1593 if (error || !req->newptr) 1594 return (error); 1595 1596 if (!arg1) 1597 error = EPERM; 1598 else 1599 error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 1600 return (error); 1601 } 1602 1603 /* 1604 * Handle our generic '\0' terminated 'C' string. 1605 * Two cases: 1606 * a variable string: point arg1 at it, arg2 is max length. 1607 * a constant string: point arg1 at it, arg2 is zero. 1608 */ 1609 1610 int 1611 sysctl_handle_string(SYSCTL_HANDLER_ARGS) 1612 { 1613 size_t outlen; 1614 int error = 0, ro_string = 0; 1615 1616 /* 1617 * A zero-length buffer indicates a fixed size read-only 1618 * string: 1619 */ 1620 if (arg2 == 0) { 1621 arg2 = strlen((char *)arg1) + 1; 1622 ro_string = 1; 1623 } 1624 1625 if (req->oldptr != NULL) { 1626 char *tmparg; 1627 1628 if (ro_string) { 1629 tmparg = arg1; 1630 } else { 1631 /* try to make a coherent snapshot of the string */ 1632 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); 1633 memcpy(tmparg, arg1, arg2); 1634 } 1635 1636 outlen = strnlen(tmparg, arg2 - 1) + 1; 1637 error = SYSCTL_OUT(req, tmparg, outlen); 1638 1639 if (!ro_string) 1640 free(tmparg, M_SYSCTLTMP); 1641 } else { 1642 outlen = strnlen((char *)arg1, arg2 - 1) + 1; 1643 error = SYSCTL_OUT(req, NULL, outlen); 1644 } 1645 if (error || !req->newptr) 1646 return (error); 1647 1648 if ((req->newlen - req->newidx) >= arg2) { 1649 error = EINVAL; 1650 } else { 1651 arg2 = (req->newlen - req->newidx); 1652 error = SYSCTL_IN(req, arg1, arg2); 1653 ((char *)arg1)[arg2] = '\0'; 1654 } 1655 return (error); 1656 } 1657 1658 /* 1659 * Handle any kind of opaque data. 1660 * arg1 points to it, arg2 is the size. 1661 */ 1662 1663 int 1664 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 1665 { 1666 int error, tries; 1667 u_int generation; 1668 struct sysctl_req req2; 1669 1670 /* 1671 * Attempt to get a coherent snapshot, by using the thread 1672 * pre-emption counter updated from within mi_switch() to 1673 * determine if we were pre-empted during a bcopy() or 1674 * copyout(). Make 3 attempts at doing this before giving up. 1675 * If we encounter an error, stop immediately. 1676 */ 1677 tries = 0; 1678 req2 = *req; 1679 retry: 1680 generation = curthread->td_generation; 1681 error = SYSCTL_OUT(req, arg1, arg2); 1682 if (error) 1683 return (error); 1684 tries++; 1685 if (generation != curthread->td_generation && tries < 3) { 1686 *req = req2; 1687 goto retry; 1688 } 1689 1690 error = SYSCTL_IN(req, arg1, arg2); 1691 1692 return (error); 1693 } 1694 1695 /* 1696 * Transfer functions to/from kernel space. 1697 * XXX: rather untested at this point 1698 */ 1699 static int 1700 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 1701 { 1702 size_t i = 0; 1703 1704 if (req->oldptr) { 1705 i = l; 1706 if (req->oldlen <= req->oldidx) 1707 i = 0; 1708 else 1709 if (i > req->oldlen - req->oldidx) 1710 i = req->oldlen - req->oldidx; 1711 if (i > 0) 1712 bcopy(p, (char *)req->oldptr + req->oldidx, i); 1713 } 1714 req->oldidx += l; 1715 if (req->oldptr && i != l) 1716 return (ENOMEM); 1717 return (0); 1718 } 1719 1720 static int 1721 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 1722 { 1723 if (!req->newptr) 1724 return (0); 1725 if (req->newlen - req->newidx < l) 1726 return (EINVAL); 1727 bcopy((char *)req->newptr + req->newidx, p, l); 1728 req->newidx += l; 1729 return (0); 1730 } 1731 1732 int 1733 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1734 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 1735 { 1736 int error = 0; 1737 struct sysctl_req req; 1738 1739 bzero(&req, sizeof req); 1740 1741 req.td = td; 1742 req.flags = flags; 1743 1744 if (oldlenp) { 1745 req.oldlen = *oldlenp; 1746 } 1747 req.validlen = req.oldlen; 1748 1749 if (old) { 1750 req.oldptr= old; 1751 } 1752 1753 if (new != NULL) { 1754 req.newlen = newlen; 1755 req.newptr = new; 1756 } 1757 1758 req.oldfunc = sysctl_old_kernel; 1759 req.newfunc = sysctl_new_kernel; 1760 req.lock = REQ_UNWIRED; 1761 1762 error = sysctl_root(0, name, namelen, &req); 1763 1764 if (req.lock == REQ_WIRED && req.validlen > 0) 1765 vsunlock(req.oldptr, req.validlen); 1766 1767 if (error && error != ENOMEM) 1768 return (error); 1769 1770 if (retval) { 1771 if (req.oldptr && req.oldidx > req.validlen) 1772 *retval = req.validlen; 1773 else 1774 *retval = req.oldidx; 1775 } 1776 return (error); 1777 } 1778 1779 int 1780 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1781 void *new, size_t newlen, size_t *retval, int flags) 1782 { 1783 int oid[CTL_MAXNAME]; 1784 size_t oidlen, plen; 1785 int error; 1786 1787 oid[0] = 0; /* sysctl internal magic */ 1788 oid[1] = 3; /* name2oid */ 1789 oidlen = sizeof(oid); 1790 1791 error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1792 (void *)name, strlen(name), &plen, flags); 1793 if (error) 1794 return (error); 1795 1796 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1797 new, newlen, retval, flags); 1798 return (error); 1799 } 1800 1801 /* 1802 * Transfer function to/from user space. 1803 */ 1804 static int 1805 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 1806 { 1807 size_t i, len, origidx; 1808 int error; 1809 1810 origidx = req->oldidx; 1811 req->oldidx += l; 1812 if (req->oldptr == NULL) 1813 return (0); 1814 /* 1815 * If we have not wired the user supplied buffer and we are currently 1816 * holding locks, drop a witness warning, as it's possible that 1817 * write operations to the user page can sleep. 1818 */ 1819 if (req->lock != REQ_WIRED) 1820 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1821 "sysctl_old_user()"); 1822 i = l; 1823 len = req->validlen; 1824 if (len <= origidx) 1825 i = 0; 1826 else { 1827 if (i > len - origidx) 1828 i = len - origidx; 1829 if (req->lock == REQ_WIRED) { 1830 error = copyout_nofault(p, (char *)req->oldptr + 1831 origidx, i); 1832 } else 1833 error = copyout(p, (char *)req->oldptr + origidx, i); 1834 if (error != 0) 1835 return (error); 1836 } 1837 if (i < l) 1838 return (ENOMEM); 1839 return (0); 1840 } 1841 1842 static int 1843 sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 1844 { 1845 int error; 1846 1847 if (!req->newptr) 1848 return (0); 1849 if (req->newlen - req->newidx < l) 1850 return (EINVAL); 1851 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1852 "sysctl_new_user()"); 1853 error = copyin((char *)req->newptr + req->newidx, p, l); 1854 req->newidx += l; 1855 return (error); 1856 } 1857 1858 /* 1859 * Wire the user space destination buffer. If set to a value greater than 1860 * zero, the len parameter limits the maximum amount of wired memory. 1861 */ 1862 int 1863 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1864 { 1865 int ret; 1866 size_t wiredlen; 1867 1868 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1869 ret = 0; 1870 if (req->lock != REQ_WIRED && req->oldptr && 1871 req->oldfunc == sysctl_old_user) { 1872 if (wiredlen != 0) { 1873 ret = vslock(req->oldptr, wiredlen); 1874 if (ret != 0) { 1875 if (ret != ENOMEM) 1876 return (ret); 1877 wiredlen = 0; 1878 } 1879 } 1880 req->lock = REQ_WIRED; 1881 req->validlen = wiredlen; 1882 } 1883 return (0); 1884 } 1885 1886 int 1887 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 1888 int *nindx, struct sysctl_req *req) 1889 { 1890 struct sysctl_oid_list *lsp; 1891 struct sysctl_oid *oid; 1892 int indx; 1893 1894 SYSCTL_ASSERT_LOCKED(); 1895 lsp = &sysctl__children; 1896 indx = 0; 1897 while (indx < CTL_MAXNAME) { 1898 SLIST_FOREACH(oid, lsp, oid_link) { 1899 if (oid->oid_number == name[indx]) 1900 break; 1901 } 1902 if (oid == NULL) 1903 return (ENOENT); 1904 1905 indx++; 1906 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1907 if (oid->oid_handler != NULL || indx == namelen) { 1908 *noid = oid; 1909 if (nindx != NULL) 1910 *nindx = indx; 1911 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1912 ("%s found DYING node %p", __func__, oid)); 1913 return (0); 1914 } 1915 lsp = SYSCTL_CHILDREN(oid); 1916 } else if (indx == namelen) { 1917 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0) 1918 return (ENOENT); 1919 *noid = oid; 1920 if (nindx != NULL) 1921 *nindx = indx; 1922 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1923 ("%s found DYING node %p", __func__, oid)); 1924 return (0); 1925 } else { 1926 return (ENOTDIR); 1927 } 1928 } 1929 return (ENOENT); 1930 } 1931 1932 /* 1933 * Traverse our tree, and find the right node, execute whatever it points 1934 * to, and return the resulting error code. 1935 */ 1936 1937 static int 1938 sysctl_root(SYSCTL_HANDLER_ARGS) 1939 { 1940 struct sysctl_oid *oid; 1941 struct rm_priotracker tracker; 1942 int error, indx, lvl; 1943 1944 SYSCTL_RLOCK(&tracker); 1945 1946 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1947 if (error) 1948 goto out; 1949 1950 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1951 /* 1952 * You can't call a sysctl when it's a node, but has 1953 * no handler. Inform the user that it's a node. 1954 * The indx may or may not be the same as namelen. 1955 */ 1956 if (oid->oid_handler == NULL) { 1957 error = EISDIR; 1958 goto out; 1959 } 1960 } 1961 1962 /* Is this sysctl writable? */ 1963 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) { 1964 error = EPERM; 1965 goto out; 1966 } 1967 1968 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 1969 1970 #ifdef CAPABILITY_MODE 1971 /* 1972 * If the process is in capability mode, then don't permit reading or 1973 * writing unless specifically granted for the node. 1974 */ 1975 if (IN_CAPABILITY_MODE(req->td)) { 1976 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) || 1977 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) { 1978 error = EPERM; 1979 goto out; 1980 } 1981 } 1982 #endif 1983 1984 /* Is this sysctl sensitive to securelevels? */ 1985 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1986 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1987 error = securelevel_gt(req->td->td_ucred, lvl); 1988 if (error) 1989 goto out; 1990 } 1991 1992 /* Is this sysctl writable by only privileged users? */ 1993 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1994 int priv; 1995 1996 if (oid->oid_kind & CTLFLAG_PRISON) 1997 priv = PRIV_SYSCTL_WRITEJAIL; 1998 #ifdef VIMAGE 1999 else if ((oid->oid_kind & CTLFLAG_VNET) && 2000 prison_owns_vnet(req->td->td_ucred)) 2001 priv = PRIV_SYSCTL_WRITEJAIL; 2002 #endif 2003 else 2004 priv = PRIV_SYSCTL_WRITE; 2005 error = priv_check(req->td, priv); 2006 if (error) 2007 goto out; 2008 } 2009 2010 if (!oid->oid_handler) { 2011 error = EINVAL; 2012 goto out; 2013 } 2014 2015 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 2016 arg1 = (int *)arg1 + indx; 2017 arg2 -= indx; 2018 } else { 2019 arg1 = oid->oid_arg1; 2020 arg2 = oid->oid_arg2; 2021 } 2022 #ifdef MAC 2023 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 2024 req); 2025 if (error != 0) 2026 goto out; 2027 #endif 2028 #ifdef VIMAGE 2029 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL) 2030 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1); 2031 #endif 2032 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker); 2033 2034 out: 2035 SYSCTL_RUNLOCK(&tracker); 2036 return (error); 2037 } 2038 2039 #ifndef _SYS_SYSPROTO_H_ 2040 struct sysctl_args { 2041 int *name; 2042 u_int namelen; 2043 void *old; 2044 size_t *oldlenp; 2045 void *new; 2046 size_t newlen; 2047 }; 2048 #endif 2049 int 2050 sys___sysctl(struct thread *td, struct sysctl_args *uap) 2051 { 2052 int error, i, name[CTL_MAXNAME]; 2053 size_t j; 2054 2055 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 2056 return (EINVAL); 2057 2058 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 2059 if (error) 2060 return (error); 2061 2062 error = userland_sysctl(td, name, uap->namelen, 2063 uap->old, uap->oldlenp, 0, 2064 uap->new, uap->newlen, &j, 0); 2065 if (error && error != ENOMEM) 2066 return (error); 2067 if (uap->oldlenp) { 2068 i = copyout(&j, uap->oldlenp, sizeof(j)); 2069 if (i) 2070 return (i); 2071 } 2072 return (error); 2073 } 2074 2075 /* 2076 * This is used from various compatibility syscalls too. That's why name 2077 * must be in kernel space. 2078 */ 2079 int 2080 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 2081 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 2082 int flags) 2083 { 2084 int error = 0, memlocked; 2085 struct sysctl_req req; 2086 2087 bzero(&req, sizeof req); 2088 2089 req.td = td; 2090 req.flags = flags; 2091 2092 if (oldlenp) { 2093 if (inkernel) { 2094 req.oldlen = *oldlenp; 2095 } else { 2096 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 2097 if (error) 2098 return (error); 2099 } 2100 } 2101 req.validlen = req.oldlen; 2102 req.oldptr = old; 2103 2104 if (new != NULL) { 2105 req.newlen = newlen; 2106 req.newptr = new; 2107 } 2108 2109 req.oldfunc = sysctl_old_user; 2110 req.newfunc = sysctl_new_user; 2111 req.lock = REQ_UNWIRED; 2112 2113 #ifdef KTRACE 2114 if (KTRPOINT(curthread, KTR_SYSCTL)) 2115 ktrsysctl(name, namelen); 2116 #endif 2117 memlocked = 0; 2118 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) { 2119 memlocked = 1; 2120 sx_xlock(&sysctlmemlock); 2121 } 2122 CURVNET_SET(TD_TO_VNET(td)); 2123 2124 for (;;) { 2125 req.oldidx = 0; 2126 req.newidx = 0; 2127 error = sysctl_root(0, name, namelen, &req); 2128 if (error != EAGAIN) 2129 break; 2130 kern_yield(PRI_USER); 2131 } 2132 2133 CURVNET_RESTORE(); 2134 2135 if (req.lock == REQ_WIRED && req.validlen > 0) 2136 vsunlock(req.oldptr, req.validlen); 2137 if (memlocked) 2138 sx_xunlock(&sysctlmemlock); 2139 2140 if (error && error != ENOMEM) 2141 return (error); 2142 2143 if (retval) { 2144 if (req.oldptr && req.oldidx > req.validlen) 2145 *retval = req.validlen; 2146 else 2147 *retval = req.oldidx; 2148 } 2149 return (error); 2150 } 2151 2152 /* 2153 * Drain into a sysctl struct. The user buffer should be wired if a page 2154 * fault would cause issue. 2155 */ 2156 static int 2157 sbuf_sysctl_drain(void *arg, const char *data, int len) 2158 { 2159 struct sysctl_req *req = arg; 2160 int error; 2161 2162 error = SYSCTL_OUT(req, data, len); 2163 KASSERT(error >= 0, ("Got unexpected negative value %d", error)); 2164 return (error == 0 ? len : -error); 2165 } 2166 2167 struct sbuf * 2168 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, 2169 struct sysctl_req *req) 2170 { 2171 2172 /* Supply a default buffer size if none given. */ 2173 if (buf == NULL && length == 0) 2174 length = 64; 2175 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL); 2176 sbuf_set_drain(s, sbuf_sysctl_drain, req); 2177 return (s); 2178 } 2179