devsrv.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. typedef struct Srv Srv;
  16. struct Srv
  17. {
  18. char *name;
  19. char *owner;
  20. uint32_t perm;
  21. Chan *chan;
  22. Srv *link;
  23. uint32_t path;
  24. };
  25. static QLock srvlk;
  26. static Srv *srv;
  27. static int qidpath;
  28. static int
  29. srvgen(Chan *c, char* d, Dirtab* dir, int i, int s, Dir *dp)
  30. {
  31. Proc *up = externup();
  32. Srv *sp;
  33. Qid q;
  34. if(s == DEVDOTDOT){
  35. devdir(c, c->qid, "#s", 0, eve, 0555, dp);
  36. return 1;
  37. }
  38. for(sp = srv; sp && s; sp = sp->link)
  39. s--;
  40. if(sp == 0) {
  41. return -1;
  42. }
  43. mkqid(&q, sp->path, 0, QTFILE);
  44. /* make sure name string continues to exist after we release lock */
  45. kstrcpy(up->genbuf, sp->name, sizeof up->genbuf);
  46. devdir(c, q, up->genbuf, 0, sp->owner, sp->perm, dp);
  47. return 1;
  48. }
  49. static void
  50. srvinit(void)
  51. {
  52. qidpath = 1;
  53. }
  54. static Chan*
  55. srvattach(char *spec)
  56. {
  57. return devattach('s', spec);
  58. }
  59. static Walkqid*
  60. srvwalk(Chan *c, Chan *nc, char **name, int nname)
  61. {
  62. Walkqid *wqid;
  63. qlock(&srvlk);
  64. wqid = devwalk(c, nc, name, nname, 0, 0, srvgen);
  65. qunlock(&srvlk);
  66. return wqid;
  67. }
  68. static Srv*
  69. srvlookup(char *name, uint32_t qidpath)
  70. {
  71. Srv *sp;
  72. for(sp = srv; sp; sp = sp->link)
  73. if(sp->path == qidpath || (name && strcmp(sp->name, name) == 0))
  74. return sp;
  75. return nil;
  76. }
  77. static int32_t
  78. srvstat(Chan *c, uint8_t *db, int32_t n)
  79. {
  80. int32_t r;
  81. qlock(&srvlk);
  82. r = devstat(c, db, n, 0, 0, srvgen);
  83. qunlock(&srvlk);
  84. return r;
  85. }
  86. char*
  87. srvname(Chan *c)
  88. {
  89. Srv *sp;
  90. char *s;
  91. for(sp = srv; sp; sp = sp->link)
  92. if(sp->chan == c){
  93. s = smalloc(3+strlen(sp->name)+1);
  94. sprint(s, "#s/%s", sp->name);
  95. return s;
  96. }
  97. return nil;
  98. }
  99. static Chan*
  100. srvopen(Chan *c, int omode)
  101. {
  102. Proc *up = externup();
  103. Srv *sp;
  104. if(c->qid.type == QTDIR){
  105. if(omode & ORCLOSE)
  106. error(Eperm);
  107. if(omode != OREAD)
  108. error(Eisdir);
  109. c->mode = omode;
  110. c->flag |= COPEN;
  111. c->offset = 0;
  112. return c;
  113. }
  114. qlock(&srvlk);
  115. if(waserror()){
  116. qunlock(&srvlk);
  117. nexterror();
  118. }
  119. sp = srvlookup(nil, c->qid.path);
  120. if(sp == 0 || sp->chan == 0)
  121. error(Eshutdown);
  122. if(omode&OTRUNC)
  123. error("srv file already exists");
  124. if(openmode(omode)!=sp->chan->mode && sp->chan->mode!=ORDWR)
  125. error(Eperm);
  126. devpermcheck(sp->owner, sp->perm, omode);
  127. cclose(c);
  128. incref(&sp->chan->r);
  129. qunlock(&srvlk);
  130. poperror();
  131. return sp->chan;
  132. }
  133. static void
  134. srvcreate(Chan *c, char *name, int omode, int perm)
  135. {
  136. Proc *up = externup();
  137. Srv *sp;
  138. char *sname;
  139. if(openmode(omode) != OWRITE)
  140. error(Eperm);
  141. if(omode & OCEXEC) /* can't happen */
  142. panic("someone broke namec");
  143. sp = smalloc(sizeof *sp);
  144. sname = smalloc(strlen(name)+1);
  145. qlock(&srvlk);
  146. if(waserror()){
  147. free(sname);
  148. free(sp);
  149. qunlock(&srvlk);
  150. nexterror();
  151. }
  152. if(sp == nil || sname == nil)
  153. error(Enomem);
  154. if(srvlookup(name, -1))
  155. error(Eexist);
  156. sp->path = qidpath++;
  157. sp->link = srv;
  158. strcpy(sname, name);
  159. sp->name = sname;
  160. c->qid.type = QTFILE;
  161. c->qid.path = sp->path;
  162. srv = sp;
  163. qunlock(&srvlk);
  164. poperror();
  165. kstrdup(&sp->owner, up->user);
  166. sp->perm = perm&0777;
  167. c->flag |= COPEN;
  168. c->mode = OWRITE;
  169. }
  170. static void
  171. srvremove(Chan *c)
  172. {
  173. Proc *up = externup();
  174. Srv *sp, **l;
  175. if(c->qid.type == QTDIR)
  176. error(Eperm);
  177. qlock(&srvlk);
  178. if(waserror()){
  179. qunlock(&srvlk);
  180. nexterror();
  181. }
  182. l = &srv;
  183. for(sp = *l; sp; sp = sp->link) {
  184. if(sp->path == c->qid.path)
  185. break;
  186. l = &sp->link;
  187. }
  188. if(sp == 0)
  189. error(Enonexist);
  190. /*
  191. * Only eve can remove system services.
  192. * No one can remove #s/boot.
  193. */
  194. if(strcmp(sp->owner, eve) == 0 && !iseve())
  195. error(Eperm);
  196. if(strcmp(sp->name, "boot") == 0)
  197. error(Eperm);
  198. /*
  199. * No removing personal services.
  200. */
  201. if((sp->perm&7) != 7 && strcmp(sp->owner, up->user) && !iseve())
  202. error(Eperm);
  203. *l = sp->link;
  204. qunlock(&srvlk);
  205. poperror();
  206. if(sp->chan)
  207. cclose(sp->chan);
  208. free(sp->owner);
  209. free(sp->name);
  210. free(sp);
  211. }
  212. static int32_t
  213. srvwstat(Chan *c, uint8_t *dp, int32_t n)
  214. {
  215. Proc *up = externup();
  216. Dir d;
  217. Srv *sp;
  218. char *strs;
  219. if(c->qid.type & QTDIR)
  220. error(Eperm);
  221. strs = nil;
  222. qlock(&srvlk);
  223. if(waserror()){
  224. qunlock(&srvlk);
  225. free(strs);
  226. nexterror();
  227. }
  228. sp = srvlookup(nil, c->qid.path);
  229. if(sp == 0)
  230. error(Enonexist);
  231. if(strcmp(sp->owner, up->user) != 0 && !iseve())
  232. error(Eperm);
  233. strs = smalloc(n);
  234. n = convM2D(dp, n, &d, strs);
  235. if(n == 0)
  236. error(Eshortstat);
  237. if(d.mode != (uint32_t)~0UL)
  238. sp->perm = d.mode & 0777;
  239. if(d.uid && *d.uid)
  240. kstrdup(&sp->owner, d.uid);
  241. if(d.name && *d.name && strcmp(sp->name, d.name) != 0) {
  242. if(strchr(d.name, '/') != nil)
  243. error(Ebadchar);
  244. kstrdup(&sp->name, d.name);
  245. }
  246. qunlock(&srvlk);
  247. free(strs);
  248. poperror();
  249. return n;
  250. }
  251. static void
  252. srvclose(Chan *c)
  253. {
  254. Proc *up = externup();
  255. /*
  256. * in theory we need to override any changes in removability
  257. * since open, but since all that's checked is the owner,
  258. * which is immutable, all is well.
  259. */
  260. if(c->flag & CRCLOSE){
  261. if(waserror())
  262. return;
  263. srvremove(c);
  264. poperror();
  265. }
  266. }
  267. static int32_t
  268. srvread(Chan *c, void *va, int32_t n, int64_t m)
  269. {
  270. int32_t r;
  271. isdir(c);
  272. qlock(&srvlk);
  273. r = devdirread(c, va, n, 0, 0, srvgen);
  274. qunlock(&srvlk);
  275. return r;
  276. }
  277. static int32_t
  278. srvwrite(Chan *c, void *va, int32_t n, int64_t mm)
  279. {
  280. Proc *up = externup();
  281. Srv *sp;
  282. Chan *c1;
  283. int fd;
  284. char buf[32];
  285. if(n >= sizeof buf)
  286. error(Egreg);
  287. memmove(buf, va, n); /* so we can NUL-terminate */
  288. buf[n] = 0;
  289. fd = strtoul(buf, 0, 0);
  290. c1 = fdtochan(fd, -1, 0, 1); /* error check and inc ref */
  291. qlock(&srvlk);
  292. if(waserror()) {
  293. qunlock(&srvlk);
  294. cclose(c1);
  295. nexterror();
  296. }
  297. if(c1->flag & (CCEXEC|CRCLOSE))
  298. error("posted fd has remove-on-close or close-on-exec");
  299. if(c1->qid.type & QTAUTH)
  300. error("cannot post auth file in srv");
  301. sp = srvlookup(nil, c->qid.path);
  302. if(sp == 0)
  303. error(Enonexist);
  304. if(sp->chan)
  305. error(Ebadusefd);
  306. sp->chan = c1;
  307. qunlock(&srvlk);
  308. poperror();
  309. return n;
  310. }
  311. Dev srvdevtab = {
  312. .dc = 's',
  313. .name = "srv",
  314. .reset = devreset,
  315. .init = srvinit,
  316. .shutdown = devshutdown,
  317. .attach = srvattach,
  318. .walk = srvwalk,
  319. .stat = srvstat,
  320. .open = srvopen,
  321. .create = srvcreate,
  322. .close = srvclose,
  323. .read = srvread,
  324. .bread = devbread,
  325. .write = srvwrite,
  326. .bwrite = devbwrite,
  327. .remove = srvremove,
  328. .wstat = srvwstat,
  329. };