kern_sysctl.c revision 44078
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 * $Id: kern_sysctl.c,v 1.83 1999/01/10 07:45:31 phk Exp $ 41 */ 42 43#include "opt_compat.h" 44 45#include <sys/param.h> 46#include <sys/buf.h> 47#include <sys/kernel.h> 48#include <sys/sysctl.h> 49#include <sys/malloc.h> 50#include <sys/proc.h> 51#include <sys/systm.h> 52#include <sys/sysproto.h> 53#include <vm/vm.h> 54#include <vm/vm_extern.h> 55 56static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 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 71/* 72 * Initialization of the MIB tree. 73 * 74 * Order by number in each list. 75 */ 76 77void sysctl_register_oid(struct sysctl_oid *oidp) 78{ 79 struct sysctl_oid_list *parent = oidp->oid_parent; 80 struct sysctl_oid *p; 81 struct sysctl_oid *q; 82 int n; 83 84 /* 85 * If this oid has a number OID_AUTO, give it a number which 86 * is greater than any current oid. Make sure it is at least 87 * 100 to leave space for pre-assigned oid numbers. 88 */ 89 if (oidp->oid_number == OID_AUTO) { 90 /* First, find the highest oid in the parent list >99 */ 91 n = 99; 92 SLIST_FOREACH(p, parent, oid_link) { 93 if (p->oid_number > n) 94 n = p->oid_number; 95 } 96 oidp->oid_number = n + 1; 97 } 98 99 /* 100 * Insert the oid into the parent's list in order. 101 */ 102 q = NULL; 103 SLIST_FOREACH(p, parent, oid_link) { 104 if (oidp->oid_number < p->oid_number) 105 break; 106 q = p; 107 } 108 if (q) 109 SLIST_INSERT_AFTER(q, oidp, oid_link); 110 else 111 SLIST_INSERT_HEAD(parent, oidp, oid_link); 112} 113 114void sysctl_unregister_oid(struct sysctl_oid *oidp) 115{ 116 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link); 117} 118 119/* 120 * Bulk-register all the oids in a linker_set. 121 */ 122void sysctl_register_set(struct linker_set *lsp) 123{ 124 int count = lsp->ls_length; 125 int i; 126 for (i = 0; i < count; i++) 127 sysctl_register_oid((struct sysctl_oid *) lsp->ls_items[i]); 128} 129 130void sysctl_unregister_set(struct linker_set *lsp) 131{ 132 int count = lsp->ls_length; 133 int i; 134 for (i = 0; i < count; i++) 135 sysctl_unregister_oid((struct sysctl_oid *) lsp->ls_items[i]); 136} 137 138/* 139 * Register the kernel's oids on startup. 140 */ 141extern struct linker_set sysctl_set; 142 143static void sysctl_register_all(void *arg) 144{ 145 sysctl_register_set(&sysctl_set); 146} 147 148SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 149 150/* 151 * "Staff-functions" 152 * 153 * These functions implement a presently undocumented interface 154 * used by the sysctl program to walk the tree, and get the type 155 * so it can print the value. 156 * This interface is under work and consideration, and should probably 157 * be killed with a big axe by the first person who can find the time. 158 * (be aware though, that the proper interface isn't as obvious as it 159 * may seem, there are various conflicting requirements. 160 * 161 * {0,0} printf the entire MIB-tree. 162 * {0,1,...} return the name of the "..." OID. 163 * {0,2,...} return the next OID. 164 * {0,3} return the OID of the name in "new" 165 * {0,4,...} return the kind & format info for the "..." OID. 166 */ 167 168static void 169sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 170{ 171 int k; 172 struct sysctl_oid *oidp; 173 174 SLIST_FOREACH(oidp, l, oid_link) { 175 176 for (k=0; k<i; k++) 177 printf(" "); 178 179 printf("%d %s ", oidp->oid_number, oidp->oid_name); 180 181 printf("%c%c", 182 oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 183 oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 184 185 if (oidp->oid_handler) 186 printf(" *Handler"); 187 188 switch (oidp->oid_kind & CTLTYPE) { 189 case CTLTYPE_NODE: 190 printf(" Node\n"); 191 if (!oidp->oid_handler) { 192 sysctl_sysctl_debug_dump_node( 193 oidp->oid_arg1, i+2); 194 } 195 break; 196 case CTLTYPE_INT: printf(" Int\n"); break; 197 case CTLTYPE_STRING: printf(" String\n"); break; 198 case CTLTYPE_QUAD: printf(" Quad\n"); break; 199 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 200 default: printf("\n"); 201 } 202 203 } 204} 205 206static int 207sysctl_sysctl_debug SYSCTL_HANDLER_ARGS 208{ 209 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 210 return ENOENT; 211} 212 213SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 214 0, 0, sysctl_sysctl_debug, "-", ""); 215 216static int 217sysctl_sysctl_name SYSCTL_HANDLER_ARGS 218{ 219 int *name = (int *) arg1; 220 u_int namelen = arg2; 221 int error = 0; 222 struct sysctl_oid *oid; 223 struct sysctl_oid_list *lsp = &sysctl__children; 224 char buf[10]; 225 226 while (namelen) { 227 if (!lsp) { 228 snprintf(buf,sizeof(buf),"%d",*name); 229 if (req->oldidx) 230 error = SYSCTL_OUT(req, ".", 1); 231 if (!error) 232 error = SYSCTL_OUT(req, buf, strlen(buf)); 233 if (error) 234 return (error); 235 namelen--; 236 name++; 237 continue; 238 } 239 SLIST_FOREACH(oid, lsp, oid_link) { 240 if (oid->oid_number != *name) 241 continue; 242 243 if (req->oldidx) 244 error = SYSCTL_OUT(req, ".", 1); 245 if (!error) 246 error = SYSCTL_OUT(req, oid->oid_name, 247 strlen(oid->oid_name)); 248 if (error) 249 return (error); 250 251 namelen--; 252 name++; 253 254 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 255 break; 256 257 if (oid->oid_handler) 258 break; 259 260 lsp = (struct sysctl_oid_list *)oid->oid_arg1; 261 break; 262 } 263 } 264 return (SYSCTL_OUT(req, "", 1)); 265} 266 267SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 268 269static int 270sysctl_sysctl_next_ls (struct sysctl_oid_list *lsp, int *name, u_int namelen, 271 int *next, int *len, int level, struct sysctl_oid **oidpp) 272{ 273 struct sysctl_oid *oidp; 274 275 *len = level; 276 SLIST_FOREACH(oidp, lsp, oid_link) { 277 *next = oidp->oid_number; 278 *oidpp = oidp; 279 280 if (!namelen) { 281 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 282 return 0; 283 if (oidp->oid_handler) 284 /* We really should call the handler here...*/ 285 return 0; 286 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 287 if (!sysctl_sysctl_next_ls (lsp, 0, 0, next+1, 288 len, level+1, oidpp)) 289 return 0; 290 goto next; 291 } 292 293 if (oidp->oid_number < *name) 294 continue; 295 296 if (oidp->oid_number > *name) { 297 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 298 return 0; 299 if (oidp->oid_handler) 300 return 0; 301 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 302 if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1, 303 next+1, len, level+1, oidpp)) 304 return (0); 305 goto next; 306 } 307 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 308 continue; 309 310 if (oidp->oid_handler) 311 continue; 312 313 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 314 if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1, next+1, 315 len, level+1, oidpp)) 316 return (0); 317 next: 318 namelen = 1; 319 *len = level; 320 } 321 return 1; 322} 323 324static int 325sysctl_sysctl_next SYSCTL_HANDLER_ARGS 326{ 327 int *name = (int *) arg1; 328 u_int namelen = arg2; 329 int i, j, error; 330 struct sysctl_oid *oid; 331 struct sysctl_oid_list *lsp = &sysctl__children; 332 int newoid[CTL_MAXNAME]; 333 334 i = sysctl_sysctl_next_ls (lsp, name, namelen, newoid, &j, 1, &oid); 335 if (i) 336 return ENOENT; 337 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 338 return (error); 339} 340 341SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 342 343static int 344name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp) 345{ 346 int i; 347 struct sysctl_oid *oidp; 348 struct sysctl_oid_list *lsp = &sysctl__children; 349 char *p; 350 351 if (!*name) 352 return ENOENT; 353 354 p = name + strlen(name) - 1 ; 355 if (*p == '.') 356 *p = '\0'; 357 358 *len = 0; 359 360 for (p = name; *p && *p != '.'; p++) 361 ; 362 i = *p; 363 if (i == '.') 364 *p = '\0'; 365 366 oidp = SLIST_FIRST(lsp); 367 368 while (oidp && *len < CTL_MAXNAME) { 369 if (strcmp(name, oidp->oid_name)) { 370 oidp = SLIST_NEXT(oidp, oid_link); 371 continue; 372 } 373 *oid++ = oidp->oid_number; 374 (*len)++; 375 376 if (!i) { 377 if (oidpp) 378 *oidpp = oidp; 379 return (0); 380 } 381 382 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 383 break; 384 385 if (oidp->oid_handler) 386 break; 387 388 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 389 oidp = SLIST_FIRST(lsp); 390 name = p+1; 391 for (p = name; *p && *p != '.'; p++) 392 ; 393 i = *p; 394 if (i == '.') 395 *p = '\0'; 396 } 397 return ENOENT; 398} 399 400static int 401sysctl_sysctl_name2oid SYSCTL_HANDLER_ARGS 402{ 403 char *p; 404 int error, oid[CTL_MAXNAME], len; 405 struct sysctl_oid *op = 0; 406 407 if (!req->newlen) 408 return ENOENT; 409 410 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 411 412 error = SYSCTL_IN(req, p, req->newlen); 413 if (error) { 414 free(p, M_SYSCTL); 415 return (error); 416 } 417 418 p [req->newlen] = '\0'; 419 420 error = name2oid(p, oid, &len, &op); 421 422 free(p, M_SYSCTL); 423 424 if (error) 425 return (error); 426 427 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 428 return (error); 429} 430 431SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 432 sysctl_sysctl_name2oid, "I", ""); 433 434static int 435sysctl_sysctl_oidfmt SYSCTL_HANDLER_ARGS 436{ 437 int *name = (int *) arg1, error; 438 u_int namelen = arg2; 439 int indx; 440 struct sysctl_oid *oid; 441 struct sysctl_oid_list *lsp = &sysctl__children; 442 443 oid = SLIST_FIRST(lsp); 444 445 indx = 0; 446 while (oid && indx < CTL_MAXNAME) { 447 if (oid->oid_number == name[indx]) { 448 indx++; 449 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 450 if (oid->oid_handler) 451 goto found; 452 if (indx == namelen) 453 goto found; 454 lsp = (struct sysctl_oid_list *)oid->oid_arg1; 455 oid = SLIST_FIRST(lsp); 456 } else { 457 if (indx != namelen) 458 return EISDIR; 459 goto found; 460 } 461 } else { 462 oid = SLIST_NEXT(oid, oid_link); 463 } 464 } 465 return ENOENT; 466found: 467 if (!oid->oid_fmt) 468 return ENOENT; 469 error = SYSCTL_OUT(req, 470 &oid->oid_kind, sizeof(oid->oid_kind)); 471 if (!error) 472 error = SYSCTL_OUT(req, oid->oid_fmt, 473 strlen(oid->oid_fmt)+1); 474 return (error); 475} 476 477 478SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, ""); 479 480/* 481 * Default "handler" functions. 482 */ 483 484/* 485 * Handle an int, signed or unsigned. 486 * Two cases: 487 * a variable: point arg1 at it. 488 * a constant: pass it in arg2. 489 */ 490 491int 492sysctl_handle_int SYSCTL_HANDLER_ARGS 493{ 494 int error = 0; 495 496 if (arg1) 497 error = SYSCTL_OUT(req, arg1, sizeof(int)); 498 else 499 error = SYSCTL_OUT(req, &arg2, sizeof(int)); 500 501 if (error || !req->newptr) 502 return (error); 503 504 if (!arg1) 505 error = EPERM; 506 else 507 error = SYSCTL_IN(req, arg1, sizeof(int)); 508 return (error); 509} 510 511/* 512 * Handle a long, signed or unsigned. 513 * Two cases: 514 * a variable: point arg1 at it. 515 * a constant: pass it in arg2. 516 */ 517 518int 519sysctl_handle_long SYSCTL_HANDLER_ARGS 520{ 521 int error = 0; 522 523 error = SYSCTL_OUT(req, arg1, sizeof(long)); 524 525 if (error || !req->newptr) 526 return (error); 527 528 if (!arg1) 529 error = EPERM; 530 else 531 error = SYSCTL_IN(req, arg1, sizeof(long)); 532 return (error); 533} 534 535/* 536 * Handle our generic '\0' terminated 'C' string. 537 * Two cases: 538 * a variable string: point arg1 at it, arg2 is max length. 539 * a constant string: point arg1 at it, arg2 is zero. 540 */ 541 542int 543sysctl_handle_string SYSCTL_HANDLER_ARGS 544{ 545 int error=0; 546 547 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1); 548 549 if (error || !req->newptr || !arg2) 550 return (error); 551 552 if ((req->newlen - req->newidx) > arg2) { 553 error = E2BIG; 554 } else { 555 arg2 = (req->newlen - req->newidx); 556 error = SYSCTL_IN(req, arg1, arg2); 557 ((char *)arg1)[arg2] = '\0'; 558 } 559 560 return (error); 561} 562 563/* 564 * Handle any kind of opaque data. 565 * arg1 points to it, arg2 is the size. 566 */ 567 568int 569sysctl_handle_opaque SYSCTL_HANDLER_ARGS 570{ 571 int error; 572 573 error = SYSCTL_OUT(req, arg1, arg2); 574 575 if (error || !req->newptr) 576 return (error); 577 578 error = SYSCTL_IN(req, arg1, arg2); 579 580 return (error); 581} 582 583/* 584 * Transfer functions to/from kernel space. 585 * XXX: rather untested at this point 586 */ 587static int 588sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 589{ 590 size_t i = 0; 591 592 if (req->oldptr) { 593 i = l; 594 if (i > req->oldlen - req->oldidx) 595 i = req->oldlen - req->oldidx; 596 if (i > 0) 597 bcopy(p, (char *)req->oldptr + req->oldidx, i); 598 } 599 req->oldidx += l; 600 if (req->oldptr && i != l) 601 return (ENOMEM); 602 return (0); 603} 604 605static int 606sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 607{ 608 if (!req->newptr) 609 return 0; 610 if (req->newlen - req->newidx < l) 611 return (EINVAL); 612 bcopy((char *)req->newptr + req->newidx, p, l); 613 req->newidx += l; 614 return (0); 615} 616 617int 618kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval) 619{ 620 int error = 0; 621 struct sysctl_req req; 622 623 bzero(&req, sizeof req); 624 625 req.p = p; 626 627 if (oldlenp) { 628 req.oldlen = *oldlenp; 629 } 630 631 if (old) { 632 req.oldptr= old; 633 } 634 635 if (newlen) { 636 req.newlen = newlen; 637 req.newptr = new; 638 } 639 640 req.oldfunc = sysctl_old_kernel; 641 req.newfunc = sysctl_new_kernel; 642 req.lock = 1; 643 644 /* XXX this should probably be done in a general way */ 645 while (memlock.sl_lock) { 646 memlock.sl_want = 1; 647 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 648 memlock.sl_locked++; 649 } 650 memlock.sl_lock = 1; 651 652 error = sysctl_root(0, name, namelen, &req); 653 654 if (req.lock == 2) 655 vsunlock(req.oldptr, req.oldlen, B_WRITE); 656 657 memlock.sl_lock = 0; 658 659 if (memlock.sl_want) { 660 memlock.sl_want = 0; 661 wakeup((caddr_t)&memlock); 662 } 663 664 if (error && error != ENOMEM) 665 return (error); 666 667 if (retval) { 668 if (req.oldptr && req.oldidx > req.oldlen) 669 *retval = req.oldlen; 670 else 671 *retval = req.oldidx; 672 } 673 return (error); 674} 675 676/* 677 * Transfer function to/from user space. 678 */ 679static int 680sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 681{ 682 int error = 0; 683 size_t i = 0; 684 685 if (req->lock == 1 && req->oldptr) { 686 vslock(req->oldptr, req->oldlen); 687 req->lock = 2; 688 } 689 if (req->oldptr) { 690 i = l; 691 if (i > req->oldlen - req->oldidx) 692 i = req->oldlen - req->oldidx; 693 if (i > 0) 694 error = copyout(p, (char *)req->oldptr + req->oldidx, 695 i); 696 } 697 req->oldidx += l; 698 if (error) 699 return (error); 700 if (req->oldptr && i < l) 701 return (ENOMEM); 702 return (0); 703} 704 705static int 706sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 707{ 708 int error; 709 710 if (!req->newptr) 711 return 0; 712 if (req->newlen - req->newidx < l) 713 return (EINVAL); 714 error = copyin((char *)req->newptr + req->newidx, p, l); 715 req->newidx += l; 716 return (error); 717} 718 719/* 720 * Traverse our tree, and find the right node, execute whatever it points 721 * at, and return the resulting error code. 722 */ 723 724int 725sysctl_root SYSCTL_HANDLER_ARGS 726{ 727 int *name = (int *) arg1; 728 u_int namelen = arg2; 729 int indx, i; 730 struct sysctl_oid *oid; 731 struct sysctl_oid_list *lsp = &sysctl__children; 732 733 oid = SLIST_FIRST(lsp); 734 735 indx = 0; 736 while (oid && indx < CTL_MAXNAME) { 737 if (oid->oid_number == name[indx]) { 738 indx++; 739 if (oid->oid_kind & CTLFLAG_NOLOCK) 740 req->lock = 0; 741 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 742 if (oid->oid_handler) 743 goto found; 744 if (indx == namelen) 745 return ENOENT; 746 lsp = (struct sysctl_oid_list *)oid->oid_arg1; 747 oid = SLIST_FIRST(lsp); 748 } else { 749 if (indx != namelen) 750 return EISDIR; 751 goto found; 752 } 753 } else { 754 oid = SLIST_NEXT(oid, oid_link); 755 } 756 } 757 return ENOENT; 758found: 759 /* If writing isn't allowed */ 760 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) || 761 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0))) 762 return (EPERM); 763 764 /* Most likely only root can write */ 765 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && 766 req->newptr && req->p && 767 (i = suser(req->p->p_ucred, &req->p->p_acflag))) 768 return (i); 769 770 if (!oid->oid_handler) 771 return EINVAL; 772 773 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 774 i = (oid->oid_handler) (oid, 775 name + indx, namelen - indx, 776 req); 777 } else { 778 i = (oid->oid_handler) (oid, 779 oid->oid_arg1, oid->oid_arg2, 780 req); 781 } 782 return (i); 783} 784 785#ifndef _SYS_SYSPROTO_H_ 786struct sysctl_args { 787 int *name; 788 u_int namelen; 789 void *old; 790 size_t *oldlenp; 791 void *new; 792 size_t newlen; 793}; 794#endif 795 796int 797__sysctl(struct proc *p, struct sysctl_args *uap) 798{ 799 int error, i, name[CTL_MAXNAME]; 800 size_t j; 801 802 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 803 return (EINVAL); 804 805 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 806 if (error) 807 return (error); 808 809 error = userland_sysctl(p, name, uap->namelen, 810 uap->old, uap->oldlenp, 0, 811 uap->new, uap->newlen, &j); 812 if (error && error != ENOMEM) 813 return (error); 814 if (uap->oldlenp) { 815 i = copyout(&j, uap->oldlenp, sizeof(j)); 816 if (i) 817 return (i); 818 } 819 return (error); 820} 821 822/* 823 * This is used from various compatibility syscalls too. That's why name 824 * must be in kernel space. 825 */ 826int 827userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval) 828{ 829 int error = 0; 830 struct sysctl_req req, req2; 831 832 bzero(&req, sizeof req); 833 834 req.p = p; 835 836 if (oldlenp) { 837 if (inkernel) { 838 req.oldlen = *oldlenp; 839 } else { 840 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 841 if (error) 842 return (error); 843 } 844 } 845 846 if (old) { 847 if (!useracc(old, req.oldlen, B_WRITE)) 848 return (EFAULT); 849 req.oldptr= old; 850 } 851 852 if (newlen) { 853 if (!useracc(new, req.newlen, B_READ)) 854 return (EFAULT); 855 req.newlen = newlen; 856 req.newptr = new; 857 } 858 859 req.oldfunc = sysctl_old_user; 860 req.newfunc = sysctl_new_user; 861 req.lock = 1; 862 863 /* XXX this should probably be done in a general way */ 864 while (memlock.sl_lock) { 865 memlock.sl_want = 1; 866 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 867 memlock.sl_locked++; 868 } 869 memlock.sl_lock = 1; 870 871 do { 872 req2 = req; 873 error = sysctl_root(0, name, namelen, &req2); 874 } while (error == EAGAIN); 875 876 req = req2; 877 if (req.lock == 2) 878 vsunlock(req.oldptr, req.oldlen, B_WRITE); 879 880 memlock.sl_lock = 0; 881 882 if (memlock.sl_want) { 883 memlock.sl_want = 0; 884 wakeup((caddr_t)&memlock); 885 } 886 887 if (error && error != ENOMEM) 888 return (error); 889 890 if (retval) { 891 if (req.oldptr && req.oldidx > req.oldlen) 892 *retval = req.oldlen; 893 else 894 *retval = req.oldidx; 895 } 896 return (error); 897} 898 899#ifdef COMPAT_43 900#include <sys/socket.h> 901#include <vm/vm_param.h> 902 903#define KINFO_PROC (0<<8) 904#define KINFO_RT (1<<8) 905#define KINFO_VNODE (2<<8) 906#define KINFO_FILE (3<<8) 907#define KINFO_METER (4<<8) 908#define KINFO_LOADAVG (5<<8) 909#define KINFO_CLOCKRATE (6<<8) 910 911/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 912#define KINFO_BSDI_SYSINFO (101<<8) 913 914/* 915 * XXX this is bloat, but I hope it's better here than on the potentially 916 * limited kernel stack... -Peter 917 */ 918 919static struct { 920 int bsdi_machine; /* "i386" on BSD/386 */ 921/* ^^^ this is an offset to the string, relative to the struct start */ 922 char *pad0; 923 long pad1; 924 long pad2; 925 long pad3; 926 u_long pad4; 927 u_long pad5; 928 u_long pad6; 929 930 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 931 int bsdi_osrelease; /* "1.1" on BSD/386 */ 932 long pad7; 933 long pad8; 934 char *pad9; 935 936 long pad10; 937 long pad11; 938 int pad12; 939 long pad13; 940 quad_t pad14; 941 long pad15; 942 943 struct timeval pad16; 944 /* we dont set this, because BSDI's uname used gethostname() instead */ 945 int bsdi_hostname; /* hostname on BSD/386 */ 946 947 /* the actual string data is appended here */ 948 949} bsdi_si; 950/* 951 * this data is appended to the end of the bsdi_si structure during copyout. 952 * The "char *" offsets are relative to the base of the bsdi_si struct. 953 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 954 * should not exceed the length of the buffer here... (or else!! :-) 955 */ 956static char bsdi_strings[80]; /* It had better be less than this! */ 957 958#ifndef _SYS_SYSPROTO_H_ 959struct getkerninfo_args { 960 int op; 961 char *where; 962 size_t *size; 963 int arg; 964}; 965#endif 966 967int 968ogetkerninfo(struct proc *p, struct getkerninfo_args *uap) 969{ 970 int error, name[6]; 971 size_t size; 972 973 switch (uap->op & 0xff00) { 974 975 case KINFO_RT: 976 name[0] = CTL_NET; 977 name[1] = PF_ROUTE; 978 name[2] = 0; 979 name[3] = (uap->op & 0xff0000) >> 16; 980 name[4] = uap->op & 0xff; 981 name[5] = uap->arg; 982 error = userland_sysctl(p, name, 6, uap->where, uap->size, 983 0, 0, 0, &size); 984 break; 985 986 case KINFO_VNODE: 987 name[0] = CTL_KERN; 988 name[1] = KERN_VNODE; 989 error = userland_sysctl(p, name, 2, uap->where, uap->size, 990 0, 0, 0, &size); 991 break; 992 993 case KINFO_PROC: 994 name[0] = CTL_KERN; 995 name[1] = KERN_PROC; 996 name[2] = uap->op & 0xff; 997 name[3] = uap->arg; 998 error = userland_sysctl(p, name, 4, uap->where, uap->size, 999 0, 0, 0, &size); 1000 break; 1001 1002 case KINFO_FILE: 1003 name[0] = CTL_KERN; 1004 name[1] = KERN_FILE; 1005 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1006 0, 0, 0, &size); 1007 break; 1008 1009 case KINFO_METER: 1010 name[0] = CTL_VM; 1011 name[1] = VM_METER; 1012 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1013 0, 0, 0, &size); 1014 break; 1015 1016 case KINFO_LOADAVG: 1017 name[0] = CTL_VM; 1018 name[1] = VM_LOADAVG; 1019 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1020 0, 0, 0, &size); 1021 break; 1022 1023 case KINFO_CLOCKRATE: 1024 name[0] = CTL_KERN; 1025 name[1] = KERN_CLOCKRATE; 1026 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1027 0, 0, 0, &size); 1028 break; 1029 1030 case KINFO_BSDI_SYSINFO: { 1031 /* 1032 * this is pretty crude, but it's just enough for uname() 1033 * from BSDI's 1.x libc to work. 1034 * 1035 * In particular, it doesn't return the same results when 1036 * the supplied buffer is too small. BSDI's version apparently 1037 * will return the amount copied, and set the *size to how 1038 * much was needed. The emulation framework here isn't capable 1039 * of that, so we just set both to the amount copied. 1040 * BSDI's 2.x product apparently fails with ENOMEM in this 1041 * scenario. 1042 */ 1043 1044 u_int needed; 1045 u_int left; 1046 char *s; 1047 1048 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1049 bzero(bsdi_strings, sizeof(bsdi_strings)); 1050 1051 s = bsdi_strings; 1052 1053 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1054 strcpy(s, ostype); 1055 s += strlen(s) + 1; 1056 1057 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1058 strcpy(s, osrelease); 1059 s += strlen(s) + 1; 1060 1061 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1062 strcpy(s, machine); 1063 s += strlen(s) + 1; 1064 1065 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1066 1067 if (uap->where == NULL) { 1068 /* process is asking how much buffer to supply.. */ 1069 size = needed; 1070 error = 0; 1071 break; 1072 } 1073 1074 1075 /* if too much buffer supplied, trim it down */ 1076 if (size > needed) 1077 size = needed; 1078 1079 /* how much of the buffer is remaining */ 1080 left = size; 1081 1082 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1083 break; 1084 1085 /* is there any point in continuing? */ 1086 if (left > sizeof(bsdi_si)) { 1087 left -= sizeof(bsdi_si); 1088 error = copyout(&bsdi_strings, 1089 uap->where + sizeof(bsdi_si), left); 1090 } 1091 break; 1092 } 1093 1094 default: 1095 return (EOPNOTSUPP); 1096 } 1097 if (error) 1098 return (error); 1099 p->p_retval[0] = size; 1100 if (uap->size) 1101 error = copyout((caddr_t)&size, (caddr_t)uap->size, 1102 sizeof(size)); 1103 return (error); 1104} 1105#endif /* COMPAT_43 */ 1106