9p.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 (*end)(Srv*);
  157. void* aux;
  158. void (*attach)(Req*);
  159. void (*auth)(Req*);
  160. void (*open)(Req*);
  161. void (*create)(Req*);
  162. void (*read)(Req*);
  163. void (*write)(Req*);
  164. void (*remove)(Req*);
  165. void (*flush)(Req*);
  166. void (*stat)(Req*);
  167. void (*wstat)(Req*);
  168. void (*walk)(Req*);
  169. char* (*clone)(Fid*, Fid*);
  170. char* (*walk1)(Fid*, char*, Qid*);
  171. int infd;
  172. int outfd;
  173. int nopipe;
  174. int srvfd;
  175. int leavefdsopen; /* magic for acme win */
  176. char* keyspec;
  177. /* below is implementation-specific; don't use */
  178. Fidpool* fpool;
  179. Reqpool* rpool;
  180. uint msize;
  181. uchar* rbuf;
  182. QLock rlock;
  183. uchar* wbuf;
  184. QLock wlock;
  185. char* addr;
  186. };
  187. void srv(Srv*);
  188. void postmountsrv(Srv*, char*, char*, int);
  189. void _postmountsrv(Srv*, char*, char*, int);
  190. void listensrv(Srv*, char*);
  191. void _listensrv(Srv*, char*);
  192. int postfd(char*, int);
  193. int chatty9p;
  194. void respond(Req*, char*);
  195. void responderror(Req*);
  196. void threadpostmountsrv(Srv*, char*, char*, int);
  197. /*
  198. * Helper. Assumes user is same as group.
  199. */
  200. int hasperm(File*, char*, int);
  201. void* emalloc9p(ulong);
  202. void* erealloc9p(void*, ulong);
  203. char* estrdup9p(char*);
  204. enum {
  205. OMASK = 3
  206. };
  207. void readstr(Req*, char*);
  208. void readbuf(Req*, void*, long);
  209. void walkandclone(Req*, char*(*walk1)(Fid*,char*,void*),
  210. char*(*clone)(Fid*,Fid*,void*), void*);
  211. void auth9p(Req*);
  212. void authread(Req*);
  213. void authwrite(Req*);
  214. void authdestroy(Fid*);
  215. int authattach(Req*);
  216. extern void (*_forker)(void (*)(void*), void*, int);