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