1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Karels at Berkeley Software Design, Inc. 7 * 8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD 9 * project, to make these variables more userfriendly. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 40 * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.92.2.9 2003/05/01 22:48:09 trhodes Exp $ 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/buf.h> 47 #include <sys/sysctl.h> 48 #include <sys/malloc.h> 49 #include <sys/proc.h> 50 #include <sys/priv.h> 51 #include <sys/sysproto.h> 52 #include <sys/lock.h> 53 54 #include <sys/mplock2.h> 55 56 #include <vm/vm.h> 57 #include <vm/vm_extern.h> 58 59 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 60 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 61 62 static struct lock sysctl_lkp; 63 static struct lock sysctl_ctx_lkp; 64 65 static void sysctl_lock(int type); 66 static void sysctl_unlock(void); 67 static void sysctl_ctx_lock(int type); 68 static void sysctl_ctx_unlock(void); 69 70 static int sysctl_root(SYSCTL_HANDLER_ARGS); 71 static void sysctl_register_oid_int(struct sysctl_oid *oipd); 72 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd); 73 static struct sysctl_ctx_entry* sysctl_ctx_entry_find_int 74 (struct sysctl_ctx_list *, struct sysctl_oid *oidp); 75 76 struct sysctl_oid_list sysctl__children; /* root list */ 77 78 static struct sysctl_oid * 79 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock) 80 { 81 struct sysctl_oid *oidp; 82 83 SLIST_FOREACH(oidp, list, oid_link) { 84 if (strcmp(oidp->oid_name, name) == 0) { 85 break; 86 } 87 } 88 return (oidp); 89 } 90 91 /* 92 * Initialization of the MIB tree. 93 * 94 * Order by number in each list. 95 */ 96 97 void 98 sysctl_register_oid(struct sysctl_oid *oidp) 99 { 100 sysctl_lock(LK_EXCLUSIVE); 101 sysctl_register_oid_int(oidp); 102 sysctl_unlock(); 103 } 104 105 static void 106 sysctl_register_oid_int(struct sysctl_oid *oidp) 107 { 108 struct sysctl_oid_list *parent = oidp->oid_parent; 109 struct sysctl_oid *p; 110 struct sysctl_oid *q; 111 112 /* 113 * First check if another oid with the same name already 114 * exists in the parent's list. 115 */ 116 p = sysctl_find_oidname(oidp->oid_name, parent, 0); 117 if (p != NULL) { 118 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) 119 p->oid_refcnt++; 120 else 121 kprintf("can't re-use a leaf (%s)!\n", p->oid_name); 122 return; 123 } 124 125 /* 126 * If this oid has a number OID_AUTO, give it a number which 127 * is greater than any current oid. Make sure it is at least 128 * 256 to leave space for pre-assigned oid numbers. 129 */ 130 if (oidp->oid_number == OID_AUTO) { 131 int newoid = 0x100; /* minimum AUTO oid */ 132 133 /* 134 * Adjust based on highest oid in parent list 135 */ 136 SLIST_FOREACH(p, parent, oid_link) { 137 if (newoid <= p->oid_number) 138 newoid = p->oid_number + 1; 139 } 140 oidp->oid_number = newoid; 141 } 142 143 /* 144 * Insert the oid into the parent's list in order. 145 */ 146 q = NULL; 147 SLIST_FOREACH(p, parent, oid_link) { 148 if (oidp->oid_number < p->oid_number) 149 break; 150 q = p; 151 } 152 if (q) 153 SLIST_INSERT_AFTER(q, oidp, oid_link); 154 else 155 SLIST_INSERT_HEAD(parent, oidp, oid_link); 156 } 157 158 void 159 sysctl_unregister_oid(struct sysctl_oid *oidp) 160 { 161 sysctl_lock(LK_EXCLUSIVE); 162 sysctl_unregister_oid_int(oidp); 163 sysctl_unlock(); 164 } 165 166 static void 167 sysctl_unregister_oid_int(struct sysctl_oid *oidp) 168 { 169 struct sysctl_oid *p; 170 171 if (oidp->oid_number == OID_AUTO) 172 panic("Trying to unregister OID_AUTO entry: %p", oidp); 173 174 SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 175 if (p != oidp) 176 continue; 177 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link); 178 return; 179 } 180 181 /* 182 * This can happen when a module fails to register and is 183 * being unloaded afterwards. It should not be a panic() 184 * for normal use. 185 */ 186 kprintf("%s: failed to unregister sysctl\n", __func__); 187 } 188 189 /* Initialize a new context to keep track of dynamically added sysctls. */ 190 int 191 sysctl_ctx_init(struct sysctl_ctx_list *c) 192 { 193 if (c == NULL) 194 return(EINVAL); 195 TAILQ_INIT(c); 196 return(0); 197 } 198 199 /* Free the context, and destroy all dynamic oids registered in this context */ 200 int 201 sysctl_ctx_free(struct sysctl_ctx_list *clist) 202 { 203 struct sysctl_ctx_entry *e, *e1; 204 int error; 205 206 error = 0; 207 sysctl_ctx_lock(LK_EXCLUSIVE); 208 /* 209 * First perform a "dry run" to check if it's ok to remove oids. 210 * XXX FIXME 211 * XXX This algorithm is a hack. But I don't know any 212 * XXX better solution for now... 213 */ 214 TAILQ_FOREACH(e, clist, link) { 215 error = sysctl_remove_oid(e->entry, 0, 0); 216 if (error) 217 break; 218 } 219 /* 220 * Restore deregistered entries, either from the end, 221 * or from the place where error occured. 222 * e contains the entry that was not unregistered 223 */ 224 if (error) 225 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 226 else 227 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 228 while (e1 != NULL) { 229 sysctl_register_oid(e1->entry); 230 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 231 } 232 if (error) { 233 sysctl_ctx_unlock(); 234 return(EBUSY); 235 } 236 /* Now really delete the entries */ 237 e = TAILQ_FIRST(clist); 238 while (e != NULL) { 239 e1 = TAILQ_NEXT(e, link); 240 error = sysctl_remove_oid(e->entry, 1, 0); 241 if (error) 242 panic("sysctl_remove_oid: corrupt tree, entry: %s", 243 e->entry->oid_name); 244 kfree(e, M_SYSCTLOID); 245 e = e1; 246 } 247 sysctl_ctx_unlock(); 248 return (error); 249 } 250 251 /* Add an entry to the context */ 252 struct sysctl_ctx_entry * 253 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 254 { 255 struct sysctl_ctx_entry *e; 256 257 if (clist == NULL || oidp == NULL) 258 return(NULL); 259 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 260 e->entry = oidp; 261 sysctl_ctx_lock(LK_EXCLUSIVE); 262 TAILQ_INSERT_HEAD(clist, e, link); 263 sysctl_ctx_unlock(); 264 return (e); 265 } 266 267 /* Find an entry in the context */ 268 struct sysctl_ctx_entry * 269 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 270 { 271 struct sysctl_ctx_entry *e; 272 273 if (clist == NULL || oidp == NULL) 274 return(NULL); 275 276 sysctl_ctx_lock(LK_SHARED); 277 e = sysctl_ctx_entry_find_int(clist, oidp); 278 sysctl_ctx_unlock(); 279 280 return(e); 281 } 282 283 struct sysctl_ctx_entry * 284 sysctl_ctx_entry_find_int(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 285 { 286 struct sysctl_ctx_entry *e; 287 288 KKASSERT(clist != NULL && oidp != NULL); 289 290 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) { 291 if(e->entry == oidp) 292 break; 293 } 294 295 return (e); 296 } 297 298 /* 299 * Delete an entry from the context. 300 * NOTE: this function doesn't free oidp! You have to remove it 301 * with sysctl_remove_oid(). 302 */ 303 int 304 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 305 { 306 struct sysctl_ctx_entry *e; 307 308 if (clist == NULL || oidp == NULL) 309 return (EINVAL); 310 311 sysctl_ctx_lock(LK_EXCLUSIVE); 312 e = sysctl_ctx_entry_find_int(clist, oidp); 313 if (e == NULL) { 314 sysctl_ctx_unlock(); 315 return (ENOENT); 316 } 317 TAILQ_REMOVE(clist, e, link); 318 kfree(e, M_SYSCTLOID); 319 sysctl_ctx_unlock(); 320 321 return(0); 322 } 323 324 /* 325 * Remove dynamically created sysctl trees. 326 * oidp - top of the tree to be removed 327 * del - if 0 - just deregister, otherwise free up entries as well 328 * recurse - if != 0 traverse the subtree to be deleted 329 */ 330 int 331 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 332 { 333 struct sysctl_oid *p; 334 int error; 335 336 if (oidp == NULL) 337 return(EINVAL); 338 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 339 kprintf("can't remove non-dynamic nodes!\n"); 340 return (EINVAL); 341 } 342 sysctl_lock(LK_EXCLUSIVE | LK_CANRECURSE); 343 /* 344 * WARNING: normal method to do this should be through 345 * sysctl_ctx_free(). Use recursing as the last resort 346 * method to purge your sysctl tree of leftovers... 347 * However, if some other code still references these nodes, 348 * it will panic. 349 */ 350 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 351 if (oidp->oid_refcnt == 1) { 352 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) { 353 if (!recurse) { 354 sysctl_unlock(); 355 return(ENOTEMPTY); 356 } 357 error = sysctl_remove_oid(p, del, recurse); 358 if (error) { 359 sysctl_unlock(); 360 return(error); 361 } 362 } 363 if (del) 364 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 365 } 366 } 367 if (oidp->oid_refcnt > 1 ) { 368 oidp->oid_refcnt--; 369 } else { 370 if (oidp->oid_refcnt == 0) { 371 kprintf("Warning: bad oid_refcnt=%u (%s)!\n", 372 oidp->oid_refcnt, oidp->oid_name); 373 sysctl_unlock(); 374 return(EINVAL); 375 } 376 sysctl_unregister_oid_int(oidp); 377 if (del) { 378 if (oidp->oid_descr) 379 kfree(__DECONST(char *,oidp->oid_descr), 380 M_SYSCTLOID); 381 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID); 382 kfree(oidp, M_SYSCTLOID); 383 } 384 } 385 sysctl_unlock(); 386 return(0); 387 } 388 389 /* 390 * Create new sysctls at run time. 391 * clist may point to a valid context initialized with sysctl_ctx_init(). 392 */ 393 struct sysctl_oid * 394 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 395 int number, const char *name, int kind, void *arg1, int arg2, 396 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 397 { 398 struct sysctl_oid *oidp; 399 ssize_t len; 400 char *newname; 401 402 /* You have to hook up somewhere.. */ 403 if (parent == NULL) 404 return(NULL); 405 sysctl_lock(LK_EXCLUSIVE); 406 /* Check if the node already exists, otherwise create it */ 407 oidp = sysctl_find_oidname(name, parent, 0); 408 if (oidp != NULL) { 409 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 410 oidp->oid_refcnt++; 411 /* Update the context */ 412 if (clist != NULL) 413 sysctl_ctx_entry_add(clist, oidp); 414 sysctl_unlock(); 415 return (oidp); 416 } else { 417 kprintf("can't re-use a leaf (%s)!\n", name); 418 sysctl_unlock(); 419 return (NULL); 420 } 421 } 422 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO); 423 oidp->oid_parent = parent; 424 SLIST_NEXT(oidp, oid_link) = NULL; 425 oidp->oid_number = number; 426 oidp->oid_refcnt = 1; 427 len = strlen(name); 428 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK); 429 bcopy(name, newname, len + 1); 430 newname[len] = '\0'; 431 oidp->oid_name = newname; 432 oidp->oid_handler = handler; 433 oidp->oid_kind = CTLFLAG_DYN | kind; 434 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 435 struct sysctl_oid_list *children; 436 437 /* Allocate space for children */ 438 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK); 439 SYSCTL_SET_CHILDREN(oidp, children); 440 SLIST_INIT(children); 441 } else { 442 oidp->oid_arg1 = arg1; 443 oidp->oid_arg2 = arg2; 444 } 445 oidp->oid_fmt = fmt; 446 if (descr) { 447 int len = strlen(descr) + 1; 448 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK); 449 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr); 450 }; 451 /* Update the context, if used */ 452 if (clist != NULL) 453 sysctl_ctx_entry_add(clist, oidp); 454 /* Register this oid */ 455 sysctl_register_oid_int(oidp); 456 sysctl_unlock(); 457 return (oidp); 458 } 459 460 /* 461 * Register the kernel's oids on startup. 462 */ 463 SET_DECLARE(sysctl_set, struct sysctl_oid); 464 465 static void 466 sysctl_register_all(void *arg) 467 { 468 struct sysctl_oid **oidp; 469 470 lockinit(&sysctl_lkp, "sysctl", 0, 0); 471 lockinit(&sysctl_ctx_lkp, "sysctl ctx", 0, 0); 472 SET_FOREACH(oidp, sysctl_set) 473 sysctl_register_oid_int(*oidp); 474 } 475 476 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0); 477 478 /* 479 * "Staff-functions" 480 * 481 * These functions implement a presently undocumented interface 482 * used by the sysctl program to walk the tree, and get the type 483 * so it can print the value. 484 * This interface is under work and consideration, and should probably 485 * be killed with a big axe by the first person who can find the time. 486 * (be aware though, that the proper interface isn't as obvious as it 487 * may seem, there are various conflicting requirements. 488 * 489 * {0,0} kprintf the entire MIB-tree. 490 * {0,1,...} return the name of the "..." OID. 491 * {0,2,...} return the next OID. 492 * {0,3} return the OID of the name in "new" 493 * {0,4,...} return the kind & format info for the "..." OID. 494 */ 495 496 static void 497 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 498 { 499 int k; 500 struct sysctl_oid *oidp; 501 502 sysctl_lock(LK_SHARED); 503 SLIST_FOREACH(oidp, l, oid_link) { 504 505 for (k=0; k<i; k++) 506 kprintf(" "); 507 508 kprintf("%d %s ", oidp->oid_number, oidp->oid_name); 509 510 kprintf("%c%c", 511 oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 512 oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 513 514 if (oidp->oid_handler) 515 kprintf(" *Handler"); 516 517 switch (oidp->oid_kind & CTLTYPE) { 518 case CTLTYPE_NODE: 519 kprintf(" Node\n"); 520 if (!oidp->oid_handler) { 521 sysctl_sysctl_debug_dump_node( 522 oidp->oid_arg1, i+2); 523 } 524 break; 525 case CTLTYPE_INT: kprintf(" Int\n"); break; 526 case CTLTYPE_STRING: kprintf(" String\n"); break; 527 case CTLTYPE_QUAD: kprintf(" Quad\n"); break; 528 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break; 529 default: kprintf("\n"); 530 } 531 532 } 533 sysctl_unlock(); 534 } 535 536 static int 537 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 538 { 539 int error; 540 541 error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 542 if (error) 543 return error; 544 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 545 return ENOENT; 546 } 547 548 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 549 0, 0, sysctl_sysctl_debug, "-", ""); 550 551 static int 552 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 553 { 554 int *name = (int *) arg1; 555 u_int namelen = arg2; 556 int error = 0; 557 struct sysctl_oid *oid; 558 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 559 char buf[16]; 560 561 sysctl_lock(LK_SHARED); 562 while (namelen) { 563 if (!lsp) { 564 ksnprintf(buf, sizeof(buf), "%d", *name); 565 if (req->oldidx) 566 error = SYSCTL_OUT(req, ".", 1); 567 if (!error) 568 error = SYSCTL_OUT(req, buf, strlen(buf)); 569 if (error) { 570 sysctl_unlock(); 571 return (error); 572 } 573 namelen--; 574 name++; 575 continue; 576 } 577 lsp2 = NULL; 578 SLIST_FOREACH(oid, lsp, oid_link) { 579 if (oid->oid_number != *name) 580 continue; 581 582 if (req->oldidx) 583 error = SYSCTL_OUT(req, ".", 1); 584 if (!error) 585 error = SYSCTL_OUT(req, oid->oid_name, 586 strlen(oid->oid_name)); 587 if (error) { 588 sysctl_unlock(); 589 return (error); 590 } 591 592 namelen--; 593 name++; 594 595 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 596 break; 597 598 if (oid->oid_handler) 599 break; 600 601 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; 602 break; 603 } 604 lsp = lsp2; 605 } 606 sysctl_unlock(); 607 return (SYSCTL_OUT(req, "", 1)); 608 } 609 610 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 611 612 static int 613 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 614 int *next, int *len, int level, struct sysctl_oid **oidpp) 615 { 616 struct sysctl_oid *oidp; 617 618 *len = level; 619 sysctl_lock(LK_SHARED); 620 SLIST_FOREACH(oidp, lsp, oid_link) { 621 *next = oidp->oid_number; 622 *oidpp = oidp; 623 624 if (!namelen) { 625 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) { 626 sysctl_unlock(); 627 return 0; 628 } 629 if (oidp->oid_handler) { 630 /* We really should call the handler here...*/ 631 sysctl_unlock(); 632 return 0; 633 } 634 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 635 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 636 len, level+1, oidpp)) { 637 sysctl_unlock(); 638 return 0; 639 } 640 goto emptynode; 641 } 642 643 if (oidp->oid_number < *name) 644 continue; 645 646 if (oidp->oid_number > *name) { 647 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) { 648 sysctl_unlock(); 649 return 0; 650 } 651 if (oidp->oid_handler) { 652 sysctl_unlock(); 653 return 0; 654 } 655 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 656 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 657 next+1, len, level+1, oidpp)) { 658 sysctl_unlock(); 659 return (0); 660 } 661 goto next; 662 } 663 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 664 continue; 665 666 if (oidp->oid_handler) 667 continue; 668 669 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 670 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 671 len, level+1, oidpp)) { 672 sysctl_unlock(); 673 return (0); 674 } 675 next: 676 namelen = 1; 677 *len = level; 678 emptynode: 679 *len = level; 680 } 681 sysctl_unlock(); 682 return 1; 683 } 684 685 static int 686 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 687 { 688 int *name = (int *) arg1; 689 u_int namelen = arg2; 690 int i, j, error; 691 struct sysctl_oid *oid; 692 struct sysctl_oid_list *lsp = &sysctl__children; 693 int newoid[CTL_MAXNAME]; 694 695 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 696 if (i) 697 return ENOENT; 698 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 699 return (error); 700 } 701 702 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 703 704 static int 705 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp) 706 { 707 int i; 708 struct sysctl_oid *oidp; 709 struct sysctl_oid_list *lsp = &sysctl__children; 710 char *p; 711 712 if (!*name) 713 return ENOENT; 714 715 p = name + strlen(name) - 1 ; 716 if (*p == '.') 717 *p = '\0'; 718 719 *len = 0; 720 721 for (p = name; *p && *p != '.'; p++) 722 ; 723 i = *p; 724 if (i == '.') 725 *p = '\0'; 726 727 sysctl_lock(LK_SHARED); 728 oidp = SLIST_FIRST(lsp); 729 730 while (oidp && *len < CTL_MAXNAME) { 731 if (strcmp(name, oidp->oid_name)) { 732 oidp = SLIST_NEXT(oidp, oid_link); 733 continue; 734 } 735 *oid++ = oidp->oid_number; 736 (*len)++; 737 738 if (!i) { 739 if (oidpp) 740 *oidpp = oidp; 741 sysctl_unlock(); 742 return (0); 743 } 744 745 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 746 break; 747 748 if (oidp->oid_handler) 749 break; 750 751 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 752 oidp = SLIST_FIRST(lsp); 753 name = p+1; 754 for (p = name; *p && *p != '.'; p++) 755 ; 756 i = *p; 757 if (i == '.') 758 *p = '\0'; 759 } 760 sysctl_unlock(); 761 return ENOENT; 762 } 763 764 static int 765 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 766 { 767 char *p; 768 int error, oid[CTL_MAXNAME], len; 769 struct sysctl_oid *op = NULL; 770 771 if (!req->newlen) 772 return ENOENT; 773 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 774 return (ENAMETOOLONG); 775 776 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK); 777 778 error = SYSCTL_IN(req, p, req->newlen); 779 if (error) { 780 kfree(p, M_SYSCTL); 781 return (error); 782 } 783 784 p [req->newlen] = '\0'; 785 786 error = name2oid(p, oid, &len, &op); 787 788 kfree(p, M_SYSCTL); 789 790 if (error) 791 return (error); 792 793 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 794 return (error); 795 } 796 797 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 798 sysctl_sysctl_name2oid, "I", ""); 799 800 static int 801 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 802 { 803 struct sysctl_oid *oid; 804 int error; 805 806 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 807 if (error) 808 return (error); 809 810 if (!oid->oid_fmt) 811 return (ENOENT); 812 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 813 if (error) 814 return (error); 815 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 816 return (error); 817 } 818 819 820 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, ""); 821 822 static int 823 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 824 { 825 struct sysctl_oid *oid; 826 int error; 827 828 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 829 if (error) 830 return (error); 831 832 if (!oid->oid_descr) 833 return (ENOENT); 834 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 835 return (error); 836 } 837 838 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, ""); 839 840 /* 841 * Default "handler" functions. 842 */ 843 844 /* 845 * Handle an int, signed or unsigned. 846 * Two cases: 847 * a variable: point arg1 at it. 848 * a constant: pass it in arg2. 849 */ 850 851 int 852 sysctl_handle_int(SYSCTL_HANDLER_ARGS) 853 { 854 int error = 0; 855 856 if (arg1) 857 error = SYSCTL_OUT(req, arg1, sizeof(int)); 858 else 859 error = SYSCTL_OUT(req, &arg2, sizeof(int)); 860 861 if (error || !req->newptr) 862 return (error); 863 864 if (!arg1) 865 error = EPERM; 866 else 867 error = SYSCTL_IN(req, arg1, sizeof(int)); 868 return (error); 869 } 870 871 /* 872 * Handle a long, signed or unsigned. arg1 points to it. 873 */ 874 875 int 876 sysctl_handle_long(SYSCTL_HANDLER_ARGS) 877 { 878 int error = 0; 879 880 if (!arg1) 881 return (EINVAL); 882 error = SYSCTL_OUT(req, arg1, sizeof(long)); 883 884 if (error || !req->newptr) 885 return (error); 886 887 error = SYSCTL_IN(req, arg1, sizeof(long)); 888 return (error); 889 } 890 891 /* 892 * Handle a quad, signed or unsigned. arg1 points to it. 893 */ 894 895 int 896 sysctl_handle_quad(SYSCTL_HANDLER_ARGS) 897 { 898 int error = 0; 899 900 if (!arg1) 901 return (EINVAL); 902 error = SYSCTL_OUT(req, arg1, sizeof(quad_t)); 903 904 if (error || !req->newptr) 905 return (error); 906 907 error = SYSCTL_IN(req, arg1, sizeof(quad_t)); 908 return (error); 909 } 910 911 /* 912 * Handle our generic '\0' terminated 'C' string. 913 * Two cases: 914 * a variable string: point arg1 at it, arg2 is max length. 915 * a constant string: point arg1 at it, arg2 is zero. 916 */ 917 918 int 919 sysctl_handle_string(SYSCTL_HANDLER_ARGS) 920 { 921 int error=0; 922 923 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1); 924 925 if (error || !req->newptr) 926 return (error); 927 928 if ((req->newlen - req->newidx) >= arg2) { 929 error = EINVAL; 930 } else { 931 arg2 = (req->newlen - req->newidx); 932 error = SYSCTL_IN(req, arg1, arg2); 933 ((char *)arg1)[arg2] = '\0'; 934 } 935 936 return (error); 937 } 938 939 /* 940 * Handle any kind of opaque data. 941 * arg1 points to it, arg2 is the size. 942 */ 943 944 int 945 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 946 { 947 int error; 948 949 error = SYSCTL_OUT(req, arg1, arg2); 950 951 if (error || !req->newptr) 952 return (error); 953 954 error = SYSCTL_IN(req, arg1, arg2); 955 956 return (error); 957 } 958 959 /* 960 * Transfer functions to/from kernel space. 961 * XXX: rather untested at this point 962 */ 963 static int 964 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 965 { 966 size_t i = 0; 967 968 if (req->oldptr) { 969 i = l; 970 if (i > req->oldlen - req->oldidx) 971 i = req->oldlen - req->oldidx; 972 if (i > 0) 973 bcopy(p, (char *)req->oldptr + req->oldidx, i); 974 } 975 req->oldidx += l; 976 if (req->oldptr && i != l) 977 return (ENOMEM); 978 return (0); 979 } 980 981 static int 982 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 983 { 984 985 if (!req->newptr) 986 return 0; 987 if (req->newlen - req->newidx < l) 988 return (EINVAL); 989 bcopy((char *)req->newptr + req->newidx, p, l); 990 req->newidx += l; 991 return (0); 992 } 993 994 int 995 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval) 996 { 997 int error = 0; 998 struct sysctl_req req; 999 1000 bzero(&req, sizeof req); 1001 1002 req.td = curthread; 1003 1004 if (oldlenp) { 1005 req.oldlen = *oldlenp; 1006 } 1007 1008 if (old) { 1009 req.oldptr = old; 1010 } 1011 1012 if (new != NULL) { 1013 req.newlen = newlen; 1014 req.newptr = new; 1015 } 1016 1017 req.oldfunc = sysctl_old_kernel; 1018 req.newfunc = sysctl_new_kernel; 1019 #if 0 1020 req.lock = 1; 1021 #endif 1022 1023 sysctl_lock(LK_SHARED); 1024 1025 error = sysctl_root(0, name, namelen, &req); 1026 1027 #if 0 1028 if (req.lock == 2) 1029 vsunlock(req.oldptr, req.oldlen); 1030 #endif 1031 1032 sysctl_unlock(); 1033 1034 if (error && error != ENOMEM) 1035 return (error); 1036 1037 if (retval) { 1038 if (req.oldptr && req.oldidx > req.oldlen) 1039 *retval = req.oldlen; 1040 else 1041 *retval = req.oldidx; 1042 } 1043 return (error); 1044 } 1045 1046 int 1047 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp, 1048 void *new, size_t newlen, size_t *retval) 1049 { 1050 int oid[CTL_MAXNAME]; 1051 size_t oidlen, plen; 1052 int error; 1053 1054 oid[0] = 0; /* sysctl internal magic */ 1055 oid[1] = 3; /* name2oid */ 1056 oidlen = sizeof(oid); 1057 1058 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name, 1059 strlen(name), &plen); 1060 if (error) 1061 return (error); 1062 1063 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp, 1064 new, newlen, retval); 1065 return (error); 1066 } 1067 1068 /* 1069 * Transfer function to/from user space. 1070 */ 1071 static int 1072 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 1073 { 1074 int error = 0; 1075 size_t i = 0; 1076 1077 #if 0 1078 if (req->lock == 1 && req->oldptr) { 1079 vslock(req->oldptr, req->oldlen); 1080 req->lock = 2; 1081 } 1082 #endif 1083 if (req->oldptr) { 1084 i = l; 1085 if (i > req->oldlen - req->oldidx) 1086 i = req->oldlen - req->oldidx; 1087 if (i > 0) 1088 error = copyout(p, (char *)req->oldptr + req->oldidx, 1089 i); 1090 } 1091 req->oldidx += l; 1092 if (error) 1093 return (error); 1094 if (req->oldptr && i < l) 1095 return (ENOMEM); 1096 return (0); 1097 } 1098 1099 static int 1100 sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 1101 { 1102 int error; 1103 1104 if (!req->newptr) 1105 return 0; 1106 if (req->newlen - req->newidx < l) 1107 return (EINVAL); 1108 error = copyin((char *)req->newptr + req->newidx, p, l); 1109 req->newidx += l; 1110 return (error); 1111 } 1112 1113 int 1114 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 1115 int *nindx, struct sysctl_req *req) 1116 { 1117 struct sysctl_oid *oid; 1118 int indx; 1119 1120 sysctl_lock(LK_SHARED); 1121 oid = SLIST_FIRST(&sysctl__children); 1122 indx = 0; 1123 while (oid && indx < CTL_MAXNAME) { 1124 if (oid->oid_number == name[indx]) { 1125 indx++; 1126 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1127 if (oid->oid_handler != NULL || 1128 indx == namelen) { 1129 *noid = oid; 1130 if (nindx != NULL) 1131 *nindx = indx; 1132 sysctl_unlock(); 1133 return (0); 1134 } 1135 oid = SLIST_FIRST( 1136 (struct sysctl_oid_list *)oid->oid_arg1); 1137 } else if (indx == namelen) { 1138 *noid = oid; 1139 if (nindx != NULL) 1140 *nindx = indx; 1141 sysctl_unlock(); 1142 return (0); 1143 } else { 1144 sysctl_unlock(); 1145 return (ENOTDIR); 1146 } 1147 } else { 1148 oid = SLIST_NEXT(oid, oid_link); 1149 } 1150 } 1151 sysctl_unlock(); 1152 return (ENOENT); 1153 } 1154 1155 /* 1156 * Traverse our tree, and find the right node, execute whatever it points 1157 * to, and return the resulting error code. 1158 */ 1159 1160 int 1161 sysctl_root(SYSCTL_HANDLER_ARGS) 1162 { 1163 struct thread *td = req->td; 1164 struct proc *p = td ? td->td_proc : NULL; 1165 struct sysctl_oid *oid; 1166 int error, indx; 1167 1168 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1169 if (error) 1170 return (error); 1171 1172 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1173 /* 1174 * You can't call a sysctl when it's a node, but has 1175 * no handler. Inform the user that it's a node. 1176 * The indx may or may not be the same as namelen. 1177 */ 1178 if (oid->oid_handler == NULL) 1179 return (EISDIR); 1180 } 1181 1182 /* If writing isn't allowed */ 1183 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) || 1184 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0))) 1185 return (EPERM); 1186 1187 /* Most likely only root can write */ 1188 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p && 1189 (error = priv_check_cred(td->td_ucred, 1190 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL : 1191 PRIV_SYSCTL_WRITE, 0))) 1192 return (error); 1193 1194 if (!oid->oid_handler) 1195 return EINVAL; 1196 1197 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) 1198 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx, 1199 req); 1200 else 1201 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2, 1202 req); 1203 return (error); 1204 } 1205 1206 /* 1207 * MPALMOSTSAFE 1208 */ 1209 int 1210 sys___sysctl(struct sysctl_args *uap) 1211 { 1212 int error, i, name[CTL_MAXNAME]; 1213 size_t j; 1214 1215 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1216 return (EINVAL); 1217 1218 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 1219 if (error) 1220 return (error); 1221 1222 get_mplock(); 1223 error = userland_sysctl(name, uap->namelen, 1224 uap->old, uap->oldlenp, 0, 1225 uap->new, uap->newlen, &j); 1226 rel_mplock(); 1227 if (error && error != ENOMEM) 1228 return (error); 1229 if (uap->oldlenp) { 1230 i = copyout(&j, uap->oldlenp, sizeof(j)); 1231 if (i) 1232 return (i); 1233 } 1234 return (error); 1235 } 1236 1237 /* 1238 * This is used from various compatibility syscalls too. That's why name 1239 * must be in kernel space. 1240 */ 1241 int 1242 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval) 1243 { 1244 int error = 0; 1245 struct sysctl_req req, req2; 1246 1247 bzero(&req, sizeof req); 1248 1249 if (oldlenp) { 1250 if (inkernel) { 1251 req.oldlen = *oldlenp; 1252 } else { 1253 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 1254 if (error) 1255 return (error); 1256 } 1257 } 1258 1259 if (old) { 1260 if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 1261 return (EFAULT); 1262 req.oldptr= old; 1263 } 1264 1265 if (new != NULL) { 1266 if (!useracc(new, newlen, VM_PROT_READ)) 1267 return (EFAULT); 1268 req.newlen = newlen; 1269 req.newptr = new; 1270 } 1271 1272 req.oldfunc = sysctl_old_user; 1273 req.newfunc = sysctl_new_user; 1274 #if 0 1275 req.lock = 1; 1276 #endif 1277 req.td = curthread; 1278 1279 sysctl_lock(LK_SHARED); 1280 1281 do { 1282 req2 = req; 1283 error = sysctl_root(0, name, namelen, &req2); 1284 } while (error == EAGAIN); 1285 1286 req = req2; 1287 #if 0 1288 if (req.lock == 2) 1289 vsunlock(req.oldptr, req.oldlen); 1290 #endif 1291 1292 sysctl_unlock(); 1293 1294 if (error && error != ENOMEM) 1295 return (error); 1296 1297 if (retval) { 1298 if (req.oldptr && req.oldidx > req.oldlen) 1299 *retval = req.oldlen; 1300 else 1301 *retval = req.oldidx; 1302 } 1303 return (error); 1304 } 1305 1306 static void 1307 sysctl_lock(int flag) 1308 { 1309 lockmgr(&sysctl_lkp, flag); 1310 } 1311 1312 static void 1313 sysctl_unlock(void) 1314 { 1315 lockmgr(&sysctl_lkp, LK_RELEASE); 1316 } 1317 1318 static void 1319 sysctl_ctx_lock(int flag) 1320 { 1321 lockmgr(&sysctl_ctx_lkp, flag); 1322 } 1323 1324 static void 1325 sysctl_ctx_unlock(void) 1326 { 1327 lockmgr(&sysctl_ctx_lkp, LK_RELEASE); 1328 } 1329 1330 int 1331 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 1332 { 1333 int error, value; 1334 1335 value = *(int *)arg1; 1336 error = sysctl_handle_int(oidp, &value, 0, req); 1337 if (error || !req->newptr) 1338 return (error); 1339 if (value < low || value > high) 1340 return (EINVAL); 1341 *(int *)arg1 = value; 1342 return (0); 1343 } 1344