cpu.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 = EARGF(usage());
  121. break;
  122. case 'P':
  123. patternfile = EARGF(usage());
  124. break;
  125. case 'u':
  126. user = EARGF(usage());
  127. keyspec = smprint("user=%s", 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. return fd;
  494. }
  495. void
  496. loghex(uchar *p, int n)
  497. {
  498. char buf[100];
  499. int i;
  500. for(i = 0; i < n; i++)
  501. sprint(buf+2*i, "%2.2ux", p[i]);
  502. syslog(0, "cpu", buf);
  503. }
  504. static int
  505. srvp9auth(int fd, char *user)
  506. {
  507. uchar key[16];
  508. uchar digest[SHA1dlen];
  509. char fromclientsecret[21];
  510. char fromserversecret[21];
  511. int i;
  512. AuthInfo *ai;
  513. ai = auth_proxy(0, nil, "proto=%q role=server %s", p9authproto, keyspec);
  514. if(ai == nil)
  515. return -1;
  516. if(auth_chuid(ai, nil) < 0)
  517. return -1;
  518. strecpy(user, user+MaxStr, ai->cuid);
  519. memmove(key+4, ai->secret, ai->nsecret);
  520. if(ealgs == nil)
  521. return fd;
  522. /* exchange random numbers */
  523. srand(truerand());
  524. for(i = 0; i < 4; i++)
  525. key[i+12] = rand();
  526. if(readn(fd, key, 4) != 4)
  527. return -1;
  528. if(write(fd, key+12, 4) != 4)
  529. return -1;
  530. /* scramble into two secrets */
  531. sha1(key, sizeof(key), digest, nil);
  532. mksecret(fromclientsecret, digest);
  533. mksecret(fromserversecret, digest+10);
  534. /* set up encryption */
  535. i = pushssl(fd, ealgs, fromserversecret, fromclientsecret, nil);
  536. if(i < 0)
  537. werrstr("can't establish ssl connection: %r");
  538. return i;
  539. }
  540. /*
  541. * set authentication mechanism
  542. */
  543. int
  544. setam(char *name)
  545. {
  546. for(am = authmethod; am->name != nil; am++)
  547. if(strcmp(am->name, name) == 0)
  548. return 0;
  549. am = authmethod;
  550. return -1;
  551. }
  552. /*
  553. * set authentication mechanism and encryption/hash algs
  554. */
  555. int
  556. setamalg(char *s)
  557. {
  558. ealgs = strchr(s, ' ');
  559. if(ealgs != nil)
  560. *ealgs++ = 0;
  561. return setam(s);
  562. }
  563. char *rmtnotefile = "/mnt/term/dev/cpunote";
  564. /*
  565. * loop reading /mnt/term/dev/note looking for notes.
  566. * The child returns to start the shell.
  567. */
  568. void
  569. rmtnoteproc(void)
  570. {
  571. int n, fd, pid, notepid;
  572. char buf[256];
  573. /* new proc returns to start shell */
  574. pid = rfork(RFPROC|RFFDG|RFNOTEG|RFNAMEG|RFMEM);
  575. switch(pid){
  576. case -1:
  577. syslog(0, "cpu", "cpu -R: can't start noteproc: %r");
  578. return;
  579. case 0:
  580. return;
  581. }
  582. /* new proc reads notes from other side and posts them to shell */
  583. switch(notepid = rfork(RFPROC|RFFDG|RFMEM)){
  584. case -1:
  585. syslog(0, "cpu", "cpu -R: can't start wait proc: %r");
  586. _exits(0);
  587. case 0:
  588. fd = open(rmtnotefile, OREAD);
  589. if(fd < 0){
  590. syslog(0, "cpu", "cpu -R: can't open %s", rmtnotefile);
  591. _exits(0);
  592. }
  593. for(;;){
  594. n = read(fd, buf, sizeof(buf)-1);
  595. if(n <= 0){
  596. postnote(PNGROUP, pid, "hangup");
  597. _exits(0);
  598. }
  599. buf[n] = 0;
  600. postnote(PNGROUP, pid, buf);
  601. }
  602. }
  603. /* original proc waits for shell proc to die and kills note proc */
  604. for(;;){
  605. n = waitpid();
  606. if(n < 0 || n == pid)
  607. break;
  608. }
  609. postnote(PNPROC, notepid, "kill");
  610. _exits(0);
  611. }
  612. enum
  613. {
  614. Qdir,
  615. Qcpunote,
  616. Nfid = 32,
  617. };
  618. struct {
  619. char *name;
  620. Qid qid;
  621. ulong perm;
  622. } fstab[] =
  623. {
  624. [Qdir] { ".", {Qdir, 0, QTDIR}, DMDIR|0555 },
  625. [Qcpunote] { "cpunote", {Qcpunote, 0}, 0444 },
  626. };
  627. typedef struct Note Note;
  628. struct Note
  629. {
  630. Note *next;
  631. char msg[ERRMAX];
  632. };
  633. typedef struct Request Request;
  634. struct Request
  635. {
  636. Request *next;
  637. Fcall f;
  638. };
  639. typedef struct Fid Fid;
  640. struct Fid
  641. {
  642. int fid;
  643. int file;
  644. int omode;
  645. };
  646. Fid fids[Nfid];
  647. struct {
  648. Lock;
  649. Note *nfirst, *nlast;
  650. Request *rfirst, *rlast;
  651. } nfs;
  652. int
  653. fsreply(int fd, Fcall *f)
  654. {
  655. uchar buf[IOHDRSZ+Maxfdata];
  656. int n;
  657. if(dbg)
  658. fprint(2, "<-%F\n", f);
  659. n = convS2M(f, buf, sizeof buf);
  660. if(n > 0){
  661. if(write(fd, buf, n) != n){
  662. close(fd);
  663. return -1;
  664. }
  665. }
  666. return 0;
  667. }
  668. /* match a note read request with a note, reply to the request */
  669. int
  670. kick(int fd)
  671. {
  672. Request *rp;
  673. Note *np;
  674. int rv;
  675. for(;;){
  676. lock(&nfs);
  677. rp = nfs.rfirst;
  678. np = nfs.nfirst;
  679. if(rp == nil || np == nil){
  680. unlock(&nfs);
  681. break;
  682. }
  683. nfs.rfirst = rp->next;
  684. nfs.nfirst = np->next;
  685. unlock(&nfs);
  686. rp->f.type = Rread;
  687. rp->f.count = strlen(np->msg);
  688. rp->f.data = np->msg;
  689. rv = fsreply(fd, &rp->f);
  690. free(rp);
  691. free(np);
  692. if(rv < 0)
  693. return -1;
  694. }
  695. return 0;
  696. }
  697. void
  698. flushreq(int tag)
  699. {
  700. Request **l, *rp;
  701. lock(&nfs);
  702. for(l = &nfs.rfirst; *l != nil; l = &(*l)->next){
  703. rp = *l;
  704. if(rp->f.tag == tag){
  705. *l = rp->next;
  706. unlock(&nfs);
  707. free(rp);
  708. return;
  709. }
  710. }
  711. unlock(&nfs);
  712. }
  713. Fid*
  714. getfid(int fid)
  715. {
  716. int i, freefid;
  717. freefid = -1;
  718. for(i = 0; i < Nfid; i++){
  719. if(freefid < 0 && fids[i].file < 0)
  720. freefid = i;
  721. if(fids[i].fid == fid)
  722. return &fids[i];
  723. }
  724. if(freefid >= 0){
  725. fids[freefid].fid = fid;
  726. return &fids[freefid];
  727. }
  728. return nil;
  729. }
  730. int
  731. fsstat(int fd, Fid *fid, Fcall *f)
  732. {
  733. Dir d;
  734. uchar statbuf[256];
  735. memset(&d, 0, sizeof(d));
  736. d.name = fstab[fid->file].name;
  737. d.uid = user;
  738. d.gid = user;
  739. d.muid = user;
  740. d.qid = fstab[fid->file].qid;
  741. d.mode = fstab[fid->file].perm;
  742. d.atime = d.mtime = time(0);
  743. f->stat = statbuf;
  744. f->nstat = convD2M(&d, statbuf, sizeof statbuf);
  745. return fsreply(fd, f);
  746. }
  747. int
  748. fsread(int fd, Fid *fid, Fcall *f)
  749. {
  750. Dir d;
  751. uchar buf[256];
  752. Request *rp;
  753. switch(fid->file){
  754. default:
  755. return -1;
  756. case Qdir:
  757. if(f->offset == 0 && f->count >0){
  758. memset(&d, 0, sizeof(d));
  759. d.name = fstab[Qcpunote].name;
  760. d.uid = user;
  761. d.gid = user;
  762. d.muid = user;
  763. d.qid = fstab[Qcpunote].qid;
  764. d.mode = fstab[Qcpunote].perm;
  765. d.atime = d.mtime = time(0);
  766. f->count = convD2M(&d, buf, sizeof buf);
  767. f->data = (char*)buf;
  768. } else
  769. f->count = 0;
  770. return fsreply(fd, f);
  771. case Qcpunote:
  772. rp = mallocz(sizeof(*rp), 1);
  773. if(rp == nil)
  774. return -1;
  775. rp->f = *f;
  776. lock(&nfs);
  777. if(nfs.rfirst == nil)
  778. nfs.rfirst = rp;
  779. else
  780. nfs.rlast->next = rp;
  781. nfs.rlast = rp;
  782. unlock(&nfs);
  783. return kick(fd);;
  784. }
  785. }
  786. char Eperm[] = "permission denied";
  787. char Enofile[] = "out of files";
  788. char Enotdir[] = "not a directory";
  789. void
  790. notefs(int fd)
  791. {
  792. uchar buf[IOHDRSZ+Maxfdata];
  793. int i, j, n, ncpunote;
  794. char err[ERRMAX];
  795. Fcall f;
  796. Fid *fid, *nfid;
  797. int doreply;
  798. rfork(RFNOTEG);
  799. fmtinstall('F', fcallfmt);
  800. for(n = 0; n < Nfid; n++){
  801. fids[n].file = -1;
  802. fids[n].omode = -1;
  803. }
  804. ncpunote = 0;
  805. for(;;){
  806. n = read9pmsg(fd, buf, sizeof(buf));
  807. if(n <= 0){
  808. if(dbg)
  809. fprint(2, "read9pmsg(%d) returns %d: %r\n", fd, n);
  810. break;
  811. }
  812. if(convM2S(buf, n, &f) <= BIT16SZ)
  813. break;
  814. if(dbg)
  815. fprint(2, "->%F\n", &f);
  816. doreply = 1;
  817. fid = getfid(f.fid);
  818. if(fid == nil){
  819. nofids:
  820. f.type = Rerror;
  821. f.ename = Enofile;
  822. fsreply(fd, &f);
  823. continue;
  824. }
  825. switch(f.type++){
  826. default:
  827. f.type = Rerror;
  828. f.ename = "unknown type";
  829. break;
  830. case Tflush:
  831. flushreq(f.oldtag);
  832. break;
  833. case Tversion:
  834. if(f.msize > IOHDRSZ+Maxfdata)
  835. f.msize = IOHDRSZ+Maxfdata;
  836. break;
  837. case Tauth:
  838. f.type = Rerror;
  839. f.ename = "cpu: authentication not required";
  840. break;
  841. case Tattach:
  842. f.qid = fstab[Qdir].qid;
  843. fid->file = Qdir;
  844. break;
  845. case Twalk:
  846. nfid = nil;
  847. if(f.newfid != f.fid){
  848. nfid = getfid(f.newfid);
  849. if(nfid == nil)
  850. goto nofids;
  851. nfid->file = fid->file;
  852. fid = nfid;
  853. }
  854. f.ename = nil;
  855. for(i=0; i<f.nwname; i++){
  856. if(i > MAXWELEM){
  857. f.type = Rerror;
  858. f.ename = "too many name elements";
  859. break;
  860. }
  861. if(fid->file != Qdir){
  862. f.type = Rerror;
  863. f.ename = Enotdir;
  864. break;
  865. }
  866. if(strcmp(f.wname[i], "cpunote") == 0){
  867. fid->file = Qcpunote;
  868. f.wqid[i] = fstab[Qcpunote].qid;
  869. continue;
  870. }
  871. f.type = Rerror;
  872. f.ename = err;
  873. strcpy(err, "cpu: file \"");
  874. for(j=0; j<=i; j++){
  875. if(strlen(err)+1+strlen(f.wname[j])+32 > sizeof err)
  876. break;
  877. if(j != 0)
  878. strcat(err, "/");
  879. strcat(err, f.wname[j]);
  880. }
  881. strcat(err, "\" does not exist");
  882. break;
  883. }
  884. if(nfid != nil && (f.ename != nil || i < f.nwname))
  885. nfid ->file = -1;
  886. if(f.type != Rerror)
  887. f.nwqid = i;
  888. break;
  889. case Topen:
  890. if(f.mode != OREAD){
  891. f.type = Rerror;
  892. f.ename = Eperm;
  893. }
  894. fid->omode = f.mode;
  895. if(fid->file == Qcpunote)
  896. ncpunote++;
  897. f.qid = fstab[fid->file].qid;
  898. break;
  899. case Tcreate:
  900. f.type = Rerror;
  901. f.ename = Eperm;
  902. break;
  903. case Tread:
  904. if(fsread(fd, fid, &f) < 0)
  905. goto err;
  906. doreply = 0;
  907. break;
  908. case Twrite:
  909. f.type = Rerror;
  910. f.ename = Eperm;
  911. break;
  912. case Tclunk:
  913. if(fid->omode != -1 && fid->file == Qcpunote){
  914. ncpunote--;
  915. if(ncpunote == 0) /* remote side is done */
  916. goto err;
  917. }
  918. fid->file = -1;
  919. fid->omode = -1;
  920. break;
  921. case Tremove:
  922. f.type = Rerror;
  923. f.ename = Eperm;
  924. break;
  925. case Tstat:
  926. if(fsstat(fd, fid, &f) < 0)
  927. goto err;
  928. doreply = 0;
  929. break;
  930. case Twstat:
  931. f.type = Rerror;
  932. f.ename = Eperm;
  933. break;
  934. }
  935. if(doreply)
  936. if(fsreply(fd, &f) < 0)
  937. break;
  938. }
  939. err:
  940. if(dbg)
  941. fprint(2, "notefs exiting: %r\n");
  942. werrstr("success");
  943. postnote(PNGROUP, exportpid, "kill");
  944. if(dbg)
  945. fprint(2, "postnote PNGROUP %d: %r\n", exportpid);
  946. close(fd);
  947. }
  948. char notebuf[ERRMAX];
  949. void
  950. catcher(void*, char *text)
  951. {
  952. int n;
  953. n = strlen(text);
  954. if(n >= sizeof(notebuf))
  955. n = sizeof(notebuf)-1;
  956. memmove(notebuf, text, n);
  957. notebuf[n] = '\0';
  958. noted(NCONT);
  959. }
  960. /*
  961. * mount in /dev a note file for the remote side to read.
  962. */
  963. void
  964. lclnoteproc(int netfd)
  965. {
  966. Waitmsg *w;
  967. Note *np;
  968. int pfd[2];
  969. int pid;
  970. if(pipe(pfd) < 0){
  971. fprint(2, "cpu: can't start note proc: pipe: %r\n");
  972. return;
  973. }
  974. /* new proc mounts and returns to start exportfs */
  975. switch(pid = rfork(RFPROC|RFNAMEG|RFFDG|RFMEM)){
  976. default:
  977. exportpid = pid;
  978. break;
  979. case -1:
  980. fprint(2, "cpu: can't start note proc: rfork: %r\n");
  981. return;
  982. case 0:
  983. close(pfd[0]);
  984. if(mount(pfd[1], -1, "/dev", MBEFORE, "") < 0)
  985. fprint(2, "cpu: can't mount note proc: %r\n");
  986. close(pfd[1]);
  987. return;
  988. }
  989. close(netfd);
  990. close(pfd[1]);
  991. /* new proc listens for note file system rpc's */
  992. switch(rfork(RFPROC|RFNAMEG|RFMEM)){
  993. case -1:
  994. fprint(2, "cpu: can't start note proc: rfork1: %r\n");
  995. _exits(0);
  996. case 0:
  997. notefs(pfd[0]);
  998. _exits(0);
  999. }
  1000. /* original proc waits for notes */
  1001. notify(catcher);
  1002. w = nil;
  1003. for(;;) {
  1004. *notebuf = 0;
  1005. free(w);
  1006. w = wait();
  1007. if(w == nil) {
  1008. if(*notebuf == 0)
  1009. break;
  1010. np = mallocz(sizeof(Note), 1);
  1011. if(np != nil){
  1012. strcpy(np->msg, notebuf);
  1013. lock(&nfs);
  1014. if(nfs.nfirst == nil)
  1015. nfs.nfirst = np;
  1016. else
  1017. nfs.nlast->next = np;
  1018. nfs.nlast = np;
  1019. unlock(&nfs);
  1020. kick(pfd[0]);
  1021. }
  1022. unlock(&nfs);
  1023. } else if(w->pid == exportpid)
  1024. break;
  1025. }
  1026. if(w == nil)
  1027. exits(nil);
  1028. exits(0);
  1029. /* exits(w->msg); */
  1030. }