Example #1
0
/*
 * handle internet6 requests
 */
static int
sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep)
{
	struct list *lp;
	int indx;

	if (*bufpp == NULL) {
		listall(string, &inet6list);
		return (-1);
	}
	if ((indx = findname(string, "third", bufpp, &inet6list)) == -1)
		return (-1);
	mib[2] = indx;
	if (indx <= sizeof(inet6vars)/sizeof(inet6vars[0])
	 && inet6vars[indx].list != NULL) {
		lp = &inet6vars[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);
}
Example #2
0
/*
 * handle internet requests
 */
static int
sysctl_inet(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 <= IPPROTO_MAXID && inetvars[indx].list != NULL)
		lp = &inetvars[indx];
	else if (!flags)
		return (-1);
	else {
		printf("%s: no variables defined for 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);
}
Example #3
0
static int
sysctl_linux(char *string, char **bufpp, int mib[], int flags, int *typep)
{
	struct list *lp = &linuxvars;
	int indx;
	char name[BUFSIZ], *cp;

	if (*bufpp == NULL) {
		(void)strcpy(name, string);
		cp = &name[strlen(name)];
		*cp++ = '.';
		(void)strcpy(cp, "kern");
		listall(name, &linuxkernvars);
		return (-1);
	}
	if ((indx = findname(string, "third", bufpp, lp)) == -1)
		return (-1);
	mib[2] = indx;
	lp = &linuxkernvars;
	*typep = lp->list[indx].ctl_type;
	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
		return (-1);
	mib[3] = indx;
	*typep = lp->list[indx].ctl_type;
	return (4);
}
Example #4
0
/*
 * handle 3rd level requests.
 */
static int
sysctl_3rd(struct list *lp, char *string, char **bufpp, int mib[], int flags,
    int *typep)
{
	int indx;

	if (*bufpp == NULL) {
		listall(string, lp);
		return (-1);
	}
	if ((indx = findname(string, "third", bufpp, lp)) == -1)
		return (-1);
	mib[2] = indx;
	*typep = lp->list[indx].ctl_type;
	return (3);
}
Example #5
0
int
main(int argc, char *argv[])
{
	char *path;
	int ch;

	while ((ch = getopt(argc, argv, "Aan")) != -1) {
		switch (ch) {

		case 'A':
			Aflag = 1;
			break;

		case 'a':
			aflag = 1;
			break;

		case 'n':
			nflag = 1;
			break;

		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc == 0)
		usage();
	path = *argv++;
	if (strcmp(path, "-") == 0)
		stdinflag = 1;
	argc--;
	if (Aflag || aflag) {
		listall(path, &pclist);
		return (0);
	}
	if (argc == 0)
		usage();
	while (argc-- > 0)
		parse(path, *argv, 1);
	return (0);
}
Example #6
0
/*
 * handle vfs requests
 */
static int
sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep)
{
	struct list *lp = &vfsvars[mib[1]];
	int indx;

	if (lp->list == NULL) {
		if (flags)
			printf("%s: no variables defined for file system\n",
			    string);
		return (-1);
	}
	if (*bufpp == NULL) {
		listall(string, lp);
		return (-1);
	}
	if ((indx = findname(string, "third", bufpp, lp)) == -1)
		return (-1);
	mib[2] = indx;
	*typep = lp->list[indx].ctl_type;
	return (3);
}
Example #7
0
void typeline(bc *bc, char *preload,int echocr)
{
int i=0,j;
int code;
int backcount=0;
char linesave[LINESIZE],ch;
int linesin;
char ref;
int xdelta;
char *p1,*p2;
char token[128];
char *fake;
int scrollback;
int startx, starty;
char spaces[256], *zerospaces;
char *tail;
int oldlen;

	memset(spaces, ' ', sizeof(spaces));
	zerospaces = spaces + sizeof(spaces)-1;
	*zerospaces = 0;
top:
	tail="";
	startx = bc->txpos;
	starty = bc->typos;
	xdelta=0;
	linesin=bc->hcount>HISTSIZE ? HISTSIZE : bc->hcount;
	strcpy(bc->debline, preload);
	i=strlen(bc->debline);
	tprintf(bc, preload);
	ref=0;
	fake=0;
	scrollback=0;
	while(!(bc->flags & (BF_CCHIT | BF_QUIT)))
	{
		if(!fake)
		{
			SDL_Delay(10);
			scaninput(bc);
			code=takedown(bc);
//if(code=='q' + MYALTED) exit(0); // alt-q
		} else
		{
			code=*fake++;
			if(!code) {fake=0;continue;}
		}
		if(code==-1) continue;
		if(code==MYPAGEUP || code==(MYPAGEUP|MYSHIFTED))
		{
			if(code==MYPAGEUP) ++scrollback;
			else scrollback+=bc->tysize;
			scrollback=showhistory(bc, scrollback);
			continue;
		} else if(code==MYPAGEDOWN || code==(MYPAGEDOWN|MYSHIFTED))
		{
			if(code==MYPAGEDOWN) --scrollback;
			else scrollback-=bc->tysize;
			if(scrollback<0) scrollback=0;
			scrollback=showhistory(bc, scrollback);
			if(scrollback)
				continue;
			else
				++ref;
		}
		if(code==9 || code==4)
		{
			j=0;
			while(i+xdelta-j>0)
			{
				ch=bc->debline[i+xdelta-j-1];
				if(!isalpha(ch) && !isdigit(ch) && ch!='_' && ch!='.') break;
				++j;
			}
			p1=token;
			while(j)
				*p1++=bc->debline[i+xdelta-j--];
			*p1=0;
			if(*token)
			{
				if(code==9)
					fake=complete(token);
				else
				{
					cr(bc);
					listall(token);
					ref=1;
				}
			}
		} else if(code==0x7f)
		{
			if(!xdelta) continue;
			p1=bc->debline+i+xdelta;
			p2=p1+1;
			while((*p1++=*p2++));
			--i;
			++xdelta;
			++ref;
		} else if(code==MYLEFT)
		{
			if(i+xdelta>0) {--xdelta;++ref;}
		} else if(code==MYRIGHT)
		{
			if(xdelta<0) {++xdelta;++ref;}
		} else if(code==MYUP)
		{
			if(backcount<linesin)
			{
				if(!backcount)
					memcpy(linesave,bc->debline,LINESIZE);
				++backcount;
				oldlen = strlen(bc->debline);
				memcpy(bc->debline,
					bc->debhist+LINESIZE*((bc->hcount-backcount) &
						(HISTSIZE-1)), LINESIZE);
				oldlen -= strlen(bc->debline);
				if(oldlen>0) tail=zerospaces-oldlen;
				xdelta=0;
			}
			++ref;
		} else if(code==MYDOWN)
		{
			if(backcount)
			{
				--backcount;
				oldlen = strlen(bc->debline);
				if(!backcount) memcpy(bc->debline,linesave,LINESIZE);
				else
					memcpy(bc->debline,bc->debhist+LINESIZE*((bc->hcount-backcount) & (HISTSIZE-1)),LINESIZE);
				oldlen -= strlen(bc->debline);
				if(oldlen>0) tail=zerospaces-oldlen;
				xdelta=0;
			}
			++ref;
		} else if(code>=0 && code<128)
		{
			if(code==8)
			{
				if(i+xdelta)
				{
					--i;
					p1=bc->debline+i+xdelta;
					p2=p1+1;
					while((*p1++=*p2++));
					++ref;
				} else continue;
			} else if(code==CR)
			{
				if(echocr) cr(bc);
				break;
			}
			else if(code>=0x20 && i<bc->txsize-1 && i<sizeof(bc->debline)-1)
			{
				p2=bc->debline+i;
				p1=p2+1;
				j=1-xdelta;
				while(j--) *p1--=*p2--;
				*p1=code;
				++ref;
			} else continue;
		}
		if(ref)
		{
			if(scrollback)
				scrollback=showhistory(bc, 0);
			i=strlen(bc->debline);
			tprintf(bc, "\033%dx\033%dy%s %s\033%dx\033%dy",
				startx, starty, bc->debline, tail, i+xdelta+startx, starty);
			tail="";
			ref=0;
		}

	}
	if(bc->flags & BF_CCHIT)
	{
		flushinput(bc);
		if(!(bc->flags & BF_RUNNING))
		{
			bc->flags &= ~BF_CCHIT;
			preload="";
			tprintf(bc, "\n");
			goto top;
		}
	}
	if(i)
	{
		memcpy(bc->debhist+LINESIZE*(bc->hcount & (HISTSIZE-1)),bc->debline,LINESIZE);
		++bc->hcount;
	}
}
Example #8
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 #9
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;
}
Example #10
0
/*
 * handle kern.proc requests
 */
static int
sysctl_proc(char *string, char **bufpp, int mib[], int flags, int *typep)
{
	char *cp, name[BUFSIZ];
	struct list *lp;
	int indx;

	if (*bufpp == NULL) {
		strcpy(name, string);
		cp = &name[strlen(name)];
		*cp++ = '.';
		strcpy(cp, "curproc");
		parse(name, Aflag);
		return (-1);
	}
	cp = strsep(bufpp, ".");
	if (cp == NULL) {
		warnx("%s: incomplete specification", string);
		return (-1);
	}
	if (strcmp(cp, "curproc") == 0) {
		mib[1] = PROC_CURPROC;
	} else {
		mib[1] = atoi(cp);
		if (mib[1] == 0) {
			warnx("second level name %s in %s is invalid", cp,
			    string);
			return (-1);
		}
	}
	*typep = CTLTYPE_NODE;
	lp = &procvars;
	if (*bufpp == NULL) {
		listall(string, lp);
		return (-1);
	}
	if ((indx = findname(string, "third", bufpp, lp)) == -1)
		return (-1);
	mib[2] = indx;
	*typep = lp->list[indx].ctl_type;
	if (*typep != CTLTYPE_NODE)
		return(3);
	lp = &proclimitvars;
	if (*bufpp == NULL) {
		listall(string, lp);
		return (-1);
	}
	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
		return (-1);
	mib[3] = indx;
	lp = &proclimittypevars;
	if (*bufpp == NULL) {
		listall(string, lp);
		return (-1);
	}
	if ((indx = findname(string, "fifth", bufpp, lp)) == -1)
		return (-1);
	mib[4] = indx;
	*typep = CTLTYPE_LIMIT;
	return(5);
}