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