exportfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * exportfs - Export a plan 9 name space across a network
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <auth.h>
  7. #include <fcall.h>
  8. #include <libsec.h>
  9. #define Extern
  10. #include "exportfs.h"
  11. #define QIDPATH ((1LL<<48)-1)
  12. vlong newqid = 0;
  13. enum {
  14. Encnone,
  15. Encssl,
  16. Enctls,
  17. };
  18. void (*fcalls[])(Fsrpc*) =
  19. {
  20. [Tversion] Xversion,
  21. [Tauth] Xauth,
  22. [Tflush] Xflush,
  23. [Tattach] Xattach,
  24. [Twalk] Xwalk,
  25. [Topen] slave,
  26. [Tcreate] Xcreate,
  27. [Tclunk] Xclunk,
  28. [Tread] slave,
  29. [Twrite] slave,
  30. [Tremove] Xremove,
  31. [Tstat] Xstat,
  32. [Twstat] Xwstat,
  33. };
  34. /* accounting and debugging counters */
  35. int filecnt;
  36. int freecnt;
  37. int qidcnt;
  38. int qfreecnt;
  39. int ncollision;
  40. int netfd; /* initially stdin */
  41. int srvfd = -1;
  42. int nonone = 1;
  43. char *filterp;
  44. char *ealgs = "rc4_256 sha1";
  45. char *aanfilter = "/bin/aan";
  46. int encproto = Encnone;
  47. int readonly;
  48. static void mksecret(char *, uchar *);
  49. static int localread9pmsg(int, void *, uint, ulong *);
  50. static char *anstring = "tcp!*!0";
  51. char *netdir = "", *local = "", *remote = "";
  52. int filter(int, char *);
  53. void
  54. usage(void)
  55. {
  56. fprint(2, "usage: %s [-adnsR] [-f dbgfile] [-m msize] [-r root] "
  57. "[-S srvfile] [-e 'crypt hash'] [-P exclusion-file] "
  58. "[-A announce-string] [-B address]\n", argv0);
  59. fatal("usage");
  60. }
  61. static void
  62. noteconn(int fd)
  63. {
  64. NetConnInfo *nci;
  65. nci = getnetconninfo(nil, fd);
  66. if (nci == nil)
  67. return;
  68. netdir = strdup(nci->dir);
  69. local = strdup(nci->lsys);
  70. remote = strdup(nci->rsys);
  71. freenetconninfo(nci);
  72. }
  73. void
  74. main(int argc, char **argv)
  75. {
  76. char buf[ERRMAX], ebuf[ERRMAX], *srvfdfile;
  77. Fsrpc *r;
  78. int doauth, n, fd;
  79. char *dbfile, *srv, *na, *nsfile, *keyspec;
  80. AuthInfo *ai;
  81. ulong initial;
  82. dbfile = "/tmp/exportdb";
  83. srv = nil;
  84. srvfd = -1;
  85. srvfdfile = nil;
  86. na = nil;
  87. nsfile = nil;
  88. keyspec = "";
  89. doauth = 0;
  90. ai = nil;
  91. ARGBEGIN{
  92. case 'a':
  93. doauth = 1;
  94. break;
  95. case 'd':
  96. dbg++;
  97. break;
  98. case 'e':
  99. ealgs = EARGF(usage());
  100. if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
  101. ealgs = nil;
  102. break;
  103. case 'f':
  104. dbfile = EARGF(usage());
  105. break;
  106. case 'k':
  107. keyspec = EARGF(usage());
  108. break;
  109. case 'm':
  110. messagesize = strtoul(EARGF(usage()), nil, 0);
  111. break;
  112. case 'n':
  113. nonone = 0;
  114. break;
  115. case 'r':
  116. srv = EARGF(usage());
  117. break;
  118. case 's':
  119. srv = "/";
  120. break;
  121. case 'A':
  122. anstring = EARGF(usage());
  123. break;
  124. case 'B':
  125. na = EARGF(usage());
  126. break;
  127. case 'F':
  128. /* accepted but ignored, for backwards compatibility */
  129. break;
  130. case 'N':
  131. nsfile = EARGF(usage());
  132. break;
  133. case 'P':
  134. patternfile = EARGF(usage());
  135. break;
  136. case 'R':
  137. readonly = 1;
  138. break;
  139. case 'S':
  140. if(srvfdfile)
  141. usage();
  142. srvfdfile = EARGF(usage());
  143. break;
  144. default:
  145. usage();
  146. }ARGEND
  147. USED(argc, argv);
  148. if(doauth){
  149. /*
  150. * We use p9any so we don't have to visit this code again, with the
  151. * cost that this code is incompatible with the old world, which
  152. * requires p9sk2. (The two differ in who talks first, so compatibility
  153. * is awkward.)
  154. */
  155. ai = auth_proxy(0, auth_getkey, "proto=p9any role=server %s", keyspec);
  156. if(ai == nil)
  157. fatal("auth_proxy: %r");
  158. if(nonone && strcmp(ai->cuid, "none") == 0)
  159. fatal("exportfs by none disallowed");
  160. if(auth_chuid(ai, nsfile) < 0)
  161. fatal("auth_chuid: %r");
  162. putenv("service", "exportfs");
  163. }
  164. if(srvfdfile){
  165. if((srvfd = open(srvfdfile, ORDWR)) < 0)
  166. sysfatal("open '%s': %r", srvfdfile);
  167. }
  168. if(na){
  169. if(srv == nil)
  170. sysfatal("-B requires -s");
  171. local = "me";
  172. remote = na;
  173. if((fd = dial(netmkaddr(na, 0, "importfs"), 0, 0, 0)) < 0)
  174. sysfatal("can't dial %s: %r", na);
  175. ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
  176. if(ai == nil)
  177. sysfatal("%r: %s", na);
  178. dup(fd, 0);
  179. dup(fd, 1);
  180. close(fd);
  181. }
  182. exclusions();
  183. if(dbg) {
  184. n = create(dbfile, OWRITE|OTRUNC, 0666);
  185. dup(n, DFD);
  186. close(n);
  187. }
  188. if(srvfd >= 0 && srv){
  189. fprint(2, "exportfs: -S cannot be used with -r or -s\n");
  190. usage();
  191. }
  192. DEBUG(DFD, "exportfs: started\n");
  193. rfork(RFNOTEG);
  194. if(messagesize == 0){
  195. messagesize = iounit(netfd);
  196. if(messagesize == 0)
  197. messagesize = 8192+IOHDRSZ;
  198. }
  199. Workq = emallocz(sizeof(Fsrpc)*Nr_workbufs);
  200. // for(i=0; i<Nr_workbufs; i++)
  201. // Workq[i].buf = emallocz(messagesize);
  202. fhash = emallocz(sizeof(Fid*)*FHASHSIZE);
  203. fmtinstall('F', fcallfmt);
  204. /*
  205. * Get tree to serve from network connection,
  206. * check we can get there and ack the connection
  207. */
  208. if(srvfd != -1) {
  209. /* do nothing */
  210. }
  211. else if(srv) {
  212. chdir(srv);
  213. DEBUG(DFD, "invoked as server for %s", srv);
  214. strncpy(buf, srv, sizeof buf);
  215. }
  216. else {
  217. noteconn(netfd);
  218. buf[0] = 0;
  219. n = read(0, buf, sizeof(buf)-1);
  220. if(n < 0) {
  221. errstr(buf, sizeof buf);
  222. fprint(0, "read(0): %s", buf);
  223. DEBUG(DFD, "read(0): %s", buf);
  224. exits(buf);
  225. }
  226. buf[n] = 0;
  227. if(chdir(buf) < 0) {
  228. errstr(ebuf, sizeof ebuf);
  229. fprint(0, "chdir(%d:\"%s\"): %s", n, buf, ebuf);
  230. DEBUG(DFD, "chdir(%d:\"%s\"): %s", n, buf, ebuf);
  231. exits(ebuf);
  232. }
  233. }
  234. DEBUG(DFD, "\niniting root\n");
  235. initroot();
  236. DEBUG(DFD, "exportfs: %s\n", buf);
  237. if(srv == nil && srvfd == -1 && write(0, "OK", 2) != 2)
  238. fatal("open ack write");
  239. if (readn(netfd, &initial, sizeof(ulong)) < sizeof(ulong))
  240. fatal("can't read initial string: %r\n");
  241. if (strncmp((char *)&initial, "impo", sizeof(ulong)) == 0) {
  242. char buf[128], *p, *args[3];
  243. /* New import. Read import's parameters... */
  244. initial = 0;
  245. p = buf;
  246. while (p - buf < sizeof buf) {
  247. if ((n = read(netfd, p, 1)) < 0)
  248. fatal("can't read impo arguments: %r\n");
  249. if (n == 0)
  250. fatal("connection closed while reading arguments\n");
  251. if (*p == '\n')
  252. *p = '\0';
  253. if (*p++ == '\0')
  254. break;
  255. }
  256. if (tokenize(buf, args, nelem(args)) != 2)
  257. fatal("impo arguments invalid: impo%s...\n", buf);
  258. if (strcmp(args[0], "aan") == 0)
  259. filterp = aanfilter;
  260. else if (strcmp(args[0], "nofilter") != 0)
  261. fatal("import filter argument unsupported: %s\n", args[0]);
  262. if (strcmp(args[1], "ssl") == 0)
  263. encproto = Encssl;
  264. else if (strcmp(args[1], "tls") == 0)
  265. encproto = Enctls;
  266. else if (strcmp(args[1], "clear") != 0)
  267. fatal("import encryption proto unsupported: %s\n", args[1]);
  268. if (encproto == Enctls)
  269. sysfatal("%s: tls has not yet been implemented", argv[0]);
  270. }
  271. if (encproto != Encnone && ealgs && ai) {
  272. uchar key[16];
  273. uchar digest[SHA1dlen];
  274. char fromclientsecret[21];
  275. char fromserversecret[21];
  276. int i;
  277. memmove(key+4, ai->secret, ai->nsecret);
  278. /* exchange random numbers */
  279. srand(truerand());
  280. for(i = 0; i < 4; i++)
  281. key[i+12] = rand();
  282. if (initial)
  283. fatal("Protocol botch: old import\n");
  284. if(readn(netfd, key, 4) != 4)
  285. fatal("can't read key part; %r\n");
  286. if(write(netfd, key+12, 4) != 4)
  287. fatal("can't write key part; %r\n");
  288. /* scramble into two secrets */
  289. sha1(key, sizeof(key), digest, nil);
  290. mksecret(fromclientsecret, digest);
  291. mksecret(fromserversecret, digest+10);
  292. if (filterp)
  293. netfd = filter(netfd, filterp);
  294. switch (encproto) {
  295. case Encssl:
  296. netfd = pushssl(netfd, ealgs, fromserversecret,
  297. fromclientsecret, nil);
  298. break;
  299. case Enctls:
  300. default:
  301. fatal("Unsupported encryption protocol\n");
  302. }
  303. if(netfd < 0)
  304. fatal("can't establish ssl connection: %r");
  305. }
  306. else if (filterp) {
  307. if (initial)
  308. fatal("Protocol botch: don't know how to deal with this\n");
  309. netfd = filter(netfd, filterp);
  310. }
  311. /*
  312. * Start serving file requests from the network
  313. */
  314. for(;;) {
  315. r = getsbuf();
  316. if(r == 0)
  317. fatal("Out of service buffers");
  318. n = localread9pmsg(netfd, r->buf, messagesize, &initial);
  319. if(n <= 0)
  320. fatal(nil);
  321. if(convM2S(r->buf, n, &r->work) == 0)
  322. fatal("convM2S format error");
  323. DEBUG(DFD, "%F\n", &r->work);
  324. (fcalls[r->work.type])(r);
  325. }
  326. }
  327. /*
  328. * WARNING: Replace this with the original version as soon as all
  329. * _old_ imports have been replaced with negotiating imports. Also
  330. * cpu relies on this (which needs to be fixed!) -- pb.
  331. */
  332. static int
  333. localread9pmsg(int fd, void *abuf, uint n, ulong *initial)
  334. {
  335. int m, len;
  336. uchar *buf;
  337. buf = abuf;
  338. /* read count */
  339. assert(BIT32SZ == sizeof(ulong));
  340. if (*initial) {
  341. memcpy(buf, initial, BIT32SZ);
  342. *initial = 0;
  343. }
  344. else {
  345. m = readn(fd, buf, BIT32SZ);
  346. if(m != BIT32SZ){
  347. if(m < 0)
  348. return -1;
  349. return 0;
  350. }
  351. }
  352. len = GBIT32(buf);
  353. if(len <= BIT32SZ || len > n){
  354. werrstr("bad length in 9P2000 message header");
  355. return -1;
  356. }
  357. len -= BIT32SZ;
  358. m = readn(fd, buf+BIT32SZ, len);
  359. if(m < len)
  360. return 0;
  361. return BIT32SZ+m;
  362. }
  363. void
  364. reply(Fcall *r, Fcall *t, char *err)
  365. {
  366. uchar *data;
  367. int n;
  368. t->tag = r->tag;
  369. t->fid = r->fid;
  370. if(err) {
  371. t->type = Rerror;
  372. t->ename = err;
  373. }
  374. else
  375. t->type = r->type + 1;
  376. DEBUG(DFD, "\t%F\n", t);
  377. data = malloc(messagesize); /* not mallocz; no need to clear */
  378. if(data == nil)
  379. fatal(Enomem);
  380. n = convS2M(t, data, messagesize);
  381. if(write(netfd, data, n)!=n)
  382. {syslog(0, "exportfs", "short write: %r");
  383. fatal("mount write");
  384. }
  385. free(data);
  386. }
  387. Fid *
  388. getfid(int nr)
  389. {
  390. Fid *f;
  391. for(f = fidhash(nr); f; f = f->next)
  392. if(f->nr == nr)
  393. return f;
  394. return 0;
  395. }
  396. int
  397. freefid(int nr)
  398. {
  399. Fid *f, **l;
  400. char buf[128];
  401. l = &fidhash(nr);
  402. for(f = *l; f; f = f->next) {
  403. if(f->nr == nr) {
  404. if(f->mid) {
  405. sprint(buf, "/mnt/exportfs/%d", f->mid);
  406. unmount(0, buf);
  407. psmap[f->mid] = 0;
  408. }
  409. if(f->f) {
  410. freefile(f->f);
  411. f->f = nil;
  412. }
  413. if(f->dir){
  414. free(f->dir);
  415. f->dir = nil;
  416. }
  417. *l = f->next;
  418. f->next = fidfree;
  419. fidfree = f;
  420. return 1;
  421. }
  422. l = &f->next;
  423. }
  424. return 0;
  425. }
  426. Fid *
  427. newfid(int nr)
  428. {
  429. Fid *new, **l;
  430. int i;
  431. l = &fidhash(nr);
  432. for(new = *l; new; new = new->next)
  433. if(new->nr == nr)
  434. return 0;
  435. if(fidfree == 0) {
  436. fidfree = emallocz(sizeof(Fid) * Fidchunk);
  437. for(i = 0; i < Fidchunk-1; i++)
  438. fidfree[i].next = &fidfree[i+1];
  439. fidfree[Fidchunk-1].next = 0;
  440. }
  441. new = fidfree;
  442. fidfree = new->next;
  443. memset(new, 0, sizeof(Fid));
  444. new->next = *l;
  445. *l = new;
  446. new->nr = nr;
  447. new->fid = -1;
  448. new->mid = 0;
  449. return new;
  450. }
  451. Fsrpc *
  452. getsbuf(void)
  453. {
  454. static int ap;
  455. int look, rounds;
  456. Fsrpc *wb;
  457. int small_instead_of_fast = 1;
  458. if(small_instead_of_fast)
  459. ap = 0; /* so we always start looking at the beginning and reuse buffers */
  460. for(rounds = 0; rounds < 10; rounds++) {
  461. for(look = 0; look < Nr_workbufs; look++) {
  462. if(++ap == Nr_workbufs)
  463. ap = 0;
  464. if(Workq[ap].busy == 0)
  465. break;
  466. }
  467. if(look == Nr_workbufs){
  468. sleep(10 * rounds);
  469. continue;
  470. }
  471. wb = &Workq[ap];
  472. wb->pid = 0;
  473. wb->canint = 0;
  474. wb->flushtag = NOTAG;
  475. wb->busy = 1;
  476. if(wb->buf == nil) /* allocate buffers dynamically to keep size down */
  477. wb->buf = emallocz(messagesize);
  478. return wb;
  479. }
  480. fatal("No more work buffers");
  481. return nil;
  482. }
  483. void
  484. freefile(File *f)
  485. {
  486. File *parent, *child;
  487. Loop:
  488. f->ref--;
  489. if(f->ref > 0)
  490. return;
  491. freecnt++;
  492. if(f->ref < 0) abort();
  493. DEBUG(DFD, "free %s\n", f->name);
  494. /* delete from parent */
  495. parent = f->parent;
  496. if(parent->child == f)
  497. parent->child = f->childlist;
  498. else{
  499. for(child=parent->child; child->childlist!=f; child=child->childlist)
  500. if(child->childlist == nil)
  501. fatal("bad child list");
  502. child->childlist = f->childlist;
  503. }
  504. freeqid(f->qidt);
  505. free(f->name);
  506. f->name = nil;
  507. free(f);
  508. f = parent;
  509. if(f != nil)
  510. goto Loop;
  511. }
  512. File *
  513. file(File *parent, char *name)
  514. {
  515. Dir *dir;
  516. char *path;
  517. File *f;
  518. DEBUG(DFD, "\tfile: 0x%p %s name %s\n", parent, parent->name, name);
  519. path = makepath(parent, name);
  520. if(patternfile != nil && excludefile(path)){
  521. free(path);
  522. return nil;
  523. }
  524. dir = dirstat(path);
  525. free(path);
  526. if(dir == nil)
  527. return nil;
  528. for(f = parent->child; f; f = f->childlist)
  529. if(strcmp(name, f->name) == 0)
  530. break;
  531. if(f == nil){
  532. f = emallocz(sizeof(File));
  533. f->name = estrdup(name);
  534. f->parent = parent;
  535. f->childlist = parent->child;
  536. parent->child = f;
  537. parent->ref++;
  538. f->ref = 0;
  539. filecnt++;
  540. }
  541. f->ref++;
  542. f->qid.type = dir->qid.type;
  543. f->qid.vers = dir->qid.vers;
  544. f->qidt = uniqueqid(dir);
  545. f->qid.path = f->qidt->uniqpath;
  546. f->inval = 0;
  547. free(dir);
  548. return f;
  549. }
  550. void
  551. initroot(void)
  552. {
  553. Dir *dir;
  554. root = emallocz(sizeof(File));
  555. root->name = estrdup(".");
  556. dir = dirstat(root->name);
  557. if(dir == nil)
  558. fatal("root stat");
  559. root->ref = 1;
  560. root->qid.vers = dir->qid.vers;
  561. root->qidt = uniqueqid(dir);
  562. root->qid.path = root->qidt->uniqpath;
  563. root->qid.type = QTDIR;
  564. free(dir);
  565. psmpt = emallocz(sizeof(File));
  566. psmpt->name = estrdup("/");
  567. dir = dirstat(psmpt->name);
  568. if(dir == nil)
  569. return;
  570. psmpt->ref = 1;
  571. psmpt->qid.vers = dir->qid.vers;
  572. psmpt->qidt = uniqueqid(dir);
  573. psmpt->qid.path = psmpt->qidt->uniqpath;
  574. free(dir);
  575. psmpt = file(psmpt, "mnt");
  576. if(psmpt == 0)
  577. return;
  578. psmpt = file(psmpt, "exportfs");
  579. }
  580. char*
  581. makepath(File *p, char *name)
  582. {
  583. int i, n;
  584. char *c, *s, *path, *seg[256];
  585. seg[0] = name;
  586. n = strlen(name)+2;
  587. for(i = 1; i < 256 && p; i++, p = p->parent){
  588. seg[i] = p->name;
  589. n += strlen(p->name)+1;
  590. }
  591. path = malloc(n);
  592. if(path == nil)
  593. fatal("out of memory");
  594. s = path;
  595. while(i--) {
  596. for(c = seg[i]; *c; c++)
  597. *s++ = *c;
  598. *s++ = '/';
  599. }
  600. while(s[-1] == '/')
  601. s--;
  602. *s = '\0';
  603. return path;
  604. }
  605. int
  606. qidhash(vlong path)
  607. {
  608. int h, n;
  609. h = 0;
  610. for(n=0; n<64; n+=Nqidbits){
  611. h ^= path;
  612. path >>= Nqidbits;
  613. }
  614. return h & (Nqidtab-1);
  615. }
  616. void
  617. freeqid(Qidtab *q)
  618. {
  619. ulong h;
  620. Qidtab *l;
  621. q->ref--;
  622. if(q->ref > 0)
  623. return;
  624. qfreecnt++;
  625. h = qidhash(q->path);
  626. if(qidtab[h] == q)
  627. qidtab[h] = q->next;
  628. else{
  629. for(l=qidtab[h]; l->next!=q; l=l->next)
  630. if(l->next == nil)
  631. fatal("bad qid list");
  632. l->next = q->next;
  633. }
  634. free(q);
  635. }
  636. Qidtab*
  637. qidlookup(Dir *d)
  638. {
  639. ulong h;
  640. Qidtab *q;
  641. h = qidhash(d->qid.path);
  642. for(q=qidtab[h]; q!=nil; q=q->next)
  643. if(q->type==d->type && q->dev==d->dev && q->path==d->qid.path)
  644. return q;
  645. return nil;
  646. }
  647. int
  648. qidexists(vlong path)
  649. {
  650. int h;
  651. Qidtab *q;
  652. for(h=0; h<Nqidtab; h++)
  653. for(q=qidtab[h]; q!=nil; q=q->next)
  654. if(q->uniqpath == path)
  655. return 1;
  656. return 0;
  657. }
  658. Qidtab*
  659. uniqueqid(Dir *d)
  660. {
  661. ulong h;
  662. vlong path;
  663. Qidtab *q;
  664. q = qidlookup(d);
  665. if(q != nil){
  666. q->ref++;
  667. return q;
  668. }
  669. path = d->qid.path;
  670. while(qidexists(path)){
  671. DEBUG(DFD, "collision on %s\n", d->name);
  672. /* collision: find a new one */
  673. ncollision++;
  674. path &= QIDPATH;
  675. ++newqid;
  676. if(newqid >= (1<<16)){
  677. DEBUG(DFD, "collision wraparound\n");
  678. newqid = 1;
  679. }
  680. path |= newqid<<48;
  681. DEBUG(DFD, "assign qid %.16llux\n", path);
  682. }
  683. q = mallocz(sizeof(Qidtab), 1);
  684. if(q == nil)
  685. fatal("no memory for qid table");
  686. qidcnt++;
  687. q->ref = 1;
  688. q->type = d->type;
  689. q->dev = d->dev;
  690. q->path = d->qid.path;
  691. q->uniqpath = path;
  692. h = qidhash(d->qid.path);
  693. q->next = qidtab[h];
  694. qidtab[h] = q;
  695. return q;
  696. }
  697. void
  698. fatal(char *s, ...)
  699. {
  700. char buf[ERRMAX];
  701. va_list arg;
  702. Proc *m;
  703. if (s) {
  704. va_start(arg, s);
  705. vsnprint(buf, ERRMAX, s, arg);
  706. va_end(arg);
  707. }
  708. /* Clear away the slave children */
  709. for(m = Proclist; m; m = m->next)
  710. postnote(PNPROC, m->pid, "kill");
  711. DEBUG(DFD, "%s\n", buf);
  712. if (s)
  713. sysfatal("%s", buf); /* caution: buf could contain '%' */
  714. else
  715. exits(nil);
  716. }
  717. void*
  718. emallocz(uint n)
  719. {
  720. void *p;
  721. p = mallocz(n, 1);
  722. if(p == nil)
  723. fatal(Enomem);
  724. return p;
  725. }
  726. char*
  727. estrdup(char *s)
  728. {
  729. char *t;
  730. t = strdup(s);
  731. if(t == nil)
  732. fatal(Enomem);
  733. return t;
  734. }
  735. /* Network on fd1, mount driver on fd0 */
  736. int
  737. filter(int fd, char *cmd)
  738. {
  739. int p[2], lfd, len, nb, argc;
  740. char newport[128], buf[128], devdir[40], *s, *file, *argv[16];
  741. /* Get a free port and post it to the client. */
  742. if (announce(anstring, devdir) < 0)
  743. sysfatal("filter: Cannot announce %s: %r", anstring);
  744. snprint(buf, sizeof(buf), "%s/local", devdir);
  745. buf[sizeof buf - 1] = '\0';
  746. if ((lfd = open(buf, OREAD)) < 0)
  747. sysfatal("filter: Cannot open %s: %r", buf);
  748. if ((len = read(lfd, newport, sizeof newport - 1)) < 0)
  749. sysfatal("filter: Cannot read %s: %r", buf);
  750. close(lfd);
  751. newport[len] = '\0';
  752. if ((s = strchr(newport, '\n')) != nil)
  753. *s = '\0';
  754. if ((nb = write(fd, newport, len)) < 0)
  755. sysfatal("getport; cannot write port; %r");
  756. assert(nb == len);
  757. argc = tokenize(cmd, argv, nelem(argv)-2);
  758. if (argc == 0)
  759. sysfatal("filter: empty command");
  760. argv[argc++] = buf;
  761. argv[argc] = nil;
  762. file = argv[0];
  763. if (s = strrchr(argv[0], '/'))
  764. argv[0] = s+1;
  765. if(pipe(p) < 0)
  766. fatal("pipe");
  767. switch(rfork(RFNOWAIT|RFPROC|RFFDG)) {
  768. case -1:
  769. fatal("rfork record module");
  770. case 0:
  771. if (dup(p[0], 1) < 0)
  772. fatal("filter: Cannot dup to 1; %r\n");
  773. if (dup(p[0], 0) < 0)
  774. fatal("filter: Cannot dup to 0; %r\n");
  775. close(p[0]);
  776. close(p[1]);
  777. exec(file, argv);
  778. fatal("exec record module");
  779. default:
  780. close(fd);
  781. close(p[0]);
  782. }
  783. return p[1];
  784. }
  785. static void
  786. mksecret(char *t, uchar *f)
  787. {
  788. sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  789. f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
  790. }