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