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