Example #1
0
File: devcons.c Project: 8l/inferno
static void
consinit(void)
{
	randominit();
	debuginit();
	debugkey('f', "files/6", fddump, 0);
	debugkey('q', "panic", qpanic, 1);
	debugkey('r', "exit", rexit, 1);
	klogq = qopen(128*1024, 0, 0, 0);
}
Example #2
0
/*
 * Parse a name into a MIB entry.
 * Lookup and print out the MIB entry if it exists.
 * Set a new value if requested.
 */
static void
parse(char *string, int flags)
{
	int indx, type, len;
#ifndef __QNXNTO__
	int state, special = 0;
#endif
	void *newval = 0;
	int intval, newsize = 0;
	quad_t quadval;
	size_t size;
	struct list *lp;
	int mib[CTL_MAXNAME];
	char *cp, *bufp, buf[BUFSIZ];
#ifndef __QNXNTO__
	double loads[3];
#endif

	bufp = buf;
	snprintf(buf, BUFSIZ, "%s", string);
	if ((cp = strchr(string, '=')) != NULL) {
		if (!wflag)
			errx(2, "Must specify -w to set variables");
		*strchr(buf, '=') = '\0';
		*cp++ = '\0';
		while (isspace((unsigned char) *cp))
			cp++;
		newval = cp;
		newsize = strlen(cp);
	}
	if ((indx = findname(string, "top", &bufp, &toplist)) == -1)
		return;
	mib[0] = indx;
	if (indx == CTL_DEBUG)
		debuginit();
	if (mib[0] == CTL_PROC) {
		type = CTLTYPE_NODE;
		len = 1;
	} else {
		lp = &secondlevel[indx];
		if (lp->list == 0) {
			warnx("Class `%s' is not implemented",
			    topname[indx].ctl_name);
			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]) {
#ifndef __QNXNTO__
		case KERN_PROF:
			mib[2] = GPROF_STATE;
			size = sizeof state;
			if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) {
				if (flags == 0)
					return;
				if (!nflag)
					printf("%s: ", string);
				printf(
				    "kernel is not compiled for profiling\n");
				return;
			}
			if (!nflag)
				printf("%s: %s\n", string,
				    state == GMON_PROF_OFF ? "off" : "running");
			return;
		case KERN_VNODE:
		case KERN_FILE:
			USEAPP(string, "pstat");
			return;
		case KERN_PROC:
		case KERN_PROC2:
		case KERN_PROC_ARGS:
			USEAPP(string, "ps");
			return;
		case KERN_CLOCKRATE:
			special |= CLOCK;
			break;
		case KERN_BOOTTIME:
			special |= BOOTTIME;
			break;
		case KERN_NTPTIME:
			USEAPP(string, "ntpdc -c kerninfo");
			return;
#endif
		case KERN_MBUF:
			len = sysctl_3rd(&mbufvars, string, &bufp, mib, flags,
			    &type);
			if (len < 0)
				return;
			break;
#ifndef __QNXNTO__
		case KERN_CP_TIME:
			special |= CPTIME;
			break;
		case KERN_MSGBUF:
			USEAPP(string, "dmesg");
			return;
		case KERN_CONSDEV:
			special |= CONSDEV;
			break;
		case KERN_PIPE:
			len = sysctl_3rd(&pipevars, string, &bufp, mib, flags,
			    &type);
			if (len < 0)
				return;
			break;
		case KERN_TKSTAT:
			len = sysctl_3rd(&tkstatvars, string, &bufp, mib, flags,
			    &type);
			if (len < 0)
				return;
			break;
#endif
		}
		break;

#ifndef __QNXNTO__
	case CTL_HW:
		switch (mib[1]) {
		case HW_DISKSTATS:
			USEAPP(string, "iostat");
			return;
		}
		break;

	case CTL_VM:
		switch (mib[1]) {
		case VM_LOADAVG:
			getloadavg(loads, 3);
			if (!nflag)
				printf("%s: ", string);
			printf("%.2f %.2f %.2f\n", loads[0], loads[1],
			    loads[2]);
			return;

		case VM_METER:
		case VM_UVMEXP:
		case VM_UVMEXP2:
			USEAPP(string, "vmstat' or 'systat");
			return;
		}
		break;
