Home | History | Annotate | Line # | Download | only in kern
sysv_sem.c revision 1.37
      1  1.37  sommerfe /*	$NetBSD: sysv_sem.c,v 1.37 2000/05/27 21:00:25 sommerfeld Exp $	*/
      2  1.33   thorpej 
      3  1.33   thorpej /*-
      4  1.33   thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.33   thorpej  * All rights reserved.
      6  1.33   thorpej  *
      7  1.33   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.33   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.33   thorpej  * NASA Ames Research Center.
     10  1.33   thorpej  *
     11  1.33   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.33   thorpej  * modification, are permitted provided that the following conditions
     13  1.33   thorpej  * are met:
     14  1.33   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.33   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.33   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.33   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.33   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.33   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.33   thorpej  *    must display the following acknowledgement:
     21  1.33   thorpej  *	This product includes software developed by the NetBSD
     22  1.33   thorpej  *	Foundation, Inc. and its contributors.
     23  1.33   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.33   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.33   thorpej  *    from this software without specific prior written permission.
     26  1.33   thorpej  *
     27  1.33   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.33   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.33   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.33   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.33   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.33   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.33   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.33   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.33   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.33   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.33   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.33   thorpej  */
     39   1.9       cgd 
     40   1.1       cgd /*
     41   1.1       cgd  * Implementation of SVID semaphores
     42   1.1       cgd  *
     43  1.33   thorpej  * Author: Daniel Boulet
     44   1.1       cgd  *
     45   1.1       cgd  * This software is provided ``AS IS'' without any warranties of any kind.
     46   1.1       cgd  */
     47  1.31      tron 
     48  1.32      tron #define SYSVSEM
     49   1.1       cgd 
     50   1.3   mycroft #include <sys/param.h>
     51   1.3   mycroft #include <sys/systm.h>
     52   1.3   mycroft #include <sys/kernel.h>
     53   1.3   mycroft #include <sys/proc.h>
     54   1.3   mycroft #include <sys/sem.h>
     55   1.3   mycroft #include <sys/malloc.h>
     56   1.1       cgd 
     57  1.10       cgd #include <sys/mount.h>
     58  1.10       cgd #include <sys/syscallargs.h>
     59  1.25  christos 
     60   1.1       cgd int	semtot = 0;
     61   1.1       cgd 
     62  1.27  christos #ifdef SEM_DEBUG
     63  1.28  christos #define SEM_PRINTF(a) printf a
     64  1.27  christos #else
     65  1.27  christos #define SEM_PRINTF(a)
     66  1.27  christos #endif
     67  1.27  christos 
     68  1.25  christos struct sem_undo *semu_alloc __P((struct proc *));
     69  1.25  christos int semundo_adjust __P((struct proc *, struct sem_undo **, int, int, int));
     70  1.25  christos void semundo_clear __P((int, int));
     71  1.25  christos 
     72  1.37  sommerfe /*
     73  1.37  sommerfe  * XXXSMP Once we go MP, there needs to be a lock for the semaphore system.
     74  1.37  sommerfe  * Until then, we're saved by being a non-preemptive kernel.
     75  1.37  sommerfe  */
     76  1.37  sommerfe 
     77  1.25  christos void
     78   1.1       cgd seminit()
     79   1.1       cgd {
     80  1.35  augustss 	int i;
     81   1.1       cgd 
     82   1.5   mycroft 	if (sema == NULL)
     83   1.5   mycroft 		panic("sema is NULL");
     84   1.5   mycroft 	if (semu == NULL)
     85   1.5   mycroft 		panic("semu is NULL");
     86   1.5   mycroft 
     87   1.5   mycroft 	for (i = 0; i < seminfo.semmni; i++) {
     88  1.33   thorpej 		sema[i]._sem_base = 0;
     89   1.5   mycroft 		sema[i].sem_perm.mode = 0;
     90   1.5   mycroft 	}
     91   1.5   mycroft 	for (i = 0; i < seminfo.semmnu; i++) {
     92  1.35  augustss 		struct sem_undo *suptr = SEMU(i);
     93   1.5   mycroft 		suptr->un_proc = NULL;
     94   1.5   mycroft 	}
     95   1.5   mycroft 	semu_list = NULL;
     96   1.1       cgd }
     97   1.1       cgd 
     98   1.1       cgd /*
     99  1.37  sommerfe  * Placebo.
    100   1.1       cgd  */
    101   1.1       cgd 
    102   1.1       cgd int
    103  1.24   mycroft sys_semconfig(p, v, retval)
    104   1.1       cgd 	struct proc *p;
    105  1.23   thorpej 	void *v;
    106  1.23   thorpej 	register_t *retval;
    107  1.23   thorpej {
    108   1.5   mycroft 	*retval = 0;
    109  1.37  sommerfe 	return 0;
    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.5   mycroft semu_alloc(p)
    119   1.5   mycroft 	struct proc *p;
    120   1.1       cgd {
    121  1.35  augustss 	int i;
    122  1.35  augustss 	struct sem_undo *suptr;
    123  1.35  augustss 	struct sem_undo **supptr;
    124   1.5   mycroft 	int attempt;
    125   1.1       cgd 
    126   1.1       cgd 	/*
    127   1.5   mycroft 	 * Try twice to allocate something.
    128   1.5   mycroft 	 * (we'll purge any empty structures after the first pass so
    129   1.5   mycroft 	 * two passes are always enough)
    130   1.1       cgd 	 */
    131   1.1       cgd 
    132   1.5   mycroft 	for (attempt = 0; attempt < 2; attempt++) {
    133   1.5   mycroft 		/*
    134   1.5   mycroft 		 * Look for a free structure.
    135   1.5   mycroft 		 * Fill it in and return it if we find one.
    136   1.5   mycroft 		 */
    137   1.5   mycroft 
    138   1.5   mycroft 		for (i = 0; i < seminfo.semmnu; i++) {
    139   1.5   mycroft 			suptr = SEMU(i);
    140   1.5   mycroft 			if (suptr->un_proc == NULL) {
    141   1.5   mycroft 				suptr->un_next = semu_list;
    142   1.5   mycroft 				semu_list = suptr;
    143   1.5   mycroft 				suptr->un_cnt = 0;
    144   1.5   mycroft 				suptr->un_proc = p;
    145   1.5   mycroft 				return(suptr);
    146   1.5   mycroft 			}
    147   1.5   mycroft 		}
    148   1.1       cgd 
    149   1.5   mycroft 		/*
    150   1.5   mycroft 		 * We didn't find a free one, if this is the first attempt
    151   1.5   mycroft 		 * then try to free some structures.
    152   1.5   mycroft 		 */
    153   1.5   mycroft 
    154   1.5   mycroft 		if (attempt == 0) {
    155   1.5   mycroft 			/* All the structures are in use - try to free some */
    156   1.5   mycroft 			int did_something = 0;
    157   1.5   mycroft 
    158   1.5   mycroft 			supptr = &semu_list;
    159   1.5   mycroft 			while ((suptr = *supptr) != NULL) {
    160   1.5   mycroft 				if (suptr->un_cnt == 0)  {
    161   1.5   mycroft 					suptr->un_proc = NULL;
    162   1.5   mycroft 					*supptr = suptr->un_next;
    163   1.5   mycroft 					did_something = 1;
    164   1.5   mycroft 				} else
    165   1.5   mycroft 					supptr = &(suptr->un_next);
    166   1.5   mycroft 			}
    167   1.5   mycroft 
    168   1.5   mycroft 			/* If we didn't free anything then just give-up */
    169   1.5   mycroft 			if (!did_something)
    170   1.5   mycroft 				return(NULL);
    171   1.5   mycroft 		} else {
    172   1.5   mycroft 			/*
    173   1.5   mycroft 			 * The second pass failed even though we freed
    174   1.5   mycroft 			 * something after the first pass!
    175   1.5   mycroft 			 * This is IMPOSSIBLE!
    176   1.5   mycroft 			 */
    177   1.5   mycroft 			panic("semu_alloc - second attempt failed");
    178   1.5   mycroft 		}
    179   1.1       cgd 	}
    180  1.25  christos 	return NULL;
    181   1.1       cgd }
    182   1.1       cgd 
    183   1.1       cgd /*
    184   1.1       cgd  * Adjust a particular entry for a particular proc
    185   1.1       cgd  */
    186   1.1       cgd 
    187   1.1       cgd int
    188   1.5   mycroft semundo_adjust(p, supptr, semid, semnum, adjval)
    189  1.35  augustss 	struct proc *p;
    190   1.5   mycroft 	struct sem_undo **supptr;
    191   1.5   mycroft 	int semid, semnum;
    192   1.5   mycroft 	int adjval;
    193   1.1       cgd {
    194  1.35  augustss 	struct sem_undo *suptr;
    195  1.35  augustss 	struct undo *sunptr;
    196   1.5   mycroft 	int i;
    197   1.1       cgd 
    198   1.5   mycroft 	/* Look for and remember the sem_undo if the caller doesn't provide
    199   1.5   mycroft 	   it */
    200   1.1       cgd 
    201   1.5   mycroft 	suptr = *supptr;
    202   1.4   mycroft 	if (suptr == NULL) {
    203  1.11   mycroft 		for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
    204   1.5   mycroft 			if (suptr->un_proc == p) {
    205   1.5   mycroft 				*supptr = suptr;
    206   1.5   mycroft 				break;
    207   1.5   mycroft 			}
    208   1.5   mycroft 		}
    209   1.5   mycroft 		if (suptr == NULL) {
    210   1.5   mycroft 			if (adjval == 0)
    211   1.5   mycroft 				return(0);
    212   1.5   mycroft 			suptr = semu_alloc(p);
    213   1.5   mycroft 			if (suptr == NULL)
    214   1.5   mycroft 				return(ENOSPC);
    215   1.5   mycroft 			*supptr = suptr;
    216   1.5   mycroft 		}
    217   1.1       cgd 	}
    218   1.1       cgd 
    219   1.6   mycroft 	/*
    220   1.6   mycroft 	 * Look for the requested entry and adjust it (delete if adjval becomes
    221   1.6   mycroft 	 * 0).
    222   1.6   mycroft 	 */
    223   1.6   mycroft 	sunptr = &suptr->un_ent[0];
    224   1.5   mycroft 	for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
    225   1.6   mycroft 		if (sunptr->un_id != semid || sunptr->un_num != semnum)
    226   1.6   mycroft 			continue;
    227   1.6   mycroft 		if (adjval == 0)
    228   1.6   mycroft 			sunptr->un_adjval = 0;
    229   1.6   mycroft 		else
    230   1.6   mycroft 			sunptr->un_adjval += adjval;
    231   1.6   mycroft 		if (sunptr->un_adjval == 0) {
    232   1.6   mycroft 			suptr->un_cnt--;
    233   1.6   mycroft 			if (i < suptr->un_cnt)
    234   1.6   mycroft 				suptr->un_ent[i] =
    235   1.6   mycroft 				    suptr->un_ent[suptr->un_cnt];
    236   1.5   mycroft 		}
    237   1.6   mycroft 		return(0);
    238   1.1       cgd 	}
    239   1.1       cgd 
    240   1.5   mycroft 	/* Didn't find the right entry - create it */
    241   1.5   mycroft 	if (adjval == 0)
    242   1.5   mycroft 		return(0);
    243  1.11   mycroft 	if (suptr->un_cnt == SEMUME)
    244   1.5   mycroft 		return(EINVAL);
    245  1.11   mycroft 
    246  1.11   mycroft 	sunptr = &suptr->un_ent[suptr->un_cnt];
    247  1.11   mycroft 	suptr->un_cnt++;
    248  1.11   mycroft 	sunptr->un_adjval = adjval;
    249  1.11   mycroft 	sunptr->un_id = semid;
    250  1.11   mycroft 	sunptr->un_num = semnum;
    251   1.1       cgd 	return(0);
    252   1.1       cgd }
    253   1.1       cgd 
    254   1.1       cgd void
    255   1.5   mycroft semundo_clear(semid, semnum)
    256   1.5   mycroft 	int semid, semnum;
    257   1.1       cgd {
    258  1.35  augustss 	struct sem_undo *suptr;
    259   1.1       cgd 
    260   1.6   mycroft 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
    261  1.35  augustss 		struct undo *sunptr;
    262  1.35  augustss 		int i;
    263   1.6   mycroft 
    264  1.19   mycroft 		sunptr = &suptr->un_ent[0];
    265  1.19   mycroft 		for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
    266   1.6   mycroft 			if (sunptr->un_id == semid) {
    267   1.6   mycroft 				if (semnum == -1 || sunptr->un_num == semnum) {
    268   1.6   mycroft 					suptr->un_cnt--;
    269   1.6   mycroft 					if (i < suptr->un_cnt) {
    270   1.6   mycroft 						suptr->un_ent[i] =
    271   1.6   mycroft 						  suptr->un_ent[suptr->un_cnt];
    272  1.19   mycroft 						i--, sunptr--;
    273   1.6   mycroft 					}
    274   1.6   mycroft 				}
    275   1.6   mycroft 				if (semnum != -1)
    276   1.6   mycroft 					break;
    277   1.6   mycroft 			}
    278   1.6   mycroft 		}
    279   1.1       cgd 	}
    280   1.1       cgd }
    281   1.1       cgd 
    282   1.1       cgd int
    283  1.34  christos sys_____semctl13(p, v, retval)
    284   1.1       cgd 	struct proc *p;
    285  1.33   thorpej 	void *v;
    286  1.23   thorpej 	register_t *retval;
    287  1.23   thorpej {
    288  1.34  christos 	struct sys_____semctl13_args /* {
    289  1.10       cgd 		syscallarg(int) semid;
    290  1.10       cgd 		syscallarg(int) semnum;
    291  1.10       cgd 		syscallarg(int) cmd;
    292  1.34  christos 		syscallarg(union __semun *) arg;
    293  1.23   thorpej 	} */ *uap = v;
    294  1.33   thorpej 	struct semid_ds sembuf;
    295  1.33   thorpej 	int cmd, error;
    296  1.34  christos 	void *pass_arg;
    297  1.34  christos 	union __semun karg;
    298  1.33   thorpej 
    299  1.33   thorpej 	cmd = SCARG(uap, cmd);
    300  1.33   thorpej 
    301  1.33   thorpej 	switch (cmd) {
    302  1.33   thorpej 	case IPC_SET:
    303  1.33   thorpej 	case IPC_STAT:
    304  1.33   thorpej 		pass_arg = &sembuf;
    305  1.33   thorpej 		break;
    306  1.33   thorpej 
    307  1.33   thorpej 	case GETALL:
    308  1.33   thorpej 	case SETVAL:
    309  1.33   thorpej 	case SETALL:
    310  1.34  christos 		pass_arg = &karg;
    311  1.34  christos 		break;
    312  1.34  christos 	default:
    313  1.34  christos 		pass_arg = NULL;
    314  1.33   thorpej 		break;
    315  1.33   thorpej 	}
    316  1.33   thorpej 
    317  1.34  christos 	if (pass_arg) {
    318  1.34  christos 		error = copyin(SCARG(uap, arg), &karg, sizeof(karg));
    319  1.33   thorpej 		if (error)
    320  1.34  christos 			return error;
    321  1.34  christos 		if (cmd == IPC_SET) {
    322  1.34  christos 			error = copyin(karg.buf, &sembuf, sizeof(sembuf));
    323  1.34  christos 			if (error)
    324  1.34  christos 				return (error);
    325  1.34  christos 		}
    326  1.33   thorpej 	}
    327  1.33   thorpej 
    328  1.33   thorpej 	error = semctl1(p, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    329  1.33   thorpej 	    pass_arg, retval);
    330  1.33   thorpej 
    331  1.33   thorpej 	if (error == 0 && cmd == IPC_STAT)
    332  1.34  christos 		error = copyout(&sembuf, karg.buf, sizeof(sembuf));
    333  1.33   thorpej 
    334  1.33   thorpej 	return (error);
    335  1.33   thorpej }
    336  1.33   thorpej 
    337  1.33   thorpej int
    338  1.33   thorpej semctl1(p, semid, semnum, cmd, v, retval)
    339  1.33   thorpej 	struct proc *p;
    340  1.33   thorpej 	int semid, semnum, cmd;
    341  1.33   thorpej 	void *v;
    342  1.33   thorpej 	register_t *retval;
    343  1.33   thorpej {
    344   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    345  1.33   thorpej 	union __semun *arg = v;
    346  1.33   thorpej 	struct semid_ds *sembuf = v, *semaptr;
    347  1.33   thorpej 	int i, error, ix;
    348   1.1       cgd 
    349  1.27  christos 	SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
    350  1.33   thorpej 	    semid, semnum, cmd, v));
    351   1.1       cgd 
    352  1.33   thorpej 	ix = IPCID_TO_IX(semid);
    353  1.33   thorpej 	if (ix < 0 || ix >= seminfo.semmsl)
    354  1.33   thorpej 		return (EINVAL);
    355   1.6   mycroft 
    356  1.33   thorpej 	semaptr = &sema[ix];
    357   1.6   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    358  1.33   thorpej 	    semaptr->sem_perm._seq != IPCID_TO_SEQ(semid))
    359  1.33   thorpej 		return (EINVAL);
    360   1.1       cgd 
    361   1.6   mycroft 	switch (cmd) {
    362   1.6   mycroft 	case IPC_RMID:
    363  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
    364  1.33   thorpej 			return (error);
    365   1.6   mycroft 		semaptr->sem_perm.cuid = cred->cr_uid;
    366   1.6   mycroft 		semaptr->sem_perm.uid = cred->cr_uid;
    367   1.6   mycroft 		semtot -= semaptr->sem_nsems;
    368  1.33   thorpej 		for (i = semaptr->_sem_base - sem; i < semtot; i++)
    369   1.6   mycroft 			sem[i] = sem[i + semaptr->sem_nsems];
    370   1.6   mycroft 		for (i = 0; i < seminfo.semmni; i++) {
    371   1.6   mycroft 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
    372  1.33   thorpej 			    sema[i]._sem_base > semaptr->_sem_base)
    373  1.33   thorpej 				sema[i]._sem_base -= semaptr->sem_nsems;
    374   1.6   mycroft 		}
    375   1.6   mycroft 		semaptr->sem_perm.mode = 0;
    376  1.33   thorpej 		semundo_clear(ix, -1);
    377  1.33   thorpej 		wakeup(semaptr);
    378   1.6   mycroft 		break;
    379   1.1       cgd 
    380   1.6   mycroft 	case IPC_SET:
    381  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
    382  1.33   thorpej 			return (error);
    383  1.33   thorpej 		semaptr->sem_perm.uid = sembuf->sem_perm.uid;
    384  1.33   thorpej 		semaptr->sem_perm.gid = sembuf->sem_perm.gid;
    385   1.6   mycroft 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
    386  1.33   thorpej 		    (sembuf->sem_perm.mode & 0777);
    387   1.6   mycroft 		semaptr->sem_ctime = time.tv_sec;
    388   1.6   mycroft 		break;
    389   1.1       cgd 
    390   1.6   mycroft 	case IPC_STAT:
    391  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    392  1.33   thorpej 			return (error);
    393  1.33   thorpej 		memcpy(sembuf, semaptr, sizeof(struct semid_ds));
    394   1.6   mycroft 		break;
    395   1.1       cgd 
    396   1.6   mycroft 	case GETNCNT:
    397  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    398  1.33   thorpej 			return (error);
    399   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    400  1.33   thorpej 			return (EINVAL);
    401  1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semncnt;
    402   1.6   mycroft 		break;
    403   1.1       cgd 
    404   1.6   mycroft 	case GETPID:
    405  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    406  1.33   thorpej 			return (error);
    407   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    408  1.33   thorpej 			return (EINVAL);
    409  1.33   thorpej 		*retval = semaptr->_sem_base[semnum].sempid;
    410   1.6   mycroft 		break;
    411   1.1       cgd 
    412   1.6   mycroft 	case GETVAL:
    413  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    414  1.33   thorpej 			return (error);
    415   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    416  1.33   thorpej 			return (EINVAL);
    417  1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semval;
    418   1.6   mycroft 		break;
    419   1.1       cgd 
    420   1.6   mycroft 	case GETALL:
    421  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    422  1.33   thorpej 			return (error);
    423   1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    424  1.33   thorpej 			error = copyout(&semaptr->_sem_base[i].semval,
    425  1.33   thorpej 			    &arg->array[i], sizeof(arg->array[i]));
    426  1.33   thorpej 			if (error != 0)
    427   1.6   mycroft 				break;
    428   1.6   mycroft 		}
    429   1.6   mycroft 		break;
    430   1.1       cgd 
    431   1.6   mycroft 	case GETZCNT:
    432  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    433  1.33   thorpej 			return (error);
    434   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    435  1.33   thorpej 			return (EINVAL);
    436  1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semzcnt;
    437   1.6   mycroft 		break;
    438   1.1       cgd 
    439   1.6   mycroft 	case SETVAL:
    440  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    441  1.33   thorpej 			return (error);
    442   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    443  1.33   thorpej 			return (EINVAL);
    444  1.33   thorpej 		semaptr->_sem_base[semnum].semval = arg->val;
    445  1.33   thorpej 		semundo_clear(ix, semnum);
    446  1.33   thorpej 		wakeup(semaptr);
    447   1.6   mycroft 		break;
    448   1.1       cgd 
    449   1.6   mycroft 	case SETALL:
    450  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    451  1.33   thorpej 			return (error);
    452   1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    453  1.33   thorpej 			error = copyin(&arg->array[i],
    454  1.33   thorpej 			    &semaptr->_sem_base[i].semval,
    455  1.33   thorpej 			    sizeof(arg->array[i]));
    456  1.33   thorpej 			if (error != 0)
    457   1.6   mycroft 				break;
    458   1.6   mycroft 		}
    459  1.33   thorpej 		semundo_clear(ix, -1);
    460  1.33   thorpej 		wakeup(semaptr);
    461   1.6   mycroft 		break;
    462   1.1       cgd 
    463   1.6   mycroft 	default:
    464  1.33   thorpej 		return (EINVAL);
    465   1.6   mycroft 	}
    466   1.4   mycroft 
    467  1.33   thorpej 	return (error);
    468   1.1       cgd }
    469   1.1       cgd 
    470   1.1       cgd int
    471  1.24   mycroft sys_semget(p, v, retval)
    472   1.1       cgd 	struct proc *p;
    473  1.23   thorpej 	void *v;
    474  1.23   thorpej 	register_t *retval;
    475  1.23   thorpej {
    476  1.35  augustss 	struct sys_semget_args /* {
    477  1.10       cgd 		syscallarg(key_t) key;
    478  1.10       cgd 		syscallarg(int) nsems;
    479  1.10       cgd 		syscallarg(int) semflg;
    480  1.23   thorpej 	} */ *uap = v;
    481   1.6   mycroft 	int semid, eval;
    482  1.10       cgd 	int key = SCARG(uap, key);
    483  1.10       cgd 	int nsems = SCARG(uap, nsems);
    484  1.10       cgd 	int semflg = SCARG(uap, semflg);
    485   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    486   1.1       cgd 
    487  1.27  christos 	SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
    488   1.1       cgd 
    489   1.6   mycroft 	if (key != IPC_PRIVATE) {
    490   1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    491   1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
    492  1.33   thorpej 			    sema[semid].sem_perm._key == key)
    493   1.6   mycroft 				break;
    494   1.6   mycroft 		}
    495   1.6   mycroft 		if (semid < seminfo.semmni) {
    496  1.27  christos 			SEM_PRINTF(("found public key\n"));
    497   1.7   hpeyerl 			if ((eval = ipcperm(cred, &sema[semid].sem_perm,
    498   1.7   hpeyerl 			    semflg & 0700)))
    499   1.6   mycroft 				return(eval);
    500   1.6   mycroft 			if (nsems > 0 && sema[semid].sem_nsems < nsems) {
    501  1.27  christos 				SEM_PRINTF(("too small\n"));
    502   1.6   mycroft 				return(EINVAL);
    503   1.6   mycroft 			}
    504   1.6   mycroft 			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
    505  1.27  christos 				SEM_PRINTF(("not exclusive\n"));
    506   1.6   mycroft 				return(EEXIST);
    507   1.6   mycroft 			}
    508   1.6   mycroft 			goto found;
    509   1.6   mycroft 		}
    510   1.6   mycroft 	}
    511   1.6   mycroft 
    512  1.27  christos 	SEM_PRINTF(("need to allocate the semid_ds\n"));
    513   1.6   mycroft 	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
    514   1.6   mycroft 		if (nsems <= 0 || nsems > seminfo.semmsl) {
    515  1.27  christos 			SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
    516  1.27  christos 			    seminfo.semmsl));
    517   1.6   mycroft 			return(EINVAL);
    518   1.6   mycroft 		}
    519   1.6   mycroft 		if (nsems > seminfo.semmns - semtot) {
    520  1.27  christos 			SEM_PRINTF(("not enough semaphores left (need %d, got %d)\n",
    521  1.27  christos 			    nsems, seminfo.semmns - semtot));
    522   1.6   mycroft 			return(ENOSPC);
    523   1.6   mycroft 		}
    524   1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    525   1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
    526   1.6   mycroft 				break;
    527   1.6   mycroft 		}
    528   1.6   mycroft 		if (semid == seminfo.semmni) {
    529  1.27  christos 			SEM_PRINTF(("no more semid_ds's available\n"));
    530   1.6   mycroft 			return(ENOSPC);
    531   1.6   mycroft 		}
    532  1.27  christos 		SEM_PRINTF(("semid %d is available\n", semid));
    533  1.33   thorpej 		sema[semid].sem_perm._key = key;
    534   1.6   mycroft 		sema[semid].sem_perm.cuid = cred->cr_uid;
    535   1.6   mycroft 		sema[semid].sem_perm.uid = cred->cr_uid;
    536   1.6   mycroft 		sema[semid].sem_perm.cgid = cred->cr_gid;
    537   1.6   mycroft 		sema[semid].sem_perm.gid = cred->cr_gid;
    538   1.6   mycroft 		sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
    539  1.33   thorpej 		sema[semid].sem_perm._seq =
    540  1.33   thorpej 		    (sema[semid].sem_perm._seq + 1) & 0x7fff;
    541   1.6   mycroft 		sema[semid].sem_nsems = nsems;
    542   1.6   mycroft 		sema[semid].sem_otime = 0;
    543   1.6   mycroft 		sema[semid].sem_ctime = time.tv_sec;
    544  1.33   thorpej 		sema[semid]._sem_base = &sem[semtot];
    545   1.6   mycroft 		semtot += nsems;
    546  1.33   thorpej 		memset(sema[semid]._sem_base, 0,
    547  1.33   thorpej 		    sizeof(sema[semid]._sem_base[0])*nsems);
    548  1.33   thorpej 		SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid]._sem_base,
    549  1.27  christos 		    &sem[semtot]));
    550   1.1       cgd 	} else {
    551  1.27  christos 		SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
    552   1.6   mycroft 		return(ENOENT);
    553   1.1       cgd 	}
    554   1.1       cgd 
    555   1.6   mycroft found:
    556   1.6   mycroft 	*retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
    557   1.6   mycroft 	return(0);
    558   1.1       cgd }
    559   1.1       cgd 
    560   1.1       cgd int
    561  1.24   mycroft sys_semop(p, v, retval)
    562   1.1       cgd 	struct proc *p;
    563  1.23   thorpej 	void *v;
    564  1.23   thorpej 	register_t *retval;
    565  1.23   thorpej {
    566  1.35  augustss 	struct sys_semop_args /* {
    567  1.10       cgd 		syscallarg(int) semid;
    568  1.10       cgd 		syscallarg(struct sembuf *) sops;
    569  1.29    kleink 		syscallarg(size_t) nsops;
    570  1.23   thorpej 	} */ *uap = v;
    571  1.10       cgd 	int semid = SCARG(uap, semid);
    572  1.10       cgd 	int nsops = SCARG(uap, nsops);
    573   1.6   mycroft 	struct sembuf sops[MAX_SOPS];
    574  1.35  augustss 	struct semid_ds *semaptr;
    575  1.35  augustss 	struct sembuf *sopptr = NULL;
    576  1.35  augustss 	struct __sem *semptr = NULL;
    577   1.6   mycroft 	struct sem_undo *suptr = NULL;
    578   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    579   1.6   mycroft 	int i, j, eval;
    580  1.25  christos 	int do_wakeup, do_undos;
    581   1.1       cgd 
    582  1.27  christos 	SEM_PRINTF(("call to semop(%d, %p, %d)\n", semid, sops, nsops));
    583   1.1       cgd 
    584   1.6   mycroft 	semid = IPCID_TO_IX(semid);	/* Convert back to zero origin */
    585   1.6   mycroft 
    586   1.6   mycroft 	if (semid < 0 || semid >= seminfo.semmsl)
    587   1.6   mycroft 		return(EINVAL);
    588   1.6   mycroft 
    589   1.6   mycroft 	semaptr = &sema[semid];
    590  1.11   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    591  1.33   thorpej 	    semaptr->sem_perm._seq != IPCID_TO_SEQ(SCARG(uap, semid)))
    592   1.6   mycroft 		return(EINVAL);
    593   1.6   mycroft 
    594   1.7   hpeyerl 	if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
    595  1.27  christos 		SEM_PRINTF(("eval = %d from ipaccess\n", eval));
    596   1.6   mycroft 		return(eval);
    597   1.6   mycroft 	}
    598   1.1       cgd 
    599   1.6   mycroft 	if (nsops > MAX_SOPS) {
    600  1.27  christos 		SEM_PRINTF(("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops));
    601   1.6   mycroft 		return(E2BIG);
    602   1.6   mycroft 	}
    603   1.1       cgd 
    604  1.10       cgd 	if ((eval = copyin(SCARG(uap, sops), sops, nsops * sizeof(sops[0])))
    605  1.10       cgd 	    != 0) {
    606  1.27  christos 		SEM_PRINTF(("eval = %d from copyin(%p, %p, %d)\n", eval,
    607  1.27  christos 		    SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
    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.33   thorpej 			semptr = &semaptr->_sem_base[sopptr->sem_num];
    632   1.1       cgd 
    633  1.27  christos 			SEM_PRINTF(("semop:  semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
    634  1.33   thorpej 			    semaptr, semaptr->_sem_base, semptr,
    635   1.6   mycroft 			    sopptr->sem_num, semptr->semval, sopptr->sem_op,
    636  1.27  christos 			    (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait"));
    637   1.1       cgd 
    638   1.6   mycroft 			if (sopptr->sem_op < 0) {
    639  1.25  christos 				if ((int)(semptr->semval +
    640  1.25  christos 					  sopptr->sem_op) < 0) {
    641  1.27  christos 					SEM_PRINTF(("semop:  can't do it now\n"));
    642   1.6   mycroft 					break;
    643   1.6   mycroft 				} else {
    644   1.6   mycroft 					semptr->semval += sopptr->sem_op;
    645   1.6   mycroft 					if (semptr->semval == 0 &&
    646   1.6   mycroft 					    semptr->semzcnt > 0)
    647   1.6   mycroft 						do_wakeup = 1;
    648   1.6   mycroft 				}
    649   1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    650   1.6   mycroft 					do_undos = 1;
    651   1.6   mycroft 			} else if (sopptr->sem_op == 0) {
    652   1.6   mycroft 				if (semptr->semval > 0) {
    653  1.27  christos 					SEM_PRINTF(("semop:  not zero now\n"));
    654   1.6   mycroft 					break;
    655   1.6   mycroft 				}
    656   1.6   mycroft 			} else {
    657   1.6   mycroft 				if (semptr->semncnt > 0)
    658   1.6   mycroft 					do_wakeup = 1;
    659   1.6   mycroft 				semptr->semval += sopptr->sem_op;
    660   1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    661   1.6   mycroft 					do_undos = 1;
    662   1.6   mycroft 			}
    663   1.6   mycroft 		}
    664   1.1       cgd 
    665   1.6   mycroft 		/*
    666   1.6   mycroft 		 * Did we get through the entire vector?
    667   1.6   mycroft 		 */
    668   1.6   mycroft 		if (i >= nsops)
    669   1.6   mycroft 			goto done;
    670   1.1       cgd 
    671   1.6   mycroft 		/*
    672   1.6   mycroft 		 * No ... rollback anything that we've already done
    673   1.6   mycroft 		 */
    674  1.27  christos 		SEM_PRINTF(("semop:  rollback 0 through %d\n", i-1));
    675   1.6   mycroft 		for (j = 0; j < i; j++)
    676  1.33   thorpej 			semaptr->_sem_base[sops[j].sem_num].semval -=
    677   1.6   mycroft 			    sops[j].sem_op;
    678   1.1       cgd 
    679   1.6   mycroft 		/*
    680   1.6   mycroft 		 * If the request that we couldn't satisfy has the
    681   1.6   mycroft 		 * NOWAIT flag set then return with EAGAIN.
    682   1.6   mycroft 		 */
    683   1.6   mycroft 		if (sopptr->sem_flg & IPC_NOWAIT)
    684   1.6   mycroft 			return(EAGAIN);
    685   1.1       cgd 
    686   1.6   mycroft 		if (sopptr->sem_op == 0)
    687   1.6   mycroft 			semptr->semzcnt++;
    688   1.6   mycroft 		else
    689   1.6   mycroft 			semptr->semncnt++;
    690   1.1       cgd 
    691  1.27  christos 		SEM_PRINTF(("semop:  good night!\n"));
    692   1.6   mycroft 		eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
    693   1.6   mycroft 		    "semwait", 0);
    694  1.27  christos 		SEM_PRINTF(("semop:  good morning (eval=%d)!\n", eval));
    695   1.1       cgd 
    696   1.6   mycroft 		suptr = NULL;	/* sem_undo may have been reallocated */
    697   1.1       cgd 
    698   1.6   mycroft 		if (eval != 0)
    699   1.6   mycroft 			return(EINTR);
    700  1.27  christos 		SEM_PRINTF(("semop:  good morning!\n"));
    701   1.1       cgd 
    702   1.6   mycroft 		/*
    703   1.6   mycroft 		 * Make sure that the semaphore still exists
    704   1.6   mycroft 		 */
    705   1.6   mycroft 		if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    706  1.33   thorpej 		    semaptr->sem_perm._seq != IPCID_TO_SEQ(SCARG(uap, semid))) {
    707   1.6   mycroft 			/* The man page says to return EIDRM. */
    708   1.6   mycroft 			/* Unfortunately, BSD doesn't define that code! */
    709   1.1       cgd #ifdef EIDRM
    710   1.6   mycroft 			return(EIDRM);
    711   1.1       cgd #else
    712   1.6   mycroft 			return(EINVAL);
    713   1.1       cgd #endif
    714   1.6   mycroft 		}
    715   1.1       cgd 
    716   1.6   mycroft 		/*
    717   1.6   mycroft 		 * The semaphore is still alive.  Readjust the count of
    718   1.6   mycroft 		 * waiting processes.
    719   1.6   mycroft 		 */
    720   1.6   mycroft 		if (sopptr->sem_op == 0)
    721   1.6   mycroft 			semptr->semzcnt--;
    722   1.6   mycroft 		else
    723   1.6   mycroft 			semptr->semncnt--;
    724   1.6   mycroft 	}
    725   1.1       cgd 
    726   1.6   mycroft done:
    727   1.6   mycroft 	/*
    728   1.6   mycroft 	 * Process any SEM_UNDO requests.
    729   1.6   mycroft 	 */
    730   1.6   mycroft 	if (do_undos) {
    731   1.5   mycroft 		for (i = 0; i < nsops; i++) {
    732   1.6   mycroft 			/*
    733   1.6   mycroft 			 * We only need to deal with SEM_UNDO's for non-zero
    734   1.6   mycroft 			 * op's.
    735   1.6   mycroft 			 */
    736   1.6   mycroft 			int adjval;
    737   1.1       cgd 
    738   1.6   mycroft 			if ((sops[i].sem_flg & SEM_UNDO) == 0)
    739   1.6   mycroft 				continue;
    740   1.6   mycroft 			adjval = sops[i].sem_op;
    741   1.6   mycroft 			if (adjval == 0)
    742   1.6   mycroft 				continue;
    743   1.6   mycroft 			eval = semundo_adjust(p, &suptr, semid,
    744   1.6   mycroft 			    sops[i].sem_num, -adjval);
    745   1.6   mycroft 			if (eval == 0)
    746   1.6   mycroft 				continue;
    747   1.1       cgd 
    748   1.6   mycroft 			/*
    749   1.6   mycroft 			 * Oh-Oh!  We ran out of either sem_undo's or undo's.
    750   1.6   mycroft 			 * Rollback the adjustments to this point and then
    751   1.6   mycroft 			 * rollback the semaphore ups and down so we can return
    752   1.6   mycroft 			 * with an error with all structures restored.  We
    753   1.6   mycroft 			 * rollback the undo's in the exact reverse order that
    754   1.6   mycroft 			 * we applied them.  This guarantees that we won't run
    755   1.6   mycroft 			 * out of space as we roll things back out.
    756   1.6   mycroft 			 */
    757   1.6   mycroft 			for (j = i - 1; j >= 0; j--) {
    758   1.6   mycroft 				if ((sops[j].sem_flg & SEM_UNDO) == 0)
    759   1.6   mycroft 					continue;
    760   1.6   mycroft 				adjval = sops[j].sem_op;
    761   1.6   mycroft 				if (adjval == 0)
    762   1.6   mycroft 					continue;
    763   1.6   mycroft 				if (semundo_adjust(p, &suptr, semid,
    764   1.6   mycroft 				    sops[j].sem_num, adjval) != 0)
    765   1.1       cgd 					panic("semop - can't undo undos");
    766   1.6   mycroft 			}
    767   1.1       cgd 
    768   1.6   mycroft 			for (j = 0; j < nsops; j++)
    769  1.33   thorpej 				semaptr->_sem_base[sops[j].sem_num].semval -=
    770   1.6   mycroft 				    sops[j].sem_op;
    771   1.1       cgd 
    772  1.27  christos 			SEM_PRINTF(("eval = %d from semundo_adjust\n", eval));
    773   1.6   mycroft 			return(eval);
    774   1.1       cgd 		} /* loop through the sops */
    775   1.6   mycroft 	} /* if (do_undos) */
    776   1.1       cgd 
    777   1.6   mycroft 	/* We're definitely done - set the sempid's */
    778   1.6   mycroft 	for (i = 0; i < nsops; i++) {
    779   1.1       cgd 		sopptr = &sops[i];
    780  1.33   thorpej 		semptr = &semaptr->_sem_base[sopptr->sem_num];
    781   1.1       cgd 		semptr->sempid = p->p_pid;
    782   1.6   mycroft 	}
    783   1.1       cgd 
    784   1.6   mycroft 	/* Do a wakeup if any semaphore was up'd. */
    785   1.6   mycroft 	if (do_wakeup) {
    786  1.27  christos 		SEM_PRINTF(("semop:  doing wakeup\n"));
    787   1.1       cgd #ifdef SEM_WAKEUP
    788   1.4   mycroft 		sem_wakeup((caddr_t)semaptr);
    789   1.1       cgd #else
    790   1.4   mycroft 		wakeup((caddr_t)semaptr);
    791   1.1       cgd #endif
    792  1.27  christos 		SEM_PRINTF(("semop:  back from wakeup\n"));
    793   1.6   mycroft 	}
    794  1.27  christos 	SEM_PRINTF(("semop:  done\n"));
    795   1.6   mycroft 	*retval = 0;
    796   1.6   mycroft 	return(0);
    797   1.1       cgd }
    798   1.1       cgd 
    799   1.1       cgd /*
    800   1.6   mycroft  * Go through the undo structures for this process and apply the adjustments to
    801   1.6   mycroft  * semaphores.
    802   1.1       cgd  */
    803  1.25  christos void
    804   1.1       cgd semexit(p)
    805   1.6   mycroft 	struct proc *p;
    806   1.1       cgd {
    807  1.35  augustss 	struct sem_undo *suptr;
    808  1.35  augustss 	struct sem_undo **supptr;
    809   1.1       cgd 
    810   1.6   mycroft 	/*
    811  1.17   mycroft 	 * Go through the chain of undo vectors looking for one associated with
    812  1.17   mycroft 	 * this process.
    813  1.17   mycroft 	 */
    814  1.17   mycroft 
    815  1.17   mycroft 	for (supptr = &semu_list; (suptr = *supptr) != NULL;
    816  1.17   mycroft 	    supptr = &suptr->un_next) {
    817  1.17   mycroft 		if (suptr->un_proc == p)
    818  1.17   mycroft 			break;
    819  1.17   mycroft 	}
    820  1.17   mycroft 
    821  1.17   mycroft 	/*
    822  1.37  sommerfe 	 * If there is no undo vector, skip to the end.
    823  1.14   mycroft 	 */
    824  1.14   mycroft 
    825  1.37  sommerfe 	if (suptr == NULL)
    826  1.37  sommerfe 		return;
    827  1.37  sommerfe 
    828  1.14   mycroft 	/*
    829  1.37  sommerfe 	 * We now have an undo vector for this process.
    830  1.15   mycroft 	 */
    831   1.1       cgd 
    832  1.27  christos 	SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
    833  1.27  christos 	    suptr->un_cnt));
    834   1.1       cgd 
    835   1.5   mycroft 	/*
    836   1.5   mycroft 	 * If there are any active undo elements then process them.
    837   1.5   mycroft 	 */
    838   1.5   mycroft 	if (suptr->un_cnt > 0) {
    839   1.6   mycroft 		int ix;
    840   1.1       cgd 
    841   1.6   mycroft 		for (ix = 0; ix < suptr->un_cnt; ix++) {
    842   1.6   mycroft 			int semid = suptr->un_ent[ix].un_id;
    843   1.6   mycroft 			int semnum = suptr->un_ent[ix].un_num;
    844   1.6   mycroft 			int adjval = suptr->un_ent[ix].un_adjval;
    845   1.6   mycroft 			struct semid_ds *semaptr;
    846   1.6   mycroft 
    847   1.6   mycroft 			semaptr = &sema[semid];
    848   1.6   mycroft 			if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
    849   1.6   mycroft 				panic("semexit - semid not allocated");
    850   1.6   mycroft 			if (semnum >= semaptr->sem_nsems)
    851   1.6   mycroft 				panic("semexit - semnum out of range");
    852   1.6   mycroft 
    853  1.27  christos 			SEM_PRINTF(("semexit:  %p id=%d num=%d(adj=%d) ; sem=%d\n",
    854   1.6   mycroft 			    suptr->un_proc, suptr->un_ent[ix].un_id,
    855   1.6   mycroft 			    suptr->un_ent[ix].un_num,
    856   1.6   mycroft 			    suptr->un_ent[ix].un_adjval,
    857  1.33   thorpej 			    semaptr->_sem_base[semnum].semval));
    858   1.6   mycroft 
    859  1.14   mycroft 			if (adjval < 0 &&
    860  1.33   thorpej 			    semaptr->_sem_base[semnum].semval < -adjval)
    861  1.33   thorpej 				semaptr->_sem_base[semnum].semval = 0;
    862  1.14   mycroft 			else
    863  1.33   thorpej 				semaptr->_sem_base[semnum].semval += adjval;
    864   1.1       cgd 
    865   1.1       cgd #ifdef SEM_WAKEUP
    866   1.6   mycroft 			sem_wakeup((caddr_t)semaptr);
    867   1.1       cgd #else
    868   1.6   mycroft 			wakeup((caddr_t)semaptr);
    869   1.1       cgd #endif
    870  1.27  christos 			SEM_PRINTF(("semexit:  back from wakeup\n"));
    871   1.6   mycroft 		}
    872   1.5   mycroft 	}
    873   1.1       cgd 
    874   1.5   mycroft 	/*
    875   1.5   mycroft 	 * Deallocate the undo vector.
    876   1.5   mycroft 	 */
    877  1.27  christos 	SEM_PRINTF(("removing vector\n"));
    878   1.5   mycroft 	suptr->un_proc = NULL;
    879   1.5   mycroft 	*supptr = suptr->un_next;
    880   1.1       cgd }
    881