sysctl.c revision 1.139
1/* $OpenBSD: sysctl.c,v 1.139 2006/04/25 04:37:01 deraadt 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#ifndef lint 34static const char copyright[] = 35"@(#) Copyright (c) 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37#endif /* not lint */ 38 39#ifndef lint 40#if 0 41static const char sccsid[] = "@(#)sysctl.c 8.5 (Berkeley) 5/9/95"; 42#else 43static const char rcsid[] = "$OpenBSD: sysctl.c,v 1.139 2006/04/25 04:37:01 deraadt Exp $"; 44#endif 45#endif /* not lint */ 46 47#include <sys/param.h> 48#include <sys/gmon.h> 49#include <sys/mount.h> 50#include <sys/stat.h> 51#include <sys/sem.h> 52#include <sys/shm.h> 53#include <sys/sysctl.h> 54#include <sys/socket.h> 55#include <sys/malloc.h> 56#include <sys/dkstat.h> 57#include <sys/uio.h> 58#include <sys/tty.h> 59#include <sys/namei.h> 60#include <sys/sensors.h> 61#include <machine/cpu.h> 62#include <net/route.h> 63#include <net/if.h> 64 65#include <netinet/in.h> 66#include <netinet/in_systm.h> 67#include <netinet/ip.h> 68#include <netinet/in_pcb.h> 69#include <netinet/ip_icmp.h> 70#include <netinet/ip_ipip.h> 71#include <netinet/ip_ether.h> 72#include <netinet/ip_ah.h> 73#include <netinet/ip_esp.h> 74#include <netinet/icmp_var.h> 75#include <netinet/ip_var.h> 76#include <netinet/udp.h> 77#include <netinet/udp_var.h> 78#include <netinet/tcp.h> 79#include <netinet/tcp_timer.h> 80#include <netinet/tcp_var.h> 81#include <netinet/ip_gre.h> 82#include <netinet/ip_ipcomp.h> 83#include <netinet/ip_carp.h> 84 85#ifdef INET6 86#include <netinet/ip6.h> 87#include <netinet/icmp6.h> 88#include <netinet6/ip6_var.h> 89#include <netinet6/pim6_var.h> 90#endif 91 92#include <uvm/uvm_swap_encrypt.h> 93 94#include <ufs/ufs/quota.h> 95#include <ufs/ufs/inode.h> 96#include <ufs/ffs/fs.h> 97#include <ufs/ffs/ffs_extern.h> 98 99#include <nfs/rpcv2.h> 100#include <nfs/nfsproto.h> 101#include <nfs/nfs.h> 102 103#include <netipx/ipx.h> 104#include <netipx/ipx_var.h> 105#include <netipx/spx_var.h> 106#include <ddb/db_var.h> 107#include <dev/rndvar.h> 108 109#include <err.h> 110#include <errno.h> 111#include <stdio.h> 112#include <stdlib.h> 113#include <string.h> 114#include <ctype.h> 115 116#ifdef CPU_BIOS 117#include <machine/biosvar.h> 118#endif 119 120struct ctlname topname[] = CTL_NAMES; 121struct ctlname kernname[] = CTL_KERN_NAMES; 122struct ctlname vmname[] = CTL_VM_NAMES; 123struct ctlname fsname[] = CTL_FS_NAMES; 124struct ctlname netname[] = CTL_NET_NAMES; 125struct ctlname hwname[] = CTL_HW_NAMES; 126struct ctlname username[] = CTL_USER_NAMES; 127struct ctlname debugname[CTL_DEBUG_MAXID]; 128struct ctlname kernmallocname[] = CTL_KERN_MALLOC_NAMES; 129struct ctlname forkstatname[] = CTL_KERN_FORKSTAT_NAMES; 130struct ctlname nchstatsname[] = CTL_KERN_NCHSTATS_NAMES; 131struct ctlname ttysname[] = CTL_KERN_TTY_NAMES; 132struct ctlname semname[] = CTL_KERN_SEMINFO_NAMES; 133struct ctlname shmname[] = CTL_KERN_SHMINFO_NAMES; 134struct ctlname watchdogname[] = CTL_KERN_WATCHDOG_NAMES; 135struct ctlname tcname[] = CTL_KERN_TIMECOUNTER_NAMES; 136struct ctlname *vfsname; 137#ifdef CTL_MACHDEP_NAMES 138struct ctlname machdepname[] = CTL_MACHDEP_NAMES; 139#endif 140struct ctlname ddbname[] = CTL_DDB_NAMES; 141char names[BUFSIZ]; 142int lastused; 143 144struct list { 145 struct ctlname *list; 146 int size; 147}; 148struct list toplist = { topname, CTL_MAXID }; 149struct list secondlevel[] = { 150 { 0, 0 }, /* CTL_UNSPEC */ 151 { kernname, KERN_MAXID }, /* CTL_KERN */ 152 { vmname, VM_MAXID }, /* CTL_VM */ 153 { fsname, FS_MAXID }, /* CTL_FS */ 154 { netname, NET_MAXID }, /* CTL_NET */ 155 { 0, CTL_DEBUG_MAXID }, /* CTL_DEBUG */ 156 { hwname, HW_MAXID }, /* CTL_HW */ 157#ifdef CTL_MACHDEP_NAMES 158 { machdepname, CPU_MAXID }, /* CTL_MACHDEP */ 159#else 160 { 0, 0 }, /* CTL_MACHDEP */ 161#endif 162 { username, USER_MAXID }, /* CTL_USER_NAMES */ 163 { ddbname, DBCTL_MAXID }, /* CTL_DDB_NAMES */ 164 { 0, 0 }, /* CTL_VFS */ 165}; 166 167int Aflag, aflag, nflag, qflag; 168 169/* 170 * Variables requiring special processing. 171 */ 172#define CLOCK 0x00000001 173#define BOOTTIME 0x00000002 174#define CHRDEV 0x00000004 175#define BLKDEV 0x00000008 176#define RNDSTATS 0x00000010 177#define BADDYNAMIC 0x00000020 178#define BIOSGEO 0x00000040 179#define BIOSDEV 0x00000080 180#define MAJ2DEV 0x00000100 181#define UNSIGNED 0x00000200 182#define KMEMBUCKETS 0x00000400 183#define LONGARRAY 0x00000800 184#define KMEMSTATS 0x00001000 185#define SENSORS 0x00002000 186#define ZTSSCALE 0x00004000 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_bpf(char *, char **, int *, int, int *); 200int sysctl_ipx(char *, char **, int *, int, int *); 201int sysctl_fs(char *, char **, int *, int, int *); 202static int sysctl_vfs(char *, char **, int[], int, int *); 203static int sysctl_vfsgen(char *, char **, int[], int, int *); 204int sysctl_bios(char *, char **, int *, int, int *); 205int sysctl_swpenc(char *, char **, int *, int, int *); 206int sysctl_forkstat(char *, char **, int *, int, int *); 207int sysctl_tty(char *, char **, int *, int, int *); 208int sysctl_nchstats(char *, char **, int *, int, int *); 209int sysctl_malloc(char *, char **, int *, int, int *); 210int sysctl_seminfo(char *, char **, int *, int, int *); 211int sysctl_shminfo(char *, char **, int *, int, int *); 212int sysctl_watchdog(char *, char **, int *, int, int *); 213int sysctl_tc(char *, char **, int *, int, int *); 214int sysctl_sensors(char *, char **, int *, int, int *); 215void print_sensor(struct sensor *); 216int sysctl_emul(char *, char *, int); 217#ifdef CPU_CHIPSET 218int sysctl_chipset(char *, char **, int *, int, int *); 219#endif 220void vfsinit(void); 221 222char *equ = "="; 223 224int 225main(int argc, char *argv[]) 226{ 227 int ch, lvl1; 228 229 while ((ch = getopt(argc, argv, "Aanqw")) != -1) { 230 switch (ch) { 231 232 case 'A': 233 Aflag = 1; 234 break; 235 236 case 'a': 237 aflag = 1; 238 break; 239 240 case 'n': 241 nflag = 1; 242 break; 243 244 case 'q': 245 qflag = 1; 246 break; 247 248 case 'w': 249 /* flag no longer needed; var=value implies write */ 250 break; 251 252 default: 253 usage(); 254 } 255 } 256 argc -= optind; 257 argv += optind; 258 259 if (argc == 0 && (Aflag || aflag)) { 260 debuginit(); 261 vfsinit(); 262 for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++) 263 listall(topname[lvl1].ctl_name, &secondlevel[lvl1]); 264 return (0); 265 } 266 if (argc == 0) 267 usage(); 268 for (; *argv != NULL; ++argv) 269 parse(*argv, 1); 270 return (0); 271} 272 273/* 274 * List all variables known to the system. 275 */ 276void 277listall(char *prefix, struct list *lp) 278{ 279 char *cp, name[BUFSIZ]; 280 int lvl2, len; 281 282 if (lp->list == NULL) 283 return; 284 if ((len = strlcpy(name, prefix, sizeof(name))) >= sizeof(name)) 285 errx(1, "%s: name too long", prefix); 286 cp = name + len++; 287 *cp++ = '.'; 288 for (lvl2 = 0; lvl2 < lp->size; lvl2++) { 289 if (lp->list[lvl2].ctl_name == NULL) 290 continue; 291 if (strlcpy(cp, lp->list[lvl2].ctl_name, 292 sizeof(name) - len) >= sizeof(name) - len) 293 warn("%s: name too long", lp->list[lvl2].ctl_name); 294 parse(name, Aflag); 295 } 296} 297 298/* 299 * Parse a name into a MIB entry. 300 * Lookup and print out the MIB entry if it exists. 301 * Set a new value if requested. 302 */ 303void 304parse(char *string, int flags) 305{ 306 int indx, type, state, intval, len; 307 size_t size, newsize = 0; 308 int lal = 0, special = 0; 309 void *newval = 0; 310 int64_t quadval; 311 struct list *lp; 312 int mib[CTL_MAXNAME]; 313 char *cp, *bufp, buf[BUFSIZ]; 314#ifdef CPU_ZTSSCALE 315 struct ztsscale { 316 int ts_minx; 317 int ts_maxx; 318 int ts_miny; 319 int ts_maxy; 320 } tsbuf; 321#endif 322 323 (void)strlcpy(buf, string, sizeof(buf)); 324 bufp = buf; 325 if ((cp = strchr(string, '=')) != NULL) { 326 *strchr(buf, '=') = '\0'; 327 *cp++ = '\0'; 328 while (isspace(*cp)) 329 cp++; 330 newval = cp; 331 newsize = strlen(cp); 332 } 333 if ((indx = findname(string, "top", &bufp, &toplist)) == -1) 334 return; 335 mib[0] = indx; 336 if (indx == CTL_VFS) 337 vfsinit(); 338 if (indx == CTL_DEBUG) 339 debuginit(); 340 lp = &secondlevel[indx]; 341 if (lp->list == 0) { 342 warnx("%s: class is not implemented", topname[indx].ctl_name); 343 return; 344 } 345 if (bufp == NULL) { 346 listall(topname[indx].ctl_name, lp); 347 return; 348 } 349 if ((indx = findname(string, "second", &bufp, lp)) == -1) 350 return; 351 mib[1] = indx; 352 type = lp->list[indx].ctl_type; 353 len = 2; 354 switch (mib[0]) { 355 356 case CTL_KERN: 357 switch (mib[1]) { 358 case KERN_PROF: 359 mib[2] = GPROF_STATE; 360 size = sizeof(state); 361 if (sysctl(mib, 3, &state, &size, NULL, 0) == -1) { 362 if (flags == 0) 363 return; 364 if (!nflag) 365 (void)printf("%s: ", string); 366 (void)puts("kernel is not compiled for profiling"); 367 return; 368 } 369 if (!nflag) 370 (void)printf("%s = %s\n", string, 371 state == GMON_PROF_OFF ? "off" : "running"); 372 return; 373 case KERN_FORKSTAT: 374 sysctl_forkstat(string, &bufp, mib, flags, &type); 375 return; 376 case KERN_TTY: 377 len = sysctl_tty(string, &bufp, mib, flags, &type); 378 if (len < 0) 379 return; 380 break; 381 case KERN_NCHSTATS: 382 sysctl_nchstats(string, &bufp, mib, flags, &type); 383 return; 384 case KERN_MALLOCSTATS: 385 len = sysctl_malloc(string, &bufp, mib, flags, &type); 386 if (len < 0) 387 return; 388 if (mib[2] == KERN_MALLOC_BUCKET) 389 special |= KMEMBUCKETS; 390 if (mib[2] == KERN_MALLOC_KMEMSTATS) 391 special |= KMEMSTATS; 392 newsize = 0; 393 break; 394 case KERN_MBSTAT: 395 if (flags == 0) 396 return; 397 warnx("use netstat to view %s", string); 398 return; 399 case KERN_MSGBUF: 400 if (flags == 0) 401 return; 402 warnx("use dmesg to view %s", string); 403 return; 404 case KERN_VNODE: 405 case KERN_FILE: 406 if (flags == 0) 407 return; 408 warnx("use pstat to view %s information", string); 409 return; 410 case KERN_PROC: 411 case KERN_PROC2: 412 if (flags == 0) 413 return; 414 warnx("use ps to view %s information", string); 415 return; 416 case KERN_CLOCKRATE: 417 special |= CLOCK; 418 break; 419 case KERN_BOOTTIME: 420 special |= BOOTTIME; 421 break; 422 case KERN_RND: 423 special |= RNDSTATS; 424 break; 425 case KERN_HOSTID: 426 case KERN_ARND: 427 special |= UNSIGNED; 428 break; 429 case KERN_CPTIME: 430 special |= LONGARRAY; 431 lal = CPUSTATES; 432 break; 433 case KERN_SEMINFO: 434 len = sysctl_seminfo(string, &bufp, mib, flags, &type); 435 if (len < 0) 436 return; 437 break; 438 case KERN_SHMINFO: 439 len = sysctl_shminfo(string, &bufp, mib, flags, &type); 440 if (len < 0) 441 return; 442 break; 443 case KERN_WATCHDOG: 444 len = sysctl_watchdog(string, &bufp, mib, flags, 445 &type); 446 if (len < 0) 447 return; 448 break; 449 case KERN_TIMECOUNTER: 450 len = sysctl_tc(string, &bufp, mib, flags, 451 &type); 452 if (len < 0) 453 return; 454 break; 455 case KERN_EMUL: 456 sysctl_emul(string, newval, flags); 457 return; 458 } 459 break; 460 461 case CTL_HW: 462 switch (mib[1]) { 463 case HW_DISKSTATS: 464 /* 465 * Only complain if someone asks explicitly for this, 466 * otherwise "fail" silently. 467 */ 468 if (flags) 469 warnx("use vmstat to view %s information", 470 string); 471 return; 472 case HW_SENSORS: 473 special |= SENSORS; 474 len = sysctl_sensors(string, &bufp, mib, flags, &type); 475 if (len < 0) 476 return; 477 break; 478 case HW_PHYSMEM: 479 case HW_USERMEM: 480 special |= UNSIGNED; 481 break; 482 } 483 break; 484 485 case CTL_VM: 486 if (mib[1] == VM_LOADAVG) { 487 double loads[3]; 488 489 getloadavg(loads, 3); 490 if (!nflag) 491 (void)printf("%s%s", string, equ); 492 (void)printf("%.2f %.2f %.2f\n", loads[0], 493 loads[1], loads[2]); 494 return; 495 } else if (mib[1] == VM_PSSTRINGS) { 496 struct _ps_strings _ps; 497 498 size = sizeof(_ps); 499 if (sysctl(mib, 2, &_ps, &size, NULL, 0) == -1) { 500 if (flags == 0) 501 return; 502 if (!nflag) 503 (void)printf("%s: ", string); 504 (void)puts("can't find ps strings"); 505 return; 506 } 507 if (!nflag) 508 (void)printf("%s%s", string, equ); 509 (void)printf("%p\n", _ps.val); 510 return; 511 } else if (mib[1] == VM_SWAPENCRYPT) { 512 len = sysctl_swpenc(string, &bufp, mib, flags, &type); 513 if (len < 0) 514 return; 515 516 break; 517 } else if (mib[1] == VM_NKMEMPAGES || 518 mib[1] == VM_ANONMIN || 519 mib[1] == VM_VTEXTMIN || 520 mib[1] == VM_VNODEMIN) { 521 break; 522 } 523 if (flags == 0) 524 return; 525 warnx("use vmstat or systat to view %s information", string); 526 return; 527 528 break; 529 530 case CTL_NET: 531 if (mib[1] == PF_INET) { 532 len = sysctl_inet(string, &bufp, mib, flags, &type); 533 if (len < 0) 534 return; 535 536 if ((mib[2] == IPPROTO_TCP && 537 mib[3] == TCPCTL_BADDYNAMIC) || 538 (mib[2] == IPPROTO_UDP && 539 mib[3] == UDPCTL_BADDYNAMIC)) { 540 541 special |= BADDYNAMIC; 542 543 if (newval != NULL) 544 parse_baddynamic(mib, len, string, 545 &newval, &newsize, flags, nflag); 546 } 547 break; 548 } 549#ifdef INET6 550 if (mib[1] == PF_INET6) { 551 len = sysctl_inet6(string, &bufp, mib, flags, &type); 552 if (len < 0) 553 return; 554 555 break; 556 } 557#endif 558 if (mib[1] == PF_IPX) { 559 len = sysctl_ipx(string, &bufp, mib, flags, &type); 560 if (len >= 0) 561 break; 562 return; 563 } 564 if (mib[1] == PF_BPF) { 565 len = sysctl_bpf(string, &bufp, mib, flags, &type); 566 if (len < 0) 567 return; 568 break; 569 } 570 if (flags == 0) 571 return; 572 warnx("use netstat to view %s information", string); 573 return; 574 575 case CTL_DEBUG: 576 mib[2] = CTL_DEBUG_VALUE; 577 len = 3; 578 break; 579 580 case CTL_MACHDEP: 581#ifdef CPU_CONSDEV 582 if (mib[1] == CPU_CONSDEV) 583 special |= CHRDEV; 584#endif 585#ifdef CPU_BLK2CHR 586 if (mib[1] == CPU_BLK2CHR) { 587 if (bufp == NULL) 588 return; 589 mib[2] = makedev(atoi(bufp),0); 590 bufp = NULL; 591 len = 3; 592 special |= CHRDEV; 593 break; 594 } 595#endif 596#ifdef CPU_CHR2BLK 597 if (mib[1] == CPU_CHR2BLK) { 598 if (bufp == NULL) 599 return; 600 mib[2] = makedev(atoi(bufp),0); 601 bufp = NULL; 602 len = 3; 603 special |= BLKDEV; 604 break; 605 } 606#endif 607#ifdef CPU_BIOS 608 if (mib[1] == CPU_BIOS) { 609 len = sysctl_bios(string, &bufp, mib, flags, &type); 610 if (len < 0) 611 return; 612 if (mib[2] == BIOS_DEV) 613 special |= BIOSDEV; 614 if (mib[2] == BIOS_DISKINFO) 615 special |= BIOSGEO; 616 break; 617 } 618#endif 619#ifdef CPU_CHIPSET 620 if (mib[1] == CPU_CHIPSET) { 621 len = sysctl_chipset(string, &bufp, mib, flags, &type); 622 if (len < 0) 623 return; 624 break; 625 } 626#endif 627#ifdef CPU_ZTSSCALE 628 if (mib[1] == CPU_ZTSSCALE) { 629 special |= ZTSSCALE; 630 if (newsize > 0) { 631 const char *errstr = 0; 632 633 /* Unspecified values default to 0. */ 634 bzero(&tsbuf, sizeof tsbuf); 635 newval = (void *)strtok(newval, ","); 636 if (newval != NULL) { 637 tsbuf.ts_minx = (int)strtonum(newval, 638 0, 32768, &errstr); 639 newval = (void *)strtok(NULL, ","); 640 } 641 if (!errstr && newval != NULL) { 642 tsbuf.ts_maxx = (int)strtonum(newval, 643 0, 32768, &errstr); 644 newval = (void *)strtok(NULL, ","); 645 } 646 if (!errstr && newval != NULL) { 647 tsbuf.ts_miny = (int)strtonum(newval, 648 0, 32768, &errstr); 649 newval = (void *)strtok(NULL, ","); 650 } 651 if (!errstr && newval != NULL) { 652 tsbuf.ts_maxy = (int)strtonum(newval, 653 0, 32768, &errstr); 654 newval = (void *)strtok(NULL, ","); 655 } 656 if (errstr) 657 errx(1, "calibration value is %s", 658 errstr); 659 if (newval != NULL) 660 errx(1, "too many calibration values"); 661 newval = &tsbuf; 662 newsize = sizeof(tsbuf); 663 } 664 break; 665 } 666#endif 667 break; 668 669 case CTL_FS: 670 len = sysctl_fs(string, &bufp, mib, flags, &type); 671 if (len >= 0) 672 break; 673 return; 674 675 case CTL_VFS: 676 if (mib[1]) 677 len = sysctl_vfs(string, &bufp, mib, flags, &type); 678 else 679 len = sysctl_vfsgen(string, &bufp, mib, flags, &type); 680 if (len >= 0) { 681 if (type == CTLTYPE_STRUCT) { 682 if (flags) 683 warnx("use nfsstat to view %s information", 684 MOUNT_NFS); 685 return; 686 } else 687 break; 688 } 689 return; 690 691 case CTL_USER: 692 case CTL_DDB: 693 break; 694 695 default: 696 warnx("illegal top level value: %d", mib[0]); 697 return; 698 699 } 700 if (bufp) { 701 warnx("name %s in %s is unknown", bufp, string); 702 return; 703 } 704 if (newsize > 0) { 705 switch (type) { 706 case CTLTYPE_INT: 707 errno = 0; 708 if (special & UNSIGNED) 709 intval = strtoul(newval, &cp, 10); 710 else 711 intval = strtol(newval, &cp, 10); 712 if (*cp != '\0') { 713 warnx("%s: illegal value: %s", string, 714 (char *)newval); 715 return; 716 } 717 if (errno == ERANGE) { 718 warnx("%s: value %s out of range", string, 719 (char *)newval); 720 return; 721 } 722 newval = &intval; 723 newsize = sizeof(intval); 724 break; 725 726 case CTLTYPE_QUAD: 727 /* XXX - assumes sizeof(long long) == sizeof(quad_t) */ 728 (void)sscanf(newval, "%lld", (long long *)&quadval); 729 newval = &quadval; 730 newsize = sizeof(quadval); 731 break; 732 } 733 } 734 size = BUFSIZ; 735 if (sysctl(mib, len, buf, &size, newval, newsize) == -1) { 736 if (flags == 0) 737 return; 738 switch (errno) { 739 case EOPNOTSUPP: 740 warnx("%s: value is not available", string); 741 return; 742 case ENOTDIR: 743 warnx("%s: specification is incomplete", string); 744 return; 745 case ENOMEM: 746 warnx("%s: type is unknown to this program", string); 747 return; 748 case ENXIO: 749 if (special & BIOSGEO) 750 return; 751 default: 752 warn("%s", string); 753 return; 754 } 755 } 756 if (special & KMEMBUCKETS) { 757 struct kmembuckets *kb = (struct kmembuckets *)buf; 758 if (!nflag) 759 (void)printf("%s%s", string, equ); 760 printf("("); 761 printf("calls = %llu ", (long long)kb->kb_calls); 762 printf("total_allocated = %llu ", (long long)kb->kb_total); 763 printf("total_free = %lld ", (long long)kb->kb_totalfree); 764 printf("elements = %lld ", (long long)kb->kb_elmpercl); 765 printf("high watermark = %lld ", (long long)kb->kb_highwat); 766 printf("could_free = %lld", (long long)kb->kb_couldfree); 767 printf(")\n"); 768 return; 769 } 770 if (special & KMEMSTATS) { 771 struct kmemstats *km = (struct kmemstats *)buf; 772 int j, first = 1; 773 774 if (!nflag) 775 (void)printf("%s%s", string, equ); 776 (void)printf("(inuse = %ld, calls = %ld, memuse = %ldK, " 777 "limblocks = %d, mapblocks = %d, maxused = %ldK, " 778 "limit = %ldK, spare = %ld, sizes = (", 779 km->ks_inuse, km->ks_calls, 780 (km->ks_memuse + 1023) / 1024, km->ks_limblocks, 781 km->ks_mapblocks, (km->ks_maxused + 1023) / 1024, 782 (km->ks_limit + 1023) / 1024, km->ks_spare); 783 for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) { 784 if ((km->ks_size & j ) == 0) 785 continue; 786 if (first) 787 (void)printf("%d", j); 788 else 789 (void)printf(",%d", j); 790 first = 0; 791 } 792 if (first) 793 (void)printf("none"); 794 (void)printf("))\n"); 795 return; 796 } 797 if (special & CLOCK) { 798 struct clockinfo *clkp = (struct clockinfo *)buf; 799 800 if (!nflag) 801 (void)printf("%s%s", string, equ); 802 (void)printf( 803 "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n", 804 clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz); 805 return; 806 } 807 if (special & BOOTTIME) { 808 struct timeval *btp = (struct timeval *)buf; 809 time_t boottime; 810 811 if (!nflag) { 812 boottime = btp->tv_sec; 813 (void)printf("%s%s%s", string, equ, ctime(&boottime)); 814 } else 815 (void)printf("%ld\n", btp->tv_sec); 816 return; 817 } 818 if (special & BLKDEV) { 819 dev_t dev = *(dev_t *)buf; 820 821 if (!nflag) 822 (void)printf("%s%s%s\n", string, equ, 823 devname(dev, S_IFBLK)); 824 else 825 (void)printf("0x%x\n", dev); 826 return; 827 } 828 if (special & CHRDEV) { 829 dev_t dev = *(dev_t *)buf; 830 831 if (!nflag) 832 (void)printf("%s%s%s\n", string, equ, 833 devname(dev, S_IFCHR)); 834 else 835 (void)printf("0x%x\n", dev); 836 return; 837 } 838#ifdef CPU_BIOS 839 if (special & BIOSGEO) { 840 bios_diskinfo_t *pdi = (bios_diskinfo_t *)buf; 841 842 if (!nflag) 843 (void)printf("%s%s", string, equ); 844 (void)printf("bootdev = 0x%x, " 845 "cylinders = %u, heads = %u, sectors = %u\n", 846 pdi->bsd_dev, pdi->bios_cylinders, 847 pdi->bios_heads, pdi->bios_sectors); 848 return; 849 } 850 if (special & BIOSDEV) { 851 int dev = *(int*)buf; 852 853 if (!nflag) 854 (void)printf("%s%s", string, equ); 855 (void) printf("0x%02x\n", dev); 856 return; 857 } 858#endif 859 if (special & UNSIGNED) { 860 if (newsize == 0) { 861 if (!nflag) 862 (void)printf("%s%s", string, equ); 863 (void)printf("%u\n", *(u_int *)buf); 864 } else { 865 if (!qflag) { 866 if (!nflag) 867 (void)printf("%s: %u -> ", string, 868 *(u_int *)buf); 869 (void)printf("%u\n", *(u_int *)newval); 870 } 871 } 872 return; 873 } 874 if (special & RNDSTATS) { 875 struct rndstats *rndstats = (struct rndstats *)buf; 876 int i; 877 878 if (!nflag) 879 (void)printf("%s%s", string, equ); 880 (void)printf( 881 "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", 882 (unsigned long long)rndstats->rnd_total, 883 (unsigned long long)rndstats->rnd_used, 884 (unsigned long long)rndstats->rnd_reads, 885 (unsigned long long)rndstats->arc4_reads, 886 (unsigned long long)rndstats->arc4_nstirs, 887 (unsigned long long)rndstats->arc4_stirs, 888 (unsigned long long)rndstats->rnd_pad[0], 889 (unsigned long long)rndstats->rnd_pad[1], 890 (unsigned long long)rndstats->rnd_pad[2], 891 (unsigned long long)rndstats->rnd_pad[3], 892 (unsigned long long)rndstats->rnd_pad[4], 893 (unsigned long long)rndstats->rnd_waits, 894 (unsigned long long)rndstats->rnd_enqs, 895 (unsigned long long)rndstats->rnd_deqs, 896 (unsigned long long)rndstats->rnd_drops, 897 (unsigned long long)rndstats->rnd_drople); 898 for (i = 0; i < sizeof(rndstats->rnd_ed)/sizeof(rndstats->rnd_ed[0]); 899 i++) 900 (void)printf(" %llu", (unsigned long long)rndstats->rnd_ed[i]); 901 for (i = 0; i < sizeof(rndstats->rnd_sc)/sizeof(rndstats->rnd_sc[0]); 902 i++) 903 (void)printf(" %llu", (unsigned long long)rndstats->rnd_sc[i]); 904 for (i = 0; i < sizeof(rndstats->rnd_sb)/sizeof(rndstats->rnd_sb[0]); 905 i++) 906 (void)printf(" %llu", (unsigned long long)rndstats->rnd_sb[i]); 907 printf("\n"); 908 return; 909 } 910 if (special & BADDYNAMIC) { 911 in_port_t port, lastport; 912 u_int32_t *baddynamic = (u_int32_t *)buf; 913 914 if (!qflag) { 915 if (!nflag) 916 (void)printf("%s%s", string, 917 newsize ? ": " : equ); 918 lastport = 0; 919 for (port = IPPORT_RESERVED/2; port < IPPORT_RESERVED; 920 port++) 921 if (DP_ISSET(baddynamic, port)) { 922 (void)printf("%s%hd", 923 lastport ? "," : "", port); 924 lastport = port; 925 } 926 if (newsize != 0) { 927 if (!nflag) 928 fputs(" -> ", stdout); 929 baddynamic = (u_int32_t *)newval; 930 lastport = 0; 931 for (port = IPPORT_RESERVED/2; 932 port < IPPORT_RESERVED; port++) 933 if (DP_ISSET(baddynamic, port)) { 934 (void)printf("%s%hd", 935 lastport ? "," : "", port); 936 lastport = port; 937 } 938 } 939 (void)putchar('\n'); 940 } 941 return; 942 } 943 if (special & LONGARRAY) { 944 long *la = (long *)buf; 945 if (!nflag) 946 printf("%s%s", string, equ); 947 while (lal--) 948 printf("%ld%s", *la++, lal? ",":""); 949 putchar('\n'); 950 return; 951 } 952 if (special & SENSORS) { 953 struct sensor *s = (struct sensor *)buf; 954 955 if (size > 0 && (s->flags & SENSOR_FINVALID) == 0) { 956 if (!nflag) 957 printf("%s%s", string, equ); 958 print_sensor(s); 959 printf("\n"); 960 } 961 return; 962 } 963#ifdef CPU_ZTSSCALE 964 if (special & ZTSSCALE) { 965 struct ztsscale *tsp; 966 967 if (newsize == 0) { 968 if (!nflag) 969 (void)printf("%s%s", string, equ); 970 tsp = (struct ztsscale *)buf; 971 (void)printf("%d,%d,%d,%d\n", tsp->ts_minx, 972 tsp->ts_maxx, tsp->ts_miny, tsp->ts_maxy); 973 } else { 974 if (!qflag) { 975 if (!nflag) { 976 tsp = (struct ztsscale *)buf; 977 (void)printf("%s: %d,%d,%d,%d -> ", 978 string, tsp->ts_minx, tsp->ts_maxx, 979 tsp->ts_miny, tsp->ts_maxy); 980 } 981 tsp = (struct ztsscale *)newval; 982 (void)printf("%d,%d,%d,%d\n", tsp->ts_minx, 983 tsp->ts_maxx, tsp->ts_miny, tsp->ts_maxy); 984 } 985 } 986 return; 987 } 988#endif 989 switch (type) { 990 case CTLTYPE_INT: 991 if (newsize == 0) { 992 if (!nflag) 993 (void)printf("%s%s", string, equ); 994 (void)printf("%d\n", *(int *)buf); 995 } else { 996 if (!qflag) { 997 if (!nflag) 998 (void)printf("%s: %d -> ", string, 999 *(int *)buf); 1000 (void)printf("%d\n", *(int *)newval); 1001 } 1002 } 1003 return; 1004 1005 case CTLTYPE_STRING: 1006 if (newval == NULL) { 1007 if (!nflag) 1008 (void)printf("%s%s", string, equ); 1009 (void)puts(buf); 1010 } else { 1011 if (!qflag) { 1012 if (!nflag) 1013 (void)printf("%s: %s -> ", string, buf); 1014 (void)puts((char *)newval); 1015 } 1016 } 1017 return; 1018 1019 case CTLTYPE_QUAD: 1020 if (newsize == 0) { 1021 long long tmp = *(quad_t *)buf; 1022 1023 if (!nflag) 1024 (void)printf("%s%s", string, equ); 1025 (void)printf("%lld\n", tmp); 1026 } else { 1027 long long tmp = *(quad_t *)buf; 1028 1029 if (!qflag) { 1030 if (!nflag) 1031 (void)printf("%s: %lld -> ", 1032 string, tmp); 1033 tmp = *(quad_t *)newval; 1034 (void)printf("%qd\n", tmp); 1035 } 1036 } 1037 return; 1038 1039 case CTLTYPE_STRUCT: 1040 warnx("%s: unknown structure returned", string); 1041 return; 1042 1043 default: 1044 case CTLTYPE_NODE: 1045 warnx("%s: unknown type returned", string); 1046 return; 1047 } 1048} 1049 1050void 1051parse_baddynamic(int mib[], size_t len, char *string, void **newvalp, 1052 size_t *newsizep, int flags, int nflag) 1053{ 1054 static u_int32_t newbaddynamic[DP_MAPSIZE]; 1055 in_port_t port; 1056 size_t size; 1057 char action, *cp; 1058 1059 if (strchr((char *)*newvalp, '+') || strchr((char *)*newvalp, '-')) { 1060 size = sizeof(newbaddynamic); 1061 if (sysctl(mib, len, newbaddynamic, &size, 0, 0) == -1) { 1062 if (flags == 0) 1063 return; 1064 if (!nflag) 1065 (void)printf("%s: ", string); 1066 (void)puts("kernel does contain bad dynamic port tables"); 1067 return; 1068 } 1069 1070 while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) { 1071 if (*cp != '+' && *cp != '-') 1072 errx(1, "cannot mix +/- with full list"); 1073 action = *cp++; 1074 port = atoi(cp); 1075 if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED) 1076 errx(1, "invalid port, range is %d to %d", 1077 IPPORT_RESERVED/2, IPPORT_RESERVED-1); 1078 if (action == '+') 1079 DP_SET(newbaddynamic, port); 1080 else 1081 DP_CLR(newbaddynamic, port); 1082 } 1083 } else { 1084 (void)memset((void *)newbaddynamic, 0, sizeof(newbaddynamic)); 1085 while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) { 1086 port = atoi(cp); 1087 if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED) 1088 errx(1, "invalid port, range is %d to %d", 1089 IPPORT_RESERVED/2, IPPORT_RESERVED-1); 1090 DP_SET(newbaddynamic, port); 1091 } 1092 } 1093 1094 *newvalp = (void *)newbaddynamic; 1095 *newsizep = sizeof(newbaddynamic); 1096} 1097 1098/* 1099 * Initialize the set of debugging names 1100 */ 1101void 1102debuginit(void) 1103{ 1104 int mib[3], loc, i; 1105 size_t size; 1106 1107 if (secondlevel[CTL_DEBUG].list != 0) 1108 return; 1109 secondlevel[CTL_DEBUG].list = debugname; 1110 mib[0] = CTL_DEBUG; 1111 mib[2] = CTL_DEBUG_NAME; 1112 for (loc = lastused, i = 0; i < CTL_DEBUG_MAXID; i++) { 1113 mib[1] = i; 1114 size = BUFSIZ - loc; 1115 if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1) 1116 continue; 1117 debugname[i].ctl_name = &names[loc]; 1118 debugname[i].ctl_type = CTLTYPE_INT; 1119 loc += size; 1120 } 1121 lastused = loc; 1122} 1123 1124struct ctlname vfsgennames[] = CTL_VFSGENCTL_NAMES; 1125struct ctlname ffsname[] = FFS_NAMES; 1126struct ctlname nfsname[] = FS_NFS_NAMES; 1127struct list *vfsvars; 1128int *vfs_typenums; 1129 1130/* 1131 * Initialize the set of filesystem names 1132 */ 1133void 1134vfsinit(void) 1135{ 1136 int mib[4], maxtypenum, cnt, loc, size; 1137 struct vfsconf vfc; 1138 size_t buflen; 1139 1140 if (secondlevel[CTL_VFS].list != 0) 1141 return; 1142 mib[0] = CTL_VFS; 1143 mib[1] = VFS_GENERIC; 1144 mib[2] = VFS_MAXTYPENUM; 1145 buflen = 4; 1146 if (sysctl(mib, 3, &maxtypenum, &buflen, (void *)0, (size_t)0) < 0) 1147 return; 1148 maxtypenum++; /* + generic */ 1149 if ((vfs_typenums = calloc(maxtypenum, sizeof(int))) == NULL) 1150 return; 1151 if ((vfsvars = calloc(maxtypenum, sizeof(*vfsvars))) == NULL) { 1152 free(vfs_typenums); 1153 return; 1154 } 1155 if ((vfsname = calloc(maxtypenum, sizeof(*vfsname))) == NULL) { 1156 free(vfs_typenums); 1157 free(vfsvars); 1158 return; 1159 } 1160 mib[2] = VFS_CONF; 1161 buflen = sizeof vfc; 1162 for (loc = lastused, cnt = 1; cnt < maxtypenum; cnt++) { 1163 mib[3] = cnt - 1; 1164 if (sysctl(mib, 4, &vfc, &buflen, (void *)0, (size_t)0) < 0) { 1165 if (errno == EOPNOTSUPP) 1166 continue; 1167 warn("vfsinit"); 1168 free(vfsname); 1169 free(vfsvars); 1170 free(vfs_typenums); 1171 return; 1172 } 1173 if (!strcmp(vfc.vfc_name, MOUNT_FFS)) { 1174 vfsvars[cnt].list = ffsname; 1175 vfsvars[cnt].size = FFS_MAXID; 1176 } 1177 if (!strcmp(vfc.vfc_name, MOUNT_NFS)) { 1178 vfsvars[cnt].list = nfsname; 1179 vfsvars[cnt].size = NFS_MAXID; 1180 } 1181 vfs_typenums[cnt] = vfc.vfc_typenum; 1182 strlcat(&names[loc], vfc.vfc_name, sizeof names - loc); 1183 vfsname[cnt].ctl_name = &names[loc]; 1184 vfsname[cnt].ctl_type = CTLTYPE_NODE; 1185 size = strlen(vfc.vfc_name) + 1; 1186 loc += size; 1187 } 1188 lastused = loc; 1189 1190 vfsname[0].ctl_name = "mounts"; 1191 vfsname[0].ctl_type = CTLTYPE_NODE; 1192 vfsvars[0].list = vfsname + 1; 1193 vfsvars[0].size = maxtypenum - 1; 1194 1195 secondlevel[CTL_VFS].list = vfsname; 1196 secondlevel[CTL_VFS].size = maxtypenum; 1197 return; 1198} 1199 1200int 1201sysctl_vfsgen(char *string, char **bufpp, int mib[], int flags, int *typep) 1202{ 1203 int indx; 1204 size_t size; 1205 struct vfsconf vfc; 1206 1207 if (*bufpp == NULL) { 1208 listall(string, vfsvars); 1209 return (-1); 1210 } 1211 1212 if ((indx = findname(string, "third", bufpp, vfsvars)) == -1) 1213 return (-1); 1214 1215 mib[1] = VFS_GENERIC; 1216 mib[2] = VFS_CONF; 1217 mib[3] = indx; 1218 size = sizeof vfc; 1219 if (sysctl(mib, 4, &vfc, &size, (void *)0, (size_t)0) < 0) { 1220 if (errno != EOPNOTSUPP) 1221 warn("vfs print"); 1222 return -1; 1223 } 1224 if (flags == 0 && vfc.vfc_refcount == 0) 1225 return -1; 1226 if (!nflag) 1227 fprintf(stdout, "%s has %d mounted instance%s\n", 1228 string, vfc.vfc_refcount, 1229 vfc.vfc_refcount != 1 ? "s" : ""); 1230 else 1231 fprintf(stdout, "%d\n", vfc.vfc_refcount); 1232 1233 return -1; 1234} 1235 1236int 1237sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep) 1238{ 1239 struct list *lp = &vfsvars[mib[1]]; 1240 int indx; 1241 1242 if (lp->list == NULL) { 1243 if (flags) 1244 warnx("No variables defined for file system %s", string); 1245 return (-1); 1246 } 1247 if (*bufpp == NULL) { 1248 listall(string, lp); 1249 return (-1); 1250 } 1251 if ((indx = findname(string, "third", bufpp, lp)) == -1) 1252 return (-1); 1253 1254 mib[1] = vfs_typenums[mib[1]]; 1255 mib[2] = indx; 1256 *typep = lp->list[indx].ctl_type; 1257 return (3); 1258} 1259 1260struct ctlname posixname[] = CTL_FS_POSIX_NAMES; 1261struct list fslist = { posixname, FS_POSIX_MAXID }; 1262 1263/* 1264 * handle file system requests 1265 */ 1266int 1267sysctl_fs(char *string, char **bufpp, int mib[], int flags, int *typep) 1268{ 1269 int indx; 1270 1271 if (*bufpp == NULL) { 1272 listall(string, &fslist); 1273 return (-1); 1274 } 1275 if ((indx = findname(string, "third", bufpp, &fslist)) == -1) 1276 return (-1); 1277 mib[2] = indx; 1278 *typep = fslist.list[indx].ctl_type; 1279 return (3); 1280} 1281 1282#ifdef CPU_BIOS 1283struct ctlname biosname[] = CTL_BIOS_NAMES; 1284struct list bioslist = { biosname, BIOS_MAXID }; 1285 1286/* 1287 * handle BIOS requests 1288 */ 1289int 1290sysctl_bios(char *string, char **bufpp, int mib[], int flags, int *typep) 1291{ 1292 char *name; 1293 int indx; 1294 1295 if (*bufpp == NULL) { 1296 listall(string, &bioslist); 1297 return (-1); 1298 } 1299 if ((indx = findname(string, "third", bufpp, &bioslist)) == -1) 1300 return (-1); 1301 mib[2] = indx; 1302 if (indx == BIOS_DISKINFO) { 1303 if (*bufpp == NULL) { 1304 char name[BUFSIZ]; 1305 1306 /* scan all the bios devices */ 1307 for (indx = 0; indx < 256; indx++) { 1308 snprintf(name, sizeof(name), "%s.%u", 1309 string, indx); 1310 parse(name, 1); 1311 } 1312 return (-1); 1313 } 1314 if ((name = strsep(bufpp, ".")) == NULL) { 1315 warnx("%s: incomplete specification", string); 1316 return (-1); 1317 } 1318 mib[3] = atoi(name); 1319 *typep = CTLTYPE_STRUCT; 1320 return (4); 1321 } else { 1322 *typep = bioslist.list[indx].ctl_type; 1323 return (3); 1324 } 1325} 1326#endif 1327 1328struct ctlname swpencname[] = CTL_SWPENC_NAMES; 1329struct list swpenclist = { swpencname, SWPENC_MAXID }; 1330 1331/* 1332 * handle swap encrypt requests 1333 */ 1334int 1335sysctl_swpenc(char *string, char **bufpp, int mib[], int flags, int *typep) 1336{ 1337 int indx; 1338 1339 if (*bufpp == NULL) { 1340 listall(string, &swpenclist); 1341 return (-1); 1342 } 1343 if ((indx = findname(string, "third", bufpp, &swpenclist)) == -1) 1344 return (-1); 1345 mib[2] = indx; 1346 *typep = swpenclist.list[indx].ctl_type; 1347 return (3); 1348} 1349 1350struct ctlname inetname[] = CTL_IPPROTO_NAMES; 1351struct ctlname ipname[] = IPCTL_NAMES; 1352struct ctlname icmpname[] = ICMPCTL_NAMES; 1353struct ctlname ipipname[] = IPIPCTL_NAMES; 1354struct ctlname tcpname[] = TCPCTL_NAMES; 1355struct ctlname udpname[] = UDPCTL_NAMES; 1356struct ctlname espname[] = ESPCTL_NAMES; 1357struct ctlname ahname[] = AHCTL_NAMES; 1358struct ctlname etheripname[] = ETHERIPCTL_NAMES; 1359struct ctlname grename[] = GRECTL_NAMES; 1360struct ctlname mobileipname[] = MOBILEIPCTL_NAMES; 1361struct ctlname ipcompname[] = IPCOMPCTL_NAMES; 1362struct ctlname carpname[] = CARPCTL_NAMES; 1363struct ctlname bpfname[] = CTL_NET_BPF_NAMES; 1364struct ctlname ifqname[] = CTL_IFQ_NAMES; 1365struct list inetlist = { inetname, IPPROTO_MAXID }; 1366struct list inetvars[] = { 1367 { ipname, IPCTL_MAXID }, /* ip */ 1368 { icmpname, ICMPCTL_MAXID }, /* icmp */ 1369 { 0, 0 }, /* igmp */ 1370 { 0, 0 }, /* ggmp */ 1371 { ipipname, IPIPCTL_MAXID }, /* ipencap */ 1372 { 0, 0 }, 1373 { tcpname, TCPCTL_MAXID }, /* tcp */ 1374 { 0, 0 }, 1375 { 0, 0 }, /* egp */ 1376 { 0, 0 }, 1377 { 0, 0 }, 1378 { 0, 0 }, 1379 { 0, 0 }, /* pup */ 1380 { 0, 0 }, 1381 { 0, 0 }, 1382 { 0, 0 }, 1383 { 0, 0 }, 1384 { udpname, UDPCTL_MAXID }, /* udp */ 1385 { 0, 0 }, 1386 { 0, 0 }, 1387 { 0, 0 }, 1388 { 0, 0 }, 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 { 0, 0 }, 1400 { 0, 0 }, 1401 { 0, 0 }, 1402 { 0, 0 }, 1403 { 0, 0 }, 1404 { 0, 0 }, 1405 { 0, 0 }, 1406 { 0, 0 }, 1407 { 0, 0 }, 1408 { 0, 0 }, 1409 { 0, 0 }, 1410 { 0, 0 }, 1411 { 0, 0 }, 1412 { 0, 0 }, 1413 { 0, 0 }, 1414 { grename, GRECTL_MAXID }, /* GRE */ 1415 { 0, 0 }, 1416 { 0, 0 }, 1417 { espname, ESPCTL_MAXID }, /* esp */ 1418 { ahname, AHCTL_MAXID }, /* ah */ 1419 { 0, 0 }, 1420 { 0, 0 }, 1421 { 0, 0 }, 1422 { mobileipname, MOBILEIPCTL_MAXID }, /* mobileip */ 1423 { 0, 0 }, 1424 { 0, 0 }, 1425 { 0, 0 }, 1426 { 0, 0 }, 1427 { 0, 0 }, 1428 { 0, 0 }, 1429 { 0, 0 }, 1430 { 0, 0 }, 1431 { 0, 0 }, 1432 { 0, 0 }, 1433 { 0, 0 }, 1434 { 0, 0 }, 1435 { 0, 0 }, 1436 { 0, 0 }, 1437 { 0, 0 }, 1438 { 0, 0 }, 1439 { 0, 0 }, 1440 { 0, 0 }, 1441 { 0, 0 }, 1442 { 0, 0 }, 1443 { 0, 0 }, 1444 { 0, 0 }, 1445 { 0, 0 }, 1446 { 0, 0 }, 1447 { 0, 0 }, 1448 { 0, 0 }, 1449 { 0, 0 }, 1450 { 0, 0 }, 1451 { 0, 0 }, 1452 { 0, 0 }, 1453 { 0, 0 }, 1454 { 0, 0 }, 1455 { 0, 0 }, 1456 { 0, 0 }, 1457 { 0, 0 }, 1458 { 0, 0 }, 1459 { 0, 0 }, 1460 { 0, 0 }, 1461 { 0, 0 }, 1462 { 0, 0 }, 1463 { 0, 0 }, 1464 { etheripname, ETHERIPCTL_MAXID }, 1465 { 0, 0 }, 1466 { 0, 0 }, 1467 { 0, 0 }, 1468 { 0, 0 }, 1469 { 0, 0 }, 1470 { 0, 0 }, 1471 { 0, 0 }, 1472 { 0, 0 }, 1473 { 0, 0 }, 1474 { 0, 0 }, 1475 { ipcompname, IPCOMPCTL_MAXID }, 1476 { 0, 0 }, 1477 { 0, 0 }, 1478 { 0, 0 }, 1479 { carpname, CARPCTL_MAXID }, 1480}; 1481struct list bpflist = { bpfname, NET_BPF_MAXID }; 1482struct list ifqlist = { ifqname, IFQCTL_MAXID }; 1483 1484struct list kernmalloclist = { kernmallocname, KERN_MALLOC_MAXID }; 1485struct list forkstatlist = { forkstatname, KERN_FORKSTAT_MAXID }; 1486struct list nchstatslist = { nchstatsname, KERN_NCHSTATS_MAXID }; 1487struct list ttylist = { ttysname, KERN_TTY_MAXID }; 1488struct list semlist = { semname, KERN_SEMINFO_MAXID }; 1489struct list shmlist = { shmname, KERN_SHMINFO_MAXID }; 1490struct list watchdoglist = { watchdogname, KERN_WATCHDOG_MAXID }; 1491struct list tclist = { tcname, KERN_TIMECOUNTER_MAXID }; 1492 1493/* 1494 * handle vfs namei cache statistics 1495 */ 1496int 1497sysctl_nchstats(char *string, char **bufpp, int mib[], int flags, int *typep) 1498{ 1499 static struct nchstats nch; 1500 int indx; 1501 size_t size; 1502 static int keepvalue = 0; 1503 1504 if (*bufpp == NULL) { 1505 bzero(&nch, sizeof(struct nchstats)); 1506 listall(string, &nchstatslist); 1507 return (-1); 1508 } 1509 if ((indx = findname(string, "third", bufpp, &nchstatslist)) == -1) 1510 return (-1); 1511 mib[2] = indx; 1512 if (*bufpp != NULL) { 1513 warnx("fourth level name in %s is invalid", string); 1514 return (-1); 1515 } 1516 if (keepvalue == 0) { 1517 size = sizeof(struct nchstats); 1518 if (sysctl(mib, 2, &nch, &size, NULL, 0) < 0) 1519 return (-1); 1520 keepvalue = 1; 1521 } 1522 if (!nflag) 1523 (void)printf("%s%s", string, equ); 1524 switch (indx) { 1525 case KERN_NCHSTATS_GOODHITS: 1526 (void)printf("%ld\n", nch.ncs_goodhits); 1527 break; 1528 case KERN_NCHSTATS_NEGHITS: 1529 (void)printf("%ld\n", nch.ncs_neghits); 1530 break; 1531 case KERN_NCHSTATS_BADHITS: 1532 (void)printf("%ld\n", nch.ncs_badhits); 1533 break; 1534 case KERN_NCHSTATS_FALSEHITS: 1535 (void)printf("%ld\n", nch.ncs_falsehits); 1536 break; 1537 case KERN_NCHSTATS_MISS: 1538 (void)printf("%ld\n", nch.ncs_miss); 1539 break; 1540 case KERN_NCHSTATS_LONG: 1541 (void)printf("%ld\n", nch.ncs_long); 1542 break; 1543 case KERN_NCHSTATS_PASS2: 1544 (void)printf("%ld\n", nch.ncs_pass2); 1545 break; 1546 case KERN_NCHSTATS_2PASSES: 1547 (void)printf("%ld\n", nch.ncs_2passes); 1548 break; 1549 case KERN_NCHSTATS_REVHITS: 1550 (void)printf("%ld\n", nch.ncs_revhits); 1551 break; 1552 case KERN_NCHSTATS_REVMISS: 1553 (void)printf("%ld\n", nch.ncs_revmiss); 1554 break; 1555 } 1556 return (-1); 1557} 1558 1559/* 1560 * handle tty statistics 1561 */ 1562int 1563sysctl_tty(char *string, char **bufpp, int mib[], int flags, int *typep) 1564{ 1565 int indx; 1566 1567 if (*bufpp == NULL) { 1568 listall(string, &ttylist); 1569 return (-1); 1570 } 1571 if ((indx = findname(string, "third", bufpp, &ttylist)) == -1) 1572 return (-1); 1573 mib[2] = indx; 1574 1575 if ((*typep = ttylist.list[indx].ctl_type) == CTLTYPE_STRUCT) { 1576 if (flags) 1577 warnx("use pstat -t to view %s information", 1578 string); 1579 return (-1); 1580 } 1581 return (3); 1582} 1583 1584/* 1585 * handle fork statistics 1586 */ 1587int 1588sysctl_forkstat(char *string, char **bufpp, int mib[], int flags, int *typep) 1589{ 1590 static struct forkstat fks; 1591 static int keepvalue = 0; 1592 int indx; 1593 size_t size; 1594 1595 if (*bufpp == NULL) { 1596 bzero(&fks, sizeof(struct forkstat)); 1597 listall(string, &forkstatlist); 1598 return (-1); 1599 } 1600 if ((indx = findname(string, "third", bufpp, &forkstatlist)) == -1) 1601 return (-1); 1602 if (*bufpp != NULL) { 1603 warnx("fourth level name in %s is invalid", string); 1604 return (-1); 1605 } 1606 if (keepvalue == 0) { 1607 size = sizeof(struct forkstat); 1608 if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0) 1609 return (-1); 1610 keepvalue = 1; 1611 } 1612 if (!nflag) 1613 (void)printf("%s%s", string, equ); 1614 switch (indx) { 1615 case KERN_FORKSTAT_FORK: 1616 (void)printf("%d\n", fks.cntfork); 1617 break; 1618 case KERN_FORKSTAT_VFORK: 1619 (void)printf("%d\n", fks.cntvfork); 1620 break; 1621 case KERN_FORKSTAT_RFORK: 1622 (void)printf("%d\n", fks.cntrfork); 1623 break; 1624 case KERN_FORKSTAT_KTHREAD: 1625 (void)printf("%d\n", fks.cntkthread); 1626 break; 1627 case KERN_FORKSTAT_SIZFORK: 1628 (void)printf("%d\n", fks.sizfork); 1629 break; 1630 case KERN_FORKSTAT_SIZVFORK: 1631 (void)printf("%d\n", fks.sizvfork); 1632 break; 1633 case KERN_FORKSTAT_SIZRFORK: 1634 (void)printf("%d\n", fks.sizrfork); 1635 break; 1636 case KERN_FORKSTAT_SIZKTHREAD: 1637 (void)printf("%d\n", fks.sizkthread); 1638 break; 1639 } 1640 return (-1); 1641} 1642 1643/* 1644 * handle malloc statistics 1645 */ 1646int 1647sysctl_malloc(char *string, char **bufpp, int mib[], int flags, int *typep) 1648{ 1649 int indx, stor, i; 1650 char *name, bufp[BUFSIZ], *buf, *ptr; 1651 struct list lp; 1652 size_t size; 1653 1654 if (*bufpp == NULL) { 1655 listall(string, &kernmalloclist); 1656 return (-1); 1657 } 1658 if ((indx = findname(string, "third", bufpp, &kernmalloclist)) == -1) 1659 return (-1); 1660 mib[2] = indx; 1661 if (mib[2] == KERN_MALLOC_BUCKET) { 1662 if ((name = strsep(bufpp, ".")) == NULL) { 1663 size = BUFSIZ; 1664 stor = mib[2]; 1665 mib[2] = KERN_MALLOC_BUCKETS; 1666 buf = bufp; 1667 if (sysctl(mib, 3, buf, &size, NULL, 0) < 0) 1668 return (-1); 1669 mib[2] = stor; 1670 for (stor = 0, i = 0; i < size; i++) 1671 if (buf[i] == ',') 1672 stor++; 1673 lp.list = calloc(stor + 2, sizeof(struct ctlname)); 1674 if (lp.list == NULL) 1675 return (-1); 1676 lp.size = stor + 2; 1677 for (i = 1; 1678 (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL; 1679 i++) { 1680 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1681 } 1682 lp.list[i].ctl_name = buf; 1683 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1684 listall(string, &lp); 1685 free(lp.list); 1686 return (-1); 1687 } 1688 mib[3] = atoi(name); 1689 return (4); 1690 } else if (mib[2] == KERN_MALLOC_BUCKETS) { 1691 *typep = CTLTYPE_STRING; 1692 return (3); 1693 } else if (mib[2] == KERN_MALLOC_KMEMSTATS) { 1694 size = BUFSIZ; 1695 stor = mib[2]; 1696 mib[2] = KERN_MALLOC_KMEMNAMES; 1697 buf = bufp; 1698 if (sysctl(mib, 3, buf, &size, NULL, 0) < 0) 1699 return (-1); 1700 mib[2] = stor; 1701 if ((name = strsep(bufpp, ".")) == NULL) { 1702 for (stor = 0, i = 0; i < size; i++) 1703 if (buf[i] == ',') 1704 stor++; 1705 lp.list = calloc(stor + 2, sizeof(struct ctlname)); 1706 if (lp.list == NULL) 1707 return (-1); 1708 lp.size = stor + 2; 1709 for (i = 1; 1710 (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL; i++) { 1711 if (lp.list[i].ctl_name[0] == '\0') { 1712 i--; 1713 continue; 1714 } 1715 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1716 } 1717 lp.list[i].ctl_name = buf; 1718 lp.list[i].ctl_type = CTLTYPE_STRUCT; 1719 listall(string, &lp); 1720 free(lp.list); 1721 return (-1); 1722 } 1723 ptr = strstr(buf, name); 1724 tryagain: 1725 if (ptr == NULL) { 1726 warnx("fourth level name %s in %s is invalid", name, 1727 string); 1728 return (-1); 1729 } 1730 if ((*(ptr + strlen(name)) != ',') && 1731 (*(ptr + strlen(name)) != '\0')) { 1732 ptr = strstr(ptr + 1, name); /* retry */ 1733 goto tryagain; 1734 } 1735 if ((ptr != buf) && (*(ptr - 1) != ',')) { 1736 ptr = strstr(ptr + 1, name); /* retry */ 1737 goto tryagain; 1738 } 1739 for (i = 0, stor = 0; buf + i < ptr; i++) 1740 if (buf[i] == ',') 1741 stor++; 1742 mib[3] = stor; 1743 return (4); 1744 } else if (mib[2] == KERN_MALLOC_KMEMNAMES) { 1745 *typep = CTLTYPE_STRING; 1746 return (3); 1747 } 1748 return (-1); 1749} 1750 1751#ifdef CPU_CHIPSET 1752/* 1753 * handle machdep.chipset requests 1754 */ 1755struct ctlname chipsetname[] = CTL_CHIPSET_NAMES; 1756struct list chipsetlist = { chipsetname, CPU_CHIPSET_MAXID }; 1757 1758int 1759sysctl_chipset(char *string, char **bufpp, int mib[], int flags, int *typep) 1760{ 1761 int indx, bwx; 1762 static void *q; 1763 size_t len; 1764 char *p; 1765 1766 if (*bufpp == NULL) { 1767 listall(string, &chipsetlist); 1768 return (-1); 1769 } 1770 if ((indx = findname(string, "third", bufpp, &chipsetlist)) == -1) 1771 return (-1); 1772 mib[2] = indx; 1773 if (!nflag) 1774 printf("%s%s", string, equ); 1775 switch(mib[2]) { 1776 case CPU_CHIPSET_MEM: 1777 case CPU_CHIPSET_DENSE: 1778 case CPU_CHIPSET_PORTS: 1779 case CPU_CHIPSET_HAE_MASK: 1780 len = sizeof(void *); 1781 if (sysctl(mib, 3, &q, &len, NULL, 0) < 0) 1782 return (-1); 1783 printf("%p\n", q); 1784 break; 1785 case CPU_CHIPSET_BWX: 1786 len = sizeof(int); 1787 if (sysctl(mib, 3, &bwx, &len, NULL, 0) < 0) 1788 return (-1); 1789 printf("%d\n", bwx); 1790 break; 1791 case CPU_CHIPSET_TYPE: 1792 if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0) 1793 return (-1); 1794 p = malloc(len + 1); 1795 if (p == NULL) 1796 return (-1); 1797 if (sysctl(mib, 3, p, &len, NULL, 0) < 0) { 1798 free(p); 1799 return (-1); 1800 } 1801 p[len] = '\0'; 1802 printf("%s\n", p); 1803 free(p); 1804 break; 1805 } 1806 return (-1); 1807} 1808#endif 1809/* 1810 * handle internet requests 1811 */ 1812int 1813sysctl_inet(char *string, char **bufpp, int mib[], int flags, int *typep) 1814{ 1815 struct list *lp; 1816 int indx; 1817 1818 if (*bufpp == NULL) { 1819 listall(string, &inetlist); 1820 return (-1); 1821 } 1822 if ((indx = findname(string, "third", bufpp, &inetlist)) == -1) 1823 return (-1); 1824 mib[2] = indx; 1825 if (indx < IPPROTO_MAXID && inetvars[indx].list != NULL) 1826 lp = &inetvars[indx]; 1827 else if (!flags) 1828 return (-1); 1829 else { 1830 warnx("%s: no variables defined for this protocol", string); 1831 return (-1); 1832 } 1833 if (*bufpp == NULL) { 1834 listall(string, lp); 1835 return (-1); 1836 } 1837 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1838 return (-1); 1839 mib[3] = indx; 1840 *typep = lp->list[indx].ctl_type; 1841 if (*typep == CTLTYPE_NODE) { 1842 int tindx; 1843 1844 if (*bufpp == 0) { 1845 listall(string, &ifqlist); 1846 return(-1); 1847 } 1848 lp = &ifqlist; 1849 if ((tindx = findname(string, "fifth", bufpp, lp)) == -1) 1850 return (-1); 1851 mib[4] = tindx; 1852 *typep = lp->list[tindx].ctl_type; 1853 return(5); 1854 } 1855 return (4); 1856} 1857 1858#ifdef INET6 1859struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES; 1860struct ctlname ip6name[] = IPV6CTL_NAMES; 1861struct ctlname icmp6name[] = ICMPV6CTL_NAMES; 1862struct ctlname pim6name[] = PIM6CTL_NAMES; 1863struct list inet6list = { inet6name, IPV6PROTO_MAXID }; 1864struct list inet6vars[] = { 1865/*0*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1866 { 0, 0 }, 1867 { 0, 0 }, 1868 { 0, 0 }, 1869 { 0, 0 }, 1870 { 0, 0 }, 1871/*10*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1872 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1873/*20*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1874 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1875/*30*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1876 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1877/*40*/ { 0, 0 }, 1878 { ip6name, IPV6CTL_MAXID }, /* ipv6 */ 1879 { 0, 0 }, 1880 { 0, 0 }, 1881 { 0, 0 }, 1882 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1883/*50*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1884 { 0, 0 }, 1885 { 0, 0 }, 1886 { 0, 0 }, 1887 { icmp6name, ICMPV6CTL_MAXID }, /* icmp6 */ 1888 { 0, 0 }, 1889/*60*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1890 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1891/*70*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1892 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1893/*80*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1894 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1895/*90*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1896 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1897/*100*/ { 0, 0 }, 1898 { 0, 0 }, 1899 { 0, 0 }, 1900 { pim6name, PIM6CTL_MAXID }, /* pim6 */ 1901}; 1902 1903/* 1904 * handle internet6 requests 1905 */ 1906int 1907sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep) 1908{ 1909 struct list *lp; 1910 int indx; 1911 1912 if (*bufpp == NULL) { 1913 listall(string, &inet6list); 1914 return (-1); 1915 } 1916 if ((indx = findname(string, "third", bufpp, &inet6list)) == -1) 1917 return (-1); 1918 mib[2] = indx; 1919 if (indx < IPV6PROTO_MAXID && inet6vars[indx].list != NULL) 1920 lp = &inet6vars[indx]; 1921 else if (!flags) 1922 return (-1); 1923 else { 1924 warnx("%s: no variables defined for this protocol", string); 1925 return (-1); 1926 } 1927 if (*bufpp == NULL) { 1928 listall(string, lp); 1929 return (-1); 1930 } 1931 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1932 return (-1); 1933 mib[3] = indx; 1934 *typep = lp->list[indx].ctl_type; 1935 return (4); 1936} 1937#endif 1938 1939struct ctlname ipxname[] = CTL_IPXPROTO_NAMES; 1940struct ctlname ipxpname[] = IPXCTL_NAMES; 1941struct ctlname spxpname[] = SPXCTL_NAMES; 1942struct list ipxlist = { ipxname, IPXCTL_MAXID }; 1943struct list ipxvars[] = { 1944 { ipxpname, IPXCTL_MAXID }, /* ipx */ 1945 { 0, 0 }, 1946 { 0, 0 }, 1947 { 0, 0 }, 1948 { 0, 0 }, 1949 { spxpname, SPXCTL_MAXID }, 1950}; 1951 1952/* 1953 * Handle internet requests 1954 */ 1955int 1956sysctl_ipx(char *string, char **bufpp, int mib[], int flags, int *typep) 1957{ 1958 struct list *lp; 1959 int indx; 1960 1961 if (*bufpp == NULL) { 1962 listall(string, &ipxlist); 1963 return (-1); 1964 } 1965 if ((indx = findname(string, "third", bufpp, &ipxlist)) == -1) 1966 return (-1); 1967 mib[2] = indx; 1968 if (indx <= IPXPROTO_SPX && ipxvars[indx].list != NULL) 1969 lp = &ipxvars[indx]; 1970 else if (!flags) 1971 return (-1); 1972 else { 1973 warnx("%s: no variables defined for this protocol", string); 1974 return (-1); 1975 } 1976 if (*bufpp == NULL) { 1977 listall(string, lp); 1978 return (-1); 1979 } 1980 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1981 return (-1); 1982 mib[3] = indx; 1983 *typep = lp->list[indx].ctl_type; 1984 return (4); 1985} 1986 1987/* handle bpf requests */ 1988int 1989sysctl_bpf(char *string, char **bufpp, int mib[], int flags, int *typep) 1990{ 1991 int indx; 1992 1993 if (*bufpp == NULL) { 1994 listall(string, &bpflist); 1995 return (-1); 1996 } 1997 if ((indx = findname(string, "third", bufpp, &bpflist)) == -1) 1998 return (-1); 1999 mib[2] = indx; 2000 *typep = CTLTYPE_INT; 2001 return (3); 2002} 2003 2004/* 2005 * Handle SysV semaphore info requests 2006 */ 2007int 2008sysctl_seminfo(string, bufpp, mib, flags, typep) 2009 char *string; 2010 char **bufpp; 2011 int mib[]; 2012 int flags; 2013 int *typep; 2014{ 2015 int indx; 2016 2017 if (*bufpp == NULL) { 2018 listall(string, &semlist); 2019 return (-1); 2020 } 2021 if ((indx = findname(string, "third", bufpp, &semlist)) == -1) 2022 return (-1); 2023 mib[2] = indx; 2024 *typep = CTLTYPE_INT; 2025 return (3); 2026} 2027 2028/* 2029 * Handle SysV shared memory info requests 2030 */ 2031int 2032sysctl_shminfo(string, bufpp, mib, flags, typep) 2033 char *string; 2034 char **bufpp; 2035 int mib[]; 2036 int flags; 2037 int *typep; 2038{ 2039 int indx; 2040 2041 if (*bufpp == NULL) { 2042 listall(string, &shmlist); 2043 return (-1); 2044 } 2045 if ((indx = findname(string, "third", bufpp, &shmlist)) == -1) 2046 return (-1); 2047 mib[2] = indx; 2048 *typep = CTLTYPE_INT; 2049 return (3); 2050} 2051 2052/* 2053 * Handle watchdog support 2054 */ 2055int 2056sysctl_watchdog(char *string, char **bufpp, int mib[], int flags, 2057 int *typep) 2058{ 2059 int indx; 2060 2061 if (*bufpp == NULL) { 2062 listall(string, &watchdoglist); 2063 return (-1); 2064 } 2065 if ((indx = findname(string, "third", bufpp, &watchdoglist)) == -1) 2066 return (-1); 2067 mib[2] = indx; 2068 *typep = watchdoglist.list[indx].ctl_type; 2069 return (3); 2070} 2071 2072/* 2073 * Handle timecounter support 2074 */ 2075int 2076sysctl_tc(char *string, char **bufpp, int mib[], int flags, 2077 int *typep) 2078{ 2079 int indx; 2080 2081 if (*bufpp == NULL) { 2082 listall(string, &tclist); 2083 return (-1); 2084 } 2085 if ((indx = findname(string, "third", bufpp, &tclist)) == -1) 2086 return (-1); 2087 mib[2] = indx; 2088 *typep = tclist.list[indx].ctl_type; 2089 return (3); 2090} 2091 2092/* 2093 * Handle hardware monitoring sensors support 2094 */ 2095int 2096sysctl_sensors(char *string, char **bufpp, int mib[], int flags, int *typep) 2097{ 2098 char *name; 2099 int indx; 2100 2101 if (*bufpp == NULL) { 2102 char buf[BUFSIZ]; 2103 2104 /* scan all sensors */ 2105 for (indx = 0; indx < 256; indx++) { 2106 snprintf(buf, sizeof(buf), "%s.%u", string, indx); 2107 parse(buf, 0); 2108 } 2109 return (-1); 2110 } 2111 if ((name = strsep(bufpp, ".")) == NULL) { 2112 warnx("%s: incomplete specification", string); 2113 return (-1); 2114 } 2115 mib[2] = atoi(name); 2116 *typep = CTLTYPE_STRUCT; 2117 return (3); 2118} 2119 2120void 2121print_sensor(struct sensor *s) 2122{ 2123 const char *name; 2124 2125 printf("%s, %s, ", s->device, s->desc); 2126 2127 if (s->flags & SENSOR_FUNKNOWN) 2128 printf("unknown"); 2129 else { 2130 switch (s->type) { 2131 case SENSOR_TEMP: 2132 printf("%.2f degC", 2133 (s->value - 273150000) / 1000000.0); 2134 break; 2135 case SENSOR_FANRPM: 2136 printf("%lld RPM", s->value); 2137 break; 2138 case SENSOR_VOLTS_DC: 2139 printf("%.2f V DC", s->value / 1000000.0); 2140 break; 2141 case SENSOR_AMPS: 2142 printf("%.2f A", s->value / 1000000.0); 2143 break; 2144 case SENSOR_INDICATOR: 2145 printf("%s", s->value ? "On" : "Off"); 2146 break; 2147 case SENSOR_INTEGER: 2148 printf("%lld raw", s->value); 2149 break; 2150 case SENSOR_PERCENT: 2151 printf("%.2f%%", s->value / 1000.0); 2152 break; 2153 case SENSOR_LUX: 2154 printf("%.2f lx", s->value / 1000000.0); 2155 break; 2156 case SENSOR_DRIVE: 2157 switch (s->value) { 2158 case SENSOR_DRIVE_EMPTY: 2159 name = "empty"; 2160 break; 2161 case SENSOR_DRIVE_READY: 2162 name = "ready"; 2163 break; 2164 case SENSOR_DRIVE_POWERUP: 2165 name = "powering up"; 2166 break; 2167 case SENSOR_DRIVE_ONLINE: 2168 name = "online"; 2169 break; 2170 case SENSOR_DRIVE_IDLE: 2171 name = "idle"; 2172 break; 2173 case SENSOR_DRIVE_ACTIVE: 2174 name = "active"; 2175 break; 2176 case SENSOR_DRIVE_REBUILD: 2177 name = "rebuilding"; 2178 break; 2179 case SENSOR_DRIVE_POWERDOWN: 2180 name = "powering down"; 2181 break; 2182 case SENSOR_DRIVE_FAIL: 2183 name = "failed"; 2184 break; 2185 case SENSOR_DRIVE_PFAIL: 2186 name = "degraded"; 2187 break; 2188 default: 2189 name = "unknown"; 2190 break; 2191 } 2192 printf("drive %s", name); 2193 break; 2194 case SENSOR_TIMEDELTA: 2195 printf("%.2f secs", s->value / 1000000000.0); 2196 break; 2197 default: 2198 printf("unknown"); 2199 } 2200 } 2201 2202 switch (s->status) { 2203 case SENSOR_S_OK: 2204 printf(", OK"); 2205 break; 2206 case SENSOR_S_WARN: 2207 printf(", WARNING"); 2208 break; 2209 case SENSOR_S_CRIT: 2210 printf(", CRITICAL"); 2211 break; 2212 case SENSOR_S_UNKNOWN: 2213 printf(", UNKNOWN"); 2214 break; 2215 } 2216 2217 if (s->tv.tv_sec) { 2218 time_t t = s->tv.tv_sec; 2219 char *ct; 2220 2221 ct = ctime(&t); 2222 if (ct) { 2223 ct = strdup(ct); 2224 ct[19] = '\0'; 2225 printf(", %s.%2f", ct, s->tv.tv_usec / 1000.0); 2226 } 2227 } 2228} 2229 2230struct emulname { 2231 char *name; 2232 int index; 2233} *emul_names; 2234int emul_num, nemuls; 2235int emul_init(void); 2236 2237int 2238sysctl_emul(char *string, char *newval, int flags) 2239{ 2240 int mib[4], enabled, i, old, print, found = 0; 2241 char *head, *target; 2242 size_t len; 2243 2244 if (emul_init() == -1) { 2245 warnx("emul_init: out of memory"); 2246 return (1); 2247 } 2248 2249 mib[0] = CTL_KERN; 2250 mib[1] = KERN_EMUL; 2251 mib[3] = KERN_EMUL_ENABLED; 2252 head = "kern.emul."; 2253 2254 if (aflag || strcmp(string, "kern.emul") == 0) { 2255 if (newval) { 2256 warnx("%s: specification is incomplete", string); 2257 return (1); 2258 } 2259 if (nflag) 2260 printf("%d\n", nemuls); 2261 else 2262 printf("%snemuls%s%d\n", head, equ, nemuls); 2263 for (i = 0; i < emul_num; i++) { 2264 if (emul_names[i].name == NULL) 2265 break; 2266 if (i > 0 && strcmp(emul_names[i].name, 2267 emul_names[i-1].name) == 0) 2268 continue; 2269 mib[2] = emul_names[i].index; 2270 len = sizeof(int); 2271 if (sysctl(mib, 4, &enabled, &len, NULL, 0) == -1) { 2272 warn("%s", string); 2273 continue; 2274 } 2275 if (nflag) 2276 printf("%d\n", enabled); 2277 else 2278 printf("%s%s%s%d\n", head, emul_names[i].name, 2279 equ, enabled); 2280 } 2281 return (0); 2282 } 2283 /* User specified a third level name */ 2284 target = strrchr(string, '.'); 2285 target++; 2286 if (strcmp(target, "nemuls") == 0) { 2287 if (newval) { 2288 warnx("Operation not permitted"); 2289 return (1); 2290 } 2291 if (nflag) 2292 printf("%d\n", nemuls); 2293 else 2294 printf("%snemuls = %d\n", head, nemuls); 2295 return (0); 2296 } 2297 print = 1; 2298 for (i = 0; i < emul_num; i++) { 2299 if (!emul_names[i].name || (strcmp(target, emul_names[i].name))) 2300 continue; 2301 found = 1; 2302 mib[2] = emul_names[i].index; 2303 len = sizeof(int); 2304 if (newval) { 2305 enabled = atoi(newval); 2306 if (sysctl(mib, 4, &old, &len, &enabled, len) == -1) { 2307 warn("%s", string); 2308 print = 0; 2309 continue; 2310 } 2311 if (print) { 2312 if (nflag) 2313 printf("%d\n", enabled); 2314 else 2315 printf("%s%s: %d -> %d\n", head, 2316 target, old, enabled); 2317 } 2318 } else { 2319 if (sysctl(mib, 4, &enabled, &len, NULL, 0) == -1) { 2320 warn("%s", string); 2321 continue; 2322 } 2323 if (print) { 2324 if (nflag) 2325 printf("%d\n", enabled); 2326 else 2327 printf("%s%s = %d\n", head, target, 2328 enabled); 2329 } 2330 } 2331 print = 0; 2332 } 2333 if (!found) 2334 warnx("third level name %s in kern.emul is invalid", 2335 string); 2336 return (0); 2337 2338 2339} 2340 2341int 2342emulcmp(const void *m, const void *n) 2343{ 2344 const struct emulname *a = m, *b = n; 2345 2346 if (!a || !a->name) 2347 return 1; 2348 if (!b || !b->name) 2349 return -1; 2350 return (strcmp(a->name, b->name)); 2351} 2352 2353int 2354emul_init(void) 2355{ 2356 static int done; 2357 char string[16]; 2358 int mib[4], i; 2359 size_t len; 2360 2361 if (done) 2362 return (0); 2363 done = 1; 2364 2365 mib[0] = CTL_KERN; 2366 mib[1] = KERN_EMUL; 2367 mib[2] = KERN_EMUL_NUM; 2368 len = sizeof(int); 2369 if (sysctl(mib, 3, &emul_num, &len, NULL, 0) == -1) 2370 return (-1); 2371 2372 emul_names = calloc(emul_num, sizeof(*emul_names)); 2373 if (emul_names == NULL) 2374 return (-1); 2375 2376 nemuls = emul_num; 2377 for (i = 0; i < emul_num; i++) { 2378 emul_names[i].index = mib[2] = i + 1; 2379 mib[3] = KERN_EMUL_NAME; 2380 len = sizeof(string); 2381 if (sysctl(mib, 4, string, &len, NULL, 0) == -1) 2382 continue; 2383 if (strcmp(string, "native") == 0) 2384 continue; 2385 emul_names[i].name = strdup(string); 2386 if (emul_names[i].name == NULL) { 2387 free(emul_names); 2388 return (-1); 2389 } 2390 } 2391 qsort(emul_names, nemuls, sizeof(*emul_names), emulcmp); 2392 for (i = 0; i < emul_num; i++) { 2393 if (!emul_names[i].name || (i > 0 && 2394 strcmp(emul_names[i].name, emul_names[i - 1].name) == 0)) 2395 nemuls--; 2396 } 2397 return (0); 2398} 2399 2400/* 2401 * Scan a list of names searching for a particular name. 2402 */ 2403int 2404findname(char *string, char *level, char **bufp, struct list *namelist) 2405{ 2406 char *name; 2407 int i; 2408 2409 if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) { 2410 warnx("%s: incomplete specification", string); 2411 return (-1); 2412 } 2413 for (i = 0; i < namelist->size; i++) 2414 if (namelist->list[i].ctl_name != NULL && 2415 strcmp(name, namelist->list[i].ctl_name) == 0) 2416 break; 2417 if (i == namelist->size) { 2418 warnx("%s level name %s in %s is invalid", level, name, string); 2419 return (-1); 2420 } 2421 return (i); 2422} 2423 2424void 2425usage(void) 2426{ 2427 2428 (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n", 2429 "sysctl [-n] variable ...", "sysctl [-nqw] variable=value ...", 2430 "sysctl [-n] -Aa"); 2431 exit(1); 2432} 2433