/* * Copyright (c) 1993 The Regents of the University of California. * All rights reserved. * * %sccs.include.redist.c% */ #ifndef lint char copyright[] = "@(#) Copyright (c) 1993 The Regents of the University of California.\n\ All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)sysctl.c 5.4 (Berkeley) 04/18/93"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct ctlname topname[] = CTL_NAMES; struct ctlname kernname[] = CTL_KERN_NAMES; struct ctlname vmname[] = CTL_VM_NAMES; struct ctlname netname[] = CTL_NET_NAMES; struct ctlname hwname[] = CTL_HW_NAMES; struct list { struct ctlname *list; int size; }; struct list toplist = { topname, CTL_MAXID }; struct list secondlevel[] = { { 0, 0 }, /* CTL_UNSPEC */ { kernname, KERN_MAXID }, /* CTL_KERN */ { vmname, VM_MAXID }, /* CTL_VM */ { 0, 0 }, /* CTL_FS */ { netname, NET_MAXID }, /* CTL_NET */ { 0, 0 }, /* CTL_DEBUG */ { hwname, HW_MAXID }, /* CTL_HW */ { 0, 0 }, /* CTL_MACHDEP */ }; int Aflag, aflag, nflag, wflag; int main(argc, argv) int argc; char *argv[]; { extern char *optarg; extern int optind; int ch, lvl1; while ((ch = getopt(argc, argv, "Aanw")) != EOF) { switch (ch) { case 'A': Aflag = 1; break; case 'a': aflag = 1; break; case 'n': nflag = 1; break; case 'w': wflag = 1; break; default: usage(); } } argc -= optind; argv += optind; if (Aflag || aflag) { for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++) listall(topname[lvl1].ctl_name, &secondlevel[lvl1]); exit(0); } if (argc == 0) usage(); while (argc-- > 0) parse(*argv, 1); exit(0); } /* * List all variables known to the system. */ listall(prefix, lp) char *prefix; struct list *lp; { int lvl2; char *cp, name[BUFSIZ]; if (lp->list == 0) return; strcpy(name, prefix); cp = &name[strlen(name)]; *cp++ = '.'; for (lvl2 = 0; lvl2 < lp->size; lvl2++) { if (lp->list[lvl2].ctl_name == 0) continue; strcpy(cp, lp->list[lvl2].ctl_name); parse(name, Aflag); } } /* * Parse a name into a MIB entry. * Lookup and print out the MIB entry if it exists. * Set a new value if requested. */ parse(string, flags) char *string; int flags; { int indx, type, size, len; int isclockrate = 0; void *newval = 0; int intval, newsize = 0; quad_t quadval; struct list *lp; int mib[CTL_MAXNAME]; char *cp, *bufp, buf[BUFSIZ], strval[BUFSIZ]; bufp = buf; snprintf(buf, BUFSIZ, "%s", string); if ((cp = strchr(string, '=')) != NULL) { if (!wflag) { fprintf(stderr, "Must specify -w to set variables\n"); exit(2); } *strchr(buf, '=') = '\0'; *cp++ = '\0'; while (isspace(*cp)) cp++; newval = cp; newsize = strlen(cp); } if ((indx = findname(string, "top", &bufp, &toplist)) == -1) return; mib[0] = indx; lp = &secondlevel[indx]; if (lp->list == 0) { fprintf(stderr, "%s: class is not implemented\n", topname[indx]); return; } if (bufp == NULL) { listall(topname[indx].ctl_name, lp); return; } if ((indx = findname(string, "second", &bufp, lp)) == -1) return; mib[1] = indx; type = lp->list[indx].ctl_type; len = 2; switch (mib[0]) { case CTL_KERN: switch (mib[1]) { case KERN_VNODE: case KERN_FILE: if (flags == 0) return; fprintf(stderr, "Use pstat to view %s information\n", string); return; case KERN_PROC: if (flags == 0) return; fprintf(stderr, "Use ps to view %s information\n", string); return; case KERN_CLOCKRATE: isclockrate = 1; break; } break; case CTL_HW: break; case CTL_VM: if (mib[1] == VM_LOADAVG) { double loads[3]; getloadavg(loads, 3); if (!nflag) fprintf(stdout, "%s: ", string); fprintf(stdout, "%.2f %.2f %.2f\n", loads[0], loads[1], loads[2]); return; } if (flags == 0) return; fprintf(stderr, "Use vmstat or systat to view %s information\n", string); return; case CTL_NET: if (mib[1] == PF_INET) { len = sysctl_inet(string, &bufp, mib, flags, &type); if (len >= 0) break; return; } if (flags == 0) return; fprintf(stderr, "Use netstat to view %s information\n", string); return; case CTL_FS: case CTL_DEBUG: case CTL_MACHDEP: break; default: fprintf(stderr, "Illegal top level value: %d\n", mib[0]); return; } if (bufp) { fprintf(stderr, "name %s in %s is unknown\n", *bufp, string); return; } if (newsize > 0) { switch (type) { case CTLTYPE_INT: intval = atoi(newval); newval = &intval; newsize = sizeof intval; break; case CTLTYPE_QUAD: sscanf(newval, "%qd", &quadval); newval = &quadval; newsize = sizeof quadval; break; } } size = BUFSIZ; if (sysctl(mib, len, buf, &size, newsize ? newval : 0, newsize) == -1) { if (flags == 0) return; switch (errno) { case EOPNOTSUPP: fprintf(stderr, "%s: value is not available\n", string); return; case ENOTDIR: fprintf(stderr, "%s: specification is incomplete\n", string); return; case ENOMEM: fprintf(stderr, "%s: type is unknown to this program\n", string); return; default: perror(string); return; } } if (isclockrate) { struct clockinfo *clkp = (struct clockinfo *)buf; if (!nflag) fprintf(stdout, "%s: ", string); fprintf(stdout, "hz = %d, tick = %d, profhz = %d, stathz = %d\n", clkp->hz, clkp->tick, clkp->profhz, clkp->stathz); return; } switch (type) { case CTLTYPE_INT: if (newsize == 0) { if (!nflag) fprintf(stdout, "%s = ", string); fprintf(stdout, "%d\n", *(int *)buf); } else { if (!nflag) fprintf(stdout, "%s: %d -> ", string, *(int *)buf); fprintf(stdout, "%d\n", *(int *)newval); } return; case CTLTYPE_STRING: if (newsize == 0) { if (!nflag) fprintf(stdout, "%s = ", string); fprintf(stdout, "%s\n", buf); } else { if (!nflag) fprintf(stdout, "%s: %s -> ", string, buf); fprintf(stdout, "%s\n", newval); } return; case CTLTYPE_QUAD: if (newsize == 0) { if (!nflag) fprintf(stdout, "%s = ", string); fprintf(stdout, "%qd\n", *(quad_t *)buf); } else { if (!nflag) fprintf(stdout, "%s: %qd -> ", string, *(quad_t *)buf); fprintf(stdout, "%qd\n", *(quad_t *)newval); } return; case CTLTYPE_STRUCT: fprintf(stderr, "%s: unknown structure returned\n", string); return; default: case CTLTYPE_NODE: fprintf(stderr, "%s: unknown type returned\n", string); return; } } struct ctlname inetname[] = CTL_IPPROTO_NAMES; struct ctlname ipname[] = IPCTL_NAMES; struct ctlname icmpname[] = ICMPCTL_NAMES; struct list inetlist = { inetname, IPPROTO_MAXID }; struct list inetvars[] = { { ipname, IPCTL_MAXID }, { icmpname, ICMPCTL_MAXID }, }; /* * handle internet requests */ sysctl_inet(string, bufpp, mib, flags, typep) char *string; char **bufpp; int mib[]; int flags; int *typep; { struct list *lp; int indx; if (*bufpp == NULL) { listall(string, &inetlist); return (-1); } if ((indx = findname(string, "third", bufpp, &inetlist)) == -1) return (-1); mib[2] = indx; if (indx < 2) lp = &inetvars[indx]; else if (!flags) return (-1); else { fprintf(stderr, "%s: no variables defined for this protocol\n", string); return (-1); } if (*bufpp == NULL) { listall(string, lp); return (-1); } if ((indx = findname(string, "fourth", bufpp, lp)) == -1) return (-1); mib[3] = indx; *typep = lp->list[indx].ctl_type; return (4); } /* * Scan a list of names searching for a particular name. */ findname(string, level, bufp, namelist) char *string; char *level; char **bufp; struct list *namelist; { char *name; int i; if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) { fprintf(stderr, "%s: incomplete specification\n", string); return (-1); } for (i = 0; i < namelist->size; i++) if (!strcmp(name, namelist->list[i].ctl_name)) break; if (i == namelist->size) { fprintf(stderr, "%s level name %s in %s is invalid\n", level, name, string); return (-1); } return (i); } usage() { (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n\t%s\n", "sysctl [-n] variable ...", "sysctl [-n] -w variable=value ...", "sysctl [-n] -a", "sysctl [-n] -A"); exit(1); }