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