]> git.lizzy.rs Git - plan9front.git/blob - sys/man/2/9p
/sys/man/*/*: fix perms (sorry)
[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 .BR 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 (see
364 .IR 9pqueue (2)).
365 .PP
366 .I Srvrelease
367 temporarily releases the calling process from the server
368 loop and if neccesary spawns a new process to handle 9p 
369 requests. When released, the process can do blocking work
370 that would otherwise halt processing of 9p requests.
371 .I Srvacquire
372 rejoins the calling process with the server loop after
373 a srvrelease.
374 .PP
375 The constraints on the service functions are as follows.
376 These constraints are checked while the server executes.
377 If a service function fails to do something it ought to have,
378 .I srv
379 will call
380 .I endsrv
381 and then abort.
382 .TP 
383 .I Auth
384 If authentication is desired,
385 the
386 .I auth
387 function should record that
388 .IB r -> afid
389 is the new authentication fid and
390 set 
391 .IB r -> afid -> qid
392 and
393 .IR ofcall.qid .
394 .I Auth
395 may be nil, in which case it will be treated as having
396 responded with the error
397 .RI `` "argv0: authentication not required" ,''
398 where
399 .I argv0
400 is the program name variable as set by
401 .I ARGBEGIN
402 (see
403 .IR arg (2)).
404 .TP
405 .I Attach
406 The
407 .I attach
408 function should check the authentication state of
409 .I afid
410 if desired,
411 and set
412 .IB r -> fid -> qid
413 and
414 .I ofcall.qid
415 to the qid of the file system root.
416 .I Attach
417 may be nil only if file trees are in use;
418 in this case, the qid will be filled from the root
419 of the tree, and no authentication will be done.
420 .TP
421 .I Walk
422 If file trees are in use,
423 .I walk
424 is handled internally, and 
425 .IB srv -> walk
426 is never called.
427 .IP
428 If file trees are not in use,
429 .I walk
430 should consult
431 .IB r -> ifcall . wname
432 and
433 .IB r -> ifcall . nwname \fR,
434 filling in 
435 .IB ofcall . qid
436 and
437 .IB ofcall . nqid \fR,
438 and also copying any necessary 
439 .I aux
440 state from
441 .IB r -> fid
442 to
443 .IB r -> newfid
444 when the two are different.
445 As long as
446 .I walk 
447 sets
448 .IB ofcall . nqid
449 appropriately, it can
450 .I respond
451 with a nil error string even when 9P
452 demands an error 
453 .RI ( e.g. ,
454 in the case of a short walk);
455 the library detects error conditions and handles them appropriately.
456 .IP
457 Because implementing the full walk message is intricate and
458 prone to error, the helper routine
459 .I walkandclone
460 will handle the request given pointers to two functions
461 .I walk1
462 and (optionally)
463 .I clone .
464 .IR Clone ,
465 if non-nil, is called to signal the creation of
466 .I newfid
467 from
468 .IR oldfid .
469 Typically a 
470 .I clone
471 routine will copy or increment a reference count in
472 .IR oldfid 's
473 .I aux
474 element.
475 .I Walk1
476 should walk
477 .I fid
478 to
479 .IR name ,
480 initializing
481 .IB fid -> qid
482 to the new path's qid.
483 Both should return nil
484 on success or an error message on error.
485 .I Walkandclone
486 will call
487 .I respond
488 after handling the request.
489 .TP
490 .I Walk1\fR, \fPClone
491 If the client provides functions
492 .IB srv -> walk1
493 and (optionally)
494 .IB srv -> clone \fR,
495 the 9P service loop will call
496 .I walkandclone
497 with these functions to handle the request.
498 Unlike the
499 .I walk1
500 above,
501 .IB srv -> walk1
502 must fill in both
503 .IB fid -> qid
504 and
505 .BI * qid
506 with the new qid on a successful walk.
507 .TP
508 .I Open
509 If file trees are in use, the file
510 metadata will be consulted on open, create, remove, and wstat
511 to see if the requester has the appropriate permissions.
512 If not, an error will be sent back without consulting a service function.
513 .IP
514 If not using file trees or the user has the appropriate permissions,
515 .I open
516 is called with
517 .IB r -> ofcall . qid
518 already initialized to the one stored in the 
519 .B Fid
520 structure (that is, the one returned in the previous walk).
521 If the qid changes, both should be updated.
522 .TP
523 .I Create
524 The
525 .I create
526 function must fill in
527 both
528 .IB r -> fid -> qid
529 and
530 .IB r -> ofcall . qid
531 on success.
532 When using file trees,
533 .I create
534 should allocate a new 
535 .B File
536 with
537 .IR createfile ;
538 note that
539 .I createfile
540 may return nil (because, say, the file already exists).
541 If the 
542 .I create
543 function is nil,
544 .I srv 
545 behaves as though it were a function that always responded
546 with the error ``create prohibited''.
547 .TP
548 .I Remove
549 .I Remove
550 should mark the file as removed, whether
551 by calling
552 .I removefile
553 when using file trees, or by updating an internal data structure.
554 In general it is not a good idea to clean up the
555 .I aux
556 information associated with the corresponding
557 .B File
558 at this time, to avoid memory errors if other
559 fids have references to that file.
560 Instead, it is suggested that 
561 .I remove
562 simply mark the file as removed (so that further
563 operations on it know to fail) and wait until the
564 file tree's destroy function is called to reclaim the
565 .I aux
566 pointer.
567 If not using file trees, it is prudent to take the
568 analogous measures.
569 If
570 .I remove
571 is not provided, all remove requests will draw
572 ``remove prohibited'' errors.
573 .TP
574 .I Read
575 The
576 .I read
577 function must be provided; it fills
578 .IB r -> ofcall . data
579 with at most
580 .IB r -> ifcall . count
581 bytes of data from offset
582 .IB r -> ifcall . offset
583 of the file.
584 It also sets
585 .IB r -> ofcall . count
586 to the number of bytes being returned.
587 If using file trees, 
588 .I srv
589 will handle reads of directories internally, only
590 calling
591 .I read
592 for requests on files.
593 .I Readstr
594 and
595 .I readbuf
596 are useful for satisfying read requests on a string or buffer.
597 Consulting the request in
598 .IB r -> ifcall \fR,
599 they fill
600 .IB r -> ofcall . data
601 and set
602 .IB r -> ofcall . count \fR;
603 they do not call
604 .IB respond .
605 Similarly,
606 .I dirread9p
607 can be used to handle directory reads in servers
608 not using file trees.
609 The passed
610 .I gen
611 function will be called as necessary to
612 fill
613 .I dir
614 with information for the
615 .IR n th
616 entry in the directory.
617 The string pointers placed in
618 .I dir
619 should be fresh copies
620 made with
621 .IR estrdup9p ;
622 they will be freed by
623 .I dirread9p
624 after each successful call to 
625 .IR gen .
626 .I Gen
627 should return zero if it successfully filled
628 .IR dir ,
629 minus one on end of directory.
630 .TP
631 .I Write
632 The 
633 .I write 
634 function is similar but need not be provided.
635 If it is not, all writes will draw 
636 ``write prohibited'' errors.
637 Otherwise, 
638 .I write
639 should attempt to write the
640 .IB r -> ifcall . count
641 bytes of 
642 .IB r -> ifcall . data
643 to offset
644 .IB r -> ifcall . offset
645 of the file, setting
646 .IB r -> ofcall . count
647 to the number of bytes actually written.
648 Most programs consider it an error to
649 write less than the requested amount.
650 .TP
651 .I Stat
652 .I Stat
653 should fill
654 .IB r -> d
655 with the stat information for
656 .IB r -> fid \fR.
657 If using file trees, 
658 .IB r -> d
659 will have been initialized with the stat info from 
660 the tree, and
661 .I stat
662 itself may be nil.
663 .TP
664 .I Wstat
665 The
666 .I wstat
667 consults 
668 .IB r -> d
669 in changing the metadata for
670 .IB r -> fid
671 as described in
672 .IR stat (5).
673 When using file trees,
674 .I srv
675 will take care to check that the request satisfies
676 the permissions outlined in
677 .IR stat (5).
678 Otherwise 
679 .I wstat
680 should take care to enforce permissions
681 where appropriate.
682 .TP
683 .I Flush
684 Servers that always call 
685 .I respond
686 before returning from the service functions
687 need not provide a 
688 .I flush
689 implementation:
690 .I flush
691 is only necessary in programs
692 that arrange for 
693 .I respond
694 to be called asynchronously.
695 .I Flush
696 should cause the request
697 .IB r -> oldreq
698 to be cancelled or hurried along.
699 If
700 .I oldreq
701 is cancelled, this should be signalled by calling
702 .I respond
703 on
704 .I oldreq
705 with error string
706 .RB ` interrupted '.
707 .I Flush
708 must respond to
709 .I r
710 with a nil error string.
711 .I Flush
712 may respond to
713 .I r
714 before forcing a response to
715 .IB r -> oldreq \fR.
716 In this case, the library will delay sending
717 the
718 .I Rflush
719 message until the response to 
720 .IB r -> oldreq
721 has been sent.
722 .PD
723 .PP
724 .IR Destroyfid ,
725 .IR destroyreq ,
726 .IR start ,
727 and
728 .I end
729 are auxiliary functions, not called in direct response to 9P requests.
730 .TP
731 .I Destroyfid
732 When a 
733 .BR Fid 's
734 reference count drops to zero
735 .RI ( i.e., 
736 it has been clunked and there are no outstanding
737 requests referring to it),
738 .I destroyfid
739 is called to allow the program to dispose
740 of the
741 .IB fid -> aux
742 pointer.
743 .TP
744 .I Destroyreq
745 Similarly, when a
746 .BR Req 's
747 reference count drops to zero
748 .RI ( i.e. ,
749 it has been handled via
750 .I respond
751 and other outstanding pointers to it have been closed),
752 .I destroyreq
753 is called to allow the program to dispose of the
754 .IB r -> aux
755 pointer.
756 .TP
757 .I Start
758 This gets called (from the forked service process) 
759 prior entering the 9P service loop.
760 .TP
761 .I End
762 Once the 9P service loop has finished
763 (end of file been reached on the service pipe
764 or a bad message has been read),
765 .I end
766 is called (if provided) to allow any final cleanup.
767 For example, it was used by the Palm Pilot synchronization
768 file system (never finished) to gracefully terminate the serial conversation once
769 the file system had been unmounted.
770 After calling
771 .IR end ,
772 the service loop (which runs in a separate process
773 from its caller) terminates using 
774 .I _exits
775 (see
776 .IR exits (2)).
777 .PD
778 .PP
779 If the 
780 .B chatty9p
781 flag is at least one,
782 a transcript of the 9P session is printed
783 on standard error.
784 If the
785 .B chatty9p
786 flag is greater than one,
787 additional unspecified debugging output is generated.
788 By convention, servers written using this library
789 accept the
790 .B -D
791 option to increment
792 .BR chatty9p .
793 .SH EXAMPLES
794 .IR Archfs (4),
795 .IR cdfs (4),
796 .IR nntpfs (4),
797 .IR snap (4),
798 and
799 .B /sys/src/lib9p/ramfs.c
800 are good examples of simple single-threaded file servers.
801 .PP
802 In general, the
803 .B File
804 interface is appropriate for maintaining arbitrary file trees (as in
805 .IR ramfs ).
806 The 
807 .B File
808 interface is best avoided when the 
809 tree structure is easily generated as necessary;
810 this is true when the tree is highly structured (as in
811 .I cdfs
812 and
813 .IR nntpfs )
814 or is maintained elsewhere.
815 .SH SOURCE
816 .B /sys/src/lib9p
817 .SH SEE ALSO
818 .IR 9pfid (2),
819 .IR 9pfile (2),
820 .IR 9pqueue (2),
821 .IR srv (3),
822 .IR shr (3),
823 .IR intro (5)
824 .SH BUGS
825 The switch to 9P2000 was taken as an opportunity to tidy
826 much of the interface; we promise to avoid such gratuitous change
827 in the future.