1
0

9p.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #pragma src "/sys/src/lib9p"
  2. #pragma lib "lib9p.a"
  3. /*
  4. * Maps from ulongs to void*s.
  5. */
  6. typedef struct Intmap Intmap;
  7. #pragma incomplete Intmap
  8. Intmap* allocmap(void (*inc)(void*));
  9. void freemap(Intmap*, void (*destroy)(void*));
  10. void* lookupkey(Intmap*, ulong);
  11. void* insertkey(Intmap*, ulong, void*);
  12. int caninsertkey(Intmap*, ulong, void*);
  13. void* deletekey(Intmap*, ulong);
  14. /*
  15. * Fid and Request structures.
  16. */
  17. typedef struct Fid Fid;
  18. typedef struct Req Req;
  19. typedef struct Fidpool Fidpool;
  20. typedef struct Reqpool Reqpool;
  21. typedef struct File File;
  22. typedef struct Filelist Filelist;
  23. typedef struct Tree Tree;
  24. typedef struct Readdir Readdir;
  25. typedef struct Srv Srv;
  26. #pragma incomplete Filelist
  27. #pragma incomplete Readdir
  28. struct Fid
  29. {
  30. ulong fid;
  31. char omode; /* -1 = not open */
  32. File* file;
  33. char* uid;
  34. Qid qid;
  35. void* aux;
  36. /* below is implementation-specific; don't use */
  37. Readdir* rdir;
  38. Ref ref;
  39. Fidpool* pool;
  40. vlong diroffset;
  41. long dirindex;
  42. };
  43. struct Req
  44. {
  45. ulong tag;
  46. void* aux;
  47. Fcall ifcall;
  48. Fcall ofcall;
  49. Dir d;
  50. Req* oldreq;
  51. Fid* fid;
  52. Fid* afid;
  53. Fid* newfid;
  54. Srv* srv;
  55. /* below is implementation-specific; don't use */
  56. QLock lk;
  57. Ref ref;
  58. Reqpool* pool;
  59. uchar* buf;
  60. uchar type;
  61. uchar responded;
  62. char* error;
  63. void* rbuf;
  64. Req** flush;
  65. int nflush;
  66. };
  67. /*
  68. * Pools to maintain Fid <-> fid and Req <-> tag maps.
  69. */
  70. struct Fidpool {
  71. Intmap *map;
  72. void (*destroy)(Fid*);
  73. Srv *srv;
  74. };
  75. struct Reqpool {
  76. Intmap *map;
  77. void (*destroy)(Req*);
  78. Srv *srv;
  79. };
  80. Fidpool* allocfidpool(void (*destroy)(Fid*));
  81. void freefidpool(Fidpool*);
  82. Fid* allocfid(Fidpool*, ulong);
  83. Fid* lookupfid(Fidpool*, ulong);
  84. void closefid(Fid*);
  85. Fid* removefid(Fidpool*, ulong);
  86. Reqpool* allocreqpool(void (*destroy)(Req*));
  87. void freereqpool(Reqpool*);
  88. Req* allocreq(Reqpool*, ulong);
  89. Req* lookupreq(Reqpool*, ulong);
  90. void closereq(Req*);
  91. Req* removereq(Reqpool*, ulong);
  92. typedef int Dirgen(int, Dir*, void*);
  93. void dirread9p(Req*, Dirgen*, void*);
  94. /*
  95. * File trees.
  96. */
  97. struct File {
  98. Ref;
  99. Dir;
  100. File *parent;
  101. void *aux;
  102. /* below is implementation-specific; don't use */
  103. RWLock;
  104. Filelist *filelist;
  105. Tree *tree;
  106. int nchild;
  107. int allocd;
  108. int nxchild;
  109. Ref readers;
  110. };
  111. struct Tree {
  112. File *root;
  113. void (*destroy)(File *file);
  114. /* below is implementation-specific; don't use */
  115. Lock genlock;
  116. ulong qidgen;
  117. ulong dirqidgen;
  118. };
  119. Tree* alloctree(char*, char*, ulong, void(*destroy)(File*));
  120. void freetree(Tree*);
  121. File* createfile(File*, char*, char*, ulong, void*);
  122. int removefile(File*);
  123. void closefile(File*);
  124. File* walkfile(File*, char*);
  125. Readdir* opendirfile(File*);
  126. long readdirfile(Readdir*, uchar*, long);
  127. void closedirfile(Readdir*);
  128. /*
  129. * Kernel-style command parser
  130. */
  131. typedef struct Cmdbuf Cmdbuf;
  132. typedef struct Cmdtab Cmdtab;
  133. Cmdbuf* parsecmd(char *a, int n);
  134. void respondcmderror(Req*, Cmdbuf*, char*, ...);
  135. Cmdtab* lookupcmd(Cmdbuf*, Cmdtab*, int);
  136. #pragma varargck argpos respondcmderr 3
  137. struct Cmdbuf
  138. {
  139. char *buf;
  140. char **f;
  141. int nf;
  142. };
  143. struct Cmdtab
  144. {
  145. int index; /* used by client to switch on result */
  146. char *cmd; /* command name */
  147. int narg; /* expected #args; 0 ==> variadic */
  148. };
  149. /*
  150. * File service loop.
  151. */
  152. struct Srv {
  153. Tree* tree;
  154. void (*destroyfid)(Fid*);
  155. void (*destroyreq)(Req*);
  156. void (*start)(Srv*);
  157. void (*end)(Srv*);
  158. void* aux;
  159. void (*attach)(Req*);
  160. void (*auth)(Req*);
  161. void (*open)(Req*);
  162. void (*create)(Req*);
  163. void (*read)(Req*);
  164. void (*write)(Req*);
  165. void (*remove)(Req*);
  166. void (*flush)(Req*);
  167. void (*stat)(Req*);
  168. void (*wstat)(Req*);
  169. void (*walk)(Req*);
  170. char* (*clone)(Fid*, Fid*);
  171. char* (*walk1)(Fid*, char*, Qid*);
  172. int infd;
  173. int outfd;
  174. int nopipe;
  175. int srvfd;
  176. int leavefdsopen; /* magic for acme win */
  177. char* keyspec;
  178. /* below is implementation-specific; don't use */
  179. Fidpool* fpool;
  180. Reqpool* rpool;
  181. uint msize;
  182. uchar* rbuf;
  183. QLock rlock;
  184. uchar* wbuf;
  185. QLock wlock;
  186. char* addr;
  187. };
  188. void srv(Srv*);
  189. void postmountsrv(Srv*, char*, char*, int);
  190. void _postmountsrv(Srv*, char*, char*, int);
  191. void listensrv(Srv*, char*);
  192. void _listensrv(Srv*, char*);
  193. int postfd(char*, int);
  194. int chatty9p;
  195. void respond(Req*, char*);
  196. void responderror(Req*);
  197. void threadpostmountsrv(Srv*, char*, char*, int);
  198. void threadlistensrv(Srv *s, char *addr);
  199. /*
  200. * Helper. Assumes user is same as group.
  201. */
  202. int hasperm(File*, char*, int);
  203. void* emalloc9p(ulong);
  204. void* erealloc9p(void*, ulong);
  205. char* estrdup9p(char*);
  206. enum {
  207. OMASK = 3
  208. };
  209. void readstr(Req*, char*);
  210. void readbuf(Req*, void*, long);
  211. void walkandclone(Req*, char*(*walk1)(Fid*,char*,void*),
  212. char*(*clone)(Fid*,Fid*,void*), void*);
  213. void auth9p(Req*);
  214. void authread(Req*);
  215. void authwrite(Req*);
  216. void authdestroy(Fid*);
  217. int authattach(Req*);
  218. extern void (*_forker)(void (*)(void*), void*, int);