pgrp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. static Ref pgrpid;
  16. static Ref mountid;
  17. void
  18. pgrpnote(uint32_t noteid, char *a, int32_t n, int flag)
  19. {
  20. int i;
  21. Proc *p;
  22. char buf[ERRMAX];
  23. if(n >= ERRMAX-1)
  24. error(Etoobig);
  25. memmove(buf, a, n);
  26. buf[n] = 0;
  27. for(i = 0; (p = psincref(i)) != nil; i++){
  28. if(p == up || p->state == Dead || p->noteid != noteid || p->kp){
  29. psdecref(p);
  30. continue;
  31. }
  32. qlock(&p->debug);
  33. if(p->pid == 0 || p->noteid != noteid){
  34. qunlock(&p->debug);
  35. psdecref(p);
  36. continue;
  37. }
  38. if(!waserror()) {
  39. postnote(p, 0, buf, flag);
  40. poperror();
  41. }
  42. qunlock(&p->debug);
  43. psdecref(p);
  44. }
  45. }
  46. Pgrp*
  47. newpgrp(void)
  48. {
  49. Pgrp *p;
  50. p = smalloc(sizeof(Pgrp));
  51. p->ref = 1;
  52. p->pgrpid = incref(&pgrpid);
  53. return p;
  54. }
  55. Rgrp*
  56. newrgrp(void)
  57. {
  58. Rgrp *r;
  59. r = smalloc(sizeof(Rgrp));
  60. r->ref = 1;
  61. return r;
  62. }
  63. void
  64. closergrp(Rgrp *r)
  65. {
  66. if(decref(r) == 0)
  67. free(r);
  68. }
  69. void
  70. closepgrp(Pgrp *p)
  71. {
  72. Mhead **h, **e, *f, *next;
  73. if(decref(p) != 0)
  74. return;
  75. qlock(&p->debug);
  76. wlock(&p->ns);
  77. p->pgrpid = -1;
  78. e = &p->mnthash[MNTHASH];
  79. for(h = p->mnthash; h < e; h++) {
  80. for(f = *h; f; f = next) {
  81. wlock(&f->lock);
  82. cclose(f->from);
  83. mountfree(f->mount);
  84. f->mount = nil;
  85. next = f->hash;
  86. wunlock(&f->lock);
  87. putmhead(f);
  88. }
  89. }
  90. wunlock(&p->ns);
  91. qunlock(&p->debug);
  92. free(p);
  93. }
  94. void
  95. pgrpinsert(Mount **order, Mount *mount)
  96. {
  97. Mount *f;
  98. mount->order = 0;
  99. if(*order == 0) {
  100. *order = mount;
  101. return;
  102. }
  103. for(f = *order; f; f = f->order) {
  104. if(mount->mountid < f->mountid) {
  105. mount->order = f;
  106. *order = mount;
  107. return;
  108. }
  109. order = &f->order;
  110. }
  111. *order = mount;
  112. }
  113. /*
  114. * pgrpcpy MUST preserve the mountid allocation order of the parent group
  115. */
  116. void
  117. pgrpcpy(Pgrp *to, Pgrp *from)
  118. {
  119. int i;
  120. Mount *n, *mount, **link, *order;
  121. Mhead *f, **tom, **l, *mh;
  122. wlock(&from->ns);
  123. order = 0;
  124. tom = to->mnthash;
  125. for(i = 0; i < MNTHASH; i++) {
  126. l = tom++;
  127. for(f = from->mnthash[i]; f; f = f->hash) {
  128. rlock(&f->lock);
  129. mh = newmhead(f->from);
  130. *l = mh;
  131. l = &mh->hash;
  132. link = &mh->mount;
  133. for(mount = f->mount; mount != nil; mount = mount->next) {
  134. n = newmount(mh, mount->to, mount->mflag, mount->spec);
  135. mount->copy = n;
  136. pgrpinsert(&order, mount);
  137. *link = n;
  138. link = &n->next;
  139. }
  140. runlock(&f->lock);
  141. }
  142. }
  143. /*
  144. * Allocate mount ids in the same sequence as the parent group
  145. */
  146. lock(&mountid);
  147. for(mount = order; mount != nil; mount = mount->order)
  148. mount->copy->mountid = mountid.ref++;
  149. unlock(&mountid);
  150. wunlock(&from->ns);
  151. }
  152. Fgrp*
  153. dupfgrp(Fgrp *f)
  154. {
  155. Fgrp *new;
  156. Chan *c;
  157. int i;
  158. new = smalloc(sizeof(Fgrp));
  159. if(f == nil){
  160. new->fd = smalloc(DELTAFD*sizeof(Chan*));
  161. new->nfd = DELTAFD;
  162. new->ref = 1;
  163. return new;
  164. }
  165. lock(f);
  166. /* Make new fd list shorter if possible, preserving quantization */
  167. new->nfd = f->maxfd+1;
  168. i = new->nfd%DELTAFD;
  169. if(i != 0)
  170. new->nfd += DELTAFD - i;
  171. new->fd = malloc(new->nfd*sizeof(Chan*));
  172. if(new->fd == nil){
  173. unlock(f);
  174. free(new);
  175. error("no memory for fgrp");
  176. }
  177. new->ref = 1;
  178. new->maxfd = f->maxfd;
  179. for(i = 0; i <= f->maxfd; i++) {
  180. if(c = f->fd[i]){
  181. incref(c);
  182. new->fd[i] = c;
  183. }
  184. }
  185. unlock(f);
  186. return new;
  187. }
  188. void
  189. closefgrp(Fgrp *f)
  190. {
  191. int i;
  192. Chan *c;
  193. if(f == 0)
  194. return;
  195. if(decref(f) != 0)
  196. return;
  197. /*
  198. * If we get into trouble, forceclosefgrp
  199. * will bail us out.
  200. */
  201. up->closingfgrp = f;
  202. for(i = 0; i <= f->maxfd; i++){
  203. if(c = f->fd[i]){
  204. f->fd[i] = nil;
  205. cclose(c);
  206. }
  207. }
  208. up->closingfgrp = nil;
  209. free(f->fd);
  210. free(f);
  211. }
  212. /*
  213. * Called from sleep because up is in the middle
  214. * of closefgrp and just got a kill ctl message.
  215. * This usually means that up has wedged because
  216. * of some kind of deadly embrace with mntclose
  217. * trying to talk to itself. To break free, hand the
  218. * unclosed channels to the close queue. Once they
  219. * are finished, the blocked cclose that we've
  220. * interrupted will finish by itself.
  221. */
  222. void
  223. forceclosefgrp(void)
  224. {
  225. int i;
  226. Chan *c;
  227. Fgrp *f;
  228. if(up->procctl != Proc_exitme || up->closingfgrp == nil){
  229. print("bad forceclosefgrp call");
  230. return;
  231. }
  232. f = up->closingfgrp;
  233. for(i = 0; i <= f->maxfd; i++){
  234. if(c = f->fd[i]){
  235. f->fd[i] = nil;
  236. ccloseq(c);
  237. }
  238. }
  239. }
  240. Mount*
  241. newmount(Mhead *mh, Chan *to, int flag, char *spec)
  242. {
  243. Mount *mount;
  244. mount = smalloc(sizeof(Mount));
  245. mount->to = to;
  246. mount->head = mh;
  247. incref(to);
  248. mount->mountid = incref(&mountid);
  249. mount->mflag = flag;
  250. if(spec != 0)
  251. kstrdup(&mount->spec, spec);
  252. return mount;
  253. }
  254. void
  255. mountfree(Mount *mount)
  256. {
  257. Mount *f;
  258. while(mount != nil) {
  259. f = mount->next;
  260. cclose(mount->to);
  261. mount->mountid = 0;
  262. free(mount->spec);
  263. free(mount);
  264. mount = f;
  265. }
  266. }
  267. void
  268. resrcwait(char *reason)
  269. {
  270. char *p;
  271. if(up == nil)
  272. panic("resrcwait");
  273. p = up->psstate;
  274. if(reason) {
  275. up->psstate = reason;
  276. print("%s\n", reason);
  277. }
  278. tsleep(&up->sleep, return0, 0, 300);
  279. up->psstate = p;
  280. }