Home | History | Annotate | Line # | Download | only in kern
sysv_sem.c revision 1.8
      1  1.1      cgd /*
      2  1.1      cgd  * Implementation of SVID semaphores
      3  1.1      cgd  *
      4  1.1      cgd  * Author:  Daniel Boulet
      5  1.1      cgd  *
      6  1.1      cgd  * This software is provided ``AS IS'' without any warranties of any kind.
      7  1.4  mycroft  *
      8  1.8  mycroft  *	$Id: sysv_sem.c,v 1.8 1994/05/25 08:15:49 mycroft Exp $
      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.1      cgd static int	semctl(), semget(), semop(), semconfig();
     19  1.1      cgd int	(*semcalls[])() = { semctl, semget, semop, semconfig };
     20  1.1      cgd int	semtot = 0;
     21  1.1      cgd 
     22  1.1      cgd static struct proc *semlock_holder = NULL;
     23  1.1      cgd 
     24  1.1      cgd int
     25  1.1      cgd seminit()
     26  1.1      cgd {
     27  1.5  mycroft 	register int i;
     28  1.5  mycroft 	vm_offset_t whocares1, whocares2;
     29  1.1      cgd 
     30  1.5  mycroft 	if (sema == NULL)
     31  1.5  mycroft 		panic("sema is NULL");
     32  1.5  mycroft 	if (semu == NULL)
     33  1.5  mycroft 		panic("semu is NULL");
     34  1.5  mycroft 
     35  1.5  mycroft 	for (i = 0; i < seminfo.semmni; i++) {
     36  1.5  mycroft 		sema[i].sem_base = 0;
     37  1.5  mycroft 		sema[i].sem_perm.mode = 0;
     38  1.5  mycroft 	}
     39  1.5  mycroft 	for (i = 0; i < seminfo.semmnu; i++) {
     40  1.5  mycroft 		register struct sem_undo *suptr = SEMU(i);
     41  1.5  mycroft 		suptr->un_proc = NULL;
     42  1.5  mycroft 	}
     43  1.5  mycroft 	semu_list = NULL;
     44  1.1      cgd }
     45  1.1      cgd 
     46  1.1      cgd /*
     47  1.1      cgd  * Entry point for all SEM calls
     48  1.1      cgd  */
     49  1.1      cgd 
     50  1.1      cgd struct semsys_args {
     51  1.1      cgd 	u_int	which;
     52  1.1      cgd };
     53  1.1      cgd 
     54  1.1      cgd int
     55  1.1      cgd semsys(p, uap, retval)
     56  1.1      cgd 	struct proc *p;
     57  1.1      cgd 	struct semsys_args *uap;
     58  1.1      cgd 	int *retval;
     59  1.1      cgd {
     60  1.5  mycroft 
     61  1.4  mycroft 	while (semlock_holder != NULL && semlock_holder != p)
     62  1.5  mycroft 		sleep((caddr_t)&semlock_holder, (PZERO - 4));
     63  1.1      cgd 
     64  1.1      cgd 	if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
     65  1.1      cgd 		return (EINVAL);
     66  1.1      cgd 	return ((*semcalls[uap->which])(p, &uap[1], retval));
     67  1.1      cgd }
     68  1.1      cgd 
     69  1.1      cgd /*
     70  1.1      cgd  * Lock or unlock the entire semaphore facility.
     71  1.1      cgd  *
     72  1.1      cgd  * This will probably eventually evolve into a general purpose semaphore
     73  1.1      cgd  * facility status enquiry mechanism (I don't like the "read /dev/kmem"
     74  1.1      cgd  * approach currently taken by ipcs and the amount of info that we want
     75  1.1      cgd  * to be able to extract for ipcs is probably beyond what the capability
     76  1.1      cgd  * of the getkerninfo facility.
     77  1.1      cgd  *
     78  1.1      cgd  * At the time that the current version of semconfig was written, ipcs is
     79  1.1      cgd  * the only user of the semconfig facility.  It uses it to ensure that the
     80  1.1      cgd  * semaphore facility data structures remain static while it fishes around
     81  1.1      cgd  * in /dev/kmem.
     82  1.1      cgd  */
     83  1.1      cgd 
     84  1.1      cgd struct semconfig_args {
     85  1.1      cgd 	semconfig_ctl_t	flag;
     86  1.1      cgd };
     87  1.1      cgd 
     88  1.1      cgd int
     89  1.1      cgd semconfig(p, uap, retval)
     90  1.1      cgd 	struct proc *p;
     91  1.1      cgd 	struct semconfig_args *uap;
     92  1.1      cgd 	int *retval;
     93  1.1      cgd {
     94  1.5  mycroft 	int eval = 0;
     95  1.1      cgd 
     96  1.5  mycroft 	switch (uap->flag) {
     97  1.5  mycroft 	case SEM_CONFIG_FREEZE:
     98  1.5  mycroft 		semlock_holder = p;
     99  1.5  mycroft 		break;
    100  1.6  mycroft 
    101  1.5  mycroft 	case SEM_CONFIG_THAW:
    102  1.5  mycroft 		semlock_holder = NULL;
    103  1.5  mycroft 		wakeup((caddr_t)&semlock_holder);
    104  1.5  mycroft 		break;
    105  1.6  mycroft 
    106  1.5  mycroft 	default:
    107  1.5  mycroft 		printf("semconfig: unknown flag parameter value (%d) - ignored\n",
    108  1.5  mycroft 		    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.1      cgd }
    186  1.1      cgd 
    187  1.1      cgd /*
    188  1.1      cgd  * Adjust a particular entry for a particular proc
    189  1.1      cgd  */
    190  1.1      cgd 
    191  1.1      cgd int
    192  1.5  mycroft semundo_adjust(p, supptr, semid, semnum, adjval)
    193  1.5  mycroft 	register struct proc *p;
    194  1.5  mycroft 	struct sem_undo **supptr;
    195  1.5  mycroft 	int semid, semnum;
    196  1.5  mycroft 	int adjval;
    197  1.1      cgd {
    198  1.5  mycroft 	register struct sem_undo *suptr;
    199  1.5  mycroft 	register struct undo *sunptr;
    200  1.5  mycroft 	int i;
    201  1.1      cgd 
    202  1.5  mycroft 	/* Look for and remember the sem_undo if the caller doesn't provide
    203  1.5  mycroft 	   it */
    204  1.1      cgd 
    205  1.5  mycroft 	suptr = *supptr;
    206  1.4  mycroft 	if (suptr == NULL) {
    207  1.5  mycroft 		for (suptr = semu_list; suptr != NULL;
    208  1.5  mycroft 		    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.5  mycroft 	if (suptr->un_cnt != SEMUME) {
    249  1.6  mycroft 		sunptr = &suptr->un_ent[suptr->un_cnt];
    250  1.5  mycroft 		suptr->un_cnt++;
    251  1.5  mycroft 		sunptr->un_adjval = adjval;
    252  1.5  mycroft 		sunptr->un_id = semid; sunptr->un_num = semnum;
    253  1.5  mycroft 	} else
    254  1.5  mycroft 		return(EINVAL);
    255  1.1      cgd 	return(0);
    256  1.1      cgd }
    257  1.1      cgd 
    258  1.1      cgd void
    259  1.5  mycroft semundo_clear(semid, semnum)
    260  1.5  mycroft 	int semid, semnum;
    261  1.1      cgd {
    262  1.6  mycroft 	register struct sem_undo *suptr;
    263  1.1      cgd 
    264  1.6  mycroft 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
    265  1.6  mycroft 		register struct undo *sunptr = &suptr->un_ent[0];
    266  1.6  mycroft 		register int i = 0;
    267  1.6  mycroft 
    268  1.6  mycroft 		while (i < suptr->un_cnt) {
    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.6  mycroft 						continue;
    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 			i++, sunptr++;
    282  1.6  mycroft 		}
    283  1.1      cgd 	}
    284  1.1      cgd }
    285  1.1      cgd 
    286  1.1      cgd struct semctl_args {
    287  1.1      cgd 	int	semid;
    288  1.1      cgd 	int	semnum;
    289  1.1      cgd 	int	cmd;
    290  1.1      cgd 	union	semun *arg;
    291  1.1      cgd };
    292  1.1      cgd 
    293  1.1      cgd int
    294  1.1      cgd semctl(p, uap, retval)
    295  1.1      cgd 	struct proc *p;
    296  1.1      cgd 	register struct semctl_args *uap;
    297  1.1      cgd 	int *retval;
    298  1.1      cgd {
    299  1.6  mycroft 	int semid = uap->semid;
    300  1.6  mycroft 	int semnum = uap->semnum;
    301  1.6  mycroft 	int cmd = uap->cmd;
    302  1.6  mycroft 	union semun *arg = 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.1      cgd #ifdef SEM_DEBUG
    310  1.6  mycroft 	printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
    311  1.1      cgd #endif
    312  1.1      cgd 
    313  1.6  mycroft 	semid = IPCID_TO_IX(semid);
    314  1.6  mycroft 	if (semid < 0 || semid >= seminfo.semmsl)
    315  1.6  mycroft 		return(EINVAL);
    316  1.6  mycroft 
    317  1.6  mycroft 	semaptr = &sema[semid];
    318  1.6  mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    319  1.6  mycroft 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
    320  1.6  mycroft 		return(EINVAL);
    321  1.6  mycroft 
    322  1.6  mycroft 	eval = 0;
    323  1.6  mycroft 	rval = 0;
    324  1.1      cgd 
    325  1.6  mycroft 	switch (cmd) {
    326  1.6  mycroft 	case IPC_RMID:
    327  1.8  mycroft 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
    328  1.8  mycroft 			return(eval);
    329  1.6  mycroft 		semaptr->sem_perm.cuid = cred->cr_uid;
    330  1.6  mycroft 		semaptr->sem_perm.uid = cred->cr_uid;
    331  1.6  mycroft 		semtot -= semaptr->sem_nsems;
    332  1.6  mycroft 		for (i = semaptr->sem_base - sem; i < semtot; i++)
    333  1.6  mycroft 			sem[i] = sem[i + semaptr->sem_nsems];
    334  1.6  mycroft 		for (i = 0; i < seminfo.semmni; i++) {
    335  1.6  mycroft 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
    336  1.6  mycroft 			    sema[i].sem_base > semaptr->sem_base)
    337  1.6  mycroft 				sema[i].sem_base -= semaptr->sem_nsems;
    338  1.6  mycroft 		}
    339  1.6  mycroft 		semaptr->sem_perm.mode = 0;
    340  1.6  mycroft 		semundo_clear(semid, -1);
    341  1.6  mycroft 		wakeup((caddr_t)semaptr);
    342  1.6  mycroft 		break;
    343  1.1      cgd 
    344  1.6  mycroft 	case IPC_SET:
    345  1.8  mycroft 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
    346  1.8  mycroft 			return(eval);
    347  1.6  mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    348  1.6  mycroft 			return(eval);
    349  1.6  mycroft 		if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
    350  1.6  mycroft 		    sizeof(sbuf))) != 0)
    351  1.6  mycroft 			return(eval);
    352  1.6  mycroft 		semaptr->sem_perm.uid = sbuf.sem_perm.uid;
    353  1.6  mycroft 		semaptr->sem_perm.gid = sbuf.sem_perm.gid;
    354  1.6  mycroft 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
    355  1.6  mycroft 		    (sbuf.sem_perm.mode & 0777);
    356  1.6  mycroft 		semaptr->sem_ctime = time.tv_sec;
    357  1.6  mycroft 		break;
    358  1.1      cgd 
    359  1.6  mycroft 	case IPC_STAT:
    360  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    361  1.6  mycroft 			return(eval);
    362  1.6  mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    363  1.6  mycroft 			return(eval);
    364  1.6  mycroft 		eval = copyout((caddr_t)semaptr, real_arg.buf,
    365  1.6  mycroft 		    sizeof(struct semid_ds));
    366  1.6  mycroft 		break;
    367  1.1      cgd 
    368  1.6  mycroft 	case GETNCNT:
    369  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    370  1.6  mycroft 			return(eval);
    371  1.6  mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    372  1.6  mycroft 			return(EINVAL);
    373  1.6  mycroft 		rval = semaptr->sem_base[semnum].semncnt;
    374  1.6  mycroft 		break;
    375  1.1      cgd 
    376  1.6  mycroft 	case GETPID:
    377  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    378  1.6  mycroft 			return(eval);
    379  1.6  mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    380  1.6  mycroft 			return(EINVAL);
    381  1.6  mycroft 		rval = semaptr->sem_base[semnum].sempid;
    382  1.6  mycroft 		break;
    383  1.1      cgd 
    384  1.6  mycroft 	case GETVAL:
    385  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    386  1.6  mycroft 			return(eval);
    387  1.6  mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    388  1.6  mycroft 			return(EINVAL);
    389  1.6  mycroft 		rval = semaptr->sem_base[semnum].semval;
    390  1.6  mycroft 		break;
    391  1.1      cgd 
    392  1.6  mycroft 	case GETALL:
    393  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    394  1.6  mycroft 			return(eval);
    395  1.6  mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    396  1.6  mycroft 			return(eval);
    397  1.6  mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    398  1.6  mycroft 			eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
    399  1.6  mycroft 			    &real_arg.array[i], sizeof(real_arg.array[0]));
    400  1.6  mycroft 			if (eval != 0)
    401  1.6  mycroft 				break;
    402  1.6  mycroft 		}
    403  1.6  mycroft 		break;
    404  1.1      cgd 
    405  1.6  mycroft 	case GETZCNT:
    406  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    407  1.6  mycroft 			return(eval);
    408  1.6  mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    409  1.6  mycroft 			return(EINVAL);
    410  1.6  mycroft 		rval = semaptr->sem_base[semnum].semzcnt;
    411  1.6  mycroft 		break;
    412  1.1      cgd 
    413  1.6  mycroft 	case SETVAL:
    414  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    415  1.6  mycroft 			return(eval);
    416  1.6  mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    417  1.6  mycroft 			return(EINVAL);
    418  1.6  mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    419  1.6  mycroft 			return(eval);
    420  1.6  mycroft 		semaptr->sem_base[semnum].semval = real_arg.val;
    421  1.6  mycroft 		semundo_clear(semid, semnum);
    422  1.6  mycroft 		wakeup((caddr_t)semaptr);
    423  1.6  mycroft 		break;
    424  1.1      cgd 
    425  1.6  mycroft 	case SETALL:
    426  1.7  hpeyerl 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    427  1.6  mycroft 			return(eval);
    428  1.6  mycroft 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
    429  1.6  mycroft 			return(eval);
    430  1.6  mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    431  1.6  mycroft 			eval = copyin(&real_arg.array[i],
    432  1.6  mycroft 			    (caddr_t)&semaptr->sem_base[i].semval,
    433  1.6  mycroft 			    sizeof(real_arg.array[0]));
    434  1.6  mycroft 			if (eval != 0)
    435  1.6  mycroft 				break;
    436  1.6  mycroft 		}
    437  1.6  mycroft 		semundo_clear(semid, -1);
    438  1.6  mycroft 		wakeup((caddr_t)semaptr);
    439  1.6  mycroft 		break;
    440  1.1      cgd 
    441  1.6  mycroft 	default:
    442  1.6  mycroft 		return(EINVAL);
    443  1.6  mycroft 	}
    444  1.4  mycroft 
    445  1.6  mycroft 	if (eval == 0)
    446  1.6  mycroft 		*retval = rval;
    447  1.6  mycroft 	return(eval);
    448  1.1      cgd }
    449  1.1      cgd 
    450  1.1      cgd struct semget_args {
    451  1.1      cgd 	key_t	key;
    452  1.1      cgd 	int	nsems;
    453  1.1      cgd 	int	semflg;
    454  1.1      cgd };
    455  1.1      cgd 
    456  1.1      cgd int
    457  1.1      cgd semget(p, uap, retval)
    458  1.1      cgd 	struct proc *p;
    459  1.1      cgd 	register struct semget_args *uap;
    460  1.1      cgd 	int *retval;
    461  1.1      cgd {
    462  1.6  mycroft 	int semid, eval;
    463  1.6  mycroft 	int key = uap->key;
    464  1.6  mycroft 	int nsems = uap->nsems;
    465  1.6  mycroft 	int semflg = uap->semflg;
    466  1.6  mycroft 	struct ucred *cred = p->p_ucred;
    467  1.1      cgd 
    468  1.1      cgd #ifdef SEM_DEBUG
    469  1.6  mycroft 	printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
    470  1.1      cgd #endif
    471  1.1      cgd 
    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.1      cgd #ifdef SEM_DEBUG
    480  1.6  mycroft 			printf("found public key\n");
    481  1.1      cgd #endif
    482  1.7  hpeyerl 			if ((eval = ipcperm(cred, &sema[semid].sem_perm,
    483  1.7  hpeyerl 			    semflg & 0700)))
    484  1.6  mycroft 				return(eval);
    485  1.6  mycroft 			if (nsems > 0 && sema[semid].sem_nsems < nsems) {
    486  1.1      cgd #ifdef SEM_DEBUG
    487  1.6  mycroft 				printf("too small\n");
    488  1.1      cgd #endif
    489  1.6  mycroft 				return(EINVAL);
    490  1.6  mycroft 			}
    491  1.6  mycroft 			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
    492  1.1      cgd #ifdef SEM_DEBUG
    493  1.6  mycroft 				printf("not exclusive\n");
    494  1.1      cgd #endif
    495  1.6  mycroft 				return(EEXIST);
    496  1.6  mycroft 			}
    497  1.6  mycroft 			goto found;
    498  1.6  mycroft 		}
    499  1.6  mycroft 	}
    500  1.6  mycroft 
    501  1.1      cgd #ifdef SEM_DEBUG
    502  1.6  mycroft 	printf("need to allocate the semid_ds\n");
    503  1.1      cgd #endif
    504  1.6  mycroft 	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
    505  1.6  mycroft 		if (nsems <= 0 || nsems > seminfo.semmsl) {
    506  1.1      cgd #ifdef SEM_DEBUG
    507  1.6  mycroft 			printf("nsems out of range (0<%d<=%d)\n", nsems,
    508  1.6  mycroft 			    seminfo.semmsl);
    509  1.1      cgd #endif
    510  1.6  mycroft 			return(EINVAL);
    511  1.6  mycroft 		}
    512  1.6  mycroft 		if (nsems > seminfo.semmns - semtot) {
    513  1.1      cgd #ifdef SEM_DEBUG
    514  1.6  mycroft 			printf("not enough semaphores left (need %d, got %d)\n",
    515  1.6  mycroft 			    nsems, seminfo.semmns - semtot);
    516  1.1      cgd #endif
    517  1.6  mycroft 			return(ENOSPC);
    518  1.6  mycroft 		}
    519  1.6  mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    520  1.6  mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
    521  1.6  mycroft 				break;
    522  1.6  mycroft 		}
    523  1.6  mycroft 		if (semid == seminfo.semmni) {
    524  1.1      cgd #ifdef SEM_DEBUG
    525  1.6  mycroft 			printf("no more semid_ds's available\n");
    526  1.1      cgd #endif
    527  1.6  mycroft 			return(ENOSPC);
    528  1.6  mycroft 		}
    529  1.1      cgd #ifdef SEM_DEBUG
    530  1.6  mycroft 		printf("semid %d is available\n", semid);
    531  1.1      cgd #endif
    532  1.6  mycroft 		sema[semid].sem_perm.key = key;
    533  1.6  mycroft 		sema[semid].sem_perm.cuid = cred->cr_uid;
    534  1.6  mycroft 		sema[semid].sem_perm.uid = cred->cr_uid;
    535  1.6  mycroft 		sema[semid].sem_perm.cgid = cred->cr_gid;
    536  1.6  mycroft 		sema[semid].sem_perm.gid = cred->cr_gid;
    537  1.6  mycroft 		sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
    538  1.6  mycroft 		sema[semid].sem_perm.seq =
    539  1.6  mycroft 		    (sema[semid].sem_perm.seq + 1) & 0x7fff;
    540  1.6  mycroft 		sema[semid].sem_nsems = nsems;
    541  1.6  mycroft 		sema[semid].sem_otime = 0;
    542  1.6  mycroft 		sema[semid].sem_ctime = time.tv_sec;
    543  1.6  mycroft 		sema[semid].sem_base = &sem[semtot];
    544  1.6  mycroft 		semtot += nsems;
    545  1.6  mycroft 		bzero(sema[semid].sem_base,
    546  1.6  mycroft 		    sizeof(sema[semid].sem_base[0])*nsems);
    547  1.1      cgd #ifdef SEM_DEBUG
    548  1.6  mycroft 		printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
    549  1.6  mycroft 		    &sem[semtot]);
    550  1.1      cgd #endif
    551  1.1      cgd 	} else {
    552  1.1      cgd #ifdef SEM_DEBUG
    553  1.6  mycroft 		printf("didn't find it and wasn't asked to create it\n");
    554  1.1      cgd #endif
    555  1.6  mycroft 		return(ENOENT);
    556  1.1      cgd 	}
    557  1.1      cgd 
    558  1.6  mycroft found:
    559  1.6  mycroft 	*retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
    560  1.6  mycroft 	return(0);
    561  1.1      cgd }
    562  1.1      cgd 
    563  1.1      cgd struct semop_args {
    564  1.1      cgd 	int	semid;
    565  1.1      cgd 	struct	sembuf *sops;
    566  1.1      cgd 	int	nsops;
    567  1.1      cgd };
    568  1.1      cgd 
    569  1.1      cgd int
    570  1.1      cgd semop(p, uap, retval)
    571  1.1      cgd 	struct proc *p;
    572  1.1      cgd 	register struct semop_args *uap;
    573  1.1      cgd 	int *retval;
    574  1.1      cgd {
    575  1.6  mycroft 	int semid = uap->semid;
    576  1.6  mycroft 	int nsops = uap->nsops;
    577  1.6  mycroft 	struct sembuf sops[MAX_SOPS];
    578  1.6  mycroft 	register struct semid_ds *semaptr;
    579  1.6  mycroft 	register struct sembuf *sopptr;
    580  1.6  mycroft 	register struct sem *semptr;
    581  1.6  mycroft 	struct sem_undo *suptr = NULL;
    582  1.6  mycroft 	struct ucred *cred = p->p_ucred;
    583  1.6  mycroft 	int i, j, eval;
    584  1.6  mycroft 	int all_ok, do_wakeup, do_undos;
    585  1.1      cgd 
    586  1.1      cgd #ifdef SEM_DEBUG
    587  1.6  mycroft 	printf("call to semop(%d, 0x%x, %d)\n", semid, sops, nsops);
    588  1.1      cgd #endif
    589  1.1      cgd 
    590  1.6  mycroft 	semid = IPCID_TO_IX(semid);	/* Convert back to zero origin */
    591  1.6  mycroft 
    592  1.6  mycroft 	if (semid < 0 || semid >= seminfo.semmsl)
    593  1.6  mycroft 		return(EINVAL);
    594  1.6  mycroft 
    595  1.6  mycroft 	semaptr = &sema[semid];
    596  1.6  mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
    597  1.6  mycroft 		return(EINVAL);
    598  1.6  mycroft 	if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
    599  1.6  mycroft 		return(EINVAL);
    600  1.6  mycroft 
    601  1.7  hpeyerl 	if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
    602  1.1      cgd #ifdef SEM_DEBUG
    603  1.6  mycroft 		printf("eval = %d from ipaccess\n", eval);
    604  1.1      cgd #endif
    605  1.6  mycroft 		return(eval);
    606  1.6  mycroft 	}
    607  1.1      cgd 
    608  1.6  mycroft 	if (nsops > MAX_SOPS) {
    609  1.1      cgd #ifdef SEM_DEBUG
    610  1.6  mycroft 		printf("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops);
    611  1.1      cgd #endif
    612  1.6  mycroft 		return(E2BIG);
    613  1.6  mycroft 	}
    614  1.1      cgd 
    615  1.6  mycroft 	if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
    616  1.6  mycroft #ifdef SEM_DEBUG
    617  1.6  mycroft 		printf("eval = %d from copyin(%08x, %08x, %d)\n", eval,
    618  1.6  mycroft 		    uap->sops, &sops, nsops * sizeof(sops[0]));
    619  1.6  mycroft #endif
    620  1.6  mycroft 		return(eval);
    621  1.6  mycroft 	}
    622  1.1      cgd 
    623  1.6  mycroft 	/*
    624  1.6  mycroft 	 * Loop trying to satisfy the vector of requests.
    625  1.6  mycroft 	 * If we reach a point where we must wait, any requests already
    626  1.6  mycroft 	 * performed are rolled back and we go to sleep until some other
    627  1.6  mycroft 	 * process wakes us up.  At this point, we start all over again.
    628  1.6  mycroft 	 *
    629  1.6  mycroft 	 * This ensures that from the perspective of other tasks, a set
    630  1.6  mycroft 	 * of requests is atomic (never partially satisfied).
    631  1.6  mycroft 	 */
    632  1.6  mycroft 	do_undos = 0;
    633  1.1      cgd 
    634  1.6  mycroft 	for (;;) {
    635  1.6  mycroft 		do_wakeup = 0;
    636  1.1      cgd 
    637  1.6  mycroft 		for (i = 0; i < nsops; i++) {
    638  1.6  mycroft 			sopptr = &sops[i];
    639  1.1      cgd 
    640  1.6  mycroft 			if (sopptr->sem_num >= semaptr->sem_nsems)
    641  1.6  mycroft 				return(EFBIG);
    642  1.1      cgd 
    643  1.6  mycroft 			semptr = &semaptr->sem_base[sopptr->sem_num];
    644  1.1      cgd 
    645  1.1      cgd #ifdef SEM_DEBUG
    646  1.6  mycroft 			printf("semop:  semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
    647  1.6  mycroft 			    semaptr, semaptr->sem_base, semptr,
    648  1.6  mycroft 			    sopptr->sem_num, semptr->semval, sopptr->sem_op,
    649  1.6  mycroft 			    (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
    650  1.1      cgd #endif
    651  1.1      cgd 
    652  1.6  mycroft 			if (sopptr->sem_op < 0) {
    653  1.6  mycroft 				if (semptr->semval + sopptr->sem_op < 0) {
    654  1.1      cgd #ifdef SEM_DEBUG
    655  1.6  mycroft 					printf("semop:  can't do it now\n");
    656  1.1      cgd #endif
    657  1.6  mycroft 					break;
    658  1.6  mycroft 				} else {
    659  1.6  mycroft 					semptr->semval += sopptr->sem_op;
    660  1.6  mycroft 					if (semptr->semval == 0 &&
    661  1.6  mycroft 					    semptr->semzcnt > 0)
    662  1.6  mycroft 						do_wakeup = 1;
    663  1.6  mycroft 				}
    664  1.6  mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    665  1.6  mycroft 					do_undos = 1;
    666  1.6  mycroft 			} else if (sopptr->sem_op == 0) {
    667  1.6  mycroft 				if (semptr->semval > 0) {
    668  1.6  mycroft #ifdef SEM_DEBUG
    669  1.6  mycroft 					printf("semop:  not zero now\n");
    670  1.6  mycroft #endif
    671  1.6  mycroft 					break;
    672  1.6  mycroft 				}
    673  1.6  mycroft 			} else {
    674  1.6  mycroft 				if (semptr->semncnt > 0)
    675  1.6  mycroft 					do_wakeup = 1;
    676  1.6  mycroft 				semptr->semval += sopptr->sem_op;
    677  1.6  mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    678  1.6  mycroft 					do_undos = 1;
    679  1.6  mycroft 			}
    680  1.6  mycroft 		}
    681  1.1      cgd 
    682  1.6  mycroft 		/*
    683  1.6  mycroft 		 * Did we get through the entire vector?
    684  1.6  mycroft 		 */
    685  1.6  mycroft 		if (i >= nsops)
    686  1.6  mycroft 			goto done;
    687  1.1      cgd 
    688  1.6  mycroft 		/*
    689  1.6  mycroft 		 * No ... rollback anything that we've already done
    690  1.6  mycroft 		 */
    691  1.1      cgd #ifdef SEM_DEBUG
    692  1.6  mycroft 		printf("semop:  rollback 0 through %d\n", i-1);
    693  1.1      cgd #endif
    694  1.6  mycroft 		for (j = 0; j < i; j++)
    695  1.6  mycroft 			semaptr->sem_base[sops[j].sem_num].semval -=
    696  1.6  mycroft 			    sops[j].sem_op;
    697  1.1      cgd 
    698  1.6  mycroft 		/*
    699  1.6  mycroft 		 * If the request that we couldn't satisfy has the
    700  1.6  mycroft 		 * NOWAIT flag set then return with EAGAIN.
    701  1.6  mycroft 		 */
    702  1.6  mycroft 		if (sopptr->sem_flg & IPC_NOWAIT)
    703  1.6  mycroft 			return(EAGAIN);
    704  1.1      cgd 
    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.1      cgd 
    710  1.1      cgd #ifdef SEM_DEBUG
    711  1.6  mycroft 		printf("semop:  good night!\n");
    712  1.1      cgd #endif
    713  1.6  mycroft 		eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
    714  1.6  mycroft 		    "semwait", 0);
    715  1.1      cgd #ifdef SEM_DEBUG
    716  1.6  mycroft 		printf("semop:  good morning (eval=%d)!\n", eval);
    717  1.1      cgd #endif
    718  1.1      cgd 
    719  1.6  mycroft 		suptr = NULL;	/* sem_undo may have been reallocated */
    720  1.1      cgd 
    721  1.6  mycroft 		if (eval != 0)
    722  1.6  mycroft 			return(EINTR);
    723  1.1      cgd #ifdef SEM_DEBUG
    724  1.6  mycroft 		printf("semop:  good morning!\n");
    725  1.1      cgd #endif
    726  1.1      cgd 
    727  1.6  mycroft 		/*
    728  1.6  mycroft 		 * Make sure that the semaphore still exists
    729  1.6  mycroft 		 */
    730  1.6  mycroft 		if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    731  1.6  mycroft 		    semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid)) {
    732  1.6  mycroft 			/* The man page says to return EIDRM. */
    733  1.6  mycroft 			/* Unfortunately, BSD doesn't define that code! */
    734  1.1      cgd #ifdef EIDRM
    735  1.6  mycroft 			return(EIDRM);
    736  1.1      cgd #else
    737  1.6  mycroft 			return(EINVAL);
    738  1.1      cgd #endif
    739  1.6  mycroft 		}
    740  1.1      cgd 
    741  1.6  mycroft 		/*
    742  1.6  mycroft 		 * The semaphore is still alive.  Readjust the count of
    743  1.6  mycroft 		 * waiting processes.
    744  1.6  mycroft 		 */
    745  1.6  mycroft 		if (sopptr->sem_op == 0)
    746  1.6  mycroft 			semptr->semzcnt--;
    747  1.6  mycroft 		else
    748  1.6  mycroft 			semptr->semncnt--;
    749  1.6  mycroft 	}
    750  1.1      cgd 
    751  1.6  mycroft done:
    752  1.6  mycroft 	/*
    753  1.6  mycroft 	 * Process any SEM_UNDO requests.
    754  1.6  mycroft 	 */
    755  1.6  mycroft 	if (do_undos) {
    756  1.5  mycroft 		for (i = 0; i < nsops; i++) {
    757  1.6  mycroft 			/*
    758  1.6  mycroft 			 * We only need to deal with SEM_UNDO's for non-zero
    759  1.6  mycroft 			 * op's.
    760  1.6  mycroft 			 */
    761  1.6  mycroft 			int adjval;
    762  1.1      cgd 
    763  1.6  mycroft 			if ((sops[i].sem_flg & SEM_UNDO) == 0)
    764  1.6  mycroft 				continue;
    765  1.6  mycroft 			adjval = sops[i].sem_op;
    766  1.6  mycroft 			if (adjval == 0)
    767  1.6  mycroft 				continue;
    768  1.6  mycroft 			eval = semundo_adjust(p, &suptr, semid,
    769  1.6  mycroft 			    sops[i].sem_num, -adjval);
    770  1.6  mycroft 			if (eval == 0)
    771  1.6  mycroft 				continue;
    772  1.1      cgd 
    773  1.6  mycroft 			/*
    774  1.6  mycroft 			 * Oh-Oh!  We ran out of either sem_undo's or undo's.
    775  1.6  mycroft 			 * Rollback the adjustments to this point and then
    776  1.6  mycroft 			 * rollback the semaphore ups and down so we can return
    777  1.6  mycroft 			 * with an error with all structures restored.  We
    778  1.6  mycroft 			 * rollback the undo's in the exact reverse order that
    779  1.6  mycroft 			 * we applied them.  This guarantees that we won't run
    780  1.6  mycroft 			 * out of space as we roll things back out.
    781  1.6  mycroft 			 */
    782  1.6  mycroft 			for (j = i - 1; j >= 0; j--) {
    783  1.6  mycroft 				if ((sops[j].sem_flg & SEM_UNDO) == 0)
    784  1.6  mycroft 					continue;
    785  1.6  mycroft 				adjval = sops[j].sem_op;
    786  1.6  mycroft 				if (adjval == 0)
    787  1.6  mycroft 					continue;
    788  1.6  mycroft 				if (semundo_adjust(p, &suptr, semid,
    789  1.6  mycroft 				    sops[j].sem_num, adjval) != 0)
    790  1.1      cgd 					panic("semop - can't undo undos");
    791  1.6  mycroft 			}
    792  1.1      cgd 
    793  1.6  mycroft 			for (j = 0; j < nsops; j++)
    794  1.6  mycroft 				semaptr->sem_base[sops[j].sem_num].semval -=
    795  1.6  mycroft 				    sops[j].sem_op;
    796  1.1      cgd 
    797  1.1      cgd #ifdef SEM_DEBUG
    798  1.6  mycroft 			printf("eval = %d from semundo_adjust\n", eval);
    799  1.1      cgd #endif
    800  1.6  mycroft 			return(eval);
    801  1.1      cgd 		} /* loop through the sops */
    802  1.6  mycroft 	} /* if (do_undos) */
    803  1.1      cgd 
    804  1.6  mycroft 	/* We're definitely done - set the sempid's */
    805  1.6  mycroft 	for (i = 0; i < nsops; i++) {
    806  1.1      cgd 		sopptr = &sops[i];
    807  1.1      cgd 		semptr = &semaptr->sem_base[sopptr->sem_num];
    808  1.1      cgd 		semptr->sempid = p->p_pid;
    809  1.6  mycroft 	}
    810  1.1      cgd 
    811  1.6  mycroft 	/* Do a wakeup if any semaphore was up'd. */
    812  1.6  mycroft 	if (do_wakeup) {
    813  1.1      cgd #ifdef SEM_DEBUG
    814  1.1      cgd 		printf("semop:  doing wakeup\n");
    815  1.1      cgd #ifdef SEM_WAKEUP
    816  1.4  mycroft 		sem_wakeup((caddr_t)semaptr);
    817  1.1      cgd #else
    818  1.4  mycroft 		wakeup((caddr_t)semaptr);
    819  1.1      cgd #endif
    820  1.1      cgd 		printf("semop:  back from wakeup\n");
    821  1.1      cgd #else
    822  1.4  mycroft 		wakeup((caddr_t)semaptr);
    823  1.1      cgd #endif
    824  1.6  mycroft 	}
    825  1.1      cgd #ifdef SEM_DEBUG
    826  1.6  mycroft 	printf("semop:  done\n");
    827  1.1      cgd #endif
    828  1.6  mycroft 	*retval = 0;
    829  1.6  mycroft 	return(0);
    830  1.1      cgd }
    831  1.1      cgd 
    832  1.1      cgd /*
    833  1.6  mycroft  * Go through the undo structures for this process and apply the adjustments to
    834  1.6  mycroft  * semaphores.
    835  1.1      cgd  */
    836  1.1      cgd semexit(p)
    837  1.6  mycroft 	struct proc *p;
    838  1.1      cgd {
    839  1.6  mycroft 	register struct sem_undo *suptr;
    840  1.6  mycroft 	register struct sem_undo **supptr;
    841  1.6  mycroft 	int did_something;
    842  1.1      cgd 
    843  1.6  mycroft 	/*
    844  1.6  mycroft 	 * If somebody else is holding the global semaphore facility lock
    845  1.6  mycroft 	 * then sleep until it is released.
    846  1.6  mycroft 	 */
    847  1.6  mycroft 	while (semlock_holder != NULL && semlock_holder != p) {
    848  1.1      cgd #ifdef SEM_DEBUG
    849  1.6  mycroft 		printf("semaphore facility locked - sleeping ...\n");
    850  1.1      cgd #endif
    851  1.6  mycroft 		sleep((caddr_t)&semlock_holder, (PZERO - 4));
    852  1.6  mycroft 	}
    853  1.1      cgd 
    854  1.6  mycroft 	did_something = 0;
    855  1.1      cgd 
    856  1.6  mycroft 	/*
    857  1.6  mycroft 	 * Go through the chain of undo vectors looking for one
    858  1.6  mycroft 	 * associated with this process.
    859  1.6  mycroft 	 */
    860  1.6  mycroft 
    861  1.6  mycroft 	for (supptr = &semu_list; (suptr = *supptr) != NULL;
    862  1.6  mycroft 	    supptr = &suptr->un_next) {
    863  1.6  mycroft 		if (suptr->un_proc == p)
    864  1.6  mycroft 			break;
    865  1.6  mycroft 	}
    866  1.1      cgd 
    867  1.6  mycroft 	if (suptr == NULL)
    868  1.6  mycroft 		goto unlock;
    869  1.1      cgd 
    870  1.1      cgd #ifdef SEM_DEBUG
    871  1.6  mycroft 	printf("proc @%08x has undo structure with %d entries\n", p,
    872  1.6  mycroft 	    suptr->un_cnt);
    873  1.1      cgd #endif
    874  1.1      cgd 
    875  1.5  mycroft 	/*
    876  1.5  mycroft 	 * If there are any active undo elements then process them.
    877  1.5  mycroft 	 */
    878  1.5  mycroft 	if (suptr->un_cnt > 0) {
    879  1.6  mycroft 		int ix;
    880  1.1      cgd 
    881  1.6  mycroft 		for (ix = 0; ix < suptr->un_cnt; ix++) {
    882  1.6  mycroft 			int semid = suptr->un_ent[ix].un_id;
    883  1.6  mycroft 			int semnum = suptr->un_ent[ix].un_num;
    884  1.6  mycroft 			int adjval = suptr->un_ent[ix].un_adjval;
    885  1.6  mycroft 			struct semid_ds *semaptr;
    886  1.6  mycroft 
    887  1.6  mycroft 			semaptr = &sema[semid];
    888  1.6  mycroft 			if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
    889  1.6  mycroft 				panic("semexit - semid not allocated");
    890  1.6  mycroft 			if (semnum >= semaptr->sem_nsems)
    891  1.6  mycroft 				panic("semexit - semnum out of range");
    892  1.6  mycroft 
    893  1.6  mycroft #ifdef SEM_DEBUG
    894  1.6  mycroft 			printf("semexit:  %08x id=%d num=%d(adj=%d) ; sem=%d\n",
    895  1.6  mycroft 			    suptr->un_proc, suptr->un_ent[ix].un_id,
    896  1.6  mycroft 			    suptr->un_ent[ix].un_num,
    897  1.6  mycroft 			    suptr->un_ent[ix].un_adjval,
    898  1.6  mycroft 			    semaptr->sem_base[semnum].semval);
    899  1.6  mycroft #endif
    900  1.6  mycroft 
    901  1.6  mycroft 			if (adjval < 0) {
    902  1.6  mycroft 				if (semaptr->sem_base[semnum].semval < -adjval)
    903  1.6  mycroft 					semaptr->sem_base[semnum].semval = 0;
    904  1.6  mycroft 				else
    905  1.6  mycroft 					semaptr->sem_base[semnum].semval +=
    906  1.6  mycroft 					    adjval;
    907  1.6  mycroft 			} else
    908  1.6  mycroft 				semaptr->sem_base[semnum].semval += adjval;
    909  1.1      cgd 
    910  1.1      cgd #ifdef SEM_WAKEUP
    911  1.6  mycroft 			sem_wakeup((caddr_t)semaptr);
    912  1.1      cgd #else
    913  1.6  mycroft 			wakeup((caddr_t)semaptr);
    914  1.1      cgd #endif
    915  1.1      cgd #ifdef SEM_DEBUG
    916  1.6  mycroft 			printf("semexit:  back from wakeup\n");
    917  1.1      cgd #endif
    918  1.6  mycroft 		}
    919  1.5  mycroft 	}
    920  1.1      cgd 
    921  1.5  mycroft 	/*
    922  1.5  mycroft 	 * Deallocate the undo vector.
    923  1.5  mycroft 	 */
    924  1.1      cgd #ifdef SEM_DEBUG
    925  1.5  mycroft 	printf("removing vector\n");
    926  1.1      cgd #endif
    927  1.5  mycroft 	suptr->un_proc = NULL;
    928  1.5  mycroft 	*supptr = suptr->un_next;
    929  1.1      cgd 
    930  1.6  mycroft unlock:
    931  1.6  mycroft 	/*
    932  1.6  mycroft 	 * If the exiting process is holding the global semaphore facility
    933  1.6  mycroft 	 * lock then release it.
    934  1.6  mycroft 	 */
    935  1.6  mycroft 	if (semlock_holder == p) {
    936  1.6  mycroft 		semlock_holder = NULL;
    937  1.6  mycroft 		wakeup((caddr_t)&semlock_holder);
    938  1.6  mycroft 	}
    939  1.1      cgd }
    940