code: mafs

ref: b3e875d7db45fca0cde9f4efa12872f61afc54d7
dir: /console.c/

View raw version
#include "all.h"
#include <bio.h>

enum { MAXARGS = 16 };

static int echo;
int consfd = -1;
int conspfd[2] = {-1, -1} ;

enum
{
	Cnoauth,
	Cchatty,
	Csync,
	Chalt,
	Cnewuser,
	Cusers,
	Cecho,
	Cdf,
	Cmax
};

int
cmdsync(Cmdbuf *)
{
	sync();
	dprint("sync\n");
	return 0;
}

int
cmdhalt(Cmdbuf *)
{
	shutdown();
	return 0;
}

int
cmdchatty(Cmdbuf *)
{
	extern int chatty9p;

	chatty9p = !chatty9p;
	return 0;
}

int
cmdnoauth(Cmdbuf *)
{
	noauth = 1;
	return 1;
}

int
cmdecho(Cmdbuf *cb)
{
	echo = strcmp(cb->f[1], "on") == 0;
	return 1;
}

int
cmddf(Cmdbuf *)
{
	u64 free, used;

	qlock(&superlock);
	/* TODO bug not counting the backup blocks */
	free = config.nblocks-1 - superb.hwmark + superb.tfree;
	used = config.nblocks - free;
	dprint("(blocks) free %ulld, used %ulld, total %ulld\n",
			free, used, config.nblocks);
	dprint("(MiB) free %ulld, used %ulld, total %ulld\n",
			free * Unit / 1048576,
			used * Unit / 1048576,
			config.size / 1048576);
	qunlock(&superlock);
	return 1;
}

int
cmdusers(Cmdbuf *)
{
	usersinit();
	return 0;
}

// TODO	{Cnewuser, "newuser", 0},
Cmdtab cmds[] = {
	{Cnoauth, "noauth", 1},
	{Cchatty, "chatty", 1},
	{Csync, "sync", 1},
	{Chalt, "halt", 1},
	{Cusers, "users", 1},
	{Cecho, "echo", 2},
	{Cdf, "df", 1},
};

// TODO	case Cnewuser:	cmdnewuser(cb); break;
int
runcmd(int idx, Cmdbuf *cb)
{
	switch(idx){
	case Cnoauth:	return cmdnoauth(cb); break;
	case Cchatty:	return cmdchatty(cb); break;
	case Csync:	return cmdsync(cb); break;
	case Chalt:	return cmdhalt(cb); break;
	case Cusers:	return cmdusers(cb); break;
	case Cecho:	return cmdecho(cb); break;
	case Cdf:	return cmddf(cb); break;
	default:
		dprint("unknown idx %d\n", idx);
	}
	return 0;
};

/* open a file in /srv/s and bind sfd to it. srv(3) */
int
srvfd(char *s, int mode, int sfd)
{
	int fd;
	char buf[32];

	fd = create(s, ORCLOSE|OWRITE, mode);
	if(fd < 0){
		/* if the file already exists */
		remove(s);
		fd = create(s, ORCLOSE|OWRITE, mode);
		if(fd < 0)
			panic(s);
	}
	sprint(buf, "%d", sfd);
	if(write(fd, buf, strlen(buf)) != strlen(buf))
		panic("srv write");
	return fd;
}

void
consproc(void)
{
	static Biobuf bio;
	char buf[512];
	char *s;
	int n;
	Cmdbuf *cb;
	Cmdtab *ct;

	snprint(buf, sizeof(buf), "/srv/%s.cmd", service);
	pipe(conspfd);
	consfd = srvfd(buf, 0600, conspfd[1]);
	Binit(&bio, conspfd[0], OREAD);

	if(chatty9p)
	dprint("consproc: Binit() complete\n");
	for(;;){
		s = Brdstr(&bio, '\n', 1);
		if(s == nil)
			continue;
		n = Blinelen(&bio);
		if(chatty9p)
			dprint("consproc: >%s\n", s);
		cb = parsecmd(s,n);
		if(cb != nil){
			ct = lookupcmd(cb, cmds, nelem(cmds));
			if(ct != nil)
				runcmd(ct->index, cb);
			else
				dprint("mafs console: unknown cmd, %r\n");
			free(cb);
		}
	}
}