#endif

	case CTL_NET:
		if (mib[1] == PF_INET) {
			len = sysctl_inet(string, &bufp, mib, flags, &type);
			if (len >= 0)
				break;
			return;
		}
#ifdef INET6
		else if (mib[1] == PF_INET6) {
			len = sysctl_inet6(string, &bufp, mib, flags, &type);
			if (len >= 0)
				break;
			return;
		}
#endif /* INET6 */
#ifdef IPSEC
		else if (mib[1] == PF_KEY) {
			len = sysctl_3rd(&keyvars, string, &bufp, mib, flags,
			    &type);
			if (len >= 0)
				break;
			return;
		}
#endif /* IPSEC */
		if (flags == 0)
			return;
		USEAPP(string, "netstat");
		return;

#ifndef __QNXNTO__
	case CTL_DEBUG:
		mib[2] = CTL_DEBUG_VALUE;
		len = 3;
		break;

	case CTL_MACHDEP:
#ifdef CPU_CONSDEV
		if (mib[1] == CPU_CONSDEV)
			special |= CONSDEV;
#endif
#ifdef CPU_DISKINFO
		if (mib[1] == CPU_DISKINFO)
			special |= DISKINFO;
#endif
		break;

	case CTL_VFS:
		if (mib[1] == VFS_GENERIC) {
			len = sysctl_3rd(&vfsgenvars, string, &bufp, mib, flags,
			    &type);
			/* Don't bother with VFS_CONF. */
			if (mib[2] == VFS_CONF)
				len = -1;
		} else
			len = sysctl_vfs(string, &bufp, mib, flags, &type);
		if (len < 0)
			return;

		/* XXX Special-case for NFS stats. */
		if (mib[1] == 2 && mib[2] == NFS_NFSSTATS) {
			USEAPP(string, "nfsstat");
			return;
		}
		break;

	case CTL_VENDOR:
	case CTL_USER:
	case CTL_DDB:
		break;
	case CTL_PROC:
		len = sysctl_proc(string, &bufp, mib, flags, &type);
		if (len < 0)
			return;
		break;
	case CTL_EMUL:
		switch (mib[1]) {
		case EMUL_LINUX:
		    len = sysctl_linux(string, &bufp, mib, flags, &type);
		    break;
		default:
		    warnx("Illegal emul level value: %d", mib[0]);
		    break;
		}
		if (len < 0)
			return;
		break;
