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