kern_sysctl.c revision 12278
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 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 37 * $Id: kern_sysctl.c,v 1.39 1995/11/13 13:54:09 phk Exp $ 38 */ 39 40/* 41 * sysctl system call. 42 */ 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/sysproto.h> 47#include <sys/kernel.h> 48#include <sys/malloc.h> 49#include <sys/proc.h> 50#include <sys/file.h> 51#include <sys/vnode.h> 52#include <sys/unistd.h> 53#include <sys/buf.h> 54#include <sys/ioctl.h> 55#include <sys/tty.h> 56#include <sys/conf.h> 57#include <vm/vm.h> 58#include <sys/sysctl.h> 59#include <sys/user.h> 60 61extern struct linker_set sysctl_; 62 63/* BEGIN_MIB */ 64SYSCTL_NODE(, 0, sysctl, CTLFLAG_RW, 0, 65 "Sysctl internal magic"); 66SYSCTL_NODE(, CTL_KERN, kern, CTLFLAG_RW, 0, 67 "High kernel, proc, limits &c"); 68SYSCTL_NODE(, CTL_VM, vm, CTLFLAG_RW, 0, 69 "Virtual memory"); 70SYSCTL_NODE(, CTL_FS, fs, CTLFLAG_RW, 0, 71 "File system"); 72SYSCTL_NODE(, CTL_NET, net, CTLFLAG_RW, 0, 73 "Network, (see socket.h)"); 74SYSCTL_NODE(, CTL_DEBUG, debug, CTLFLAG_RW, 0, 75 "Debugging"); 76SYSCTL_NODE(, CTL_HW, hw, CTLFLAG_RW, 0, 77 "hardware"); 78SYSCTL_NODE(, CTL_MACHDEP, machdep, CTLFLAG_RW, 0, 79 "machine dependent"); 80SYSCTL_NODE(, CTL_USER, user, CTLFLAG_RW, 0, 81 "user-level"); 82 83SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease, CTLFLAG_RD, osrelease, 0, ""); 84 85SYSCTL_INT(_kern, KERN_OSREV, osrevision, CTLFLAG_RD, 0, BSD, ""); 86 87SYSCTL_STRING(_kern, KERN_VERSION, version, CTLFLAG_RD, version, 0, ""); 88 89SYSCTL_STRING(_kern, KERN_OSTYPE, ostype, CTLFLAG_RD, ostype, 0, ""); 90 91extern int osreldate; 92SYSCTL_INT(_kern, KERN_OSRELDATE, osreldate, CTLFLAG_RD, &osreldate, 0, ""); 93 94SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RD, &desiredvnodes, 0, ""); 95 96SYSCTL_INT(_kern, KERN_MAXPROC, maxproc, CTLFLAG_RD, &maxproc, 0, ""); 97 98SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, 99 CTLFLAG_RD, &maxprocperuid, 0, ""); 100 101SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, 102 CTLFLAG_RD, &maxfilesperproc, 0, ""); 103 104SYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD, 0, ARG_MAX, ""); 105 106SYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD, 0, _POSIX_VERSION, ""); 107 108SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD, 0, NGROUPS_MAX, ""); 109 110SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD, 0, 1, ""); 111 112SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, &maxfiles, 0, ""); 113 114#ifdef _POSIX_SAVED_IDS 115SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 1, ""); 116#else 117SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 0, ""); 118#endif 119 120char kernelname[MAXPATHLEN] = "/kernel"; /* XXX bloat */ 121 122SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, 123 CTLFLAG_RW, kernelname, sizeof kernelname, ""); 124 125SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, 126 CTLFLAG_RW, &boottime, timeval, ""); 127 128SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, ""); 129 130SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0, ""); 131 132SYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD, 0, 1, ""); 133 134SYSCTL_INT(_hw, HW_BYTEORDER, byteorder, CTLFLAG_RD, 0, BYTE_ORDER, ""); 135 136SYSCTL_INT(_hw, HW_PAGESIZE, pagesize, CTLFLAG_RD, 0, PAGE_SIZE, ""); 137 138/* END_MIB */ 139 140extern int vfs_update_wakeup; 141extern int vfs_update_interval; 142static int 143sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS 144{ 145 int error = sysctl_handle_int(oidp, 146 oidp->oid_arg1, oidp->oid_arg2, req); 147 if (!error) 148 wakeup(&vfs_update_wakeup); 149 return error; 150} 151 152SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW, 153 &vfs_update_interval, 0, sysctl_kern_updateinterval, ""); 154 155 156char hostname[MAXHOSTNAMELEN]; 157int hostnamelen; 158static int 159sysctl_kern_hostname SYSCTL_HANDLER_ARGS 160{ 161 int error = sysctl_handle_string(oidp, 162 oidp->oid_arg1, oidp->oid_arg2, req); 163 if (req->newptr && (error == 0 || error == ENOMEM)) 164 hostnamelen = req->newlen; 165 return error; 166} 167 168SYSCTL_PROC(_kern, KERN_HOSTNAME, hostname, CTLTYPE_STRING|CTLFLAG_RW, 169 &hostname, sizeof(hostname), sysctl_kern_hostname, ""); 170 171static int 172sysctl_order_cmp(const void *a, const void *b) 173{ 174 const struct sysctl_oid **pa, **pb; 175 176 pa = (const struct sysctl_oid **)a; 177 pb = (const struct sysctl_oid **)b; 178 if (*pa == NULL) 179 return (1); 180 if (*pb == NULL) 181 return (-1); 182 return ((*pa)->oid_number - (*pb)->oid_number); 183} 184 185static void 186sysctl_order(void *arg) 187{ 188 int j; 189 struct linker_set *l = (struct linker_set *) arg; 190 struct sysctl_oid **oidpp; 191 192 j = l->ls_length; 193 oidpp = (struct sysctl_oid **) l->ls_items; 194 for (; j--; oidpp++) { 195 if (!*oidpp) 196 continue; 197 if ((*oidpp)->oid_arg1 == arg) { 198 *oidpp = 0; 199 continue; 200 } 201 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) 202 if (!(*oidpp)->oid_handler) 203 sysctl_order((*oidpp)->oid_arg1); 204 } 205 qsort(l->ls_items, l->ls_length, sizeof l->ls_items[0], 206 sysctl_order_cmp); 207} 208 209SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_order, &sysctl_); 210 211static void 212sysctl_sysctl_debug_dump_node(struct linker_set *l, int i) 213{ 214 int j, k; 215 struct sysctl_oid **oidpp; 216 217 j = l->ls_length; 218 oidpp = (struct sysctl_oid **) l->ls_items; 219 for (; j--; oidpp++) { 220 221 if (!*oidpp) 222 continue; 223 224 for (k=0; k<i; k++) 225 printf(" "); 226 227 if ((*oidpp)->oid_number > 100) { 228 printf("Junk! %p # %d %s k %x a1 %p a2 %x h %p\n", 229 *oidpp, 230 (*oidpp)->oid_number, (*oidpp)->oid_name, 231 (*oidpp)->oid_kind, (*oidpp)->oid_arg1, 232 (*oidpp)->oid_arg2, (*oidpp)->oid_handler); 233 continue; 234 } 235 printf("%d %s ", (*oidpp)->oid_number, (*oidpp)->oid_name); 236 237 printf("%c%c", 238 (*oidpp)->oid_kind & CTLFLAG_RD ? 'R':' ', 239 (*oidpp)->oid_kind & CTLFLAG_WR ? 'W':' '); 240 241 switch ((*oidpp)->oid_kind & CTLTYPE) { 242 case CTLTYPE_NODE: 243 if ((*oidpp)->oid_handler) { 244 printf(" Node(proc)\n"); 245 } else { 246 printf(" Node\n"); 247 sysctl_sysctl_debug_dump_node( 248 (*oidpp)->oid_arg1, i+2); 249 } 250 break; 251 case CTLTYPE_INT: printf(" Int\n"); break; 252 case CTLTYPE_STRING: printf(" String\n"); break; 253 case CTLTYPE_QUAD: printf(" Quad\n"); break; 254 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 255 default: printf("\n"); 256 } 257 258 } 259} 260 261 262static int 263sysctl_sysctl_debug SYSCTL_HANDLER_ARGS 264{ 265 sysctl_sysctl_debug_dump_node(&sysctl_, 0); 266 return ENOENT; 267} 268 269SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 270 0, 0, sysctl_sysctl_debug, ""); 271 272char domainname[MAXHOSTNAMELEN]; 273int domainnamelen; 274static int 275sysctl_kern_domainname SYSCTL_HANDLER_ARGS 276{ 277 int error = sysctl_handle_string(oidp, 278 oidp->oid_arg1, oidp->oid_arg2, req); 279 if (req->newptr && (error == 0 || error == ENOMEM)) 280 domainnamelen = req->newlen; 281 return error; 282} 283 284SYSCTL_PROC(_kern, KERN_DOMAINNAME, domainname, CTLTYPE_STRING|CTLFLAG_RW, 285 &domainname, sizeof(domainname), sysctl_kern_domainname, ""); 286 287long hostid; 288/* Some trouble here, if sizeof (int) != sizeof (long) */ 289SYSCTL_INT(_kern, KERN_HOSTID, hostid, CTLFLAG_RW, &hostid, 0, ""); 290 291/* 292 * Handle an integer, signed or unsigned. 293 * Two cases: 294 * a variable: point arg1 at it. 295 * a constant: pass it in arg2. 296 */ 297 298int 299sysctl_handle_int SYSCTL_HANDLER_ARGS 300{ 301 int error = 0; 302 303 if (arg1) 304 error = SYSCTL_OUT(req, arg1, sizeof(int)); 305 else if (arg2) 306 error = SYSCTL_OUT(req, &arg2, sizeof(int)); 307 308 if (error || !req->newptr) 309 return (error); 310 311 if (!arg1) 312 error = EPERM; 313 else 314 error = SYSCTL_IN(req, arg1, sizeof(int)); 315 return (error); 316} 317 318/* 319 * Handle our generic '\0' terminated 'C' string. 320 * Two cases: 321 * a variable string: point arg1 at it, arg2 is max length. 322 * a constant string: point arg1 at it, arg2 is zero. 323 */ 324 325int 326sysctl_handle_string SYSCTL_HANDLER_ARGS 327{ 328 int error=0; 329 330 if (arg2) 331 error = SYSCTL_OUT(req, arg1, arg2); 332 else 333 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1); 334 335 if (error || !req->newptr || !arg2) 336 return (error); 337 338 if ((req->newlen - req->newidx) > arg2) { 339 error = E2BIG; 340 } else { 341 arg2 = (req->newlen - req->newidx); 342 error = SYSCTL_IN(req, arg1, arg2); 343 ((char *)arg1)[arg2] = '\0'; 344 } 345 346 return (error); 347} 348 349/* 350 * Handle any kind of opaque data. 351 * arg1 points to it, arg2 is the size. 352 */ 353 354int 355sysctl_handle_opaque SYSCTL_HANDLER_ARGS 356{ 357 int error; 358 359 error = SYSCTL_OUT(req, arg1, arg2); 360 361 if (error || !req->newptr) 362 return (error); 363 364 error = SYSCTL_IN(req, arg1, arg2); 365 366 return (error); 367} 368 369/* 370 * Transfer functions to/from kernel space. 371 * XXX: rather untested at this point 372 */ 373static int 374sysctl_old_kernel(struct sysctl_req *req, void *p, int l) 375{ 376 int i = 0; 377 378 if (req->oldptr) { 379 i = min(req->oldlen - req->oldidx, l); 380 if (i > 0) 381 bcopy(p, req->oldptr + req->oldidx, i); 382 } 383 req->oldidx += l; 384 if (i != l) 385 return (ENOMEM); 386 return (0); 387 388} 389 390static int 391sysctl_new_kernel(struct sysctl_req *req, void *p, int l) 392{ 393 int i; 394 if (!req->newptr) 395 return 0; 396 if (req->newlen - req->newidx < l) 397 return (EINVAL); 398 bcopy(req->newptr + req->newidx, p, l); 399 req->newidx += l; 400 return (0); 401} 402 403/* 404 * Transfer function to/from user space. 405 */ 406static int 407sysctl_old_user(struct sysctl_req *req, void *p, int l) 408{ 409 int error = 0, i = 0; 410 411 if (req->oldptr) { 412 i = min(req->oldlen - req->oldidx, l); 413 if (i > 0) 414 error = copyout(p, req->oldptr + req->oldidx, i); 415 } 416 req->oldidx += l; 417 if (error) 418 return (error); 419 if (req->oldptr && i < l) 420 return (ENOMEM); 421 return (0); 422} 423 424static int 425sysctl_new_user(struct sysctl_req *req, void *p, int l) 426{ 427 int error, i; 428 429 if (!req->newptr) 430 return 0; 431 if (req->newlen - req->newidx < l) 432 return (EINVAL); 433 error = copyin(req->newptr + req->newidx, p, l); 434 req->newidx += l; 435 return (error); 436} 437 438#ifdef DEBUG 439static sysctlfn debug_sysctl; 440#endif 441 442/* 443 * Locking and stats 444 */ 445static struct sysctl_lock { 446 int sl_lock; 447 int sl_want; 448 int sl_locked; 449} memlock; 450 451 452 453/* 454 * Traverse our tree, and find the right node, execute whatever it points 455 * at, and return the resulting error code. 456 * We work entirely in kernel-space at this time. 457 */ 458 459 460int 461sysctl_root SYSCTL_HANDLER_ARGS 462{ 463 int *name = (int *) arg1; 464 int namelen = arg2; 465 int indx, i, j; 466 struct sysctl_oid **oidpp; 467 struct linker_set *lsp = &sysctl_; 468 469 j = lsp->ls_length; 470 oidpp = (struct sysctl_oid **) lsp->ls_items; 471 472 indx = 0; 473 while (j-- && indx < CTL_MAXNAME) { 474 if (*oidpp && ((*oidpp)->oid_number == name[indx])) { 475 indx++; 476 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 477 if ((*oidpp)->oid_handler) 478 goto found; 479 if (indx == namelen) 480 return ENOENT; 481 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 482 j = lsp->ls_length; 483 oidpp = (struct sysctl_oid **)lsp->ls_items; 484 } else { 485 if (indx != namelen) 486 return EISDIR; 487 goto found; 488 } 489 } else { 490 oidpp++; 491 } 492 } 493 return ENOENT; 494found: 495 496 /* If writing isn't allowed */ 497 if (req->newptr && !((*oidpp)->oid_kind & CTLFLAG_WR)) 498 return (EPERM); 499 500 if (!(*oidpp)->oid_handler) 501 return EINVAL; 502 503 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 504 i = ((*oidpp)->oid_handler) (*oidpp, 505 name + indx, namelen - indx, 506 req); 507 } else { 508 i = ((*oidpp)->oid_handler) (*oidpp, 509 (*oidpp)->oid_arg1, (*oidpp)->oid_arg2, 510 req); 511 } 512 return (i); 513} 514 515#ifndef _SYS_SYSPROTO_H_ 516struct sysctl_args { 517 int *name; 518 u_int namelen; 519 void *old; 520 size_t *oldlenp; 521 void *new; 522 size_t newlen; 523}; 524#endif 525 526int 527__sysctl(p, uap, retval) 528 struct proc *p; 529 register struct sysctl_args *uap; 530 int *retval; 531{ 532 int error, i, j, name[CTL_MAXNAME]; 533 534 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 535 return (EINVAL); 536 537 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 538 if (error) 539 return (error); 540 541 error = userland_sysctl(p, name, uap->namelen, 542 uap->old, uap->oldlenp, 0, 543 uap->new, uap->newlen, &j); 544 if (error && error != ENOMEM) 545 return (error); 546 if (uap->oldlenp) { 547 i = copyout(&j, uap->oldlenp, sizeof(j)); 548 if (i) 549 return (i); 550 } 551 return (error); 552} 553 554static sysctlfn kern_sysctl; 555 556/* 557 * This is used from various compatibility syscalls too. That's why name 558 * must be in kernel space. 559 */ 560int 561userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, int *retval) 562{ 563 int error = 0, dolock = 1, i, oldlen = 0; 564 u_int savelen = 0; 565 sysctlfn *fn; 566 struct sysctl_req req; 567 568 bzero(&req, sizeof req); 569 570 if (new != NULL && (error = suser(p->p_ucred, &p->p_acflag))) 571 return (error); 572 573 if (oldlenp) { 574 if (inkernel) { 575 req.oldlen = *oldlenp; 576 } else { 577 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 578 if (error) 579 return (error); 580 } 581 } 582 583 if (old) { 584 if (!useracc(old, req.oldlen, B_WRITE)) 585 return (EFAULT); 586 req.oldptr= old; 587 } 588 589 if (newlen) { 590 if (!useracc(new, req.newlen, B_READ)) 591 return (EFAULT); 592 req.newlen = newlen; 593 req.newptr = new; 594 } 595 596 req.oldfunc = sysctl_old_user; 597 req.newfunc = sysctl_new_user; 598 599 error = sysctl_root(0, name, namelen, &req); 600 601/* 602 if (error && error != ENOMEM) 603 return (error); 604*/ 605 if (error == ENOENT) 606 goto oldstuff; 607 608 if (retval) { 609 if (req.oldptr && req.oldidx > req.oldlen) 610 *retval = req.oldlen; 611 else 612 *retval = req.oldidx; 613 } 614 return (error); 615 616oldstuff: 617 oldlen = req.oldlen; 618 619 switch (name[0]) { 620 case CTL_KERN: 621 fn = kern_sysctl; 622 if (name[1] != KERN_VNODE) /* XXX */ 623 dolock = 0; 624 break; 625 case CTL_HW: 626 fn = hw_sysctl; 627 break; 628 case CTL_VM: 629 fn = vm_sysctl; 630 break; 631 case CTL_NET: 632 fn = net_sysctl; 633 break; 634 case CTL_FS: 635 fn = fs_sysctl; 636 break; 637#ifdef DEBUG 638 case CTL_DEBUG: 639 fn = debug_sysctl; 640 break; 641#endif 642 default: 643 return (EOPNOTSUPP); 644 } 645 if (old != NULL) { 646 if (!useracc(old, oldlen, B_WRITE)) 647 return (EFAULT); 648 while (memlock.sl_lock) { 649 memlock.sl_want = 1; 650 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 651 memlock.sl_locked++; 652 } 653 memlock.sl_lock = 1; 654 if (dolock) 655 vslock(old, oldlen); 656 savelen = oldlen; 657 } 658 659 660 error = (*fn)(name + 1, namelen - 1, old, &oldlen, 661 new, newlen, p); 662 663 664 if (old != NULL) { 665 if (dolock) 666 vsunlock(old, savelen, B_WRITE); 667 memlock.sl_lock = 0; 668 if (memlock.sl_want) { 669 memlock.sl_want = 0; 670 wakeup((caddr_t)&memlock); 671 } 672 } 673#if 0 674 if (error) { 675 printf("SYSCTL_ERROR: "); 676 for(i=0;i<namelen;i++) 677 printf("%d ", name[i]); 678 printf("= %d\n", error); 679 } 680#endif 681 if (error) 682 return (error); 683 if (retval) 684 *retval = oldlen; 685 return (error); 686} 687 688/* 689 * Attributes stored in the kernel. 690 */ 691int securelevel = -1; 692 693/* 694 * kernel related system variables. 695 */ 696static int 697kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 698 int *name; 699 u_int namelen; 700 void *oldp; 701 size_t *oldlenp; 702 void *newp; 703 size_t newlen; 704 struct proc *p; 705{ 706 int error, level; 707 dev_t ndumpdev; 708 709 /* all sysctl names at this level are terminal */ 710 if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF 711 || name[0] == KERN_NTP_PLL)) 712 return (ENOTDIR); /* overloaded */ 713 714 switch (name[0]) { 715 716 case KERN_SECURELVL: 717 level = securelevel; 718 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) || 719 newp == NULL) 720 return (error); 721 if (level < securelevel && p->p_pid != 1) 722 return (EPERM); 723 securelevel = level; 724 return (0); 725 case KERN_VNODE: 726 return (sysctl_vnode(oldp, oldlenp)); 727#ifdef GPROF 728 case KERN_PROF: 729 return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp, 730 newp, newlen)); 731#endif 732 case KERN_NTP_PLL: 733 return (ntp_sysctl(name + 1, namelen - 1, oldp, oldlenp, 734 newp, newlen, p)); 735 case KERN_DUMPDEV: 736 ndumpdev = dumpdev; 737 error = sysctl_struct(oldp, oldlenp, newp, newlen, &ndumpdev, 738 sizeof ndumpdev); 739 if (!error && ndumpdev != dumpdev) { 740 error = setdumpdev(ndumpdev); 741 } 742 return error; 743 default: 744 return (EOPNOTSUPP); 745 } 746 /* NOTREACHED */ 747} 748 749/* 750 * hardware related system variables. 751 */ 752int 753hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 754 int *name; 755 u_int namelen; 756 void *oldp; 757 size_t *oldlenp; 758 void *newp; 759 size_t newlen; 760 struct proc *p; 761{ 762 /* almost all sysctl names at this level are terminal */ 763 if (namelen != 1 && name[0] != HW_DEVCONF) 764 return (ENOTDIR); /* overloaded */ 765 766 switch (name[0]) { 767 case HW_PHYSMEM: 768 return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem))); 769 case HW_USERMEM: 770 return (sysctl_rdint(oldp, oldlenp, newp, 771 ctob(physmem - cnt.v_wire_count))); 772 case HW_DEVCONF: 773 return (dev_sysctl(name + 1, namelen - 1, oldp, oldlenp, 774 newp, newlen, p)); 775 default: 776 return (EOPNOTSUPP); 777 } 778 /* NOTREACHED */ 779} 780 781#ifdef DEBUG 782/* 783 * Debugging related system variables. 784 */ 785struct ctldebug debug0, debug1, debug2, debug3, debug4; 786struct ctldebug debug5, debug6, debug7, debug8, debug9; 787struct ctldebug debug10, debug11, debug12, debug13, debug14; 788struct ctldebug debug15, debug16, debug17, debug18, debug19; 789static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = { 790 &debug0, &debug1, &debug2, &debug3, &debug4, 791 &debug5, &debug6, &debug7, &debug8, &debug9, 792 &debug10, &debug11, &debug12, &debug13, &debug14, 793 &debug15, &debug16, &debug17, &debug18, &debug19, 794}; 795static int 796debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 797 int *name; 798 u_int namelen; 799 void *oldp; 800 size_t *oldlenp; 801 void *newp; 802 size_t newlen; 803 struct proc *p; 804{ 805 struct ctldebug *cdp; 806 807 /* all sysctl names at this level are name and field */ 808 if (namelen != 2) 809 return (ENOTDIR); /* overloaded */ 810 cdp = debugvars[name[0]]; 811 if (cdp->debugname == 0) 812 return (EOPNOTSUPP); 813 switch (name[1]) { 814 case CTL_DEBUG_NAME: 815 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname)); 816 case CTL_DEBUG_VALUE: 817 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar)); 818 default: 819 return (EOPNOTSUPP); 820 } 821 /* NOTREACHED */ 822} 823#endif /* DEBUG */ 824 825/* 826 * Validate parameters and get old / set new parameters 827 * for an integer-valued sysctl function. 828 */ 829int 830sysctl_int(oldp, oldlenp, newp, newlen, valp) 831 void *oldp; 832 size_t *oldlenp; 833 void *newp; 834 size_t newlen; 835 int *valp; 836{ 837 int error = 0; 838 839 if (oldp && *oldlenp < sizeof(int)) 840 return (ENOMEM); 841 if (newp && newlen != sizeof(int)) 842 return (EINVAL); 843 *oldlenp = sizeof(int); 844 if (oldp) 845 error = copyout(valp, oldp, sizeof(int)); 846 if (error == 0 && newp) 847 error = copyin(newp, valp, sizeof(int)); 848 return (error); 849} 850 851/* 852 * As above, but read-only. 853 */ 854int 855sysctl_rdint(oldp, oldlenp, newp, val) 856 void *oldp; 857 size_t *oldlenp; 858 void *newp; 859 int val; 860{ 861 int error = 0; 862 863 if (oldp && *oldlenp < sizeof(int)) 864 return (ENOMEM); 865 if (newp) 866 return (EPERM); 867 *oldlenp = sizeof(int); 868 if (oldp) 869 error = copyout((caddr_t)&val, oldp, sizeof(int)); 870 return (error); 871} 872 873/* 874 * Validate parameters and get old / set new parameters 875 * for a string-valued sysctl function. 876 */ 877int 878sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen) 879 void *oldp; 880 size_t *oldlenp; 881 void *newp; 882 size_t newlen; 883 char *str; 884 int maxlen; 885{ 886 int len, error = 0, rval = 0; 887 888 len = strlen(str) + 1; 889 if (oldp && *oldlenp < len) { 890 len = *oldlenp; 891 rval = ENOMEM; 892 } 893 if (newp && newlen >= maxlen) 894 return (EINVAL); 895 if (oldp) { 896 *oldlenp = len; 897 error = copyout(str, oldp, len); 898 if (error) 899 rval = error; 900 } 901 if ((error == 0 || error == ENOMEM) && newp) { 902 error = copyin(newp, str, newlen); 903 if (error) 904 rval = error; 905 str[newlen] = 0; 906 } 907 return (rval); 908} 909 910/* 911 * As above, but read-only. 912 */ 913int 914sysctl_rdstring(oldp, oldlenp, newp, str) 915 void *oldp; 916 size_t *oldlenp; 917 void *newp; 918 char *str; 919{ 920 int len, error = 0, rval = 0; 921 922 len = strlen(str) + 1; 923 if (oldp && *oldlenp < len) { 924 len = *oldlenp; 925 rval = ENOMEM; 926 } 927 if (newp) 928 return (EPERM); 929 *oldlenp = len; 930 if (oldp) 931 error = copyout(str, oldp, len); 932 if (error) 933 rval = error; 934 return (rval); 935} 936 937/* 938 * Validate parameters and get old / set new parameters 939 * for a structure oriented sysctl function. 940 */ 941int 942sysctl_struct(oldp, oldlenp, newp, newlen, sp, len) 943 void *oldp; 944 size_t *oldlenp; 945 void *newp; 946 size_t newlen; 947 void *sp; 948 int len; 949{ 950 int error = 0; 951 952 if (oldp && *oldlenp < len) 953 return (ENOMEM); 954 if (newp && newlen > len) 955 return (EINVAL); 956 if (oldp) { 957 *oldlenp = len; 958 error = copyout(sp, oldp, len); 959 } 960 if (error == 0 && newp) 961 error = copyin(newp, sp, len); 962 return (error); 963} 964 965/* 966 * Validate parameters and get old parameters 967 * for a structure oriented sysctl function. 968 */ 969int 970sysctl_rdstruct(oldp, oldlenp, newp, sp, len) 971 void *oldp; 972 size_t *oldlenp; 973 void *newp, *sp; 974 int len; 975{ 976 int error = 0; 977 978 if (oldp && *oldlenp < len) 979 return (ENOMEM); 980 if (newp) 981 return (EPERM); 982 *oldlenp = len; 983 if (oldp) 984 error = copyout(sp, oldp, len); 985 return (error); 986} 987 988#ifdef COMPAT_43 989#include <sys/socket.h> 990#define KINFO_PROC (0<<8) 991#define KINFO_RT (1<<8) 992#define KINFO_VNODE (2<<8) 993#define KINFO_FILE (3<<8) 994#define KINFO_METER (4<<8) 995#define KINFO_LOADAVG (5<<8) 996#define KINFO_CLOCKRATE (6<<8) 997 998/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 999#define KINFO_BSDI_SYSINFO (101<<8) 1000 1001/* 1002 * XXX this is bloat, but I hope it's better here than on the potentially 1003 * limited kernel stack... -Peter 1004 */ 1005 1006struct { 1007 int bsdi_machine; /* "i386" on BSD/386 */ 1008/* ^^^ this is an offset to the string, relative to the struct start */ 1009 char *pad0; 1010 long pad1; 1011 long pad2; 1012 long pad3; 1013 u_long pad4; 1014 u_long pad5; 1015 u_long pad6; 1016 1017 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 1018 int bsdi_osrelease; /* "1.1" on BSD/386 */ 1019 long pad7; 1020 long pad8; 1021 char *pad9; 1022 1023 long pad10; 1024 long pad11; 1025 int pad12; 1026 long pad13; 1027 quad_t pad14; 1028 long pad15; 1029 1030 struct timeval pad16; 1031 /* we dont set this, because BSDI's uname used gethostname() instead */ 1032 int bsdi_hostname; /* hostname on BSD/386 */ 1033 1034 /* the actual string data is appended here */ 1035 1036} bsdi_si; 1037/* 1038 * this data is appended to the end of the bsdi_si structure during copyout. 1039 * The "char *" offsets are relative to the base of the bsdi_si struct. 1040 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 1041 * should not exceed the length of the buffer here... (or else!! :-) 1042 */ 1043char bsdi_strings[80]; /* It had better be less than this! */ 1044 1045#ifndef _SYS_SYSPROTO_H_ 1046struct getkerninfo_args { 1047 int op; 1048 char *where; 1049 int *size; 1050 int arg; 1051}; 1052#endif 1053 1054int 1055ogetkerninfo(p, uap, retval) 1056 struct proc *p; 1057 register struct getkerninfo_args *uap; 1058 int *retval; 1059{ 1060 int error, name[6]; 1061 u_int size; 1062 1063 switch (uap->op & 0xff00) { 1064 1065 case KINFO_RT: 1066 name[0] = CTL_NET; 1067 name[1] = PF_ROUTE; 1068 name[2] = 0; 1069 name[3] = (uap->op & 0xff0000) >> 16; 1070 name[4] = uap->op & 0xff; 1071 name[5] = uap->arg; 1072 error = userland_sysctl(p, name, 6, uap->where, uap->size, 1073 0, 0, 0, 0); 1074 break; 1075 1076 case KINFO_VNODE: 1077 name[0] = CTL_KERN; 1078 name[1] = KERN_VNODE; 1079 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1080 0, 0, 0, 0); 1081 break; 1082 1083 case KINFO_PROC: 1084 name[0] = CTL_KERN; 1085 name[1] = KERN_PROC; 1086 name[2] = uap->op & 0xff; 1087 name[3] = uap->arg; 1088 error = userland_sysctl(p, name, 4, uap->where, uap->size, 1089 0, 0, 0, 0); 1090 break; 1091 1092 case KINFO_FILE: 1093 name[0] = CTL_KERN; 1094 name[1] = KERN_FILE; 1095 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1096 0, 0, 0, 0); 1097 break; 1098 1099 case KINFO_METER: 1100 name[0] = CTL_VM; 1101 name[1] = VM_METER; 1102 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1103 0, 0, 0, 0); 1104 break; 1105 1106 case KINFO_LOADAVG: 1107 name[0] = CTL_VM; 1108 name[1] = VM_LOADAVG; 1109 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1110 0, 0, 0, 0); 1111 break; 1112 1113 case KINFO_CLOCKRATE: 1114 name[0] = CTL_KERN; 1115 name[1] = KERN_CLOCKRATE; 1116 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1117 0, 0, 0, 0); 1118 break; 1119 1120 case KINFO_BSDI_SYSINFO: { 1121 /* 1122 * this is pretty crude, but it's just enough for uname() 1123 * from BSDI's 1.x libc to work. 1124 * 1125 * In particular, it doesn't return the same results when 1126 * the supplied buffer is too small. BSDI's version apparently 1127 * will return the amount copied, and set the *size to how 1128 * much was needed. The emulation framework here isn't capable 1129 * of that, so we just set both to the amount copied. 1130 * BSDI's 2.x product apparently fails with ENOMEM in this 1131 * scenario. 1132 */ 1133 1134 u_int needed; 1135 u_int left; 1136 char *s; 1137 1138 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1139 bzero(bsdi_strings, sizeof(bsdi_strings)); 1140 1141 s = bsdi_strings; 1142 1143 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1144 strcpy(s, ostype); 1145 s += strlen(s) + 1; 1146 1147 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1148 strcpy(s, osrelease); 1149 s += strlen(s) + 1; 1150 1151 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1152 strcpy(s, machine); 1153 s += strlen(s) + 1; 1154 1155 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1156 1157 if (uap->where == NULL) { 1158 /* process is asking how much buffer to supply.. */ 1159 size = needed; 1160 error = 0; 1161 break; 1162 } 1163 1164 1165 /* if too much buffer supplied, trim it down */ 1166 if (size > needed) 1167 size = needed; 1168 1169 /* how much of the buffer is remaining */ 1170 left = size; 1171 1172 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1173 break; 1174 1175 /* is there any point in continuing? */ 1176 if (left > sizeof(bsdi_si)) { 1177 left -= sizeof(bsdi_si); 1178 error = copyout(&bsdi_strings, 1179 uap->where + sizeof(bsdi_si), left); 1180 } 1181 break; 1182 } 1183 1184 default: 1185 return (EOPNOTSUPP); 1186 } 1187 if (error) 1188 return (error); 1189 *retval = size; 1190 if (uap->size) 1191 error = copyout((caddr_t)&size, (caddr_t)uap->size, 1192 sizeof(size)); 1193 return (error); 1194} 1195#endif /* COMPAT_43 */ 1196