kern_sysctl.c revision 71999
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 71999 2001-02-04 13:13:25Z phk $ 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 (i > req->oldlen - req->oldidx) 821 i = req->oldlen - req->oldidx; 822 if (i > 0) 823 bcopy(p, (char *)req->oldptr + req->oldidx, i); 824 } 825 req->oldidx += l; 826 if (req->oldptr && i != l) 827 return (ENOMEM); 828 return (0); 829} 830 831static int 832sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 833{ 834 if (!req->newptr) 835 return 0; 836 if (req->newlen - req->newidx < l) 837 return (EINVAL); 838 bcopy((char *)req->newptr + req->newidx, p, l); 839 req->newidx += l; 840 return (0); 841} 842 843int 844kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval) 845{ 846 int error = 0; 847 struct sysctl_req req; 848 849 bzero(&req, sizeof req); 850 851 req.p = p; 852 853 if (oldlenp) { 854 req.oldlen = *oldlenp; 855 } 856 857 if (old) { 858 req.oldptr= old; 859 } 860 861 if (newlen) { 862 req.newlen = newlen; 863 req.newptr = new; 864 } 865 866 req.oldfunc = sysctl_old_kernel; 867 req.newfunc = sysctl_new_kernel; 868 req.lock = 1; 869 870 /* XXX this should probably be done in a general way */ 871 while (memlock.sl_lock) { 872 memlock.sl_want = 1; 873 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 874 memlock.sl_locked++; 875 } 876 memlock.sl_lock = 1; 877 878 error = sysctl_root(0, name, namelen, &req); 879 880 if (req.lock == 2) 881 vsunlock(req.oldptr, req.oldlen); 882 883 memlock.sl_lock = 0; 884 885 if (memlock.sl_want) { 886 memlock.sl_want = 0; 887 wakeup((caddr_t)&memlock); 888 } 889 890 if (error && error != ENOMEM) 891 return (error); 892 893 if (retval) { 894 if (req.oldptr && req.oldidx > req.oldlen) 895 *retval = req.oldlen; 896 else 897 *retval = req.oldidx; 898 } 899 return (error); 900} 901 902/* 903 * Transfer function to/from user space. 904 */ 905static int 906sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 907{ 908 int error = 0; 909 size_t i = 0; 910 911 if (req->lock == 1 && req->oldptr) { 912 vslock(req->oldptr, req->oldlen); 913 req->lock = 2; 914 } 915 if (req->oldptr) { 916 i = l; 917 if (i > req->oldlen - req->oldidx) 918 i = req->oldlen - req->oldidx; 919 if (i > 0) 920 error = copyout(p, (char *)req->oldptr + req->oldidx, 921 i); 922 } 923 req->oldidx += l; 924 if (error) 925 return (error); 926 if (req->oldptr && i < l) 927 return (ENOMEM); 928 return (0); 929} 930 931static int 932sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 933{ 934 int error; 935 936 if (!req->newptr) 937 return 0; 938 if (req->newlen - req->newidx < l) 939 return (EINVAL); 940 error = copyin((char *)req->newptr + req->newidx, p, l); 941 req->newidx += l; 942 return (error); 943} 944 945int 946sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 947 int *nindx, struct sysctl_req *req) 948{ 949 struct sysctl_oid *oid; 950 int indx; 951 952 oid = SLIST_FIRST(&sysctl__children); 953 indx = 0; 954 while (oid && indx < CTL_MAXNAME) { 955 if (oid->oid_number == name[indx]) { 956 indx++; 957 if (oid->oid_kind & CTLFLAG_NOLOCK) 958 req->lock = 0; 959 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 960 if (oid->oid_handler != NULL || 961 indx == namelen) { 962 *noid = oid; 963 if (nindx != NULL) 964 *nindx = indx; 965 return (0); 966 } 967 oid = SLIST_FIRST( 968 (struct sysctl_oid_list *)oid->oid_arg1); 969 } else if (indx == namelen) { 970 *noid = oid; 971 if (nindx != NULL) 972 *nindx = indx; 973 return (0); 974 } else { 975 return (ENOTDIR); 976 } 977 } else { 978 oid = SLIST_NEXT(oid, oid_link); 979 } 980 } 981 return (ENOENT); 982} 983 984/* 985 * Traverse our tree, and find the right node, execute whatever it points 986 * to, and return the resulting error code. 987 */ 988 989int 990sysctl_root(SYSCTL_HANDLER_ARGS) 991{ 992 struct sysctl_oid *oid; 993 int error, indx; 994 995 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 996 if (error) 997 return (error); 998 999 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1000 /* 1001 * You can't call a sysctl when it's a node, but has 1002 * no handler. Inform the user that it's a node. 1003 * The indx may or may not be the same as namelen. 1004 */ 1005 if (oid->oid_handler == NULL) 1006 return (EISDIR); 1007 } 1008 1009 /* If writing isn't allowed */ 1010 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) || 1011 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0))) 1012 return (EPERM); 1013 1014 /* Most likely only root can write */ 1015 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && 1016 req->newptr && req->p && 1017 (error = suser_xxx(0, req->p, 1018 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0))) 1019 return (error); 1020 1021 if (!oid->oid_handler) 1022 return EINVAL; 1023 1024 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) 1025 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx, 1026 req); 1027 else 1028 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2, 1029 req); 1030 return (error); 1031} 1032 1033#ifndef _SYS_SYSPROTO_H_ 1034struct sysctl_args { 1035 int *name; 1036 u_int namelen; 1037 void *old; 1038 size_t *oldlenp; 1039 void *new; 1040 size_t newlen; 1041}; 1042#endif 1043 1044int 1045__sysctl(struct proc *p, struct sysctl_args *uap) 1046{ 1047 int error, i, name[CTL_MAXNAME]; 1048 size_t j; 1049 1050 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1051 return (EINVAL); 1052 1053 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 1054 if (error) 1055 return (error); 1056 1057 error = userland_sysctl(p, name, uap->namelen, 1058 uap->old, uap->oldlenp, 0, 1059 uap->new, uap->newlen, &j); 1060 if (error && error != ENOMEM) 1061 return (error); 1062 if (uap->oldlenp) { 1063 i = copyout(&j, uap->oldlenp, sizeof(j)); 1064 if (i) 1065 return (i); 1066 } 1067 return (error); 1068} 1069 1070/* 1071 * This is used from various compatibility syscalls too. That's why name 1072 * must be in kernel space. 1073 */ 1074int 1075userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval) 1076{ 1077 int error = 0; 1078 struct sysctl_req req, req2; 1079 1080 bzero(&req, sizeof req); 1081 1082 req.p = p; 1083 1084 if (oldlenp) { 1085 if (inkernel) { 1086 req.oldlen = *oldlenp; 1087 } else { 1088 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 1089 if (error) 1090 return (error); 1091 } 1092 } 1093 1094 if (old) { 1095 if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 1096 return (EFAULT); 1097 req.oldptr= old; 1098 } 1099 1100 if (newlen) { 1101 if (!useracc(new, req.newlen, VM_PROT_READ)) 1102 return (EFAULT); 1103 req.newlen = newlen; 1104 req.newptr = new; 1105 } 1106 1107 req.oldfunc = sysctl_old_user; 1108 req.newfunc = sysctl_new_user; 1109 req.lock = 1; 1110 1111 /* XXX this should probably be done in a general way */ 1112 while (memlock.sl_lock) { 1113 memlock.sl_want = 1; 1114 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 1115 memlock.sl_locked++; 1116 } 1117 memlock.sl_lock = 1; 1118 1119 do { 1120 req2 = req; 1121 error = sysctl_root(0, name, namelen, &req2); 1122 } while (error == EAGAIN); 1123 1124 req = req2; 1125 if (req.lock == 2) 1126 vsunlock(req.oldptr, req.oldlen); 1127 1128 memlock.sl_lock = 0; 1129 1130 if (memlock.sl_want) { 1131 memlock.sl_want = 0; 1132 wakeup((caddr_t)&memlock); 1133 } 1134 1135 if (error && error != ENOMEM) 1136 return (error); 1137 1138 if (retval) { 1139 if (req.oldptr && req.oldidx > req.oldlen) 1140 *retval = req.oldlen; 1141 else 1142 *retval = req.oldidx; 1143 } 1144 return (error); 1145} 1146 1147#ifdef COMPAT_43 1148#include <sys/socket.h> 1149#include <vm/vm_param.h> 1150 1151#define KINFO_PROC (0<<8) 1152#define KINFO_RT (1<<8) 1153#define KINFO_VNODE (2<<8) 1154#define KINFO_FILE (3<<8) 1155#define KINFO_METER (4<<8) 1156#define KINFO_LOADAVG (5<<8) 1157#define KINFO_CLOCKRATE (6<<8) 1158 1159/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 1160#define KINFO_BSDI_SYSINFO (101<<8) 1161 1162/* 1163 * XXX this is bloat, but I hope it's better here than on the potentially 1164 * limited kernel stack... -Peter 1165 */ 1166 1167static struct { 1168 int bsdi_machine; /* "i386" on BSD/386 */ 1169/* ^^^ this is an offset to the string, relative to the struct start */ 1170 char *pad0; 1171 long pad1; 1172 long pad2; 1173 long pad3; 1174 u_long pad4; 1175 u_long pad5; 1176 u_long pad6; 1177 1178 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 1179 int bsdi_osrelease; /* "1.1" on BSD/386 */ 1180 long pad7; 1181 long pad8; 1182 char *pad9; 1183 1184 long pad10; 1185 long pad11; 1186 int pad12; 1187 long pad13; 1188 quad_t pad14; 1189 long pad15; 1190 1191 struct timeval pad16; 1192 /* we dont set this, because BSDI's uname used gethostname() instead */ 1193 int bsdi_hostname; /* hostname on BSD/386 */ 1194 1195 /* the actual string data is appended here */ 1196 1197} bsdi_si; 1198/* 1199 * this data is appended to the end of the bsdi_si structure during copyout. 1200 * The "char *" offsets are relative to the base of the bsdi_si struct. 1201 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 1202 * should not exceed the length of the buffer here... (or else!! :-) 1203 */ 1204static char bsdi_strings[80]; /* It had better be less than this! */ 1205 1206#ifndef _SYS_SYSPROTO_H_ 1207struct getkerninfo_args { 1208 int op; 1209 char *where; 1210 size_t *size; 1211 int arg; 1212}; 1213#endif 1214 1215int 1216ogetkerninfo(struct proc *p, struct getkerninfo_args *uap) 1217{ 1218 int error, name[6]; 1219 size_t size; 1220 1221 switch (uap->op & 0xff00) { 1222 1223 case KINFO_RT: 1224 name[0] = CTL_NET; 1225 name[1] = PF_ROUTE; 1226 name[2] = 0; 1227 name[3] = (uap->op & 0xff0000) >> 16; 1228 name[4] = uap->op & 0xff; 1229 name[5] = uap->arg; 1230 error = userland_sysctl(p, name, 6, uap->where, uap->size, 1231 0, 0, 0, &size); 1232 break; 1233 1234 case KINFO_VNODE: 1235 name[0] = CTL_KERN; 1236 name[1] = KERN_VNODE; 1237 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1238 0, 0, 0, &size); 1239 break; 1240 1241 case KINFO_PROC: 1242 name[0] = CTL_KERN; 1243 name[1] = KERN_PROC; 1244 name[2] = uap->op & 0xff; 1245 name[3] = uap->arg; 1246 error = userland_sysctl(p, name, 4, uap->where, uap->size, 1247 0, 0, 0, &size); 1248 break; 1249 1250 case KINFO_FILE: 1251 name[0] = CTL_KERN; 1252 name[1] = KERN_FILE; 1253 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1254 0, 0, 0, &size); 1255 break; 1256 1257 case KINFO_METER: 1258 name[0] = CTL_VM; 1259 name[1] = VM_METER; 1260 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1261 0, 0, 0, &size); 1262 break; 1263 1264 case KINFO_LOADAVG: 1265 name[0] = CTL_VM; 1266 name[1] = VM_LOADAVG; 1267 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1268 0, 0, 0, &size); 1269 break; 1270 1271 case KINFO_CLOCKRATE: 1272 name[0] = CTL_KERN; 1273 name[1] = KERN_CLOCKRATE; 1274 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1275 0, 0, 0, &size); 1276 break; 1277 1278 case KINFO_BSDI_SYSINFO: { 1279 /* 1280 * this is pretty crude, but it's just enough for uname() 1281 * from BSDI's 1.x libc to work. 1282 * 1283 * In particular, it doesn't return the same results when 1284 * the supplied buffer is too small. BSDI's version apparently 1285 * will return the amount copied, and set the *size to how 1286 * much was needed. The emulation framework here isn't capable 1287 * of that, so we just set both to the amount copied. 1288 * BSDI's 2.x product apparently fails with ENOMEM in this 1289 * scenario. 1290 */ 1291 1292 u_int needed; 1293 u_int left; 1294 char *s; 1295 1296 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1297 bzero(bsdi_strings, sizeof(bsdi_strings)); 1298 1299 s = bsdi_strings; 1300 1301 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1302 strcpy(s, ostype); 1303 s += strlen(s) + 1; 1304 1305 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1306 strcpy(s, osrelease); 1307 s += strlen(s) + 1; 1308 1309 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1310 strcpy(s, machine); 1311 s += strlen(s) + 1; 1312 1313 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1314 1315 if (uap->where == NULL) { 1316 /* process is asking how much buffer to supply.. */ 1317 size = needed; 1318 error = 0; 1319 break; 1320 } 1321 1322 1323 /* if too much buffer supplied, trim it down */ 1324 if (size > needed) 1325 size = needed; 1326 1327 /* how much of the buffer is remaining */ 1328 left = size; 1329 1330 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1331 break; 1332 1333 /* is there any point in continuing? */ 1334 if (left > sizeof(bsdi_si)) { 1335 left -= sizeof(bsdi_si); 1336 error = copyout(&bsdi_strings, 1337 uap->where + sizeof(bsdi_si), left); 1338 } 1339 break; 1340 } 1341 1342 default: 1343 return (EOPNOTSUPP); 1344 } 1345 if (error) 1346 return (error); 1347 p->p_retval[0] = size; 1348 if (uap->size) 1349 error = copyout((caddr_t)&size, (caddr_t)uap->size, 1350 sizeof(size)); 1351 return (error); 1352} 1353#endif /* COMPAT_43 */ 1354