plan 9 kernel history: overview | file list | diff list

1991/0710/port/pgrp.c (diff list | history)

port/pgrp.c on 1990/0227
1990/0227    
#include	"u.h" 
#include	"lib.h" 
#include	"mem.h" 
#include	"dat.h" 
#include	"fns.h" 
#include	"errno.h" 
 
1991/0705    
struct 
{ 
1990/0227    
	Lock; 
1990/1110    
	Pgrp	*arena; 
1990/0227    
	Pgrp	*free; 
	ulong	pgrpid; 
}pgrpalloc; 
 
1991/0705    
struct 
{ 
	Lock; 
	Egrp	*free; 
}egrpalloc; 
 
struct 
{ 
	Lock; 
	Fgrp	*free; 
}fgrpalloc; 
 
1990/0227    
struct{ 
	Lock; 
	Mount	*free; 
	ulong	mountid; 
}mountalloc; 
 
void 
1991/0705    
grpinit(void) 
1990/0227    
{ 
	int i; 
	Pgrp *p; 
1991/0705    
	Egrp *e, *ee; 
	Fgrp *f, *fe; 
	Mount *m, *em; 
1990/0227    
 
1990/1110    
	pgrpalloc.arena = ialloc(conf.npgrp*sizeof(Pgrp), 0); 
	pgrpalloc.free = pgrpalloc.arena; 
1990/0227    
 
	p = pgrpalloc.free; 
1990/0825    
	for(i=0; i<conf.npgrp; i++,p++){ 
1990/1110    
		p->index = i; 
1990/0227    
		p->next = p+1; 
		p->mtab = ialloc(conf.nmtab*sizeof(Mtab), 0); 
	} 
1990/0825    
	p[-1].next = 0; 
1990/0227    
 
1991/0705    
	egrpalloc.free = ialloc(conf.npgrp*sizeof(Egrp), 0); 
	ee = &egrpalloc.free[conf.npgrp]; 
	for(e = egrpalloc.free; e < ee; e++) { 
		e->next = e+1; 
		e->etab = ialloc(conf.npgenv*sizeof(Envp*), 0); 
	} 
	e[-1].next = 0; 
1990/0227    
 
1991/0705    
	fgrpalloc.free = ialloc(conf.nproc*sizeof(Fgrp), 0); 
	fe = &fgrpalloc.free[conf.nproc-1]; 
	for(f = fgrpalloc.free; f < fe; f++) 
		f->next = f+1; 
	f->next = 0; 
 
	mountalloc.free = ialloc(conf.nmount*sizeof(Mount), 0); 
	em = &mountalloc.free[conf.nmount-1]; 
	for(m = mountalloc.free; m < em; m++) 
1990/0227    
		m->next = m+1; 
	m->next = 0; 
} 
 
Pgrp* 
1990/1110    
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) 
1990/11211    
		error(Etoobig); 
1990/1110    
	if(n>=4 && strncmp(a, "sys:", 4)==0) 
1990/11211    
		error(Ebadarg); 
1991/0318    
	memmove(buf, a, n); 
1990/1110    
	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; 
			} 
1991/0125    
			if(!waserror()){ 
				postnote(p, 0, buf, flag); 
				poperror(); 
1990/1110    
			} 
			unlock(&p->debug); 
		} 
	} 
} 
 
Pgrp* 
1990/0227    
newpgrp(void) 
{ 
	Pgrp *p; 
 
1991/0705    
	for(;;) { 
		lock(&pgrpalloc); 
		if(p = pgrpalloc.free){ 
			pgrpalloc.free = p->next; 
			p->ref = 1; 
			p->pgrpid = ++pgrpalloc.pgrpid; 
			p->nmtab = 0; 
			unlock(&pgrpalloc); 
			return p; 
		} 
1990/0227    
		unlock(&pgrpalloc); 
1991/0705    
		resrcwait("no pgrps"); 
1990/0227    
	} 
1991/0705    
} 
 
