fs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * ``Exec'' network device. Mounted on net, provides /net/exec.
  11. *
  12. * exec protocol directory
  13. * n connection directory
  14. * ctl control messages (like connect)
  15. * data data
  16. * err errors
  17. * local local address (pid of command)
  18. * remote remote address (command)
  19. * status status
  20. */
  21. #include <u.h>
  22. #include <libc.h>
  23. #include <fcall.h>
  24. #include <thread.h>
  25. #include <9p.h>
  26. #include "dat.h"
  27. int fsdebug;
  28. enum
  29. {
  30. Qroot,
  31. Qexec,
  32. Qclone,
  33. Qn,
  34. Qctl,
  35. Qdata,
  36. Qlocal,
  37. Qremote,
  38. Qstatus,
  39. };
  40. #define PATH(type, n) ((type)|((n)<<8))
  41. #define TYPE(path) ((int)(path) & 0xFF)
  42. #define NUM(path) ((uint)(path)>>8)
  43. typedef struct Tab Tab;
  44. struct Tab
  45. {
  46. char *name;
  47. uint32_t mode;
  48. };
  49. Tab tab[] =
  50. {
  51. "/", DMDIR|0555,
  52. "exec", DMDIR|0555,
  53. "clone", 0666,
  54. nil, DMDIR|0555,
  55. "ctl", 0666,
  56. "data", 0666,
  57. "local", 0444,
  58. "remote", 0444,
  59. "status", 0444,
  60. };
  61. void
  62. setexecname(char *s)
  63. {
  64. tab[Qexec].name = s;
  65. }
  66. uint32_t time0;
  67. static void
  68. fillstat(Dir *d, uint32_t path)
  69. {
  70. Tab *t;
  71. int type;
  72. char buf[32];
  73. memset(d, 0, sizeof(*d));
  74. d->uid = estrdup("exec");
  75. d->gid = estrdup("exec");
  76. d->qid.path = path;
  77. d->atime = d->mtime = time0;
  78. d->length = 0;
  79. type = TYPE(path);
  80. t = &tab[type];
  81. if(t->name)
  82. d->name = estrdup(t->name);
  83. else{
  84. snprint(buf, sizeof buf, "%u", NUM(path));
  85. d->name = estrdup(buf);
  86. }
  87. d->qid.type = t->mode>>24;
  88. d->mode = t->mode;
  89. }
  90. static void
  91. fsstat(Req *r)
  92. {
  93. fillstat(&r->d, r->fid->qid.path);
  94. respond(r, nil);
  95. }
  96. static int
  97. rootgen(int i, Dir *d, void*)
  98. {
  99. if(i < 1){
  100. fillstat(d, PATH(Qexec, 0));
  101. return 0;
  102. }
  103. return -1;
  104. }
  105. static int
  106. execgen(int i, Dir *d, void*)
  107. {
  108. if(i < 1){
  109. fillstat(d, PATH(Qclone, 0));
  110. return 0;
  111. }
  112. i -= 1;
  113. if(i < nclient){
  114. fillstat(d, PATH(Qn, i));
  115. return 0;
  116. }
  117. return -1;
  118. }
  119. static int
  120. conngen(int i, Dir *d, void *aux)
  121. {
  122. Client *c;
  123. c = aux;
  124. i += Qn+1;
  125. if(i <= Qstatus){
  126. fillstat(d, PATH(i, c->num));
  127. return 0;
  128. }
  129. return -1;
  130. }
  131. char *statusstr[] =
  132. {
  133. "Closed",
  134. "Exec",
  135. "Established",
  136. "Hangup",
  137. };
  138. static void
  139. fsread(Req *r)
  140. {
  141. char e[ERRMAX], *s;
  142. uint32_t path;
  143. path = r->fid->qid.path;
  144. switch(TYPE(path)){
  145. default:
  146. snprint(e, sizeof e, "bug in execnet path=%lx", path);
  147. respond(r, e);
  148. break;
  149. case Qroot:
  150. dirread9p(r, rootgen, nil);
  151. respond(r, nil);
  152. break;
  153. case Qexec:
  154. dirread9p(r, execgen, nil);
  155. respond(r, nil);
  156. break;
  157. case Qn:
  158. dirread9p(r, conngen, client[NUM(path)]);
  159. respond(r, nil);
  160. break;
  161. case Qctl:
  162. snprint(e, sizeof e, "%u", NUM(path));
  163. readstr(r, e);
  164. respond(r, nil);
  165. break;
  166. case Qdata:
  167. dataread(r, client[NUM(path)]);
  168. break;
  169. case Qlocal:
  170. snprint(e, sizeof e, "%d", client[NUM(path)]->pid);
  171. readstr(r, e);
  172. respond(r, nil);
  173. break;
  174. case Qremote:
  175. s = client[NUM(path)]->cmd;
  176. if(strlen(s) >= 5) /* "exec " */
  177. readstr(r, s+5);
  178. else
  179. readstr(r, s);
  180. respond(r, nil);
  181. break;
  182. case Qstatus:
  183. readstr(r, statusstr[client[NUM(path)]->status]);
  184. respond(r, nil);
  185. break;
  186. }
  187. }
  188. static void
  189. fswrite(Req *r)
  190. {
  191. char e[ERRMAX];
  192. uint32_t path;
  193. path = r->fid->qid.path;
  194. switch(TYPE(path)){
  195. default:
  196. snprint(e, sizeof e, "bug in execnet path=%lx", path);
  197. respond(r, e);
  198. break;
  199. case Qctl:
  200. ctlwrite(r, client[NUM(path)]);
  201. break;
  202. case Qdata:
  203. datawrite(r, client[NUM(path)]);
  204. break;
  205. }
  206. }
  207. static void
  208. fsflush(Req *r)
  209. {
  210. uint32_t path;
  211. Req *or;
  212. for(or=r; or->ifcall.type==Tflush; or=or->oldreq)
  213. ;
  214. if(or->ifcall.type != Tread && or->ifcall.type != Twrite)
  215. abort();
  216. path = or->fid->qid.path;
  217. if(TYPE(path) != Qdata)
  218. abort();
  219. clientflush(or, client[NUM(path)]);
  220. respond(r, nil);
  221. }
  222. static void
  223. fsattach(Req *r)
  224. {
  225. if(r->ifcall.aname && r->ifcall.aname[0]){
  226. respond(r, "invalid attach specifier");
  227. return;
  228. }
  229. r->fid->qid.path = PATH(Qroot, 0);
  230. r->fid->qid.type = QTDIR;
  231. r->fid->qid.vers = 0;
  232. r->ofcall.qid = r->fid->qid;
  233. respond(r, nil);
  234. }
  235. static char*
  236. fswalk1(Fid *fid, char *name, Qid *qid)
  237. {
  238. char buf[32];
  239. int i, n;
  240. uint32_t path;
  241. if(!(fid->qid.type&QTDIR))
  242. return "walk in non-directory";
  243. path = fid->qid.path;
  244. if(strcmp(name, "..") == 0){
  245. switch(TYPE(path)){
  246. case Qn:
  247. qid->path = PATH(Qexec, 0);
  248. qid->type = QTDIR;
  249. return nil;
  250. case Qroot:
  251. case Qexec:
  252. qid->path = PATH(Qroot, 0);
  253. qid->type = QTDIR;
  254. return nil;
  255. default:
  256. return "bug in fswalk1";
  257. }
  258. }
  259. i = TYPE(path)+1;
  260. for(; i<nelem(tab); i++){
  261. if(i==Qn){
  262. n = atoi(name);
  263. snprint(buf, sizeof buf, "%d", n);
  264. if(n < nclient && strcmp(buf, name) == 0){
  265. qid->path = PATH(Qn, n);
  266. qid->type = QTDIR;
  267. return nil;
  268. }
  269. break;
  270. }
  271. if(strcmp(tab[i].name, name) == 0){
  272. qid->path = PATH(i, NUM(path));
  273. qid->type = tab[i].mode>>24;
  274. return nil;
  275. }
  276. if(tab[i].mode&DMDIR)
  277. break;
  278. }
  279. return "directory entry not found";
  280. }
  281. static void
  282. fsopen(Req *r)
  283. {
  284. static int need[4] = { 4, 2, 6, 1 };
  285. uint32_t path;
  286. int n;
  287. Tab *t;
  288. /*
  289. * lib9p already handles the blatantly obvious.
  290. * we just have to enforce the permissions we have set.
  291. */
  292. path = r->fid->qid.path;
  293. t = &tab[TYPE(path)];
  294. n = need[r->ifcall.mode&3];
  295. if((n&t->mode) != n){
  296. respond(r, "permission denied");
  297. return;
  298. }
  299. switch(TYPE(path)){
  300. case Qclone:
  301. n = newclient();
  302. path = PATH(Qctl, n);
  303. r->fid->qid.path = path;
  304. r->ofcall.qid.path = path;
  305. if(fsdebug)
  306. fprint(2, "open clone => path=%lx\n", path);
  307. t = &tab[Qctl];
  308. /* fall through */
  309. default:
  310. if(t-tab >= Qn)
  311. client[NUM(path)]->ref++;
  312. respond(r, nil);
  313. break;
  314. }
  315. }
  316. Channel *cclunk;
  317. Channel *cclunkwait;
  318. Channel *creq;
  319. Channel *creqwait;
  320. static void
  321. fsthread(void*)
  322. {
  323. uint32_t path;
  324. Alt a[3];
  325. Fid *fid;
  326. Req *r;
  327. threadsetname("fsthread");
  328. a[0].op = CHANRCV;
  329. a[0].c = cclunk;
  330. a[0].v = &fid;
  331. a[1].op = CHANRCV;
  332. a[1].c = creq;
  333. a[1].v = &r;
  334. a[2].op = CHANEND;
  335. for(;;){
  336. switch(alt(a)){
  337. case 0:
  338. path = fid->qid.path;
  339. if(fid->omode != -1 && TYPE(path) >= Qn)
  340. closeclient(client[NUM(path)]);
  341. sendp(cclunkwait, nil);
  342. break;
  343. case 1:
  344. switch(r->ifcall.type){
  345. case Tattach:
  346. fsattach(r);
  347. break;
  348. case Topen:
  349. fsopen(r);
  350. break;
  351. case Tread:
  352. fsread(r);
  353. break;
  354. case Twrite:
  355. fswrite(r);
  356. break;
  357. case Tstat:
  358. fsstat(r);
  359. break;
  360. case Tflush:
  361. fsflush(r);
  362. break;
  363. default:
  364. respond(r, "bug in fsthread");
  365. break;
  366. }
  367. sendp(creqwait, 0);
  368. break;
  369. }
  370. }
  371. }
  372. static void
  373. fsdestroyfid(Fid *fid)
  374. {
  375. sendp(cclunk, fid);
  376. recvp(cclunkwait);
  377. }
  378. static void
  379. fssend(Req *r)
  380. {
  381. sendp(creq, r);
  382. recvp(creqwait); /* avoids need to deal with spurious flushes */
  383. }
  384. void
  385. initfs(void)
  386. {
  387. time0 = time(0);
  388. creq = chancreate(sizeof(void*), 0);
  389. creqwait = chancreate(sizeof(void*), 0);
  390. cclunk = chancreate(sizeof(void*), 0);
  391. cclunkwait = chancreate(sizeof(void*), 0);
  392. procrfork(fsthread, nil, STACK, RFNAMEG);
  393. }
  394. Srv fs =
  395. {
  396. .attach= fssend,
  397. .destroyfid= fsdestroyfid,
  398. .walk1= fswalk1,
  399. .open= fssend,
  400. .read= fssend,
  401. .write= fssend,
  402. .stat= fssend,
  403. .flush= fssend,
  404. };