Home | History | Annotate | Line # | Download | only in kern
sysv_sem.c revision 1.27
      1  1.27  christos /*	$NetBSD: sysv_sem.c,v 1.27 1996/10/10 22:43:22 christos Exp $	*/
      2   1.9       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Implementation of SVID semaphores
      5   1.1       cgd  *
      6   1.1       cgd  * Author:  Daniel Boulet
      7   1.1       cgd  *
      8   1.1       cgd  * This software is provided ``AS IS'' without any warranties of any kind.
      9   1.1       cgd  */
     10   1.1       cgd 
     11   1.3   mycroft #include <sys/param.h>
     12   1.3   mycroft #include <sys/systm.h>
     13   1.3   mycroft #include <sys/kernel.h>
     14   1.3   mycroft #include <sys/proc.h>
     15   1.3   mycroft #include <sys/sem.h>
     16   1.3   mycroft #include <sys/malloc.h>
     17   1.1       cgd 
     18  1.10       cgd #include <sys/mount.h>
     19  1.10       cgd #include <sys/syscallargs.h>
     20  1.25  christos 
     21   1.1       cgd int	semtot = 0;
     22  1.18   mycroft struct	proc *semlock_holder = NULL;
     23   1.1       cgd 
     24  1.27  christos #ifdef SEM_DEBUG
     25  1.27  christos #define SEM_PRINTF(a) kprintf a
     26  1.27  christos #else
     27  1.27  christos #define SEM_PRINTF(a)
     28  1.27  christos #endif
     29  1.27  christos 
     30  1.25  christos void semlock __P((struct proc *));
     31  1.25  christos struct sem_undo *semu_alloc __P((struct proc *));
     32  1.25  christos int semundo_adjust __P((struct proc *, struct sem_undo **, int, int, int));
     33  1.25  christos void semundo_clear __P((int, int));
     34  1.25  christos 
     35  1.25  christos void
     36   1.1       cgd seminit()
     37   1.1       cgd {
     38   1.5   mycroft 	register int i;
     39   1.1       cgd 
     40   1.5   mycroft 	if (sema == NULL)
     41   1.5   mycroft 		panic("sema is NULL");
     42   1.5   mycroft 	if (semu == NULL)
     43   1.5   mycroft 		panic("semu is NULL");
     44   1.5   mycroft 
     45   1.5   mycroft 	for (i = 0; i < seminfo.semmni; i++) {
     46   1.5   mycroft 		sema[i].sem_base = 0;
     47   1.5   mycroft 		sema[i].sem_perm.mode = 0;
     48   1.5   mycroft 	}
     49   1.5   mycroft 	for (i = 0; i < seminfo.semmnu; i++) {
     50   1.5   mycroft 		register struct sem_undo *suptr = SEMU(i);
     51   1.5   mycroft 		suptr->un_proc = NULL;
     52   1.5   mycroft 	}
     53   1.5   mycroft 	semu_list = NULL;
     54   1.1       cgd }
     55   1.1       cgd 
     56  1.13   mycroft void
     57  1.17   mycroft semlock(p)
     58  1.13   mycroft 	struct proc *p;
     59  1.13   mycroft {
     60  1.13   mycroft 
     61  1.13   mycroft 	while (semlock_holder != NULL && semlock_holder != p)
     62  1.13   mycroft 		sleep((caddr_t)&semlock_holder, (PZERO - 4));
     63  1.13   mycroft }
     64  1.13   mycroft 
     65   1.1       cgd /*
     66   1.1       cgd  * Lock or unlock the entire semaphore facility.
     67   1.1       cgd  *
     68   1.1       cgd  * This will probably eventually evolve into a general purpose semaphore
     69   1.1       cgd  * facility status enquiry mechanism (I don't like the "read /dev/kmem"
     70   1.1       cgd  * approach currently taken by ipcs and the amount of info that we want
     71  1.12   mycroft  * to be able to extract for ipcs is probably beyond the capability of
     72  1.12   mycroft  * the getkerninfo facility.
     73   1.1       cgd  *
     74   1.1       cgd  * At the time that the current version of semconfig was written, ipcs is
     75   1.1       cgd  * the only user of the semconfig facility.  It uses it to ensure that the
     76   1.1       cgd  * semaphore facility data structures remain static while it fishes around
     77   1.1       cgd  * in /dev/kmem.
     78   1.1       cgd  */
     79   1.1       cgd 
     80   1.1       cgd int
     81  1.24   mycroft sys_semconfig(p, v, retval)
     82   1.1       cgd 	struct proc *p;
     83  1.23   thorpej 	void *v;
     84  1.23   thorpej 	register_t *retval;
     85  1.23   thorpej {
     86  1.24   mycroft 	struct sys_semconfig_args /* {
     87  1.10       cgd 		syscallarg(int) flag;
     88  1.23   thorpej 	} */ *uap = v;
     89   1.5   mycroft 	int eval = 0;
     90   1.1       cgd 
     91  1.17   mycroft 	semlock(p);
     92  1.12   mycroft 
     93  1.10       cgd 	switch (SCARG(uap, flag)) {
     94   1.5   mycroft 	case SEM_CONFIG_FREEZE:
     95   1.5   mycroft 		semlock_holder = p;
     96   1.5   mycroft 		break;
     97   1.6   mycroft 
     98   1.5   mycroft 	case SEM_CONFIG_THAW:
     99   1.5   mycroft 		semlock_holder = NULL;
    100   1.5   mycroft 		wakeup((caddr_t)&semlock_holder);
    101   1.5   mycroft 		break;
    102   1.6   mycroft 
    103   1.5   mycroft 	default:
    104  1.27  christos 		kprintf(
    105  1.10       cgd 		    "semconfig: unknown flag parameter value (%d) - ignored\n",
    106  1.10       cgd 		    SCARG(uap, flag));
    107   1.5   mycroft 		eval = EINVAL;
    108   1.5   mycroft 		break;
    109   1.5   mycroft 	}
    110   1.1       cgd 
    111   1.5   mycroft 	*retval = 0;
    112   1.5   mycroft 	return(eval);
    113   1.1       cgd }
    114   1.1       cgd 
    115   1.1       cgd /*
    116   1.1       cgd  * Allocate a new sem_undo structure for a process
    117   1.1       cgd  * (returns ptr to structure or NULL if no more room)
    118   1.1       cgd  */
    119   1.1       cgd 
    120   1.1       cgd struct sem_undo *
    121   1.5   mycroft semu_alloc(p)
    122   1.5   mycroft 	struct proc *p;
    123   1.1       cgd {
    124   1.5   mycroft 	register int i;
    125   1.5   mycroft 	register struct sem_undo *suptr;
    126   1.5   mycroft 	register struct sem_undo **supptr;
    127   1.5   mycroft 	int attempt;
    128   1.1       cgd 
    129   1.1       cgd 	/*
    130   1.5   mycroft 	 * Try twice to allocate something.
    131   1.5   mycroft 	 * (we'll purge any empty structures after the first pass so
    132   1.5   mycroft 	 * two passes are always enough)
    133   1.1       cgd 	 */
    134   1.1       cgd 
    135   1.5   mycroft 	for (attempt = 0; attempt < 2; attempt++) {
    136   1.5   mycroft 		/*
    137   1.5   mycroft 		 * Look for a free structure.
    138   1.5   mycroft 		 * Fill it in and return it if we find one.
    139   1.5   mycroft 		 */
    140   1.5   mycroft 
    141   1.5   mycroft 		for (i = 0; i < seminfo.semmnu; i++) {
    142   1.5   mycroft 			suptr = SEMU(i);
    143   1.5   mycroft 			if (suptr->un_proc == NULL) {
    144   1.5   mycroft 				suptr->un_next = semu_list;
    145   1.5   mycroft 				semu_list = suptr;
    146   1.5   mycroft 				suptr->un_cnt = 0;
    147   1.5   mycroft 				suptr->un_proc = p;
    148   1.5   mycroft 				return(suptr);
    149   1.5   mycroft 			}
    150   1.5   mycroft 		}
    151   1.1       cgd 
    152   1.5   mycroft 		/*
    153   1.5   mycroft 		 * We didn't find a free one, if this is the first attempt
    154   1.5   mycroft 		 * then try to free some structures.
    155   1.5   mycroft 		 */
    156   1.5   mycroft 
    157   1.5   mycroft 		if (attempt == 0) {
    158   1.5   mycroft 			/* All the structures are in use - try to free some */
    159   1.5   mycroft 			int did_something = 0;
    160   1.5   mycroft 
    161   1.5   mycroft 			supptr = &semu_list;
    162   1.5   mycroft 			while ((suptr = *supptr) != NULL) {
    163   1.5   mycroft 				if (suptr->un_cnt == 0)  {
    164   1.5   mycroft 					suptr->un_proc = NULL;
    165   1.5   mycroft 					*supptr = suptr->un_next;
    166   1.5   mycroft 					did_something = 1;
    167   1.5   mycroft 				} else
    168   1.5   mycroft 					supptr = &(suptr->un_next);
    169   1.5   mycroft 			}
    170   1.5   mycroft 
    171   1.5   mycroft 			/* If we didn't free anything then just give-up */
    172   1.5   mycroft 			if (!did_something)
    173   1.5   mycroft 				return(NULL);
    174   1.5   mycroft 		} else {
    175   1.5   mycroft 			/*
    176   1.5   mycroft 			 * The second pass failed even though we freed
    177   1.5   mycroft 			 * something after the first pass!
    178   1.5   mycroft 			 * This is IMPOSSIBLE!
    179   1.5   mycroft 			 */
    180   1.5   mycroft 			panic("semu_alloc - second attempt failed");
    181   1.5   mycroft 		}
    182   1.1       cgd 	}
    183  1.25  christos 	return NULL;
    184   1.1       cgd }
    185   1.1       cgd 
    186   1.1       cgd /*
    187   1.1       cgd  * Adjust a particular entry for a particular proc
    188   1.1       cgd  */
    189   1.1       cgd 
    190   1.1       cgd int
    191   1.5   mycroft semundo_adjust(p, supptr, semid, semnum, adjval)
    192   1.5   mycroft 	register struct proc *p;
    193   1.5   mycroft 	struct sem_undo **supptr;
    194   1.5   mycroft 	int semid, semnum;
    195   1.5   mycroft 	int adjval;
    196   1.1       cgd {
    197   1.5   mycroft 	register struct sem_undo *suptr;
    198   1.5   mycroft 	register struct undo *sunptr;
    199   1.5   mycroft 	int i;
    200   1.1       cgd 
    201   1.5   mycroft 	/* Look for and remember the sem_undo if the caller doesn't provide
    202   1.5   mycroft 	   it */
    203   1.1       cgd 
    204   1.5   mycroft 	suptr = *supptr;
    205   1.4   mycroft 	if (suptr == NULL) {
    206  1.11   mycroft 		for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
    207   1.5   mycroft 			if (suptr->un_proc == p) {
    208   1.5   mycroft 				*supptr = suptr;
    209   1.5   mycroft 				break;
    210   1.5   mycroft 			}
    211   1.5   mycroft 		}
    212   1.5   mycroft 		if (suptr == NULL) {
    213   1.5   mycroft 			if (adjval == 0)
    214   1.5   mycroft 				return(0);
    215   1.5   mycroft 			suptr = semu_alloc(p);
    216   1.5   mycroft 			if (suptr == NULL)
    217   1.5   mycroft 				return(ENOSPC);
    218   1.5   mycroft 			*supptr = suptr;
    219   1.5   mycroft 		}
    220   1.1       cgd 	}
    221   1.1       cgd 
    222   1.6   mycroft 	/*
    223   1.6   mycroft 	 * Look for the requested entry and adjust it (delete if adjval becomes
    224   1.6   mycroft 	 * 0).
    225   1.6   mycroft 	 */
    226   1.6   mycroft 	sunptr = &suptr->un_ent[0];
    227   1.5   mycroft 	for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
    228   1.6   mycroft 		if (sunptr->un_id != semid || sunptr->un_num != semnum)
    229   1.6   mycroft 			continue;
    230   1.6   mycroft 		if (adjval == 0)
    231   1.6   mycroft 			sunptr->un_adjval = 0;
    232   1.6   mycroft 		else
    233   1.6   mycroft 			sunptr->un_adjval += adjval;
    234   1.6   mycroft 		if (sunptr->un_adjval == 0) {
    235   1.6   mycroft 			suptr->un_cnt--;
    236   1.6   mycroft 			if (i < suptr->un_cnt)
    237   1.6   mycroft 				suptr->un_ent[i] =
    238   1.6   mycroft 				    suptr->un_ent[suptr->un_cnt];
    239   1.5   mycroft 		}
    240   1.6   mycroft 		return(0);
    241   1.1       cgd 	}
    242   1.1       cgd 
    243   1.5   mycroft 	/* Didn't find the right entry - create it */
    244   1.5   mycroft 	if (adjval == 0)
    245   1.5   mycroft 		return(0);
    246  1.11   mycroft 	if (suptr->un_cnt == SEMUME)
    247   1.5   mycroft 		return(EINVAL);
    248  1.11   mycroft 
    249  1.11   mycroft 	sunptr = &suptr->un_ent[suptr->un_cnt];
    250  1.11   mycroft 	suptr->un_cnt++;
    251  1.11   mycroft 	sunptr->un_adjval = adjval;
    252  1.11   mycroft 	sunptr->un_id = semid;
    253  1.11   mycroft 	sunptr->un_num = semnum;
    254   1.1       cgd 	return(0);
    255   1.1       cgd }
    256   1.1       cgd 
    257   1.1       cgd void
    258   1.5   mycroft semundo_clear(semid, semnum)
    259   1.5   mycroft 	int semid, semnum;
    260   1.1       cgd {
    261   1.6   mycroft 	register struct sem_undo *suptr;
    262   1.1       cgd 
    263   1.6   mycroft 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
    264  1.19   mycroft 		register struct undo *sunptr;
    265  1.19   mycroft 		register int i;
    266   1.6   mycroft 
    267  1.19   mycroft 		sunptr = &suptr->un_ent[0];
    268  1.19   mycroft 		for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
    269   1.6   mycroft 			if (sunptr->un_id == semid) {
    270   1.6   mycroft 				if (semnum == -1 || sunptr->un_num == semnum) {
    271   1.6   mycroft 					suptr->un_cnt--;
    272   1.6   mycroft 					if (i < suptr->un_cnt) {
    273   1.6   mycroft 						suptr->un_ent[i] =
    274   1.6   mycroft 						  suptr->un_ent[suptr->un_cnt];
    275  1.19   mycroft 						i--, sunptr--;
    276   1.6   mycroft 					}
    277   1.6   mycroft 				}
    278   1.6   mycroft 				if (semnum != -1)
    279   1.6   mycroft 					break;
    280   1.6   mycroft 			}
    281   1.6   mycroft 		}
    282   1.1       cgd 	}
    283   1.1       cgd }
    284   1.1       cgd 
    285   1.1       cgd int
    286  1.24   mycroft sys___semctl(p, v, retval)
    287   1.1       cgd 	struct proc *p;
    288  1.25  christos 	register void *v;
    289  1.23   thorpej 	register_t *retval;
    290  1.23   thorpej {
    291  1.24   mycroft 	register struct sys___semctl_args /* {
    292  1.10       cgd 		syscallarg(int) semid;
    293  1.10       cgd 		syscallarg(int) semnum;
    294  1.10       cgd 		syscallarg(int) cmd;
    295  1.10       cgd 		syscallarg(union semun *) arg;
    296  1.23   thorpej 	} */ *uap = v;
    297  1.10       cgd 	int semid = SCARG(uap, semid);
    298  1.10       cgd 	int semnum = SCARG(uap, semnum);
    299  1.10       cgd 	int cmd = SCARG(uap, cmd);
    300  1.10       cgd 	union semun *arg = SCARG(uap, arg);
    301   1.6   mycroft 	union semun real_arg;
    302   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    303   1.6   mycroft 	int i, rval, eval;
    304   1.6   mycroft 	struct semid_ds sbuf;
    305   1.6   mycroft 	register struct semid_ds *semaptr;
    306   1.1       cgd 
    307  1.27  christos 	SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
    308  1.27  christos 	    semid, semnum, cmd, arg));
    309   1.1       cgd 
    310  1.17   mycroft 	semlock(p);
    311  1.12   mycroft 
    312   1.6   mycroft 	semid = IPCID_TO_IX(semid);
    313   1.6   mycroft 	if (semid < 0 || semid >= seminfo.semmsl)
    314   1.6   mycroft 		return(EINVAL);
    315   1.6   mycroft 
    316   1.6   mycroft 	semaptr = &sema[semid];
    317   1.6   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    318  1.10       cgd 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
    319   1.6   mycroft 		return(EINVAL);
    320   1.6   mycroft 
    321   1.6   mycroft 	eval = 0;
    322   1.6   mycroft 	rval = 0;
    323   1.1       cgd 
    324   1.6   mycroft 	switch (cmd) {
    325   1.6   mycroft 	case IPC_RMID:
    326  1.25  christos 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
    327   1.8   mycroft 			return(eval);
    328   1.6   mycroft 		semaptr->sem_perm.cuid = cred->cr_uid;
    329   1.6   mycroft 		semaptr->sem_perm.uid = cred->cr_uid;
    330   1.6   mycroft 		semtot -= semaptr->sem_nsems;
    331   1.6   mycroft 		for (i = semaptr->sem_base - sem; i < semtot; i++)
    332   1.6   mycroft 			sem[i] = sem[i + semaptr->sem_nsems];
    333   1.6   mycroft 		for (i = 0; i < seminfo.semmni; i++) {
    334   1.6   mycroft 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
    335   1.6   mycroft 			    sema[i].sem_base > semaptr->sem_base)
    336   1.6   mycroft 				sema[i].sem_base -= semaptr->sem_nsems;
    337   1.6   mycroft 		}
    338   1.6   mycroft 		semaptr->sem_perm.mode = 0;
    339   1.6   mycroft 		semundo_clear(semid, -1);
    340   1.6   mycroft 		wakeup((caddr_t)semaptr);
    341   1.6   mycroft 		break;
    342   1.1       cgd 
    343   1.6   mycroft 	case IPC_SET:
    344   1.8   mycroft 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
    345   1.8   mycroft 			return(eval);
    346   1.6   mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    347   1.6   mycroft 			return(eval);
    348   1.6   mycroft 		if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
    349   1.6   mycroft 		    sizeof(sbuf))) != 0)
    350   1.6   mycroft 			return(eval);
    351   1.6   mycroft 		semaptr->sem_perm.uid = sbuf.sem_perm.uid;
    352   1.6   mycroft 		semaptr->sem_perm.gid = sbuf.sem_perm.gid;
    353   1.6   mycroft 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
    354   1.6   mycroft 		    (sbuf.sem_perm.mode & 0777);
    355   1.6   mycroft 		semaptr->sem_ctime = time.tv_sec;
    356   1.6   mycroft 		break;
    357   1.1       cgd 
    358   1.6   mycroft 	case IPC_STAT:
    359   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    360   1.6   mycroft 			return(eval);
    361   1.6   mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    362   1.6   mycroft 			return(eval);
    363   1.6   mycroft 		eval = copyout((caddr_t)semaptr, real_arg.buf,
    364   1.6   mycroft 		    sizeof(struct semid_ds));
    365   1.6   mycroft 		break;
    366   1.1       cgd 
    367   1.6   mycroft 	case GETNCNT:
    368   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    369   1.6   mycroft 			return(eval);
    370   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    371   1.6   mycroft 			return(EINVAL);
    372   1.6   mycroft 		rval = semaptr->sem_base[semnum].semncnt;
    373   1.6   mycroft 		break;
    374   1.1       cgd 
    375   1.6   mycroft 	case GETPID:
    376   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    377   1.6   mycroft 			return(eval);
    378   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    379   1.6   mycroft 			return(EINVAL);
    380   1.6   mycroft 		rval = semaptr->sem_base[semnum].sempid;
    381   1.6   mycroft 		break;
    382   1.1       cgd 
    383   1.6   mycroft 	case GETVAL:
    384   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    385   1.6   mycroft 			return(eval);
    386   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    387   1.6   mycroft 			return(EINVAL);
    388   1.6   mycroft 		rval = semaptr->sem_base[semnum].semval;
    389   1.6   mycroft 		break;
    390   1.1       cgd 
    391   1.6   mycroft 	case GETALL:
    392   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    393   1.6   mycroft 			return(eval);
    394   1.6   mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    395   1.6   mycroft 			return(eval);
    396   1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    397   1.6   mycroft 			eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
    398   1.6   mycroft 			    &real_arg.array[i], sizeof(real_arg.array[0]));
    399   1.6   mycroft 			if (eval != 0)
    400   1.6   mycroft 				break;
    401   1.6   mycroft 		}
    402   1.6   mycroft 		break;
    403   1.1       cgd 
    404   1.6   mycroft 	case GETZCNT:
    405   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    406   1.6   mycroft 			return(eval);
    407   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    408   1.6   mycroft 			return(EINVAL);
    409   1.6   mycroft 		rval = semaptr->sem_base[semnum].semzcnt;
    410   1.6   mycroft 		break;
    411   1.1       cgd 
    412   1.6   mycroft 	case SETVAL:
    413   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    414   1.6   mycroft 			return(eval);
    415   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    416   1.6   mycroft 			return(EINVAL);
    417   1.6   mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    418   1.6   mycroft 			return(eval);
    419   1.6   mycroft 		semaptr->sem_base[semnum].semval = real_arg.val;
    420   1.6   mycroft 		semundo_clear(semid, semnum);
    421   1.6   mycroft 		wakeup((caddr_t)semaptr);
    422   1.6   mycroft 		break;
    423   1.1       cgd 
    424   1.6   mycroft 	case SETALL:
    425   1.7   hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    426   1.6   mycroft 			return(eval);
    427   1.6   mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    428   1.6   mycroft 			return(eval);
    429   1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    430   1.6   mycroft 			eval = copyin(&real_arg.array[i],
    431   1.6   mycroft 			    (caddr_t)&semaptr->sem_base[i].semval,
    432   1.6   mycroft 			    sizeof(real_arg.array[0]));
    433   1.6   mycroft 			if (eval != 0)
    434   1.6   mycroft 				break;
    435   1.6   mycroft 		}
    436   1.6   mycroft 		semundo_clear(semid, -1);
    437   1.6   mycroft 		wakeup((caddr_t)semaptr);
    438   1.6   mycroft 		break;
    439   1.1       cgd 
    440   1.6   mycroft 	default:
    441   1.6   mycroft 		return(EINVAL);
    442   1.6   mycroft 	}
    443   1.4   mycroft 
    444   1.6   mycroft 	if (eval == 0)
    445   1.6   mycroft 		*retval = rval;
    446   1.6   mycroft 	return(eval);
    447   1.1       cgd }
    448   1.1       cgd 
    449   1.1       cgd int
    450  1.24   mycroft sys_semget(p, v, retval)
    451   1.1       cgd 	struct proc *p;
    452  1.23   thorpej 	void *v;
    453  1.23   thorpej 	register_t *retval;
    454  1.23   thorpej {
    455  1.24   mycroft 	register struct sys_semget_args /* {
    456  1.10       cgd 		syscallarg(key_t) key;
    457  1.10       cgd 		syscallarg(int) nsems;
    458  1.10       cgd 		syscallarg(int) semflg;
    459  1.23   thorpej 	} */ *uap = v;
    460   1.6   mycroft 	int semid, eval;
    461  1.10       cgd 	int key = SCARG(uap, key);
    462  1.10       cgd 	int nsems = SCARG(uap, nsems);
    463  1.10       cgd 	int semflg = SCARG(uap, semflg);
    464   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    465   1.1       cgd 
    466  1.27  christos 	SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
    467   1.1       cgd 
    468  1.17   mycroft 	semlock(p);
    469  1.12   mycroft 
    470   1.6   mycroft 	if (key != IPC_PRIVATE) {
    471   1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    472   1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
    473   1.6   mycroft 			    sema[semid].sem_perm.key == key)
    474   1.6   mycroft 				break;
    475   1.6   mycroft 		}
    476   1.6   mycroft 		if (semid < seminfo.semmni) {
    477  1.27  christos 			SEM_PRINTF(("found public key\n"));
    478   1.7   hpeyerl 			if ((eval = ipcperm(cred, &sema[semid].sem_perm,
    479   1.7   hpeyerl 			    semflg & 0700)))
    480   1.6   mycroft 				return(eval);
    481   1.6   mycroft 			if (nsems > 0 && sema[semid].sem_nsems < nsems) {
    482  1.27  christos 				SEM_PRINTF(("too small\n"));
    483   1.6   mycroft 				return(EINVAL);
    484   1.6   mycroft 			}
    485   1.6   mycroft 			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
    486  1.27  christos 				SEM_PRINTF(("not exclusive\n"));
    487   1.6   mycroft 				return(EEXIST);
    488   1.6   mycroft 			}
    489   1.6   mycroft 			goto found;
    490   1.6   mycroft 		}
    491   1.6   mycroft 	}
    492   1.6   mycroft 
    493  1.27  christos 	SEM_PRINTF(("need to allocate the semid_ds\n"));
    494   1.6   mycroft 	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
    495   1.6   mycroft 		if (nsems <= 0 || nsems > seminfo.semmsl) {
    496  1.27  christos 			SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
    497  1.27  christos 			    seminfo.semmsl));
    498   1.6   mycroft 			return(EINVAL);
    499   1.6   mycroft 		}
    500   1.6   mycroft 		if (nsems > seminfo.semmns - semtot) {
    501  1.27  christos 			SEM_PRINTF(("not enough semaphores left (need %d, got %d)\n",
    502  1.27  christos 			    nsems, seminfo.semmns - semtot));
    503   1.6   mycroft 			return(ENOSPC);
    504   1.6   mycroft 		}
    505   1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    506   1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
    507   1.6   mycroft 				break;
    508   1.6   mycroft 		}
    509   1.6   mycroft 		if (semid == seminfo.semmni) {
    510  1.27  christos 			SEM_PRINTF(("no more semid_ds's available\n"));
    511   1.6   mycroft 			return(ENOSPC);
    512   1.6   mycroft 		}
    513  1.27  christos 		SEM_PRINTF(("semid %d is available\n", semid));
    514   1.6   mycroft 		sema[semid].sem_perm.key = key;
    515   1.6   mycroft 		sema[semid].sem_perm.cuid = cred->cr_uid;
    516   1.6   mycroft 		sema[semid].sem_perm.uid = cred->cr_uid;
    517   1.6   mycroft 		sema[semid].sem_perm.cgid = cred->cr_gid;
    518   1.6   mycroft 		sema[semid].sem_perm.gid = cred->cr_gid;
    519   1.6   mycroft 		sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
    520   1.6   mycroft 		sema[semid].sem_perm.seq =
    521   1.6   mycroft 		    (sema[semid].sem_perm.seq + 1) & 0x7fff;
    522   1.6   mycroft 		sema[semid].sem_nsems = nsems;
    523   1.6   mycroft 		sema[semid].sem_otime = 0;
    524   1.6   mycroft 		sema[semid].sem_ctime = time.tv_sec;
    525   1.6   mycroft 		sema[semid].sem_base = &sem[semtot];
    526   1.6   mycroft 		semtot += nsems;
    527   1.6   mycroft 		bzero(sema[semid].sem_base,
    528   1.6   mycroft 		    sizeof(sema[semid].sem_base[0])*nsems);
    529  1.27  christos 		SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid].sem_base,
    530  1.27  christos 		    &sem[semtot]));
    531   1.1       cgd 	} else {
    532  1.27  christos 		SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
    533   1.6   mycroft 		return(ENOENT);
    534   1.1       cgd 	}
    535   1.1       cgd 
    536   1.6   mycroft found:
    537   1.6   mycroft 	*retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
    538   1.6   mycroft 	return(0);
    539   1.1       cgd }
    540   1.1       cgd 
    541   1.1       cgd int
    542  1.24   mycroft sys_semop(p, v, retval)
    543   1.1       cgd 	struct proc *p;
    544  1.23   thorpej 	void *v;
    545  1.23   thorpej 	register_t *retval;
    546  1.23   thorpej {
    547  1.24   mycroft 	register struct sys_semop_args /* {
    548  1.10       cgd 		syscallarg(int) semid;
    549  1.10       cgd 		syscallarg(struct sembuf *) sops;
    550  1.10       cgd 		syscallarg(u_int) nsops;
    551  1.23   thorpej 	} */ *uap = v;
    552  1.10       cgd 	int semid = SCARG(uap, semid);
    553  1.10       cgd 	int nsops = SCARG(uap, nsops);
    554   1.6   mycroft 	struct sembuf sops[MAX_SOPS];
    555   1.6   mycroft 	register struct semid_ds *semaptr;
    556  1.25  christos 	register struct sembuf *sopptr = NULL;
    557  1.25  christos 	register struct sem *semptr = NULL;
    558   1.6   mycroft 	struct sem_undo *suptr = NULL;
    559   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    560   1.6   mycroft 	int i, j, eval;
    561  1.25  christos 	int do_wakeup, do_undos;
    562   1.1       cgd 
    563  1.27  christos 	SEM_PRINTF(("call to semop(%d, %p, %d)\n", semid, sops, nsops));
    564   1.1       cgd 
    565  1.17   mycroft 	semlock(p);
    566  1.12   mycroft 
    567   1.6   mycroft 	semid = IPCID_TO_IX(semid);	/* Convert back to zero origin */
    568   1.6   mycroft 
    569   1.6   mycroft 	if (semid < 0 || semid >= seminfo.semmsl)
    570   1.6   mycroft 		return(EINVAL);
    571   1.6   mycroft 
    572   1.6   mycroft 	semaptr = &sema[semid];
    573  1.11   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    574  1.11   mycroft 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
    575   1.6   mycroft 		return(EINVAL);
    576   1.6   mycroft 
    577   1.7   hpeyerl 	if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
    578  1.27  christos 		SEM_PRINTF(("eval = %d from ipaccess\n", eval));
    579   1.6   mycroft 		return(eval);
    580   1.6   mycroft 	}
    581   1.1       cgd 
    582   1.6   mycroft 	if (nsops > MAX_SOPS) {
    583  1.27  christos 		SEM_PRINTF(("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops));
    584   1.6   mycroft 		return(E2BIG);
    585   1.6   mycroft 	}
    586   1.1       cgd 
    587  1.10       cgd 	if ((eval = copyin(SCARG(uap, sops), sops, nsops * sizeof(sops[0])))
    588  1.10       cgd 	    != 0) {
    589  1.27  christos 		SEM_PRINTF(("eval = %d from copyin(%p, %p, %d)\n", eval,
    590  1.27  christos 		    SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
    591   1.6   mycroft 		return(eval);
    592   1.6   mycroft 	}
    593   1.1       cgd 
    594   1.6   mycroft 	/*
    595   1.6   mycroft 	 * Loop trying to satisfy the vector of requests.
    596   1.6   mycroft 	 * If we reach a point where we must wait, any requests already
    597   1.6   mycroft 	 * performed are rolled back and we go to sleep until some other
    598   1.6   mycroft 	 * process wakes us up.  At this point, we start all over again.
    599   1.6   mycroft 	 *
    600   1.6   mycroft 	 * This ensures that from the perspective of other tasks, a set
    601   1.6   mycroft 	 * of requests is atomic (never partially satisfied).
    602   1.6   mycroft 	 */
    603   1.6   mycroft 	do_undos = 0;
    604   1.1       cgd 
    605   1.6   mycroft 	for (;;) {
    606   1.6   mycroft 		do_wakeup = 0;
    607   1.1       cgd 
    608   1.6   mycroft 		for (i = 0; i < nsops; i++) {
    609   1.6   mycroft 			sopptr = &sops[i];
    610   1.1       cgd 
    611   1.6   mycroft 			if (sopptr->sem_num >= semaptr->sem_nsems)
    612   1.6   mycroft 				return(EFBIG);
    613   1.1       cgd 
    614   1.6   mycroft 			semptr = &semaptr->sem_base[sopptr->sem_num];
    615   1.1       cgd 
    616  1.27  christos 			SEM_PRINTF(("semop:  semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
    617   1.6   mycroft 			    semaptr, semaptr->sem_base, semptr,
    618   1.6   mycroft 			    sopptr->sem_num, semptr->semval, sopptr->sem_op,
    619  1.27  christos 			    (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait"));
    620   1.1       cgd 
    621   1.6   mycroft 			if (sopptr->sem_op < 0) {
    622  1.25  christos 				if ((int)(semptr->semval +
    623  1.25  christos 					  sopptr->sem_op) < 0) {
    624  1.27  christos 					SEM_PRINTF(("semop:  can't do it now\n"));
    625   1.6   mycroft 					break;
    626   1.6   mycroft 				} else {
    627   1.6   mycroft 					semptr->semval += sopptr->sem_op;
    628   1.6   mycroft 					if (semptr->semval == 0 &&
    629   1.6   mycroft 					    semptr->semzcnt > 0)
    630   1.6   mycroft 						do_wakeup = 1;
    631   1.6   mycroft 				}
    632   1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    633   1.6   mycroft 					do_undos = 1;
    634   1.6   mycroft 			} else if (sopptr->sem_op == 0) {
    635   1.6   mycroft 				if (semptr->semval > 0) {
    636  1.27  christos 					SEM_PRINTF(("semop:  not zero now\n"));
    637   1.6   mycroft 					break;
    638   1.6   mycroft 				}
    639   1.6   mycroft 			} else {
    640   1.6   mycroft 				if (semptr->semncnt > 0)
    641   1.6   mycroft 					do_wakeup = 1;
    642   1.6   mycroft 				semptr->semval += sopptr->sem_op;
    643   1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    644   1.6   mycroft 					do_undos = 1;
    645   1.6   mycroft 			}
    646   1.6   mycroft 		}
    647   1.1       cgd 
    648   1.6   mycroft 		/*
    649   1.6   mycroft 		 * Did we get through the entire vector?
    650   1.6   mycroft 		 */
    651   1.6   mycroft 		if (i >= nsops)
    652   1.6   mycroft 			goto done;
    653   1.1       cgd 
    654   1.6   mycroft 		/*
    655   1.6   mycroft 		 * No ... rollback anything that we've already done
    656   1.6   mycroft 		 */
    657  1.27  christos 		SEM_PRINTF(("semop:  rollback 0 through %d\n", i-1));
    658   1.6   mycroft 		for (j = 0; j < i; j++)
    659   1.6   mycroft 			semaptr->sem_base[sops[j].sem_num].semval -=
    660   1.6   mycroft 			    sops[j].sem_op;
    661   1.1       cgd 
    662   1.6   mycroft 		/*
    663   1.6   mycroft 		 * If the request that we couldn't satisfy has the
    664   1.6   mycroft 		 * NOWAIT flag set then return with EAGAIN.
    665   1.6   mycroft 		 */
    666   1.6   mycroft 		if (sopptr->sem_flg & IPC_NOWAIT)
    667   1.6   mycroft 			return(EAGAIN);
    668   1.1       cgd 
    669   1.6   mycroft 		if (sopptr->sem_op == 0)
    670   1.6   mycroft 			semptr->semzcnt++;
    671   1.6   mycroft 		else
    672   1.6   mycroft 			semptr->semncnt++;
    673   1.1       cgd 
    674  1.27  christos 		SEM_PRINTF(("semop:  good night!\n"));
    675   1.6   mycroft 		eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
    676   1.6   mycroft 		    "semwait", 0);
    677  1.27  christos 		SEM_PRINTF(("semop:  good morning (eval=%d)!\n", eval));
    678   1.1       cgd 
    679   1.6   mycroft 		suptr = NULL;	/* sem_undo may have been reallocated */
    680   1.1       cgd 
    681   1.6   mycroft 		if (eval != 0)
    682   1.6   mycroft 			return(EINTR);
    683  1.27  christos 		SEM_PRINTF(("semop:  good morning!\n"));
    684   1.1       cgd 
    685   1.6   mycroft 		/*
    686   1.6   mycroft 		 * Make sure that the semaphore still exists
    687   1.6   mycroft 		 */
    688   1.6   mycroft 		if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    689  1.10       cgd 		    semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid))) {
    690   1.6   mycroft 			/* The man page says to return EIDRM. */
    691   1.6   mycroft 			/* Unfortunately, BSD doesn't define that code! */
    692   1.1       cgd #ifdef EIDRM
    693   1.6   mycroft 			return(EIDRM);
    694   1.1       cgd #else
    695   1.6   mycroft 			return(EINVAL);
    696   1.1       cgd #endif
    697   1.6   mycroft 		}
    698   1.1       cgd 
    699   1.6   mycroft 		/*
    700   1.6   mycroft 		 * The semaphore is still alive.  Readjust the count of
    701   1.6   mycroft 		 * waiting processes.
    702   1.6   mycroft 		 */
    703   1.6   mycroft 		if (sopptr->sem_op == 0)
    704   1.6   mycroft 			semptr->semzcnt--;
    705   1.6   mycroft 		else
    706   1.6   mycroft 			semptr->semncnt--;
    707   1.6   mycroft 	}
    708   1.1       cgd 
    709   1.6   mycroft done:
    710   1.6   mycroft 	/*
    711   1.6   mycroft 	 * Process any SEM_UNDO requests.
    712   1.6   mycroft 	 */
    713   1.6   mycroft 	if (do_undos) {
    714   1.5   mycroft 		for (i = 0; i < nsops; i++) {
    715   1.6   mycroft 			/*
    716   1.6   mycroft 			 * We only need to deal with SEM_UNDO's for non-zero
    717   1.6   mycroft 			 * op's.
    718   1.6   mycroft 			 */
    719   1.6   mycroft 			int adjval;
    720   1.1       cgd 
    721   1.6   mycroft 			if ((sops[i].sem_flg & SEM_UNDO) == 0)
    722   1.6   mycroft 				continue;
    723   1.6   mycroft 			adjval = sops[i].sem_op;
    724   1.6   mycroft 			if (adjval == 0)
    725   1.6   mycroft 				continue;
    726   1.6   mycroft 			eval = semundo_adjust(p, &suptr, semid,
    727   1.6   mycroft 			    sops[i].sem_num, -adjval);
    728   1.6   mycroft 			if (eval == 0)
    729   1.6   mycroft 				continue;
    730   1.1       cgd 
    731   1.6   mycroft 			/*
    732   1.6   mycroft 			 * Oh-Oh!  We ran out of either sem_undo's or undo's.
    733   1.6   mycroft 			 * Rollback the adjustments to this point and then
    734   1.6   mycroft 			 * rollback the semaphore ups and down so we can return
    735   1.6   mycroft 			 * with an error with all structures restored.  We
    736   1.6   mycroft 			 * rollback the undo's in the exact reverse order that
    737   1.6   mycroft 			 * we applied them.  This guarantees that we won't run
    738   1.6   mycroft 			 * out of space as we roll things back out.
    739   1.6   mycroft 			 */
    740   1.6   mycroft 			for (j = i - 1; j >= 0; j--) {
    741   1.6   mycroft 				if ((sops[j].sem_flg & SEM_UNDO) == 0)
    742   1.6   mycroft 					continue;
    743   1.6   mycroft 				adjval = sops[j].sem_op;
    744   1.6   mycroft 				if (adjval == 0)
    745   1.6   mycroft 					continue;
    746   1.6   mycroft 				if (semundo_adjust(p, &suptr, semid,
    747   1.6   mycroft 				    sops[j].sem_num, adjval) != 0)
    748   1.1       cgd 					panic("semop - can't undo undos");
    749   1.6   mycroft 			}
    750   1.1       cgd 
    751   1.6   mycroft 			for (j = 0; j < nsops; j++)
    752   1.6   mycroft 				semaptr->sem_base[sops[j].sem_num].semval -=
    753   1.6   mycroft 				    sops[j].sem_op;
    754   1.1       cgd 
    755  1.27  christos 			SEM_PRINTF(("eval = %d from semundo_adjust\n", eval));
    756   1.6   mycroft 			return(eval);
    757   1.1       cgd 		} /* loop through the sops */
    758   1.6   mycroft 	} /* if (do_undos) */
    759   1.1       cgd 
    760   1.6   mycroft 	/* We're definitely done - set the sempid's */
    761   1.6   mycroft 	for (i = 0; i < nsops; i++) {
    762   1.1       cgd 		sopptr = &sops[i];
    763   1.1       cgd 		semptr = &semaptr->sem_base[sopptr->sem_num];
    764   1.1       cgd 		semptr->sempid = p->p_pid;
    765   1.6   mycroft 	}
    766   1.1       cgd 
    767   1.6   mycroft 	/* Do a wakeup if any semaphore was up'd. */
    768   1.6   mycroft 	if (do_wakeup) {
    769  1.27  christos 		SEM_PRINTF(("semop:  doing wakeup\n"));
    770   1.1       cgd #ifdef SEM_WAKEUP
    771   1.4   mycroft 		sem_wakeup((caddr_t)semaptr);
    772   1.1       cgd #else
    773   1.4   mycroft 		wakeup((caddr_t)semaptr);
    774   1.1       cgd #endif
    775  1.27  christos 		SEM_PRINTF(("semop:  back from wakeup\n"));
    776   1.6   mycroft 	}
    777  1.27  christos 	SEM_PRINTF(("semop:  done\n"));
    778   1.6   mycroft 	*retval = 0;
    779   1.6   mycroft 	return(0);
    780   1.1       cgd }
    781   1.1       cgd 
    782   1.1       cgd /*
    783   1.6   mycroft  * Go through the undo structures for this process and apply the adjustments to
    784   1.6   mycroft  * semaphores.
    785   1.1       cgd  */
    786  1.25  christos void
    787   1.1       cgd semexit(p)
    788   1.6   mycroft 	struct proc *p;
    789   1.1       cgd {
    790   1.6   mycroft 	register struct sem_undo *suptr;
    791   1.6   mycroft 	register struct sem_undo **supptr;
    792   1.1       cgd 
    793   1.6   mycroft 	/*
    794  1.17   mycroft 	 * Go through the chain of undo vectors looking for one associated with
    795  1.17   mycroft 	 * this process.
    796  1.17   mycroft 	 */
    797  1.17   mycroft 
    798  1.17   mycroft 	for (supptr = &semu_list; (suptr = *supptr) != NULL;
    799  1.17   mycroft 	    supptr = &suptr->un_next) {
    800  1.17   mycroft 		if (suptr->un_proc == p)
    801  1.17   mycroft 			break;
    802  1.17   mycroft 	}
    803  1.17   mycroft 
    804  1.17   mycroft 	/*
    805  1.14   mycroft 	 * There are a few possibilities to consider here ...
    806  1.14   mycroft 	 *
    807  1.14   mycroft 	 * 1) The semaphore facility isn't currently locked.  In this case,
    808  1.14   mycroft 	 *    this call should proceed normally.
    809  1.14   mycroft 	 * 2) The semaphore facility is locked by this process (i.e. the one
    810  1.14   mycroft 	 *    that is exiting).  In this case, this call should proceed as
    811  1.14   mycroft 	 *    usual and the facility should be unlocked at the end of this
    812  1.14   mycroft 	 *    routine (since the locker is exiting).
    813  1.14   mycroft 	 * 3) The semaphore facility is locked by some other process and this
    814  1.14   mycroft 	 *    process doesn't have an undo structure allocated for it.  In this
    815  1.14   mycroft 	 *    case, this call should proceed normally (i.e. not accomplish
    816  1.14   mycroft 	 *    anything and, most importantly, not block since that is
    817  1.14   mycroft 	 *    unnecessary and could result in a LOT of processes blocking in
    818  1.14   mycroft 	 *    here if the facility is locked for a long time).
    819  1.14   mycroft 	 * 4) The semaphore facility is locked by some other process and this
    820  1.14   mycroft 	 *    process has an undo structure allocated for it.  In this case,
    821  1.14   mycroft 	 *    this call should block until the facility has been unlocked since
    822  1.14   mycroft 	 *    the holder of the lock may be examining this process's proc entry
    823  1.14   mycroft 	 *    (the ipcs utility does this when printing out the information
    824  1.14   mycroft 	 *    from the allocated sem undo elements).
    825  1.14   mycroft 	 *
    826  1.14   mycroft 	 * This leads to the conclusion that we should not block unless we
    827  1.14   mycroft 	 * discover that the someone else has the semaphore facility locked and
    828  1.14   mycroft 	 * this process has an undo structure.  Let's do that...
    829  1.14   mycroft 	 *
    830  1.14   mycroft 	 * Note that we do this in a separate pass from the one that processes
    831  1.14   mycroft 	 * any existing undo structure since we don't want to risk blocking at
    832  1.14   mycroft 	 * that time (it would make the actual unlinking of the element from
    833  1.14   mycroft 	 * the chain of allocated undo structures rather messy).
    834  1.14   mycroft 	 */
    835  1.14   mycroft 
    836  1.14   mycroft 	/*
    837  1.14   mycroft 	 * Does someone else hold the semaphore facility's lock?
    838  1.14   mycroft 	 */
    839  1.14   mycroft 
    840  1.14   mycroft 	if (semlock_holder != NULL && semlock_holder != p) {
    841  1.14   mycroft 		/*
    842  1.14   mycroft 		 * Yes (i.e. we are in case 3 or 4).
    843  1.14   mycroft 		 *
    844  1.15   mycroft 		 * If we didn't find an undo vector associated with this
    845  1.14   mycroft 		 * process than we can just return (i.e. we are in case 3).
    846  1.14   mycroft 		 *
    847  1.14   mycroft 		 * Note that we know that someone else is holding the lock so
    848  1.14   mycroft 		 * we don't even have to see if we're holding it...
    849  1.14   mycroft 		 */
    850  1.14   mycroft 
    851  1.14   mycroft 		if (suptr == NULL)
    852  1.14   mycroft 			return;
    853  1.14   mycroft 
    854  1.14   mycroft 		/*
    855  1.14   mycroft 		 * We are in case 4.
    856  1.14   mycroft 		 *
    857  1.14   mycroft 		 * Go to sleep as long as someone else is locking the semaphore
    858  1.14   mycroft 		 * facility (note that we won't get here if we are holding the
    859  1.14   mycroft 		 * lock so we don't need to check for that possibility).
    860  1.14   mycroft 		 */
    861  1.14   mycroft 
    862  1.14   mycroft 		while (semlock_holder != NULL)
    863  1.14   mycroft 			sleep((caddr_t)&semlock_holder, (PZERO - 4));
    864  1.14   mycroft 
    865  1.14   mycroft 		/*
    866  1.14   mycroft 		 * Nobody is holding the facility (i.e. we are now in case 1).
    867  1.14   mycroft 		 * We can proceed safely according to the argument outlined
    868  1.14   mycroft 		 * above.
    869  1.14   mycroft 		 *
    870  1.16   mycroft 		 * We look up the undo vector again, in case the list changed
    871  1.17   mycroft 		 * while we were asleep, and the parent is now different.
    872  1.14   mycroft 		 */
    873  1.16   mycroft 
    874  1.17   mycroft 		for (supptr = &semu_list; (suptr = *supptr) != NULL;
    875  1.17   mycroft 		    supptr = &suptr->un_next) {
    876  1.17   mycroft 			if (suptr->un_proc == p)
    877  1.17   mycroft 				break;
    878  1.17   mycroft 		}
    879  1.17   mycroft 
    880  1.17   mycroft 		if (suptr == NULL)
    881  1.17   mycroft 			panic("semexit: undo vector disappeared");
    882  1.17   mycroft 	} else {
    883  1.17   mycroft 		/*
    884  1.17   mycroft 		 * No (i.e. we are in case 1 or 2).
    885  1.17   mycroft 		 *
    886  1.17   mycroft 		 * If there is no undo vector, skip to the end and unlock the
    887  1.17   mycroft 		 * semaphore facility if necessary.
    888  1.17   mycroft 		 */
    889  1.14   mycroft 
    890  1.17   mycroft 		if (suptr == NULL)
    891  1.17   mycroft 			goto unlock;
    892  1.14   mycroft 	}
    893  1.15   mycroft 
    894  1.15   mycroft 	/*
    895  1.17   mycroft 	 * We are now in case 1 or 2, and we have an undo vector for this
    896  1.17   mycroft 	 * process.
    897  1.15   mycroft 	 */
    898   1.1       cgd 
    899  1.27  christos 	SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
    900  1.27  christos 	    suptr->un_cnt));
    901   1.1       cgd 
    902   1.5   mycroft 	/*
    903   1.5   mycroft 	 * If there are any active undo elements then process them.
    904   1.5   mycroft 	 */
    905   1.5   mycroft 	if (suptr->un_cnt > 0) {
    906   1.6   mycroft 		int ix;
    907   1.1       cgd 
    908   1.6   mycroft 		for (ix = 0; ix < suptr->un_cnt; ix++) {
    909   1.6   mycroft 			int semid = suptr->un_ent[ix].un_id;
    910   1.6   mycroft 			int semnum = suptr->un_ent[ix].un_num;
    911   1.6   mycroft 			int adjval = suptr->un_ent[ix].un_adjval;
    912   1.6   mycroft 			struct semid_ds *semaptr;
    913   1.6   mycroft 
    914   1.6   mycroft 			semaptr = &sema[semid];
    915   1.6   mycroft 			if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
    916   1.6   mycroft 				panic("semexit - semid not allocated");
    917   1.6   mycroft 			if (semnum >= semaptr->sem_nsems)
    918   1.6   mycroft 				panic("semexit - semnum out of range");
    919   1.6   mycroft 
    920  1.27  christos 			SEM_PRINTF(("semexit:  %p id=%d num=%d(adj=%d) ; sem=%d\n",
    921   1.6   mycroft 			    suptr->un_proc, suptr->un_ent[ix].un_id,
    922   1.6   mycroft 			    suptr->un_ent[ix].un_num,
    923   1.6   mycroft 			    suptr->un_ent[ix].un_adjval,
    924  1.27  christos 			    semaptr->sem_base[semnum].semval));
    925   1.6   mycroft 
    926  1.14   mycroft 			if (adjval < 0 &&
    927  1.14   mycroft 			    semaptr->sem_base[semnum].semval < -adjval)
    928  1.14   mycroft 				semaptr->sem_base[semnum].semval = 0;
    929  1.14   mycroft 			else
    930   1.6   mycroft 				semaptr->sem_base[semnum].semval += adjval;
    931   1.1       cgd 
    932   1.1       cgd #ifdef SEM_WAKEUP
    933   1.6   mycroft 			sem_wakeup((caddr_t)semaptr);
    934   1.1       cgd #else
    935   1.6   mycroft 			wakeup((caddr_t)semaptr);
    936   1.1       cgd #endif
    937  1.27  christos 			SEM_PRINTF(("semexit:  back from wakeup\n"));
    938   1.6   mycroft 		}
    939   1.5   mycroft 	}
    940   1.1       cgd 
    941   1.5   mycroft 	/*
    942   1.5   mycroft 	 * Deallocate the undo vector.
    943   1.5   mycroft 	 */
    944  1.27  christos 	SEM_PRINTF(("removing vector\n"));
    945   1.5   mycroft 	suptr->un_proc = NULL;
    946   1.5   mycroft 	*supptr = suptr->un_next;
    947   1.1       cgd 
    948   1.6   mycroft unlock:
    949   1.6   mycroft 	/*
    950   1.6   mycroft 	 * If the exiting process is holding the global semaphore facility
    951  1.14   mycroft 	 * lock (i.e. we are in case 2) then release it.
    952   1.6   mycroft 	 */
    953   1.6   mycroft 	if (semlock_holder == p) {
    954   1.6   mycroft 		semlock_holder = NULL;
    955   1.6   mycroft 		wakeup((caddr_t)&semlock_holder);
    956   1.6   mycroft 	}
    957   1.1       cgd }
    958