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