cpu.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*
  2. * cpu.c - Make a connection to a cpu server
  3. *
  4. * Invoked by listen as 'cpu -R | -N service net netdir'
  5. * by users as 'cpu [-h system] [-c cmd args ...]'
  6. */
  7. #include <u.h>
  8. #include <libc.h>
  9. #include <bio.h>
  10. #include <auth.h>
  11. #include <fcall.h>
  12. #include <libsec.h>
  13. #define Maxfdata 8192
  14. #define MaxStr 128
  15. void remoteside(int);
  16. void fatal(int, char*, ...);
  17. void lclnoteproc(int);
  18. void rmtnoteproc(void);
  19. void catcher(void*, char*);
  20. void usage(void);
  21. void writestr(int, char*, char*, int);
  22. int readstr(int, char*, int);
  23. char *rexcall(int*, char*, char*);
  24. int setamalg(char*);
  25. char *keyspec = "";
  26. int notechan;
  27. int exportpid;
  28. char *system;
  29. int cflag;
  30. int dbg;
  31. char *user;
  32. char *patternfile;
  33. char *srvname = "ncpu";
  34. char *exportfs = "/bin/exportfs";
  35. char *ealgs = "rc4_256 sha1";
  36. /* message size for exportfs; may be larger so we can do big graphics in CPU window */
  37. int msgsize = Maxfdata+IOHDRSZ;
  38. /* authentication mechanisms */
  39. static int netkeyauth(int);
  40. static int netkeysrvauth(int, char*);
  41. static int p9auth(int);
  42. static int srvp9auth(int, char*);
  43. static int noauth(int);
  44. static int srvnoauth(int, char*);
  45. typedef struct AuthMethod AuthMethod;
  46. struct AuthMethod {
  47. char *name; /* name of method */
  48. int (*cf)(int); /* client side authentication */
  49. int (*sf)(int, char*); /* server side authentication */
  50. } authmethod[] =
  51. {
  52. { "p9", p9auth, srvp9auth,},
  53. { "netkey", netkeyauth, netkeysrvauth,},
  54. // { "none", noauth, srvnoauth,},
  55. { nil, nil}
  56. };
  57. AuthMethod *am = authmethod; /* default is p9 */
  58. char *p9authproto = "p9any";
  59. int setam(char*);
  60. void
  61. usage(void)
  62. {
  63. fprint(2, "usage: cpu [-h system] [-u user] [-a authmethod] "
  64. "[-e 'crypt hash'] [-k keypattern] [-P patternfile] "
  65. "[-c cmd arg ...]\n");
  66. exits("usage");
  67. }
  68. void
  69. main(int argc, char **argv)
  70. {
  71. char dat[MaxStr], buf[MaxStr], cmd[MaxStr], *p, *err;
  72. int ac, fd, ms, data;
  73. char *av[10];
  74. /* see if we should use a larger message size */
  75. fd = open("/dev/draw", OREAD);
  76. if(fd > 0){
  77. ms = iounit(fd);
  78. if(msgsize < ms+IOHDRSZ)
  79. msgsize = ms+IOHDRSZ;
  80. close(fd);
  81. }
  82. user = getuser();
  83. if(user == nil)
  84. fatal(1, "can't read user name");
  85. ARGBEGIN{
  86. case 'a':
  87. p = EARGF(usage());
  88. if(setam(p) < 0)
  89. fatal(0, "unknown auth method %s", p);
  90. break;
  91. case 'e':
  92. ealgs = EARGF(usage());
  93. if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
  94. ealgs = nil;
  95. break;
  96. case 'd':
  97. dbg++;
  98. break;
  99. case 'f':
  100. /* ignored but accepted for compatibility */
  101. break;
  102. case 'O':
  103. p9authproto = "p9sk2";
  104. remoteside(1); /* From listen */
  105. break;
  106. case 'R': /* From listen */
  107. remoteside(0);
  108. break;
  109. case 'h':
  110. system = EARGF(usage());
  111. break;
  112. case 'c':
  113. cflag++;
  114. cmd[0] = '!';
  115. cmd[1] = '\0';
  116. while(p = ARGF()) {
  117. strcat(cmd, " ");
  118. strcat(cmd, p);
  119. }
  120. break;
  121. case 'k':
  122. keyspec = smprint("%s %s", keyspec, EARGF(usage()));
  123. break;
  124. case 'P':
  125. patternfile = EARGF(usage());
  126. break;
  127. case 'u':
  128. user = EARGF(usage());
  129. keyspec = smprint("%s user=%s", keyspec, user);
  130. break;
  131. default:
  132. usage();
  133. }ARGEND;
  134. if(argc != 0)
  135. usage();
  136. if(system == nil) {
  137. p = getenv("cpu");
  138. if(p == 0)
  139. fatal(0, "set $cpu");
  140. system = p;
  141. }
  142. if(err = rexcall(&data, system, srvname))
  143. fatal(1, "%s: %s", err, system);
  144. /* Tell the remote side the command to execute and where our working directory is */
  145. if(cflag)
  146. writestr(data, cmd, "command", 0);
  147. if(getwd(dat, sizeof(dat)) == 0)
  148. writestr(data, "NO", "dir", 0);
  149. else
  150. writestr(data, dat, "dir", 0);
  151. /* start up a process to pass along notes */
  152. lclnoteproc(data);
  153. /*
  154. * Wait for the other end to execute and start our file service
  155. * of /mnt/term
  156. */
  157. if(readstr(data, buf, sizeof(buf)) < 0)
  158. fatal(1, "waiting for FS: %r");
  159. if(strncmp("FS", buf, 2) != 0) {
  160. print("remote cpu: %s", buf);
  161. exits(buf);
  162. }
  163. /* Begin serving the gnot namespace */
  164. close(0);
  165. dup(data, 0);
  166. close(data);
  167. sprint(buf, "%d", msgsize);
  168. ac = 0;
  169. av[ac++] = exportfs;
  170. av[ac++] = "-m";
  171. av[ac++] = buf;
  172. if(dbg)
  173. av[ac++] = "-d";
  174. if(patternfile != nil){
  175. av[ac++] = "-P";
  176. av[ac++] = patternfile;
  177. }
  178. av[ac] = nil;
  179. exec(exportfs, av);
  180. fatal(1, "starting exportfs");
  181. }
  182. void
  183. fatal(int syserr, char *fmt, ...)
  184. {
  185. Fmt f;
  186. char *str;
  187. va_list arg;
  188. fmtstrinit(&f);
  189. fmtprint(&f, "cpu: ");
  190. va_start(arg, fmt);
  191. fmtvprint(&f, fmt, arg);
  192. va_end(arg);
  193. if(syserr)
  194. fmtprint(&f, ": %r");
  195. str = fmtstrflush(&f);
  196. fprint(2, "%s\n", str);
  197. syslog(0, "cpu", str);
  198. exits(str);
  199. }
  200. char *negstr = "negotiating authentication method";
  201. char bug[256];
  202. int
  203. old9p(int fd)
  204. {
  205. int p[2];
  206. if(pipe(p) < 0)
  207. fatal(1, "pipe");
  208. switch(rfork(RFPROC|RFFDG|RFNAMEG)) {
  209. case -1:
  210. fatal(1, "rfork srvold9p");
  211. case 0:
  212. if(fd != 1){
  213. dup(fd, 1);
  214. close(fd);
  215. }
  216. if(p[0] != 0){
  217. dup(p[0], 0);
  218. close(p[0]);
  219. }
  220. close(p[1]);
  221. if(0){
  222. fd = open("/sys/log/cpu", OWRITE);
  223. if(fd != 2){
  224. dup(fd, 2);
  225. close(fd);
  226. }
  227. execl("/bin/srvold9p", "srvold9p", "-ds", nil);
  228. } else
  229. execl("/bin/srvold9p", "srvold9p", "-s", nil);
  230. fatal(1, "exec srvold9p");
  231. default:
  232. close(fd);
  233. close(p[0]);
  234. }
  235. return p[1];
  236. }
  237. /* Invoked with stdin, stdout and stderr connected to the network connection */
  238. void
  239. remoteside(int old)
  240. {
  241. char user[MaxStr], home[MaxStr], buf[MaxStr], xdir[MaxStr], cmd[MaxStr];
  242. int i, n, fd, badchdir, gotcmd;
  243. rfork(RFENVG);
  244. putenv("service", "cpu");
  245. fd = 0;
  246. /* negotiate authentication mechanism */
  247. n = readstr(fd, cmd, sizeof(cmd));
  248. if(n < 0)
  249. fatal(1, "authenticating");
  250. if(setamalg(cmd) < 0){
  251. writestr(fd, "unsupported auth method", nil, 0);
  252. fatal(1, "bad auth method %s", cmd);
  253. } else
  254. writestr(fd, "", "", 1);
  255. fd = (*am->sf)(fd, user);
  256. if(fd < 0)
  257. fatal(1, "srvauth");
  258. /* Set environment values for the user */
  259. putenv("user", user);
  260. sprint(home, "/usr/%s", user);
  261. putenv("home", home);
  262. /* Now collect invoking cpu's current directory or possibly a command */
  263. gotcmd = 0;
  264. if(readstr(fd, xdir, sizeof(xdir)) < 0)
  265. fatal(1, "dir/cmd");
  266. if(xdir[0] == '!') {
  267. strcpy(cmd, &xdir[1]);
  268. gotcmd = 1;
  269. if(readstr(fd, xdir, sizeof(xdir)) < 0)
  270. fatal(1, "dir");
  271. }
  272. /* Establish the new process at the current working directory of the
  273. * gnot */
  274. badchdir = 0;
  275. if(strcmp(xdir, "NO") == 0)
  276. chdir(home);
  277. else if(chdir(xdir) < 0) {
  278. badchdir = 1;
  279. chdir(home);
  280. }
  281. /* Start the gnot serving its namespace */
  282. writestr(fd, "FS", "FS", 0);
  283. writestr(fd, "/", "exportfs dir", 0);
  284. n = read(fd, buf, sizeof(buf));
  285. if(n != 2 || buf[0] != 'O' || buf[1] != 'K')
  286. exits("remote tree");
  287. if(old)
  288. fd = old9p(fd);
  289. /* make sure buffers are big by doing fversion explicitly; pick a huge number; other side will trim */
  290. strcpy(buf, VERSION9P);
  291. if(fversion(fd, 64*1024, buf, sizeof buf) < 0)
  292. exits("fversion failed");
  293. if(mount(fd, -1, "/mnt/term", MCREATE|MREPL, "") < 0)
  294. exits("mount failed");
  295. close(fd);
  296. /* the remote noteproc uses the mount so it must follow it */
  297. rmtnoteproc();
  298. for(i = 0; i < 3; i++)
  299. close(i);
  300. if(open("/mnt/term/dev/cons", OREAD) != 0)
  301. exits("open stdin");
  302. if(open("/mnt/term/dev/cons", OWRITE) != 1)
  303. exits("open stdout");
  304. dup(1, 2);
  305. if(badchdir)
  306. print("cpu: failed to chdir to '%s'\n", xdir);
  307. if(gotcmd)
  308. execl("/bin/rc", "rc", "-lc", cmd, nil);
  309. else
  310. execl("/bin/rc", "rc", "-li", nil);
  311. fatal(1, "exec shell");
  312. }
  313. char*
  314. rexcall(int *fd, char *host, char *service)
  315. {
  316. char *na;
  317. char dir[MaxStr];
  318. char err[ERRMAX];
  319. char msg[MaxStr];
  320. int n;
  321. na = netmkaddr(host, 0, service);
  322. if((*fd = dial(na, 0, dir, 0)) < 0)
  323. return "can't dial";
  324. /* negotiate authentication mechanism */
  325. if(ealgs != nil)
  326. snprint(msg, sizeof(msg), "%s %s", am->name, ealgs);
  327. else
  328. snprint(msg, sizeof(msg), "%s", am->name);
  329. writestr(*fd, msg, negstr, 0);
  330. n = readstr(*fd, err, sizeof err);
  331. if(n < 0)
  332. return negstr;
  333. if(*err){
  334. werrstr(err);
  335. return negstr;
  336. }
  337. /* authenticate */
  338. *fd = (*am->cf)(*fd);
  339. if(*fd < 0)
  340. return "can't authenticate";
  341. return 0;
  342. }
  343. void
  344. writestr(int fd, char *str, char *thing, int ignore)
  345. {
  346. int l, n;
  347. l = strlen(str);
  348. n = write(fd, str, l+1);
  349. if(!ignore && n < 0)
  350. fatal(1, "writing network: %s", thing);
  351. }
  352. int
  353. readstr(int fd, char *str, int len)
  354. {
  355. int n;
  356. while(len) {
  357. n = read(fd, str, 1);
  358. if(n < 0)
  359. return -1;
  360. if(*str == '\0')
  361. return 0;
  362. str++;
  363. len--;
  364. }
  365. return -1;
  366. }
  367. static int
  368. readln(char *buf, int n)
  369. {
  370. int i;
  371. char *p;
  372. n--; /* room for \0 */
  373. p = buf;
  374. for(i=0; i<n; i++){
  375. if(read(0, p, 1) != 1)
  376. break;
  377. if(*p == '\n' || *p == '\r')
  378. break;
  379. p++;
  380. }
  381. *p = '\0';
  382. return p-buf;
  383. }
  384. /*
  385. * user level challenge/response
  386. */
  387. static int
  388. netkeyauth(int fd)
  389. {
  390. char chall[32];
  391. char resp[32];
  392. strecpy(chall, chall+sizeof chall, getuser());
  393. print("user[%s]: ", chall);
  394. if(readln(resp, sizeof(resp)) < 0)
  395. return -1;
  396. if(*resp != 0)
  397. strcpy(chall, resp);
  398. writestr(fd, chall, "challenge/response", 1);
  399. for(;;){
  400. if(readstr(fd, chall, sizeof chall) < 0)
  401. break;
  402. if(*chall == 0)
  403. return fd;
  404. print("challenge: %s\nresponse: ", chall);
  405. if(readln(resp, sizeof(resp)) < 0)
  406. break;
  407. writestr(fd, resp, "challenge/response", 1);
  408. }
  409. return -1;
  410. }
  411. static int
  412. netkeysrvauth(int fd, char *user)
  413. {
  414. char response[32];
  415. Chalstate *ch;
  416. int tries;
  417. AuthInfo *ai;
  418. if(readstr(fd, user, 32) < 0)
  419. return -1;
  420. ai = nil;
  421. ch = nil;
  422. for(tries = 0; tries < 10; tries++){
  423. if((ch = auth_challenge("proto=p9cr role=server user=%q", user)) == nil)
  424. return -1;
  425. writestr(fd, ch->chal, "challenge", 1);
  426. if(readstr(fd, response, sizeof response) < 0)
  427. return -1;
  428. ch->resp = response;
  429. ch->nresp = strlen(response);
  430. if((ai = auth_response(ch)) != nil)
  431. break;
  432. }
  433. auth_freechal(ch);
  434. if(ai == nil)
  435. return -1;
  436. writestr(fd, "", "challenge", 1);
  437. if(auth_chuid(ai, 0) < 0)
  438. fatal(1, "newns");
  439. auth_freeAI(ai);
  440. return fd;
  441. }
  442. static void
  443. mksecret(char *t, uchar *f)
  444. {
  445. sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  446. f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
  447. }
  448. /*
  449. * plan9 authentication followed by rc4 encryption
  450. */
  451. static int
  452. p9auth(int fd)
  453. {
  454. uchar key[16];
  455. uchar digest[SHA1dlen];
  456. char fromclientsecret[21];
  457. char fromserversecret[21];
  458. int i;
  459. AuthInfo *ai;
  460. ai = auth_proxy(fd, auth_getkey, "proto=%q role=client %s", p9authproto, keyspec);
  461. if(ai == nil)
  462. return -1;
  463. memmove(key+4, ai->secret, ai->nsecret);
  464. if(ealgs == nil)
  465. return fd;
  466. /* exchange random numbers */
  467. srand(truerand());
  468. for(i = 0; i < 4; i++)
  469. key[i] = rand();
  470. if(write(fd, key, 4) != 4)
  471. return -1;
  472. if(readn(fd, key+12, 4) != 4)
  473. return -1;
  474. /* scramble into two secrets */
  475. sha1(key, sizeof(key), digest, nil);
  476. mksecret(fromclientsecret, digest);
  477. mksecret(fromserversecret, digest+10);
  478. /* set up encryption */
  479. i = pushssl(fd, ealgs, fromclientsecret, fromserversecret, nil);
  480. if(i < 0)
  481. werrstr("can't establish ssl connection: %r");
  482. return i;
  483. }
  484. static int
  485. noauth(int fd)
  486. {
  487. ealgs = nil;
  488. return fd;
  489. }
  490. static int
  491. srvnoauth(int fd, char *user)
  492. {
  493. strecpy(user, user+MaxStr, getuser());
  494. ealgs = nil;
  495. newns(user, nil);
  496. return fd;
  497. }
  498. void
  499. loghex(uchar *p, int n)
  500. {
  501. char buf[100];
  502. int i;
  503. for(i = 0; i < n; i++)
  504. sprint(buf+2*i, "%2.2ux", p[i]);
  505. syslog(0, "cpu", buf);
  506. }
  507. static int
  508. srvp9auth(int fd, char *user)
  509. {
  510. uchar key[16];
  511. uchar digest[SHA1dlen];
  512. char fromclientsecret[21];
  513. char fromserversecret[21];
  514. int i;
  515. AuthInfo *ai;
  516. ai = auth_proxy(0, nil, "proto=%q role=server %s", p9authproto, keyspec);
  517. if(ai == nil)
  518. return -1;
  519. if(auth_chuid(ai, nil) < 0)
  520. return -1;
  521. strecpy(user, user+MaxStr, ai->cuid);
  522. memmove(key+4, ai->secret, ai->nsecret);
  523. if(ealgs == nil)
  524. return fd;
  525. /* exchange random numbers */
  526. srand(truerand());
  527. for(i = 0; i < 4; i++)
  528. key[i+12] = rand();
  529. if(readn(fd, key, 4) != 4)
  530. return -1;
  531. if(write(fd, key+12, 4) != 4)
  532. return -1;
  533. /* scramble into two secrets */
  534. sha1(key, sizeof(key), digest, nil);
  535. mksecret(fromclientsecret, digest);
  536. mksecret(fromserversecret, digest+10);
  537. /* set up encryption */
  538. i = pushssl(fd, ealgs, fromserversecret, fromclientsecret, nil);
  539. if(i < 0)
  540. werrstr("can't establish ssl connection: %r");
  541. return i;
  542. }
  543. /*
  544. * set authentication mechanism
  545. */
  546. int
  547. setam(char *name)
  548. {
  549. for(am = authmethod; am->name != nil; am++)
  550. if(strcmp(am->name, name) == 0)
  551. return 0;
  552. am = authmethod;
  553. return -1;
  554. }
  555. /*
  556. * set authentication mechanism and encryption/hash algs
  557. */
  558. int
  559. setamalg(char *s)
  560. {
  561. ealgs = strchr(s, ' ');
  562. if(ealgs != nil)
  563. *ealgs++ = 0;
  564. return setam(s);
  565. }
  566. char *rmtnotefile = "/mnt/term/dev/cpunote";
  567. /*
  568. * loop reading /mnt/term/dev/note looking for notes.
  569. * The child returns to start the shell.
  570. */
  571. void
  572. rmtnoteproc(void)
  573. {
  574. int n, fd, pid, notepid;
  575. char buf[256];
  576. /* new proc returns to start shell */
  577. pid = rfork(RFPROC|RFFDG|RFNOTEG|RFNAMEG|RFMEM);
  578. switch(pid){
  579. case -1:
  580. syslog(0, "cpu", "cpu -R: can't start noteproc: %r");
  581. return;
  582. case 0:
  583. return;
  584. }
  585. /* new proc reads notes from other side and posts them to shell */
  586. switch(notepid = rfork(RFPROC|RFFDG|RFMEM)){
  587. case -1:
  588. syslog(0, "cpu", "cpu -R: can't start wait proc: %r");
  589. _exits(0);
  590. case 0:
  591. fd = open(rmtnotefile, OREAD);
  592. if(fd < 0){
  593. syslog(0, "cpu", "cpu -R: can't open %s", rmtnotefile);
  594. _exits(0);
  595. }
  596. for(;;){
  597. n = read(fd, buf, sizeof(buf)-1);
  598. if(n <= 0){
  599. postnote(PNGROUP, pid, "hangup");
  600. _exits(0);
  601. }
  602. buf[n] = 0;
  603. postnote(PNGROUP, pid, buf);
  604. }
  605. }
  606. /* original proc waits for shell proc to die and kills note proc */
  607. for(;;){
  608. n = waitpid();
  609. if(n < 0 || n == pid)
  610. break;
  611. }
  612. postnote(PNPROC, notepid, "kill");
  613. _exits(0);
  614. }
  615. enum
  616. {
  617. Qdir,
  618. Qcpunote,
  619. Nfid = 32,
  620. };
  621. struct {
  622. char *name;
  623. Qid qid;
  624. ulong perm;
  625. } fstab[] =
  626. {
  627. [Qdir] { ".", {Qdir, 0, QTDIR}, DMDIR|0555 },
  628. [Qcpunote] { "cpunote", {Qcpunote, 0}, 0444 },
  629. };
  630. typedef struct Note Note;
  631. struct Note
  632. {
  633. Note *next;
  634. char msg[ERRMAX];
  635. };
  636. typedef struct Request Request;
  637. struct Request
  638. {
  639. Request *next;
  640. Fcall f;
  641. };
  642. typedef struct Fid Fid;
  643. struct Fid
  644. {
  645. int fid;
  646. int file;
  647. int omode;
  648. };
  649. Fid fids[Nfid];
  650. struct {
  651. Lock;
  652. Note *nfirst, *nlast;
  653. Request *rfirst, *rlast;
  654. } nfs;
  655. int
  656. fsreply(int fd, Fcall *f)
  657. {
  658. uchar buf[IOHDRSZ+Maxfdata];
  659. int n;
  660. if(dbg)
  661. fprint(2, "notefs: <-%F\n", f);
  662. n = convS2M(f, buf, sizeof buf);
  663. if(n > 0){
  664. if(write(fd, buf, n) != n){
  665. close(fd);
  666. return -1;
  667. }
  668. }
  669. return 0;
  670. }
  671. /* match a note read request with a note, reply to the request */
  672. int
  673. kick(int fd)
  674. {
  675. Request *rp;
  676. Note *np;
  677. int rv;
  678. for(;;){
  679. lock(&nfs);
  680. rp = nfs.rfirst;
  681. np = nfs.nfirst;
  682. if(rp == nil || np == nil){
  683. unlock(&nfs);
  684. break;
  685. }
  686. nfs.rfirst = rp->next;
  687. nfs.nfirst = np->next;
  688. unlock(&nfs);
  689. rp->f.type = Rread;
  690. rp->f.count = strlen(np->msg);
  691. rp->f.data = np->msg;
  692. rv = fsreply(fd, &rp->f);
  693. free(rp);
  694. free(np);
  695. if(rv < 0)
  696. return -1;
  697. }
  698. return 0;
  699. }
  700. void
  701. flushreq(int tag)
  702. {
  703. Request **l, *rp;
  704. lock(&nfs);
  705. for(l = &nfs.rfirst; *l != nil; l = &(*l)->next){
  706. rp = *l;
  707. if(rp->f.tag == tag){
  708. *l = rp->next;
  709. unlock(&nfs);
  710. free(rp);
  711. return;
  712. }
  713. }
  714. unlock(&nfs);
  715. }
  716. Fid*
  717. getfid(int fid)
  718. {
  719. int i, freefid;
  720. freefid = -1;
  721. for(i = 0; i < Nfid; i++){
  722. if(freefid < 0 && fids[i].file < 0)
  723. freefid = i;
  724. if(fids[i].fid == fid)
  725. return &fids[i];
  726. }
  727. if(freefid >= 0){
  728. fids[freefid].fid = fid;
  729. return &fids[freefid];
  730. }
  731. return nil;
  732. }
  733. int
  734. fsstat(int fd, Fid *fid, Fcall *f)
  735. {
  736. Dir d;
  737. uchar statbuf[256];
  738. memset(&d, 0, sizeof(d));
  739. d.name = fstab[fid->file].name;
  740. d.uid = user;
  741. d.gid = user;
  742. d.muid = user;
  743. d.qid = fstab[fid->file].qid;
  744. d.mode = fstab[fid->file].perm;
  745. d.atime = d.mtime = time(0);
  746. f->stat = statbuf;
  747. f->nstat = convD2M(&d, statbuf, sizeof statbuf);
  748. return fsreply(fd, f);
  749. }
  750. int
  751. fsread(int fd, Fid *fid, Fcall *f)
  752. {
  753. Dir d;
  754. uchar buf[256];
  755. Request *rp;
  756. switch(fid->file){
  757. default:
  758. return -1;
  759. case Qdir:
  760. if(f->offset == 0 && f->count >0){
  761. memset(&d, 0, sizeof(d));
  762. d.name = fstab[Qcpunote].name;
  763. d.uid = user;
  764. d.gid = user;
  765. d.muid = user;
  766. d.qid = fstab[Qcpunote].qid;
  767. d.mode = fstab[Qcpunote].perm;
  768. d.atime = d.mtime = time(0);
  769. f->count = convD2M(&d, buf, sizeof buf);
  770. f->data = (char*)buf;
  771. } else
  772. f->count = 0;
  773. return fsreply(fd, f);
  774. case Qcpunote:
  775. rp = mallocz(sizeof(*rp), 1);
  776. if(rp == nil)
  777. return -1;
  778. rp->f = *f;
  779. lock(&nfs);
  780. if(nfs.rfirst == nil)
  781. nfs.rfirst = rp;
  782. else
  783. nfs.rlast->next = rp;
  784. nfs.rlast = rp;
  785. unlock(&nfs);
  786. return kick(fd);;
  787. }
  788. }
  789. char Eperm[] = "permission denied";
  790. char Enofile[] = "out of files";
  791. char Enotdir[] = "not a directory";
  792. void
  793. notefs(int fd)
  794. {
  795. uchar buf[IOHDRSZ+Maxfdata];
  796. int i, n, ncpunote;
  797. Fcall f;
  798. Qid wqid[MAXWELEM];
  799. Fid *fid, *nfid;
  800. int doreply;
  801. rfork(RFNOTEG);
  802. fmtinstall('F', fcallfmt);
  803. for(n = 0; n < Nfid; n++){
  804. fids[n].file = -1;
  805. fids[n].omode = -1;
  806. }
  807. ncpunote = 0;
  808. for(;;){
  809. n = read9pmsg(fd, buf, sizeof(buf));
  810. if(n <= 0){
  811. if(dbg)
  812. fprint(2, "read9pmsg(%d) returns %d: %r\n", fd, n);
  813. break;
  814. }
  815. if(convM2S(buf, n, &f) <= BIT16SZ)
  816. break;
  817. if(dbg)
  818. fprint(2, "notefs: ->%F\n", &f);
  819. doreply = 1;
  820. fid = getfid(f.fid);
  821. if(fid == nil){
  822. nofids:
  823. f.type = Rerror;
  824. f.ename = Enofile;
  825. fsreply(fd, &f);
  826. continue;
  827. }
  828. switch(f.type++){
  829. default:
  830. f.type = Rerror;
  831. f.ename = "unknown type";
  832. break;
  833. case Tflush:
  834. flushreq(f.oldtag);
  835. break;
  836. case Tversion:
  837. if(f.msize > IOHDRSZ+Maxfdata)
  838. f.msize = IOHDRSZ+Maxfdata;
  839. break;
  840. case Tauth:
  841. f.type = Rerror;
  842. f.ename = "authentication not required";
  843. break;
  844. case Tattach:
  845. f.qid = fstab[Qdir].qid;
  846. fid->file = Qdir;
  847. break;
  848. case Twalk:
  849. nfid = nil;
  850. if(f.newfid != f.fid){
  851. nfid = getfid(f.newfid);
  852. if(nfid == nil)
  853. goto nofids;
  854. nfid->file = fid->file;
  855. fid = nfid;
  856. }
  857. for(i=0; i<f.nwname && i<MAXWELEM; i++){
  858. if(fid->file != Qdir){
  859. f.type = Rerror;
  860. f.ename = Enotdir;
  861. break;
  862. }
  863. if(strcmp(f.wname[i], "..") == 0){
  864. wqid[i] = fstab[Qdir].qid;
  865. continue;
  866. }
  867. if(strcmp(f.wname[i], "cpunote") != 0){
  868. if(i == 0){
  869. f.type = Rerror;
  870. f.ename = "file does not exist";
  871. }
  872. break;
  873. }
  874. fid->file = Qcpunote;
  875. wqid[i] = fstab[Qcpunote].qid;
  876. }
  877. if(nfid != nil && (f.type == Rerror || i < f.nwname))
  878. nfid ->file = -1;
  879. if(f.type != Rerror){
  880. f.nwqid = i;
  881. for(i=0; i<f.nwqid; i++)
  882. f.wqid[i] = wqid[i];
  883. }
  884. break;
  885. case Topen:
  886. if(f.mode != OREAD){
  887. f.type = Rerror;
  888. f.ename = Eperm;
  889. break;
  890. }
  891. fid->omode = f.mode;
  892. if(fid->file == Qcpunote)
  893. ncpunote++;
  894. f.qid = fstab[fid->file].qid;
  895. f.iounit = 0;
  896. break;
  897. case Tread:
  898. if(fsread(fd, fid, &f) < 0)
  899. goto err;
  900. doreply = 0;
  901. break;
  902. case Tclunk:
  903. if(fid->omode != -1 && fid->file == Qcpunote){
  904. ncpunote--;
  905. if(ncpunote == 0) /* remote side is done */
  906. goto err;
  907. }
  908. fid->file = -1;
  909. fid->omode = -1;
  910. break;
  911. case Tstat:
  912. if(fsstat(fd, fid, &f) < 0)
  913. goto err;
  914. doreply = 0;
  915. break;
  916. case Tcreate:
  917. case Twrite:
  918. case Tremove:
  919. case Twstat:
  920. f.type = Rerror;
  921. f.ename = Eperm;
  922. break;
  923. }
  924. if(doreply)
  925. if(fsreply(fd, &f) < 0)
  926. break;
  927. }
  928. err:
  929. if(dbg)
  930. fprint(2, "notefs exiting: %r\n");
  931. werrstr("success");
  932. postnote(PNGROUP, exportpid, "kill");
  933. if(dbg)
  934. fprint(2, "postnote PNGROUP %d: %r\n", exportpid);
  935. close(fd);
  936. }
  937. char notebuf[ERRMAX];
  938. void
  939. catcher(void*, char *text)
  940. {
  941. int n;
  942. n = strlen(text);
  943. if(n >= sizeof(notebuf))
  944. n = sizeof(notebuf)-1;
  945. memmove(notebuf, text, n);
  946. notebuf[n] = '\0';
  947. noted(NCONT);
  948. }
  949. /*
  950. * mount in /dev a note file for the remote side to read.
  951. */
  952. void
  953. lclnoteproc(int netfd)
  954. {
  955. Waitmsg *w;
  956. Note *np;
  957. int pfd[2];
  958. int pid;
  959. if(pipe(pfd) < 0){
  960. fprint(2, "cpu: can't start note proc: pipe: %r\n");
  961. return;
  962. }
  963. /* new proc mounts and returns to start exportfs */
  964. switch(pid = rfork(RFPROC|RFNAMEG|RFFDG|RFMEM)){
  965. default:
  966. exportpid = pid;
  967. break;
  968. case -1:
  969. fprint(2, "cpu: can't start note proc: rfork: %r\n");
  970. return;
  971. case 0:
  972. close(pfd[0]);
  973. if(mount(pfd[1], -1, "/dev", MBEFORE, "") < 0)
  974. fprint(2, "cpu: can't mount note proc: %r\n");
  975. close(pfd[1]);
  976. return;
  977. }
  978. close(netfd);
  979. close(pfd[1]);
  980. /* new proc listens for note file system rpc's */
  981. switch(rfork(RFPROC|RFNAMEG|RFMEM)){
  982. case -1:
  983. fprint(2, "cpu: can't start note proc: rfork1: %r\n");
  984. _exits(0);
  985. case 0:
  986. notefs(pfd[0]);
  987. _exits(0);
  988. }
  989. /* original proc waits for notes */
  990. notify(catcher);
  991. w = nil;
  992. for(;;) {
  993. *notebuf = 0;
  994. free(w);
  995. w = wait();
  996. if(w == nil) {
  997. if(*notebuf == 0)
  998. break;
  999. np = mallocz(sizeof(Note), 1);
  1000. if(np != nil){
  1001. strcpy(np->msg, notebuf);
  1002. lock(&nfs);
  1003. if(nfs.nfirst == nil)
  1004. nfs.nfirst = np;
  1005. else
  1006. nfs.nlast->next = np;
  1007. nfs.nlast = np;
  1008. unlock(&nfs);
  1009. kick(pfd[0]);
  1010. }
  1011. unlock(&nfs);
  1012. } else if(w->pid == exportpid)
  1013. break;
  1014. }
  1015. if(w == nil)
  1016. exits(nil);
  1017. exits(0);
  1018. /* exits(w->msg); */
  1019. }