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