1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Mike Karels at Berkeley Software Design, Inc. 9 * 10 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD 11 * project, to make these variables more userfriendly. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include "opt_capsicum.h" 44 #include "opt_ddb.h" 45 #include "opt_ktrace.h" 46 #include "opt_sysctl.h" 47 48 #include <sys/param.h> 49 #include <sys/fail.h> 50 #include <sys/systm.h> 51 #include <sys/capsicum.h> 52 #include <sys/kernel.h> 53 #include <sys/limits.h> 54 #include <sys/sysctl.h> 55 #include <sys/malloc.h> 56 #include <sys/priv.h> 57 #include <sys/proc.h> 58 #include <sys/jail.h> 59 #include <sys/kdb.h> 60 #include <sys/lock.h> 61 #include <sys/mutex.h> 62 #include <sys/rmlock.h> 63 #include <sys/sbuf.h> 64 #include <sys/sx.h> 65 #include <sys/sysproto.h> 66 #include <sys/uio.h> 67 #ifdef KTRACE 68 #include <sys/ktrace.h> 69 #endif 70 71 #ifdef DDB 72 #include <ddb/ddb.h> 73 #include <ddb/db_lex.h> 74 #endif 75 76 #include <net/vnet.h> 77 78 #include <security/mac/mac_framework.h> 79 80 #include <vm/vm.h> 81 #include <vm/vm_extern.h> 82 83 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 84 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 85 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 86 87 RB_GENERATE(sysctl_oid_list, sysctl_oid, oid_link, cmp_sysctl_oid); 88 89 /* 90 * The sysctllock protects the MIB tree. It also protects sysctl 91 * contexts used with dynamic sysctls. The sysctl_register_oid() and 92 * sysctl_unregister_oid() routines require the sysctllock to already 93 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are 94 * provided for the few places in the kernel which need to use that 95 * API rather than using the dynamic API. Use of the dynamic API is 96 * strongly encouraged for most code. 97 * 98 * The sysctlmemlock is used to limit the amount of user memory wired for 99 * sysctl requests. This is implemented by serializing any userland 100 * sysctl requests larger than a single page via an exclusive lock. 101 * 102 * The sysctlstringlock is used to protect concurrent access to writable 103 * string nodes in sysctl_handle_string(). 104 */ 105 static struct rmlock sysctllock; 106 static struct sx __exclusive_cache_line sysctlmemlock; 107 static struct sx sysctlstringlock; 108 109 #define SYSCTL_WLOCK() rm_wlock(&sysctllock) 110 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock) 111 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker)) 112 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker)) 113 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock) 114 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED) 115 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED) 116 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED) 117 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \ 118 RM_SLEEPABLE) 119 #define SYSCTL_SLEEP(ch, wmesg, timo) \ 120 rm_sleep(ch, &sysctllock, 0, wmesg, timo) 121 122 static int sysctl_root(SYSCTL_HANDLER_ARGS); 123 124 /* Root list */ 125 struct sysctl_oid_list sysctl__children = RB_INITIALIZER(&sysctl__children); 126 127 static char* sysctl_escape_name(const char*); 128 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 129 int recurse); 130 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t); 131 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); 132 133 static struct sysctl_oid * 134 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 135 { 136 struct sysctl_oid *oidp; 137 138 SYSCTL_ASSERT_LOCKED(); 139 SYSCTL_FOREACH(oidp, list) { 140 if (strcmp(oidp->oid_name, name) == 0) { 141 return (oidp); 142 } 143 } 144 return (NULL); 145 } 146 147 /* 148 * Initialization of the MIB tree. 149 * 150 * Order by number in each list. 151 */ 152 void 153 sysctl_wlock(void) 154 { 155 156 SYSCTL_WLOCK(); 157 } 158 159 void 160 sysctl_wunlock(void) 161 { 162 163 SYSCTL_WUNLOCK(); 164 } 165 166 static int 167 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2, 168 struct sysctl_req *req, struct rm_priotracker *tracker) 169 { 170 int error; 171 172 if (oid->oid_kind & CTLFLAG_DYN) 173 atomic_add_int(&oid->oid_running, 1); 174 175 if (tracker != NULL) 176 SYSCTL_RUNLOCK(tracker); 177 else 178 SYSCTL_WUNLOCK(); 179 180 /* 181 * Treat set CTLFLAG_NEEDGIANT and unset CTLFLAG_MPSAFE flags the same, 182 * untill we're ready to remove all traces of Giant from sysctl(9). 183 */ 184 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) || 185 (!(oid->oid_kind & CTLFLAG_MPSAFE))) 186 mtx_lock(&Giant); 187 error = oid->oid_handler(oid, arg1, arg2, req); 188 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) || 189 (!(oid->oid_kind & CTLFLAG_MPSAFE))) 190 mtx_unlock(&Giant); 191 192 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error); 193 194 if (tracker != NULL) 195 SYSCTL_RLOCK(tracker); 196 else 197 SYSCTL_WLOCK(); 198 199 if (oid->oid_kind & CTLFLAG_DYN) { 200 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 && 201 (oid->oid_kind & CTLFLAG_DYING) != 0) 202 wakeup(&oid->oid_running); 203 } 204 205 return (error); 206 } 207 208 static void 209 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp) 210 { 211 struct sysctl_req req; 212 struct sysctl_oid *curr; 213 char *penv = NULL; 214 char path[96]; 215 ssize_t rem = sizeof(path); 216 ssize_t len; 217 uint8_t data[512] __aligned(sizeof(uint64_t)); 218 int size; 219 int error; 220 221 path[--rem] = 0; 222 223 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) { 224 len = strlen(curr->oid_name); 225 rem -= len; 226 if (curr != oidp) 227 rem -= 1; 228 if (rem < 0) { 229 printf("OID path exceeds %d bytes\n", (int)sizeof(path)); 230 return; 231 } 232 memcpy(path + rem, curr->oid_name, len); 233 if (curr != oidp) 234 path[rem + len] = '.'; 235 } 236 237 memset(&req, 0, sizeof(req)); 238 239 req.td = curthread; 240 req.oldfunc = sysctl_old_kernel; 241 req.newfunc = sysctl_new_kernel; 242 req.lock = REQ_UNWIRED; 243 244 switch (oidp->oid_kind & CTLTYPE) { 245 case CTLTYPE_INT: 246 if (getenv_array(path + rem, data, sizeof(data), &size, 247 sizeof(int), GETENV_SIGNED) == 0) 248 return; 249 req.newlen = size; 250 req.newptr = data; 251 break; 252 case CTLTYPE_UINT: 253 if (getenv_array(path + rem, data, sizeof(data), &size, 254 sizeof(int), GETENV_UNSIGNED) == 0) 255 return; 256 req.newlen = size; 257 req.newptr = data; 258 break; 259 case CTLTYPE_LONG: 260 if (getenv_array(path + rem, data, sizeof(data), &size, 261 sizeof(long), GETENV_SIGNED) == 0) 262 return; 263 req.newlen = size; 264 req.newptr = data; 265 break; 266 case CTLTYPE_ULONG: 267 if (getenv_array(path + rem, data, sizeof(data), &size, 268 sizeof(long), GETENV_UNSIGNED) == 0) 269 return; 270 req.newlen = size; 271 req.newptr = data; 272 break; 273 case CTLTYPE_S8: 274 if (getenv_array(path + rem, data, sizeof(data), &size, 275 sizeof(int8_t), GETENV_SIGNED) == 0) 276 return; 277 req.newlen = size; 278 req.newptr = data; 279 break; 280 case CTLTYPE_S16: 281 if (getenv_array(path + rem, data, sizeof(data), &size, 282 sizeof(int16_t), GETENV_SIGNED) == 0) 283 return; 284 req.newlen = size; 285 req.newptr = data; 286 break; 287 case CTLTYPE_S32: 288 if (getenv_array(path + rem, data, sizeof(data), &size, 289 sizeof(int32_t), GETENV_SIGNED) == 0) 290 return; 291 req.newlen = size; 292 req.newptr = data; 293 break; 294 case CTLTYPE_S64: 295 if (getenv_array(path + rem, data, sizeof(data), &size, 296 sizeof(int64_t), GETENV_SIGNED) == 0) 297 return; 298 req.newlen = size; 299 req.newptr = data; 300 break; 301 case CTLTYPE_U8: 302 if (getenv_array(path + rem, data, sizeof(data), &size, 303 sizeof(uint8_t), GETENV_UNSIGNED) == 0) 304 return; 305 req.newlen = size; 306 req.newptr = data; 307 break; 308 case CTLTYPE_U16: 309 if (getenv_array(path + rem, data, sizeof(data), &size, 310 sizeof(uint16_t), GETENV_UNSIGNED) == 0) 311 return; 312 req.newlen = size; 313 req.newptr = data; 314 break; 315 case CTLTYPE_U32: 316 if (getenv_array(path + rem, data, sizeof(data), &size, 317 sizeof(uint32_t), GETENV_UNSIGNED) == 0) 318 return; 319 req.newlen = size; 320 req.newptr = data; 321 break; 322 case CTLTYPE_U64: 323 if (getenv_array(path + rem, data, sizeof(data), &size, 324 sizeof(uint64_t), GETENV_UNSIGNED) == 0) 325 return; 326 req.newlen = size; 327 req.newptr = data; 328 break; 329 case CTLTYPE_STRING: 330 penv = kern_getenv(path + rem); 331 if (penv == NULL) 332 return; 333 req.newlen = strlen(penv); 334 req.newptr = penv; 335 break; 336 default: 337 return; 338 } 339 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1, 340 oidp->oid_arg2, &req, NULL); 341 if (error != 0) 342 printf("Setting sysctl %s failed: %d\n", path + rem, error); 343 if (penv != NULL) 344 freeenv(penv); 345 } 346 347 /* 348 * Locate the path to a given oid. Returns the length of the resulting path, 349 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME 350 * elements and be NULL initialized. 351 */ 352 static int 353 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle) 354 { 355 int indx; 356 357 SYSCTL_ASSERT_LOCKED(); 358 indx = 0; 359 /* 360 * Do a depth-first search of the oid tree, looking for 'needle'. Start 361 * with the first child of the root. 362 */ 363 nodes[indx] = RB_MIN(sysctl_oid_list, &sysctl__children); 364 for (;;) { 365 if (nodes[indx] == needle) 366 return (indx + 1); 367 368 if (nodes[indx] == NULL) { 369 /* Node has no more siblings, so back up to parent. */ 370 if (indx-- == 0) { 371 /* Retreat to root, so give up. */ 372 break; 373 } 374 } else if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 375 /* Node has children. */ 376 if (++indx == CTL_MAXNAME) { 377 /* Max search depth reached, so give up. */ 378 break; 379 } 380 /* Start with the first child. */ 381 nodes[indx] = RB_MIN(sysctl_oid_list, 382 &nodes[indx - 1]->oid_children); 383 continue; 384 } 385 /* Consider next sibling. */ 386 nodes[indx] = RB_NEXT(sysctl_oid_list, NULL, nodes[indx]); 387 } 388 return (-1); 389 } 390 391 static void 392 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf) 393 { 394 struct sysctl_oid *nodes[CTL_MAXNAME]; 395 char buf[128]; 396 struct sbuf sb; 397 int rc, i; 398 399 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL); 400 sbuf_set_drain(&sb, sbuf_printf_drain, NULL); 401 402 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__); 403 404 memset(nodes, 0, sizeof(nodes)); 405 rc = sysctl_search_oid(nodes, leaf); 406 if (rc > 0) { 407 for (i = 0; i < rc; i++) 408 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name, 409 i != (rc - 1), "."); 410 } else { 411 sbuf_printf(&sb, "%s", leaf->oid_name); 412 } 413 sbuf_printf(&sb, ")!\n"); 414 415 (void)sbuf_finish(&sb); 416 } 417 418 #ifdef SYSCTL_DEBUG 419 static int 420 sysctl_reuse_test(SYSCTL_HANDLER_ARGS) 421 { 422 struct rm_priotracker tracker; 423 424 SYSCTL_RLOCK(&tracker); 425 sysctl_warn_reuse(__func__, oidp); 426 SYSCTL_RUNLOCK(&tracker); 427 return (0); 428 } 429 SYSCTL_PROC(_sysctl, OID_AUTO, reuse_test, 430 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-", 431 ""); 432 #endif 433 434 void 435 sysctl_register_oid(struct sysctl_oid *oidp) 436 { 437 struct sysctl_oid_list *parent = oidp->oid_parent; 438 struct sysctl_oid *p, key; 439 int oid_number; 440 int timeout = 2; 441 442 /* 443 * First check if another oid with the same name already 444 * exists in the parent's list. 445 */ 446 SYSCTL_ASSERT_WLOCKED(); 447 p = sysctl_find_oidname(oidp->oid_name, parent); 448 if (p != NULL) { 449 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 450 p->oid_refcnt++; 451 return; 452 } else { 453 sysctl_warn_reuse(__func__, p); 454 return; 455 } 456 } 457 /* get current OID number */ 458 oid_number = oidp->oid_number; 459 460 #if (OID_AUTO >= 0) 461 #error "OID_AUTO is expected to be a negative value" 462 #endif 463 /* 464 * Any negative OID number qualifies as OID_AUTO. Valid OID 465 * numbers should always be positive. 466 * 467 * NOTE: DO NOT change the starting value here, change it in 468 * <sys/sysctl.h>, and make sure it is at least 256 to 469 * accommodate e.g. net.inet.raw as a static sysctl node. 470 */ 471 if (oid_number < 0) { 472 static int newoid; 473 474 /* 475 * By decrementing the next OID number we spend less 476 * time inserting the OIDs into a sorted list. 477 */ 478 if (--newoid < CTL_AUTO_START) 479 newoid = 0x7fffffff; 480 481 oid_number = newoid; 482 } 483 484 /* 485 * Insert the OID into the parent's list sorted by OID number. 486 */ 487 key.oid_number = oid_number; 488 p = RB_NFIND(sysctl_oid_list, parent, &key); 489 while (p != NULL && oid_number == p->oid_number) { 490 /* get the next valid OID number */ 491 if (oid_number < CTL_AUTO_START || 492 oid_number == 0x7fffffff) { 493 /* wraparound - restart */ 494 oid_number = CTL_AUTO_START; 495 /* don't loop forever */ 496 if (!timeout--) 497 panic("sysctl: Out of OID numbers\n"); 498 key.oid_number = oid_number; 499 p = RB_NFIND(sysctl_oid_list, parent, &key); 500 continue; 501 } 502 p = RB_NEXT(sysctl_oid_list, NULL, p); 503 oid_number++; 504 } 505 /* check for non-auto OID number collision */ 506 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START && 507 oid_number >= CTL_AUTO_START) { 508 printf("sysctl: OID number(%d) is already in use for '%s'\n", 509 oidp->oid_number, oidp->oid_name); 510 } 511 /* update the OID number, if any */ 512 oidp->oid_number = oid_number; 513 RB_INSERT(sysctl_oid_list, parent, oidp); 514 515 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE && 516 #ifdef VIMAGE 517 (oidp->oid_kind & CTLFLAG_VNET) == 0 && 518 #endif 519 (oidp->oid_kind & CTLFLAG_TUN) != 0 && 520 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) { 521 /* only fetch value once */ 522 oidp->oid_kind |= CTLFLAG_NOFETCH; 523 /* try to fetch value from kernel environment */ 524 sysctl_load_tunable_by_oid_locked(oidp); 525 } 526 } 527 528 void 529 sysctl_register_disabled_oid(struct sysctl_oid *oidp) 530 { 531 532 /* 533 * Mark the leaf as dormant if it's not to be immediately enabled. 534 * We do not disable nodes as they can be shared between modules 535 * and it is always safe to access a node. 536 */ 537 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0, 538 ("internal flag is set in oid_kind")); 539 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 540 oidp->oid_kind |= CTLFLAG_DORMANT; 541 sysctl_register_oid(oidp); 542 } 543 544 void 545 sysctl_enable_oid(struct sysctl_oid *oidp) 546 { 547 548 SYSCTL_ASSERT_WLOCKED(); 549 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 550 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0, 551 ("sysctl node is marked as dormant")); 552 return; 553 } 554 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0, 555 ("enabling already enabled sysctl oid")); 556 oidp->oid_kind &= ~CTLFLAG_DORMANT; 557 } 558 559 void 560 sysctl_unregister_oid(struct sysctl_oid *oidp) 561 { 562 int error; 563 564 SYSCTL_ASSERT_WLOCKED(); 565 if (oidp->oid_number == OID_AUTO) { 566 error = EINVAL; 567 } else { 568 error = ENOENT; 569 if (RB_REMOVE(sysctl_oid_list, oidp->oid_parent, oidp)) 570 error = 0; 571 } 572 573 /* 574 * This can happen when a module fails to register and is 575 * being unloaded afterwards. It should not be a panic() 576 * for normal use. 577 */ 578 if (error) { 579 printf("%s: failed(%d) to unregister sysctl(%s)\n", 580 __func__, error, oidp->oid_name); 581 } 582 } 583 584 /* Initialize a new context to keep track of dynamically added sysctls. */ 585 int 586 sysctl_ctx_init(struct sysctl_ctx_list *c) 587 { 588 589 if (c == NULL) { 590 return (EINVAL); 591 } 592 593 /* 594 * No locking here, the caller is responsible for not adding 595 * new nodes to a context until after this function has 596 * returned. 597 */ 598 TAILQ_INIT(c); 599 return (0); 600 } 601 602 /* Free the context, and destroy all dynamic oids registered in this context */ 603 int 604 sysctl_ctx_free(struct sysctl_ctx_list *clist) 605 { 606 struct sysctl_ctx_entry *e, *e1; 607 int error; 608 609 error = 0; 610 /* 611 * First perform a "dry run" to check if it's ok to remove oids. 612 * XXX FIXME 613 * XXX This algorithm is a hack. But I don't know any 614 * XXX better solution for now... 615 */ 616 SYSCTL_WLOCK(); 617 TAILQ_FOREACH(e, clist, link) { 618 error = sysctl_remove_oid_locked(e->entry, 0, 0); 619 if (error) 620 break; 621 } 622 /* 623 * Restore deregistered entries, either from the end, 624 * or from the place where error occurred. 625 * e contains the entry that was not unregistered 626 */ 627 if (error) 628 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 629 else 630 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 631 while (e1 != NULL) { 632 sysctl_register_oid(e1->entry); 633 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 634 } 635 if (error) { 636 SYSCTL_WUNLOCK(); 637 return(EBUSY); 638 } 639 /* Now really delete the entries */ 640 e = TAILQ_FIRST(clist); 641 while (e != NULL) { 642 e1 = TAILQ_NEXT(e, link); 643 error = sysctl_remove_oid_locked(e->entry, 1, 0); 644 if (error) 645 panic("sysctl_remove_oid: corrupt tree, entry: %s", 646 e->entry->oid_name); 647 free(e, M_SYSCTLOID); 648 e = e1; 649 } 650 SYSCTL_WUNLOCK(); 651 return (error); 652 } 653 654 /* Add an entry to the context */ 655 struct sysctl_ctx_entry * 656 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 657 { 658 struct sysctl_ctx_entry *e; 659 660 SYSCTL_ASSERT_WLOCKED(); 661 if (clist == NULL || oidp == NULL) 662 return(NULL); 663 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 664 e->entry = oidp; 665 TAILQ_INSERT_HEAD(clist, e, link); 666 return (e); 667 } 668 669 /* Find an entry in the context */ 670 struct sysctl_ctx_entry * 671 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 672 { 673 struct sysctl_ctx_entry *e; 674 675 SYSCTL_ASSERT_WLOCKED(); 676 if (clist == NULL || oidp == NULL) 677 return(NULL); 678 TAILQ_FOREACH(e, clist, link) { 679 if (e->entry == oidp) 680 return(e); 681 } 682 return (e); 683 } 684 685 /* 686 * Delete an entry from the context. 687 * NOTE: this function doesn't free oidp! You have to remove it 688 * with sysctl_remove_oid(). 689 */ 690 int 691 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 692 { 693 struct sysctl_ctx_entry *e; 694 695 if (clist == NULL || oidp == NULL) 696 return (EINVAL); 697 SYSCTL_WLOCK(); 698 e = sysctl_ctx_entry_find(clist, oidp); 699 if (e != NULL) { 700 TAILQ_REMOVE(clist, e, link); 701 SYSCTL_WUNLOCK(); 702 free(e, M_SYSCTLOID); 703 return (0); 704 } else { 705 SYSCTL_WUNLOCK(); 706 return (ENOENT); 707 } 708 } 709 710 /* 711 * Remove dynamically created sysctl trees. 712 * oidp - top of the tree to be removed 713 * del - if 0 - just deregister, otherwise free up entries as well 714 * recurse - if != 0 traverse the subtree to be deleted 715 */ 716 int 717 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 718 { 719 int error; 720 721 SYSCTL_WLOCK(); 722 error = sysctl_remove_oid_locked(oidp, del, recurse); 723 SYSCTL_WUNLOCK(); 724 return (error); 725 } 726 727 int 728 sysctl_remove_name(struct sysctl_oid *parent, const char *name, 729 int del, int recurse) 730 { 731 struct sysctl_oid *p; 732 int error; 733 734 error = ENOENT; 735 SYSCTL_WLOCK(); 736 p = sysctl_find_oidname(name, &parent->oid_children); 737 if (p) 738 error = sysctl_remove_oid_locked(p, del, recurse); 739 SYSCTL_WUNLOCK(); 740 741 return (error); 742 } 743 744 /* 745 * Duplicate the provided string, escaping any illegal characters. The result 746 * must be freed when no longer in use. 747 * 748 * The list of illegal characters is ".". 749 */ 750 static char* 751 sysctl_escape_name(const char* orig) 752 { 753 int i, s = 0, d = 0, nillegals = 0; 754 char *new; 755 756 /* First count the number of illegal characters */ 757 for (i = 0; orig[i] != '\0'; i++) { 758 if (orig[i] == '.') 759 nillegals++; 760 } 761 762 /* Allocate storage for new string */ 763 new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK); 764 765 /* Copy the name, escaping characters as we go */ 766 while (orig[s] != '\0') { 767 if (orig[s] == '.') { 768 /* %25 is the hexadecimal representation of '.' */ 769 new[d++] = '%'; 770 new[d++] = '2'; 771 new[d++] = '5'; 772 s++; 773 } else { 774 new[d++] = orig[s++]; 775 } 776 } 777 778 /* Finally, nul-terminate */ 779 new[d] = '\0'; 780 781 return (new); 782 } 783 784 static int 785 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 786 { 787 struct sysctl_oid *p, *tmp; 788 int error; 789 790 SYSCTL_ASSERT_WLOCKED(); 791 if (oidp == NULL) 792 return(EINVAL); 793 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 794 printf("Warning: can't remove non-dynamic nodes (%s)!\n", 795 oidp->oid_name); 796 return (EINVAL); 797 } 798 /* 799 * WARNING: normal method to do this should be through 800 * sysctl_ctx_free(). Use recursing as the last resort 801 * method to purge your sysctl tree of leftovers... 802 * However, if some other code still references these nodes, 803 * it will panic. 804 */ 805 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 806 if (oidp->oid_refcnt == 1) { 807 for(p = RB_MIN(sysctl_oid_list, &oidp->oid_children); 808 p != NULL; p = tmp) { 809 if (!recurse) { 810 printf("Warning: failed attempt to " 811 "remove oid %s with child %s\n", 812 oidp->oid_name, p->oid_name); 813 return (ENOTEMPTY); 814 } 815 tmp = RB_NEXT(sysctl_oid_list, 816 &oidp->oid_children, p); 817 error = sysctl_remove_oid_locked(p, del, 818 recurse); 819 if (error) 820 return (error); 821 } 822 } 823 } 824 if (oidp->oid_refcnt > 1 ) { 825 oidp->oid_refcnt--; 826 } else { 827 if (oidp->oid_refcnt == 0) { 828 printf("Warning: bad oid_refcnt=%u (%s)!\n", 829 oidp->oid_refcnt, oidp->oid_name); 830 return (EINVAL); 831 } 832 sysctl_unregister_oid(oidp); 833 if (del) { 834 /* 835 * Wait for all threads running the handler to drain. 836 * This preserves the previous behavior when the 837 * sysctl lock was held across a handler invocation, 838 * and is necessary for module unload correctness. 839 */ 840 while (oidp->oid_running > 0) { 841 oidp->oid_kind |= CTLFLAG_DYING; 842 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0); 843 } 844 if (oidp->oid_descr) 845 free(__DECONST(char *, oidp->oid_descr), 846 M_SYSCTLOID); 847 if (oidp->oid_label) 848 free(__DECONST(char *, oidp->oid_label), 849 M_SYSCTLOID); 850 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID); 851 free(oidp, M_SYSCTLOID); 852 } 853 } 854 return (0); 855 } 856 /* 857 * Create new sysctls at run time. 858 * clist may point to a valid context initialized with sysctl_ctx_init(). 859 */ 860 struct sysctl_oid * 861 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 862 int number, const char *name, int kind, void *arg1, intmax_t arg2, 863 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr, 864 const char *label) 865 { 866 struct sysctl_oid *oidp; 867 char *escaped; 868 869 /* You have to hook up somewhere.. */ 870 if (parent == NULL) 871 return(NULL); 872 escaped = sysctl_escape_name(name); 873 /* Check if the node already exists, otherwise create it */ 874 SYSCTL_WLOCK(); 875 oidp = sysctl_find_oidname(escaped, parent); 876 if (oidp != NULL) { 877 free(escaped, M_SYSCTLOID); 878 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 879 oidp->oid_refcnt++; 880 /* Update the context */ 881 if (clist != NULL) 882 sysctl_ctx_entry_add(clist, oidp); 883 SYSCTL_WUNLOCK(); 884 return (oidp); 885 } else { 886 sysctl_warn_reuse(__func__, oidp); 887 SYSCTL_WUNLOCK(); 888 return (NULL); 889 } 890 } 891 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 892 oidp->oid_parent = parent; 893 RB_INIT(&oidp->oid_children); 894 oidp->oid_number = number; 895 oidp->oid_refcnt = 1; 896 oidp->oid_name = escaped; 897 oidp->oid_handler = handler; 898 oidp->oid_kind = CTLFLAG_DYN | kind; 899 oidp->oid_arg1 = arg1; 900 oidp->oid_arg2 = arg2; 901 oidp->oid_fmt = fmt; 902 if (descr != NULL) 903 oidp->oid_descr = strdup(descr, M_SYSCTLOID); 904 if (label != NULL) 905 oidp->oid_label = strdup(label, M_SYSCTLOID); 906 /* Update the context, if used */ 907 if (clist != NULL) 908 sysctl_ctx_entry_add(clist, oidp); 909 /* Register this oid */ 910 sysctl_register_oid(oidp); 911 SYSCTL_WUNLOCK(); 912 return (oidp); 913 } 914 915 /* 916 * Rename an existing oid. 917 */ 918 void 919 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 920 { 921 char *newname; 922 char *oldname; 923 924 newname = strdup(name, M_SYSCTLOID); 925 SYSCTL_WLOCK(); 926 oldname = __DECONST(char *, oidp->oid_name); 927 oidp->oid_name = newname; 928 SYSCTL_WUNLOCK(); 929 free(oldname, M_SYSCTLOID); 930 } 931 932 /* 933 * Reparent an existing oid. 934 */ 935 int 936 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 937 { 938 struct sysctl_oid *oidp; 939 940 SYSCTL_WLOCK(); 941 if (oid->oid_parent == parent) { 942 SYSCTL_WUNLOCK(); 943 return (0); 944 } 945 oidp = sysctl_find_oidname(oid->oid_name, parent); 946 if (oidp != NULL) { 947 SYSCTL_WUNLOCK(); 948 return (EEXIST); 949 } 950 sysctl_unregister_oid(oid); 951 oid->oid_parent = parent; 952 oid->oid_number = OID_AUTO; 953 sysctl_register_oid(oid); 954 SYSCTL_WUNLOCK(); 955 return (0); 956 } 957 958 /* 959 * Register the kernel's oids on startup. 960 */ 961 SET_DECLARE(sysctl_set, struct sysctl_oid); 962 963 static void 964 sysctl_register_all(void *arg) 965 { 966 struct sysctl_oid **oidp; 967 968 sx_init(&sysctlmemlock, "sysctl mem"); 969 sx_init(&sysctlstringlock, "sysctl string handler"); 970 SYSCTL_INIT(); 971 SYSCTL_WLOCK(); 972 SET_FOREACH(oidp, sysctl_set) 973 sysctl_register_oid(*oidp); 974 SYSCTL_WUNLOCK(); 975 } 976 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL); 977 978 /* 979 * "Staff-functions" 980 * 981 * These functions implement a presently undocumented interface 982 * used by the sysctl program to walk the tree, and get the type 983 * so it can print the value. 984 * This interface is under work and consideration, and should probably 985 * be killed with a big axe by the first person who can find the time. 986 * (be aware though, that the proper interface isn't as obvious as it 987 * may seem, there are various conflicting requirements. 988 * 989 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} printf the entire MIB-tree. 990 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..." 991 * OID. 992 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID, honoring 993 * CTLFLAG_SKIP. 994 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in 995 * "new" 996 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info 997 * for the "..." OID. 998 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the 999 * "..." OID. 1000 * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...} return the aggregation label of 1001 * the "..." OID. 1002 * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...} return the next OID, ignoring 1003 * CTLFLAG_SKIP. 1004 */ 1005 1006 #ifdef SYSCTL_DEBUG 1007 static void 1008 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 1009 { 1010 int k; 1011 struct sysctl_oid *oidp; 1012 1013 SYSCTL_ASSERT_LOCKED(); 1014 SYSCTL_FOREACH(oidp, l) { 1015 for (k=0; k<i; k++) 1016 printf(" "); 1017 1018 printf("%d %s ", oidp->oid_number, oidp->oid_name); 1019 1020 printf("%c%c", 1021 oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 1022 oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 1023 1024 if (oidp->oid_handler) 1025 printf(" *Handler"); 1026 1027 switch (oidp->oid_kind & CTLTYPE) { 1028 case CTLTYPE_NODE: 1029 printf(" Node\n"); 1030 if (!oidp->oid_handler) { 1031 sysctl_sysctl_debug_dump_node( 1032 SYSCTL_CHILDREN(oidp), i + 2); 1033 } 1034 break; 1035 case CTLTYPE_INT: printf(" Int\n"); break; 1036 case CTLTYPE_UINT: printf(" u_int\n"); break; 1037 case CTLTYPE_LONG: printf(" Long\n"); break; 1038 case CTLTYPE_ULONG: printf(" u_long\n"); break; 1039 case CTLTYPE_STRING: printf(" String\n"); break; 1040 case CTLTYPE_S8: printf(" int8_t\n"); break; 1041 case CTLTYPE_S16: printf(" int16_t\n"); break; 1042 case CTLTYPE_S32: printf(" int32_t\n"); break; 1043 case CTLTYPE_S64: printf(" int64_t\n"); break; 1044 case CTLTYPE_U8: printf(" uint8_t\n"); break; 1045 case CTLTYPE_U16: printf(" uint16_t\n"); break; 1046 case CTLTYPE_U32: printf(" uint32_t\n"); break; 1047 case CTLTYPE_U64: printf(" uint64_t\n"); break; 1048 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 1049 default: printf("\n"); 1050 } 1051 } 1052 } 1053 1054 static int 1055 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 1056 { 1057 struct rm_priotracker tracker; 1058 int error; 1059 1060 error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 1061 if (error) 1062 return (error); 1063 SYSCTL_RLOCK(&tracker); 1064 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 1065 SYSCTL_RUNLOCK(&tracker); 1066 return (ENOENT); 1067 } 1068 1069 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD | 1070 CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", ""); 1071 #endif 1072 1073 static int 1074 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 1075 { 1076 int *name = (int *) arg1; 1077 u_int namelen = arg2; 1078 int error; 1079 struct sysctl_oid *oid, key; 1080 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 1081 struct rm_priotracker tracker; 1082 char buf[10]; 1083 1084 error = sysctl_wire_old_buffer(req, 0); 1085 if (error) 1086 return (error); 1087 1088 SYSCTL_RLOCK(&tracker); 1089 while (namelen) { 1090 if (!lsp) { 1091 snprintf(buf,sizeof(buf),"%d",*name); 1092 if (req->oldidx) 1093 error = SYSCTL_OUT(req, ".", 1); 1094 if (!error) 1095 error = SYSCTL_OUT(req, buf, strlen(buf)); 1096 if (error) 1097 goto out; 1098 namelen--; 1099 name++; 1100 continue; 1101 } 1102 lsp2 = NULL; 1103 key.oid_number = *name; 1104 oid = RB_FIND(sysctl_oid_list, lsp, &key); 1105 if (oid) { 1106 if (req->oldidx) 1107 error = SYSCTL_OUT(req, ".", 1); 1108 if (!error) 1109 error = SYSCTL_OUT(req, oid->oid_name, 1110 strlen(oid->oid_name)); 1111 if (error) 1112 goto out; 1113 1114 namelen--; 1115 name++; 1116 1117 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE && 1118 !oid->oid_handler) 1119 lsp2 = SYSCTL_CHILDREN(oid); 1120 } 1121 lsp = lsp2; 1122 } 1123 error = SYSCTL_OUT(req, "", 1); 1124 out: 1125 SYSCTL_RUNLOCK(&tracker); 1126 return (error); 1127 } 1128 1129 /* 1130 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in 1131 * capability mode. 1132 */ 1133 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD | 1134 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, ""); 1135 1136 enum sysctl_iter_action { 1137 ITER_SIBLINGS, /* Not matched, continue iterating siblings */ 1138 ITER_CHILDREN, /* Node has children we need to iterate over them */ 1139 ITER_FOUND, /* Matching node was found */ 1140 }; 1141 1142 /* 1143 * Tries to find the next node for @name and @namelen. 1144 * 1145 * Returns next action to take. 1146 */ 1147 static enum sysctl_iter_action 1148 sysctl_sysctl_next_node(struct sysctl_oid *oidp, int *name, unsigned int namelen, 1149 bool honor_skip) 1150 { 1151 1152 if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0) 1153 return (ITER_SIBLINGS); 1154 1155 if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0) 1156 return (ITER_SIBLINGS); 1157 1158 if (namelen == 0) { 1159 /* 1160 * We have reached a node with a full name match and are 1161 * looking for the next oid in its children. 1162 * 1163 * For CTL_SYSCTL_NEXTNOSKIP we are done. 1164 * 1165 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it 1166 * has a handler) and move on to the children. 1167 */ 1168 if (!honor_skip) 1169 return (ITER_FOUND); 1170 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1171 return (ITER_FOUND); 1172 /* If node does not have an iterator, treat it as leaf */ 1173 if (oidp->oid_handler) 1174 return (ITER_FOUND); 1175 1176 /* Report oid as a node to iterate */ 1177 return (ITER_CHILDREN); 1178 } 1179 1180 /* 1181 * No match yet. Continue seeking the given name. 1182 * 1183 * We are iterating in order by oid_number, so skip oids lower 1184 * than the one we are looking for. 1185 * 1186 * When the current oid_number is higher than the one we seek, 1187 * that means we have reached the next oid in the sequence and 1188 * should return it. 1189 * 1190 * If the oid_number matches the name at this level then we 1191 * have to find a node to continue searching at the next level. 1192 */ 1193 if (oidp->oid_number < *name) 1194 return (ITER_SIBLINGS); 1195 if (oidp->oid_number > *name) { 1196 /* 1197 * We have reached the next oid. 1198 * 1199 * For CTL_SYSCTL_NEXTNOSKIP we are done. 1200 * 1201 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it 1202 * has a handler) and move on to the children. 1203 */ 1204 if (!honor_skip) 1205 return (ITER_FOUND); 1206 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1207 return (ITER_FOUND); 1208 /* If node does not have an iterator, treat it as leaf */ 1209 if (oidp->oid_handler) 1210 return (ITER_FOUND); 1211 return (ITER_CHILDREN); 1212 } 1213 1214 /* match at a current level */ 1215 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1216 return (ITER_SIBLINGS); 1217 if (oidp->oid_handler) 1218 return (ITER_SIBLINGS); 1219 1220 return (ITER_CHILDREN); 1221 } 1222 1223 /* 1224 * Recursively walk the sysctl subtree at lsp until we find the given name. 1225 * Returns true and fills in next oid data in @next and @len if oid is found. 1226 */ 1227 static bool 1228 sysctl_sysctl_next_action(struct sysctl_oid_list *lsp, int *name, u_int namelen, 1229 int *next, int *len, int level, bool honor_skip) 1230 { 1231 struct sysctl_oid_list *next_lsp; 1232 struct sysctl_oid *oidp = NULL, key; 1233 bool success = false; 1234 enum sysctl_iter_action action; 1235 1236 SYSCTL_ASSERT_LOCKED(); 1237 /* 1238 * Start the search at the requested oid. But if not found, then scan 1239 * through all children. 1240 */ 1241 if (namelen > 0) { 1242 key.oid_number = *name; 1243 oidp = RB_FIND(sysctl_oid_list, lsp, &key); 1244 } 1245 if (!oidp) 1246 oidp = RB_MIN(sysctl_oid_list, lsp); 1247 for(; oidp != NULL; oidp = RB_NEXT(sysctl_oid_list, lsp, oidp)) { 1248 action = sysctl_sysctl_next_node(oidp, name, namelen, 1249 honor_skip); 1250 if (action == ITER_SIBLINGS) 1251 continue; 1252 if (action == ITER_FOUND) { 1253 success = true; 1254 break; 1255 } 1256 KASSERT((action== ITER_CHILDREN), ("ret(%d)!=ITER_CHILDREN", action)); 1257 1258 next_lsp = SYSCTL_CHILDREN(oidp); 1259 if (namelen == 0) { 1260 success = sysctl_sysctl_next_action(next_lsp, NULL, 0, 1261 next + 1, len, level + 1, honor_skip); 1262 } else { 1263 success = sysctl_sysctl_next_action(next_lsp, name + 1, 1264 namelen - 1, next + 1, len, level + 1, honor_skip); 1265 if (!success) { 1266 1267 /* 1268 * We maintain the invariant that current node oid 1269 * is >= the oid provided in @name. 1270 * As there are no usable children at this node, 1271 * current node oid is strictly > than the requested 1272 * oid. 1273 * Hence, reduce namelen to 0 to allow for picking first 1274 * nodes/leafs in the next node in list. 1275 */ 1276 namelen = 0; 1277 } 1278 } 1279 if (success) 1280 break; 1281 } 1282 1283 if (success) { 1284 *next = oidp->oid_number; 1285 if (level > *len) 1286 *len = level; 1287 } 1288 1289 return (success); 1290 } 1291 1292 static int 1293 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 1294 { 1295 int *name = (int *) arg1; 1296 u_int namelen = arg2; 1297 int len, error; 1298 bool success; 1299 struct sysctl_oid_list *lsp = &sysctl__children; 1300 struct rm_priotracker tracker; 1301 int next[CTL_MAXNAME]; 1302 1303 len = 0; 1304 SYSCTL_RLOCK(&tracker); 1305 success = sysctl_sysctl_next_action(lsp, name, namelen, next, &len, 1, 1306 oidp->oid_number == CTL_SYSCTL_NEXT); 1307 SYSCTL_RUNLOCK(&tracker); 1308 if (!success) 1309 return (ENOENT); 1310 error = SYSCTL_OUT(req, next, len * sizeof (int)); 1311 return (error); 1312 } 1313 1314 /* 1315 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in 1316 * capability mode. 1317 */ 1318 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD | 1319 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, ""); 1320 1321 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD | 1322 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, ""); 1323 1324 static int 1325 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) 1326 { 1327 struct sysctl_oid *oidp; 1328 struct sysctl_oid_list *lsp = &sysctl__children; 1329 1330 SYSCTL_ASSERT_LOCKED(); 1331 1332 for (*len = 0; *len < CTL_MAXNAME;) { 1333 oidp = sysctl_find_oidname(strsep(&name, "."), lsp); 1334 if (oidp == NULL) 1335 return (ENOENT); 1336 *oid++ = oidp->oid_number; 1337 (*len)++; 1338 1339 if (name == NULL || *name == '\0') { 1340 if (oidpp) 1341 *oidpp = oidp; 1342 return (0); 1343 } 1344 1345 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 1346 break; 1347 1348 if (oidp->oid_handler) 1349 break; 1350 1351 lsp = SYSCTL_CHILDREN(oidp); 1352 } 1353 return (ENOENT); 1354 } 1355 1356 static int 1357 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 1358 { 1359 char *p; 1360 int error, oid[CTL_MAXNAME], len = 0; 1361 struct sysctl_oid *op = NULL; 1362 struct rm_priotracker tracker; 1363 char buf[32]; 1364 1365 if (!req->newlen) 1366 return (ENOENT); 1367 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 1368 return (ENAMETOOLONG); 1369 1370 p = buf; 1371 if (req->newlen >= sizeof(buf)) 1372 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 1373 1374 error = SYSCTL_IN(req, p, req->newlen); 1375 if (error) { 1376 if (p != buf) 1377 free(p, M_SYSCTL); 1378 return (error); 1379 } 1380 1381 p [req->newlen] = '\0'; 1382 1383 SYSCTL_RLOCK(&tracker); 1384 error = name2oid(p, oid, &len, &op); 1385 SYSCTL_RUNLOCK(&tracker); 1386 1387 if (p != buf) 1388 free(p, M_SYSCTL); 1389 1390 if (error) 1391 return (error); 1392 1393 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 1394 return (error); 1395 } 1396 1397 /* 1398 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in 1399 * capability mode. 1400 */ 1401 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW | 1402 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0, 1403 sysctl_sysctl_name2oid, "I", ""); 1404 1405 static int 1406 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 1407 { 1408 struct sysctl_oid *oid; 1409 struct rm_priotracker tracker; 1410 int error; 1411 1412 error = sysctl_wire_old_buffer(req, 0); 1413 if (error) 1414 return (error); 1415 1416 SYSCTL_RLOCK(&tracker); 1417 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1418 if (error) 1419 goto out; 1420 1421 if (oid->oid_fmt == NULL) { 1422 error = ENOENT; 1423 goto out; 1424 } 1425 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 1426 if (error) 1427 goto out; 1428 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 1429 out: 1430 SYSCTL_RUNLOCK(&tracker); 1431 return (error); 1432 } 1433 1434 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD | 1435 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, ""); 1436 1437 static int 1438 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 1439 { 1440 struct sysctl_oid *oid; 1441 struct rm_priotracker tracker; 1442 int error; 1443 1444 error = sysctl_wire_old_buffer(req, 0); 1445 if (error) 1446 return (error); 1447 1448 SYSCTL_RLOCK(&tracker); 1449 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1450 if (error) 1451 goto out; 1452 1453 if (oid->oid_descr == NULL) { 1454 error = ENOENT; 1455 goto out; 1456 } 1457 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 1458 out: 1459 SYSCTL_RUNLOCK(&tracker); 1460 return (error); 1461 } 1462 1463 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD | 1464 CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, ""); 1465 1466 static int 1467 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS) 1468 { 1469 struct sysctl_oid *oid; 1470 struct rm_priotracker tracker; 1471 int error; 1472 1473 error = sysctl_wire_old_buffer(req, 0); 1474 if (error) 1475 return (error); 1476 1477 SYSCTL_RLOCK(&tracker); 1478 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1479 if (error) 1480 goto out; 1481 1482 if (oid->oid_label == NULL) { 1483 error = ENOENT; 1484 goto out; 1485 } 1486 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1); 1487 out: 1488 SYSCTL_RUNLOCK(&tracker); 1489 return (error); 1490 } 1491 1492 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD | 1493 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, ""); 1494 1495 /* 1496 * Default "handler" functions. 1497 */ 1498 1499 /* 1500 * Handle a bool. 1501 * Two cases: 1502 * a variable: point arg1 at it. 1503 * a constant: pass it in arg2. 1504 */ 1505 1506 int 1507 sysctl_handle_bool(SYSCTL_HANDLER_ARGS) 1508 { 1509 uint8_t temp; 1510 int error; 1511 1512 /* 1513 * Attempt to get a coherent snapshot by making a copy of the data. 1514 */ 1515 if (arg1) 1516 temp = *(bool *)arg1 ? 1 : 0; 1517 else 1518 temp = arg2 ? 1 : 0; 1519 1520 error = SYSCTL_OUT(req, &temp, sizeof(temp)); 1521 if (error || !req->newptr) 1522 return (error); 1523 1524 if (!arg1) 1525 error = EPERM; 1526 else { 1527 error = SYSCTL_IN(req, &temp, sizeof(temp)); 1528 if (!error) 1529 *(bool *)arg1 = temp ? 1 : 0; 1530 } 1531 return (error); 1532 } 1533 1534 /* 1535 * Handle an int8_t, signed or unsigned. 1536 * Two cases: 1537 * a variable: point arg1 at it. 1538 * a constant: pass it in arg2. 1539 */ 1540 1541 int 1542 sysctl_handle_8(SYSCTL_HANDLER_ARGS) 1543 { 1544 int8_t tmpout; 1545 int error = 0; 1546 1547 /* 1548 * Attempt to get a coherent snapshot by making a copy of the data. 1549 */ 1550 if (arg1) 1551 tmpout = *(int8_t *)arg1; 1552 else 1553 tmpout = arg2; 1554 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); 1555 1556 if (error || !req->newptr) 1557 return (error); 1558 1559 if (!arg1) 1560 error = EPERM; 1561 else 1562 error = SYSCTL_IN(req, arg1, sizeof(tmpout)); 1563 return (error); 1564 } 1565 1566 /* 1567 * Handle an int16_t, signed or unsigned. 1568 * Two cases: 1569 * a variable: point arg1 at it. 1570 * a constant: pass it in arg2. 1571 */ 1572 1573 int 1574 sysctl_handle_16(SYSCTL_HANDLER_ARGS) 1575 { 1576 int16_t tmpout; 1577 int error = 0; 1578 1579 /* 1580 * Attempt to get a coherent snapshot by making a copy of the data. 1581 */ 1582 if (arg1) 1583 tmpout = *(int16_t *)arg1; 1584 else 1585 tmpout = arg2; 1586 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); 1587 1588 if (error || !req->newptr) 1589 return (error); 1590 1591 if (!arg1) 1592 error = EPERM; 1593 else 1594 error = SYSCTL_IN(req, arg1, sizeof(tmpout)); 1595 return (error); 1596 } 1597 1598 /* 1599 * Handle an int32_t, signed or unsigned. 1600 * Two cases: 1601 * a variable: point arg1 at it. 1602 * a constant: pass it in arg2. 1603 */ 1604 1605 int 1606 sysctl_handle_32(SYSCTL_HANDLER_ARGS) 1607 { 1608 int32_t tmpout; 1609 int error = 0; 1610 1611 /* 1612 * Attempt to get a coherent snapshot by making a copy of the data. 1613 */ 1614 if (arg1) 1615 tmpout = *(int32_t *)arg1; 1616 else 1617 tmpout = arg2; 1618 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout)); 1619 1620 if (error || !req->newptr) 1621 return (error); 1622 1623 if (!arg1) 1624 error = EPERM; 1625 else 1626 error = SYSCTL_IN(req, arg1, sizeof(tmpout)); 1627 return (error); 1628 } 1629 1630 /* 1631 * Handle an int, signed or unsigned. 1632 * Two cases: 1633 * a variable: point arg1 at it. 1634 * a constant: pass it in arg2. 1635 */ 1636 1637 int 1638 sysctl_handle_int(SYSCTL_HANDLER_ARGS) 1639 { 1640 int tmpout, error = 0; 1641 1642 /* 1643 * Attempt to get a coherent snapshot by making a copy of the data. 1644 */ 1645 if (arg1) 1646 tmpout = *(int *)arg1; 1647 else 1648 tmpout = arg2; 1649 error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 1650 1651 if (error || !req->newptr) 1652 return (error); 1653 1654 if (!arg1) 1655 error = EPERM; 1656 else 1657 error = SYSCTL_IN(req, arg1, sizeof(int)); 1658 return (error); 1659 } 1660 1661 /* 1662 * Based on on sysctl_handle_int() convert milliseconds into ticks. 1663 * Note: this is used by TCP. 1664 */ 1665 1666 int 1667 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 1668 { 1669 int error, s, tt; 1670 1671 tt = *(int *)arg1; 1672 s = (int)((int64_t)tt * 1000 / hz); 1673 1674 error = sysctl_handle_int(oidp, &s, 0, req); 1675 if (error || !req->newptr) 1676 return (error); 1677 1678 tt = (int)((int64_t)s * hz / 1000); 1679 if (tt < 1) 1680 return (EINVAL); 1681 1682 *(int *)arg1 = tt; 1683 return (0); 1684 } 1685 1686 /* 1687 * Handle a long, signed or unsigned. 1688 * Two cases: 1689 * a variable: point arg1 at it. 1690 * a constant: pass it in arg2. 1691 */ 1692 1693 int 1694 sysctl_handle_long(SYSCTL_HANDLER_ARGS) 1695 { 1696 int error = 0; 1697 long tmplong; 1698 #ifdef SCTL_MASK32 1699 int tmpint; 1700 #endif 1701 1702 /* 1703 * Attempt to get a coherent snapshot by making a copy of the data. 1704 */ 1705 if (arg1) 1706 tmplong = *(long *)arg1; 1707 else 1708 tmplong = arg2; 1709 #ifdef SCTL_MASK32 1710 if (req->flags & SCTL_MASK32) { 1711 tmpint = tmplong; 1712 error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 1713 } else 1714 #endif 1715 error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 1716 1717 if (error || !req->newptr) 1718 return (error); 1719 1720 if (!arg1) 1721 error = EPERM; 1722 #ifdef SCTL_MASK32 1723 else if (req->flags & SCTL_MASK32) { 1724 error = SYSCTL_IN(req, &tmpint, sizeof(int)); 1725 *(long *)arg1 = (long)tmpint; 1726 } 1727 #endif 1728 else 1729 error = SYSCTL_IN(req, arg1, sizeof(long)); 1730 return (error); 1731 } 1732 1733 /* 1734 * Handle a 64 bit int, signed or unsigned. 1735 * Two cases: 1736 * a variable: point arg1 at it. 1737 * a constant: pass it in arg2. 1738 */ 1739 int 1740 sysctl_handle_64(SYSCTL_HANDLER_ARGS) 1741 { 1742 int error = 0; 1743 uint64_t tmpout; 1744 1745 /* 1746 * Attempt to get a coherent snapshot by making a copy of the data. 1747 */ 1748 if (arg1) 1749 tmpout = *(uint64_t *)arg1; 1750 else 1751 tmpout = arg2; 1752 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 1753 1754 if (error || !req->newptr) 1755 return (error); 1756 1757 if (!arg1) 1758 error = EPERM; 1759 else 1760 error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 1761 return (error); 1762 } 1763 1764 /* 1765 * Handle our generic '\0' terminated 'C' string. 1766 * Two cases: 1767 * a variable string: point arg1 at it, arg2 is max length. 1768 * a constant string: point arg1 at it, arg2 is zero. 1769 */ 1770 1771 int 1772 sysctl_handle_string(SYSCTL_HANDLER_ARGS) 1773 { 1774 char *tmparg; 1775 size_t outlen; 1776 int error = 0, ro_string = 0; 1777 1778 /* 1779 * If the sysctl isn't writable and isn't a preallocated tunable that 1780 * can be modified by kenv(2), microoptimise and treat it as a 1781 * read-only string. 1782 * A zero-length buffer indicates a fixed size read-only 1783 * string. In ddb, don't worry about trying to make a malloced 1784 * snapshot. 1785 */ 1786 if ((oidp->oid_kind & (CTLFLAG_WR | CTLFLAG_TUN)) == 0 || 1787 arg2 == 0 || kdb_active) { 1788 arg2 = strlen((char *)arg1) + 1; 1789 ro_string = 1; 1790 } 1791 1792 if (req->oldptr != NULL) { 1793 if (ro_string) { 1794 tmparg = arg1; 1795 outlen = strlen(tmparg) + 1; 1796 } else { 1797 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); 1798 sx_slock(&sysctlstringlock); 1799 memcpy(tmparg, arg1, arg2); 1800 sx_sunlock(&sysctlstringlock); 1801 outlen = strlen(tmparg) + 1; 1802 } 1803 1804 error = SYSCTL_OUT(req, tmparg, outlen); 1805 1806 if (!ro_string) 1807 free(tmparg, M_SYSCTLTMP); 1808 } else { 1809 if (!ro_string) 1810 sx_slock(&sysctlstringlock); 1811 outlen = strlen((char *)arg1) + 1; 1812 if (!ro_string) 1813 sx_sunlock(&sysctlstringlock); 1814 error = SYSCTL_OUT(req, NULL, outlen); 1815 } 1816 if (error || !req->newptr) 1817 return (error); 1818 1819 if (req->newlen - req->newidx >= arg2 || 1820 req->newlen - req->newidx < 0) { 1821 error = EINVAL; 1822 } else if (req->newlen - req->newidx == 0) { 1823 sx_xlock(&sysctlstringlock); 1824 ((char *)arg1)[0] = '\0'; 1825 sx_xunlock(&sysctlstringlock); 1826 } else if (req->newfunc == sysctl_new_kernel) { 1827 arg2 = req->newlen - req->newidx; 1828 sx_xlock(&sysctlstringlock); 1829 error = SYSCTL_IN(req, arg1, arg2); 1830 if (error == 0) { 1831 ((char *)arg1)[arg2] = '\0'; 1832 req->newidx += arg2; 1833 } 1834 sx_xunlock(&sysctlstringlock); 1835 } else { 1836 arg2 = req->newlen - req->newidx; 1837 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); 1838 1839 error = SYSCTL_IN(req, tmparg, arg2); 1840 if (error) { 1841 free(tmparg, M_SYSCTLTMP); 1842 return (error); 1843 } 1844 1845 sx_xlock(&sysctlstringlock); 1846 memcpy(arg1, tmparg, arg2); 1847 ((char *)arg1)[arg2] = '\0'; 1848 sx_xunlock(&sysctlstringlock); 1849 free(tmparg, M_SYSCTLTMP); 1850 req->newidx += arg2; 1851 } 1852 return (error); 1853 } 1854 1855 /* 1856 * Handle any kind of opaque data. 1857 * arg1 points to it, arg2 is the size. 1858 */ 1859 1860 int 1861 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 1862 { 1863 int error, tries; 1864 u_int generation; 1865 struct sysctl_req req2; 1866 1867 /* 1868 * Attempt to get a coherent snapshot, by using the thread 1869 * pre-emption counter updated from within mi_switch() to 1870 * determine if we were pre-empted during a bcopy() or 1871 * copyout(). Make 3 attempts at doing this before giving up. 1872 * If we encounter an error, stop immediately. 1873 */ 1874 tries = 0; 1875 req2 = *req; 1876 retry: 1877 generation = curthread->td_generation; 1878 error = SYSCTL_OUT(req, arg1, arg2); 1879 if (error) 1880 return (error); 1881 tries++; 1882 if (generation != curthread->td_generation && tries < 3) { 1883 *req = req2; 1884 goto retry; 1885 } 1886 1887 error = SYSCTL_IN(req, arg1, arg2); 1888 1889 return (error); 1890 } 1891 1892 /* 1893 * Based on on sysctl_handle_int() convert microseconds to a sbintime. 1894 */ 1895 int 1896 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS) 1897 { 1898 int error; 1899 int64_t tt; 1900 sbintime_t sb; 1901 1902 tt = *(int64_t *)arg1; 1903 sb = sbttous(tt); 1904 1905 error = sysctl_handle_64(oidp, &sb, 0, req); 1906 if (error || !req->newptr) 1907 return (error); 1908 1909 tt = ustosbt(sb); 1910 *(int64_t *)arg1 = tt; 1911 1912 return (0); 1913 } 1914 1915 /* 1916 * Based on on sysctl_handle_int() convert milliseconds to a sbintime. 1917 */ 1918 int 1919 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS) 1920 { 1921 int error; 1922 int64_t tt; 1923 sbintime_t sb; 1924 1925 tt = *(int64_t *)arg1; 1926 sb = sbttoms(tt); 1927 1928 error = sysctl_handle_64(oidp, &sb, 0, req); 1929 if (error || !req->newptr) 1930 return (error); 1931 1932 tt = mstosbt(sb); 1933 *(int64_t *)arg1 = tt; 1934 1935 return (0); 1936 } 1937 1938 /* 1939 * Convert seconds to a struct timeval. Intended for use with 1940 * intervals and thus does not permit negative seconds. 1941 */ 1942 int 1943 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS) 1944 { 1945 struct timeval *tv; 1946 int error, secs; 1947 1948 tv = arg1; 1949 secs = tv->tv_sec; 1950 1951 error = sysctl_handle_int(oidp, &secs, 0, req); 1952 if (error || req->newptr == NULL) 1953 return (error); 1954 1955 if (secs < 0) 1956 return (EINVAL); 1957 tv->tv_sec = secs; 1958 1959 return (0); 1960 } 1961 1962 /* 1963 * Transfer functions to/from kernel space. 1964 * XXX: rather untested at this point 1965 */ 1966 static int 1967 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 1968 { 1969 size_t i = 0; 1970 1971 if (req->oldptr) { 1972 i = l; 1973 if (req->oldlen <= req->oldidx) 1974 i = 0; 1975 else 1976 if (i > req->oldlen - req->oldidx) 1977 i = req->oldlen - req->oldidx; 1978 if (i > 0) 1979 bcopy(p, (char *)req->oldptr + req->oldidx, i); 1980 } 1981 req->oldidx += l; 1982 if (req->oldptr && i != l) 1983 return (ENOMEM); 1984 return (0); 1985 } 1986 1987 static int 1988 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 1989 { 1990 if (!req->newptr) 1991 return (0); 1992 if (req->newlen - req->newidx < l) 1993 return (EINVAL); 1994 bcopy((const char *)req->newptr + req->newidx, p, l); 1995 req->newidx += l; 1996 return (0); 1997 } 1998 1999 int 2000 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 2001 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 2002 { 2003 int error = 0; 2004 struct sysctl_req req; 2005 2006 bzero(&req, sizeof req); 2007 2008 req.td = td; 2009 req.flags = flags; 2010 2011 if (oldlenp) { 2012 req.oldlen = *oldlenp; 2013 } 2014 req.validlen = req.oldlen; 2015 2016 if (old) { 2017 req.oldptr= old; 2018 } 2019 2020 if (new != NULL) { 2021 req.newlen = newlen; 2022 req.newptr = new; 2023 } 2024 2025 req.oldfunc = sysctl_old_kernel; 2026 req.newfunc = sysctl_new_kernel; 2027 req.lock = REQ_UNWIRED; 2028 2029 error = sysctl_root(0, name, namelen, &req); 2030 2031 if (req.lock == REQ_WIRED && req.validlen > 0) 2032 vsunlock(req.oldptr, req.validlen); 2033 2034 if (error && error != ENOMEM) 2035 return (error); 2036 2037 if (retval) { 2038 if (req.oldptr && req.oldidx > req.validlen) 2039 *retval = req.validlen; 2040 else 2041 *retval = req.oldidx; 2042 } 2043 return (error); 2044 } 2045 2046 int 2047 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 2048 void *new, size_t newlen, size_t *retval, int flags) 2049 { 2050 int oid[CTL_MAXNAME]; 2051 size_t oidlen, plen; 2052 int error; 2053 2054 oid[0] = CTL_SYSCTL; 2055 oid[1] = CTL_SYSCTL_NAME2OID; 2056 oidlen = sizeof(oid); 2057 2058 error = kernel_sysctl(td, oid, 2, oid, &oidlen, 2059 (void *)name, strlen(name), &plen, flags); 2060 if (error) 2061 return (error); 2062 2063 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 2064 new, newlen, retval, flags); 2065 return (error); 2066 } 2067 2068 /* 2069 * Transfer function to/from user space. 2070 */ 2071 static int 2072 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 2073 { 2074 size_t i, len, origidx; 2075 int error; 2076 2077 origidx = req->oldidx; 2078 req->oldidx += l; 2079 if (req->oldptr == NULL) 2080 return (0); 2081 /* 2082 * If we have not wired the user supplied buffer and we are currently 2083 * holding locks, drop a witness warning, as it's possible that 2084 * write operations to the user page can sleep. 2085 */ 2086 if (req->lock != REQ_WIRED) 2087 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2088 "sysctl_old_user()"); 2089 i = l; 2090 len = req->validlen; 2091 if (len <= origidx) 2092 i = 0; 2093 else { 2094 if (i > len - origidx) 2095 i = len - origidx; 2096 if (req->lock == REQ_WIRED) { 2097 error = copyout_nofault(p, (char *)req->oldptr + 2098 origidx, i); 2099 } else 2100 error = copyout(p, (char *)req->oldptr + origidx, i); 2101 if (error != 0) 2102 return (error); 2103 } 2104 if (i < l) 2105 return (ENOMEM); 2106 return (0); 2107 } 2108 2109 static int 2110 sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 2111 { 2112 int error; 2113 2114 if (!req->newptr) 2115 return (0); 2116 if (req->newlen - req->newidx < l) 2117 return (EINVAL); 2118 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 2119 "sysctl_new_user()"); 2120 error = copyin((const char *)req->newptr + req->newidx, p, l); 2121 req->newidx += l; 2122 return (error); 2123 } 2124 2125 /* 2126 * Wire the user space destination buffer. If set to a value greater than 2127 * zero, the len parameter limits the maximum amount of wired memory. 2128 */ 2129 int 2130 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 2131 { 2132 int ret; 2133 size_t wiredlen; 2134 2135 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 2136 ret = 0; 2137 if (req->lock != REQ_WIRED && req->oldptr && 2138 req->oldfunc == sysctl_old_user) { 2139 if (wiredlen != 0) { 2140 ret = vslock(req->oldptr, wiredlen); 2141 if (ret != 0) { 2142 if (ret != ENOMEM) 2143 return (ret); 2144 wiredlen = 0; 2145 } 2146 } 2147 req->lock = REQ_WIRED; 2148 req->validlen = wiredlen; 2149 } 2150 return (0); 2151 } 2152 2153 int 2154 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 2155 int *nindx, struct sysctl_req *req) 2156 { 2157 struct sysctl_oid_list *lsp; 2158 struct sysctl_oid *oid; 2159 struct sysctl_oid key; 2160 int indx; 2161 2162 SYSCTL_ASSERT_LOCKED(); 2163 lsp = &sysctl__children; 2164 indx = 0; 2165 while (indx < CTL_MAXNAME) { 2166 key.oid_number = name[indx]; 2167 oid = RB_FIND(sysctl_oid_list, lsp, &key); 2168 if (oid == NULL) 2169 return (ENOENT); 2170 2171 indx++; 2172 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 2173 if (oid->oid_handler != NULL || indx == namelen) { 2174 *noid = oid; 2175 if (nindx != NULL) 2176 *nindx = indx; 2177 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 2178 ("%s found DYING node %p", __func__, oid)); 2179 return (0); 2180 } 2181 lsp = SYSCTL_CHILDREN(oid); 2182 } else if (indx == namelen) { 2183 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0) 2184 return (ENOENT); 2185 *noid = oid; 2186 if (nindx != NULL) 2187 *nindx = indx; 2188 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 2189 ("%s found DYING node %p", __func__, oid)); 2190 return (0); 2191 } else { 2192 return (ENOTDIR); 2193 } 2194 } 2195 return (ENOENT); 2196 } 2197 2198 /* 2199 * Traverse our tree, and find the right node, execute whatever it points 2200 * to, and return the resulting error code. 2201 */ 2202 2203 static int 2204 sysctl_root(SYSCTL_HANDLER_ARGS) 2205 { 2206 struct sysctl_oid *oid; 2207 struct rm_priotracker tracker; 2208 int error, indx, lvl; 2209 2210 SYSCTL_RLOCK(&tracker); 2211 2212 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 2213 if (error) 2214 goto out; 2215 2216 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 2217 /* 2218 * You can't call a sysctl when it's a node, but has 2219 * no handler. Inform the user that it's a node. 2220 * The indx may or may not be the same as namelen. 2221 */ 2222 if (oid->oid_handler == NULL) { 2223 error = EISDIR; 2224 goto out; 2225 } 2226 } 2227 2228 /* Is this sysctl writable? */ 2229 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) { 2230 error = EPERM; 2231 goto out; 2232 } 2233 2234 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 2235 2236 #ifdef CAPABILITY_MODE 2237 /* 2238 * If the process is in capability mode, then don't permit reading or 2239 * writing unless specifically granted for the node. 2240 */ 2241 if (IN_CAPABILITY_MODE(req->td)) { 2242 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) || 2243 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) { 2244 error = EPERM; 2245 goto out; 2246 } 2247 } 2248 #endif 2249 2250 /* Is this sysctl sensitive to securelevels? */ 2251 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 2252 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 2253 error = securelevel_gt(req->td->td_ucred, lvl); 2254 if (error) 2255 goto out; 2256 } 2257 2258 /* Is this sysctl writable by only privileged users? */ 2259 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 2260 int priv; 2261 2262 if (oid->oid_kind & CTLFLAG_PRISON) 2263 priv = PRIV_SYSCTL_WRITEJAIL; 2264 #ifdef VIMAGE 2265 else if ((oid->oid_kind & CTLFLAG_VNET) && 2266 prison_owns_vnet(req->td->td_ucred)) 2267 priv = PRIV_SYSCTL_WRITEJAIL; 2268 #endif 2269 else 2270 priv = PRIV_SYSCTL_WRITE; 2271 error = priv_check(req->td, priv); 2272 if (error) 2273 goto out; 2274 } 2275 2276 if (!oid->oid_handler) { 2277 error = EINVAL; 2278 goto out; 2279 } 2280 2281 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 2282 arg1 = (int *)arg1 + indx; 2283 arg2 -= indx; 2284 } else { 2285 arg1 = oid->oid_arg1; 2286 arg2 = oid->oid_arg2; 2287 } 2288 #ifdef MAC 2289 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 2290 req); 2291 if (error != 0) 2292 goto out; 2293 #endif 2294 #ifdef VIMAGE 2295 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL) 2296 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1); 2297 #endif 2298 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker); 2299 2300 out: 2301 SYSCTL_RUNLOCK(&tracker); 2302 return (error); 2303 } 2304 2305 #ifndef _SYS_SYSPROTO_H_ 2306 struct __sysctl_args { 2307 int *name; 2308 u_int namelen; 2309 void *old; 2310 size_t *oldlenp; 2311 void *new; 2312 size_t newlen; 2313 }; 2314 #endif 2315 int 2316 sys___sysctl(struct thread *td, struct __sysctl_args *uap) 2317 { 2318 int error, i, name[CTL_MAXNAME]; 2319 size_t j; 2320 2321 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 2322 return (EINVAL); 2323 2324 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 2325 if (error) 2326 return (error); 2327 2328 error = userland_sysctl(td, name, uap->namelen, 2329 uap->old, uap->oldlenp, 0, 2330 uap->new, uap->newlen, &j, 0); 2331 if (error && error != ENOMEM) 2332 return (error); 2333 if (uap->oldlenp) { 2334 i = copyout(&j, uap->oldlenp, sizeof(j)); 2335 if (i) 2336 return (i); 2337 } 2338 return (error); 2339 } 2340 2341 int 2342 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen, 2343 void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval, 2344 int flags, bool inkernel) 2345 { 2346 int oid[CTL_MAXNAME]; 2347 char namebuf[16]; 2348 char *name; 2349 size_t oidlen; 2350 int error; 2351 2352 if (namelen > MAXPATHLEN || namelen == 0) 2353 return (EINVAL); 2354 name = namebuf; 2355 if (namelen > sizeof(namebuf)) 2356 name = malloc(namelen, M_SYSCTL, M_WAITOK); 2357 error = copyin(oname, name, namelen); 2358 if (error != 0) 2359 goto out; 2360 2361 oid[0] = CTL_SYSCTL; 2362 oid[1] = CTL_SYSCTL_NAME2OID; 2363 oidlen = sizeof(oid); 2364 error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen, 2365 retval, flags); 2366 if (error != 0) 2367 goto out; 2368 error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp, 2369 inkernel, new, newlen, retval, flags); 2370 2371 out: 2372 if (namelen > sizeof(namebuf)) 2373 free(name, M_SYSCTL); 2374 return (error); 2375 } 2376 2377 #ifndef _SYS_SYSPROTO_H_ 2378 struct __sysctlbyname_args { 2379 const char *name; 2380 size_t namelen; 2381 void *old; 2382 size_t *oldlenp; 2383 void *new; 2384 size_t newlen; 2385 }; 2386 #endif 2387 int 2388 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap) 2389 { 2390 size_t rv; 2391 int error; 2392 2393 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old, 2394 uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0); 2395 if (error != 0) 2396 return (error); 2397 if (uap->oldlenp != NULL) 2398 error = copyout(&rv, uap->oldlenp, sizeof(rv)); 2399 2400 return (error); 2401 } 2402 2403 /* 2404 * This is used from various compatibility syscalls too. That's why name 2405 * must be in kernel space. 2406 */ 2407 int 2408 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 2409 size_t *oldlenp, int inkernel, const void *new, size_t newlen, 2410 size_t *retval, int flags) 2411 { 2412 int error = 0, memlocked; 2413 struct sysctl_req req; 2414 2415 bzero(&req, sizeof req); 2416 2417 req.td = td; 2418 req.flags = flags; 2419 2420 if (oldlenp) { 2421 if (inkernel) { 2422 req.oldlen = *oldlenp; 2423 } else { 2424 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 2425 if (error) 2426 return (error); 2427 } 2428 } 2429 req.validlen = req.oldlen; 2430 req.oldptr = old; 2431 2432 if (new != NULL) { 2433 req.newlen = newlen; 2434 req.newptr = new; 2435 } 2436 2437 req.oldfunc = sysctl_old_user; 2438 req.newfunc = sysctl_new_user; 2439 req.lock = REQ_UNWIRED; 2440 2441 #ifdef KTRACE 2442 if (KTRPOINT(curthread, KTR_SYSCTL)) 2443 ktrsysctl(name, namelen); 2444 #endif 2445 memlocked = 0; 2446 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) { 2447 memlocked = 1; 2448 sx_xlock(&sysctlmemlock); 2449 } 2450 CURVNET_SET(TD_TO_VNET(td)); 2451 2452 for (;;) { 2453 req.oldidx = 0; 2454 req.newidx = 0; 2455 error = sysctl_root(0, name, namelen, &req); 2456 if (error != EAGAIN) 2457 break; 2458 kern_yield(PRI_USER); 2459 } 2460 2461 CURVNET_RESTORE(); 2462 2463 if (req.lock == REQ_WIRED && req.validlen > 0) 2464 vsunlock(req.oldptr, req.validlen); 2465 if (memlocked) 2466 sx_xunlock(&sysctlmemlock); 2467 2468 if (error && error != ENOMEM) 2469 return (error); 2470 2471 if (retval) { 2472 if (req.oldptr && req.oldidx > req.validlen) 2473 *retval = req.validlen; 2474 else 2475 *retval = req.oldidx; 2476 } 2477 return (error); 2478 } 2479 2480 /* 2481 * Drain into a sysctl struct. The user buffer should be wired if a page 2482 * fault would cause issue. 2483 */ 2484 static int 2485 sbuf_sysctl_drain(void *arg, const char *data, int len) 2486 { 2487 struct sysctl_req *req = arg; 2488 int error; 2489 2490 error = SYSCTL_OUT(req, data, len); 2491 KASSERT(error >= 0, ("Got unexpected negative value %d", error)); 2492 return (error == 0 ? len : -error); 2493 } 2494 2495 struct sbuf * 2496 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, 2497 struct sysctl_req *req) 2498 { 2499 2500 /* Supply a default buffer size if none given. */ 2501 if (buf == NULL && length == 0) 2502 length = 64; 2503 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL); 2504 sbuf_set_drain(s, sbuf_sysctl_drain, req); 2505 return (s); 2506 } 2507 2508 #ifdef DDB 2509 2510 /* The current OID the debugger is working with */ 2511 static struct sysctl_oid *g_ddb_oid; 2512 2513 /* The current flags specified by the user */ 2514 static int g_ddb_sysctl_flags; 2515 2516 /* Check to see if the last sysctl printed */ 2517 static int g_ddb_sysctl_printed; 2518 2519 static const int ctl_sign[CTLTYPE+1] = { 2520 [CTLTYPE_INT] = 1, 2521 [CTLTYPE_LONG] = 1, 2522 [CTLTYPE_S8] = 1, 2523 [CTLTYPE_S16] = 1, 2524 [CTLTYPE_S32] = 1, 2525 [CTLTYPE_S64] = 1, 2526 }; 2527 2528 static const int ctl_size[CTLTYPE+1] = { 2529 [CTLTYPE_INT] = sizeof(int), 2530 [CTLTYPE_UINT] = sizeof(u_int), 2531 [CTLTYPE_LONG] = sizeof(long), 2532 [CTLTYPE_ULONG] = sizeof(u_long), 2533 [CTLTYPE_S8] = sizeof(int8_t), 2534 [CTLTYPE_S16] = sizeof(int16_t), 2535 [CTLTYPE_S32] = sizeof(int32_t), 2536 [CTLTYPE_S64] = sizeof(int64_t), 2537 [CTLTYPE_U8] = sizeof(uint8_t), 2538 [CTLTYPE_U16] = sizeof(uint16_t), 2539 [CTLTYPE_U32] = sizeof(uint32_t), 2540 [CTLTYPE_U64] = sizeof(uint64_t), 2541 }; 2542 2543 #define DB_SYSCTL_NAME_ONLY 0x001 /* Compare with -N */ 2544 #define DB_SYSCTL_VALUE_ONLY 0x002 /* Compare with -n */ 2545 #define DB_SYSCTL_OPAQUE 0x004 /* Compare with -o */ 2546 #define DB_SYSCTL_HEX 0x008 /* Compare with -x */ 2547 2548 #define DB_SYSCTL_SAFE_ONLY 0x100 /* Only simple types */ 2549 2550 static const char db_sysctl_modifs[] = { 2551 'N', 'n', 'o', 'x', 2552 }; 2553 2554 static const int db_sysctl_modif_values[] = { 2555 DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY, 2556 DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX, 2557 }; 2558 2559 /* Handlers considered safe to print while recursing */ 2560 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = { 2561 sysctl_handle_bool, 2562 sysctl_handle_8, 2563 sysctl_handle_16, 2564 sysctl_handle_32, 2565 sysctl_handle_64, 2566 sysctl_handle_int, 2567 sysctl_handle_long, 2568 sysctl_handle_string, 2569 sysctl_handle_opaque, 2570 }; 2571 2572 /* 2573 * Use in place of sysctl_old_kernel to print sysctl values. 2574 * 2575 * Compare to the output handling in show_var from sbin/sysctl/sysctl.c 2576 */ 2577 static int 2578 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len) 2579 { 2580 const u_char *val, *p; 2581 const char *sep1; 2582 size_t intlen, slen; 2583 uintmax_t umv; 2584 intmax_t mv; 2585 int sign, ctltype, hexlen, xflag, error; 2586 2587 /* Suppress false-positive GCC uninitialized variable warnings */ 2588 mv = 0; 2589 umv = 0; 2590 2591 slen = len; 2592 val = p = ptr; 2593 2594 if (ptr == NULL) { 2595 error = 0; 2596 goto out; 2597 } 2598 2599 /* We are going to print */ 2600 g_ddb_sysctl_printed = 1; 2601 2602 xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX; 2603 2604 ctltype = (g_ddb_oid->oid_kind & CTLTYPE); 2605 sign = ctl_sign[ctltype]; 2606 intlen = ctl_size[ctltype]; 2607 2608 switch (ctltype) { 2609 case CTLTYPE_NODE: 2610 case CTLTYPE_STRING: 2611 db_printf("%.*s", (int) len, (const char *) p); 2612 error = 0; 2613 goto out; 2614 2615 case CTLTYPE_INT: 2616 case CTLTYPE_UINT: 2617 case CTLTYPE_LONG: 2618 case CTLTYPE_ULONG: 2619 case CTLTYPE_S8: 2620 case CTLTYPE_S16: 2621 case CTLTYPE_S32: 2622 case CTLTYPE_S64: 2623 case CTLTYPE_U8: 2624 case CTLTYPE_U16: 2625 case CTLTYPE_U32: 2626 case CTLTYPE_U64: 2627 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4; 2628 sep1 = ""; 2629 while (len >= intlen) { 2630 switch (ctltype) { 2631 case CTLTYPE_INT: 2632 case CTLTYPE_UINT: 2633 umv = *(const u_int *)p; 2634 mv = *(const int *)p; 2635 break; 2636 case CTLTYPE_LONG: 2637 case CTLTYPE_ULONG: 2638 umv = *(const u_long *)p; 2639 mv = *(const long *)p; 2640 break; 2641 case CTLTYPE_S8: 2642 case CTLTYPE_U8: 2643 umv = *(const uint8_t *)p; 2644 mv = *(const int8_t *)p; 2645 break; 2646 case CTLTYPE_S16: 2647 case CTLTYPE_U16: 2648 umv = *(const uint16_t *)p; 2649 mv = *(const int16_t *)p; 2650 break; 2651 case CTLTYPE_S32: 2652 case CTLTYPE_U32: 2653 umv = *(const uint32_t *)p; 2654 mv = *(const int32_t *)p; 2655 break; 2656 case CTLTYPE_S64: 2657 case CTLTYPE_U64: 2658 umv = *(const uint64_t *)p; 2659 mv = *(const int64_t *)p; 2660 break; 2661 } 2662 2663 db_printf("%s", sep1); 2664 if (xflag) 2665 db_printf("%#0*jx", hexlen, umv); 2666 else if (!sign) 2667 db_printf("%ju", umv); 2668 else if (g_ddb_oid->oid_fmt[1] == 'K') { 2669 /* Kelvins are currently unsupported. */ 2670 error = EOPNOTSUPP; 2671 goto out; 2672 } else 2673 db_printf("%jd", mv); 2674 2675 sep1 = " "; 2676 len -= intlen; 2677 p += intlen; 2678 } 2679 error = 0; 2680 goto out; 2681 2682 case CTLTYPE_OPAQUE: 2683 /* TODO: Support struct functions. */ 2684 2685 /* FALLTHROUGH */ 2686 default: 2687 db_printf("Format:%s Length:%zu Dump:0x", 2688 g_ddb_oid->oid_fmt, len); 2689 while (len-- && (xflag || p < val + 16)) 2690 db_printf("%02x", *p++); 2691 if (!xflag && len > 16) 2692 db_printf("..."); 2693 error = 0; 2694 goto out; 2695 } 2696 2697 out: 2698 req->oldidx += slen; 2699 return (error); 2700 } 2701 2702 /* 2703 * Avoid setting new sysctl values from the debugger 2704 */ 2705 static int 2706 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l) 2707 { 2708 2709 if (!req->newptr) 2710 return (0); 2711 2712 /* Changing sysctls from the debugger is currently unsupported */ 2713 return (EPERM); 2714 } 2715 2716 /* 2717 * Run a sysctl handler with the DDB oldfunc and newfunc attached. 2718 * Instead of copying any output to a buffer we'll dump it right to 2719 * the console. 2720 */ 2721 static int 2722 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen, 2723 void *old, size_t *oldlenp, size_t *retval, int flags) 2724 { 2725 struct sysctl_req req; 2726 int error; 2727 2728 /* Setup the request */ 2729 bzero(&req, sizeof req); 2730 req.td = kdb_thread; 2731 req.oldfunc = sysctl_old_ddb; 2732 req.newfunc = sysctl_new_ddb; 2733 req.lock = REQ_UNWIRED; 2734 if (oldlenp) { 2735 req.oldlen = *oldlenp; 2736 } 2737 req.validlen = req.oldlen; 2738 if (old) { 2739 req.oldptr = old; 2740 } 2741 2742 /* Setup our globals for sysctl_old_ddb */ 2743 g_ddb_oid = oidp; 2744 g_ddb_sysctl_flags = flags; 2745 g_ddb_sysctl_printed = 0; 2746 2747 error = sysctl_root(0, name, namelen, &req); 2748 2749 /* Reset globals */ 2750 g_ddb_oid = NULL; 2751 g_ddb_sysctl_flags = 0; 2752 2753 if (retval) { 2754 if (req.oldptr && req.oldidx > req.validlen) 2755 *retval = req.validlen; 2756 else 2757 *retval = req.oldidx; 2758 } 2759 return (error); 2760 } 2761 2762 /* 2763 * Show a sysctl's name 2764 */ 2765 static void 2766 db_show_oid_name(int *oid, size_t nlen) 2767 { 2768 struct sysctl_oid *oidp; 2769 int qoid[CTL_MAXNAME+2]; 2770 int error; 2771 2772 qoid[0] = 0; 2773 memcpy(qoid + 2, oid, nlen * sizeof(int)); 2774 qoid[1] = 1; 2775 2776 error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL); 2777 if (error) 2778 db_error("sysctl name oid"); 2779 2780 error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0); 2781 if (error) 2782 db_error("sysctl name"); 2783 } 2784 2785 /* 2786 * Check to see if an OID is safe to print from ddb. 2787 */ 2788 static bool 2789 db_oid_safe(const struct sysctl_oid *oidp) 2790 { 2791 for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) { 2792 if (oidp->oid_handler == db_safe_handlers[i]) 2793 return (true); 2794 } 2795 2796 return (false); 2797 } 2798 2799 /* 2800 * Show a sysctl at a specific OID 2801 * Compare to the input handling in show_var from sbin/sysctl/sysctl.c 2802 */ 2803 static int 2804 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags) 2805 { 2806 int error, xflag, oflag, Nflag, nflag; 2807 size_t len; 2808 2809 xflag = flags & DB_SYSCTL_HEX; 2810 oflag = flags & DB_SYSCTL_OPAQUE; 2811 nflag = flags & DB_SYSCTL_VALUE_ONLY; 2812 Nflag = flags & DB_SYSCTL_NAME_ONLY; 2813 2814 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE && 2815 (!xflag && !oflag)) 2816 return (0); 2817 2818 if (Nflag) { 2819 db_show_oid_name(oid, nlen); 2820 error = 0; 2821 goto out; 2822 } 2823 2824 if (!nflag) { 2825 db_show_oid_name(oid, nlen); 2826 db_printf(": "); 2827 } 2828 2829 if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) { 2830 db_printf("Skipping, unsafe to print while recursing."); 2831 error = 0; 2832 goto out; 2833 } 2834 2835 /* Try once, and ask about the size */ 2836 len = 0; 2837 error = db_sysctl(oidp, oid, nlen, 2838 NULL, NULL, &len, flags); 2839 if (error) 2840 goto out; 2841 2842 if (!g_ddb_sysctl_printed) 2843 /* Lie about the size */ 2844 error = db_sysctl(oidp, oid, nlen, 2845 (void *) 1, &len, NULL, flags); 2846 2847 out: 2848 db_printf("\n"); 2849 return (error); 2850 } 2851 2852 /* 2853 * Show all sysctls under a specific OID 2854 * Compare to sysctl_all from sbin/sysctl/sysctl.c 2855 */ 2856 static int 2857 db_show_sysctl_all(int *oid, size_t len, int flags) 2858 { 2859 struct sysctl_oid *oidp; 2860 int name1[CTL_MAXNAME + 2], name2[CTL_MAXNAME + 2]; 2861 size_t l1, l2; 2862 2863 name1[0] = CTL_SYSCTL; 2864 name1[1] = CTL_SYSCTL_NEXT; 2865 l1 = 2; 2866 if (len) { 2867 memcpy(name1 + 2, oid, len * sizeof(int)); 2868 l1 += len; 2869 } else { 2870 name1[2] = CTL_KERN; 2871 l1++; 2872 } 2873 for (;;) { 2874 int i, error; 2875 2876 l2 = sizeof(name2); 2877 error = kernel_sysctl(kdb_thread, name1, l1, 2878 name2, &l2, NULL, 0, &l2, 0); 2879 if (error != 0) { 2880 if (error == ENOENT) 2881 return (0); 2882 else 2883 db_error("sysctl(next)"); 2884 } 2885 2886 l2 /= sizeof(int); 2887 2888 if (l2 < (unsigned int)len) 2889 return (0); 2890 2891 for (i = 0; i < len; i++) 2892 if (name2[i] != oid[i]) 2893 return (0); 2894 2895 /* Find the OID in question */ 2896 error = sysctl_find_oid(name2, l2, &oidp, NULL, NULL); 2897 if (error) 2898 return (error); 2899 2900 i = db_show_oid(oidp, name2, l2, flags | DB_SYSCTL_SAFE_ONLY); 2901 2902 if (db_pager_quit) 2903 return (0); 2904 2905 memcpy(name1+2, name2, l2 * sizeof(int)); 2906 l1 = 2 + l2; 2907 } 2908 } 2909 2910 /* 2911 * Show a sysctl by its user facing string 2912 */ 2913 static int 2914 db_sysctlbyname(char *name, int flags) 2915 { 2916 struct sysctl_oid *oidp; 2917 int oid[CTL_MAXNAME]; 2918 int error, nlen; 2919 2920 error = name2oid(name, oid, &nlen, &oidp); 2921 if (error) { 2922 return (error); 2923 } 2924 2925 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 2926 db_show_sysctl_all(oid, nlen, flags); 2927 } else { 2928 error = db_show_oid(oidp, oid, nlen, flags); 2929 } 2930 2931 return (error); 2932 } 2933 2934 static void 2935 db_sysctl_cmd_usage(void) 2936 { 2937 db_printf( 2938 " sysctl [/Nnox] <sysctl> \n" 2939 " \n" 2940 " <sysctl> The name of the sysctl to show. \n" 2941 " \n" 2942 " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT. \n" 2943 " This will work for most sysctls, but should not be used \n" 2944 " with sysctls that are known to malloc. \n" 2945 " \n" 2946 " While recursing any \"unsafe\" sysctls will be skipped. \n" 2947 " Call sysctl directly on the sysctl to try printing the \n" 2948 " skipped sysctl. This is unsafe and may make the ddb \n" 2949 " session unusable. \n" 2950 " \n" 2951 " Arguments: \n" 2952 " /N Display only the name of the sysctl. \n" 2953 " /n Display only the value of the sysctl. \n" 2954 " /o Display opaque values. \n" 2955 " /x Display the sysctl in hex. \n" 2956 " \n" 2957 "For example: \n" 2958 "sysctl vm.v_free_min \n" 2959 "vn.v_free_min: 12669 \n" 2960 ); 2961 } 2962 2963 /* 2964 * Show a specific sysctl similar to sysctl (8). 2965 */ 2966 DB_COMMAND_FLAGS(sysctl, db_sysctl_cmd, CS_OWN) 2967 { 2968 char name[TOK_STRING_SIZE]; 2969 int error, i, t, flags; 2970 2971 /* Parse the modifiers */ 2972 t = db_read_token(); 2973 if (t == tSLASH || t == tMINUS) { 2974 t = db_read_token(); 2975 if (t != tIDENT) { 2976 db_printf("Bad modifier\n"); 2977 error = EINVAL; 2978 goto out; 2979 } 2980 db_strcpy(modif, db_tok_string); 2981 } 2982 else { 2983 db_unread_token(t); 2984 modif[0] = '\0'; 2985 } 2986 2987 flags = 0; 2988 for (i = 0; i < nitems(db_sysctl_modifs); i++) { 2989 if (strchr(modif, db_sysctl_modifs[i])) { 2990 flags |= db_sysctl_modif_values[i]; 2991 } 2992 } 2993 2994 /* Parse the sysctl names */ 2995 t = db_read_token(); 2996 if (t != tIDENT) { 2997 db_printf("Need sysctl name\n"); 2998 error = EINVAL; 2999 goto out; 3000 } 3001 3002 /* Copy the name into a temporary buffer */ 3003 db_strcpy(name, db_tok_string); 3004 3005 /* Ensure there is no trailing cruft */ 3006 t = db_read_token(); 3007 if (t != tEOL) { 3008 db_printf("Unexpected sysctl argument\n"); 3009 error = EINVAL; 3010 goto out; 3011 } 3012 3013 error = db_sysctlbyname(name, flags); 3014 if (error == ENOENT) { 3015 db_printf("unknown oid: '%s'\n", db_tok_string); 3016 goto out; 3017 } else if (error) { 3018 db_printf("%s: error: %d\n", db_tok_string, error); 3019 goto out; 3020 } 3021 3022 out: 3023 /* Ensure we eat all of our text */ 3024 db_flush_lex(); 3025 3026 if (error == EINVAL) { 3027 db_sysctl_cmd_usage(); 3028 } 3029 } 3030 3031 #endif /* DDB */ 3032