Egrp* 
newegrp(void) 
{ 
	Egrp *e; 
 
	for(;;) { 
		lock(&egrpalloc); 
		if(e = egrpalloc.free) { 
			egrpalloc.free = e->next; 
			e->ref = 1; 
			e->nenv = 0; 
			unlock(&egrpalloc); 
			return e; 
		} 
		unlock(&egrpalloc); 
		resrcwait("no envgrps"); 
	} 
} 
 
Fgrp* 
newfgrp(void) 
{ 
	Fgrp *f; 
 
	for(;;) { 
		lock(&fgrpalloc); 
		if(f = fgrpalloc.free) { 
			fgrpalloc.free = f->next; 
			f->ref = 1; 
			f->maxfd = 0; 
			memset(f->fd, 0, sizeof(f->fd)); 
			unlock(&fgrpalloc); 
			return f; 
		} 
		unlock(&fgrpalloc); 
		resrcwait("no filegrps"); 
	} 
} 
 
Fgrp* 
dupfgrp(Fgrp *f) 
{ 
	Fgrp *new; 
	Chan *c; 
	int i; 
 
	new = newfgrp(); 
 
	new->maxfd = f->maxfd; 
	for(i = 0; i <= f->maxfd; i++) 
		if(c = f->fd[i]){ 
			incref(c); 
			new->fd[i] = c; 
		} 
 
	return new; 
} 
 
void 
resrcwait(char *reason) 
{ 
1991/0710    
	if(reason) 
		print("%s\n", reason); 
1990/0227    
	if(u == 0) 
1991/0710    
		panic("resrcwait"); 
1990/0227    
	u->p->state = Wakeme; 
	alarm(1000, wakeme, u->p); 
	sched(); 
} 
 
void 
closepgrp(Pgrp *p) 
{ 
	int i; 
	Mtab *m; 
 
	if(decref(p) == 0){ 
1991/0212    
		qlock(&p->debug); 
1990/1110    
		p->pgrpid = -1; 
1990/0227    
		m = p->mtab; 
		for(i=0; i<p->nmtab; i++,m++) 
			if(m->c){ 
				close(m->c); 
				closemount(m->mnt); 
			} 
		lock(&pgrpalloc); 
		p->next = pgrpalloc.free; 
		pgrpalloc.free = p; 
1991/0212    
		qunlock(&p->debug); 
1990/0227    
		unlock(&pgrpalloc); 
	} 
} 
 
1991/0705    
void 
closeegrp(Egrp *e) 
{ 
	Envp *ep; 
	int i; 
 
	if(decref(e) == 0) { 
		ep = e->etab; 
		for(i=0; i<e->nenv; i++,ep++) 
			if(ep->env) 
				envpgclose(ep->env); 
		lock(&egrpalloc); 
		e->next = egrpalloc.free; 
		egrpalloc.free = e; 
		unlock(&egrpalloc); 
	} 
} 
 
void 
closefgrp(Fgrp *f) 
{ 
	int i; 
	Chan *c; 
 
	if(decref(f) == 0) { 
		for(i = 0; i <= f->maxfd; i++) 
			if(c = f->fd[i]) 
				close(c); 
 
		lock(&fgrpalloc); 
		f->next = fgrpalloc.free; 
		fgrpalloc.free = f; 
		unlock(&fgrpalloc); 
	} 
} 
 
 
1990/0227    
Mount* 
newmount(void) 
{ 
	Mount *m; 
 
loop: 
	lock(&mountalloc); 
	if(m = mountalloc.free){		/* assign = */ 
		mountalloc.free = m->next; 
		m->ref = 1; 
1990/0928    
		m->next = 0; 
1990/0227    
		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){ 
1990/0321    
		if(m->c) 
			close(m->c); 
1990/0227    
		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 
1991/0705    
envcpy(Egrp *to, Egrp *from) 
1990/0227    
{ 
	Envp *ep; 
	Env *e; 
1991/0514    
	int i; 
1990/0227    
 
	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); 
		} 
	} 
1991/0514    
	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); 
		} 
 
1990/0227    
	unlock(from); 
} 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)