Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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