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