#endif
	default:
		warnx("Illegal top level value: %d", mib[0]);
		return;

	}
	if (bufp) {
		warnx("Name %s in %s is unknown", bufp, string);
		return;
	}
	if (newsize > 0) {
		switch (type) {
		case CTLTYPE_INT:
			intval = atoi(newval);
			newval = &intval;
			newsize = sizeof intval;
			break;

		case CTLTYPE_LIMIT:
			if (strcmp(newval, "unlimited") == 0) {
				quadval = RLIM_INFINITY;
				newval = &quadval;
				newsize = sizeof quadval;
				break;
			}
			/* FALLTHROUGH */
		case CTLTYPE_QUAD:
			sscanf(newval, "%lld", (long long *)&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:
			printf("%s: the value is not available\n", string);
			return;
		case ENOTDIR:
			printf("%s: the specification is incomplete\n", string);
			return;
		case ENOMEM:
			printf("%s: this type is unknown to this program\n",
			    string);
			return;
		default:
			printf("%s: sysctl() failed with %s\n",
			    string, strerror(errno));
			return;
		}
	}
	if (qflag && (newsize > 0))
		return;
#ifndef __QNXNTO__
	if (special & CLOCK) {
		struct clockinfo *clkp = (struct clockinfo *)buf;

		if (!nflag)
			printf("%s: ", string);
		printf(
		    "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
		    clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz);
		return;
	}
	if (special & BOOTTIME) {
		struct timeval *btp = (struct timeval *)buf;
		time_t boottime;

		if (!nflag) {
			boottime = btp->tv_sec;
			/* ctime() provides the trailing newline */
			printf("%s = %s", string, ctime(&boottime));
		} else
			printf("%ld\n", (long) btp->tv_sec);
		return;
	}
	if (special & CONSDEV) {
		dev_t dev = *(dev_t *)buf;

		if (!nflag)
			printf("%s = %s\n", string, devname(dev, S_IFCHR));
		else
			printf("0x%x\n", dev);
		return;
	}
	if (special & DISKINFO) {
		/* Don't know a good way to deal with this i386 specific one */
		return;
	}
	if (special & CPTIME) {
		u_int64_t *cp_time = (u_int64_t *)buf;

		if (!nflag)
			printf("%s: ", string);
		printf("user = %llu, nice = %llu, sys = %llu, intr = %llu, "
		    "idle = %llu\n", (unsigned long long) cp_time[0],
		    (unsigned long long) cp_time[1],
		    (unsigned long long) cp_time[2],
		    (unsigned long long) cp_time[3],
		    (unsigned long long) cp_time[4]);
		return;
	}
#endif

	switch (type) {
	case CTLTYPE_INT:
		if (newsize == 0) {
			if (!nflag)
				printf("%s = ", string);
			printf("%d\n", *(int *)buf);
		} else {
			if (!nflag)
				printf("%s: %d -> ", string, *(int *)buf);
			printf("%d\n", *(int *)newval);
		}
		return;

	case CTLTYPE_STRING:
		if (newsize == 0) {
			if (!nflag)
				printf("%s = ", string);
			printf("%s\n", buf);
		} else {
			if (!nflag)
				printf("%s: %s -> ", string, buf);
			printf("%s\n", (char *) newval);
		}
		return;

	case CTLTYPE_LIMIT:
#define PRINTF_LIMIT(lim) { \
if ((lim) == RLIM_INFINITY) \
	printf("unlimited");\
else \
	printf("%lld", (long long)(lim)); \
}

		if (newsize == 0) {
			if (!nflag)
				printf("%s = ", string);
			PRINTF_LIMIT((long long)(*(quad_t *)buf));
		} else {
			if (!nflag) {
				printf("%s: ", string);
				PRINTF_LIMIT((long long)(*(quad_t *)buf));
				printf(" -> ");
			}
			PRINTF_LIMIT((long long)(*(quad_t *)newval));
		}
		printf("\n");
		return;
#undef PRINTF_LIMIT

	case CTLTYPE_QUAD:
		if (newsize == 0) {
			if (!nflag)
				printf("%s = ", string);
			printf("%lld\n", (long long)(*(quad_t *)buf));
		} else {
			if (!nflag)
				printf("%s: %lld -> ", string,
				    (long long)(*(quad_t *)buf));
			printf("%lld\n", (long long)(*(quad_t *)newval));
		}
		return;

	case CTLTYPE_STRUCT:
		warnx("%s: unknown structure returned", string);
		return;

	default:
	case CTLTYPE_NODE:
		warnx("%s: unknown type returned", string);
		return;
	}
}
Example #3
0
int
main(int argc, char *argv[])
{
	char *fn = NULL;
	int ch, lvl1;

	while ((ch = getopt(argc, argv, "Aaf:nqw")) != -1) {
		switch (ch) {

		case 'A':
			Aflag = 1;
			break;

		case 'a':
			aflag = 1;
			break;

		case 'f':
			fn = optarg;
			wflag = 1;
			break;

		case 'n':
			nflag = 1;
			break;

		case 'q':
			qflag = 1;
			break;

		case 'w':
			wflag = 1;
			break;

		default:
			usage();
		}
	}

	if (qflag && !wflag)
		usage();

	argc -= optind;
	argv += optind;

	if (Aflag || aflag) {
		debuginit();
		for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++)
			listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
		return 0;
	}

	if (fn) {
#ifndef __QNXNTO__
		FILE *fp;
		char *l;

		fp = fopen(fn, "r");
		if (fp == NULL) {
			err(1, "%s", fn);
		} else {
			for (; (l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL;
			    free(l)) {
				if (*l)
					parse(l, 1);
			}
			fclose(fp);
		}
#endif
	} else {
		if (argc == 0)
			usage();
		while (argc-- > 0)
			parse(*argv++, 1);
	}
	return 0;
}