kern_sysctl.c revision 12279
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.40 1995/11/14 09:05:40 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_DUMPDEV: 733 ndumpdev = dumpdev; 734 error = sysctl_struct(oldp, oldlenp, newp, newlen, &ndumpdev, 735 sizeof ndumpdev); 736 if (!error && ndumpdev != dumpdev) { 737 error = setdumpdev(ndumpdev); 738 } 739 return error; 740 default: 741 return (EOPNOTSUPP); 742 } 743 /* NOTREACHED */ 744} 745 746/* 747 * hardware related system variables. 748 */ 749int 750hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 751 int *name; 752 u_int namelen; 753 void *oldp; 754 size_t *oldlenp; 755 void *newp; 756 size_t newlen; 757 struct proc *p; 758{ 759 /* almost all sysctl names at this level are terminal */ 760 if (namelen != 1 && name[0] != HW_DEVCONF) 761 return (ENOTDIR); /* overloaded */ 762 763 switch (name[0]) { 764 case HW_PHYSMEM: 765 return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem))); 766 case HW_USERMEM: 767 return (sysctl_rdint(oldp, oldlenp, newp, 768 ctob(physmem - cnt.v_wire_count))); 769 case HW_DEVCONF: 770 return (dev_sysctl(name + 1, namelen - 1, oldp, oldlenp, 771 newp, newlen, p)); 772 default: 773 return (EOPNOTSUPP); 774 } 775 /* NOTREACHED */ 776} 777 778#ifdef DEBUG 779/* 780 * Debugging related system variables. 781 */ 782struct ctldebug debug0, debug1, debug2, debug3, debug4; 783struct ctldebug debug5, debug6, debug7, debug8, debug9; 784struct ctldebug debug10, debug11, debug12, debug13, debug14; 785struct ctldebug debug15, debug16, debug17, debug18, debug19; 786static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = { 787 &debug0, &debug1, &debug2, &debug3, &debug4, 788 &debug5, &debug6, &debug7, &debug8, &debug9, 789 &debug10, &debug11, &debug12, &debug13, &debug14, 790 &debug15, &debug16, &debug17, &debug18, &debug19, 791}; 792static int 793debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 794 int *name; 795 u_int namelen; 796 void *oldp; 797 size_t *oldlenp; 798 void *newp; 799 size_t newlen; 800 struct proc *p; 801{ 802 struct ctldebug *cdp; 803 804 /* all sysctl names at this level are name and field */ 805 if (namelen != 2) 806 return (ENOTDIR); /* overloaded */ 807 cdp = debugvars[name[0]]; 808 if (cdp->debugname == 0) 809 return (EOPNOTSUPP); 810 switch (name[1]) { 811 case CTL_DEBUG_NAME: 812 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname)); 813 case CTL_DEBUG_VALUE: 814 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar)); 815 default: 816 return (EOPNOTSUPP); 817 } 818 /* NOTREACHED */ 819} 820#endif /* DEBUG */ 821 822/* 823 * Validate parameters and get old / set new parameters 824 * for an integer-valued sysctl function. 825 */ 826int 827sysctl_int(oldp, oldlenp, newp, newlen, valp) 828 void *oldp; 829 size_t *oldlenp; 830 void *newp; 831 size_t newlen; 832 int *valp; 833{ 834 int error = 0; 835 836 if (oldp && *oldlenp < sizeof(int)) 837 return (ENOMEM); 838 if (newp && newlen != sizeof(int)) 839 return (EINVAL); 840 *oldlenp = sizeof(int); 841 if (oldp) 842 error = copyout(valp, oldp, sizeof(int)); 843 if (error == 0 && newp) 844 error = copyin(newp, valp, sizeof(int)); 845 return (error); 846} 847 848/* 849 * As above, but read-only. 850 */ 851int 852sysctl_rdint(oldp, oldlenp, newp, val) 853 void *oldp; 854 size_t *oldlenp; 855 void *newp; 856 int val; 857{ 858 int error = 0; 859 860 if (oldp && *oldlenp < sizeof(int)) 861 return (ENOMEM); 862 if (newp) 863 return (EPERM); 864 *oldlenp = sizeof(int); 865 if (oldp) 866 error = copyout((caddr_t)&val, oldp, sizeof(int)); 867 return (error); 868} 869 870/* 871 * Validate parameters and get old / set new parameters 872 * for a string-valued sysctl function. 873 */ 874int 875sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen) 876 void *oldp; 877 size_t *oldlenp; 878 void *newp; 879 size_t newlen; 880 char *str; 881 int maxlen; 882{ 883 int len, error = 0, rval = 0; 884 885 len = strlen(str) + 1; 886 if (oldp && *oldlenp < len) { 887 len = *oldlenp; 888 rval = ENOMEM; 889 } 890 if (newp && newlen >= maxlen) 891 return (EINVAL); 892 if (oldp) { 893 *oldlenp = len; 894 error = copyout(str, oldp, len); 895 if (error) 896 rval = error; 897 } 898 if ((error == 0 || error == ENOMEM) && newp) { 899 error = copyin(newp, str, newlen); 900 if (error) 901 rval = error; 902 str[newlen] = 0; 903 } 904 return (rval); 905} 906 907/* 908 * As above, but read-only. 909 */ 910int 911sysctl_rdstring(oldp, oldlenp, newp, str) 912 void *oldp; 913 size_t *oldlenp; 914 void *newp; 915 char *str; 916{ 917 int len, error = 0, rval = 0; 918 919 len = strlen(str) + 1; 920 if (oldp && *oldlenp < len) { 921 len = *oldlenp; 922 rval = ENOMEM; 923 } 924 if (newp) 925 return (EPERM); 926 *oldlenp = len; 927 if (oldp) 928 error = copyout(str, oldp, len); 929 if (error) 930 rval = error; 931 return (rval); 932} 933 934/* 935 * Validate parameters and get old / set new parameters 936 * for a structure oriented sysctl function. 937 */ 938int 939sysctl_struct(oldp, oldlenp, newp, newlen, sp, len) 940 void *oldp; 941 size_t *oldlenp; 942 void *newp; 943 size_t newlen; 944 void *sp; 945 int len; 946{ 947 int error = 0; 948 949 if (oldp && *oldlenp < len) 950 return (ENOMEM); 951 if (newp && newlen > len) 952 return (EINVAL); 953 if (oldp) { 954 *oldlenp = len; 955 error = copyout(sp, oldp, len); 956 } 957 if (error == 0 && newp) 958 error = copyin(newp, sp, len); 959 return (error); 960} 961 962/* 963 * Validate parameters and get old parameters 964 * for a structure oriented sysctl function. 965 */ 966int 967sysctl_rdstruct(oldp, oldlenp, newp, sp, len) 968 void *oldp; 969 size_t *oldlenp; 970 void *newp, *sp; 971 int len; 972{ 973 int error = 0; 974 975 if (oldp && *oldlenp < len) 976 return (ENOMEM); 977 if (newp) 978 return (EPERM); 979 *oldlenp = len; 980 if (oldp) 981 error = copyout(sp, oldp, len); 982 return (error); 983} 984 985#ifdef COMPAT_43 986#include <sys/socket.h> 987#define KINFO_PROC (0<<8) 988#define KINFO_RT (1<<8) 989#define KINFO_VNODE (2<<8) 990#define KINFO_FILE (3<<8) 991#define KINFO_METER (4<<8) 992#define KINFO_LOADAVG (5<<8) 993#define KINFO_CLOCKRATE (6<<8) 994 995/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 996#define KINFO_BSDI_SYSINFO (101<<8) 997 998/* 999 * XXX this is bloat, but I hope it's better here than on the potentially 1000 * limited kernel stack... -Peter 1001 */ 1002 1003struct { 1004 int bsdi_machine; /* "i386" on BSD/386 */ 1005/* ^^^ this is an offset to the string, relative to the struct start */ 1006 char *pad0; 1007 long pad1; 1008 long pad2; 1009 long pad3; 1010 u_long pad4; 1011 u_long pad5; 1012 u_long pad6; 1013 1014 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 1015 int bsdi_osrelease; /* "1.1" on BSD/386 */ 1016 long pad7; 1017 long pad8; 1018 char *pad9; 1019 1020 long pad10; 1021 long pad11; 1022 int pad12; 1023 long pad13; 1024 quad_t pad14; 1025 long pad15; 1026 1027 struct timeval pad16; 1028 /* we dont set this, because BSDI's uname used gethostname() instead */ 1029 int bsdi_hostname; /* hostname on BSD/386 */ 1030 1031 /* the actual string data is appended here */ 1032 1033} bsdi_si; 1034/* 1035 * this data is appended to the end of the bsdi_si structure during copyout. 1036 * The "char *" offsets are relative to the base of the bsdi_si struct. 1037 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 1038 * should not exceed the length of the buffer here... (or else!! :-) 1039 */ 1040char bsdi_strings[80]; /* It had better be less than this! */ 1041 1042#ifndef _SYS_SYSPROTO_H_ 1043struct getkerninfo_args { 1044 int op; 1045 char *where; 1046 int *size; 1047 int arg; 1048}; 1049#endif 1050 1051int 1052ogetkerninfo(p, uap, retval) 1053 struct proc *p; 1054 register struct getkerninfo_args *uap; 1055 int *retval; 1056{ 1057 int error, name[6]; 1058 u_int size; 1059 1060 switch (uap->op & 0xff00) { 1061 1062 case KINFO_RT: 1063 name[0] = CTL_NET; 1064 name[1] = PF_ROUTE; 1065 name[2] = 0; 1066 name[3] = (uap->op & 0xff0000) >> 16; 1067 name[4] = uap->op & 0xff; 1068 name[5] = uap->arg; 1069 error = userland_sysctl(p, name, 6, uap->where, uap->size, 1070 0, 0, 0, 0); 1071 break; 1072 1073 case KINFO_VNODE: 1074 name[0] = CTL_KERN; 1075 name[1] = KERN_VNODE; 1076 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1077 0, 0, 0, 0); 1078 break; 1079 1080 case KINFO_PROC: 1081 name[0] = CTL_KERN; 1082 name[1] = KERN_PROC; 1083 name[2] = uap->op & 0xff; 1084 name[3] = uap->arg; 1085 error = userland_sysctl(p, name, 4, uap->where, uap->size, 1086 0, 0, 0, 0); 1087 break; 1088 1089 case KINFO_FILE: 1090 name[0] = CTL_KERN; 1091 name[1] = KERN_FILE; 1092 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1093 0, 0, 0, 0); 1094 break; 1095 1096 case KINFO_METER: 1097 name[0] = CTL_VM; 1098 name[1] = VM_METER; 1099 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1100 0, 0, 0, 0); 1101 break; 1102 1103 case KINFO_LOADAVG: 1104 name[0] = CTL_VM; 1105 name[1] = VM_LOADAVG; 1106 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1107 0, 0, 0, 0); 1108 break; 1109 1110 case KINFO_CLOCKRATE: 1111 name[0] = CTL_KERN; 1112 name[1] = KERN_CLOCKRATE; 1113 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1114 0, 0, 0, 0); 1115 break; 1116 1117 case KINFO_BSDI_SYSINFO: { 1118 /* 1119 * this is pretty crude, but it's just enough for uname() 1120 * from BSDI's 1.x libc to work. 1121 * 1122 * In particular, it doesn't return the same results when 1123 * the supplied buffer is too small. BSDI's version apparently 1124 * will return the amount copied, and set the *size to how 1125 * much was needed. The emulation framework here isn't capable 1126 * of that, so we just set both to the amount copied. 1127 * BSDI's 2.x product apparently fails with ENOMEM in this 1128 * scenario. 1129 */ 1130 1131 u_int needed; 1132 u_int left; 1133 char *s; 1134 1135 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1136 bzero(bsdi_strings, sizeof(bsdi_strings)); 1137 1138 s = bsdi_strings; 1139 1140 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1141 strcpy(s, ostype); 1142 s += strlen(s) + 1; 1143 1144 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1145 strcpy(s, osrelease); 1146 s += strlen(s) + 1; 1147 1148 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1149 strcpy(s, machine); 1150 s += strlen(s) + 1; 1151 1152 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1153 1154 if (uap->where == NULL) { 1155 /* process is asking how much buffer to supply.. */ 1156 size = needed; 1157 error = 0; 1158 break; 1159 } 1160 1161 1162 /* if too much buffer supplied, trim it down */ 1163 if (size > needed) 1164 size = needed; 1165 1166 /* how much of the buffer is remaining */ 1167 left = size; 1168 1169 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1170 break; 1171 1172 /* is there any point in continuing? */ 1173 if (left > sizeof(bsdi_si)) { 1174 left -= sizeof(bsdi_si); 1175 error = copyout(&bsdi_strings, 1176 uap->where + sizeof(bsdi_si), left); 1177 } 1178 break; 1179 } 1180 1181 default: 1182 return (EOPNOTSUPP); 1183 } 1184 if (error) 1185 return (error); 1186 *retval = size; 1187 if (uap->size) 1188 error = copyout((caddr_t)&size, (caddr_t)uap->size, 1189 sizeof(size)); 1190 return (error); 1191} 1192#endif /* COMPAT_43 */ 1193