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