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