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