kern_sysctl.c revision 185983
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 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 36 */ 37 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 185983 2008-12-12 12:06:28Z kib $"); 40 41#include "opt_compat.h" 42#include "opt_mac.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/kernel.h> 47#include <sys/sysctl.h> 48#include <sys/malloc.h> 49#include <sys/priv.h> 50#include <sys/proc.h> 51#include <sys/lock.h> 52#include <sys/mutex.h> 53#include <sys/sx.h> 54#include <sys/sysproto.h> 55#include <sys/uio.h> 56#include <sys/vimage.h> 57 58#include <security/mac/mac_framework.h> 59 60#include <vm/vm.h> 61#include <vm/vm_extern.h> 62 63static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 64static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 65static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 66 67/* 68 * Locking - this locks the sysctl tree in memory. 69 */ 70static struct sx sysctllock; 71 72#define SYSCTL_LOCK() sx_xlock(&sysctllock) 73#define SYSCTL_UNLOCK() sx_xunlock(&sysctllock) 74#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") 75 76static int sysctl_root(SYSCTL_HANDLER_ARGS); 77 78struct sysctl_oid_list sysctl__children; /* root list */ 79 80static struct sysctl_oid * 81sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 82{ 83 struct sysctl_oid *oidp; 84 85 SLIST_FOREACH(oidp, list, oid_link) { 86 if (strcmp(oidp->oid_name, name) == 0) { 87 return (oidp); 88 } 89 } 90 return (NULL); 91} 92 93/* 94 * Initialization of the MIB tree. 95 * 96 * Order by number in each list. 97 */ 98 99void 100sysctl_register_oid(struct sysctl_oid *oidp) 101{ 102 struct sysctl_oid_list *parent = oidp->oid_parent; 103 struct sysctl_oid *p; 104 struct sysctl_oid *q; 105 106 /* 107 * First check if another oid with the same name already 108 * exists in the parent's list. 109 */ 110 p = sysctl_find_oidname(oidp->oid_name, parent); 111 if (p != NULL) { 112 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 113 p->oid_refcnt++; 114 return; 115 } else { 116 printf("can't re-use a leaf (%s)!\n", p->oid_name); 117 return; 118 } 119 } 120 /* 121 * If this oid has a number OID_AUTO, give it a number which 122 * is greater than any current oid. 123 * NOTE: DO NOT change the starting value here, change it in 124 * <sys/sysctl.h>, and make sure it is at least 256 to 125 * accomodate e.g. net.inet.raw as a static sysctl node. 126 */ 127 if (oidp->oid_number == OID_AUTO) { 128 static int newoid = CTL_AUTO_START; 129 130 oidp->oid_number = newoid++; 131 if (newoid == 0x7fffffff) 132 panic("out of oids"); 133 } 134#if 0 135 else if (oidp->oid_number >= CTL_AUTO_START) { 136 /* do not panic; this happens when unregistering sysctl sets */ 137 printf("static sysctl oid too high: %d", oidp->oid_number); 138 } 139#endif 140 141 /* 142 * Insert the oid into the parent's list in order. 143 */ 144 q = NULL; 145 SLIST_FOREACH(p, parent, oid_link) { 146 if (oidp->oid_number < p->oid_number) 147 break; 148 q = p; 149 } 150 if (q) 151 SLIST_INSERT_AFTER(q, oidp, oid_link); 152 else 153 SLIST_INSERT_HEAD(parent, oidp, oid_link); 154} 155 156void 157sysctl_unregister_oid(struct sysctl_oid *oidp) 158{ 159 struct sysctl_oid *p; 160 int error; 161 162 error = ENOENT; 163 if (oidp->oid_number == OID_AUTO) { 164 error = EINVAL; 165 } else { 166 SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 167 if (p == oidp) { 168 SLIST_REMOVE(oidp->oid_parent, oidp, 169 sysctl_oid, oid_link); 170 error = 0; 171 break; 172 } 173 } 174 } 175 176 /* 177 * This can happen when a module fails to register and is 178 * being unloaded afterwards. It should not be a panic() 179 * for normal use. 180 */ 181 if (error) 182 printf("%s: failed to unregister sysctl\n", __func__); 183} 184 185/* Initialize a new context to keep track of dynamically added sysctls. */ 186int 187sysctl_ctx_init(struct sysctl_ctx_list *c) 188{ 189 190 if (c == NULL) { 191 return (EINVAL); 192 } 193 TAILQ_INIT(c); 194 return (0); 195} 196 197/* Free the context, and destroy all dynamic oids registered in this context */ 198int 199sysctl_ctx_free(struct sysctl_ctx_list *clist) 200{ 201 struct sysctl_ctx_entry *e, *e1; 202 int error; 203 204 error = 0; 205 /* 206 * First perform a "dry run" to check if it's ok to remove oids. 207 * XXX FIXME 208 * XXX This algorithm is a hack. But I don't know any 209 * XXX better solution for now... 210 */ 211 TAILQ_FOREACH(e, clist, link) { 212 error = sysctl_remove_oid(e->entry, 0, 0); 213 if (error) 214 break; 215 } 216 /* 217 * Restore deregistered entries, either from the end, 218 * or from the place where error occured. 219 * e contains the entry that was not unregistered 220 */ 221 if (error) 222 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 223 else 224 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 225 while (e1 != NULL) { 226 sysctl_register_oid(e1->entry); 227 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 228 } 229 if (error) 230 return(EBUSY); 231 /* Now really delete the entries */ 232 e = TAILQ_FIRST(clist); 233 while (e != NULL) { 234 e1 = TAILQ_NEXT(e, link); 235 error = sysctl_remove_oid(e->entry, 1, 0); 236 if (error) 237 panic("sysctl_remove_oid: corrupt tree, entry: %s", 238 e->entry->oid_name); 239 free(e, M_SYSCTLOID); 240 e = e1; 241 } 242 return (error); 243} 244 245/* Add an entry to the context */ 246struct sysctl_ctx_entry * 247sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 248{ 249 struct sysctl_ctx_entry *e; 250 251 if (clist == NULL || oidp == NULL) 252 return(NULL); 253 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 254 e->entry = oidp; 255 TAILQ_INSERT_HEAD(clist, e, link); 256 return (e); 257} 258 259/* Find an entry in the context */ 260struct sysctl_ctx_entry * 261sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 262{ 263 struct sysctl_ctx_entry *e; 264 265 if (clist == NULL || oidp == NULL) 266 return(NULL); 267 TAILQ_FOREACH(e, clist, link) { 268 if(e->entry == oidp) 269 return(e); 270 } 271 return (e); 272} 273 274/* 275 * Delete an entry from the context. 276 * NOTE: this function doesn't free oidp! You have to remove it 277 * with sysctl_remove_oid(). 278 */ 279int 280sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 281{ 282 struct sysctl_ctx_entry *e; 283 284 if (clist == NULL || oidp == NULL) 285 return (EINVAL); 286 e = sysctl_ctx_entry_find(clist, oidp); 287 if (e != NULL) { 288 TAILQ_REMOVE(clist, e, link); 289 free(e, M_SYSCTLOID); 290 return (0); 291 } else 292 return (ENOENT); 293} 294 295/* 296 * Remove dynamically created sysctl trees. 297 * oidp - top of the tree to be removed 298 * del - if 0 - just deregister, otherwise free up entries as well 299 * recurse - if != 0 traverse the subtree to be deleted 300 */ 301int 302sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 303{ 304 struct sysctl_oid *p; 305 int error; 306 307 if (oidp == NULL) 308 return(EINVAL); 309 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 310 printf("can't remove non-dynamic nodes!\n"); 311 return (EINVAL); 312 } 313 /* 314 * WARNING: normal method to do this should be through 315 * sysctl_ctx_free(). Use recursing as the last resort 316 * method to purge your sysctl tree of leftovers... 317 * However, if some other code still references these nodes, 318 * it will panic. 319 */ 320 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 321 if (oidp->oid_refcnt == 1) { 322 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) { 323 if (!recurse) 324 return (ENOTEMPTY); 325 error = sysctl_remove_oid(p, del, recurse); 326 if (error) 327 return (error); 328 } 329 if (del) 330 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 331 } 332 } 333 if (oidp->oid_refcnt > 1 ) { 334 oidp->oid_refcnt--; 335 } else { 336 if (oidp->oid_refcnt == 0) { 337 printf("Warning: bad oid_refcnt=%u (%s)!\n", 338 oidp->oid_refcnt, oidp->oid_name); 339 return (EINVAL); 340 } 341 sysctl_unregister_oid(oidp); 342 if (del) { 343 if (oidp->oid_descr) 344 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID); 345 free((void *)(uintptr_t)(const void *)oidp->oid_name, 346 M_SYSCTLOID); 347 free(oidp, M_SYSCTLOID); 348 } 349 } 350 return (0); 351} 352 353/* 354 * Create new sysctls at run time. 355 * clist may point to a valid context initialized with sysctl_ctx_init(). 356 */ 357struct sysctl_oid * 358sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 359 int number, const char *name, int kind, void *arg1, int arg2, 360 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 361{ 362 struct sysctl_oid *oidp; 363 ssize_t len; 364 char *newname; 365 366 /* You have to hook up somewhere.. */ 367 if (parent == NULL) 368 return(NULL); 369 /* Check if the node already exists, otherwise create it */ 370 oidp = sysctl_find_oidname(name, parent); 371 if (oidp != NULL) { 372 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 373 oidp->oid_refcnt++; 374 /* Update the context */ 375 if (clist != NULL) 376 sysctl_ctx_entry_add(clist, oidp); 377 return (oidp); 378 } else { 379 printf("can't re-use a leaf (%s)!\n", name); 380 return (NULL); 381 } 382 } 383 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 384 oidp->oid_parent = parent; 385 SLIST_NEXT(oidp, oid_link) = NULL; 386 oidp->oid_number = number; 387 oidp->oid_refcnt = 1; 388 len = strlen(name); 389 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 390 bcopy(name, newname, len + 1); 391 newname[len] = '\0'; 392 oidp->oid_name = newname; 393 oidp->oid_handler = handler; 394 oidp->oid_kind = CTLFLAG_DYN | kind; 395 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 396 /* Allocate space for children */ 397 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list), 398 M_SYSCTLOID, M_WAITOK)); 399 SLIST_INIT(SYSCTL_CHILDREN(oidp)); 400 } else { 401 oidp->oid_arg1 = arg1; 402 oidp->oid_arg2 = arg2; 403 } 404 oidp->oid_fmt = fmt; 405 if (descr) { 406 int len = strlen(descr) + 1; 407 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK); 408 if (oidp->oid_descr) 409 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr); 410 } 411 /* Update the context, if used */ 412 if (clist != NULL) 413 sysctl_ctx_entry_add(clist, oidp); 414 /* Register this oid */ 415 sysctl_register_oid(oidp); 416 return (oidp); 417} 418 419/* 420 * Rename an existing oid. 421 */ 422void 423sysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 424{ 425 ssize_t len; 426 char *newname; 427 void *oldname; 428 429 oldname = (void *)(uintptr_t)(const void *)oidp->oid_name; 430 len = strlen(name); 431 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 432 bcopy(name, newname, len + 1); 433 newname[len] = '\0'; 434 oidp->oid_name = newname; 435 free(oldname, M_SYSCTLOID); 436} 437 438/* 439 * Reparent an existing oid. 440 */ 441int 442sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 443{ 444 struct sysctl_oid *oidp; 445 446 if (oid->oid_parent == parent) 447 return (0); 448 oidp = sysctl_find_oidname(oid->oid_name, parent); 449 if (oidp != NULL) 450 return (EEXIST); 451 sysctl_unregister_oid(oid); 452 oid->oid_parent = parent; 453 oid->oid_number = OID_AUTO; 454 sysctl_register_oid(oid); 455 return (0); 456} 457 458/* 459 * Register the kernel's oids on startup. 460 */ 461SET_DECLARE(sysctl_set, struct sysctl_oid); 462 463static void 464sysctl_register_all(void *arg) 465{ 466 struct sysctl_oid **oidp; 467 468 SYSCTL_INIT(); 469 SET_FOREACH(oidp, sysctl_set) 470 sysctl_register_oid(*oidp); 471} 472SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 473 474/* 475 * "Staff-functions" 476 * 477 * These functions implement a presently undocumented interface 478 * used by the sysctl program to walk the tree, and get the type 479 * so it can print the value. 480 * This interface is under work and consideration, and should probably 481 * be killed with a big axe by the first person who can find the time. 482 * (be aware though, that the proper interface isn't as obvious as it 483 * may seem, there are various conflicting requirements. 484 * 485 * {0,0} printf the entire MIB-tree. 486 * {0,1,...} return the name of the "..." OID. 487 * {0,2,...} return the next OID. 488 * {0,3} return the OID of the name in "new" 489 * {0,4,...} return the kind & format info for the "..." OID. 490 * {0,5,...} return the description the "..." OID. 491 */ 492 493#ifdef SYSCTL_DEBUG 494static void 495sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 496{ 497 int k; 498 struct sysctl_oid *oidp; 499 500 SLIST_FOREACH(oidp, l, oid_link) { 501 502 for (k=0; k<i; k++) 503 printf(" "); 504 505 printf("%d %s ", oidp->oid_number, oidp->oid_name); 506 507 printf("%c%c", 508 oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 509 oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 510 511 if (oidp->oid_handler) 512 printf(" *Handler"); 513 514 switch (oidp->oid_kind & CTLTYPE) { 515 case CTLTYPE_NODE: 516 printf(" Node\n"); 517 if (!oidp->oid_handler) { 518 sysctl_sysctl_debug_dump_node( 519 oidp->oid_arg1, i+2); 520 } 521 break; 522 case CTLTYPE_INT: printf(" Int\n"); break; 523 case CTLTYPE_STRING: printf(" String\n"); break; 524 case CTLTYPE_QUAD: printf(" Quad\n"); break; 525 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 526 default: printf("\n"); 527 } 528 529 } 530} 531 532static int 533sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 534{ 535 int error; 536 537 error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 538 if (error) 539 return (error); 540 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 541 return (ENOENT); 542} 543 544SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 545 0, 0, sysctl_sysctl_debug, "-", ""); 546#endif 547 548static int 549sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 550{ 551 int *name = (int *) arg1; 552 u_int namelen = arg2; 553 int error = 0; 554 struct sysctl_oid *oid; 555 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 556 char buf[10]; 557 558 while (namelen) { 559 if (!lsp) { 560 snprintf(buf,sizeof(buf),"%d",*name); 561 if (req->oldidx) 562 error = SYSCTL_OUT(req, ".", 1); 563 if (!error) 564 error = SYSCTL_OUT(req, buf, strlen(buf)); 565 if (error) 566 return (error); 567 namelen--; 568 name++; 569 continue; 570 } 571 lsp2 = 0; 572 SLIST_FOREACH(oid, lsp, oid_link) { 573 if (oid->oid_number != *name) 574 continue; 575 576 if (req->oldidx) 577 error = SYSCTL_OUT(req, ".", 1); 578 if (!error) 579 error = SYSCTL_OUT(req, oid->oid_name, 580 strlen(oid->oid_name)); 581 if (error) 582 return (error); 583 584 namelen--; 585 name++; 586 587 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 588 break; 589 590 if (oid->oid_handler) 591 break; 592 593 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; 594 break; 595 } 596 lsp = lsp2; 597 } 598 return (SYSCTL_OUT(req, "", 1)); 599} 600 601static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 602 603static int 604sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 605 int *next, int *len, int level, struct sysctl_oid **oidpp) 606{ 607 struct sysctl_oid *oidp; 608 609 *len = level; 610 SLIST_FOREACH(oidp, lsp, oid_link) { 611 *next = oidp->oid_number; 612 *oidpp = oidp; 613 614 if (oidp->oid_kind & CTLFLAG_SKIP) 615 continue; 616 617 if (!namelen) { 618 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 619 return (0); 620 if (oidp->oid_handler) 621 /* We really should call the handler here...*/ 622 return (0); 623 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 624 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 625 len, level+1, oidpp)) 626 return (0); 627 goto emptynode; 628 } 629 630 if (oidp->oid_number < *name) 631 continue; 632 633 if (oidp->oid_number > *name) { 634 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 635 return (0); 636 if (oidp->oid_handler) 637 return (0); 638 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 639 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 640 next+1, len, level+1, oidpp)) 641 return (0); 642 goto next; 643 } 644 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 645 continue; 646 647 if (oidp->oid_handler) 648 continue; 649 650 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 651 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 652 len, level+1, oidpp)) 653 return (0); 654 next: 655 namelen = 1; 656 emptynode: 657 *len = level; 658 } 659 return (1); 660} 661 662static int 663sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 664{ 665 int *name = (int *) arg1; 666 u_int namelen = arg2; 667 int i, j, error; 668 struct sysctl_oid *oid; 669 struct sysctl_oid_list *lsp = &sysctl__children; 670 int newoid[CTL_MAXNAME]; 671 672 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 673 if (i) 674 return (ENOENT); 675 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 676 return (error); 677} 678 679static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 680 681static int 682name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp) 683{ 684 int i; 685 struct sysctl_oid *oidp; 686 struct sysctl_oid_list *lsp = &sysctl__children; 687 char *p; 688 689 if (!*name) 690 return (ENOENT); 691 692 p = name + strlen(name) - 1 ; 693 if (*p == '.') 694 *p = '\0'; 695 696 *len = 0; 697 698 for (p = name; *p && *p != '.'; p++) 699 ; 700 i = *p; 701 if (i == '.') 702 *p = '\0'; 703 704 oidp = SLIST_FIRST(lsp); 705 706 while (oidp && *len < CTL_MAXNAME) { 707 if (strcmp(name, oidp->oid_name)) { 708 oidp = SLIST_NEXT(oidp, oid_link); 709 continue; 710 } 711 *oid++ = oidp->oid_number; 712 (*len)++; 713 714 if (!i) { 715 if (oidpp) 716 *oidpp = oidp; 717 return (0); 718 } 719 720 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 721 break; 722 723 if (oidp->oid_handler) 724 break; 725 726 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 727 oidp = SLIST_FIRST(lsp); 728 name = p+1; 729 for (p = name; *p && *p != '.'; p++) 730 ; 731 i = *p; 732 if (i == '.') 733 *p = '\0'; 734 } 735 return (ENOENT); 736} 737 738static int 739sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 740{ 741 char *p; 742 int error, oid[CTL_MAXNAME], len; 743 struct sysctl_oid *op = 0; 744 745 if (!req->newlen) 746 return (ENOENT); 747 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 748 return (ENAMETOOLONG); 749 750 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 751 752 error = SYSCTL_IN(req, p, req->newlen); 753 if (error) { 754 free(p, M_SYSCTL); 755 return (error); 756 } 757 758 p [req->newlen] = '\0'; 759 760 error = name2oid(p, oid, &len, &op); 761 762 free(p, M_SYSCTL); 763 764 if (error) 765 return (error); 766 767 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 768 return (error); 769} 770 771SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 772 sysctl_sysctl_name2oid, "I", ""); 773 774static int 775sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 776{ 777 struct sysctl_oid *oid; 778 int error; 779 780 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 781 if (error) 782 return (error); 783 784 if (!oid->oid_fmt) 785 return (ENOENT); 786 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 787 if (error) 788 return (error); 789 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 790 return (error); 791} 792 793 794static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, ""); 795 796static int 797sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 798{ 799 struct sysctl_oid *oid; 800 int error; 801 802 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 803 if (error) 804 return (error); 805 806 if (!oid->oid_descr) 807 return (ENOENT); 808 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 809 return (error); 810} 811 812static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, ""); 813 814/* 815 * Default "handler" functions. 816 */ 817 818/* 819 * Handle an int, signed or unsigned. 820 * Two cases: 821 * a variable: point arg1 at it. 822 * a constant: pass it in arg2. 823 */ 824 825int 826sysctl_handle_int(SYSCTL_HANDLER_ARGS) 827{ 828 int tmpout, error = 0; 829 830 /* 831 * Attempt to get a coherent snapshot by making a copy of the data. 832 */ 833 if (arg1) 834 tmpout = *(int *)arg1; 835 else 836 tmpout = arg2; 837 error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 838 839 if (error || !req->newptr) 840 return (error); 841 842 if (!arg1) 843 error = EPERM; 844 else 845 error = SYSCTL_IN(req, arg1, sizeof(int)); 846 return (error); 847} 848 849 850/* 851 * Based on on sysctl_handle_int() convert milliseconds into ticks. 852 */ 853 854int 855sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 856{ 857 int error, s, tt; 858 859 tt = *(int *)oidp->oid_arg1; 860 s = (int)((int64_t)tt * 1000 / hz); 861 862 error = sysctl_handle_int(oidp, &s, 0, req); 863 if (error || !req->newptr) 864 return (error); 865 866 tt = (int)((int64_t)s * hz / 1000); 867 if (tt < 1) 868 return (EINVAL); 869 870 *(int *)oidp->oid_arg1 = tt; 871 return (0); 872} 873 874 875/* 876 * Handle a long, signed or unsigned. arg1 points to it. 877 */ 878 879int 880sysctl_handle_long(SYSCTL_HANDLER_ARGS) 881{ 882 int error = 0; 883 long tmplong; 884#ifdef SCTL_MASK32 885 int tmpint; 886#endif 887 888 /* 889 * Attempt to get a coherent snapshot by making a copy of the data. 890 */ 891 if (!arg1) 892 return (EINVAL); 893 tmplong = *(long *)arg1; 894#ifdef SCTL_MASK32 895 if (req->flags & SCTL_MASK32) { 896 tmpint = tmplong; 897 error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 898 } else 899#endif 900 error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 901 902 if (error || !req->newptr) 903 return (error); 904 905#ifdef SCTL_MASK32 906 if (req->flags & SCTL_MASK32) { 907 error = SYSCTL_IN(req, &tmpint, sizeof(int)); 908 *(long *)arg1 = (long)tmpint; 909 } else 910#endif 911 error = SYSCTL_IN(req, arg1, sizeof(long)); 912 return (error); 913} 914 915/* 916 * Handle a 64 bit int, signed or unsigned. arg1 points to it. 917 */ 918 919int 920sysctl_handle_quad(SYSCTL_HANDLER_ARGS) 921{ 922 int error = 0; 923 uint64_t tmpout; 924 925 /* 926 * Attempt to get a coherent snapshot by making a copy of the data. 927 */ 928 if (!arg1) 929 return (EINVAL); 930 tmpout = *(uint64_t *)arg1; 931 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 932 933 if (error || !req->newptr) 934 return (error); 935 936 error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 937 return (error); 938} 939 940/* 941 * Handle our generic '\0' terminated 'C' string. 942 * Two cases: 943 * a variable string: point arg1 at it, arg2 is max length. 944 * a constant string: point arg1 at it, arg2 is zero. 945 */ 946 947int 948sysctl_handle_string(SYSCTL_HANDLER_ARGS) 949{ 950 int error=0; 951 char *tmparg; 952 size_t outlen; 953 954 /* 955 * Attempt to get a coherent snapshot by copying to a 956 * temporary kernel buffer. 957 */ 958retry: 959 outlen = strlen((char *)arg1)+1; 960 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 961 962 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 963 free(tmparg, M_SYSCTLTMP); 964 goto retry; 965 } 966 967 error = SYSCTL_OUT(req, tmparg, outlen); 968 free(tmparg, M_SYSCTLTMP); 969 970 if (error || !req->newptr) 971 return (error); 972 973 if ((req->newlen - req->newidx) >= arg2) { 974 error = EINVAL; 975 } else { 976 arg2 = (req->newlen - req->newidx); 977 error = SYSCTL_IN(req, arg1, arg2); 978 ((char *)arg1)[arg2] = '\0'; 979 } 980 981 return (error); 982} 983 984/* 985 * Handle any kind of opaque data. 986 * arg1 points to it, arg2 is the size. 987 */ 988 989int 990sysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 991{ 992 int error, tries; 993 u_int generation; 994 struct sysctl_req req2; 995 996 /* 997 * Attempt to get a coherent snapshot, by using the thread 998 * pre-emption counter updated from within mi_switch() to 999 * determine if we were pre-empted during a bcopy() or 1000 * copyout(). Make 3 attempts at doing this before giving up. 1001 * If we encounter an error, stop immediately. 1002 */ 1003 tries = 0; 1004 req2 = *req; 1005retry: 1006 generation = curthread->td_generation; 1007 error = SYSCTL_OUT(req, arg1, arg2); 1008 if (error) 1009 return (error); 1010 tries++; 1011 if (generation != curthread->td_generation && tries < 3) { 1012 *req = req2; 1013 goto retry; 1014 } 1015 1016 error = SYSCTL_IN(req, arg1, arg2); 1017 1018 return (error); 1019} 1020 1021/* 1022 * Transfer functions to/from kernel space. 1023 * XXX: rather untested at this point 1024 */ 1025static int 1026sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 1027{ 1028 size_t i = 0; 1029 1030 if (req->oldptr) { 1031 i = l; 1032 if (req->oldlen <= req->oldidx) 1033 i = 0; 1034 else 1035 if (i > req->oldlen - req->oldidx) 1036 i = req->oldlen - req->oldidx; 1037 if (i > 0) 1038 bcopy(p, (char *)req->oldptr + req->oldidx, i); 1039 } 1040 req->oldidx += l; 1041 if (req->oldptr && i != l) 1042 return (ENOMEM); 1043 return (0); 1044} 1045 1046static int 1047sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 1048{ 1049 if (!req->newptr) 1050 return (0); 1051 if (req->newlen - req->newidx < l) 1052 return (EINVAL); 1053 bcopy((char *)req->newptr + req->newidx, p, l); 1054 req->newidx += l; 1055 return (0); 1056} 1057 1058int 1059kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1060 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 1061{ 1062 int error = 0; 1063 struct sysctl_req req; 1064 1065 bzero(&req, sizeof req); 1066 1067 req.td = td; 1068 req.flags = flags; 1069 1070 if (oldlenp) { 1071 req.oldlen = *oldlenp; 1072 } 1073 req.validlen = req.oldlen; 1074 1075 if (old) { 1076 req.oldptr= old; 1077 } 1078 1079 if (new != NULL) { 1080 req.newlen = newlen; 1081 req.newptr = new; 1082 } 1083 1084 req.oldfunc = sysctl_old_kernel; 1085 req.newfunc = sysctl_new_kernel; 1086 req.lock = REQ_LOCKED; 1087 1088 SYSCTL_LOCK(); 1089 1090 error = sysctl_root(0, name, namelen, &req); 1091 1092 if (req.lock == REQ_WIRED && req.validlen > 0) 1093 vsunlock(req.oldptr, req.validlen); 1094 1095 SYSCTL_UNLOCK(); 1096 1097 if (error && error != ENOMEM) 1098 return (error); 1099 1100 if (retval) { 1101 if (req.oldptr && req.oldidx > req.validlen) 1102 *retval = req.validlen; 1103 else 1104 *retval = req.oldidx; 1105 } 1106 return (error); 1107} 1108 1109int 1110kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1111 void *new, size_t newlen, size_t *retval, int flags) 1112{ 1113 int oid[CTL_MAXNAME]; 1114 size_t oidlen, plen; 1115 int error; 1116 1117 oid[0] = 0; /* sysctl internal magic */ 1118 oid[1] = 3; /* name2oid */ 1119 oidlen = sizeof(oid); 1120 1121 error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1122 (void *)name, strlen(name), &plen, flags); 1123 if (error) 1124 return (error); 1125 1126 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1127 new, newlen, retval, flags); 1128 return (error); 1129} 1130 1131/* 1132 * Transfer function to/from user space. 1133 */ 1134static int 1135sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 1136{ 1137 int error = 0; 1138 size_t i, len, origidx; 1139 1140 origidx = req->oldidx; 1141 req->oldidx += l; 1142 if (req->oldptr == NULL) 1143 return (0); 1144 /* 1145 * If we have not wired the user supplied buffer and we are currently 1146 * holding locks, drop a witness warning, as it's possible that 1147 * write operations to the user page can sleep. 1148 */ 1149 if (req->lock != REQ_WIRED) 1150 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1151 "sysctl_old_user()"); 1152 i = l; 1153 len = req->validlen; 1154 if (len <= origidx) 1155 i = 0; 1156 else { 1157 if (i > len - origidx) 1158 i = len - origidx; 1159 error = copyout(p, (char *)req->oldptr + origidx, i); 1160 } 1161 if (error) 1162 return (error); 1163 if (i < l) 1164 return (ENOMEM); 1165 return (0); 1166} 1167 1168static int 1169sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 1170{ 1171 int error; 1172 1173 if (!req->newptr) 1174 return (0); 1175 if (req->newlen - req->newidx < l) 1176 return (EINVAL); 1177 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1178 "sysctl_new_user()"); 1179 error = copyin((char *)req->newptr + req->newidx, p, l); 1180 req->newidx += l; 1181 return (error); 1182} 1183 1184/* 1185 * Wire the user space destination buffer. If set to a value greater than 1186 * zero, the len parameter limits the maximum amount of wired memory. 1187 */ 1188int 1189sysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1190{ 1191 int ret; 1192 size_t i, wiredlen; 1193 char *cp, dummy; 1194 1195 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1196 ret = 0; 1197 if (req->lock == REQ_LOCKED && req->oldptr && 1198 req->oldfunc == sysctl_old_user) { 1199 if (wiredlen != 0) { 1200 ret = vslock(req->oldptr, wiredlen); 1201 if (ret != 0) { 1202 if (ret != ENOMEM) 1203 return (ret); 1204 wiredlen = 0; 1205 } 1206 /* 1207 * Touch all the wired pages to avoid PTE modified 1208 * bit emulation traps on Alpha while holding locks 1209 * in the sysctl handler. 1210 */ 1211 for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE, 1212 cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) { 1213 copyin(cp, &dummy, 1); 1214 copyout(&dummy, cp, 1); 1215 } 1216 } 1217 req->lock = REQ_WIRED; 1218 req->validlen = wiredlen; 1219 } 1220 return (0); 1221} 1222 1223int 1224sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 1225 int *nindx, struct sysctl_req *req) 1226{ 1227 struct sysctl_oid *oid; 1228 int indx; 1229 1230 oid = SLIST_FIRST(&sysctl__children); 1231 indx = 0; 1232 while (oid && indx < CTL_MAXNAME) { 1233 if (oid->oid_number == name[indx]) { 1234 indx++; 1235 if (oid->oid_kind & CTLFLAG_NOLOCK) 1236 req->lock = REQ_UNLOCKED; 1237 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1238 if (oid->oid_handler != NULL || 1239 indx == namelen) { 1240 *noid = oid; 1241 if (nindx != NULL) 1242 *nindx = indx; 1243 return (0); 1244 } 1245 oid = SLIST_FIRST( 1246 (struct sysctl_oid_list *)oid->oid_arg1); 1247 } else if (indx == namelen) { 1248 *noid = oid; 1249 if (nindx != NULL) 1250 *nindx = indx; 1251 return (0); 1252 } else { 1253 return (ENOTDIR); 1254 } 1255 } else { 1256 oid = SLIST_NEXT(oid, oid_link); 1257 } 1258 } 1259 return (ENOENT); 1260} 1261 1262/* 1263 * Traverse our tree, and find the right node, execute whatever it points 1264 * to, and return the resulting error code. 1265 */ 1266 1267static int 1268sysctl_root(SYSCTL_HANDLER_ARGS) 1269{ 1270 struct sysctl_oid *oid; 1271 int error, indx, lvl; 1272 1273 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1274 if (error) 1275 return (error); 1276 1277 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1278 /* 1279 * You can't call a sysctl when it's a node, but has 1280 * no handler. Inform the user that it's a node. 1281 * The indx may or may not be the same as namelen. 1282 */ 1283 if (oid->oid_handler == NULL) 1284 return (EISDIR); 1285 } 1286 1287 /* Is this sysctl writable? */ 1288 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 1289 return (EPERM); 1290 1291 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 1292 1293 /* Is this sysctl sensitive to securelevels? */ 1294 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1295 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1296 error = securelevel_gt(req->td->td_ucred, lvl); 1297 if (error) 1298 return (error); 1299 } 1300 1301 /* Is this sysctl writable by only privileged users? */ 1302 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1303 if (oid->oid_kind & CTLFLAG_PRISON) 1304 error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL); 1305 else 1306 error = priv_check(req->td, PRIV_SYSCTL_WRITE); 1307 if (error) 1308 return (error); 1309 } 1310 1311 if (!oid->oid_handler) 1312 return (EINVAL); 1313 1314 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1315 arg1 = (int *)arg1 + indx; 1316 arg2 -= indx; 1317 } else { 1318 arg1 = oid->oid_arg1; 1319 arg2 = oid->oid_arg2; 1320 } 1321#ifdef MAC 1322 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1323 req); 1324 if (error != 0) 1325 return (error); 1326#endif 1327 error = oid->oid_handler(oid, arg1, arg2, req); 1328 1329 return (error); 1330} 1331 1332#ifndef _SYS_SYSPROTO_H_ 1333struct sysctl_args { 1334 int *name; 1335 u_int namelen; 1336 void *old; 1337 size_t *oldlenp; 1338 void *new; 1339 size_t newlen; 1340}; 1341#endif 1342int 1343__sysctl(struct thread *td, struct sysctl_args *uap) 1344{ 1345 int error, name[CTL_MAXNAME]; 1346 size_t j; 1347 1348 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1349 return (EINVAL); 1350 1351 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 1352 if (error) 1353 return (error); 1354 1355 mtx_lock(&Giant); 1356 1357 error = userland_sysctl(td, name, uap->namelen, 1358 uap->old, uap->oldlenp, 0, 1359 uap->new, uap->newlen, &j, 0); 1360 if (error && error != ENOMEM) 1361 goto done2; 1362 if (uap->oldlenp) { 1363 int i = copyout(&j, uap->oldlenp, sizeof(j)); 1364 if (i) 1365 error = i; 1366 } 1367done2: 1368 mtx_unlock(&Giant); 1369 return (error); 1370} 1371 1372/* 1373 * This is used from various compatibility syscalls too. That's why name 1374 * must be in kernel space. 1375 */ 1376int 1377userland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1378 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 1379 int flags) 1380{ 1381 int error = 0; 1382 struct sysctl_req req; 1383 1384 bzero(&req, sizeof req); 1385 1386 req.td = td; 1387 req.flags = flags; 1388 1389 if (oldlenp) { 1390 if (inkernel) { 1391 req.oldlen = *oldlenp; 1392 } else { 1393 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 1394 if (error) 1395 return (error); 1396 } 1397 } 1398 req.validlen = req.oldlen; 1399 1400 if (old) { 1401 if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 1402 return (EFAULT); 1403 req.oldptr= old; 1404 } 1405 1406 if (new != NULL) { 1407 if (!useracc(new, newlen, VM_PROT_READ)) 1408 return (EFAULT); 1409 req.newlen = newlen; 1410 req.newptr = new; 1411 } 1412 1413 req.oldfunc = sysctl_old_user; 1414 req.newfunc = sysctl_new_user; 1415 req.lock = REQ_LOCKED; 1416 1417 SYSCTL_LOCK(); 1418 CURVNET_SET(TD_TO_VNET(curthread)); 1419 1420 for (;;) { 1421 req.oldidx = 0; 1422 req.newidx = 0; 1423 error = sysctl_root(0, name, namelen, &req); 1424 if (error != EAGAIN) 1425 break; 1426 DROP_GIANT(); 1427 uio_yield(); 1428 PICKUP_GIANT(); 1429 } 1430 1431 if (req.lock == REQ_WIRED && req.validlen > 0) 1432 vsunlock(req.oldptr, req.validlen); 1433 1434 CURVNET_RESTORE(); 1435 SYSCTL_UNLOCK(); 1436 1437 if (error && error != ENOMEM) 1438 return (error); 1439 1440 if (retval) { 1441 if (req.oldptr && req.oldidx > req.validlen) 1442 *retval = req.validlen; 1443 else 1444 *retval = req.oldidx; 1445 } 1446 return (error); 1447} 1448 1449#ifdef COMPAT_43 1450#include <sys/socket.h> 1451#include <vm/vm_param.h> 1452 1453#define KINFO_PROC (0<<8) 1454#define KINFO_RT (1<<8) 1455#define KINFO_VNODE (2<<8) 1456#define KINFO_FILE (3<<8) 1457#define KINFO_METER (4<<8) 1458#define KINFO_LOADAVG (5<<8) 1459#define KINFO_CLOCKRATE (6<<8) 1460 1461/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 1462#define KINFO_BSDI_SYSINFO (101<<8) 1463 1464/* 1465 * XXX this is bloat, but I hope it's better here than on the potentially 1466 * limited kernel stack... -Peter 1467 */ 1468 1469static struct { 1470 int bsdi_machine; /* "i386" on BSD/386 */ 1471/* ^^^ this is an offset to the string, relative to the struct start */ 1472 char *pad0; 1473 long pad1; 1474 long pad2; 1475 long pad3; 1476 u_long pad4; 1477 u_long pad5; 1478 u_long pad6; 1479 1480 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 1481 int bsdi_osrelease; /* "1.1" on BSD/386 */ 1482 long pad7; 1483 long pad8; 1484 char *pad9; 1485 1486 long pad10; 1487 long pad11; 1488 int pad12; 1489 long pad13; 1490 quad_t pad14; 1491 long pad15; 1492 1493 struct timeval pad16; 1494 /* we dont set this, because BSDI's uname used gethostname() instead */ 1495 int bsdi_hostname; /* hostname on BSD/386 */ 1496 1497 /* the actual string data is appended here */ 1498 1499} bsdi_si; 1500 1501/* 1502 * this data is appended to the end of the bsdi_si structure during copyout. 1503 * The "char *" offsets are relative to the base of the bsdi_si struct. 1504 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 1505 * should not exceed the length of the buffer here... (or else!! :-) 1506 */ 1507static char bsdi_strings[80]; /* It had better be less than this! */ 1508 1509#ifndef _SYS_SYSPROTO_H_ 1510struct getkerninfo_args { 1511 int op; 1512 char *where; 1513 size_t *size; 1514 int arg; 1515}; 1516#endif 1517int 1518ogetkerninfo(struct thread *td, struct getkerninfo_args *uap) 1519{ 1520 int error, name[6]; 1521 size_t size; 1522 u_int needed = 0; 1523 1524 mtx_lock(&Giant); 1525 1526 switch (uap->op & 0xff00) { 1527 1528 case KINFO_RT: 1529 name[0] = CTL_NET; 1530 name[1] = PF_ROUTE; 1531 name[2] = 0; 1532 name[3] = (uap->op & 0xff0000) >> 16; 1533 name[4] = uap->op & 0xff; 1534 name[5] = uap->arg; 1535 error = userland_sysctl(td, name, 6, uap->where, uap->size, 1536 0, 0, 0, &size, 0); 1537 break; 1538 1539 case KINFO_VNODE: 1540 name[0] = CTL_KERN; 1541 name[1] = KERN_VNODE; 1542 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1543 0, 0, 0, &size, 0); 1544 break; 1545 1546 case KINFO_PROC: 1547 name[0] = CTL_KERN; 1548 name[1] = KERN_PROC; 1549 name[2] = uap->op & 0xff; 1550 name[3] = uap->arg; 1551 error = userland_sysctl(td, name, 4, uap->where, uap->size, 1552 0, 0, 0, &size, 0); 1553 break; 1554 1555 case KINFO_FILE: 1556 name[0] = CTL_KERN; 1557 name[1] = KERN_FILE; 1558 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1559 0, 0, 0, &size, 0); 1560 break; 1561 1562 case KINFO_METER: 1563 name[0] = CTL_VM; 1564 name[1] = VM_TOTAL; 1565 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1566 0, 0, 0, &size, 0); 1567 break; 1568 1569 case KINFO_LOADAVG: 1570 name[0] = CTL_VM; 1571 name[1] = VM_LOADAVG; 1572 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1573 0, 0, 0, &size, 0); 1574 break; 1575 1576 case KINFO_CLOCKRATE: 1577 name[0] = CTL_KERN; 1578 name[1] = KERN_CLOCKRATE; 1579 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1580 0, 0, 0, &size, 0); 1581 break; 1582 1583 case KINFO_BSDI_SYSINFO: { 1584 /* 1585 * this is pretty crude, but it's just enough for uname() 1586 * from BSDI's 1.x libc to work. 1587 * 1588 * *size gives the size of the buffer before the call, and 1589 * the amount of data copied after a successful call. 1590 * If successful, the return value is the amount of data 1591 * available, which can be larger than *size. 1592 * 1593 * BSDI's 2.x product apparently fails with ENOMEM if *size 1594 * is too small. 1595 */ 1596 1597 u_int left; 1598 char *s; 1599 1600 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1601 bzero(bsdi_strings, sizeof(bsdi_strings)); 1602 1603 s = bsdi_strings; 1604 1605 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1606 strcpy(s, ostype); 1607 s += strlen(s) + 1; 1608 1609 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1610 strcpy(s, osrelease); 1611 s += strlen(s) + 1; 1612 1613 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1614 strcpy(s, machine); 1615 s += strlen(s) + 1; 1616 1617 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1618 1619 if ((uap->where == NULL) || (uap->size == NULL)) { 1620 /* process is asking how much buffer to supply.. */ 1621 size = needed; 1622 error = 0; 1623 break; 1624 } 1625 1626 if ((error = copyin(uap->size, &size, sizeof(size))) != 0) 1627 break; 1628 1629 /* if too much buffer supplied, trim it down */ 1630 if (size > needed) 1631 size = needed; 1632 1633 /* how much of the buffer is remaining */ 1634 left = size; 1635 1636 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1637 break; 1638 1639 /* is there any point in continuing? */ 1640 if (left > sizeof(bsdi_si)) { 1641 left -= sizeof(bsdi_si); 1642 error = copyout(&bsdi_strings, 1643 uap->where + sizeof(bsdi_si), left); 1644 } 1645 break; 1646 } 1647 1648 default: 1649 error = EOPNOTSUPP; 1650 break; 1651 } 1652 if (error == 0) { 1653 td->td_retval[0] = needed ? needed : size; 1654 if (uap->size) { 1655 error = copyout(&size, uap->size, sizeof(size)); 1656 } 1657 } 1658 mtx_unlock(&Giant); 1659 return (error); 1660} 1661#endif /* COMPAT_43 */ 1662