plan 9 kernel history: overview | file list | diff list

1991/0828/port/devsrv.c (diff list | history)

1991/0705/sys/src/9/port/devsrv.c:4,311991/0828/sys/src/9/port/devsrv.c:4,30 (short | long | prev | next)
1990/0227    
#include	"dat.h" 
#include	"fns.h" 
#include	"errno.h" 
1991/0828    
 
1990/0227    
#include	"devtab.h" 
1990/1002    
#include	"fcall.h" 
1990/0227    
 
1990/1110    
void *calloc(unsigned, unsigned); 
1990/1002    
void free(void *); 
                 
/* This structure holds the contents of a directory entry.  Entries are kept 
 * in a linked list. 
 */ 
1990/1110    
typedef struct Entry Entry; 
struct Entry { 
1991/0828    
typedef struct	Srv Srv; 
struct Srv{ 
1990/0227    
	Lock; 
1990/1110    
	Entry *next;		/* next entry */ 
	Entry **back;		/* entry pointer */ 
	Entry *parent;		/* parent directory */ 
1990/1002    
	Dir dir;		/* dir structure */ 
1990/1110    
	union{ 
		Chan *chan;	/* if not a subdirectory */ 
		Entry *entries;	/* directory entries */ 
1990/1002    
	}; 
}; 
1991/0828    
	char	*name; 
	Chan	**chan; 
}srv; 
1990/0227    
 
1991/0828    
int 
srvgen(Chan *c, Dirtab *tab, int ntab, int s, Dir *dp) 
{ 
	if(s >= conf.nsrv) 
		return -1; 
	if(srv.chan[s] == 0) 
		return 0; 
	devdir(c, (Qid){s, 0}, &srv.name[s*NAMELEN], 0, 0666, dp); 
	return 1; 
} 
 
