Blob


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