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