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