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