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