devsrv.c 6.2 KB

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