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