1349 lines · 22.2 KB · C
RawDownload
#include <u.h>#include <libc.h>#include <auth.h>#include <fcall.h>#include <bio.h>#include <ndb.h>#include <thread.h> /* *  This fs presents a 1 level file system.  It contains *  up to three files per console (xxx and xxxctl and xxxstat) */ typedef struct Parm Parm;typedef struct Console Console;typedef struct Fid Fid;typedef struct Request Request;typedef struct Reqlist Reqlist;typedef struct Fs Fs; enum{	/* last 4 bits of qid.path */	Textern=	0,		/* fake parent of top level */	Ttopdir,			/* top level directory */	Qctl,	Qstat,	Qdata, 	Bufsize=	32*1024,	/* chars buffered per reader */	Maxcons=	128,		/* maximum consoles */	Nhash=		307,		/* Fid hash buckets */}; #define TYPE(x)		(((ulong)x.path) & 0xf)#define CONS(x)		((((ulong)x.path) >> 4)&0xfff)#define QID(c, x)	(((c)<<4) | (x)) struct Request{	Request	*next;	Fid	*fid;	Fs	*fs;	Fcall	f;	uchar	buf[1];}; struct Reqlist{	QLock;	Request	*first;	Request *last;}; struct Fid{	QLock;	Fid	*next;			/* hash list */	Fid	*cnext;			/* list of Fid's on a console */	int	fid;	int	ref; 	int	attached;	int	open;	char	*user;	char	mbuf[Bufsize];		/* message */	int	bufn;	int	used;	Qid	qid; 	Console	*c; 	char	buf[Bufsize];	char	*rp;	char	*wp; 	Reqlist	r;			/* active read requests */}; struct Parm{	char	*name;	char	*dev;	int	speed;	int	cronly;	int	ondemand;		/* open only on demand */	int	chat;			/* chat consoles are special */}; struct Console{	QLock; 	Parm; 	int	pid;			/* pid of reader */	int	cpid;			/* pid of connection */ 	int	fd;	int	cfd;	int	sfd; 	Fid	*flist;			/* open fids to broadcast to */}; struct Fs{	QLock; 	int	fd;			/* to kernel mount point */	int	messagesize;	Fid	*hash[Nhash];	Console	*cons[Maxcons];	int	ncons;}; extern	Fs*	fsmount(char*); extern	void	fsreader(void*);extern	void	fsrun(void*);extern	Fid*	fsgetfid(Fs*, int);extern	void	fsputfid(Fs*, Fid*);extern	int	fsdirgen(Fs*, Qid, int, Dir*, uchar*, int);extern	void	fsreply(Fs*, Request*, char*);extern	void	fskick(Fs*, Fid*);extern	int	fsreopen(Fs*, Console*); extern	void	fsversion(Fs*, Request*, Fid*);extern	void	fsflush(Fs*, Request*, Fid*);extern	void	fsauth(Fs*, Request*, Fid*);extern	void	fsattach(Fs*, Request*, Fid*);extern	void	fswalk(Fs*, Request*, Fid*);extern	void	fsclwalk(Fs*, Request*, Fid*);extern	void	fsopen(Fs*, Request*, Fid*);extern	void	fscreate(Fs*, Request*, Fid*);extern	void	fsread(Fs*, Request*, Fid*);extern	void	fswrite(Fs*, Request*, Fid*);extern	void	fsclunk(Fs*, Request*, Fid*);extern	void	fsremove(Fs*, Request*, Fid*);extern	void	fsstat(Fs*, Request*, Fid*);extern	void	fswstat(Fs*, Request*, Fid*);  void 	(*fcall[])(Fs*, Request*, Fid*) ={	[Tflush]	fsflush,	[Tversion]	fsversion,	[Tauth]	fsauth,	[Tattach]	fsattach,	[Twalk]		fswalk,	[Topen]		fsopen,	[Tcreate]	fscreate,	[Tread]		fsread,	[Twrite]	fswrite,	[Tclunk]	fsclunk,	[Tremove]	fsremove,	[Tstat]		fsstat,	[Twstat]	fswstat}; char Eperm[] = "permission denied";char Eexist[] = "file does not exist";char Enotdir[] = "not a directory";char Eisopen[] = "file already open";char Ebadcount[] = "bad read/write count";char Enofid[] = "no such fid"; char *consoledb = "/lib/ndb/consoledb";char *mntpt = "/mnt/consoles"; int messagesize = 8192+IOHDRSZ;int debug;Ndb *db; voidfatal(char *fmt, ...){	va_list arg;	char buf[1024]; 	fprint(2, "consolefs: ");	va_start(arg, fmt);	vseprint(buf, buf+1024, fmt, arg);	va_end(arg);	write(2, buf, strlen(buf));	write(2, "\n", 1);	threadexitsall(fmt);}  void*emalloc(uint n){	void *p; 	p = malloc(n);	if(p == nil)		fatal("malloc failed: %r");	memset(p, 0, n);	return p;} /* *  any request that can get queued for a delayed reply */Request*allocreq(Fs *fs, int bufsize){	Request *r; 	r = emalloc(sizeof(Request)+bufsize);	r->fs = fs;	r->next = nil;	return r;} /* *  for maintaining lists of requests */voidaddreq(Reqlist *l, Request *r){	qlock(l);	if(l->first == nil)		l->first = r;	else		l->last->next = r;	l->last = r;	r->next = nil;	qunlock(l);} /* *  remove the first request from a list of requests */Request*remreq(Reqlist *l){	Request *r; 	qlock(l);	r = l->first;	if(r != nil)		l->first = r->next;	qunlock(l);	return r;} /* *  remove a request with the given tag from a list of requests */Request*remtag(Reqlist *l, int tag){	Request *or, **ll; 	qlock(l);	ll = &l->first;	for(or = *ll; or; or = or->next){		if(or->f.tag == tag){			*ll = or->next;			qunlock(l);			return or;		}		ll = &or->next;	}	qunlock(l);	return nil;} Qidparentqid(Qid q){	if(q.type & QTDIR)		return (Qid){QID(0, Textern), 0, QTDIR};	else		return (Qid){QID(0, Ttopdir), 0, QTDIR};} intfsdirgen(Fs *fs, Qid parent, int i, Dir *d, uchar *buf, int nbuf){	static char name[64];	char *p;	int xcons; 	d->uid = d->gid = d->muid = "network";	d->length = 0;	d->atime = time(nil);	d->mtime = d->atime;	d->type = 'C';	d->dev = '0'; 	switch(TYPE(parent)){	case Textern:		if(i != 0)			return -1;		p = "consoles";		d->mode = DMDIR|0555;		d->qid.type = QTDIR;		d->qid.path = QID(0, Ttopdir);		d->qid.vers = 0;		break;	case Ttopdir:		xcons = i/3;		if(xcons >= fs->ncons)			return -1;		p = fs->cons[xcons]->name;		switch(i%3){		case 0:			if(fs->cons[xcons]->cfd < 0)				return 0;			snprint(name, sizeof name, "%sctl", p);			p = name;			d->qid.type = QTFILE;			d->qid.path = QID(xcons, Qctl);			d->qid.vers = 0;			break;		case 1:			if(fs->cons[xcons]->sfd < 0)				return 0;			snprint(name, sizeof name, "%sstatus", p);			p = name;			d->qid.type = QTFILE;			d->qid.path = QID(xcons, Qstat);			d->qid.vers = 0;			break;		case 2:			d->qid.type = QTFILE;			d->qid.path = QID(xcons, Qdata);			d->qid.vers = 0;			break;		}		d->mode = 0666;		break;	default:		return -1;	}	d->name = p;	if(buf != nil)		return convD2M(d, buf, nbuf);	return 1;} /* *  mount the user interface and start a request processor */Fs*fsmount(char *mntpt){	Fs *fs;	int pfd[2], srv;	char buf[32];	int n;	static void *v[2]; 	fs = emalloc(sizeof(Fs)); 	if(pipe(pfd) < 0)		fatal("opening pipe: %r"); 	/* start up the file system process */	v[0] = fs;	v[1] = pfd;	proccreate(fsrun, v, 16*1024); 	/* Typically mounted before /srv exists */	if(access("#s/consoles", AEXIST) < 0){		srv = create("#s/consoles", OWRITE, 0666);		if(srv < 0)			fatal("post: %r"); 		n = snprint(buf, sizeof buf, "%d", pfd[1]);		if(write(srv, buf, n) < 0)			fatal("write srv: %r"); 		close(srv);	} 	if(mount(pfd[1], -1, mntpt, MBEFORE, "") == -1)		fatal("mount %s: %r", mntpt);	close(pfd[1]);	return fs;} /* *  reopen a console */intopencons(Console *c){	int p[2]; 	if(pipe(p) == -1)		return -1;	c->cpid = rfork(RFFDG|RFREND|RFPROC|RFNAMEG);	if(c->cpid == -1)		return -1;	if(c->cpid == 0){		bind("/dev/null", "/dev/consctl", MREPL);		dup(p[0], 0);		dup(p[0], 1);		close(p[0]);		close(p[1]);		execl("/bin/rc", "rc", "-c", c->dev+1, nil);		exits("bad juju");	}	c->fd = p[1];	close(p[0]);	return 0;} intopenserial(Console *c){	char buf[128]; 	c->fd = open(c->dev, ORDWR);	if(c->fd < 0)		return -1; 	snprint(buf, sizeof(buf), "%sctl", c->dev);	c->cfd = open(buf, ORDWR);	fprint(c->cfd, "b%d", c->speed); 	snprint(buf, sizeof(buf), "%sstatus", c->dev);	c->sfd = open(buf, OREAD); 	return 0;} voidcloseconsfds(Console *c, char *msg){	if(c->pid > 0){		if(postnote(PNPROC, c->pid, msg) != 0)			fprint(2, "postnote failed: %r\n");		c->pid = 0;	}	if(c->cpid > 0){		if(postnote(PNPROC, c->cpid, msg) != 0)			fprint(2, "postnote failed: %r\n");		c->cpid = 0;	}	close(c->fd);	close(c->cfd);	close(c->sfd);	c->cfd = -1;	c->fd = -1;	c->sfd = -1;} intfsreopen(Fs* fs, Console *c){	static void *v[2]; 	closeconsfds(c, "reopen"); 	if(c->flist == nil && c->ondemand)		return 0; 	if(c->dev[0] == '!')		opencons(c);	else		openserial(c); 	v[0] = fs;	v[1] = c;	proccreate(fsreader, v, 16*1024); 	return 0;} voidchange(Fs *fs, Console *c, int doreopen, Parm p){	qlock(c);	if(p.speed != c->speed){		c->speed = p.speed;		doreopen = 1;	}	if(p.ondemand != c->ondemand){		c->ondemand = p.ondemand;		doreopen = 1;	}	c->cronly = p.cronly;	if(doreopen)		fsreopen(fs, c); 	qunlock(c);} /* *  create a console interface */Console*console(Fs* fs, Parm p){	Console *c;

Lines 1–500 of 1349.