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