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