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