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