9pcon.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. #include <fcall.h>
  13. #include <bio.h>
  14. uint messagesize = 65536; /* just a buffer size */
  15. void
  16. usage(void)
  17. {
  18. fprint(2, "usage: aux/9pcon [-m messagesize] /srv/service | -c command | -n networkaddress\n");
  19. exits("usage");
  20. }
  21. int
  22. connectcmd(char *cmd)
  23. {
  24. int p[2];
  25. if(pipe(p) < 0)
  26. return -1;
  27. switch(fork()){
  28. case -1:
  29. fprint(2, "fork failed: %r\n");
  30. _exits("exec");
  31. case 0:
  32. dup(p[0], 0);
  33. dup(p[0], 1);
  34. close(p[1]);
  35. execl("/bin/rc", "rc", "-c", cmd, nil);
  36. fprint(2, "exec failed: %r\n");
  37. _exits("exec");
  38. default:
  39. close(p[0]);
  40. return p[1];
  41. }
  42. }
  43. void
  44. watch(int fd)
  45. {
  46. int n;
  47. uint8_t *buf;
  48. Fcall f;
  49. buf = malloc(messagesize);
  50. if(buf == nil)
  51. sysfatal("out of memory");
  52. while((n = read9pmsg(fd, buf, messagesize)) > 0){
  53. if(convM2S(buf, n, &f) == 0){
  54. print("convM2S: %r\n");
  55. continue;
  56. }
  57. print("\t<- %F\n", &f);
  58. }
  59. if(n == 0)
  60. print("server eof\n");
  61. else
  62. print("read9pmsg from server: %r\n");
  63. }
  64. char*
  65. tversion(Fcall *f, int p, char **argv)
  66. {
  67. f->msize = atoi(argv[0]);
  68. if(f->msize > messagesize)
  69. return "message size too big; use -m option on command line";
  70. f->version = argv[1];
  71. return nil;
  72. }
  73. char*
  74. tauth(Fcall *f, int p, char **argv)
  75. {
  76. f->afid = atoi(argv[0]);
  77. f->uname = argv[1];
  78. f->aname = argv[2];
  79. return nil;
  80. }
  81. char*
  82. tflush(Fcall *f, int p, char **argv)
  83. {
  84. f->oldtag = atoi(argv[0]);
  85. return nil;
  86. }
  87. char*
  88. tattach(Fcall *f, int p, char **argv)
  89. {
  90. f->fid = atoi(argv[0]);
  91. f->afid = atoi(argv[1]);
  92. f->uname = argv[2];
  93. f->aname = argv[3];
  94. return nil;
  95. }
  96. char*
  97. twalk(Fcall *f, int argc, char **argv)
  98. {
  99. int i;
  100. if(argc < 2)
  101. return "usage: Twalk tag fid newfid [name...]";
  102. f->fid = atoi(argv[0]);
  103. f->newfid = atoi(argv[1]);
  104. f->nwname = argc-2;
  105. if(f->nwname > MAXWELEM)
  106. return "too many names";
  107. for(i=0; i<argc-2; i++)
  108. f->wname[i] = argv[2+i];
  109. return nil;
  110. }
  111. char*
  112. topen(Fcall *f, int p, char **argv)
  113. {
  114. f->fid = atoi(argv[0]);
  115. f->mode = atoi(argv[1]);
  116. return nil;
  117. }
  118. char*
  119. tcreate(Fcall *f, int p, char **argv)
  120. {
  121. f->fid = atoi(argv[0]);
  122. f->name = argv[1];
  123. f->perm = strtoul(argv[2], 0, 8);
  124. f->mode = atoi(argv[3]);
  125. return nil;
  126. }
  127. char*
  128. tread(Fcall *f, int p, char **argv)
  129. {
  130. f->fid = atoi(argv[0]);
  131. f->offset = strtoll(argv[1], 0, 0);
  132. f->count = strtol(argv[2], 0, 0);
  133. return nil;
  134. }
  135. char*
  136. twrite(Fcall *f, int p, char **argv)
  137. {
  138. f->fid = atoi(argv[0]);
  139. f->offset = strtoll(argv[1], 0, 0);
  140. f->data = argv[2];
  141. f->count = strlen(argv[2]);
  142. return nil;
  143. }
  144. char*
  145. tclunk(Fcall *f, int p, char **argv)
  146. {
  147. f->fid = atoi(argv[0]);
  148. return nil;
  149. }
  150. char*
  151. tremove(Fcall *f, int p, char **argv)
  152. {
  153. f->fid = atoi(argv[0]);
  154. return nil;
  155. }
  156. char*
  157. tstat(Fcall *f, int p, char **argv)
  158. {
  159. f->fid = atoi(argv[0]);
  160. return nil;
  161. }
  162. uint32_t
  163. xstrtoul(char *s)
  164. {
  165. if(strcmp(s, "~0") == 0)
  166. return ~0U;
  167. return strtoul(s, 0, 0);
  168. }
  169. uint64_t
  170. xstrtoull(char *s)
  171. {
  172. if(strcmp(s, "~0") == 0)
  173. return ~0ULL;
  174. return strtoull(s, 0, 0);
  175. }
  176. char*
  177. twstat(Fcall *f, int p, char **argv)
  178. {
  179. static uint8_t buf[DIRMAX];
  180. Dir d;
  181. memset(&d, 0, sizeof d);
  182. nulldir(&d);
  183. d.name = argv[1];
  184. d.uid = argv[2];
  185. d.gid = argv[3];
  186. d.mode = xstrtoul(argv[4]);
  187. d.mtime = xstrtoul(argv[5]);
  188. d.length = xstrtoull(argv[6]);
  189. f->fid = atoi(argv[0]);
  190. f->stat = buf;
  191. f->nstat = convD2M(&d, buf, sizeof buf);
  192. if(f->nstat < BIT16SZ)
  193. return "convD2M failed (internal error)";
  194. return nil;
  195. }
  196. int taggen;
  197. char*
  198. settag(Fcall *f, int p, char **argv)
  199. {
  200. static char buf[120];
  201. taggen = atoi(argv[0])-1;
  202. snprint(buf, sizeof buf, "next tag is %d", taggen+1);
  203. return buf;
  204. }
  205. typedef struct Cmd Cmd;
  206. struct Cmd {
  207. char *name;
  208. int type;
  209. int argc;
  210. char *usage;
  211. char *(*fn)(Fcall *f, int p, char**);
  212. };
  213. Cmd msg9p[] = {
  214. "Tversion", Tversion, 2, "messagesize version", tversion,
  215. "Tauth", Tauth, 3, "afid uname aname", tauth,
  216. "Tflush", Tflush, 1, "oldtag", tflush,
  217. "Tattach", Tattach, 4, "fid afid uname aname", tattach,
  218. "Twalk", Twalk, 0, "fid newfid [name...]", twalk,
  219. "Topen", Topen, 2, "fid mode", topen,
  220. "Tcreate", Tcreate, 4, "fid name perm mode", tcreate,
  221. "Tread", Tread, 3, "fid offset count", tread,
  222. "Twrite", Twrite, 3, "fid offset data", twrite,
  223. "Tclunk", Tclunk, 1, "fid", tclunk,
  224. "Tremove", Tremove, 1, "fid", tremove,
  225. "Tstat", Tstat, 1, "fid", tstat,
  226. "Twstat", Twstat, 7, "fid name uid gid mode mtime length", twstat,
  227. "nexttag", 0, 0, "", settag,
  228. };
  229. void
  230. shell9p(int fd)
  231. {
  232. char *e, *f[10], *p;
  233. uint8_t *buf;
  234. int i, n, nf;
  235. Biobuf b;
  236. Fcall t;
  237. buf = malloc(messagesize);
  238. if(buf == nil){
  239. fprint(2, "out of memory\n");
  240. return;
  241. }
  242. taggen = 0;
  243. Binit(&b, 0, OREAD);
  244. while(p = Brdline(&b, '\n')){
  245. p[Blinelen(&b)-1] = '\0';
  246. if(p[0] == '#')
  247. continue;
  248. if((nf = tokenize(p, f, nelem(f))) == 0)
  249. continue;
  250. for(i=0; i<nelem(msg9p); i++)
  251. if(strcmp(f[0], msg9p[i].name) == 0)
  252. break;
  253. if(i == nelem(msg9p)){
  254. fprint(2, "?unknown message\n");
  255. continue;
  256. }
  257. memset(&t, 0, sizeof t);
  258. t.type = msg9p[i].type;
  259. if(t.type == Tversion)
  260. t.tag = NOTAG;
  261. else
  262. t.tag = ++taggen;
  263. if(nf < 1 || (msg9p[i].argc && nf != 1+msg9p[i].argc)){
  264. fprint(2, "?usage: %s %s\n", msg9p[i].name, msg9p[i].usage);
  265. continue;
  266. }
  267. if(e = msg9p[i].fn(&t, nf-1, f+1)){
  268. fprint(2, "?%s\n", e);
  269. continue;
  270. }
  271. n = convS2M(&t, buf, messagesize);
  272. if(n <= BIT16SZ){
  273. fprint(2, "?message too large for buffer\n");
  274. continue;
  275. }
  276. if(write(fd, buf, n) != n){
  277. fprint(2, "?write fails: %r\n");
  278. break;
  279. }
  280. print("\t-> %F\n", &t);
  281. }
  282. }
  283. void
  284. main(int argc, char **argv)
  285. {
  286. int fd, pid, cmd, net;
  287. cmd = 0;
  288. net = 0;
  289. ARGBEGIN{
  290. case 'c':
  291. cmd = 1;
  292. break;
  293. case 'm':
  294. messagesize = atoi(EARGF(usage()));
  295. break;
  296. case 'n':
  297. net = 1;
  298. break;
  299. default:
  300. usage();
  301. }ARGEND
  302. fmtinstall('F', fcallfmt);
  303. fmtinstall('D', dirfmt);
  304. fmtinstall('M', dirmodefmt);
  305. if(argc != 1)
  306. usage();
  307. if(cmd && net)
  308. usage();
  309. if(cmd)
  310. fd = connectcmd(argv[0]);
  311. else if(net){
  312. fd = dial(netmkaddr(argv[0], "net", "9fs"), 0, 0, 0);
  313. if(fd < 0)
  314. sysfatal("dial: %r");
  315. }else{
  316. fd = open(argv[0], ORDWR);
  317. if(fd < 0)
  318. sysfatal("open: %r");
  319. }
  320. switch(pid = rfork(RFPROC|RFMEM)){
  321. case -1:
  322. sysfatal("rfork: %r");
  323. break;
  324. case 0:
  325. watch(fd);
  326. postnote(PNPROC, getppid(), "kill");
  327. break;
  328. default:
  329. shell9p(fd);
  330. postnote(PNPROC, pid, "kill");
  331. break;
  332. }
  333. exits(nil);
  334. }