devsrv.c 6.0 KB

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