9p.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. };
  109. struct Tree {
  110. File *root;
  111. void (*destroy)(File *file);
  112. /* below is implementation-specific; don't use */
  113. Lock genlock;
  114. ulong qidgen;
  115. ulong dirqidgen;
  116. };
  117. Tree* alloctree(char*, char*, ulong, void(*destroy)(File*));
  118. void freetree(Tree*);
  119. File* createfile(File*, char*, char*, ulong, void*);
  120. int removefile(File*);
  121. void closefile(File*);
  122. File* walkfile(File*, char*);
  123. Readdir* opendirfile(File*);
  124. long readdirfile(Readdir*, uchar*, long);
  125. void closedirfile(Readdir*);
  126. /*
  127. * Kernel-style command parser
  128. */
  129. typedef struct Cmdbuf Cmdbuf;
  130. typedef struct Cmdtab Cmdtab;
  131. Cmdbuf* parsecmd(char *a, int n);
  132. void respondcmderror(Req*, Cmdbuf*, char*, ...);
  133. Cmdtab* lookupcmd(Cmdbuf*, Cmdtab*, int);
  134. #pragma varargck argpos respondcmderr 3
  135. struct Cmdbuf
  136. {
  137. char *buf;
  138. char **f;
  139. int nf;
  140. };
  141. struct Cmdtab
  142. {
  143. int index; /* used by client to switch on result */
  144. char *cmd; /* command name */
  145. int narg; /* expected #args; 0 ==> variadic */
  146. };
  147. /*
  148. * File service loop.
  149. */
  150. struct Srv {
  151. Tree* tree;
  152. void (*destroyfid)(Fid*);
  153. void (*destroyreq)(Req*);
  154. void (*end)(Srv*);
  155. void* aux;
  156. void (*attach)(Req*);
  157. void (*auth)(Req*);
  158. void (*open)(Req*);
  159. void (*create)(Req*);
  160. void (*read)(Req*);
  161. void (*write)(Req*);
  162. void (*remove)(Req*);
  163. void (*flush)(Req*);
  164. void (*stat)(Req*);
  165. void (*wstat)(Req*);
  166. void (*walk)(Req*);
  167. char* (*clone)(Fid*, Fid*);
  168. char* (*walk1)(Fid*, char*, Qid*);
  169. int infd;
  170. int outfd;
  171. int nopipe;
  172. int srvfd;
  173. int leavefdsopen; /* magic for acme win */
  174. /* below is implementation-specific; don't use */
  175. Fidpool* fpool;
  176. Reqpool* rpool;
  177. uint msize;
  178. uchar* rbuf;
  179. QLock rlock;
  180. uchar* wbuf;
  181. QLock wlock;
  182. };
  183. void srv(Srv*);
  184. void postmountsrv(Srv*, char*, char*, int);
  185. int postfd(char*, int);
  186. int chatty9p;
  187. void respond(Req*, char*);
  188. void threadpostmountsrv(Srv*, char*, char*, int);
  189. /*
  190. * Helper. Assumes user is same as group.
  191. */
  192. int hasperm(File*, char*, int);
  193. void* emalloc9p(ulong);
  194. void* erealloc9p(void*, ulong);
  195. char* estrdup9p(char*);
  196. enum {
  197. OMASK = 3
  198. };
  199. void readstr(Req*, char*);
  200. void readbuf(Req*, void*, long);
  201. void walkandclone(Req*, char*(*walk1)(Fid*,char*,void*), char*(*clone)(Fid*,Fid*,void*), void*);