kern_sysctl.c revision 12187
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.33 1995/11/09 20:22: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/kernel.h> 47#include <sys/malloc.h> 48#include <sys/proc.h> 49#include <sys/file.h> 50#include <sys/vnode.h> 51#include <sys/unistd.h> 52#include <sys/buf.h> 53#include <sys/ioctl.h> 54#include <sys/tty.h> 55#include <sys/conf.h> 56#include <vm/vm.h> 57#include <sys/sysctl.h> 58#include <sys/user.h> 59 60extern struct linker_set sysctl_; 61 62/* BEGIN_MIB */ 63SYSCTL_NODE(, 0, sysctl, CTLFLAG_RW, 0, 64 "Sysctl internal magic"); 65SYSCTL_NODE(, CTL_KERN, kern, CTLFLAG_RW, 0, 66 "High kernel, proc, limits &c"); 67SYSCTL_NODE(, CTL_VM, vm, CTLFLAG_RW, 0, 68 "Virtual memory"); 69SYSCTL_NODE(, CTL_FS, fs, CTLFLAG_RW, 0, 70 "File system"); 71SYSCTL_NODE(, CTL_NET, net, CTLFLAG_RW, 0, 72 "Network, (see socket.h)"); 73SYSCTL_NODE(, CTL_DEBUG, debug, CTLFLAG_RW, 0, 74 "Debugging"); 75SYSCTL_NODE(, CTL_HW, hw, CTLFLAG_RW, 0, 76 "hardware"); 77SYSCTL_NODE(, CTL_MACHDEP, machdep, CTLFLAG_RW, 0, 78 "machine dependent"); 79SYSCTL_NODE(, CTL_USER, user, CTLFLAG_RW, 0, 80 "user-level"); 81 82SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease, CTLFLAG_RD, osrelease, 0, ""); 83 84SYSCTL_INT(_kern, KERN_OSREV, osrevision, CTLFLAG_RD, 0, BSD, ""); 85 86SYSCTL_STRING(_kern, KERN_VERSION, version, CTLFLAG_RD, version, 0, ""); 87 88SYSCTL_STRING(_kern, KERN_OSTYPE, ostype, CTLFLAG_RD, ostype, 0, ""); 89 90extern int osreldate; 91SYSCTL_INT(_kern, KERN_OSRELDATE, osreldate, CTLFLAG_RD, &osreldate, 0, ""); 92 93SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RD, &desiredvnodes, 0, ""); 94 95SYSCTL_INT(_kern, KERN_MAXPROC, maxproc, CTLFLAG_RD, &maxproc, 0, ""); 96 97SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, 98 CTLFLAG_RD, &maxprocperuid, 0, ""); 99 100SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, 101 CTLFLAG_RD, &maxfilesperproc, 0, ""); 102 103SYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD, 0, ARG_MAX, ""); 104 105SYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD, 0, _POSIX_VERSION, ""); 106 107SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD, 0, NGROUPS_MAX, ""); 108 109SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD, 0, 1, ""); 110 111SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, &maxfiles, 0, ""); 112 113#ifdef _POSIX_SAVED_IDS 114SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 1, ""); 115#else 116SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 0, ""); 117#endif 118 119char kernelname[MAXPATHLEN] = "/kernel"; /* XXX bloat */ 120 121SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, 122 CTLFLAG_RW, kernelname, sizeof kernelname, ""); 123 124SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, 125 CTLFLAG_RW, &boottime, timeval, ""); 126 127SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, ""); 128 129SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0, ""); 130 131SYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD, 0, 1, ""); 132 133SYSCTL_INT(_hw, HW_BYTEORDER, byteorder, CTLFLAG_RD, 0, BYTE_ORDER, ""); 134 135SYSCTL_INT(_hw, HW_PAGESIZE, pagesize, CTLFLAG_RD, 0, PAGE_SIZE, ""); 136 137/* END_MIB */ 138 139extern int vfs_update_wakeup; 140extern int vfs_update_interval; 141static int 142sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS 143{ 144 int error = sysctl_handle_int(oidp, 145 oidp->oid_arg1, oidp->oid_arg2, 146 oldp, oldlenp, newp, newlen); 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, 163 oldp, oldlenp, newp, newlen); 164 if (newp && (error == 0 || error == ENOMEM)) 165 hostnamelen = newlen; 166 return error; 167} 168 169SYSCTL_PROC(_kern, KERN_HOSTNAME, hostname, CTLTYPE_STRING|CTLFLAG_RW, 170 &hostname, sizeof(hostname), sysctl_kern_hostname, ""); 171 172static int 173sysctl_order_cmp(void *a, void *b) 174{ 175 struct sysctl_oid **pa,**pb; 176 pa = (struct sysctl_oid**) a; 177 pb = (struct sysctl_oid**) b; 178 if (!*pa) return 1; 179 if (!*pb) return -1; 180 return ((*pa)->oid_number - (*pb)->oid_number); 181} 182 183static void 184sysctl_order(void *arg) 185{ 186 int j; 187 struct linker_set *l = (struct linker_set *) arg; 188 struct sysctl_oid **oidpp; 189 190 j = l->ls_length; 191 oidpp = (struct sysctl_oid **) l->ls_items; 192 for (; j--; oidpp++) { 193 if (!*oidpp) 194 continue; 195 if ((*oidpp)->oid_arg1 == arg) { 196 *oidpp = 0; 197 continue; 198 } 199 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) 200 if (!(*oidpp)->oid_handler) 201 sysctl_order((*oidpp)->oid_arg1); 202 } 203 qsort(l->ls_items, l->ls_length, sizeof l->ls_items[0], 204 sysctl_order_cmp); 205} 206 207SYSINIT(sysctl,SI_SUB_KMEM,SI_ORDER_ANY,sysctl_order,&sysctl_); 208 209static void 210sysctl_sysctl_debug_dump_node(struct linker_set *l,int i) 211{ 212 int j,k; 213 struct sysctl_oid **oidpp; 214 215 j = l->ls_length; 216 oidpp = (struct sysctl_oid **) l->ls_items; 217 for (; j--; oidpp++) { 218 219 if (!*oidpp) 220 continue; 221 222 for (k=0; k<i; k++) 223 printf(" "); 224 225 if ((*oidpp)->oid_number > 100) { 226 printf("Junk! %p # %d %s k %x a1 %p a2 %x h %p\n", 227 *oidpp, 228 (*oidpp)->oid_number, (*oidpp)->oid_name, 229 (*oidpp)->oid_kind, (*oidpp)->oid_arg1, 230 (*oidpp)->oid_arg2, (*oidpp)->oid_handler); 231 continue; 232 } 233 printf("%d %s ", (*oidpp)->oid_number, (*oidpp)->oid_name); 234 235 printf("%c%c", 236 (*oidpp)->oid_kind & CTLFLAG_RD ? 'R':' ', 237 (*oidpp)->oid_kind & CTLFLAG_WR ? 'W':' '); 238 239 switch ((*oidpp)->oid_kind & CTLTYPE) { 240 case CTLTYPE_NODE: 241 if ((*oidpp)->oid_handler) { 242 printf(" Node(proc)\n"); 243 } else { 244 printf(" Node\n"); 245 sysctl_sysctl_debug_dump_node( 246 (*oidpp)->oid_arg1,i+2); 247 } 248 break; 249 case CTLTYPE_INT: printf(" Int\n"); break; 250 case CTLTYPE_STRING: printf(" String\n"); break; 251 case CTLTYPE_QUAD: printf(" Quad\n"); break; 252 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 253 default: printf("\n"); 254 } 255 256 } 257} 258 259 260static int 261sysctl_sysctl_debug SYSCTL_HANDLER_ARGS 262{ 263 sysctl_sysctl_debug_dump_node(&sysctl_,0); 264 return ENOENT; 265} 266 267SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 268 0, 0, sysctl_sysctl_debug, ""); 269 270char domainname[MAXHOSTNAMELEN]; 271int domainnamelen; 272static int 273sysctl_kern_domainname SYSCTL_HANDLER_ARGS 274{ 275 int error = sysctl_handle_string(oidp, 276 oidp->oid_arg1, oidp->oid_arg2, 277 oldp, oldlenp, newp, newlen); 278 if (newp && (error == 0 || error == ENOMEM)) 279 domainnamelen = newlen; 280 return error; 281} 282 283SYSCTL_PROC(_kern, KERN_DOMAINNAME, domainname, CTLTYPE_STRING|CTLFLAG_RW, 284 &domainname, sizeof(domainname), sysctl_kern_domainname, ""); 285 286long hostid; 287/* Some trouble here, if sizeof (int) != sizeof (long) */ 288SYSCTL_INT(_kern, KERN_HOSTID, hostid, CTLFLAG_RW, &hostid, 0, ""); 289 290int 291sysctl_handle_int SYSCTL_HANDLER_ARGS 292{ 293 /* If there isn't sufficient space to return */ 294 if (oldp && *oldlenp < sizeof(int)) 295 return (ENOMEM); 296 297 /* If it is a constant, don't write */ 298 if (newp && !arg1) 299 return (EPERM); 300 301 /* If we get more than an int */ 302 if (newp && newlen != sizeof(int)) 303 return (EINVAL); 304 305 *oldlenp = sizeof(int); 306 if (oldp && arg1 ) 307 bcopy(arg1, oldp, sizeof(int)); 308 else if (oldp) 309 bcopy(&arg2, oldp, sizeof(int)); 310 if (newp) 311 bcopy(newp, arg1, sizeof(int)); 312 return (0); 313} 314 315int 316sysctl_handle_string SYSCTL_HANDLER_ARGS 317{ 318 int len, error=0; 319 char *str = (char *)arg1; 320 321 len = strlen(str) + 1; 322 323 if (oldp && *oldlenp < len) { 324 len = *oldlenp; 325 error=ENOMEM; 326 } 327 328 if (newp && newlen >= arg2) 329 return (EINVAL); 330 331 if (oldp) { 332 *oldlenp = len; 333 bcopy(str, oldp, len); 334 } 335 336 if (newp) { 337 bcopy(newp, str, newlen); 338 str[newlen] = 0; 339 } 340 return (error); 341} 342 343int 344sysctl_handle_opaque SYSCTL_HANDLER_ARGS 345{ 346 if (oldp && *oldlenp < arg2) 347 return (ENOMEM); 348 349 if (newp && newlen != arg2) 350 return (EINVAL); 351 352 if (oldp) { 353 *oldlenp = arg2; 354 bcopy(arg1, oldp, arg2); 355 } 356 if (newp) 357 bcopy(newp, arg1, arg2); 358 return (0); 359} 360 361#ifdef DEBUG 362static sysctlfn debug_sysctl; 363#endif 364 365/* 366 * Locking and stats 367 */ 368static struct sysctl_lock { 369 int sl_lock; 370 int sl_want; 371 int sl_locked; 372} memlock; 373 374 375 376/* 377 * Traverse our tree, and find the right node, execute whatever it points 378 * at, and return the resulting error code. 379 * We work entirely in kernel-space at this time. 380 */ 381 382 383int 384sysctl_root SYSCTL_HANDLER_ARGS 385{ 386 int *name = (int *) arg1; 387 int namelen = arg2; 388 int indx, i, j; 389 struct sysctl_oid **oidpp; 390 struct linker_set *lsp = &sysctl_; 391 392 j = lsp->ls_length; 393 oidpp = (struct sysctl_oid **) lsp->ls_items; 394 395 indx = 0; 396 while (j-- && indx < CTL_MAXNAME) { 397 if (*oidpp && ((*oidpp)->oid_number == name[indx])) { 398 indx++; 399 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 400 if ((*oidpp)->oid_handler) 401 goto found; 402 if (indx == namelen) 403 return ENOENT; 404 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 405 j = lsp->ls_length; 406 oidpp = (struct sysctl_oid **)lsp->ls_items; 407 } else { 408 if (indx != namelen) 409 return EISDIR; 410 goto found; 411 } 412 } else { 413 oidpp++; 414 } 415 } 416 return EJUSTRETURN; 417found: 418 419 /* If writing isn't allowed */ 420 if (newp && !((*oidpp)->oid_kind & CTLFLAG_WR)) 421 return (EPERM); 422 423 if (!(*oidpp)->oid_handler) 424 return EINVAL; 425 426 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 427 i = ((*oidpp)->oid_handler) (*oidpp, 428 name + indx, namelen - indx, 429 oldp, oldlenp, newp, newlen); 430 } else { 431 i = ((*oidpp)->oid_handler) (*oidpp, 432 (*oidpp)->oid_arg1, (*oidpp)->oid_arg2, 433 oldp, oldlenp, newp, newlen); 434 } 435 return (i); 436} 437 438struct sysctl_args { 439 int *name; 440 u_int namelen; 441 void *old; 442 size_t *oldlenp; 443 void *new; 444 size_t newlen; 445}; 446 447int 448__sysctl(p, uap, retval) 449 struct proc *p; 450 register struct sysctl_args *uap; 451 int *retval; 452{ 453 int error, name[CTL_MAXNAME]; 454 455 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 456 return (EINVAL); 457 458 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 459 if (error) 460 return (error); 461 462 return (userland_sysctl(p, name, uap->namelen, 463 uap->old, uap->oldlenp, 0, 464 uap->new, uap->newlen, retval)); 465} 466 467static sysctlfn kern_sysctl; 468 469/* 470 * This is used from various compatibility syscalls too. That's why name 471 * must be in kernel space. 472 */ 473int 474userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, int *retval) 475{ 476 int error = 0, dolock = 1, i; 477 u_int savelen = 0, oldlen = 0; 478 sysctlfn *fn; 479 void *oldp = 0; 480 void *newp = 0; 481 482 if (new != NULL && (error = suser(p->p_ucred, &p->p_acflag))) 483 return (error); 484 485 if (oldlenp) { 486 if (inkernel) { 487 oldlen = *oldlenp; 488 } else { 489 error = copyin(oldlenp, &oldlen, sizeof(oldlen)); 490 if (error) 491 return (error); 492 } 493 } 494 495 if (old) 496 oldp = malloc(oldlen, M_TEMP, M_WAITOK); 497 498 if (newlen) { 499 newp = malloc(newlen, M_TEMP, M_WAITOK); 500 error = copyin(new, newp, newlen); 501 } 502 if (error) { 503 if (oldp) 504 free(oldp, M_TEMP); 505 if (newp) 506 free(newp, M_TEMP); 507 return error; 508 } 509 510 error = sysctl_root(0, name, namelen, oldp, &oldlen, 511 newp, newlen); 512 513 if (!error || error == ENOMEM) { 514 if (oldlenp) { 515 if (inkernel) { 516 *oldlenp = oldlen; 517 } else { 518 i = copyout(&oldlen, oldlenp, sizeof(oldlen)); 519 if (i) 520 error = i; 521 } 522 } 523 if ((error == ENOMEM || !error ) && oldp) { 524 i = copyout(oldp, old, oldlen); 525 if (i) 526 error = i; 527 free(oldp, M_TEMP); 528 } 529 if (newp) 530 free(newp, M_TEMP); 531 return (error); 532 } 533 534 if (oldp) 535 free(oldp, M_TEMP); 536 if (newp) 537 free(newp, M_TEMP); 538 539 switch (name[0]) { 540 case CTL_KERN: 541 fn = kern_sysctl; 542 if (name[1] != KERN_VNODE) /* XXX */ 543 dolock = 0; 544 break; 545 case CTL_HW: 546 fn = hw_sysctl; 547 break; 548 case CTL_VM: 549 fn = vm_sysctl; 550 break; 551 case CTL_NET: 552 fn = net_sysctl; 553 break; 554 case CTL_FS: 555 fn = fs_sysctl; 556 break; 557#ifdef DEBUG 558 case CTL_DEBUG: 559 fn = debug_sysctl; 560 break; 561#endif 562 default: 563 return (EOPNOTSUPP); 564 } 565 if (old != NULL) { 566 if (!useracc(old, oldlen, B_WRITE)) 567 return (EFAULT); 568 while (memlock.sl_lock) { 569 memlock.sl_want = 1; 570 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 571 memlock.sl_locked++; 572 } 573 memlock.sl_lock = 1; 574 if (dolock) 575 vslock(old, oldlen); 576 savelen = oldlen; 577 } 578 579 580 error = (*fn)(name + 1, namelen - 1, old, &oldlen, 581 new, newlen, p); 582 583 584 if (old != NULL) { 585 if (dolock) 586 vsunlock(old, savelen, B_WRITE); 587 memlock.sl_lock = 0; 588 if (memlock.sl_want) { 589 memlock.sl_want = 0; 590 wakeup((caddr_t)&memlock); 591 } 592 } 593#if 0 594 if (error) { 595 printf("SYSCTL_ERROR: "); 596 for(i=0;i<namelen;i++) 597 printf("%d ", name[i]); 598 printf("= %d\n", error); 599 } 600#endif 601 if (error) 602 return (error); 603 if (retval) 604 *retval = oldlen; 605 if (oldlenp) { 606 if (inkernel) { 607 *oldlenp = oldlen; 608 } else { 609 error = copyout(&oldlen, oldlenp, sizeof(oldlen)); 610 } 611 } 612 return (error); 613} 614 615/* 616 * Attributes stored in the kernel. 617 */ 618int securelevel = -1; 619 620/* 621 * kernel related system variables. 622 */ 623int 624kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 625 int *name; 626 u_int namelen; 627 void *oldp; 628 size_t *oldlenp; 629 void *newp; 630 size_t newlen; 631 struct proc *p; 632{ 633 int error, level; 634 dev_t ndumpdev; 635 636 /* all sysctl names at this level are terminal */ 637 if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF 638 || name[0] == KERN_NTP_PLL)) 639 return (ENOTDIR); /* overloaded */ 640 641 switch (name[0]) { 642 643 case KERN_SECURELVL: 644 level = securelevel; 645 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) || 646 newp == NULL) 647 return (error); 648 if (level < securelevel && p->p_pid != 1) 649 return (EPERM); 650 securelevel = level; 651 return (0); 652 case KERN_VNODE: 653 return (sysctl_vnode(oldp, oldlenp)); 654 case KERN_PROC: 655 return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp)); 656 case KERN_FILE: 657 return (sysctl_file(oldp, oldlenp)); 658#ifdef GPROF 659 case KERN_PROF: 660 return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp, 661 newp, newlen)); 662#endif 663 case KERN_NTP_PLL: 664 return (ntp_sysctl(name + 1, namelen - 1, oldp, oldlenp, 665 newp, newlen, p)); 666 case KERN_DUMPDEV: 667 ndumpdev = dumpdev; 668 error = sysctl_struct(oldp, oldlenp, newp, newlen, &ndumpdev, 669 sizeof ndumpdev); 670 if (!error && ndumpdev != dumpdev) { 671 error = setdumpdev(ndumpdev); 672 } 673 return error; 674 default: 675 return (EOPNOTSUPP); 676 } 677 /* NOTREACHED */ 678} 679 680/* 681 * hardware related system variables. 682 */ 683int 684hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 685 int *name; 686 u_int namelen; 687 void *oldp; 688 size_t *oldlenp; 689 void *newp; 690 size_t newlen; 691 struct proc *p; 692{ 693 /* almost all sysctl names at this level are terminal */ 694 if (namelen != 1 && name[0] != HW_DEVCONF) 695 return (ENOTDIR); /* overloaded */ 696 697 switch (name[0]) { 698 case HW_PHYSMEM: 699 return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem))); 700 case HW_USERMEM: 701 return (sysctl_rdint(oldp, oldlenp, newp, 702 ctob(physmem - cnt.v_wire_count))); 703 case HW_DEVCONF: 704 return (dev_sysctl(name + 1, namelen - 1, oldp, oldlenp, 705 newp, newlen, p)); 706 default: 707 return (EOPNOTSUPP); 708 } 709 /* NOTREACHED */ 710} 711 712#ifdef DEBUG 713/* 714 * Debugging related system variables. 715 */ 716struct ctldebug debug0, debug1, debug2, debug3, debug4; 717struct ctldebug debug5, debug6, debug7, debug8, debug9; 718struct ctldebug debug10, debug11, debug12, debug13, debug14; 719struct ctldebug debug15, debug16, debug17, debug18, debug19; 720static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = { 721 &debug0, &debug1, &debug2, &debug3, &debug4, 722 &debug5, &debug6, &debug7, &debug8, &debug9, 723 &debug10, &debug11, &debug12, &debug13, &debug14, 724 &debug15, &debug16, &debug17, &debug18, &debug19, 725}; 726static int 727debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 728 int *name; 729 u_int namelen; 730 void *oldp; 731 size_t *oldlenp; 732 void *newp; 733 size_t newlen; 734 struct proc *p; 735{ 736 struct ctldebug *cdp; 737 738 /* all sysctl names at this level are name and field */ 739 if (namelen != 2) 740 return (ENOTDIR); /* overloaded */ 741 cdp = debugvars[name[0]]; 742 if (cdp->debugname == 0) 743 return (EOPNOTSUPP); 744 switch (name[1]) { 745 case CTL_DEBUG_NAME: 746 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname)); 747 case CTL_DEBUG_VALUE: 748 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar)); 749 default: 750 return (EOPNOTSUPP); 751 } 752 /* NOTREACHED */ 753} 754#endif /* DEBUG */ 755 756/* 757 * Validate parameters and get old / set new parameters 758 * for an integer-valued sysctl function. 759 */ 760int 761sysctl_int(oldp, oldlenp, newp, newlen, valp) 762 void *oldp; 763 size_t *oldlenp; 764 void *newp; 765 size_t newlen; 766 int *valp; 767{ 768 int error = 0; 769 770 if (oldp && *oldlenp < sizeof(int)) 771 return (ENOMEM); 772 if (newp && newlen != sizeof(int)) 773 return (EINVAL); 774 *oldlenp = sizeof(int); 775 if (oldp) 776 error = copyout(valp, oldp, sizeof(int)); 777 if (error == 0 && newp) 778 error = copyin(newp, valp, sizeof(int)); 779 return (error); 780} 781 782/* 783 * As above, but read-only. 784 */ 785int 786sysctl_rdint(oldp, oldlenp, newp, val) 787 void *oldp; 788 size_t *oldlenp; 789 void *newp; 790 int val; 791{ 792 int error = 0; 793 794 if (oldp && *oldlenp < sizeof(int)) 795 return (ENOMEM); 796 if (newp) 797 return (EPERM); 798 *oldlenp = sizeof(int); 799 if (oldp) 800 error = copyout((caddr_t)&val, oldp, sizeof(int)); 801 return (error); 802} 803 804/* 805 * Validate parameters and get old / set new parameters 806 * for a string-valued sysctl function. 807 */ 808int 809sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen) 810 void *oldp; 811 size_t *oldlenp; 812 void *newp; 813 size_t newlen; 814 char *str; 815 int maxlen; 816{ 817 int len, error = 0, rval = 0; 818 819 len = strlen(str) + 1; 820 if (oldp && *oldlenp < len) { 821 len = *oldlenp; 822 rval = ENOMEM; 823 } 824 if (newp && newlen >= maxlen) 825 return (EINVAL); 826 if (oldp) { 827 *oldlenp = len; 828 error = copyout(str, oldp, len); 829 if (error) 830 rval = error; 831 } 832 if ((error == 0 || error == ENOMEM) && newp) { 833 error = copyin(newp, str, newlen); 834 if (error) 835 rval = error; 836 str[newlen] = 0; 837 } 838 return (rval); 839} 840 841/* 842 * As above, but read-only. 843 */ 844int 845sysctl_rdstring(oldp, oldlenp, newp, str) 846 void *oldp; 847 size_t *oldlenp; 848 void *newp; 849 char *str; 850{ 851 int len, error = 0, rval = 0; 852 853 len = strlen(str) + 1; 854 if (oldp && *oldlenp < len) { 855 len = *oldlenp; 856 rval = ENOMEM; 857 } 858 if (newp) 859 return (EPERM); 860 *oldlenp = len; 861 if (oldp) 862 error = copyout(str, oldp, len); 863 if (error) 864 rval = error; 865 return (rval); 866} 867 868/* 869 * Validate parameters and get old / set new parameters 870 * for a structure oriented sysctl function. 871 */ 872int 873sysctl_struct(oldp, oldlenp, newp, newlen, sp, len) 874 void *oldp; 875 size_t *oldlenp; 876 void *newp; 877 size_t newlen; 878 void *sp; 879 int len; 880{ 881 int error = 0; 882 883 if (oldp && *oldlenp < len) 884 return (ENOMEM); 885 if (newp && newlen > len) 886 return (EINVAL); 887 if (oldp) { 888 *oldlenp = len; 889 error = copyout(sp, oldp, len); 890 } 891 if (error == 0 && newp) 892 error = copyin(newp, sp, len); 893 return (error); 894} 895 896/* 897 * Validate parameters and get old parameters 898 * for a structure oriented sysctl function. 899 */ 900int 901sysctl_rdstruct(oldp, oldlenp, newp, sp, len) 902 void *oldp; 903 size_t *oldlenp; 904 void *newp, *sp; 905 int len; 906{ 907 int error = 0; 908 909 if (oldp && *oldlenp < len) 910 return (ENOMEM); 911 if (newp) 912 return (EPERM); 913 *oldlenp = len; 914 if (oldp) 915 error = copyout(sp, oldp, len); 916 return (error); 917} 918 919/* 920 * Get file structures. 921 */ 922int 923sysctl_file(where, sizep) 924 char *where; 925 size_t *sizep; 926{ 927 int buflen, error; 928 struct file *fp; 929 char *start = where; 930 931 buflen = *sizep; 932 if (where == NULL) { 933 /* 934 * overestimate by 10 files 935 */ 936 *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file); 937 return (0); 938 } 939 940 /* 941 * first copyout filehead 942 */ 943 if (buflen < sizeof(filehead)) { 944 *sizep = 0; 945 return (0); 946 } 947 error = copyout((caddr_t)&filehead, where, sizeof(filehead)); 948 if (error) 949 return (error); 950 buflen -= sizeof(filehead); 951 where += sizeof(filehead); 952 953 /* 954 * followed by an array of file structures 955 */ 956 for (fp = filehead; fp != NULL; fp = fp->f_filef) { 957 if (buflen < sizeof(struct file)) { 958 *sizep = where - start; 959 return (ENOMEM); 960 } 961 error = copyout((caddr_t)fp, where, sizeof (struct file)); 962 if (error) 963 return (error); 964 buflen -= sizeof(struct file); 965 where += sizeof(struct file); 966 } 967 *sizep = where - start; 968 return (0); 969} 970 971/* 972 * try over estimating by 5 procs 973 */ 974#define KERN_PROCSLOP (5 * sizeof (struct kinfo_proc)) 975 976int 977sysctl_doproc(name, namelen, where, sizep) 978 int *name; 979 u_int namelen; 980 char *where; 981 size_t *sizep; 982{ 983 register struct proc *p; 984 register struct kinfo_proc *dp = (struct kinfo_proc *)where; 985 register int needed = 0; 986 int buflen = where != NULL ? *sizep : 0; 987 int doingzomb; 988 struct eproc eproc; 989 int error = 0; 990 991 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL)) 992 return (EINVAL); 993 p = (struct proc *)allproc; 994 doingzomb = 0; 995again: 996 for (; p != NULL; p = p->p_next) { 997 /* 998 * Skip embryonic processes. 999 */ 1000 if (p->p_stat == SIDL) 1001 continue; 1002 /* 1003 * TODO - make more efficient (see notes below). 1004 * do by session. 1005 */ 1006 switch (name[0]) { 1007 1008 case KERN_PROC_PID: 1009 /* could do this with just a lookup */ 1010 if (p->p_pid != (pid_t)name[1]) 1011 continue; 1012 break; 1013 1014 case KERN_PROC_PGRP: 1015 /* could do this by traversing pgrp */ 1016 if (p->p_pgrp == NULL || p->p_pgrp->pg_id != (pid_t)name[1]) 1017 continue; 1018 break; 1019 1020 case KERN_PROC_TTY: 1021 if ((p->p_flag & P_CONTROLT) == 0 || 1022 p->p_session == NULL || 1023 p->p_session->s_ttyp == NULL || 1024 p->p_session->s_ttyp->t_dev != (dev_t)name[1]) 1025 continue; 1026 break; 1027 1028 case KERN_PROC_UID: 1029 if (p->p_ucred == NULL || p->p_ucred->cr_uid != (uid_t)name[1]) 1030 continue; 1031 break; 1032 1033 case KERN_PROC_RUID: 1034 if (p->p_ucred == NULL || p->p_cred->p_ruid != (uid_t)name[1]) 1035 continue; 1036 break; 1037 } 1038 if (buflen >= sizeof(struct kinfo_proc)) { 1039 fill_eproc(p, &eproc); 1040 error = copyout((caddr_t)p, &dp->kp_proc, 1041 sizeof(struct proc)); 1042 if (error) 1043 return (error); 1044 error = copyout((caddr_t)&eproc, &dp->kp_eproc, 1045 sizeof(eproc)); 1046 if (error) 1047 return (error); 1048 dp++; 1049 buflen -= sizeof(struct kinfo_proc); 1050 } 1051 needed += sizeof(struct kinfo_proc); 1052 } 1053 if (doingzomb == 0) { 1054 p = zombproc; 1055 doingzomb++; 1056 goto again; 1057 } 1058 if (where != NULL) { 1059 *sizep = (caddr_t)dp - where; 1060 if (needed > *sizep) 1061 return (ENOMEM); 1062 } else { 1063 needed += KERN_PROCSLOP; 1064 *sizep = needed; 1065 } 1066 return (0); 1067} 1068 1069/* 1070 * Fill in an eproc structure for the specified process. 1071 */ 1072void 1073fill_eproc(p, ep) 1074 register struct proc *p; 1075 register struct eproc *ep; 1076{ 1077 register struct tty *tp; 1078 1079 bzero(ep, sizeof(*ep)); 1080 1081 ep->e_paddr = p; 1082 if (p->p_cred) { 1083 ep->e_pcred = *p->p_cred; 1084 if (p->p_ucred) 1085 ep->e_ucred = *p->p_ucred; 1086 } 1087 if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 1088 register struct vmspace *vm = p->p_vmspace; 1089 1090#ifdef pmap_resident_count 1091 ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/ 1092#else 1093 ep->e_vm.vm_rssize = vm->vm_rssize; 1094#endif 1095 ep->e_vm.vm_tsize = vm->vm_tsize; 1096 ep->e_vm.vm_dsize = vm->vm_dsize; 1097 ep->e_vm.vm_ssize = vm->vm_ssize; 1098#ifndef sparc 1099 ep->e_vm.vm_pmap = vm->vm_pmap; 1100#endif 1101 } 1102 if (p->p_pptr) 1103 ep->e_ppid = p->p_pptr->p_pid; 1104 if (p->p_pgrp) { 1105 ep->e_sess = p->p_pgrp->pg_session; 1106 ep->e_pgid = p->p_pgrp->pg_id; 1107 ep->e_jobc = p->p_pgrp->pg_jobc; 1108 } 1109 if ((p->p_flag & P_CONTROLT) && 1110 (ep->e_sess != NULL) && 1111 ((tp = ep->e_sess->s_ttyp) != NULL)) { 1112 ep->e_tdev = tp->t_dev; 1113 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 1114 ep->e_tsess = tp->t_session; 1115 } else 1116 ep->e_tdev = NODEV; 1117 if (ep->e_sess && ep->e_sess->s_ttyvp) 1118 ep->e_flag = EPROC_CTTY; 1119 if (SESS_LEADER(p)) 1120 ep->e_flag |= EPROC_SLEADER; 1121 if (p->p_wmesg) { 1122 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 1123 ep->e_wmesg[WMESGLEN] = 0; 1124 } 1125} 1126 1127#ifdef COMPAT_43 1128#include <sys/socket.h> 1129#define KINFO_PROC (0<<8) 1130#define KINFO_RT (1<<8) 1131#define KINFO_VNODE (2<<8) 1132#define KINFO_FILE (3<<8) 1133#define KINFO_METER (4<<8) 1134#define KINFO_LOADAVG (5<<8) 1135#define KINFO_CLOCKRATE (6<<8) 1136 1137/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 1138#define KINFO_BSDI_SYSINFO (101<<8) 1139 1140/* 1141 * XXX this is bloat, but I hope it's better here than on the potentially 1142 * limited kernel stack... -Peter 1143 */ 1144 1145struct { 1146 int bsdi_machine; /* "i386" on BSD/386 */ 1147/* ^^^ this is an offset to the string, relative to the struct start */ 1148 char *pad0; 1149 long pad1; 1150 long pad2; 1151 long pad3; 1152 u_long pad4; 1153 u_long pad5; 1154 u_long pad6; 1155 1156 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 1157 int bsdi_osrelease; /* "1.1" on BSD/386 */ 1158 long pad7; 1159 long pad8; 1160 char *pad9; 1161 1162 long pad10; 1163 long pad11; 1164 int pad12; 1165 long pad13; 1166 quad_t pad14; 1167 long pad15; 1168 1169 struct timeval pad16; 1170 /* we dont set this, because BSDI's uname used gethostname() instead */ 1171 int bsdi_hostname; /* hostname on BSD/386 */ 1172 1173 /* the actual string data is appended here */ 1174 1175} bsdi_si; 1176/* 1177 * this data is appended to the end of the bsdi_si structure during copyout. 1178 * The "char *" offsets are relative to the base of the bsdi_si struct. 1179 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 1180 * should not exceed the length of the buffer here... (or else!! :-) 1181 */ 1182char bsdi_strings[80]; /* It had better be less than this! */ 1183 1184struct getkerninfo_args { 1185 int op; 1186 char *where; 1187 int *size; 1188 int arg; 1189}; 1190 1191int 1192ogetkerninfo(p, uap, retval) 1193 struct proc *p; 1194 register struct getkerninfo_args *uap; 1195 int *retval; 1196{ 1197 int error, name[6]; 1198 u_int size; 1199 1200 switch (uap->op & 0xff00) { 1201 1202 case KINFO_RT: 1203 name[0] = CTL_NET; 1204 name[1] = PF_ROUTE; 1205 name[2] = 0; 1206 name[3] = (uap->op & 0xff0000) >> 16; 1207 name[4] = uap->op & 0xff; 1208 name[5] = uap->arg; 1209 error = userland_sysctl(p, name, 6, uap->where, uap->size, 1210 0, 0, 0, 0); 1211 break; 1212 1213 case KINFO_VNODE: 1214 name[0] = CTL_KERN; 1215 name[1] = KERN_VNODE; 1216 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1217 0, 0, 0, 0); 1218 break; 1219 1220 case KINFO_PROC: 1221 name[0] = CTL_KERN; 1222 name[1] = KERN_PROC; 1223 name[2] = uap->op & 0xff; 1224 name[3] = uap->arg; 1225 error = userland_sysctl(p, name, 4, uap->where, uap->size, 1226 0, 0, 0, 0); 1227 break; 1228 1229 case KINFO_FILE: 1230 name[0] = CTL_KERN; 1231 name[1] = KERN_FILE; 1232 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1233 0, 0, 0, 0); 1234 break; 1235 1236 case KINFO_METER: 1237 name[0] = CTL_VM; 1238 name[1] = VM_METER; 1239 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1240 0, 0, 0, 0); 1241 break; 1242 1243 case KINFO_LOADAVG: 1244 name[0] = CTL_VM; 1245 name[1] = VM_LOADAVG; 1246 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1247 0, 0, 0, 0); 1248 break; 1249 1250 case KINFO_CLOCKRATE: 1251 name[0] = CTL_KERN; 1252 name[1] = KERN_CLOCKRATE; 1253 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1254 0, 0, 0, 0); 1255 break; 1256 1257 case KINFO_BSDI_SYSINFO: { 1258 /* 1259 * this is pretty crude, but it's just enough for uname() 1260 * from BSDI's 1.x libc to work. 1261 * 1262 * In particular, it doesn't return the same results when 1263 * the supplied buffer is too small. BSDI's version apparently 1264 * will return the amount copied, and set the *size to how 1265 * much was needed. The emulation framework here isn't capable 1266 * of that, so we just set both to the amount copied. 1267 * BSDI's 2.x product apparently fails with ENOMEM in this 1268 * scenario. 1269 */ 1270 1271 u_int needed; 1272 u_int left; 1273 char *s; 1274 1275 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1276 bzero(bsdi_strings, sizeof(bsdi_strings)); 1277 1278 s = bsdi_strings; 1279 1280 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1281 strcpy(s, ostype); 1282 s += strlen(s) + 1; 1283 1284 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1285 strcpy(s, osrelease); 1286 s += strlen(s) + 1; 1287 1288 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1289 strcpy(s, machine); 1290 s += strlen(s) + 1; 1291 1292 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1293 1294 if (uap->where == NULL) { 1295 /* process is asking how much buffer to supply.. */ 1296 size = needed; 1297 error = 0; 1298 break; 1299 } 1300 1301 1302 /* if too much buffer supplied, trim it down */ 1303 if (size > needed) 1304 size = needed; 1305 1306 /* how much of the buffer is remaining */ 1307 left = size; 1308 1309 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1310 break; 1311 1312 /* is there any point in continuing? */ 1313 if (left > sizeof(bsdi_si)) { 1314 left -= sizeof(bsdi_si); 1315 error = copyout(&bsdi_strings, 1316 uap->where + sizeof(bsdi_si), left); 1317 } 1318 break; 1319 } 1320 1321 default: 1322 return (EOPNOTSUPP); 1323 } 1324 if (error) 1325 return (error); 1326 *retval = size; 1327 if (uap->size) 1328 error = copyout((caddr_t)&size, (caddr_t)uap->size, 1329 sizeof(size)); 1330 return (error); 1331} 1332#endif /* COMPAT_43 */ 1333