~kris/9p

9hist

ref: 9584dbf140aab3d221ec0028d0df1e422aa63a6d 9hist/port/pgrp.c -rw-r--r-- 3.7 KiB
9584dbf1 — David du Colombier Plan 9 from Bell Labs 1991-06-14 35 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"

struct{
	Lock;
	Pgrp	*arena;
	Pgrp	*free;
	ulong	pgrpid;
}pgrpalloc;

struct{
	Lock;
	Mount	*free;
	ulong	mountid;
}mountalloc;

void
pgrpinit(void)
{
	int i;
	Pgrp *p;
	Mount *m;

	pgrpalloc.arena = ialloc(conf.npgrp*sizeof(Pgrp), 0);
	pgrpalloc.free = pgrpalloc.arena;

	p = pgrpalloc.free;
	for(i=0; i<conf.npgrp; i++,p++){
		p->index = i;
		p->next = p+1;
		p->mtab = ialloc(conf.nmtab*sizeof(Mtab), 0);
		p->etab = ialloc(conf.npgenv*sizeof(Envp*), 0);
	}
	p[-1].next = 0;

	mountalloc.free = ialloc(conf.nmount*sizeof(Mount), 0);

	m = mountalloc.free;
	for(i=0; i<conf.nmount-1; i++,m++)
		m->next = m+1;
	m->next = 0;
}

Pgrp*
pgrptab(int i)
{
	return &pgrpalloc.arena[i];
}

void
pgrpnote(Pgrp *pg, char *a, long n, int flag)
{
	int i;
	Proc *p;
	char buf[ERRLEN];

	if(n >= ERRLEN-1)
		error(Etoobig);
	if(n>=4 && strncmp(a, "sys:", 4)==0)
		error(Ebadarg);
	memmove(buf, a, n);
	buf[n] = 0;
	p = proctab(0);
	for(i=0; i<conf.nproc; i++, p++){
		if(p->pgrp == pg){
			lock(&p->debug);
			if(p->pid==0 || p->pgrp!=pg){
				unlock(&p->debug);
				continue;
			}
			if(!waserror()){
				postnote(p, 0, buf, flag);
				poperror();
			}
			unlock(&p->debug);
		}
	}
}

Pgrp*
newpgrp(void)
{
	Pgrp *p;

loop:
	lock(&pgrpalloc);
	if(p = pgrpalloc.free){		/* assign = */
		pgrpalloc.free = p->next;
		p->ref = 1;
		p->pgrpid = ++pgrpalloc.pgrpid;
		p->nmtab = 0;
		p->nenv = 0;
		unlock(&pgrpalloc);
		return p;
	}
	unlock(&pgrpalloc);
	print("no pgrps\n");
	if(u == 0)
		panic("newpgrp");
	u->p->state = Wakeme;
	alarm(1000, wakeme, u->p);
	sched();
	goto loop;
}

void
closepgrp(Pgrp *p)
{
	int i;
	Mtab *m;
	Envp *ep;

	if(decref(p) == 0){
		qlock(&p->debug);
		p->pgrpid = -1;
		m = p->mtab;
		for(i=0; i<p->nmtab; i++,m++)
			if(m->c){
				close(m->c);
				closemount(m->mnt);
			}
		ep = p->etab;
		for(i=0; i<p->nenv; i++,ep++)
			if(ep->env)
				envpgclose(ep->env);
		lock(&pgrpalloc);
		p->next = pgrpalloc.free;
		pgrpalloc.free = p;
		qunlock(&p->debug);
		unlock(&pgrpalloc);
	}
}

Mount*
newmount(void)
{
	Mount *m;

loop:
	lock(&mountalloc);
	if(m = mountalloc.free){		/* assign = */
		mountalloc.free = m->next;
		m->ref = 1;
		m->next = 0;
		m->mountid = ++mountalloc.mountid;
		unlock(&mountalloc);
		return m;
	}
	unlock(&mountalloc);
	print("no mounts\n");
	if(u == 0)
		panic("newmount");
	u->p->state = Wakeme;
	alarm(1000, wakeme, u->p);
	sched();
	goto loop;
}

void
closemount(Mount *m)
{
	lock(m);
	if(m->ref == 1){
		if(m->c)
			close(m->c);
		if(m->next)
			closemount(m->next);
		unlock(m);
		lock(&mountalloc);
		m->mountid = 0;
		m->next = mountalloc.free;
		mountalloc.free = m;
		unlock(&mountalloc);
		return;
	}
	m->ref--;
	unlock(m);
}

void
envcpy(Pgrp *to, Pgrp *from)
{
	Envp *ep;
	Env *e;
	int i;

	lock(from);
	to->nenv = from->nenv;
	ep = to->etab;
	for(i=0; i<from->nenv; i++,ep++){
		ep->chref = 0;
		e = ep->env = from->etab[i].env;
		if(e){
			lock(e);
			if(waserror()){
				unlock(e);
				unlock(from);
				ep->env = 0;
				nexterror();
			}
			/*
			 * If pgrp being forked has an open channel
			 * on this env, it may write it after the fork
			 * so make a copy now.
			 * Don't worry about other pgrps, because they
			 * will copy if they are about to write.
			 */
			if(from->etab[i].chref){
				ep->env = copyenv(e, 0);
				unlock(ep->env);
			}else
				e->pgref++;
			poperror();
			unlock(e);
		}
	}
	unlock(from);
}

void
pgrpcpy(Pgrp *to, Pgrp *from)
{
	int i;
	Mtab *m;

	lock(from);
	memmove(to->user, from->user, NAMELEN);
	memmove(to->mtab, from->mtab, from->nmtab*sizeof(Mtab));
	to->nmtab = from->nmtab;
	m = to->mtab;
	for(i=0; i<from->nmtab; i++,m++)
		if(m->c){
			incref(m->c);
			lock(m->mnt);
			m->mnt->ref++;
			unlock(m->mnt);
		}

	unlock(from);
}