cpu.c 21 KB

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