sysctl.c revision 1.93
1/* $OpenBSD: sysctl.c,v 1.93 2003/05/26 08:35:05 tedu Exp $ */ 2/* $NetBSD: sysctl.c,v 1.9 1995/09/30 07:12:50 thorpej Exp $ */ 3 4/* 5 * Copyright (c) 1993 6 * The Regents of the University of California. All rights reserved. 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 37#ifndef lint 38static const char copyright[] = 39"@(#) Copyright (c) 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static const char sccsid[] = "@(#)sysctl.c 8.5 (Berkeley) 5/9/95"; 46#else 47static char *rcsid = "$OpenBSD: sysctl.c,v 1.93 2003/05/26 08:35:05 tedu Exp $"; 48#endif 49#endif /* not lint */ 50 51#include <sys/param.h> 52#include <sys/gmon.h> 53#include <sys/mount.h> 54#include <sys/stat.h> 55#include <sys/sem.h> 56#include <sys/shm.h> 57#include <sys/sysctl.h> 58#include <sys/socket.h> 59#include <sys/malloc.h> 60#include <sys/dkstat.h> 61#include <sys/uio.h> 62#include <sys/tty.h> 63#include <sys/namei.h> 64#include <sys/sensors.h> 65#include <machine/cpu.h> 66#include <net/route.h> 67 68#include <netinet/in.h> 69#include <netinet/in_systm.h> 70#include <netinet/ip.h> 71#include <netinet/in_pcb.h> 72#include <netinet/ip_icmp.h> 73#include <netinet/ip_ipip.h> 74#include <netinet/ip_ether.h> 75#include <netinet/ip_ah.h> 76#include <netinet/ip_esp.h> 77#include <netinet/icmp_var.h> 78#include <netinet/ip_var.h> 79#include <netinet/udp.h> 80#include <netinet/udp_var.h> 81#include <netinet/tcp.h> 82#include <netinet/tcp_timer.h> 83#include <netinet/tcp_var.h> 84#include <netinet/ip_gre.h> 85#include <netinet/ip_ipcomp.h> 86 87#ifdef INET6 88#include <netinet/ip6.h> 89#include <netinet/icmp6.h> 90#include <netinet6/ip6_var.h> 91#include <netinet6/pim6_var.h> 92#endif 93 94#include <uvm/uvm_swap_encrypt.h> 95 96#include <ufs/ufs/quota.h> 97#include <ufs/ufs/inode.h> 98#include <ufs/ffs/fs.h> 99#include <ufs/ffs/ffs_extern.h> 100 101#include <nfs/rpcv2.h> 102#include <nfs/nfsproto.h> 103#include <nfs/nfs.h> 104 105#include <netipx/ipx.h> 106#include <netipx/ipx_var.h> 107#include <netipx/spx_var.h> 108#include <ddb/db_var.h> 109#include <dev/rndvar.h> 110 111#include <err.h> 112#include <errno.h> 113#include <stdio.h> 114#include <stdlib.h> 115#include <string.h> 116#include <ctype.h> 117 118#ifdef CPU_BIOS 119#include <machine/biosvar.h> 120#endif 121 122struct ctlname topname[] = CTL_NAMES; 123struct ctlname kernname[] = CTL_KERN_NAMES; 124struct ctlname vmname[] = CTL_VM_NAMES; 125struct ctlname fsname[] = CTL_FS_NAMES; 126struct ctlname netname[] = CTL_NET_NAMES; 127struct ctlname hwname[] = CTL_HW_NAMES; 128struct ctlname username[] = CTL_USER_NAMES; 129struct ctlname debugname[CTL_DEBUG_MAXID]; 130struct ctlname kernmallocname[] = CTL_KERN_MALLOC_NAMES; 131struct ctlname forkstatname[] = CTL_KERN_FORKSTAT_NAMES; 132struct ctlname nchstatsname[] = CTL_KERN_NCHSTATS_NAMES; 133struct ctlname ttyname[] = CTL_KERN_TTY_NAMES; 134struct ctlname semname[] = CTL_KERN_SEMINFO_NAMES; 135struct ctlname shmname[] = CTL_KERN_SHMINFO_NAMES; 136struct ctlname watchdogname[] = CTL_KERN_WATCHDOG_NAMES; 137struct ctlname *vfsname; 138#ifdef CTL_MACHDEP_NAMES 139struct ctlname machdepname[] = CTL_MACHDEP_NAMES; 140#endif 141struct ctlname ddbname[] = CTL_DDB_NAMES; 142char names[BUFSIZ]; 143int lastused; 144 145struct list { 146 struct ctlname *list; 147 int size; 148}; 149struct list toplist = { topname, CTL_MAXID }; 150struct list secondlevel[] = { 151 { 0, 0 }, /* CTL_UNSPEC */ 152 { kernname, KERN_MAXID }, /* CTL_KERN */ 153 { vmname, VM_MAXID }, /* CTL_VM */ 154 { fsname, FS_MAXID }, /* CTL_FS */ 155 { netname, NET_MAXID }, /* CTL_NET */ 156 { 0, CTL_DEBUG_MAXID }, /* CTL_DEBUG */ 157 { hwname, HW_MAXID }, /* CTL_HW */ 158#ifdef CTL_MACHDEP_NAMES 159 { machdepname, CPU_MAXID }, /* CTL_MACHDEP */ 160#else 161 { 0, 0 }, /* CTL_MACHDEP */ 162#endif 163 { username, USER_MAXID }, /* CTL_USER_NAMES */ 164 { ddbname, DBCTL_MAXID }, /* CTL_DDB_NAMES */ 165 { 0, 0 }, /* CTL_VFS */ 166}; 167 168int Aflag, aflag, nflag, qflag, wflag; 169 170/* 171 * Variables requiring special processing. 172 */ 173#define CLOCK 0x00000001 174#define BOOTTIME 0x00000002 175#define CHRDEV 0x00000004 176#define BLKDEV 0x00000008 177#define RNDSTATS 0x00000010 178#define BADDYNAMIC 0x00000020 179#define BIOSGEO 0x00000040 180#define BIOSDEV 0x00000080 181#define MAJ2DEV 0x00000100 182#define UNSIGNED 0x00000200 183#define KMEMBUCKETS 0x00000400 184#define LONGARRAY 0x00000800 185#define KMEMSTATS 0x00001000 186#define SENSORS 0x00002000 187 188/* prototypes */ 189void debuginit(void); 190void listall(char *, struct list *); 191void parse(char *, int); 192void parse_baddynamic(int *, size_t, char *, void **, size_t *, int, int); 193void usage(void); 194int findname(char *, char *, char **, struct list *); 195int sysctl_inet(char *, char **, int *, int, int *); 196#ifdef INET6 197int sysctl_inet6(char *, char **, int *, int, int *); 198#endif 199int sysctl_ipx(char *, char **, int *, int, int *); 200int sysctl_fs(char *, char **, int *, int, int *); 201static int sysctl_vfs(char *, char **, int[], int, int *); 202static int sysctl_vfsgen(char *, char **, int[], int, int *); 203int sysctl_bios(char *, char **, int *, int, int *); 204int sysctl_swpenc(char *, char **, int *, int, int *); 205int sysctl_forkstat(char *, char **, int *, int, int *); 206int sysctl_tty(char *, char **, int *, int, int *); 207int sysctl_nchstats(char *, char **, int *, int, int *); 208int sysctl_malloc(char *, char **, int *, int, int *); 209int sysctl_seminfo(char *, char **, int *, int, int *); 210int sysctl_shminfo(char *, char **, int *, int, int *); 211int sysctl_watchdog(char *, char **, int *, int, int *); 212int sysctl_sensors(char *, char **, int *, int, int *); 213#ifdef CPU_CHIPSET 214int sysctl_chipset(char *, char **, int *, int, int *); 215#endif 216void vfsinit(void); 217 218int 219main(int argc, char *argv[]) 220{ 221 int ch, lvl1; 222 223 while ((ch = getopt(argc, argv, "Aanqw")) != -1) { 224 switch (ch) { 225 226 case 'A': 227 Aflag = 1; 228 break; 229 230 case 'a': 231 aflag = 1; 232 break; 233 234 case 'n': 235 nflag = 1; 236 break; 237 238 case 'q': 239 qflag = 1; 240 break; 241 242 case 'w': 243 wflag = 1; 244 break; 245 246 default: 247 usage(); 248 } 249 } 250 argc -= optind; 251 argv += optind; 252 253 if (argc == 0 && (Aflag || aflag)) { 254 debuginit(); 255 vfsinit(); 256 for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++) 257 listall(topname[lvl1].ctl_name, &secondlevel[lvl1]); 258 return (0); 259 } 260 if (argc == 0) 261 usage(); 262 for (; *argv != NULL; ++argv) 263 parse(*argv, 1); 264 return (0); 265} 266 267/* 268 * List all variables known to the system. 269 */ 270void 271listall(char *prefix, struct list *lp) 272{ 273 char *cp, name[BUFSIZ]; 274 int lvl2, len; 275 276 if (lp->list == NULL) 277 return; 278 if ((len = strlcpy(name, prefix, sizeof(name))) >= sizeof(name)) 279 warn("%s: name too long", prefix); 280 cp = name + len++; 281 *cp++ = '.'; 282 for (lvl2 = 0; lvl2 < lp->size; lvl2++) { 283 if (lp->list[lvl2].ctl_name == NULL) 284 continue; 285 if (strlcpy(cp, lp->list[lvl2].ctl_name, 286 sizeof(name) - len) >= sizeof(name) - len) 287 warn("%s: name too long", lp->list[lvl2].ctl_name); 288 parse(name, Aflag); 289 } 290} 291 292/* 293 * Parse a name into a MIB entry. 294 * Lookup and print out the MIB entry if it exists. 295 * Set a new value if requested. 296 */ 297void 298parse(char *string, int flags) 299{ 300 int indx, type, state, intval, len; 301 size_t size, newsize = 0; 302 int lal = 0, special = 0; 303 void *newval = 0; 304 int64_t quadval; 305 struct list *lp; 306 int mib[CTL_MAXNAME]; 307 char *cp, *bufp, buf[BUFSIZ]; 308 309 (void)strlcpy(buf, string, sizeof(buf)); 310 bufp = buf; 311 if ((cp = strchr(string, '=')) != NULL) { 312 if (!wflag) 313 errx(2, "must specify -w to set variables"); 314 *strchr(buf, '=') = '\0'; 315 *cp++ = '\0'; 316 while (isspace(*cp)) 317 cp++; 318 newval = cp; 319 newsize = strlen(cp); 320 } 321 if ((indx = findname(string, "top", &bufp, &toplist)) == -1) 322 return; 323 mib[0] = indx; 324 if (indx == CTL_VFS) 325 vfsinit(); 326 if (indx == CTL_DEBUG) 327 debuginit(); 328 lp = &secondlevel[indx]; 329 if (lp->list == 0) { 330 warnx("%s: class is not implemented", topname[indx].ctl_name); 331 return; 332 } 333 if (bufp == NULL) { 334 listall(topname[indx].ctl_name, lp); 335 return; 336 } 337 if ((indx = findname(string, "second", &bufp, lp)) == -1) 338 return; 339 mib[1] = indx; 340 type = lp->list[indx].ctl_type; 341 len = 2; 342 switch (mib[0]) { 343 344 case CTL_KERN: 345 switch (mib[1]) { 346 case KERN_PROF: 347 mib[2] = GPROF_STATE; 348 size = sizeof(state); 349 if (sysctl(mib, 3, &state, &size, NULL, 0) == -1) { 350 if (flags == 0) 351 return; 352 if (!nflag) 353 (void)printf("%s: ", string); 354 (void)puts("kernel is not compiled for profiling"); 355 return; 356 } 357 if (!nflag) 358 (void)printf("%s = %s\n", string, 359 state == GMON_PROF_OFF ? "off" : "running"); 360 return; 361 case KERN_FORKSTAT: 362 sysctl_forkstat(string, &bufp, mib, flags, &type); 363 return; 364 case KERN_TTY: 365 len = sysctl_tty(string, &bufp, mib, flags, &type); 366 if (len < 0) 367 return; 368 newsize = 0; 369 break; 370 case KERN_NCHSTATS: 371 sysctl_nchstats(string, &bufp, mib, flags, &type); 372 return; 373 case KERN_MALLOCSTATS: 374 len = sysctl_malloc(string, &bufp, mib, flags, &type); 375 if (len < 0) 376 return; 377 if (mib[2] == KERN_MALLOC_BUCKET) 378 special |= KMEMBUCKETS; 379 if (mib[2] == KERN_MALLOC_KMEMSTATS) 380 special |= KMEMSTATS; 381 newsize = 0; 382 break; 383 case KERN_MBSTAT: 384 if (flags == 0) 385 return; 386 warnx("use netstat to view %s", string); 387 return; 388 case KERN_MSGBUF: 389 if (flags == 0) 390 return; 391 warnx("use dmesg to view %s", string); 392 return; 393 case KERN_VNODE: 394 case KERN_FILE: 395 if (flags == 0) 396 return; 397 warnx("use pstat to view %s information", string); 398 return; 399 case KERN_PROC: 400 if (flags == 0) 401 return; 402 warnx("use ps to view %s information", string); 403 return; 404 case KERN_CLOCKRATE: 405 special |= CLOCK; 406 break; 407 case KERN_BOOTTIME: 408 special |= BOOTTIME; 409 break; 410 case KERN_RND: 411 special |= RNDSTATS; 412 break; 413 case KERN_HOSTID: 414 case KERN_ARND: 415 special |= UNSIGNED; 416 break; 417 case KERN_CPTIME: 418 special |= LONGARRAY; 419 lal = CPUSTATES; 420 break; 421 case KERN_SEMINFO: 422 len = sysctl_seminfo(string, &bufp, mib, flags, &type); 423 if (len < 0) 424 return; 425 break; 426 case KERN_SHMINFO: 427 len = sysctl_shminfo(string, &bufp, mib, flags, &type); 428 if (len < 0) 429 return; 430 break; 431 case KERN_WATCHDOG: 432 len = sysctl_watchdog(string, &bufp, mib, flags, 433 &type); 434 if (len < 0) 435 return; 436 break; 437 } 438 break; 439 440 case CTL_HW: 441 switch (mib[1]) { 442 case HW_DISKSTATS: 443 /* 444 * Only complain if someone asks explicitly for this, 445 * otherwise "fail" silently. 446 */ 447 if (flags) 448 warnx("use vmstat to view %s information", 449 string); 450 return; 451 case HW_SENSORS: 452 special |= SENSORS; 453 len = sysctl_sensors(string, &bufp, mib, flags, &type); 454 if (len < 0) 455 return; 456 break; 457 } 458 break; 459 460 case CTL_VM: 461 if (mib[1] == VM_LOADAVG) { 462 double loads[3]; 463 464 getloadavg(loads, 3); 465 if (!nflag) 466 (void)printf("%s = ", string); 467 (void)printf("%.2f %.2f %.2f\n", loads[0], 468 loads[1], loads[2]); 469 return; 470 } else if (mib[1] == VM_PSSTRINGS) { 471 struct _ps_strings _ps; 472 473 size = sizeof(_ps); 474 if (sysctl(mib, 2, &_ps, &size, NULL, 0) == -1) { 475 if (flags == 0) 476 return; 477 if (!nflag) 478 (void)printf("%s: ", string); 479 (void)puts("can't find ps strings"); 480 return; 481 } 482 if (!nflag) 483 (void)printf("%s = ", string); 484 (void)printf("%p\n", _ps.val); 485 return; 486 } else if (mib[1] == VM_SWAPENCRYPT) { 487 len = sysctl_swpenc(string, &bufp, mib, flags, &type); 488 if (len < 0) 489 return; 490 491 break; 492 } else if (mib[1] == VM_NKMEMPAGES || 493 mib[1] == VM_ANONMIN || 494 mib[1] == VM_VTEXTMIN || 495 mib[1] == VM_VNODEMIN) { 496 break; 497 } 498 if (flags == 0) 499 return; 500 warnx("use vmstat or systat to view %s information", string); 501 return; 502 503 break; 504 505 case CTL_NET: 506 if (mib[1] == PF_INET) { 507 len = sysctl_inet(string, &bufp, mib, flags, &type); 508 if (len < 0) 509 return; 510 511 if ((mib[2] == IPPROTO_TCP && 512 mib[3] == TCPCTL_BADDYNAMIC) || 513 (mib[2] == IPPROTO_UDP && 514 mib[3] == UDPCTL_BADDYNAMIC)) { 515 516 special |= BADDYNAMIC; 517 518 if (newval != NULL) 519 parse_baddynamic(mib, len, string, 520 &newval, &newsize, flags, nflag); 521 } 522 break; 523 } 524#ifdef INET6 525 if (mib[1] == PF_INET6) { 526 len = sysctl_inet6(string, &bufp, mib, flags, &type); 527 if (len < 0) 528 return; 529 530 break; 531 } 532#endif 533 if (mib[1] == PF_IPX) { 534 len = sysctl_ipx(string, &bufp, mib, flags, &type); 535 if (len >= 0) 536 break; 537 return; 538 } 539 if (flags == 0) 540 return; 541 warnx("use netstat to view %s information", string); 542 return; 543 544 case CTL_DEBUG: 545 mib[2] = CTL_DEBUG_VALUE; 546 len = 3; 547 break; 548 549 case CTL_MACHDEP: 550#ifdef CPU_CONSDEV 551 if (mib[1] == CPU_CONSDEV) 552 special |= CHRDEV; 553#endif 554#ifdef CPU_BLK2CHR 555 if (mib[1] == CPU_BLK2CHR) { 556 if (bufp == NULL) 557 return; 558 mib[2] = makedev(atoi(bufp),0); 559 bufp = NULL; 560 len = 3; 561 special |= CHRDEV; 562 break; 563 } 564#endif 565#ifdef CPU_CHR2BLK 566 if (mib[1] == CPU_CHR2BLK) { 567 if (bufp == NULL) 568 return; 569 mib[2] = makedev(atoi(bufp),0); 570 bufp = NULL; 571 len = 3; 572 special |= BLKDEV; 573 break; 574 } 575#endif 576#ifdef CPU_BIOS 577 if (mib[1] == CPU_BIOS) { 578 len = sysctl_bios(string, &bufp, mib, flags, &type); 579 if (len < 0) 580 return; 581 if (mib[2] == BIOS_DEV) 582 special |= BIOSDEV; 583 if (mib[2] == BIOS_DISKINFO) 584 special |= BIOSGEO; 585 break; 586 } 587#endif 588#ifdef CPU_CHIPSET 589 if (mib[1] == CPU_CHIPSET) { 590 len = sysctl_chipset(string, &bufp, mib, flags, &type); 591 if (len < 0) 592 return; 593 break; 594 } 595#endif 596#ifdef CPU_LONGRUN 597 if (mib[1] == CPU_LONGRUN) 598 return; 599#endif 600 601 break; 602 603 case CTL_FS: 604 len = sysctl_fs(string, &bufp, mib, flags, &type); 605 if (len >= 0) 606 break; 607 return; 608 609 case CTL_VFS: 610 if (mib[1]) 611 len = sysctl_vfs(string, &bufp, mib, flags, &type); 612 else 613 len = sysctl_vfsgen(string, &bufp, mib, flags, &type); 614 if (len >= 0) { 615 if (type == CTLTYPE_STRUCT) { 616 if (flags) 617 warnx("use nfsstat to view %s information", 618 MOUNT_NFS); 619 return; 620 } else 621 break; 622 } 623 return; 624 625 case CTL_USER: 626 case CTL_DDB: 627 break; 628 629 default: 630 warnx("illegal top level value: %d", mib[0]); 631 return; 632 633 } 634 if (bufp) { 635 warnx("name %s in %s is unknown", bufp, string); 636 return; 637 } 638 if (newsize > 0) { 639 switch (type) { 640 case CTLTYPE_INT: 641 errno = 0; 642 if (special & UNSIGNED) 643 intval = strtoul(newval, &cp, 10); 644 else 645 intval = strtol(newval, &cp, 10); 646 if (*cp != '\0') { 647 warnx("%s: illegal value: %s", string, 648 (char *)newval); 649 return; 650 } 651 if (errno == ERANGE) { 652 warnx("%s: value %s out of range", string, 653 (char *)newval); 654 return; 655 } 656 newval = &intval; 657 newsize = sizeof(intval); 658 break; 659 660 case CTLTYPE_QUAD: 661 /* XXX - assumes sizeof(long long) == sizeof(quad_t) */ 662 (void)sscanf(newval, "%lld", (long long *)&quadval); 663 newval = &quadval; 664 newsize = sizeof(quadval); 665 break; 666 } 667 } 668 size = BUFSIZ; 669 if (sysctl(mib, len, buf, &size, newval, newsize) == -1) { 670 if (flags == 0) 671 return; 672 switch (errno) { 673 case EOPNOTSUPP: 674 warnx("%s: value is not available", string); 675 return; 676 case ENOTDIR: 677 warnx("%s: specification is incomplete", string); 678 return; 679 case ENOMEM: 680 warnx("%s: type is unknown to this program", string); 681 return; 682 case ENXIO: 683 if (special & BIOSGEO) 684 return; 685 default: 686 warn("%s", string); 687 return; 688 } 689 } 690 if (special & KMEMBUCKETS) { 691 struct kmembuckets *kb = (struct kmembuckets *)buf; 692 if (!nflag) 693 (void)printf("%s = ", string); 694 printf("("); 695 printf("calls = %llu ", (long long)kb->kb_calls); 696 printf("total_allocated = %llu ", (long long)kb->kb_total); 697 printf("total_free = %lld ", (long long)kb->kb_totalfree); 698 printf("elements = %lld ", (long long)kb->kb_elmpercl); 699 printf("high watermark = %lld ", (long long)kb->kb_highwat); 700 printf("could_free = %lld", (long long)kb->kb_couldfree); 701 printf(")\n"); 702 return; 703 } 704 if (special & KMEMSTATS) { 705 struct kmemstats *km = (struct kmemstats *)buf; 706 int j, first = 1; 707 708 if (!nflag) 709 (void)printf("%s = ", string); 710 (void)printf("(inuse = %ld, calls = %ld, memuse = %ldK, " 711 "limblocks = %d, mapblocks = %d, maxused = %ldK, " 712 "limit = %ldK, spare = %ld, sizes = (", 713 km->ks_inuse, km->ks_calls, 714 (km->ks_memuse + 1023) / 1024, km->ks_limblocks, 715 km->ks_mapblocks, (km->ks_maxused + 1023) / 1024, 716 (km->ks_limit + 1023) / 1024, km->ks_spare); 717 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 718 if ((km->ks_size & j ) == 0) 719 continue; 720 if (first) 721 (void)printf("%d", j); 722 else 723 (void)printf(",%d", j); 724 first = 0; 725 } 726 if (first) 727 (void)printf("none"); 728 (void)printf("))\n"); 729 return; 730 } 731 if (special & CLOCK) { 732 struct clockinfo *clkp = (struct clockinfo *)buf; 733 734 if (!nflag) 735 (void)printf("%s = ", string); 736 (void)printf( 737 "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n", 738 clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz); 739 return; 740 } 741 if (special & BOOTTIME) { 742 struct timeval *btp = (struct timeval *)buf; 743 time_t boottime; 744 745 if (!nflag) { 746 boottime = btp->tv_sec; 747 (void)printf("%s = %s", string, ctime(&boottime)); 748 } else 749 (void)printf("%ld\n", btp->tv_sec); 750 return; 751 } 752 if (special & BLKDEV) { 753 dev_t dev = *(dev_t *)buf; 754 755 if (!nflag) 756 (void)printf("%s = %s\n", string, 757 devname(dev, S_IFBLK)); 758 else 759 (void)printf("0x%x\n", dev); 760 return; 761 } 762 if (special & CHRDEV) { 763 dev_t dev = *(dev_t *)buf; 764 765 if (!nflag) 766 (void)printf("%s = %s\n", string, 767 devname(dev, S_IFCHR)); 768 else 769 (void)printf("0x%x\n", dev); 770 return; 771 } 772#ifdef CPU_BIOS 773 if (special & BIOSGEO) { 774 bios_diskinfo_t *pdi = (bios_diskinfo_t *)buf; 775 776 if (!nflag) 777 (void)printf("%s = ", string); 778 (void)printf("bootdev = 0x%x, " 779 "cylinders = %u, heads = %u, sectors = %u\n", 780 pdi->bsd_dev, pdi->bios_cylinders, 781 pdi->bios_heads, pdi->bios_sectors); 782 return; 783 } 784 if (special & BIOSDEV) { 785 int dev = *(int*)buf; 786 787 if (!nflag) 788 (void)printf("%s = ", string); 789 (void) printf("0x%02x\n", dev); 790 return; 791 } 792#endif 793 if (special & UNSIGNED) { 794 if (newsize == 0) { 795 if (!nflag) 796 (void)printf("%s = ", string); 797 (void)printf("%u\n", *(u_int *)buf); 798 } else { 799 if (!qflag) { 800 if (!nflag) 801 (void)printf("%s: %u -> ", string, 802 *(u_int *)buf); 803 (void)printf("%u\n", *(u_int *)newval); 804 } 805 } 806 return; 807 } 808 if (special & RNDSTATS) { 809 struct rndstats *rndstats = (struct rndstats *)buf; 810 int i; 811 812 if (!nflag) 813 (void)printf("%s = ", string); 814 (void)printf( 815 "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", 816 (unsigned long long)rndstats->rnd_total, 817 (unsigned long long)rndstats->rnd_used, 818 (unsigned long long)rndstats->rnd_reads, 819 (unsigned long long)rndstats->arc4_reads, 820 (unsigned long long)rndstats->arc4_nstirs, 821 (unsigned long long)rndstats->arc4_stirs, 822 (unsigned long long)rndstats->rnd_pad[0], 823 (unsigned long long)rndstats->rnd_pad[1], 824 (unsigned long long)rndstats->rnd_pad[2], 825 (unsigned long long)rndstats->rnd_pad[3], 826 (unsigned long long)rndstats->rnd_pad[4], 827 (unsigned long long)rndstats->rnd_waits, 828 (unsigned long long)rndstats->rnd_enqs, 829 (unsigned long long)rndstats->rnd_deqs, 830 (unsigned long long)rndstats->rnd_drops, 831 (unsigned long long)rndstats->rnd_drople); 832 for (i = 0; i < sizeof(rndstats->rnd_ed)/sizeof(rndstats->rnd_ed[0]); 833 i++) 834 (void)printf(" %llu", (unsigned long long)rndstats->rnd_ed[i]); 835 for (i = 0; i < sizeof(rndstats->rnd_sc)/sizeof(rndstats->rnd_sc[0]); 836 i++) 837 (void)printf(" %llu", (unsigned long long)rndstats->rnd_sc[i]); 838 for (i = 0; i < sizeof(rndstats->rnd_sb)/sizeof(rndstats->rnd_sb[0]); 839 i++) 840 (void)printf(" %llu", (unsigned long long)rndstats->rnd_sb[i]); 841 printf("\n"); 842 return; 843 } 844 if (special & BADDYNAMIC) { 845 in_port_t port, lastport; 846 u_int32_t *baddynamic = (u_int32_t *)buf; 847 848 if (!qflag) { 849 if (!nflag) 850 (void)printf("%s%s", string, 851 newsize ? ": " : " = "); 852 lastport = 0; 853 for (port = IPPORT_RESERVED/2; port < IPPORT_RESERVED; 854 port++) 855 if (DP_ISSET(baddynamic, port)) { 856 (void)printf("%s%hd", 857 lastport ? "," : "", port); 858 lastport = port; 859 } 860 if (newsize != 0) { 861 if (!nflag) 862 fputs(" -> ", stdout); 863 baddynamic = (u_int32_t *)newval; 864 lastport = 0; 865 for (port = IPPORT_RESERVED/2; 866 port < IPPORT_RESERVED; port++) 867 if (DP_ISSET(baddynamic, port)) { 868 (void)printf("%s%hd", 869 lastport ? "," : "", port); 870 lastport = port; 871 } 872 } 873 (void)putchar('\n'); 874 } 875 return; 876 } 877 if (special & LONGARRAY) { 878 long *la = (long *)buf; 879 if (!nflag) 880 printf("%s = ", string); 881 while (lal--) 882 printf("%ld%s", *la++, lal? ",":""); 883 putchar('\n'); 884 return; 885 } 886 if (special & SENSORS) { 887 struct sensor *s = (struct sensor *)buf; 888 889 if (size > 0) { 890 if (!nflag) 891 printf("%s = ", string); 892 printf("%s, %s, ", s->device, s->desc); 893 switch (s->type) { 894 case SENSOR_TEMP: 895 printf("temp, %.2f degC / %.2f degF", 896 (s->value / 1000000.0) - 273.16, 897 ((s->value / 1000000.0) - 273.16) * 9 / 5 + 898 32); 899 break; 900 case SENSOR_FANRPM: 901 printf("fanrpm, %lld RPM", s->value); 902 break; 903 case SENSOR_VOLTS_DC: 904 printf("volts_dc, %.2f V", 905 s->value / 1000000.0); 906 break; 907 default: 908 printf("unknown"); 909 } 910 printf("\n"); 911 } 912 return; 913 } 914 915 switch (type) { 916 case CTLTYPE_INT: 917 if (newsize == 0) { 918 if (!nflag) 919 (void)printf("%s = ", string); 920 (void)printf("%d\n", *(int *)buf); 921 } else { 922 if (!qflag) { 923 if (!nflag) 924 (void)printf("%s: %d -> ", string, 925 *(int *)buf); 926 (void)printf("%d\n", *(int *)newval); 927 } 928 } 929 return; 930 931 case CTLTYPE_STRING: 932 if (newval == NULL) { 933 if (!nflag) 934 (void)printf("%s = ", string); 935 (void)puts(buf); 936 } else { 937 if (!qflag) { 938 if (!nflag) 939 (void)printf("%s: %s -> ", string, buf); 940 (void)puts((char *)newval); 941 } 942 } 943 return; 944 945 case CTLTYPE_QUAD: 946 if (newsize == 0) { 947 long long tmp = *(quad_t *)buf; 948 949 if (!nflag) 950 (void)printf("%s = ", string); 951 (void)printf("%lld\n", tmp); 952 } else { 953 long long tmp = *(quad_t *)buf; 954 955 if (!qflag) { 956 if (!nflag) 957 (void)printf("%s: %lld -> ", 958 string, tmp); 959 tmp = *(quad_t *)newval; 960 (void)printf("%qd\n", tmp); 961 } 962 } 963 return; 964 965 case CTLTYPE_STRUCT: 966 warnx("%s: unknown structure returned", string); 967 return; 968 969 default: 970 case CTLTYPE_NODE: 971 warnx("%s: unknown type returned", string); 972 return; 973 } 974} 975 976void 977parse_baddynamic(int mib[], size_t len, char *string, void **newvalp, 978 size_t *newsizep, int flags, int nflag) 979{ 980 static u_int32_t newbaddynamic[DP_MAPSIZE]; 981 in_port_t port; 982 size_t size; 983 char action, *cp; 984 985 if (strchr((char *)*newvalp, '+') || strchr((char *)*newvalp, '-')) { 986 size = sizeof(newbaddynamic); 987 if (sysctl(mib, len, newbaddynamic, &size, 0, 0) == -1) { 988 if (flags == 0) 989 return; 990 if (!nflag) 991 (void)printf("%s: ", string); 992 (void)puts("kernel does contain bad dynamic port tables"); 993 return; 994 } 995 996 while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) { 997 if (*cp != '+' && *cp != '-') 998 errx(1, "cannot mix +/- with full list"); 999 action = *cp++; 1000 port = atoi(cp); 1001 if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED) 1002 errx(1, "invalid port, range is %d to %d", 1003 IPPORT_RESERVED/2, IPPORT_RESERVED-1); 1004 if (action == '+') 1005 DP_SET(newbaddynamic, port); 1006 else 1007 DP_CLR(newbaddynamic, port); 1008 } 1009 } else { 1010 (void)memset((void *)newbaddynamic, 0, sizeof(newbaddynamic)); 1011 while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) { 1012 port = atoi(cp); 1013 if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED) 1014 errx(1, "invalid port, range is %d to %d", 1015 IPPORT_RESERVED/2, IPPORT_RESERVED-1); 1016 DP_SET(newbaddynamic, port); 1017 } 1018 } 1019 1020 *newvalp = (void *)newbaddynamic; 1021 *newsizep = sizeof(newbaddynamic); 1022} 1023 1024/* 1025 * Initialize the set of debugging names 1026 */ 1027void 1028debuginit(void) 1029{ 1030 int mib[3], loc, i; 1031 size_t size; 1032 1033 if (secondlevel[CTL_DEBUG].list != 0) 1034 return; 1035 secondlevel[CTL_DEBUG].list = debugname; 1036 mib[0] = CTL_DEBUG; 1037 mib[2] = CTL_DEBUG_NAME; 1038 for (loc = lastused, i = 0; i < CTL_DEBUG_MAXID; i++) { 1039 mib[1] = i; 1040 size = BUFSIZ - loc; 1041 if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1) 1042 continue; 1043 debugname[i].ctl_name = &names[loc]; 1044 debugname[i].ctl_type = CTLTYPE_INT; 1045 loc += size; 1046 } 1047 lastused = loc; 1048} 1049 1050struct ctlname vfsgennames[] = CTL_VFSGENCTL_NAMES; 1051struct ctlname ffsname[] = FFS_NAMES; 1052struct ctlname nfsname[] = FS_NFS_NAMES; 1053struct list *vfsvars; 1054int *vfs_typenums; 1055 1056/* 1057 * Initialize the set of filesystem names 1058 */ 1059void 1060vfsinit(void) 1061{ 1062 int mib[4], maxtypenum, cnt, loc, size; 1063 struct vfsconf vfc; 1064 size_t buflen; 1065 1066 if (secondlevel[CTL_VFS].list != 0) 1067 return; 1068 mib[0] = CTL_VFS; 1069 mib[1] = VFS_GENERIC; 1070 mib[2] = VFS_MAXTYPENUM; 1071 buflen = 4; 1072 if (sysctl(mib, 3, &maxtypenum, &buflen, (void *)0, (size_t)0) < 0) 1073 return; 1074 maxtypenum++; /* + generic */ 1075 if ((vfs_typenums = malloc(maxtypenum * sizeof(int))) == NULL) 1076 return; 1077 memset(vfs_typenums, 0, maxtypenum * sizeof(int)); 1078 if ((vfsvars = malloc(maxtypenum * sizeof(*vfsvars))) == NULL) { 1079 free(vfs_typenums); 1080 return; 1081 } 1082 memset(vfsvars, 0, maxtypenum * sizeof(*vfsvars)); 1083 if ((vfsname = malloc(maxtypenum * sizeof(*vfsname))) == NULL) { 1084 free(vfs_typenums); 1085 free(vfsvars); 1086 return; 1087 } 1088 memset(vfsname, 0, maxtypenum * sizeof(*vfsname)); 1089 mib[2] = VFS_CONF; 1090 buflen = sizeof vfc; 1091 for (loc = lastused, cnt = 1; cnt < maxtypenum; cnt++) { 1092 mib[3] = cnt - 1; 1093 if (sysctl(mib, 4, &vfc, &buflen, (void *)0, (size_t)0) < 0) { 1094 if (errno == EOPNOTSUPP) 1095 continue; 1096 warn("vfsinit"); 1097 free(vfsname); 1098 return; 1099 } 1100 if (!strcmp(vfc.vfc_name, MOUNT_FFS)) { 1101 vfsvars[cnt].list = ffsname; 1102 vfsvars[cnt].size = FFS_MAXID; 1103 } 1104 if (!strcmp(vfc.vfc_name, MOUNT_NFS)) { 1105 vfsvars[cnt].list = nfsname; 1106 vfsvars[cnt].size = NFS_MAXID; 1107 } 1108 vfs_typenums[cnt] = vfc.vfc_typenum; 1109 strlcat(&names[loc], vfc.vfc_name, sizeof names - loc); 1110 vfsname[cnt].ctl_name = &names[loc]; 1111 vfsname[cnt].ctl_type = CTLTYPE_NODE; 1112 size = strlen(vfc.vfc_name) + 1; 1113 loc += size; 1114 } 1115 lastused = loc; 1116 1117 vfsname[0].ctl_name = "mounts"; 1118 vfsname[0].ctl_type = CTLTYPE_NODE; 1119 vfsvars[0].list = vfsname + 1; 1120 vfsvars[0].size = maxtypenum - 1; 1121 1122 secondlevel[CTL_VFS].list = vfsname; 1123 secondlevel[CTL_VFS].size = maxtypenum; 1124 return; 1125} 1126 1127int 1128sysctl_vfsgen(char *string, char **bufpp, int mib[], int flags, int *typep) 1129{ 1130 int indx; 1131 size_t size; 1132 struct vfsconf vfc; 1133 1134 if (*bufpp == NULL) { 1135 listall(string, vfsvars); 1136 return (-1); 1137 } 1138 1139 if ((indx = findname(string, "third", bufpp, vfsvars)) == -1) 1140 return (-1); 1141 1142 mib[1] = VFS_GENERIC; 1143 mib[2] = VFS_CONF; 1144 mib[3] = indx; 1145 size = sizeof vfc; 1146 if (sysctl(mib, 4, &vfc, &size, (void *)0, (size_t)0) < 0) { 1147 if (errno != EOPNOTSUPP) 1148 warn("vfs print"); 1149 return -1; 1150 } 1151 if (flags == 0 && vfc.vfc_refcount == 0) 1152 return -1; 1153 if (!nflag) 1154 fprintf(stdout, "%s has %d mounted instance%s\n", 1155 string, vfc.vfc_refcount, 1156 vfc.vfc_refcount != 1 ? "s" : ""); 1157 else 1158 fprintf(stdout, "%d\n", vfc.vfc_refcount); 1159 1160 return -1; 1161} 1162 1163int 1164sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep) 1165{ 1166 struct list *lp = &vfsvars[mib[1]]; 1167 int indx; 1168 1169 if (lp->list == NULL) { 1170 if (flags) 1171 warnx("No variables defined for file system %s", string); 1172 return (-1); 1173 } 1174 if (*bufpp == NULL) { 1175 listall(string, lp); 1176 return (-1); 1177 } 1178 if ((indx = findname(string, "third", bufpp, lp)) == -1) 1179 return (-1); 1180 1181 mib[1] = vfs_typenums[mib[1]]; 1182 mib[2] = indx; 1183 *typep = lp->list[indx].ctl_type; 1184 return (3); 1185} 1186 1187struct ctlname posixname[] = CTL_FS_POSIX_NAMES; 1188struct list fslist = { posixname, FS_POSIX_MAXID }; 1189 1190/* 1191 * handle file system requests 1192 */ 1193int 1194sysctl_fs(char *string, char **bufpp, int mib[], int flags, int *typep) 1195{ 1196 int indx; 1197 1198 if (*bufpp == NULL) { 1199 listall(string, &fslist); 1200 return (-1); 1201 } 1202 if ((indx = findname(string, "third", bufpp, &fslist)) == -1) 1203 return (-1); 1204 mib[2] = indx; 1205 *typep = fslist.list[indx].ctl_type; 1206 return (3); 1207} 1208 1209#ifdef CPU_BIOS 1210struct ctlname biosname[] = CTL_BIOS_NAMES; 1211struct list bioslist = { biosname, BIOS_MAXID }; 1212 1213/* 1214 * handle BIOS requests 1215 */ 1216int 1217sysctl_bios(char *string, char **bufpp, int mib[], int flags, int *typep) 1218{ 1219 char *name; 1220 int indx; 1221 1222 if (*bufpp == NULL) { 1223 listall(string, &bioslist); 1224 return (-1); 1225 } 1226 if ((indx = findname(string, "third", bufpp, &bioslist)) == -1) 1227 return (-1); 1228 mib[2] = indx; 1229 if (indx == BIOS_DISKINFO) { 1230 if (*bufpp == NULL) { 1231 char name[BUFSIZ]; 1232 1233 /* scan all the bios devices */ 1234 for (indx = 0; indx < 256; indx++) { 1235 snprintf(name, sizeof(name), "%s.%u", 1236 string, indx); 1237 parse(name, 1); 1238 } 1239 return (-1); 1240 } 1241 if ((name = strsep(bufpp, ".")) == NULL) { 1242 warnx("%s: incomplete specification", string); 1243 return (-1); 1244 } 1245 mib[3] = atoi(name); 1246 *typep = CTLTYPE_STRUCT; 1247 return (4); 1248 } else { 1249 *typep = bioslist.list[indx].ctl_type; 1250 return (3); 1251 } 1252} 1253#endif 1254 1255struct ctlname swpencname[] = CTL_SWPENC_NAMES; 1256struct list swpenclist = { swpencname, SWPENC_MAXID }; 1257 1258/* 1259 * handle swap encrypt requests 1260 */ 1261int 1262sysctl_swpenc(char *string, char **bufpp, int mib[], int flags, int *typep) 1263{ 1264 int indx; 1265 1266 if (*bufpp == NULL) { 1267 listall(string, &swpenclist); 1268 return (-1); 1269 } 1270 if ((indx = findname(string, "third", bufpp, &swpenclist)) == -1) 1271 return (-1); 1272 mib[2] = indx; 1273 *typep = swpenclist.list[indx].ctl_type; 1274 return (3); 1275} 1276 1277struct ctlname inetname[] = CTL_IPPROTO_NAMES; 1278struct ctlname ipname[] = IPCTL_NAMES; 1279struct ctlname icmpname[] = ICMPCTL_NAMES; 1280struct ctlname ipipname[] = IPIPCTL_NAMES; 1281struct ctlname tcpname[] = TCPCTL_NAMES; 1282struct ctlname udpname[] = UDPCTL_NAMES; 1283struct ctlname espname[] = ESPCTL_NAMES; 1284struct ctlname ahname[] = AHCTL_NAMES; 1285struct ctlname etheripname[] = ETHERIPCTL_NAMES; 1286struct ctlname grename[] = GRECTL_NAMES; 1287struct ctlname mobileipname[] = MOBILEIPCTL_NAMES; 1288struct ctlname ipcompname[] = IPCOMPCTL_NAMES; 1289struct list inetlist = { inetname, IPPROTO_MAXID }; 1290struct list inetvars[] = { 1291 { ipname, IPCTL_MAXID }, /* ip */ 1292 { icmpname, ICMPCTL_MAXID }, /* icmp */ 1293 { 0, 0 }, /* igmp */ 1294 { 0, 0 }, /* ggmp */ 1295 { ipipname, IPIPCTL_MAXID }, /* ipencap */ 1296 { 0, 0 }, 1297 { tcpname, TCPCTL_MAXID }, /* tcp */ 1298 { 0, 0 }, 1299 { 0, 0 }, /* egp */ 1300 { 0, 0 }, 1301 { 0, 0 }, 1302 { 0, 0 }, 1303 { 0, 0 }, /* pup */ 1304 { 0, 0 }, 1305 { 0, 0 }, 1306 { 0, 0 }, 1307 { 0, 0 }, 1308 { udpname, UDPCTL_MAXID }, /* udp */ 1309 { 0, 0 }, 1310 { 0, 0 }, 1311 { 0, 0 }, 1312 { 0, 0 }, 1313 { 0, 0 }, 1314 { 0, 0 }, 1315 { 0, 0 }, 1316 { 0, 0 }, 1317 { 0, 0 }, 1318 { 0, 0 }, 1319 { 0, 0 }, 1320 { 0, 0 }, 1321 { 0, 0 }, 1322 { 0, 0 }, 1323 { 0, 0 }, 1324 { 0, 0 }, 1325 { 0, 0 }, 1326 { 0, 0 }, 1327 { 0, 0 }, 1328 { 0, 0 }, 1329 { 0, 0 }, 1330 { 0, 0 }, 1331 { 0, 0 }, 1332 { 0, 0 }, 1333 { 0, 0 }, 1334 { 0, 0 }, 1335 { 0, 0 }, 1336 { 0, 0 }, 1337 { 0, 0 }, 1338 { grename, GRECTL_MAXID }, /* GRE */ 1339 { 0, 0 }, 1340 { 0, 0 }, 1341 { espname, ESPCTL_MAXID }, /* esp */ 1342 { ahname, AHCTL_MAXID }, /* ah */ 1343 { 0, 0 }, 1344 { 0, 0 }, 1345 { 0, 0 }, 1346 { mobileipname, MOBILEIPCTL_MAXID }, /* mobileip */ 1347 { 0, 0 }, 1348 { 0, 0 }, 1349 { 0, 0 }, 1350 { 0, 0 }, 1351 { 0, 0 }, 1352 { 0, 0 }, 1353 { 0, 0 }, 1354 { 0, 0 }, 1355 { 0, 0 }, 1356 { 0, 0 }, 1357 { 0, 0 }, 1358 { 0, 0 }, 1359 { 0, 0 }, 1360 { 0, 0 }, 1361 { 0, 0 }, 1362 { 0, 0 }, 1363 { 0, 0 }, 1364 { 0, 0 }, 1365 { 0, 0 }, 1366 { 0, 0 }, 1367 { 0, 0 }, 1368 { 0, 0 }, 1369 { 0, 0 }, 1370 { 0, 0 }, 1371 { 0, 0 }, 1372 { 0, 0 }, 1373 { 0, 0 }, 1374 { 0, 0 }, 1375 { 0, 0 }, 1376 { 0, 0 }, 1377 { 0, 0 }, 1378 { 0, 0 }, 1379 { 0, 0 }, 1380 { 0, 0 }, 1381 { 0, 0 }, 1382 { 0, 0 }, 1383 { 0, 0 }, 1384 { 0, 0 }, 1385 { 0, 0 }, 1386 { 0, 0 }, 1387 { 0, 0 }, 1388 { etheripname, ETHERIPCTL_MAXID }, 1389 { 0, 0 }, 1390 { 0, 0 }, 1391 { 0, 0 }, 1392 { 0, 0 }, 1393 { 0, 0 }, 1394 { 0, 0 }, 1395 { 0, 0 }, 1396 { 0, 0 }, 1397 { 0, 0 }, 1398 { 0, 0 }, 1399 { ipcompname, IPCOMPCTL_MAXID }, 1400}; 1401 1402struct list kernmalloclist = { kernmallocname, KERN_MALLOC_MAXID }; 1403struct list forkstatlist = { forkstatname, KERN_FORKSTAT_MAXID }; 1404struct list nchstatslist = { nchstatsname, KERN_NCHSTATS_MAXID }; 1405struct list ttylist = { ttyname, KERN_TTY_MAXID }; 1406struct list semlist = { semname, KERN_SEMINFO_MAXID }; 1407struct list shmlist = { shmname, KERN_SHMINFO_MAXID }; 1408struct list watchdoglist = { watchdogname, KERN_WATCHDOG_MAXID }; 1409 1410/* 1411 * handle vfs namei cache statistics 1412 */ 1413int 1414sysctl_nchstats(char *string, char **bufpp, int mib[], int flags, int *typep) 1415{ 1416 static struct nchstats nch; 1417 int indx; 1418 size_t size; 1419 static int keepvalue = 0; 1420 1421 if (*bufpp == NULL) { 1422 bzero(&nch, sizeof(struct nchstats)); 1423 listall(string, &nchstatslist); 1424 return (-1); 1425 } 1426 if ((indx = findname(string, "third", bufpp, &nchstatslist)) == -1) 1427 return (-1); 1428 mib[2] = indx; 1429 if (*bufpp != NULL) { 1430 warnx("fourth level name in %s is invalid", string); 1431 return (-1); 1432 } 1433 if (keepvalue == 0) { 1434 size = sizeof(struct nchstats); 1435 if (sysctl(mib, 2, &nch, &size, NULL, 0) < 0) 1436 return (-1); 1437 keepvalue = 1; 1438 } 1439 if (!nflag) 1440 (void)printf("%s = ", string); 1441 switch (indx) { 1442 case KERN_NCHSTATS_GOODHITS: 1443 (void)printf("%ld\n", nch.ncs_goodhits); 1444 break; 1445 case KERN_NCHSTATS_NEGHITS: 1446 (void)printf("%ld\n", nch.ncs_neghits); 1447 break; 1448 case KERN_NCHSTATS_BADHITS: 1449 (void)printf("%ld\n", nch.ncs_badhits); 1450 break; 1451 case KERN_NCHSTATS_FALSEHITS: 1452 (void)printf("%ld\n", nch.ncs_falsehits); 1453 break; 1454 case KERN_NCHSTATS_MISS: 1455 (void)printf("%ld\n", nch.ncs_miss); 1456 break; 1457 case KERN_NCHSTATS_LONG: 1458 (void)printf("%ld\n", nch.ncs_long); 1459 break; 1460 case KERN_NCHSTATS_PASS2: 1461 (void)printf("%ld\n", nch.ncs_pass2); 1462 break; 1463 case KERN_NCHSTATS_2PASSES: 1464 (void)printf("%ld\n", nch.ncs_2passes); 1465 break; 1466 } 1467 return (-1); 1468} 1469 1470/* 1471 * handle tty statistics 1472 */ 1473int 1474sysctl_tty(char *string, char **bufpp, int mib[], int flags, int *typep) 1475{ 1476 int indx; 1477 1478 if (*bufpp == NULL) { 1479 listall(string, &ttylist); 1480 return (-1); 1481 } 1482 if ((indx = findname(string, "third", bufpp, &ttylist)) == -1) 1483 return (-1); 1484 mib[2] = indx; 1485 *typep = CTLTYPE_QUAD; 1486 return (3); 1487} 1488 1489/* 1490 * handle fork statistics 1491 */ 1492int 1493sysctl_forkstat(char *string, char **bufpp, int mib[], int flags, int *typep) 1494{ 1495 static struct forkstat fks; 1496 static int keepvalue = 0; 1497 int indx; 1498 size_t size; 1499 1500 if (*bufpp == NULL) { 1501 bzero(&fks, sizeof(struct forkstat)); 1502 listall(string, &forkstatlist); 1503 return (-1); 1504 } 1505 if ((indx = findname(string, "third", bufpp, &forkstatlist)) == -1) 1506 return (-1); 1507 if (*bufpp != NULL) { 1508 warnx("fourth level name in %s is invalid", string); 1509 return (-1); 1510 } 1511 if (keepvalue == 0) { 1512 size = sizeof(struct forkstat); 1513 if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) 1514 return (-1); 1515 keepvalue = 1; 1516 } 1517 if (!nflag) 1518 (void)printf("%s = ", string); 1519 switch (indx) { 1520 case KERN_FORKSTAT_FORK: 1521 (void)printf("%d\n", fks.cntfork); 1522 break; 1523 case KERN_FORKSTAT_VFORK: 1524 (void)printf("%d\n", fks.cntvfork); 1525 break; 1526 case KERN_FORKSTAT_RFORK: 1527 (void)printf("%d\n", fks.cntrfork); 1528 break; 1529 case KERN_FORKSTAT_KTHREAD: 1530 (void)printf("%d\n", fks.cntkthread); 1531 break; 1532 case KERN_FORKSTAT_SIZFORK: 1533 (void)printf("%d\n", fks.sizfork); 1534 break; 1535 case KERN_FORKSTAT_SIZVFORK: 1536 (void)printf("%d\n", fks.sizvfork); 1537 break; 1538 case KERN_FORKSTAT_SIZRFORK: 1539 (void)printf("%d\n", fks.sizrfork); 1540 break; 1541 case KERN_FORKSTAT_SIZKTHREAD: 1542 (void)printf("%d\n", fks.sizkthread); 1543 break; 1544 } 1545 return (-1); 1546} 1547 1548/* 1549 * handle malloc statistics 1550 */ 1551int 1552sysctl_malloc(char *string, char **bufpp, int mib[], int flags, int *typep) 1553{ 1554 int indx, stor, i; 1555 char *name, bufp[BUFSIZ], *buf, *ptr; 1556 struct list lp; 1557 size_t size; 1558 1559 if (*bufpp == NULL) { 1560 listall(string, &kernmalloclist); 1561 return (-1); 1562 } 1563 if ((indx = findname(string, "third", bufpp, &kernmalloclist)) == -1) 1564 return (-1); 1565 mib[2] = indx; 1566 if (mib[2] == KERN_MALLOC_BUCKET) { 1567 if ((name = strsep(bufpp, ".")) == NULL) { 1568 size = BUFSIZ; 1569 stor = mib[2]; 1570 mib[2] = KERN_MALLOC_BUCKETS; 1571 buf = bufp; 1572 if (sysctl(mib, 3, buf, &size, NULL, 0) < 0) 1573 return (-1); 1574 mib[2] = stor; 1575 for (stor = 0, i = 0; i < size; i++) 1576 if (buf[i] == ',') 1577 stor++; 1578 lp.list = calloc(stor + 2, sizeof(struct ctlname)); 1579 if (lp.list == NULL) 1580 return (-1); 1581 lp.size = stor + 2; 1582 for (i = 1; 1583 (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL; 1584 i++) { 1585 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1586 } 1587 lp.list[i].ctl_name = buf; 1588 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1589 listall(string, &lp); 1590 free(lp.list); 1591 return (-1); 1592 } 1593 mib[3] = atoi(name); 1594 return (4); 1595 } else if (mib[2] == KERN_MALLOC_BUCKETS) { 1596 *typep = CTLTYPE_STRING; 1597 return (3); 1598 } else if (mib[2] == KERN_MALLOC_KMEMSTATS) { 1599 size = BUFSIZ; 1600 stor = mib[2]; 1601 mib[2] = KERN_MALLOC_KMEMNAMES; 1602 buf = bufp; 1603 if (sysctl(mib, 3, buf, &size, NULL, 0) < 0) 1604 return (-1); 1605 mib[2] = stor; 1606 if ((name = strsep(bufpp, ".")) == NULL) { 1607 for (stor = 0, i = 0; i < size; i++) 1608 if (buf[i] == ',') 1609 stor++; 1610 lp.list = calloc(stor + 2, sizeof(struct ctlname)); 1611 if (lp.list == NULL) 1612 return (-1); 1613 lp.size = stor + 2; 1614 for (i = 1; (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL; i++) { 1615 if (lp.list[i].ctl_name[0] == '\0') { 1616 i--; 1617 continue; 1618 } 1619 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1620 } 1621 lp.list[i].ctl_name = buf; 1622 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1623 listall(string, &lp); 1624 free(lp.list); 1625 return (-1); 1626 } 1627 ptr = strstr(buf, name); 1628 tryagain: 1629 if (ptr == NULL) { 1630 warnx("fourth level name %s in %s is invalid", name, 1631 string); 1632 return (-1); 1633 } 1634 if ((*(ptr + strlen(name)) != ',') && 1635 (*(ptr + strlen(name)) != '\0')) { 1636 ptr = strstr(ptr + 1, name); /* retry */ 1637 goto tryagain; 1638 } 1639 if ((ptr != buf) && (*(ptr - 1) != ',')) { 1640 ptr = strstr(ptr + 1, name); /* retry */ 1641 goto tryagain; 1642 } 1643 for (i = 0, stor = 0; buf + i < ptr; i++) 1644 if (buf[i] == ',') 1645 stor++; 1646 mib[3] = stor; 1647 return (4); 1648 } else if (mib[2] == KERN_MALLOC_KMEMNAMES) { 1649 *typep = CTLTYPE_STRING; 1650 return (3); 1651 } 1652 return (-1); 1653} 1654 1655#ifdef CPU_CHIPSET 1656/* 1657 * handle machdep.chipset requests 1658 */ 1659struct ctlname chipsetname[] = CTL_CHIPSET_NAMES; 1660struct list chipsetlist = { chipsetname, CPU_CHIPSET_MAXID }; 1661 1662int 1663sysctl_chipset(char *string, char **bufpp, int mib[], int flags, int *typep) 1664{ 1665 int indx, bwx; 1666 static void *q; 1667 size_t len; 1668 char *p; 1669 1670 if (*bufpp == NULL) { 1671 listall(string, &chipsetlist); 1672 return (-1); 1673 } 1674 if ((indx = findname(string, "third", bufpp, &chipsetlist)) == -1) 1675 return (-1); 1676 mib[2] = indx; 1677 if (!nflag) 1678 printf("%s = ", string); 1679 switch(mib[2]) { 1680 case CPU_CHIPSET_MEM: 1681 case CPU_CHIPSET_DENSE: 1682 case CPU_CHIPSET_PORTS: 1683 case CPU_CHIPSET_HAE_MASK: 1684 len = sizeof(void *); 1685 if (sysctl(mib, 3, &q, &len, NULL, 0) < 0) 1686 return (-1); 1687 printf("%p\n", q); 1688 break; 1689 case CPU_CHIPSET_BWX: 1690 len = sizeof(int); 1691 if (sysctl(mib, 3, &bwx, &len, NULL, 0) < 0) 1692 return (-1); 1693 printf("%d\n", bwx); 1694 break; 1695 case CPU_CHIPSET_TYPE: 1696 if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0) 1697 return (-1); 1698 p = malloc(len + 1); 1699 if (p == NULL) 1700 return (-1); 1701 if (sysctl(mib, 3, p, &len, NULL, 0) < 0) 1702 return (-1); 1703 p[len] = '\0'; 1704 printf("%s\n", p); 1705 break; 1706 } 1707 return (-1); 1708} 1709#endif 1710/* 1711 * handle internet requests 1712 */ 1713int 1714sysctl_inet(char *string, char **bufpp, int mib[], int flags, int *typep) 1715{ 1716 struct list *lp; 1717 int indx; 1718 1719 if (*bufpp == NULL) { 1720 listall(string, &inetlist); 1721 return (-1); 1722 } 1723 if ((indx = findname(string, "third", bufpp, &inetlist)) == -1) 1724 return (-1); 1725 mib[2] = indx; 1726 if (indx < IPPROTO_MAXID && inetvars[indx].list != NULL) 1727 lp = &inetvars[indx]; 1728 else if (!flags) 1729 return (-1); 1730 else { 1731 warnx("%s: no variables defined for this protocol", string); 1732 return (-1); 1733 } 1734 if (*bufpp == NULL) { 1735 listall(string, lp); 1736 return (-1); 1737 } 1738 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1739 return (-1); 1740 mib[3] = indx; 1741 *typep = lp->list[indx].ctl_type; 1742 return (4); 1743} 1744 1745#ifdef INET6 1746struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES; 1747struct ctlname ip6name[] = IPV6CTL_NAMES; 1748struct ctlname icmp6name[] = ICMPV6CTL_NAMES; 1749struct ctlname pim6name[] = PIM6CTL_NAMES; 1750struct list inet6list = { inet6name, IPV6PROTO_MAXID }; 1751struct list inet6vars[] = { 1752/*0*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1753 { 0, 0 }, 1754 { 0, 0 }, 1755 { 0, 0 }, 1756 { 0, 0 }, 1757 { 0, 0 }, 1758/*10*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1759 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1760/*20*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1761 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1762/*30*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1763 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1764/*40*/ { 0, 0 }, 1765 { ip6name, IPV6CTL_MAXID }, /* ipv6 */ 1766 { 0, 0 }, 1767 { 0, 0 }, 1768 { 0, 0 }, 1769 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1770/*50*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1771 { 0, 0 }, 1772 { 0, 0 }, 1773 { 0, 0 }, 1774 { icmp6name, ICMPV6CTL_MAXID }, /* icmp6 */ 1775 { 0, 0 }, 1776/*60*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1777 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1778/*70*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1779 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1780/*80*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1781 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1782/*90*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1783 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1784/*100*/ { 0, 0 }, 1785 { 0, 0 }, 1786 { 0, 0 }, 1787 { pim6name, PIM6CTL_MAXID }, /* pim6 */ 1788}; 1789 1790/* 1791 * handle internet6 requests 1792 */ 1793int 1794sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep) 1795{ 1796 struct list *lp; 1797 int indx; 1798 1799 if (*bufpp == NULL) { 1800 listall(string, &inet6list); 1801 return (-1); 1802 } 1803 if ((indx = findname(string, "third", bufpp, &inet6list)) == -1) 1804 return (-1); 1805 mib[2] = indx; 1806 if (indx < IPV6PROTO_MAXID && inet6vars[indx].list != NULL) 1807 lp = &inet6vars[indx]; 1808 else if (!flags) 1809 return (-1); 1810 else { 1811 warnx("%s: no variables defined for this protocol", string); 1812 return (-1); 1813 } 1814 if (*bufpp == NULL) { 1815 listall(string, lp); 1816 return (-1); 1817 } 1818 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1819 return (-1); 1820 mib[3] = indx; 1821 *typep = lp->list[indx].ctl_type; 1822 return (4); 1823} 1824#endif 1825 1826struct ctlname ipxname[] = CTL_IPXPROTO_NAMES; 1827struct ctlname ipxpname[] = IPXCTL_NAMES; 1828struct ctlname spxpname[] = SPXCTL_NAMES; 1829struct list ipxlist = { ipxname, IPXCTL_MAXID }; 1830struct list ipxvars[] = { 1831 { ipxpname, IPXCTL_MAXID }, /* ipx */ 1832 { 0, 0 }, 1833 { 0, 0 }, 1834 { 0, 0 }, 1835 { 0, 0 }, 1836 { spxpname, SPXCTL_MAXID }, 1837}; 1838 1839/* 1840 * Handle internet requests 1841 */ 1842int 1843sysctl_ipx(char *string, char **bufpp, int mib[], int flags, int *typep) 1844{ 1845 struct list *lp; 1846 int indx; 1847 1848 if (*bufpp == NULL) { 1849 listall(string, &ipxlist); 1850 return (-1); 1851 } 1852 if ((indx = findname(string, "third", bufpp, &ipxlist)) == -1) 1853 return (-1); 1854 mib[2] = indx; 1855 if (indx <= IPXPROTO_SPX && ipxvars[indx].list != NULL) 1856 lp = &ipxvars[indx]; 1857 else if (!flags) 1858 return (-1); 1859 else { 1860 warnx("%s: no variables defined for this protocol", string); 1861 return (-1); 1862 } 1863 if (*bufpp == NULL) { 1864 listall(string, lp); 1865 return (-1); 1866 } 1867 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1868 return (-1); 1869 mib[3] = indx; 1870 *typep = lp->list[indx].ctl_type; 1871 return (4); 1872} 1873 1874/* 1875 * Handle SysV semaphore info requests 1876 */ 1877int 1878sysctl_seminfo(string, bufpp, mib, flags, typep) 1879 char *string; 1880 char **bufpp; 1881 int mib[]; 1882 int flags; 1883 int *typep; 1884{ 1885 int indx; 1886 1887 if (*bufpp == NULL) { 1888 listall(string, &semlist); 1889 return (-1); 1890 } 1891 if ((indx = findname(string, "third", bufpp, &semlist)) == -1) 1892 return (-1); 1893 mib[2] = indx; 1894 *typep = CTLTYPE_INT; 1895 return (3); 1896} 1897 1898/* 1899 * Handle SysV shared memory info requests 1900 */ 1901int 1902sysctl_shminfo(string, bufpp, mib, flags, typep) 1903 char *string; 1904 char **bufpp; 1905 int mib[]; 1906 int flags; 1907 int *typep; 1908{ 1909 int indx; 1910 1911 if (*bufpp == NULL) { 1912 listall(string, &shmlist); 1913 return (-1); 1914 } 1915 if ((indx = findname(string, "third", bufpp, &shmlist)) == -1) 1916 return (-1); 1917 mib[2] = indx; 1918 *typep = CTLTYPE_INT; 1919 return (3); 1920} 1921 1922/* 1923 * Handle watchdog support 1924 */ 1925int 1926sysctl_watchdog(char *string, char **bufpp, int mib[], int flags, 1927 int *typep) 1928{ 1929 int indx; 1930 1931 if (*bufpp == NULL) { 1932 listall(string, &watchdoglist); 1933 return (-1); 1934 } 1935 if ((indx = findname(string, "third", bufpp, &watchdoglist)) == -1) 1936 return (-1); 1937 mib[2] = indx; 1938 *typep = watchdoglist.list[indx].ctl_type; 1939 return (3); 1940} 1941 1942/* 1943 * Handle hardware monitoring sensors support 1944 */ 1945int 1946sysctl_sensors(char *string, char **bufpp, int mib[], int flags, int *typep) 1947{ 1948 char *name; 1949 int indx; 1950 1951 if (*bufpp == NULL) { 1952 char name[BUFSIZ]; 1953 1954 /* scan all sensors */ 1955 for (indx = 0; indx < 256; indx++) { 1956 snprintf(name, sizeof(name), "%s.%u", string, indx); 1957 parse(name, 0); 1958 } 1959 return (-1); 1960 } 1961 if ((name = strsep(bufpp, ".")) == NULL) { 1962 warnx("%s: incomplete specification", string); 1963 return (-1); 1964 } 1965 mib[2] = atoi(name); 1966 *typep = CTLTYPE_STRUCT; 1967 return (3); 1968} 1969 1970/* 1971 * Scan a list of names searching for a particular name. 1972 */ 1973int 1974findname(char *string, char *level, char **bufp, struct list *namelist) 1975{ 1976 char *name; 1977 int i; 1978 1979 if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) { 1980 warnx("%s: incomplete specification", string); 1981 return (-1); 1982 } 1983 for (i = 0; i < namelist->size; i++) 1984 if (namelist->list[i].ctl_name != NULL && 1985 strcmp(name, namelist->list[i].ctl_name) == 0) 1986 break; 1987 if (i == namelist->size) { 1988 warnx("%s level name %s in %s is invalid", level, name, string); 1989 return (-1); 1990 } 1991 return (i); 1992} 1993 1994void 1995usage(void) 1996{ 1997 1998 (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n\t%s\n", 1999 "sysctl [-n] variable ...", "sysctl [-nq] -w variable=value ...", 2000 "sysctl [-n] -a", "sysctl [-n] -A"); 2001 exit(1); 2002} 2003