]> git.lizzy.rs Git - plan9front.git/blob - sys/include/9p.h
lib9p: expose Srv.forker handler and srvforker(), threadsrvforker() and threadsrv...
[plan9front.git] / sys / include / 9p.h
1 #pragma src "/sys/src/lib9p"
2 #pragma lib "lib9p.a"
3
4 /*
5  * Maps from ulongs to void*s.
6  */
7 typedef struct Intmap   Intmap;
8
9 #pragma incomplete Intmap
10
11 Intmap* allocmap(void (*inc)(void*));
12 void            freemap(Intmap*, void (*destroy)(void*));
13 void*   lookupkey(Intmap*, ulong);
14 void*   insertkey(Intmap*, ulong, void*);
15 int             caninsertkey(Intmap*, ulong, void*);
16 void*   deletekey(Intmap*, ulong);
17
18 /*
19  * Fid and Request structures.
20  */
21 typedef struct Fid              Fid;
22 typedef struct Req              Req;
23 typedef struct Fidpool  Fidpool;
24 typedef struct Reqpool  Reqpool;
25 typedef struct File             File;
26 typedef struct Filelist Filelist;
27 typedef struct Tree             Tree;
28 typedef struct Readdir  Readdir;
29 typedef struct Srv Srv;
30 typedef struct Reqqueue Reqqueue;
31 typedef struct Queueelem Queueelem;
32
33 #pragma incomplete Filelist
34 #pragma incomplete Readdir
35
36 struct Queueelem
37 {
38         Queueelem *prev, *next;
39         void (*f)(Req *);
40 };
41
42 struct Reqqueue
43 {
44         QLock;
45         Rendez;
46         Queueelem;
47         int pid, flush;
48         Req *cur;
49 };
50
51 struct Fid
52 {
53         ulong   fid;
54         char            omode;  /* -1 = not open */
55         File*           file;
56         char*   uid;
57         Qid             qid;
58         void*   aux;
59
60 /* below is implementation-specific; don't use */
61         Readdir*        rdir;
62         Ref             ref;
63         Fidpool*        pool;
64         vlong   diroffset;
65         long            dirindex;
66 };
67
68 struct Req
69 {
70         ulong   tag;
71         void*   aux;
72         Fcall           ifcall;
73         Fcall           ofcall;
74         Dir             d;
75         Req*            oldreq;
76         Fid*            fid;
77         Fid*            afid;
78         Fid*            newfid;
79         Srv*            srv;
80         
81         Queueelem qu;
82
83 /* below is implementation-specific; don't use */
84         QLock   lk;
85         Ref             ref;
86         Reqpool*        pool;
87         uchar*  buf;
88         uchar   type;
89         uchar   responded;
90         char*   error;
91         void*   rbuf;
92         Req**   flush;
93         int             nflush;
94 };
95
96 /*
97  * Pools to maintain Fid <-> fid and Req <-> tag maps.
98  */
99
100 struct Fidpool {
101         Intmap  *map;
102         void            (*destroy)(Fid*);
103         Srv             *srv;
104 };
105
106 struct Reqpool {
107         Intmap  *map;
108         void            (*destroy)(Req*);
109         Srv             *srv;
110 };
111
112 Fidpool*        allocfidpool(void (*destroy)(Fid*));
113 void            freefidpool(Fidpool*);
114 Fid*            allocfid(Fidpool*, ulong);
115 Fid*            lookupfid(Fidpool*, ulong);
116 void            closefid(Fid*);
117 Fid*            removefid(Fidpool*, ulong);
118
119 Reqpool*        allocreqpool(void (*destroy)(Req*));
120 void            freereqpool(Reqpool*);
121 Req*            allocreq(Reqpool*, ulong);
122 Req*            lookupreq(Reqpool*, ulong);
123 void            closereq(Req*);
124 Req*            removereq(Reqpool*, ulong);
125
126 typedef int     Dirgen(int, Dir*, void*);
127 void            dirread9p(Req*, Dirgen*, void*);
128
129 /*
130  * File trees.
131  */
132 struct File {
133         Ref;
134         Dir;
135         File *parent;
136         void *aux;
137
138 /* below is implementation-specific; don't use */
139         RWLock;
140         Filelist *filelist;
141         Tree *tree;
142         int nchild;
143         int allocd;
144         int nxchild;
145         Ref readers;
146 };
147
148 struct Tree {
149         File *root;
150         void    (*destroy)(File *file);
151
152 /* below is implementation-specific; don't use */
153         Lock genlock;
154         ulong qidgen;
155 };
156
157 Tree*   alloctree(char*, char*, ulong, void(*destroy)(File*));
158 void            freetree(Tree*);
159 File*           createfile(File*, char*, char*, ulong, void*);
160 int             removefile(File*);
161 void            closefile(File*);
162 File*           walkfile(File*, char*);
163 Readdir*        opendirfile(File*);
164 long            readdirfile(Readdir*, uchar*, long, long);
165 void            closedirfile(Readdir*);
166
167 /*
168  * Kernel-style command parser
169  */
170 typedef struct Cmdbuf Cmdbuf;
171 typedef struct Cmdtab Cmdtab;
172 Cmdbuf*         parsecmd(char *a, int n);
173 void            respondcmderror(Req*, Cmdbuf*, char*, ...);
174 Cmdtab* lookupcmd(Cmdbuf*, Cmdtab*, int);
175 #pragma varargck argpos respondcmderr 3
176 struct Cmdbuf
177 {
178         char    *buf;
179         char    **f;
180         int     nf;
181 };
182
183 struct Cmdtab
184 {
185         int     index;  /* used by client to switch on result */
186         char    *cmd;   /* command name */
187         int     narg;   /* expected #args; 0 ==> variadic */
188 };
189
190 /*
191  * File service loop.
192  */
193 struct Srv {
194         Tree*   tree;
195         void            (*destroyfid)(Fid*);
196         void            (*destroyreq)(Req*);
197         void            (*start)(Srv*);
198         void            (*end)(Srv*);
199         void*   aux;
200
201         void            (*attach)(Req*);
202         void            (*auth)(Req*);
203         void            (*open)(Req*);
204         void            (*create)(Req*);
205         void            (*read)(Req*);
206         void            (*write)(Req*);
207         void            (*remove)(Req*);
208         void            (*flush)(Req*);
209         void            (*stat)(Req*);
210         void            (*wstat)(Req*);
211         void            (*walk)(Req*);
212         char*   (*clone)(Fid*, Fid*);
213         char*   (*walk1)(Fid*, char*, Qid*);
214
215         int             infd;
216         int             outfd;
217         int             srvfd;
218         char*   keyspec;
219
220 /* below is implementation-specific; don't use */
221         Fidpool*        fpool;
222         Reqpool*        rpool;
223         uint            msize;
224
225         uchar*  rbuf;
226         QLock   rlock;
227         uchar*  wbuf;
228         QLock   wlock;
229         
230         char*   addr;
231
232         QLock   slock;
233         Ref     sref;   /* srvwork procs */
234         Ref     rref;   /* requests in flight */
235
236         int     spid;   /* pid of srv() caller */
237
238         void    (*forker)(void (*)(void*), void*, int);
239         void    (*free)(Srv*);
240 };
241
242 void            srvforker(void (*)(void*), void*, int);
243 void            threadsrvforker(void (*)(void*), void*, int);
244
245 void            srv(Srv*);
246 void            postsrv(Srv*, char*);
247 void            postmountsrv(Srv*, char*, char*, int);
248 void            postsharesrv(Srv*, char*, char*, char*);
249 void            listensrv(Srv*, char*);
250
251 void            threadsrv(Srv*);
252 void            threadpostsrv(Srv*, char*);
253 void            threadpostmountsrv(Srv*, char*, char*, int);
254 void            threadpostsharesrv(Srv*, char*, char*, char*);
255 void            threadlistensrv(Srv *s, char *addr);
256
257 int             chatty9p;
258 void            respond(Req*, char*);
259 void            responderror(Req*);
260
261 /*
262  * Helper.  Assumes user is same as group.
263  */
264 int             hasperm(File*, char*, int);
265
266 void*   emalloc9p(ulong);
267 void*   erealloc9p(void*, ulong);
268 char*   estrdup9p(char*);
269
270 enum {
271         OMASK = 3
272 };
273
274 void            readstr(Req*, char*);
275 void            readbuf(Req*, void*, long);
276 void            walkandclone(Req*, char*(*walk1)(Fid*,char*,void*), 
277                         char*(*clone)(Fid*,Fid*,void*), void*);
278
279 void            auth9p(Req*);
280 void            authread(Req*);
281 void            authwrite(Req*);
282 void            authdestroy(Fid*);
283 int             authattach(Req*);
284
285 void            srvacquire(Srv *);
286 void            srvrelease(Srv *);
287
288 Reqqueue*       reqqueuecreate(void);
289 void            reqqueuepush(Reqqueue*, Req*, void (*)(Req *));
290 void            reqqueueflush(Reqqueue*, Req*);
291 void            reqqueuefree(Reqqueue*);