sysctl.c revision 1.47
1/* $OpenBSD: sysctl.c,v 1.47 2000/02/06 16:58:01 itojun 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 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 char sccsid[] = "@(#)sysctl.c 8.5 (Berkeley) 5/9/95"; 46#else 47static char *rcsid = "$OpenBSD: sysctl.c,v 1.47 2000/02/06 16:58:01 itojun 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/sysctl.h> 56#include <sys/socket.h> 57#include <vm/vm_param.h> 58#include <machine/cpu.h> 59#include <net/route.h> 60 61#include <netinet/in.h> 62#include <netinet/in_systm.h> 63#include <netinet/ip.h> 64#include <netinet/in_pcb.h> 65#include <netinet/ip_icmp.h> 66#include <netinet/ip_ipip.h> 67#include <netinet/ip_ether.h> 68#include <netinet/ip_ah.h> 69#include <netinet/ip_esp.h> 70#include <netinet/icmp_var.h> 71#include <netinet/ip_var.h> 72#include <netinet/udp.h> 73#include <netinet/udp_var.h> 74#include <netinet/tcp.h> 75#include <netinet/tcp_timer.h> 76#include <netinet/tcp_var.h> 77#include <netinet/ip_gre.h> 78 79#ifdef INET6 80#include <netinet/ip6.h> 81#include <netinet/icmp6.h> 82#include <netinet6/ip6_var.h> 83#include <netinet6/pim6_var.h> 84#endif 85 86#include <netipx/ipx.h> 87#include <netipx/ipx_var.h> 88#include <netipx/spx_var.h> 89#include <ddb/db_var.h> 90#include <dev/rndvar.h> 91 92#include <err.h> 93#include <errno.h> 94#include <stdio.h> 95#include <stdlib.h> 96#include <string.h> 97#include <ctype.h> 98 99#ifdef CPU_BIOS 100#include <machine/biosvar.h> 101#endif 102 103struct ctlname topname[] = CTL_NAMES; 104struct ctlname kernname[] = CTL_KERN_NAMES; 105struct ctlname vmname[] = CTL_VM_NAMES; 106struct ctlname fsname[] = CTL_FS_NAMES; 107struct ctlname netname[] = CTL_NET_NAMES; 108struct ctlname hwname[] = CTL_HW_NAMES; 109struct ctlname username[] = CTL_USER_NAMES; 110struct ctlname debugname[CTL_DEBUG_MAXID]; 111struct ctlname *vfsname; 112#ifdef CTL_MACHDEP_NAMES 113struct ctlname machdepname[] = CTL_MACHDEP_NAMES; 114#endif 115struct ctlname ddbname[] = CTL_DDB_NAMES; 116char names[BUFSIZ]; 117int lastused; 118 119struct list { 120 struct ctlname *list; 121 int size; 122}; 123struct list toplist = { topname, CTL_MAXID }; 124struct list secondlevel[] = { 125 { 0, 0 }, /* CTL_UNSPEC */ 126 { kernname, KERN_MAXID }, /* CTL_KERN */ 127 { vmname, VM_MAXID }, /* CTL_VM */ 128 { fsname, FS_MAXID }, /* CTL_FS */ 129 { netname, NET_MAXID }, /* CTL_NET */ 130 { 0, CTL_DEBUG_MAXID }, /* CTL_DEBUG */ 131 { hwname, HW_MAXID }, /* CTL_HW */ 132#ifdef CTL_MACHDEP_NAMES 133 { machdepname, CPU_MAXID }, /* CTL_MACHDEP */ 134#else 135 { 0, 0 }, /* CTL_MACHDEP */ 136#endif 137 { username, USER_MAXID }, /* CTL_USER_NAMES */ 138 { ddbname, DBCTL_MAXID }, /* CTL_DDB_NAMES */ 139 { 0, 0 }, /* CTL_VFS */ 140}; 141 142int Aflag, aflag, nflag, wflag; 143 144/* 145 * Variables requiring special processing. 146 */ 147#define CLOCK 0x00000001 148#define BOOTTIME 0x00000002 149#define CHRDEV 0x00000004 150#define BLKDEV 0x00000008 151#define RNDSTATS 0x00000010 152#define BADDYNAMIC 0x00000020 153#define BIOSGEO 0x00000040 154#define BIOSDEV 0x00000080 155#define MAJ2DEV 0x00000100 156#define UNSIGNED 0x00000200 157 158/* prototypes */ 159void debuginit __P((void)); 160void listall __P((char *, struct list *)); 161void parse __P((char *, int)); 162void parse_baddynamic __P((int *, size_t, char *, void **, size_t *, int, int)); 163void usage __P((void)); 164int findname __P((char *, char *, char **, struct list *)); 165int sysctl_inet __P((char *, char **, int *, int, int *)); 166#ifdef INET6 167int sysctl_inet6 __P((char *, char **, int *, int, int *)); 168#endif 169int sysctl_ipx __P((char *, char **, int *, int, int *)); 170int sysctl_fs __P((char *, char **, int *, int, int *)); 171int sysctl_bios __P((char *, char **, int *, int, int *)); 172void vfsinit __P((void)); 173 174int 175main(argc, argv) 176 int argc; 177 char *argv[]; 178{ 179 int ch, lvl1; 180 181 while ((ch = getopt(argc, argv, "Aanw")) != -1) { 182 switch (ch) { 183 184 case 'A': 185 Aflag = 1; 186 break; 187 188 case 'a': 189 aflag = 1; 190 break; 191 192 case 'n': 193 nflag = 1; 194 break; 195 196 case 'w': 197 wflag = 1; 198 break; 199 200 default: 201 usage(); 202 } 203 } 204 argc -= optind; 205 argv += optind; 206 207 if (argc == 0 && (Aflag || aflag)) { 208 debuginit(); 209 vfsinit(); 210 for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++) 211 listall(topname[lvl1].ctl_name, &secondlevel[lvl1]); 212 exit(0); 213 } 214 if (argc == 0) 215 usage(); 216 for (; *argv != NULL; ++argv) 217 parse(*argv, 1); 218 exit(0); 219} 220 221/* 222 * List all variables known to the system. 223 */ 224void 225listall(prefix, lp) 226 char *prefix; 227 struct list *lp; 228{ 229 int lvl2; 230 char *cp, name[BUFSIZ]; 231 232 if (lp->list == NULL) 233 return; 234 (void)strncpy(name, prefix, BUFSIZ-1); 235 name[BUFSIZ-1] = '\0'; 236 cp = &name[strlen(name)]; 237 *cp++ = '.'; 238 for (lvl2 = 0; lvl2 < lp->size; lvl2++) { 239 if (lp->list[lvl2].ctl_name == NULL) 240 continue; 241 (void)strcpy(cp, lp->list[lvl2].ctl_name); 242 parse(name, Aflag); 243 } 244} 245 246/* 247 * Parse a name into a MIB entry. 248 * Lookup and print out the MIB entry if it exists. 249 * Set a new value if requested. 250 */ 251void 252parse(string, flags) 253 char *string; 254 int flags; 255{ 256 int indx, type, state, intval; 257 size_t size, len, newsize = 0; 258 int special = 0; 259 void *newval = 0; 260 quad_t quadval; 261 struct list *lp; 262 struct vfsconf vfc; 263 int mib[CTL_MAXNAME]; 264 char *cp, *bufp, buf[BUFSIZ]; 265 266 (void)strncpy(buf, string, sizeof(buf) - 1); 267 buf[sizeof(buf) - 1] = '\0'; 268 bufp = buf; 269 if ((cp = strchr(string, '=')) != NULL) { 270 if (!wflag) 271 errx(2, "must specify -w to set variables"); 272 *strchr(buf, '=') = '\0'; 273 *cp++ = '\0'; 274 while (isspace(*cp)) 275 cp++; 276 newval = cp; 277 newsize = strlen(cp); 278 } 279 if ((indx = findname(string, "top", &bufp, &toplist)) == -1) 280 return; 281 mib[0] = indx; 282 if (indx == CTL_VFS) 283 vfsinit(); 284 if (indx == CTL_DEBUG) 285 debuginit(); 286 lp = &secondlevel[indx]; 287 if (lp->list == 0) { 288 warnx("%s: class is not implemented", topname[indx].ctl_name); 289 return; 290 } 291 if (bufp == NULL) { 292 listall(topname[indx].ctl_name, lp); 293 return; 294 } 295 if ((indx = findname(string, "second", &bufp, lp)) == -1) 296 return; 297 mib[1] = indx; 298 type = lp->list[indx].ctl_type; 299 len = 2; 300 switch (mib[0]) { 301 302 case CTL_KERN: 303 switch (mib[1]) { 304 case KERN_PROF: 305 mib[2] = GPROF_STATE; 306 size = sizeof(state); 307 if (sysctl(mib, 3, &state, &size, NULL, 0) == -1) { 308 if (flags == 0) 309 return; 310 if (!nflag) 311 (void)printf("%s: ", string); 312 (void)puts("kernel is not compiled for profiling"); 313 return; 314 } 315 if (!nflag) 316 (void)printf("%s = %s\n", string, 317 state == GMON_PROF_OFF ? "off" : "running"); 318 return; 319 case KERN_VNODE: 320 case KERN_FILE: 321 if (flags == 0) 322 return; 323 warnx("use pstat to view %s information", string); 324 return; 325 case KERN_PROC: 326 if (flags == 0) 327 return; 328 warnx("use ps to view %s information", string); 329 return; 330 case KERN_NTPTIME: 331 if (flags == 0) 332 return; 333 warnx("use xntpdc to view %s information", string); 334 return; 335 case KERN_CLOCKRATE: 336 special |= CLOCK; 337 break; 338 case KERN_BOOTTIME: 339 special |= BOOTTIME; 340 break; 341 case KERN_RND: 342 special |= RNDSTATS; 343 break; 344 case KERN_HOSTID: 345 case KERN_ARND: 346 special |= UNSIGNED; 347 break; 348 } 349 break; 350 351 case CTL_HW: 352 break; 353 354 case CTL_VM: 355 if (mib[1] == VM_LOADAVG) { 356 double loads[3]; 357 358 getloadavg(loads, 3); 359 if (!nflag) 360 (void)printf("%s = ", string); 361 (void)printf("%.2f %.2f %.2f\n", loads[0], 362 loads[1], loads[2]); 363 return; 364 } else if (mib[1] == VM_PSSTRINGS) { 365 struct _ps_strings _ps; 366 367 len = sizeof(_ps); 368 if (sysctl(mib, 2, &_ps, &len, NULL, 0) == -1) { 369 if (flags == 0) 370 return; 371 if (!nflag) 372 (void)printf("%s: ", string); 373 (void)puts("can't find ps strings"); 374 return; 375 } 376 if (!nflag) 377 (void)printf("%s = ", string); 378 (void)printf("%p\n", _ps.val); 379 return; 380 } 381#ifdef UVM 382 if (mib[1] != VM_SWAPENCRYPT) { 383#endif 384 if (flags == 0) 385 return; 386 warnx("use vmstat or systat to view %s information", string); 387 return; 388#ifdef UVM 389 } 390#endif 391 break; 392 393 case CTL_NET: 394 if (mib[1] == PF_INET) { 395 len = sysctl_inet(string, &bufp, mib, flags, &type); 396 if (len < 0) 397 return; 398 399 if ((mib[2] == IPPROTO_TCP && 400 mib[3] == TCPCTL_BADDYNAMIC) || 401 (mib[2] == IPPROTO_UDP && 402 mib[3] == UDPCTL_BADDYNAMIC)) { 403 404 special |= BADDYNAMIC; 405 406 if (newval != NULL) 407 parse_baddynamic(mib, len, string, 408 &newval, &newsize, flags, nflag); 409 } 410 break; 411 } 412#ifdef INET6 413 if (mib[1] == PF_INET6) { 414 len = sysctl_inet6(string, &bufp, mib, flags, &type); 415 if (len < 0) 416 return; 417 418 break; 419 } 420#endif 421 if (mib[1] == PF_IPX) { 422 len = sysctl_ipx(string, &bufp, mib, flags, &type); 423 if (len >= 0) 424 break; 425 return; 426 } 427 if (flags == 0) 428 return; 429 warnx("use netstat to view %s information", string); 430 return; 431 432 case CTL_DEBUG: 433 mib[2] = CTL_DEBUG_VALUE; 434 len = 3; 435 break; 436 437 case CTL_MACHDEP: 438#ifdef CPU_CONSDEV 439 if (mib[1] == CPU_CONSDEV) 440 special |= CHRDEV; 441#endif 442#ifdef CPU_BLK2CHR 443 if (mib[1] == CPU_BLK2CHR) { 444 if (bufp == NULL) 445 return; 446 mib[2] = makedev(atoi(bufp),0); 447 bufp = NULL; 448 len = 3; 449 special |= CHRDEV; 450 break; 451 } 452#endif 453#ifdef CPU_CHR2BLK 454 if (mib[1] == CPU_CHR2BLK) { 455 if (bufp == NULL) 456 return; 457 mib[2] = makedev(atoi(bufp),0); 458 bufp = NULL; 459 len = 3; 460 special |= BLKDEV; 461 break; 462 } 463#endif 464#ifdef CPU_BIOS 465 if (mib[1] == CPU_BIOS) { 466 len = sysctl_bios(string, &bufp, mib, flags, &type); 467 if (len < 0) 468 return; 469 if (mib[2] == BIOS_DEV) 470 special |= BIOSDEV; 471 if (mib[2] == BIOS_DISKINFO) 472 special |= BIOSGEO; 473 break; 474 } 475#endif 476 break; 477 478 case CTL_FS: 479 len = sysctl_fs(string, &bufp, mib, flags, &type); 480 if (len >= 0) 481 break; 482 return; 483 484 case CTL_VFS: 485 mib[3] = mib[1]; 486 mib[1] = VFS_GENERIC; 487 mib[2] = VFS_CONF; 488 len = 4; 489 size = sizeof vfc; 490 if (sysctl(mib, 4, &vfc, &size, (void *)0, (size_t)0) < 0) { 491 if (errno != EOPNOTSUPP) 492 perror("vfs print"); 493 return; 494 } 495 if (flags == 0 && vfc.vfc_refcount == 0) 496 return; 497 if (!nflag) 498 fprintf(stdout, "%s has %d mounted instance%s\n", 499 string, vfc.vfc_refcount, 500 vfc.vfc_refcount != 1 ? "s" : ""); 501 else 502 fprintf(stdout, "%d\n", vfc.vfc_refcount); 503 return; 504 505 case CTL_USER: 506 case CTL_DDB: 507 break; 508 509 default: 510 warnx("illegal top level value: %d", mib[0]); 511 return; 512 513 } 514 if (bufp) { 515 warnx("name %s in %s is unknown", bufp, string); 516 return; 517 } 518 if (newsize > 0) { 519 switch (type) { 520 case CTLTYPE_INT: 521 errno = 0; 522 if (special & UNSIGNED) 523 intval = strtoul(newval, &cp, 10); 524 else 525 intval = strtol(newval, &cp, 10); 526 if (*cp != '\0') { 527 warnx("%s: illegal value: %s", string, 528 (char *)newval); 529 return; 530 } 531 if (errno == ERANGE) { 532 warnx("%s: value %s out of range", string, 533 (char *)newval); 534 return; 535 } 536 newval = &intval; 537 newsize = sizeof(intval); 538 break; 539 540 case CTLTYPE_QUAD: 541 (void)sscanf(newval, "%qd", &quadval); 542 newval = &quadval; 543 newsize = sizeof(quadval); 544 break; 545 } 546 } 547 size = BUFSIZ; 548 if (sysctl(mib, len, buf, &size, newsize ? newval : 0, newsize) == -1) { 549 if (flags == 0) 550 return; 551 switch (errno) { 552 case EOPNOTSUPP: 553 warnx("%s: value is not available", string); 554 return; 555 case ENOTDIR: 556 warnx("%s: specification is incomplete", string); 557 return; 558 case ENOMEM: 559 warnx("%s: type is unknown to this program", string); 560 return; 561 case ENXIO: 562 if (special & BIOSGEO) 563 return; 564 default: 565 warn(string); 566 return; 567 } 568 } 569 if (special & CLOCK) { 570 struct clockinfo *clkp = (struct clockinfo *)buf; 571 572 if (!nflag) 573 (void)printf("%s = ", string); 574 (void)printf( 575 "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n", 576 clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz); 577 return; 578 } 579 if (special & BOOTTIME) { 580 struct timeval *btp = (struct timeval *)buf; 581 time_t boottime; 582 583 if (!nflag) { 584 boottime = btp->tv_sec; 585 (void)printf("%s = %s", string, ctime(&boottime)); 586 } else 587 (void)printf("%ld\n", btp->tv_sec); 588 return; 589 } 590 if (special & BLKDEV) { 591 dev_t dev = *(dev_t *)buf; 592 593 if (!nflag) 594 (void)printf("%s = %s\n", string, 595 devname(dev, S_IFBLK)); 596 else 597 (void)printf("0x%x\n", dev); 598 return; 599 } 600 if (special & CHRDEV) { 601 dev_t dev = *(dev_t *)buf; 602 603 if (!nflag) 604 (void)printf("%s = %s\n", string, 605 devname(dev, S_IFCHR)); 606 else 607 (void)printf("0x%x\n", dev); 608 return; 609 } 610#ifdef CPU_BIOS 611 if (special & BIOSGEO) { 612 bios_diskinfo_t *pdi = (bios_diskinfo_t *)buf; 613 614 if (!nflag) 615 (void)printf("%s = ", string); 616 (void)printf("bootdev = 0x%x, " 617 "cylinders = %u, heads = %u, sectors = %u\n", 618 pdi->bsd_dev, pdi->bios_cylinders, pdi->bios_heads, 619 pdi->bios_sectors); 620 return; 621 } 622 if (special & BIOSDEV) { 623 int dev = *(int*)buf; 624 625 if (!nflag) 626 (void)printf("%s = ", string); 627 (void) printf("0x%02x\n", dev); 628 return; 629 } 630#endif 631 if (special & UNSIGNED) { 632 if (newsize == 0) { 633 if (!nflag) 634 (void)printf("%s = ", string); 635 (void)printf("%u\n", *(u_int *)buf); 636 } else { 637 if (!nflag) 638 (void)printf("%s: %u -> ", string, 639 *(u_int *)buf); 640 (void)printf("%u\n", *(u_int *)newval); 641 } 642 return; 643 } 644 if (special & RNDSTATS) { 645 struct rndstats *rndstats = (struct rndstats *)buf; 646 647 if (!nflag) 648 (void)printf("%s = ", string); 649 (void)printf( 650 "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u\n", 651 rndstats->rnd_total, rndstats->rnd_used, 652 rndstats->arc4_reads, rndstats->rnd_timer, 653 rndstats->rnd_mouse, rndstats->rnd_tty, 654 rndstats->rnd_disk, rndstats->rnd_net, 655 rndstats->rnd_reads, rndstats->rnd_waits, 656 rndstats->rnd_enqs, rndstats->rnd_deqs, 657 rndstats->rnd_drops, rndstats->rnd_drople, 658 rndstats->rnd_asleep, rndstats->rnd_queued); 659 return; 660 } 661 if (special & BADDYNAMIC) { 662 in_port_t port, lastport; 663 u_int32_t *baddynamic = (u_int32_t *)buf; 664 665 if (!nflag) 666 (void)printf("%s%s", string, newsize ? ": " : " = "); 667 lastport = 0; 668 for (port = IPPORT_RESERVED/2; port < IPPORT_RESERVED; port++) 669 if (DP_ISSET(baddynamic, port)) { 670 (void)printf("%s%hd", lastport ? "," : "", 671 port); 672 lastport = port; 673 } 674 if (newsize != 0) { 675 if (!nflag) 676 fputs(" -> ", stdout); 677 baddynamic = (u_int32_t *)newval; 678 lastport = 0; 679 for (port = IPPORT_RESERVED/2; port < IPPORT_RESERVED; 680 port++) 681 if (DP_ISSET(baddynamic, port)) { 682 (void)printf("%s%hd", 683 lastport ? "," : "", port); 684 lastport = port; 685 } 686 } 687 (void)putchar('\n'); 688 return; 689 } 690 switch (type) { 691 case CTLTYPE_INT: 692 if (newsize == 0) { 693 if (!nflag) 694 (void)printf("%s = ", string); 695 (void)printf("%d\n", *(int *)buf); 696 } else { 697 if (!nflag) 698 (void)printf("%s: %d -> ", string, 699 *(int *)buf); 700 (void)printf("%d\n", *(int *)newval); 701 } 702 return; 703 704 case CTLTYPE_STRING: 705 if (newsize == 0) { 706 if (!nflag) 707 (void)printf("%s = ", string); 708 (void)puts(buf); 709 } else { 710 if (!nflag) 711 (void)printf("%s: %s -> ", string, buf); 712 (void)puts((char *)newval); 713 } 714 return; 715 716 case CTLTYPE_QUAD: 717 if (newsize == 0) { 718 if (!nflag) 719 (void)printf("%s = ", string); 720 (void)printf("%qd\n", *(quad_t *)buf); 721 } else { 722 if (!nflag) 723 (void)printf("%s: %qd -> ", string, 724 *(quad_t *)buf); 725 (void)printf("%qd\n", *(quad_t *)newval); 726 } 727 return; 728 729 case CTLTYPE_STRUCT: 730 warnx("%s: unknown structure returned", string); 731 return; 732 733 default: 734 case CTLTYPE_NODE: 735 warnx("%s: unknown type returned", string); 736 return; 737 } 738} 739 740void 741parse_baddynamic(mib, len, string, newvalp, newsizep, flags, nflag) 742 int mib[]; 743 size_t len; 744 char *string; 745 void **newvalp; 746 size_t *newsizep; 747 int flags; 748 int nflag; 749{ 750 static u_int32_t newbaddynamic[DP_MAPSIZE]; 751 in_port_t port; 752 size_t size; 753 char action, *cp; 754 755 if (strchr((char *)*newvalp, '+') || strchr((char *)*newvalp, '-')) { 756 size = sizeof(newbaddynamic); 757 if (sysctl(mib, len, newbaddynamic, &size, 0, 0) == -1) { 758 if (flags == 0) 759 return; 760 if (!nflag) 761 (void)printf("%s: ", string); 762 (void)puts("kernel does contain bad dynamic port tables"); 763 return; 764 } 765 766 while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) { 767 if (*cp != '+' && *cp != '-') 768 errx(1, "cannot mix +/- with full list"); 769 action = *cp++; 770 port = atoi(cp); 771 if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED) 772 errx(1, "invalid port, range is %d to %d", 773 IPPORT_RESERVED/2, IPPORT_RESERVED-1); 774 if (action == '+') 775 DP_SET(newbaddynamic, port); 776 else 777 DP_CLR(newbaddynamic, port); 778 } 779 } else { 780 (void)memset((void *)newbaddynamic, 0, sizeof(newbaddynamic)); 781 while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) { 782 port = atoi(cp); 783 if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED) 784 errx(1, "invalid port, range is %d to %d", 785 IPPORT_RESERVED/2, IPPORT_RESERVED-1); 786 DP_SET(newbaddynamic, port); 787 } 788 } 789 790 *newvalp = (void *)newbaddynamic; 791 *newsizep = sizeof(newbaddynamic); 792} 793 794/* 795 * Initialize the set of debugging names 796 */ 797void 798debuginit() 799{ 800 int mib[3], loc, i; 801 size_t size; 802 803 if (secondlevel[CTL_DEBUG].list != 0) 804 return; 805 secondlevel[CTL_DEBUG].list = debugname; 806 mib[0] = CTL_DEBUG; 807 mib[2] = CTL_DEBUG_NAME; 808 for (loc = lastused, i = 0; i < CTL_DEBUG_MAXID; i++) { 809 mib[1] = i; 810 size = BUFSIZ - loc; 811 if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1) 812 continue; 813 debugname[i].ctl_name = &names[loc]; 814 debugname[i].ctl_type = CTLTYPE_INT; 815 loc += size; 816 } 817 lastused = loc; 818} 819 820/* 821 * Initialize the set of filesystem names 822 */ 823void 824vfsinit() 825{ 826 int mib[4], maxtypenum, cnt, loc, size; 827 struct vfsconf vfc; 828 size_t buflen; 829 830 if (secondlevel[CTL_VFS].list != 0) 831 return; 832 mib[0] = CTL_VFS; 833 mib[1] = VFS_GENERIC; 834 mib[2] = VFS_MAXTYPENUM; 835 buflen = 4; 836 if (sysctl(mib, 3, &maxtypenum, &buflen, (void *)0, (size_t)0) < 0) 837 return; 838 if ((vfsname = malloc(maxtypenum * sizeof(*vfsname))) == 0) 839 return; 840 memset(vfsname, 0, maxtypenum * sizeof(*vfsname)); 841 mib[2] = VFS_CONF; 842 buflen = sizeof vfc; 843 for (loc = lastused, cnt = 0; cnt < maxtypenum; cnt++) { 844 mib[3] = cnt; 845 if (sysctl(mib, 4, &vfc, &buflen, (void *)0, (size_t)0) < 0) { 846 if (errno == EOPNOTSUPP) 847 continue; 848 perror("vfsinit"); 849 free(vfsname); 850 return; 851 } 852 strcat(&names[loc], vfc.vfc_name); 853 vfsname[cnt].ctl_name = &names[loc]; 854 vfsname[cnt].ctl_type = CTLTYPE_INT; 855 size = strlen(vfc.vfc_name) + 1; 856 loc += size; 857 } 858 lastused = loc; 859 secondlevel[CTL_VFS].list = vfsname; 860 secondlevel[CTL_VFS].size = maxtypenum; 861 return; 862} 863 864struct ctlname posixname[] = CTL_FS_POSIX_NAMES; 865struct list fslist = { posixname, FS_POSIX_MAXID }; 866 867/* 868 * handle file system requests 869 */ 870int 871sysctl_fs(string, bufpp, mib, flags, typep) 872 char *string; 873 char **bufpp; 874 int mib[]; 875 int flags; 876 int *typep; 877{ 878 int indx; 879 880 if (*bufpp == NULL) { 881 listall(string, &fslist); 882 return(-1); 883 } 884 if ((indx = findname(string, "third", bufpp, &fslist)) == -1) 885 return(-1); 886 mib[2] = indx; 887 *typep = fslist.list[indx].ctl_type; 888 return(3); 889} 890 891#ifdef CPU_BIOS 892struct ctlname biosname[] = CTL_BIOS_NAMES; 893struct list bioslist = { biosname, BIOS_MAXID }; 894 895/* 896 * handle BIOS requests 897 */ 898int 899sysctl_bios(string, bufpp, mib, flags, typep) 900 char *string; 901 char **bufpp; 902 int mib[]; 903 int flags; 904 int *typep; 905{ 906 char *name; 907 int indx; 908 909 if (*bufpp == NULL) { 910 listall(string, &bioslist); 911 return(-1); 912 } 913 if ((indx = findname(string, "third", bufpp, &bioslist)) == -1) 914 return(-1); 915 mib[2] = indx; 916 if (indx == BIOS_DISKINFO) { 917 if (*bufpp == NULL) { 918 char name[BUFSIZ]; 919 920 /* scan all the bios devices */ 921 for (indx = 0; indx < 256; indx++) { 922 snprintf(name, sizeof(name), "%s.%u", 923 string, indx); 924 parse(name, 1); 925 } 926 return(-1); 927 } 928 if ((name = strsep(bufpp, ".")) == NULL) { 929 warnx("%s: incomplete specification", string); 930 return(-1); 931 } 932 mib[3] = atoi(name); 933 *typep = CTLTYPE_STRUCT; 934 return 4; 935 } else { 936 *typep = bioslist.list[indx].ctl_type; 937 return(3); 938 } 939} 940#endif 941 942struct ctlname inetname[] = CTL_IPPROTO_NAMES; 943struct ctlname ipname[] = IPCTL_NAMES; 944struct ctlname icmpname[] = ICMPCTL_NAMES; 945struct ctlname ipipname[] = IPIPCTL_NAMES; 946struct ctlname tcpname[] = TCPCTL_NAMES; 947struct ctlname udpname[] = UDPCTL_NAMES; 948struct ctlname espname[] = ESPCTL_NAMES; 949struct ctlname ahname[] = AHCTL_NAMES; 950struct ctlname etheripname[] = ETHERIPCTL_NAMES; 951struct ctlname grename[] = GRECTL_NAMES; 952struct ctlname mobileipname[] = MOBILEIPCTL_NAMES; 953struct list inetlist = { inetname, IPPROTO_MAXID }; 954struct list inetvars[] = { 955 { ipname, IPCTL_MAXID }, /* ip */ 956 { icmpname, ICMPCTL_MAXID }, /* icmp */ 957 { 0, 0 }, /* igmp */ 958 { 0, 0 }, /* ggmp */ 959 { ipipname, IPIPCTL_MAXID }, /* ipencap */ 960 { 0, 0 }, 961 { tcpname, TCPCTL_MAXID }, /* tcp */ 962 { 0, 0 }, 963 { 0, 0 }, /* egp */ 964 { 0, 0 }, 965 { 0, 0 }, 966 { 0, 0 }, 967 { 0, 0 }, /* pup */ 968 { 0, 0 }, 969 { 0, 0 }, 970 { 0, 0 }, 971 { 0, 0 }, 972 { udpname, UDPCTL_MAXID }, /* udp */ 973 { 0, 0 }, 974 { 0, 0 }, 975 { 0, 0 }, 976 { 0, 0 }, 977 { 0, 0 }, 978 { 0, 0 }, 979 { 0, 0 }, 980 { 0, 0 }, 981 { 0, 0 }, 982 { 0, 0 }, 983 { 0, 0 }, 984 { 0, 0 }, 985 { 0, 0 }, 986 { 0, 0 }, 987 { 0, 0 }, 988 { 0, 0 }, 989 { 0, 0 }, 990 { 0, 0 }, 991 { 0, 0 }, 992 { 0, 0 }, 993 { 0, 0 }, 994 { 0, 0 }, 995 { 0, 0 }, 996 { 0, 0 }, 997 { 0, 0 }, 998 { 0, 0 }, 999 { 0, 0 }, 1000 { 0, 0 }, 1001 { 0, 0 }, 1002 { grename, GRECTL_MAXID }, /* GRE */ 1003 { 0, 0 }, 1004 { 0, 0 }, 1005 { espname, ESPCTL_MAXID }, /* esp */ 1006 { ahname, AHCTL_MAXID }, /* ah */ 1007 { 0, 0 }, 1008 { 0, 0 }, 1009 { 0, 0 }, 1010 { mobileipname, MOBILEIPCTL_MAXID }, /* mobileip */ 1011 { 0, 0 }, 1012 { 0, 0 }, 1013 { 0, 0 }, 1014 { 0, 0 }, 1015 { 0, 0 }, 1016 { 0, 0 }, 1017 { 0, 0 }, 1018 { 0, 0 }, 1019 { 0, 0 }, 1020 { 0, 0 }, 1021 { 0, 0 }, 1022 { 0, 0 }, 1023 { 0, 0 }, 1024 { 0, 0 }, 1025 { 0, 0 }, 1026 { 0, 0 }, 1027 { 0, 0 }, 1028 { 0, 0 }, 1029 { 0, 0 }, 1030 { 0, 0 }, 1031 { 0, 0 }, 1032 { 0, 0 }, 1033 { 0, 0 }, 1034 { 0, 0 }, 1035 { 0, 0 }, 1036 { 0, 0 }, 1037 { 0, 0 }, 1038 { 0, 0 }, 1039 { 0, 0 }, 1040 { 0, 0 }, 1041 { 0, 0 }, 1042 { 0, 0 }, 1043 { 0, 0 }, 1044 { 0, 0 }, 1045 { 0, 0 }, 1046 { 0, 0 }, 1047 { 0, 0 }, 1048 { 0, 0 }, 1049 { 0, 0 }, 1050 { 0, 0 }, 1051 { 0, 0 }, 1052 { etheripname, ETHERIPCTL_MAXID }, 1053}; 1054 1055/* 1056 * handle internet requests 1057 */ 1058int 1059sysctl_inet(string, bufpp, mib, flags, typep) 1060 char *string; 1061 char **bufpp; 1062 int mib[]; 1063 int flags; 1064 int *typep; 1065{ 1066 struct list *lp; 1067 int indx; 1068 1069 if (*bufpp == NULL) { 1070 listall(string, &inetlist); 1071 return(-1); 1072 } 1073 if ((indx = findname(string, "third", bufpp, &inetlist)) == -1) 1074 return(-1); 1075 mib[2] = indx; 1076 if (indx < IPPROTO_MAXID && inetvars[indx].list != NULL) 1077 lp = &inetvars[indx]; 1078 else if (!flags) 1079 return(-1); 1080 else { 1081 warnx("%s: no variables defined for this protocol", string); 1082 return(-1); 1083 } 1084 if (*bufpp == NULL) { 1085 listall(string, lp); 1086 return(-1); 1087 } 1088 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1089 return(-1); 1090 mib[3] = indx; 1091 *typep = lp->list[indx].ctl_type; 1092 return(4); 1093} 1094 1095#ifdef INET6 1096struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES; 1097struct ctlname ip6name[] = IPV6CTL_NAMES; 1098struct ctlname icmp6name[] = ICMPV6CTL_NAMES; 1099struct ctlname pim6name[] = PIMCTL_NAMES; 1100struct list inet6list = { inet6name, IPV6PROTO_MAXID }; 1101struct list inet6vars[] = { 1102/*0*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1103 { 0, 0 }, 1104 { 0, 0 }, 1105 { 0, 0 }, 1106 { 0, 0 }, 1107 { 0, 0 }, 1108/*10*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1109 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1110/*20*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1111 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1112/*30*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1113 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1114/*40*/ { 0, 0 }, 1115 { ip6name, IPV6CTL_MAXID }, /* ipv6 */ 1116 { 0, 0 }, 1117 { 0, 0 }, 1118 { 0, 0 }, 1119 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1120/*50*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1121 { 0, 0 }, 1122 { 0, 0 }, 1123 { 0, 0 }, 1124 { icmp6name, ICMPV6CTL_MAXID }, /* icmp6 */ 1125 { 0, 0 }, 1126/*60*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1127 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1128/*70*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1129 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1130/*80*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1131 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1132/*90*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1133 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, 1134/*100*/ { 0, 0 }, 1135 { 0, 0 }, 1136 { 0, 0 }, 1137 { pim6name, PIMCTL_MAXID }, /* pim6 */ 1138}; 1139 1140/* 1141 * handle internet6 requests 1142 */ 1143int 1144sysctl_inet6(string, bufpp, mib, flags, typep) 1145 char *string; 1146 char **bufpp; 1147 int mib[]; 1148 int flags; 1149 int *typep; 1150{ 1151 struct list *lp; 1152 int indx; 1153 1154 if (*bufpp == NULL) { 1155 listall(string, &inet6list); 1156 return(-1); 1157 } 1158 if ((indx = findname(string, "third", bufpp, &inet6list)) == -1) 1159 return(-1); 1160 mib[2] = indx; 1161 if (indx < IPV6PROTO_MAXID && inet6vars[indx].list != NULL) 1162 lp = &inet6vars[indx]; 1163 else if (!flags) 1164 return(-1); 1165 else { 1166 warnx("%s: no variables defined for this protocol", string); 1167 return(-1); 1168 } 1169 if (*bufpp == NULL) { 1170 listall(string, lp); 1171 return(-1); 1172 } 1173 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1174 return(-1); 1175 mib[3] = indx; 1176 *typep = lp->list[indx].ctl_type; 1177 return(4); 1178} 1179#endif 1180 1181struct ctlname ipxname[] = CTL_IPXPROTO_NAMES; 1182struct ctlname ipxpname[] = IPXCTL_NAMES; 1183struct ctlname spxpname[] = SPXCTL_NAMES; 1184struct list ipxlist = { ipxname, IPXCTL_MAXID }; 1185struct list ipxvars[] = { 1186 { ipxpname, IPXCTL_MAXID }, /* ipx */ 1187 { 0, 0 }, 1188 { 0, 0 }, 1189 { 0, 0 }, 1190 { 0, 0 }, 1191 { spxpname, SPXCTL_MAXID }, 1192}; 1193 1194/* 1195 * Handle internet requests 1196 */ 1197int 1198sysctl_ipx(string, bufpp, mib, flags, typep) 1199 char *string; 1200 char **bufpp; 1201 int mib[]; 1202 int flags; 1203 int *typep; 1204{ 1205 struct list *lp; 1206 int indx; 1207 1208 if (*bufpp == NULL) { 1209 listall(string, &ipxlist); 1210 return(-1); 1211 } 1212 if ((indx = findname(string, "third", bufpp, &ipxlist)) == -1) 1213 return(-1); 1214 mib[2] = indx; 1215 if (indx <= IPXPROTO_SPX && ipxvars[indx].list != NULL) 1216 lp = &ipxvars[indx]; 1217 else if (!flags) 1218 return(-1); 1219 else { 1220 warnx("%s: no variables defined for this protocol", string); 1221 return(-1); 1222 } 1223 if (*bufpp == NULL) { 1224 listall(string, lp); 1225 return(-1); 1226 } 1227 if ((indx = findname(string, "fourth", bufpp, lp)) == -1) 1228 return(-1); 1229 mib[3] = indx; 1230 *typep = lp->list[indx].ctl_type; 1231 return(4); 1232} 1233 1234/* 1235 * Scan a list of names searching for a particular name. 1236 */ 1237int 1238findname(string, level, bufp, namelist) 1239 char *string; 1240 char *level; 1241 char **bufp; 1242 struct list *namelist; 1243{ 1244 char *name; 1245 int i; 1246 1247 if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) { 1248 warnx("%s: incomplete specification", string); 1249 return(-1); 1250 } 1251 for (i = 0; i < namelist->size; i++) 1252 if (namelist->list[i].ctl_name != NULL && 1253 strcmp(name, namelist->list[i].ctl_name) == 0) 1254 break; 1255 if (i == namelist->size) { 1256 warnx("%s level name %s in %s is invalid", level, name, string); 1257 return(-1); 1258 } 1259 return(i); 1260} 1261 1262void 1263usage() 1264{ 1265 1266 (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n\t%s\n", 1267 "sysctl [-n] variable ...", "sysctl [-n] -w variable=value ...", 1268 "sysctl [-n] -a", "sysctl [-n] -A"); 1269 exit(1); 1270} 1271