9p.h 4.2 KB

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