ns.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <auth.h>
  5. #include <fcall.h>
  6. #pragma varargck type "P" char*
  7. int nsrv;
  8. Dir *srv;
  9. Biobuf stdout;
  10. typedef struct Mount Mount;
  11. struct Mount
  12. {
  13. char *cmd;
  14. char *flag;
  15. char *new;
  16. char *old;
  17. char *spec;
  18. };
  19. void xlatemnt(Mount*);
  20. char *quote(char*);
  21. int rflag;
  22. void
  23. usage(void)
  24. {
  25. fprint(2, "usage: ns [-r] [pid]\n");
  26. exits("usage");
  27. }
  28. void
  29. main(int argc, char **argv)
  30. {
  31. Mount *m;
  32. int line, fd, n, pid;
  33. char buf[1024], *av[5];
  34. ARGBEGIN{
  35. case 'r':
  36. rflag++;
  37. break;
  38. default:
  39. usage();
  40. }ARGEND
  41. if(argc > 1)
  42. usage();
  43. if(argc == 1){
  44. pid = atoi(argv[0]);
  45. if(pid == 0)
  46. usage();
  47. }else
  48. pid = getpid();
  49. Binit(&stdout, 1, OWRITE);
  50. sprint(buf, "/proc/%d/ns", pid);
  51. fd = open(buf, OREAD);
  52. if(fd < 0) {
  53. fprint(2, "ns: open %s: %r\n", buf);
  54. exits("open ns");
  55. }
  56. for(line=1; ; line++) {
  57. n = read(fd, buf, sizeof(buf));
  58. if(n == sizeof(buf)) {
  59. fprint(2, "ns: ns string too long\n");
  60. exits("read ns");
  61. }
  62. if(n < 0) {
  63. fprint(2, "ns: read %r\n");
  64. exits("read ns");
  65. }
  66. if(n == 0)
  67. break;
  68. buf[n] = '\0';
  69. m = mallocz(sizeof(Mount), 1);
  70. if(m == nil) {
  71. fprint(2, "ns: no memory: %r\n");
  72. exits("no memory");
  73. }
  74. n = tokenize(buf, av, 5);
  75. switch(n){
  76. case 2:
  77. if(strcmp(av[0], "cd") == 0){
  78. Bprint(&stdout, "%s %s\n", av[0], av[1]);
  79. continue;
  80. }
  81. /* fall through */
  82. default:
  83. fprint(2, "ns: unrecognized format of ns file: %d elements on line %d\n", n, line);
  84. exits("format");
  85. case 5:
  86. m->cmd = strdup(av[0]);
  87. m->flag = strdup(av[1]);
  88. m->new = strdup(av[2]);
  89. m->old = strdup(av[3]);
  90. m->spec = strdup(av[4]);
  91. break;
  92. case 4:
  93. if(av[1][0] == '-'){
  94. m->cmd = strdup(av[0]);
  95. m->flag = strdup(av[1]);
  96. m->new = strdup(av[2]);
  97. m->old = strdup(av[3]);
  98. m->spec = strdup("");
  99. }else{
  100. m->cmd = strdup(av[0]);
  101. m->flag = strdup("");
  102. m->new = strdup(av[1]);
  103. m->old = strdup(av[2]);
  104. m->spec = strdup(av[3]);
  105. }
  106. break;
  107. case 3:
  108. m->cmd = strdup(av[0]);
  109. m->flag = strdup("");
  110. m->new = strdup(av[1]);
  111. m->old = strdup(av[2]);
  112. m->spec = strdup("");
  113. break;
  114. }
  115. if(!rflag && strcmp(m->cmd, "mount")==0)
  116. xlatemnt(m);
  117. Bprint(&stdout, "%s %s %s %s %s\n", m->cmd, m->flag,
  118. quote(m->new), quote(m->old), quote(m->spec));
  119. free(m->cmd);
  120. free(m->flag);
  121. free(m->new);
  122. free(m->old);
  123. free(m->spec);
  124. free(m);
  125. }
  126. exits(nil);
  127. }
  128. void
  129. xlatemnt(Mount *m)
  130. {
  131. int n, fd;
  132. char *s, *t, *net, *port;
  133. char buf[256];
  134. if(strncmp(m->new, "/net/", 5) != 0)
  135. return;
  136. s = strdup(m->new);
  137. net = s+5;
  138. for(t=net; *t!='/'; t++)
  139. if(*t == '\0')
  140. goto Return;
  141. *t = '\0';
  142. port = t+1;
  143. for(t=port; *t!='/'; t++)
  144. if(*t == '\0')
  145. goto Return;
  146. *t = '\0';
  147. if(strcmp(t+1, "data") != 0)
  148. goto Return;
  149. snprint(buf, sizeof buf, "/net/%s/%s/remote", net, port);
  150. fd = open(buf, OREAD);
  151. if(fd < 0)
  152. goto Return;
  153. n = read(fd, buf, sizeof buf);
  154. close(fd);
  155. if(n<=1 || n>sizeof buf)
  156. goto Return;
  157. if(buf[n-1] == '\n')
  158. --n;
  159. buf[n] = '\0';
  160. t = malloc(strlen(net)+1+n+1);
  161. if(t == nil)
  162. goto Return;
  163. sprint(t, "%s!%s", net, buf);
  164. free(m->new);
  165. m->new = t;
  166. Return:
  167. free(s);
  168. }
  169. char*
  170. quote(char *s)
  171. {
  172. static char buf[3][1024];
  173. static int i;
  174. char *p, *ep;
  175. if(strpbrk(s, " '\\\t#$") == nil)
  176. return s;
  177. i = (i+1)%3;
  178. p = &buf[i][0];
  179. ep = &buf[i][1024];
  180. *p++ = '\'';
  181. while(p < ep-5){
  182. switch(*s){
  183. case '\0':
  184. goto out;
  185. case '\'':
  186. *p++ = '\'';
  187. break;
  188. }
  189. *p++ = *s++;
  190. }
  191. out:
  192. *p++ = '\'';
  193. *p = '\0';
  194. return buf[i];
  195. }