cpu.c 20 KB

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