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