/* get rid of this after understanding what it does
static int
walkpath(Chan *ch, char *path, char **cr)
{
	char buf[Servicelen], *p, *fp;

	fp = path;
	if(*path != '/'){
	noent:
		werrstr("%s: %s", fp, Enoent);
		return -1;
	}
	path++;
	for(;;){
		p = strchr(path, '/');
		if(p == nil){
			if(cr != nil){
				if(*path == 0){
					werrstr("%s: trailing slash", fp);
					return -1;
				}
				*cr = path;
				break;
			}
			p = path + strlen(path);
		}
		if(*path == '/'){
			path++;
			continue;
		}
		if(*path == 0)
			break;
		if(p - path >= Servicelen)
			goto noent;
		memset(buf, 0, sizeof buf);
		memcpy(buf, path, p - path);
		if(chanwalk(ch, buf) <= 0){
			werrstr("%s: %r", fp);
			return -1;
		}
		if(*p == 0)
			break;
		path = p + 1;
	}
	return 1;
}

int
cmddebugdeind(int, char **argv)
{
	Chan *ch;
	Buf *b;
	Dentry *d;

	ch = chanattach(fsmain, 0);
	if(ch == nil)
		return -1;
	ch->uid = -1;
	if(walkpath(ch, argv[1], nil) < 0)
		goto error;
	rlock(fsmain);
	dprint("loc %ulld / %uld, offset %ulld\n", ch->loc->blk, ch->loc->deind, BLOCK * ch->loc->blk + (RBLOCK - BLOCK) + DENTRYSIZ * ch->loc->deind);
	b = getbuf(fsmain->d, ch->loc->blk, TDENTRY, 0);
	if(b == nil){
		runlock(fsmain);
		goto error;
	}
	d = &b->de[ch->loc->deind];
	dprint("name %s\n", d->name);
	dprint("uid %d, muid %d, gid %d\n", d->uid, d->muid, d->gid);
	dprint("mode %#o, qid %ulld, type %#x, version %d\n", d->mode, d->path, d->type, d->vers);
	dprint("size %d\n", d->size);
	dprint("atime %ulld, mtime %ulld\n", d->atime, d->mtime);
	putbuf(b);
	runlock(fsmain);
	chanclunk(ch);
	return 0;
error:
	chanclunk(ch);
	return -1;
}

int
cmddebugchdeind(int, char **argv)
{
	Chan *ch;
	uchar *c;
	Buf *b;
	int loc, new;

	loc = strtol(argv[2], nil, 0);
	new = strtol(argv[3], nil, 0);
	if(loc >= DENTRYSIZ)
		return -9001;
	ch = chanattach(fsmain, 0);
	if(ch == nil)
		return -1;
	ch->uid = -1;
	if(walkpath(ch, argv[1], nil) < 0)
		goto error;
	rlock(fsmain);
	b = getbuf(fsmain->d, ch->loc->blk, TDENTRY, 0);
	if(b == nil){
		runlock(fsmain);
		goto error;
	}
	c = (uchar *) &b->de[ch->loc->deind];
	dprint("loc %d, old value %#.2x, new value %#.2x\n", loc, c[loc], new);
	c[loc] = new;
	b->op |= BDELWRI;
	putbuf(b);
	runlock(fsmain);
	chanclunk(ch);
	return 0;
error:
	chanclunk(ch);
	return -1;
}

int
cmddebuggetblk(int argc, char **argv)
{
	Chan *ch;
	Buf *b;
	int rc;
	uvlong r, start, end, i;

	if(argc != 3 && argc != 4)
		return -9001;
	start = atoll(argv[2]);
	if(argc == 4)
		end = atoll(argv[3]);
	else
		end = start;
	ch = chanattach(fsmain, 0);
	if(ch == nil)
		return -1;
	ch->uid = -1;
	if(walkpath(ch, argv[1], nil) < 0)
		goto error;
	rlock(fsmain);
	b = getbuf(fsmain->d, ch->loc->blk, TDENTRY, 0);
	if(b == nil){
		runlock(fsmain);
		goto error;
	}
	for(i = start; i <= end; i++){
		rc = getblk(fsmain, ch->loc, b, i, &r, GBREAD);
		if(rc > 0)
			dprint("getblk %ulld = %ulld\n", i, r);
		if(rc == 0)
			dprint("getblk %ulld not found\n", i);
		if(rc < 0)
			dprint("getblk %ulld: %r\n", i);
	}
	putbuf(b);
	runlock(fsmain);
	chanclunk(ch);
	return 0;
error:
	chanclunk(ch);
	return -1;
} */