blob: 9e75573517fe3a8e4ca84c63b52ccbfabdc7a52b [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH 9P 3
2.SH NAME
3Srv,
4dirread9p,
5emalloc9p,
6erealloc9p,
7estrdup9p,
8postfd,
9postmountsrv,
10readbuf,
11readstr,
12respond,
13threadpostmountsrv,
14srv \- 9P file service
15.SH SYNOPSIS
16.ft L
17.nf
18#include <u.h>
19#include <libc.h>
20#include <fcall.h>
21#include <thread.h>
22#include <9p.h>
23.fi
24.PP
25.ft L
26.nf
27.ta \w'\fL1234'u +\w'\fLTree* 'u
28typedef struct Srv {
29 Tree* tree;
30
31 void (*attach)(Req *r);
32 void (*auth)(Req *r);
33 void (*open)(Req *r);
34 void (*create)(Req *r);
35 void (*read)(Req *r);
36 void (*write)(Req *r);
37 void (*remove)(Req *r);
38 void (*flush)(Req *r);
39 void (*stat)(Req *r);
40 void (*wstat)(Req *r);
41 void (*walk)(Req *r);
42
43 char* (*walk1)(Fid *fid, char *name, Qid *qid);
44 char* (*clone)(Fid *oldfid, Fid *newfid);
45
46 void (*destroyfid)(Fid *fid);
47 void (*destroyreq)(Req *r);
48 void (*end)(Srv *s);
49 void* aux;
50
51 int infd;
52 int outfd;
53 int srvfd;
54 int nopipe;
55} Srv;
56.fi
57.PP
58.nf
59.ft L
60.ta \w'\fLvoid* 'u
61int srv(Srv *s)
62void postmountsrv(Srv *s, char *name, char *mtpt, int flag)
63void threadpostmountsrv(Srv *s, char *name, char *mtpt, int flag)
64int postfd(char *srvname, int fd)
65void respond(Req *r, char *error)
66ulong readstr(Req *r, char *src)
67ulong readbuf(Req *r, void *src, ulong nsrc)
68typedef int Dirgen(int n, Dir *dir, void *aux)
69void dirread9p(Req *r, Dirgen *gen, void *aux)
70void walkandclone(Req *r, char *(*walk1)(Fid *old, char *name, void *v),
71 char *(*clone)(Fid *old, Fid *new, void *v), void *v)
72.fi
73.PP
74.nf
75.ft L
76.ta \w'\fLvoid* 'u
77void* emalloc9p(ulong n)
78void* erealloc9p(void *v, ulong n)
79char* estrdup9p(char *s)
80.fi
81.PP
82.nf
83.ft L
84extern int chatty9p;
85.fi
86.SH DESCRIPTION
87The function
88.I srv
89serves a 9P session by reading requests from
90.BR s->infd ,
91dispatching them to the function pointers kept in
92.BR Srv ,
93and
94writing the responses to
95.BR s->outfd .
96(Typically,
97.I postmountsrv
98or
99.I threadpostmountsrv
100initializes the
101.B infd
102and
103.B outfd
104structure members. See the description below.)
105.PP
106.B Req
107and
108.B Fid
109structures are allocated one-to-one with uncompleted
110requests and active fids, and are described in
rscbf8a59f2004-04-11 03:42:27 +0000111.IR 9pfid (3).
rsccfa37a72004-04-10 18:53:55 +0000112.PP
113The behavior of
114.I srv
115depends on whether there is a file tree
116(see
rscbf8a59f2004-04-11 03:42:27 +0000117.IR 9pfile (3))
rsccfa37a72004-04-10 18:53:55 +0000118associated with the server, that is,
119whether the
120.B tree
121element is nonzero.
122The differences are made explicit in the
123discussion of the service loop below.
124The
125.B aux
126element is the client's, to do with as it pleases.
127.PP
128.I Srv
129does not return until the 9P conversation is finished.
130Since it is usually run in a separate process so that
131the caller can exit, the service loop has little chance
132to return gracefully on out of memory errors.
133It calls
134.IR emalloc9p ,
135.IR erealloc9p ,
136and
137.I estrdup9p
138to obtain its memory.
139The default implementations of these functions
140act as
141.IR malloc ,
142.IR realloc ,
143and
144.I strdup
145but abort the program if they run out of memory.
146If alternate behavior is desired, clients can link against
147alternate implementations of these functions.
148.PP
149.I Postmountsrv
150and
151.I threadpostmountsrv
152are wrappers that create a separate process in which to run
153.IR srv .
154They do the following:
155.IP
156If
157.IB s -> nopipe
158is zero (the common case),
159initialize
160.IB s -> infd
161and
162.IB s -> outfd
163to be one end of a freshly allocated pipe,
164with
165.IB s -> srvfd
166initialized as the other end.
167.IP
168If
169.B name
170is non-nil, call
171.BI postfd( s -> srvfd ,
172.IB name )
173to post
174.IB s -> srvfd
175as
176.BI /srv/ name .
177.IP
178Fork a child process via
179.I rfork
180(see
rscbf8a59f2004-04-11 03:42:27 +0000181.IR fork (3))
rsccfa37a72004-04-10 18:53:55 +0000182or
183.I procrfork
184(see
rscbf8a59f2004-04-11 03:42:27 +0000185.IR thread (3)),
rsccfa37a72004-04-10 18:53:55 +0000186using the
187.BR RFFDG ,
188.RR RFNOTEG ,
189.BR RFNAMEG ,
190and
191.BR RFMEM
192flags.
193The child process
194calls
195.IB close( s -> srvfd )
196and then
197.IB srv( s ) \fR;
198it will exit once
199.I srv
200returns.
201.IP
202If
203.I mtpt
204is non-nil,
205call
206.BI amount( s -> srvfd,
207.IB mtpt ,
208.IB flag ,
209\fB"")\fR;
210otherwise, close
211.IB s -> srvfd \fR.
212.IP
213The parent returns to the caller.
214.LP
215If any error occurs during
216this process, the entire process is terminated by calling
217.I sysfatal
218(see
rscbf8a59f2004-04-11 03:42:27 +0000219.IR perror (3)).
rsccfa37a72004-04-10 18:53:55 +0000220.SS Service functions
221The functions in a
222.B Srv
223structure named after 9P transactions
224are called to satisfy requests as they arrive.
225If a function is provided, it
226.I must
227arrange for
228.I respond
229to be called when the request is satisfied.
230The only parameter of each service function
231is a
232.B Req*
233parameter (say
234.IR r ).
235The incoming request parameters are stored in
236.IB r -> ifcall \fR;
237.IB r -> fid
238and
239.IB r -> newfid
240are pointers to
241.B Fid
242structures corresponding to the
243numeric fids in
244.IB r -> ifcall \fR;
245similarly,
246.IB r -> oldreq
247is the
248.B Req
249structure corresponding to
250.IB r -> ifcall.oldtag \fR.
251The outgoing response data should be stored in
252.IB r -> ofcall \fR.
253The one exception to this rule is that
254.I stat
255should fill in
256.IB r -> d
257rather than
258.IB r -> ofcall.stat \fR:
259the library will convert the structure into the machine-independent
260wire representation.
261Similarly,
262.I wstat
263may consult
264.IB r -> d
265rather than decoding
266.IB r -> ifcall . stat
267itself.
268When a request has been handled,
269.I respond
270should be called with
271.I r
272and an error string.
273If the request was satisfied successfully, the error
274string should be a nil pointer.
275Note that it is permissible for a function to return
276without itself calling
277.IR respond ,
278as long as it has arranged for
279.I respond
280to be called at some point in the future
281by another proc sharing its address space,
282but see the discussion of
283.I flush
284below.
285Once
286.I respond
287has been called, the
288.B Req*
289as well as any pointers it once contained must
290be considered freed and not referenced.
291.PP
292If the service loop detects an error in a request
293(e.g., an attempt to reuse an extant fid, an open of
294an already open fid, a read from a fid opened for write, etc.)
295it will reply with an error without consulting
296the service functions.
297.PP
298The service loop provided by
299.I srv
300(and indirectly by
301.I postmountsrv
302and
303.IR threadpostmountsrv )
304is single-threaded.
305If it is expected that some requests might
306block, arranging for alternate processes
307to handle them is suggested.
308.PP
309The constraints on the service functions are as follows.
310These constraints are checked while the server executes.
311If a service function fails to do something it ought to have,
312.I srv
313will call
314.I endsrv
315and then abort.
316.TP
317.I Auth
318If authentication is desired,
319the
320.I auth
321function should record that
322.I afid
323is the new authentication fid and
324set
325.I afid->qid
326and
327.IR ofcall.qid .
328.I Auth
329may be nil, in which case it will be treated as having
330responded with the error
331.RI `` "argv0: authentication not required" ,''
332where
333.I argv0
334is the program name variable as set by
335.I ARGBEGIN
336(see
rscbf8a59f2004-04-11 03:42:27 +0000337.IR arg (3)).
rsccfa37a72004-04-10 18:53:55 +0000338.TP
339.I Attach
340The
341.I attach
342function should check the authentication state of
343.I afid
344if desired,
345and set
346.IB r -> fid -> qid
347and
348.I ofcall.qid
349to the qid of the file system root.
350.I Attach
351may be nil only if file trees are in use;
352in this case, the qid will be filled from the root
353of the tree, and no authentication will be done.
354.TP
355.I Walk
356If file trees are in use,
357.I walk
358is handled internally, and
359.IB srv -> walk
360is never called.
361.IP
362If file trees are not in use,
363.I walk
364should consult
365.IB r -> ifcall . wname
366and
367.IB r -> ifcall . nwname \fR,
368filling in
369.IB ofcall . qid
370and
371.IB ofcall . nqid \fR,
372and also copying any necessary
373.I aux
374state from
375.IB r -> fid
376to
377.IB r -> newfid
378when the two are different.
379As long as
380.I walk
381sets
382.IB ofcall . nqid
383appropriately, it can
384.I respond
385with a nil error string even when 9P
386demands an error
387.RI ( e.g. ,
388in the case of a short walk);
389the library detects error conditions and handles them appropriately.
390.IP
391Because implementing the full walk message is intricate and
392prone to error, the helper routine
393.I walkandclone
394will handle the request given pointers to two functions
395.I walk1
396and (optionally)
397.I clone .
398.IR Clone ,
399if non-nil, is called to signal the creation of
400.I newfid
401from
402.IR oldfid .
403Typically a
404.I clone
405routine will copy or increment a reference count in
406.IR oldfid 's
407.I aux
408element.
409.I Walk1
410should walk
411.I fid
412to
413.IR name ,
414initializing
415.IB fid -> qid
416to the new path's qid.
417Both should return nil
418on success or an error message on error.
419.I Walkandclone
420will call
421.I respond
422after handling the request.
423.TP
424.I Walk1\fR, \fPClone
425If the client provides functions
426.IB srv -> walk1
427and (optionally)
428.IB srv -> clone \fR,
429the 9P service loop will call
430.I walkandclone
431with these functions to handle the request.
432Unlike the
433.I walk1
434above,
435.IB srv -> walk1
436must fill in both
437.IB fid -> qid
438and
439.BI * qid
440with the new qid on a successful walk.
441.TP
442.I Open
443If file trees are in use, the file
444metadata will be consulted on open, create, remove, and wstat
445to see if the requester has the appropriate permissions.
446If not, an error will be sent back without consulting a service function.
447.PP
448If not using file trees or the user has the appropriate permissions,
449.I open
450is called with
451.IB r -> ofcall . qid
452already initialized to the one stored in the
453.B Fid
454structure (that is, the one returned in the previous walk).
455If the qid changes, both should be updated.
456.TP
457.I Create
458The
459.I create
460function must fill in
461both
462.IB r -> fid -> qid
463and
464.IB r -> ofcall . qid
465on success.
466When using file trees,
467.I create
468should allocate a new
469.B File
470with
471.IR createfile ;
472note that
473.I createfile
474may return nil (because, say, the file already exists).
475If the
476.I create
477function is nil,
478.I srv
479behaves as though it were a function that always responded
480with the error ``create prohibited''.
481.TP
482.I Remove
483.I Remove
484should mark the file as removed, whether
485by calling
486.I removefile
487when using file trees, or by updating an internal data structure.
488In general it is not a good idea to clean up the
489.I aux
490information associated with the corresponding
491.B File
492at this time, to avoid memory errors if other
493fids have references to that file.
494Instead, it is suggested that
495.I remove
496simply mark the file as removed (so that further
497operations on it know to fail) and wait until the
498file tree's destroy function is called to reclaim the
499.I aux
500pointer.
501If not using file trees, it is prudent to take the
502analogous measures.
503If
504.I remove
505is not provided, all remove requests will draw
506``remove prohibited'' errors.
507.TP
508.I Read
509The
510.I read
511function must be provided; it fills
512.IB r -> ofcall . data
513with at most
514.IB r -> ifcall . count
515bytes of data from offset
516.IB r -> ifcall . offset
517of the file.
518It also sets
519.IB r -> ofcall . count
520to the number of bytes being returned.
521If using file trees,
522.I srv
523will handle reads of directories internally, only
524calling
525.I read
526for requests on files.
527.I Readstr
528and
529.I readbuf
530are useful for satisfying read requests on a string or buffer.
531Consulting the request in
532.IB r -> ifcall \fR,
533they fill
534.IB r -> ofcall . data
535and set
536.IB r -> ofcall . count \fR;
537they do not call
538.IB respond .
539Similarly,
540.I dirread9p
541can be used to handle directory reads in servers
542not using file trees.
543The passed
544.I gen
545function will be called as necessary to
546fill
547.I dir
548with information for the
549.IR n th
550entry in the directory.
551The string pointers placed in
552.I dir
553should be fresh copies
554made with
555.IR estrdup9p ;
556they will be freed by
557.I dirread9p
558after each successful call to
559.IR gen .
560.I Gen
561should return zero if it successfully filled
562.IR dir ,
563minus one on end of directory.
564.TP
565.I Write
566The
567.I write
568function is similar but need not be provided.
569If it is not, all writes will draw
570``write prohibited'' errors.
571Otherwise,
572.I write
573should attempt to write the
574.IB r -> ifcall . count
575bytes of
576.IB r -> ifcall . data
577to offset
578.IB r -> ifcall . offset
579of the file, setting
580.IB r -> ofcall . count
581to the number of bytes actually written.
582Most programs consider it an error to
583write less than the requested amount.
584.TP
585.I Stat
586.I Stat
587should fill
588.IB r -> d
589with the stat information for
590.IB r -> fid \fR.
591If using file trees,
592.IB r -> d
593will have been initialized with the stat info from
594the tree, and
595.I stat
596itself may be nil.
597.TP
598.I Wstat
599The
600.I wstat
601consults
602.IB r -> d
603in changing the metadata for
604.IB r -> fid
605as described in
606.IR stat (5).
607When using file trees,
608.I srv
609will take care to check that the request satisfies
610the permissions outlined in
611.IR stat (5).
612Otherwise
613.I wstat
614should take care to enforce permissions
615where appropriate.
616.TP
617.I Flush
618Single-threaded servers, which always call
619.I respond
620before returning from the service functions,
621need not provide a
622.I flush
623implementation:
624.I flush
625is only necessary in multithreaded programs,
626which arrange for
627.I respond
628to be called asynchronously.
629.I Flush
630should cause the request
631.IB r -> oldreq
632to be cancelled or hurried along.
633If
634.I oldreq
635is cancelled, this should be signalled by calling
636.I respond
637on
638.I oldreq
639with error string
640.RB ` interrupted '.
641.I Flush
642must respond to
643.I r
644with a nil error string.
645.I Flush
646may respond to
647.I r
648before forcing a response to
649.IB r -> oldreq \fR.
650In this case, the library will delay sending
651the
652.I Rflush
653message until the response to
654.IB r -> oldreq
655has been sent.
656.PD
657.PP
658.IR Destroyfid ,
659.IR destroyreq ,
660and
661.I end
662are auxiliary functions, not called in direct response to 9P requests.
663.TP
664.I Destroyfid
665When a
666.BR Fid 's
667reference count drops to zero
668.RI ( i.e.,
669it has been clunked and there are no outstanding
670requests referring to it),
671.I destroyfid
672is called to allow the program to dispose
673of the
674.IB fid -> aux
675pointer.
676.TP
677.I Destroyreq
678Similarly, when a
679.BR Req 's
680reference count drops to zero
681.RI ( i.e. ,
682it has been handled via
683.I respond
684and other outstanding pointers to it have been closed),
685.I destroyreq
686is called to allow the program to dispose of the
687.IB r -> aux
688pointer.
689.TP
690.I End
691Once the 9P service loop has finished
692(end of file been reached on the service pipe
693or a bad message has been read),
694.I end
695is called (if provided) to allow any final cleanup.
696For example, it was used by the Palm Pilot synchronization
697file system (never finished) to gracefully terminate the serial conversation once
698the file system had been unmounted.
699After calling
700.IR end ,
701the service loop (which runs in a separate process
702from its caller) terminates using
703.I _exits
704(see
rscbf8a59f2004-04-11 03:42:27 +0000705.IR exits (3)).
rsccfa37a72004-04-10 18:53:55 +0000706.PD
707.PP
708If the
709.B chatty9p
710flag is at least one,
711a transcript of the 9P session is printed
712on standard error.
713If the
714.B chatty9p
715flag is greater than one,
716additional unspecified debugging output is generated.
717By convention, servers written using this library
718accept the
719.B -D
720option to increment
721.BR chatty9p .
722.SH EXAMPLES
723.IR Archfs (4),
724.IR cdfs (4),
725.IR nntpfs (4),
726.IR snap (4),
727and
rscb5fdffe2004-04-19 19:22:56 +0000728.B /usr/local/plan9/src/lib9p/ramfs.c
rsccfa37a72004-04-10 18:53:55 +0000729are good examples of simple single-threaded file servers.
730.IR Webfs (4)
731and
732.I sshnet
733(see
734.IR ssh (1))
735are good examples of multithreaded file servers.
736.PP
737In general, the
738.B File
739interface is appropriate for maintaining arbitrary file trees (as in
740.IR ramfs ).
741The
742.B File
743interface is best avoided when the
744tree structure is easily generated as necessary;
745this is true when the tree is highly structured (as in
746.I cdfs
747and
748.IR nntpfs )
749or is maintained elsewhere.
750.SH SOURCE
rscb5fdffe2004-04-19 19:22:56 +0000751.B /usr/local/plan9/src/lib9p
rsccfa37a72004-04-10 18:53:55 +0000752.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000753.IR 9pfid (3),
754.IR 9pfile (3),
rsccfa37a72004-04-10 18:53:55 +0000755.IR srv (3),
756.IR intro (5)
757.SH BUGS
758The switch to 9P2000 was taken as an opportunity to tidy
759much of the interface; we promise to avoid such gratuitous change
760in the future.