forsyth20121205-174063a80cc13 years agoHistory
623 lines · 15.3 KB · Text
RawDownload
.TH STYXSERVER 10.2.SH NAMEStyxserver \- C Styx server library.SH SYNOPSIS.EX#include <lib9.h>#include <styx.h>#include <styxserver.h>#define Qroot	0 #define MSGMAX	((((8192+128)*2)+3) & ~3) extern char Enomem[];	/* out of memory */extern char Eperm[];		/* permission denied */extern char Enodev[];	/* no free devices */extern char Ehungup[];	/* i/o on hungup channel */extern char Eexist[];		/* file exists */extern char Enonexist[];	/* file does not exist */extern char Ebadcmd[];	/* bad command */extern char Ebadarg[];	/* bad arguments */ typedef uvlong	Path;typedef struct Styxserver	Styxserver;typedef struct Styxops Styxops;typedef struct Styxfile Styxfile;typedef struct Client Client; struct Styxserver{	Styxops *ops;	Path qidgen;	int connfd;	Client *clients;	Client *curc;	Styxfile *root;	Styxfile **ftab;	void	*priv;	/* private */}; struct Client{	Styxserver *server;	Client *next;	int		fd;	char	msg[MSGMAX];	uint		nread;		/* valid bytes in msg (including nc)*/	int		nc;		/* bytes consumed from front of msg by convM2S */	char	data[MSGMAX];	/* Tread/Rread data */	int		state;	Fid		*fids;	char		*uname;	/* uid */	char		*aname;	/* attach name */	void		*u;}; struct Styxops{	char *(*newclient)(Client *c);	char *(*freeclient)(Client *c); 	char *(*attach)(char *uname, char *aname);	char *(*walk)(Qid *qid, char *name);	char *(*open)(Qid *qid, int mode);	char *(*create)(Qid *qid, char *name, int perm, int mode);	char *(*read)(Qid qid, char *buf, ulong *n, vlong offset);	char *(*write)(Qid qid, char *buf, ulong *n, vlong offset);	char *(*close)(Qid qid, int mode);	char *(*remove)(Qid qid);	char *(*stat)(Qid qid, Dir *d);	char *(*wstat)(Qid qid, Dir *d);}; struct Styxfile{	Dir	d;	Styxfile *parent;	Styxfile *child;	Styxfile *sibling;	Styxfile *next;	int ref;	int open;	void	*u;}; void styxsetowner(char *user);char *styxinit(Styxserver *server, Styxops *ops, char *port, int perm, int needfile);char *styxwait(Styxserver *server);char *styxprocess(Styxserver *server);char *styxend(Styxserver *server); Client *styxclient(Styxserver *server); Styxfile *styxaddfile(Styxserver *server, Path pqid, Path qid, char *name,					int mode, char *owner);Styxfile *styxadddir(Styxserver *server, Path pqid, Path qid, char *name,					int mode, char *owner);int styxrmfile(Styxserver *server, Path qid);Styxfile *styxfindfile(Styxserver *server, Path qid); int	styxperm(Styxfile *file, char *uid, int mode);long styxreadstr(ulong off, char *buf, ulong n, char *str);Qid styxqid(int path, int isdir);void *styxmalloc(int bytes);void styxfree(void *p);void styxdebug(void);.EE.SH DESCRIPTIONThe C Styx server library provides a small suite of functions to enable theproduction of a file server based on the Inferno Styx protocol. The followingelements define the primary routines in the interface:.TP.BI styxinit(server\fP,\fP\ ops\fP,\fP\ port\fP,\fP\ perm\fP,\fP\ needfile )Initializes the interface given a pointer to a Styxserver structure.I server, a callback table of operations.I ops, a port number.I portto announce the file service onand the permissions.I permon the root directory. The default permission is 0555 (read and execute for user, group and others) if the latter is specified as -1. If the last argument.I needfileis set to true, the styx library will check that each path number it deals withhas a corresponding file associated with it and, if it hasn't, it will issue a"file does not exist" message automatically. In case of an error, the error message isreturned, otherwise nil is returned to indicate success.By default, files are owned by.BR inferno ;.I styxsetownercan be called before.I styxinitto make.I userthe default owner of files..TP.BI styxwait(server )Waits for communication from a client. Return value as above..TP.BI styxprocess(server )Processes the client message after a successful call to.I styxwait .This may result in calls to the functions in the table provided to.I styxinit .Return value as above..TP.BI styxend(server )End all file service. Return value as above..TP .BI styxclient(server )Returns the client whose request is currently being processed..PPThe next set of functions allow the creation of a file system structure basedupon the.I Styxfilestructure. This contains a Dir structure .I ddescribing the properties of the file(defined in lib9.h) and pointers to other files in the file tree:.I parent,.I child,.I siblingand.I next .The.I reffieldcounts current references to the file. The.I openfield counts the current number of opens on the file. Finally the.I ufield allows further fields to be tagged onto each file. It is notused by the Styx server library..PPEach file must have a unique path number in the server. The root ofthe tree.I Qrootalways has path number zero. It's corresponding file is created during library initializationand placed in the.I rootfield of the server structure. All other files must be supplied with a path numberto identify them. Files are created/deleted as follows:.TP.BI styxaddfile(server\fP,\fP\ ppath\fP,\fP\ path\fP,\fP\ name\fP,\fP\ mode\fP,\fP\ owner )Add a new file (ie non-directory) with the given path.I path, name.I name, mode.I modeand owner.I ownerto the directory identified by the path.I ppath .If.I pathis -1 the library will generate a unique path number instead.Returns nil if the parent file with path.I ppathdoes not exist, if the parent is not a directory, if the path number.I pathalready is assigned to a file or if the parent already contains a file of name.I name ..TP.BI styxadddir(server\fP,\fP\ ppath\fP,\fP\ path\fP,\fP\ name\fP,\fP\ mode\fP,\fP\ owner )Add a new directory with the given path.I path, name.I name, mode.I modeand owner.I ownerto the directory identified by the path.I ppath .Returns nil in the same circumstances as.I styxaddfile ..TP.BI styxrmfile(server\fP,\fP\  path )Remove the file or directory with path.I pathfrom the file server tree. If the file is a directory, it's contents will be recursivelyremoved. If the file does not exist, -1 is returned, otherwise 0 is returned forsuccess..TP.BI styxfindfile(server\fP,\fP\  path )Return the file structure corresponding to the file or directory with path.I path .Nil is returned if the file does not exist..PPIf the file system is created in this way the Styx library will check read/write/executepermissions, check for invalid uses of files and check that path numbers existin the file system (see.I styxinitfor the latter). If it's not feasible to do this (for instance if there is a more suitableway of describing the file system in question), then all file checking must bedone as part of the callback functions below..PPThe library provides a callback mechanism so that the implementer of thefile server can take corresponding action when a particular request is madeof the server. All of these functions may return an error message which willbe communicated back to the client. Otherwise they should return nil toindicate the success of the operation. Any of these functions may be nil in which case the libraryperforms a default operation which will be described below. These routines usethe.I Qidstructure defined in lib9.h to describe files. This structure contains the path number(.I path), a version number(.I vers) typically zero and a type(.I type) which indicates whether the file is a directory, append-only etc..TP.BI newclient(c )Called whenever a new client connects to the server. The Client structure.I ccontains mainly private data but the.I unamefield contains a user name and the.I anamefield an attach name if required. The.I ufield may be used to tag further data onto each client. It is not used bythe Styx server library..TP.BI freeclient(c )Called whenever a client disconnects from the server..TP.BI attach(uname\fP,\fP\ aname )Called when a client user first mounts the file server. The.I unameis the user id and.I anameis typically the file tree to access if the server provides a choice.The default action is to allow the attach to the root of the file system..TP.BI walk(qid\fP,\fP\ name )In a directory represented by.I qid, find a file member whose name is that given and place it's Qid in.I qid .The default action is to perform the walk using any directory structure provided..TP.BI open(qid\fP,\fP\ mode )Open the file represented by.I qidwith mode.I mode .The latter may be one of OREAD, OWRITE, ORDWR etc (see lib9.h). If the Qidof the newly opened file is different from that given (a file server may understandthe opening of a file called "new" say to signify the creation of a directory whoseQid is returned instead) place it's Qid in.I qid .The default action is to nominally allow the open..TP.BI create(qid\fP,\fP\ name\fP,\fP\ perm\fP,\fP\ mode )Create a file in the directory given by.I qidwith name.I name, permissions.I permand mode.I mode .Place the Qid of the newly created file in.I qid .The default action is to issue a permission denied message..TP.BI read(qid\fP,\fP\ buf\fP,\fP\ n\fP,\fP\ offset )Read.I nbytes of the file represented by.I qidat offset.I offsetand place the result in.I buf. Place in.I nthe actual number of bytes read.The default action is to read directories but to issue permission denied on ordinaryfiles..TP.BI write(qid\fP,\fP\ buf\fP,\fP\ n\fP,\fP\ offset )Write.I nbytes to the file represented by.I qidat offset.I offsetfrom the buffer.I buf. Place in.I nthe actual number of bytes written.The default action is to issue permission denied..TP.BI close(qid\fP,\fP\ mode )Close the file represented by.I qid .The mode it was originally opened with is given by.I mode .The default action is to allow the close..TP.BI remove(qid )Remove the file represented by.I qid .The default action is to issue a permission denied message..TP.BI stat(qid\fP,\fP\ d )Place the information for the file represented by.I qidin the Dir structure(see lib9.h).I d .The default action is to allow the stat using any information in the file tree..TP.BI wstat(qid\fP,\fP\ d )Update the information for the file represented by.I qidaccording to the Dir structure.I d .The default action is to disallow this with a permission denied message..PPA small number of utility functions are provided:.TP.BI styxperm(file\fP,\fP\ uid\fP,\fP\ mode )Does the file/directory.I fileallow the user.I uidthe permission given by.I mode .For example use.I OREADfor read permission,.I OWRITEfor write permission and.I ORDWRfor both..TP.BI styxreadstr(off\fP,\fP\ buf\fP,\fP\ n\fP,\fP\ str )Read.I nbytes of data from the string.I strat offset.I offand place the result in.I buf .Returns the actual number of bytes read..TP.BI styxqid(path\fP,\fP\ isdir )Returns a typical Qid structure with the given path number.I pathand whether the Qid is for a directory.I isdir ..TP.BI styxmalloc(n )Allocate.I nbytes of memory and return it..TP.BI styxfree(p )Free the memory pointed to by.I p ..TP.BI styxdebug()Print out some of the actions of the server..SH EXAMPLE.PPA very small file server example is illustrated. First the include files and globals..PP.EX	#include <lib9.h>	#include "styxserver.h" 	int nq;	Styxserver *server;.EE.PPThe main processing loop:.PP.EX	main(int argc, char **argv)	{		Styxserver s; 		server = &s;		styxinit(&s, &ops, "6701", 100, 0555, 0);		myinit(&s);		for(;;) {			styxwait(&s);			styxprocess(&s);		}		return 0;	}.EE.PPHere the port number is 6701 and the root file permissions are 0555 - no writepermission for anyone which implies that files and directories cannot be created in the root directory..PPThe creation of the directory tree:.PP.EX	myinit(Styxserver *s)	{		styxaddfile(s, Qroot, 1, "fred", 0664, "inferno");		styxaddfile(s, Qroot, 2, "joe", 0664, "inferno");		styxadddir(s, Qroot, 3, "adir", 0775, "inferno");		styxaddfile(s, 3, 4, "bill", 0664, "inferno");		styxadddir(s, Qroot, 5, "new", 0775, "inferno");		styxadddir(s, 5, 6, "cdir", 0775, "inferno");		styxaddfile(s, 6, 7, "cfile", 0664, "inferno");		nq = 8;	}.EE.PPThis creates two files.I fredand.I joeand two directories.I adirand.I newat the top level..I adircontains a file called.I billand.I newcontains a directory called.I cdirwhich contains a file called.I cfile .Note that each new path number is unique..PPThe callback functions:.PP.EX	Styxops ops = {		nil,			/* newclient */		nil,			/* freeclient */ 		nil,			/* attach */		nil,			/* walk */		nil,			/* open */		mycreate,		/* create */		myread,		/* read */		nil,			/* write */		nil,			/* close */		myremove,	/* remove */		nil,			/* stat */		nil,			/* wstat */	};

Lines 1–500 of 623.