1990/0227    
void 
srvinit(void) 
{ 
1991/0705/sys/src/9/port/devsrv.c:34,1251991/0828/sys/src/9/port/devsrv.c:33,72
1990/0227    
void 
srvreset(void) 
{ 
1991/0828    
	srv.chan = ialloc(conf.nsrv*sizeof(Chan*), 0); 
	srv.name = ialloc(conf.nsrv*NAMELEN, 0); 
1990/0227    
} 
 
1990/1110    
Entry * 
srvalloc(int mode){ 
	Entry *e; 
1990/1002    
	static Lock qidlock; 
	static nextqid; 
                 
1990/1110    
	e = calloc(1, sizeof(Entry)); 
1990/1002    
	e->dir.atime = e->dir.mtime = seconds(); 
	lock(&qidlock);	/* for qid allocation */ 
1990/11211    
	e->dir.qid = (Qid){mode|nextqid++, 0}; 
1990/1002    
	unlock(&qidlock); 
	return e; 
} 
                 
1990/0227    
Chan * 
srvattach(char *spec) 
{ 
1990/1002    
	Chan *c; 
	static Lock rootlock; 
1990/1110    
	static Entry *root; 
1990/1002    
                 
	lock(&rootlock); 
1990/1110    
	if(root==0){ 
		root = srvalloc(CHDIR); 
1990/1002    
		root->dir.mode = CHDIR | 0777; 
	} 
	unlock(&rootlock); 
	c = devattach('s', spec); 
	c->qid = root->dir.qid; 
	c->aux = root; 
	return c; 
1991/0828    
	return devattach('s', spec); 
1990/0227    
} 
 
Chan * 
srvclone(Chan *c, Chan *nc) 
{ 
1990/1002    
	nc = devclone(c, nc); 
	return nc; 
1991/0828    
	return devclone(c, nc); 
1990/0227    
} 
 
int 
srvwalk(Chan *c, char *name) 
{ 
1990/1110    
	Entry *dir, *e; 
1990/1002    
                 
	isdir(c); 
1990/1110    
	if(strcmp(name, ".") == 0) 
1990/1002    
		return 1; 
1990/1110    
	if((dir=c->aux) == 0) 
1990/1002    
		panic("bad aux pointer in srvwalk"); 
1990/1110    
	if(strcmp(name, "..") == 0) 
1990/1002    
		e = dir->parent; 
1990/1110    
	else{ 
1990/1002    
		lock(dir); 
1990/1110    
		for(e=dir->entries; e; e=e->next) 
1990/1002    
			if (strcmp(name, e->dir.name) == 0) 
				break; 
		unlock(dir); 
	} 
1990/1110    
	if(e==0){ 
1990/11211    
		strncpy(u->error, errstrtab[Enonexist], NAMELEN); 
1990/1002    
		return 0; 
	} 
	c->qid = e->dir.qid; 
	c->aux = e; 
	return 1; 
1991/0828    
	return devwalk(c, name, 0, 0, srvgen); 
1990/0227    
} 
 
void 
srvstat(Chan *c, char *db) 
{ 
1990/1110    
	Entry *e; 
1990/1002    
                 
1990/1110    
	e = c->aux; 
1990/1002    
	convD2M(&e->dir, db); 
1991/0828    
	devstat(c, db, 0, 0, srvgen); 
1990/0227    
} 
 
Chan * 
srvopen(Chan *c, int omode) 
{ 
1990/1110    
	Entry *e; 
1990/0227    
	Chan *f; 
 
1990/11211    
	if(c->qid.path & CHDIR){ 
1991/0828    
	if(c->qid.path == CHDIR){ 
1990/1110    
		if(omode != OREAD) 
1990/11211    
			error(Eisdir); 
1990/0227    
		c->mode = omode; 
1991/0705/sys/src/9/port/devsrv.c:127,1421991/0828/sys/src/9/port/devsrv.c:74,95
1990/0227    
		c->offset = 0; 
		return c; 
	} 
1990/1110    
	if((e=c->aux) == 0) 
1990/11211    
		error(Egreg); 
1990/1110    
	if((f=e->chan) == 0) 
1991/0828    
	lock(&srv); 
	if(waserror()){ 
		unlock(&srv); 
		nexterror(); 
	} 
	f = srv.chan[c->qid.path]; 
	if(f == 0) 
1990/11211    
		error(Eshutdown); 
1990/1110    
	if(omode & OTRUNC) 
1991/0828    
	if(omode&OTRUNC) 
1990/11211    
		error(Eperm); 
1990/1110    
	if(omode!=f->mode && f->mode!=ORDWR) 
1990/11211    
		error(Eperm); 
1990/0227    
	close(c); 
	incref(f); 
1991/0828    
	unlock(&srv); 
	poperror(); 
1990/0227    
	return f; 
} 
 
1991/0705/sys/src/9/port/devsrv.c:143,2041991/0828/sys/src/9/port/devsrv.c:96,150
1990/0227    
void 
srvcreate(Chan *c, char *name, int omode, ulong perm) 
{ 
1990/1110    
	Entry *parent, *e; 
1991/0828    
	int j, i; 
1990/0227    
 
1990/1110    
	parent = c->aux; 
1990/1002    
	isdir(c); 
	lock(parent); 
1990/1110    
	if (waserror()){ 
1990/1002    
		unlock(parent); 
1991/0828    
	if(omode != OWRITE) 
		error(Eperm); 
	lock(&srv); 
	if(waserror()){ 
		unlock(&srv); 
1990/0227    
		nexterror(); 
	} 
1990/1110    
	for(e=parent->entries; e; e=e->next) 
		if(strcmp(name, e->dir.name) == 0) 
1991/0828    
	j = -1; 
	for(i=0; i<conf.nsrv; i++){ 
		if(srv.chan[i] == 0){ 
			if(j == -1) 
				j = i; 
		}else if(strcmp(name, &srv.name[i*NAMELEN]) == 0) 
1990/11211    
			error(Einuse); 
1990/1110    
	e = srvalloc(perm & CHDIR); 
1990/1002    
	e->parent = parent; 
	strcpy(e->dir.name, name); 
	e->dir.mode = perm & parent->dir.mode; 
1990/11211    
	strcpy(e->dir.gid, parent->dir.gid); 
1990/1110    
	if(e->next = parent->entries)	/* assign = */ 
1990/1002    
		e->next->back = &e->next; 
1990/1110    
	e->back = &parent->entries; 
	*e->back = e; 
1990/1002    
	parent->dir.mtime = e->dir.mtime; 
	unlock(parent); 
1991/0828    
	} 
	if(j == -1) 
		error(Enosrv); 
	srv.chan[j] = c; 
	unlock(&srv); 
1990/1002    
	poperror(); 
1990/1121    
	c->qid = e->dir.qid; 
1990/1002    
	c->aux = e; 
1991/0828    
	strcpy(&srv.name[j*NAMELEN], name); 
	c->qid.path = j; 
1990/0227    
	c->flag |= COPEN; 
1990/1002    
	c->mode = omode; 
1991/0828    
	c->mode = OWRITE; 
1990/0227    
} 
 
void 
srvremove(Chan *c) 
{ 
1990/1110    
	Entry *e; 
1991/0828    
	Chan *f; 
1990/0227    
 
1990/1110    
	e = c->aux; 
	if(e->parent == 0) 
1991/0828    
	if(c->qid.path == CHDIR) 
1990/11211    
		error(Eperm); 
1990/1002    
	lock(e->parent); 
1991/0828    
	lock(&srv); 
1990/1110    
	if(waserror()){ 
1990/1002    
		unlock(e->parent); 
1991/0828    
		unlock(&srv); 
1990/0227    
		nexterror(); 
	} 
1990/1110    
	if(e->dir.mode & CHDIR){ 
1990/1002    
		if (e->entries != 0) 
1990/11211    
			error(Eperm); 
1990/1110    
	}else{ 
		if(e->chan == 0) 
1990/11211    
			error(Eshutdown); 
1990/1002    
		close(e->chan); 
	} 
1990/1110    
	if(*e->back = e->next)	/* assign = */ 
1990/1002    
		e->next->back = e->back; 
	unlock(e->parent); 
1991/0828    
	f = srv.chan[c->qid.path]; 
	if(f == 0) 
		error(Eshutdown); 
	if(strcmp(&srv.name[c->qid.path*NAMELEN], "boot") == 0) 
		error(Eperm); 
	srv.chan[c->qid.path] = 0; 
	unlock(&srv); 
1990/0227    
	poperror(); 
1990/1002    
	free(e); 
1991/0828    
	close(f); 
1990/0227    
} 
 
void 
1991/0705/sys/src/9/port/devsrv.c:212,2741991/0828/sys/src/9/port/devsrv.c:158,178
1990/0227    
{ 
} 
 
1990/1002    
/* A directory is being read.  The entries must be synthesized.  e points 
 * to a list of entries in this directory.  Count is the size to be 
 * read. 
 */ 
1990/1110    
int 
srvdirentry(Entry *e, char *a, long count){ 
1990/1002    
	Dir dir; 
1990/1110    
	int n; 
1990/1002    
                 
1990/1110    
	n = 0; 
	while(n!=count && e!=0){ 
1990/1002    
		n += convD2M(&e->dir, a + n); 
		e = e->next; 
	} 
	return n; 
} 
                 
1990/0227    
long 
1991/0411    
srvread(Chan *c, void *va, long n, ulong offset) 
1990/0227    
{ 
1990/1110    
	Entry *dir, *e; 
1990/0227    
                 
1990/1110    
	dir = c->aux; 
1990/1002    
	isdir(c); 
1990/1110    
	if(n <= 0) 
1990/1002    
		return 0; 
1990/1110    
	if(offset%DIRLEN || n%DIRLEN) 
1990/11211    
		error(Ebaddirread); 
1990/1002    
	lock(dir); 
1990/1110    
	for(e=dir->entries; e; e=e->next) 
		if(offset <= 0){ 
			n = srvdirentry(e, va, n); 
1990/1002    
			unlock(dir); 
			c->offset += n; 
			return n; 
1990/1110    
		}else 
1990/1002    
			offset -= DIRLEN; 
	unlock(dir); 
	return 0; 
1991/0828    
	return devdirread(c, va, n, 0, 0, srvgen); 
1990/0227    
} 
 
long 
1991/0411    
srvwrite(Chan *c, void *va, long n, ulong offset) 
1990/0227    
{ 
1990/1110    
	Entry *e; 
1990/0227    
	int i, fd; 
	char buf[32]; 
 
1990/1110    
	e = c->aux; 
	if(e->dir.mode & CHDIR) 
1990/1002    
		panic("write to directory"); 
	lock(e); 
1990/1110    
	if(waserror()){ 
1990/1002    
		unlock(e); 
		nexterror(); 
	} 
1990/1110    
	if(e->chan) 
1991/0828    
	i = c->qid.path; 
	if(srv.chan[i] != c)	/* already been written to */ 
1990/11211    
		error(Egreg); 
1990/1110    
	if(n >= sizeof buf) 
1990/11211    
		error(Egreg); 
1991/0705/sys/src/9/port/devsrv.c:275,2831991/0828/sys/src/9/port/devsrv.c:179,186
1991/0318    
	memmove(buf, va, n);	/* so we can NUL-terminate */ 
1990/0227    
	buf[n] = 0; 
	fd = strtoul(buf, 0, 0); 
1991/0705    
	e->chan = fdtochan(fd, -1); 
1990/1002    
	incref(e->chan); 
	unlock(e); 
	poperror(); 
1991/0828    
	fdtochan(fd, -1);	/* error check only */ 
	srv.chan[i] = u->p->fgrp->fd[fd]; 
	incref(u->p->fgrp->fd[fd]); 
1990/0227    
	return n; 
} 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)