Home | History | Annotate | Line # | Download | only in kern
sysv_sem.c revision 1.35
      1  1.35  augustss /*	$NetBSD: sysv_sem.c,v 1.35 2000/03/30 09:27:13 augustss 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.35  augustss 	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.35  augustss 		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.35  augustss 	int i;
    164  1.35  augustss 	struct sem_undo *suptr;
    165  1.35  augustss 	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.35  augustss 	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.35  augustss 	struct sem_undo *suptr;
    237  1.35  augustss 	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.35  augustss 	struct sem_undo *suptr;
    301   1.1       cgd 
    302   1.6   mycroft 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
    303  1.35  augustss 		struct undo *sunptr;
    304  1.35  augustss 		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.34  christos 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.34  christos 	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.34  christos 		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.34  christos 	void *pass_arg;
    339  1.34  christos 	union __semun karg;
    340  1.33   thorpej 
    341  1.33   thorpej 	cmd = SCARG(uap, cmd);
    342  1.33   thorpej 
    343  1.33   thorpej 	switch (cmd) {
    344  1.33   thorpej 	case IPC_SET:
    345  1.33   thorpej 	case IPC_STAT:
    346  1.33   thorpej 		pass_arg = &sembuf;
    347  1.33   thorpej 		break;
    348  1.33   thorpej 
    349  1.33   thorpej 	case GETALL:
    350  1.33   thorpej 	case SETVAL:
    351  1.33   thorpej 	case SETALL:
    352  1.34  christos 		pass_arg = &karg;
    353  1.34  christos 		break;
    354  1.34  christos 	default:
    355  1.34  christos 		pass_arg = NULL;
    356  1.33   thorpej 		break;
    357  1.33   thorpej 	}
    358  1.33   thorpej 
    359  1.34  christos 	if (pass_arg) {
    360  1.34  christos 		error = copyin(SCARG(uap, arg), &karg, sizeof(karg));
    361  1.33   thorpej 		if (error)
    362  1.34  christos 			return error;
    363  1.34  christos 		if (cmd == IPC_SET) {
    364  1.34  christos 			error = copyin(karg.buf, &sembuf, sizeof(sembuf));
    365  1.34  christos 			if (error)
    366  1.34  christos 				return (error);
    367  1.34  christos 		}
    368  1.33   thorpej 	}
    369  1.33   thorpej 
    370  1.33   thorpej 	error = semctl1(p, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    371  1.33   thorpej 	    pass_arg, retval);
    372  1.33   thorpej 
    373  1.33   thorpej 	if (error == 0 && cmd == IPC_STAT)
    374  1.34  christos 		error = copyout(&sembuf, karg.buf, sizeof(sembuf));
    375  1.33   thorpej 
    376  1.33   thorpej 	return (error);
    377  1.33   thorpej }
    378  1.33   thorpej 
    379  1.33   thorpej int
    380  1.33   thorpej semctl1(p, semid, semnum, cmd, v, retval)
    381  1.33   thorpej 	struct proc *p;
    382  1.33   thorpej 	int semid, semnum, cmd;
    383  1.33   thorpej 	void *v;
    384  1.33   thorpej 	register_t *retval;
    385  1.33   thorpej {
    386   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    387  1.33   thorpej 	union __semun *arg = v;
    388  1.33   thorpej 	struct semid_ds *sembuf = v, *semaptr;
    389  1.33   thorpej 	int i, error, ix;
    390   1.1       cgd 
    391  1.27  christos 	SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
    392  1.33   thorpej 	    semid, semnum, cmd, v));
    393   1.1       cgd 
    394  1.17   mycroft 	semlock(p);
    395  1.12   mycroft 
    396  1.33   thorpej 	ix = IPCID_TO_IX(semid);
    397  1.33   thorpej 	if (ix < 0 || ix >= seminfo.semmsl)
    398  1.33   thorpej 		return (EINVAL);
    399   1.6   mycroft 
    400  1.33   thorpej 	semaptr = &sema[ix];
    401   1.6   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    402  1.33   thorpej 	    semaptr->sem_perm._seq != IPCID_TO_SEQ(semid))
    403  1.33   thorpej 		return (EINVAL);
    404   1.1       cgd 
    405   1.6   mycroft 	switch (cmd) {
    406   1.6   mycroft 	case IPC_RMID:
    407  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
    408  1.33   thorpej 			return (error);
    409   1.6   mycroft 		semaptr->sem_perm.cuid = cred->cr_uid;
    410   1.6   mycroft 		semaptr->sem_perm.uid = cred->cr_uid;
    411   1.6   mycroft 		semtot -= semaptr->sem_nsems;
    412  1.33   thorpej 		for (i = semaptr->_sem_base - sem; i < semtot; i++)
    413   1.6   mycroft 			sem[i] = sem[i + semaptr->sem_nsems];
    414   1.6   mycroft 		for (i = 0; i < seminfo.semmni; i++) {
    415   1.6   mycroft 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
    416  1.33   thorpej 			    sema[i]._sem_base > semaptr->_sem_base)
    417  1.33   thorpej 				sema[i]._sem_base -= semaptr->sem_nsems;
    418   1.6   mycroft 		}
    419   1.6   mycroft 		semaptr->sem_perm.mode = 0;
    420  1.33   thorpej 		semundo_clear(ix, -1);
    421  1.33   thorpej 		wakeup(semaptr);
    422   1.6   mycroft 		break;
    423   1.1       cgd 
    424   1.6   mycroft 	case IPC_SET:
    425  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
    426  1.33   thorpej 			return (error);
    427  1.33   thorpej 		semaptr->sem_perm.uid = sembuf->sem_perm.uid;
    428  1.33   thorpej 		semaptr->sem_perm.gid = sembuf->sem_perm.gid;
    429   1.6   mycroft 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
    430  1.33   thorpej 		    (sembuf->sem_perm.mode & 0777);
    431   1.6   mycroft 		semaptr->sem_ctime = time.tv_sec;
    432   1.6   mycroft 		break;
    433   1.1       cgd 
    434   1.6   mycroft 	case IPC_STAT:
    435  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    436  1.33   thorpej 			return (error);
    437  1.33   thorpej 		memcpy(sembuf, semaptr, sizeof(struct semid_ds));
    438   1.6   mycroft 		break;
    439   1.1       cgd 
    440   1.6   mycroft 	case GETNCNT:
    441  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    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 		*retval = semaptr->_sem_base[semnum].semncnt;
    446   1.6   mycroft 		break;
    447   1.1       cgd 
    448   1.6   mycroft 	case GETPID:
    449  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    450  1.33   thorpej 			return (error);
    451   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    452  1.33   thorpej 			return (EINVAL);
    453  1.33   thorpej 		*retval = semaptr->_sem_base[semnum].sempid;
    454   1.6   mycroft 		break;
    455   1.1       cgd 
    456   1.6   mycroft 	case GETVAL:
    457  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    458  1.33   thorpej 			return (error);
    459   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    460  1.33   thorpej 			return (EINVAL);
    461  1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semval;
    462   1.6   mycroft 		break;
    463   1.1       cgd 
    464   1.6   mycroft 	case GETALL:
    465  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    466  1.33   thorpej 			return (error);
    467   1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    468  1.33   thorpej 			error = copyout(&semaptr->_sem_base[i].semval,
    469  1.33   thorpej 			    &arg->array[i], sizeof(arg->array[i]));
    470  1.33   thorpej 			if (error != 0)
    471   1.6   mycroft 				break;
    472   1.6   mycroft 		}
    473   1.6   mycroft 		break;
    474   1.1       cgd 
    475   1.6   mycroft 	case GETZCNT:
    476  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    477  1.33   thorpej 			return (error);
    478   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    479  1.33   thorpej 			return (EINVAL);
    480  1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semzcnt;
    481   1.6   mycroft 		break;
    482   1.1       cgd 
    483   1.6   mycroft 	case SETVAL:
    484  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    485  1.33   thorpej 			return (error);
    486   1.6   mycroft 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
    487  1.33   thorpej 			return (EINVAL);
    488  1.33   thorpej 		semaptr->_sem_base[semnum].semval = arg->val;
    489  1.33   thorpej 		semundo_clear(ix, semnum);
    490  1.33   thorpej 		wakeup(semaptr);
    491   1.6   mycroft 		break;
    492   1.1       cgd 
    493   1.6   mycroft 	case SETALL:
    494  1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    495  1.33   thorpej 			return (error);
    496   1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    497  1.33   thorpej 			error = copyin(&arg->array[i],
    498  1.33   thorpej 			    &semaptr->_sem_base[i].semval,
    499  1.33   thorpej 			    sizeof(arg->array[i]));
    500  1.33   thorpej 			if (error != 0)
    501   1.6   mycroft 				break;
    502   1.6   mycroft 		}
    503  1.33   thorpej 		semundo_clear(ix, -1);
    504  1.33   thorpej 		wakeup(semaptr);
    505   1.6   mycroft 		break;
    506   1.1       cgd 
    507   1.6   mycroft 	default:
    508  1.33   thorpej 		return (EINVAL);
    509   1.6   mycroft 	}
    510   1.4   mycroft 
    511  1.33   thorpej 	return (error);
    512   1.1       cgd }
    513   1.1       cgd 
    514   1.1       cgd int
    515  1.24   mycroft sys_semget(p, v, retval)
    516   1.1       cgd 	struct proc *p;
    517  1.23   thorpej 	void *v;
    518  1.23   thorpej 	register_t *retval;
    519  1.23   thorpej {
    520  1.35  augustss 	struct sys_semget_args /* {
    521  1.10       cgd 		syscallarg(key_t) key;
    522  1.10       cgd 		syscallarg(int) nsems;
    523  1.10       cgd 		syscallarg(int) semflg;
    524  1.23   thorpej 	} */ *uap = v;
    525   1.6   mycroft 	int semid, eval;
    526  1.10       cgd 	int key = SCARG(uap, key);
    527  1.10       cgd 	int nsems = SCARG(uap, nsems);
    528  1.10       cgd 	int semflg = SCARG(uap, semflg);
    529   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    530   1.1       cgd 
    531  1.27  christos 	SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
    532   1.1       cgd 
    533  1.17   mycroft 	semlock(p);
    534  1.12   mycroft 
    535   1.6   mycroft 	if (key != IPC_PRIVATE) {
    536   1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    537   1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
    538  1.33   thorpej 			    sema[semid].sem_perm._key == key)
    539   1.6   mycroft 				break;
    540   1.6   mycroft 		}
    541   1.6   mycroft 		if (semid < seminfo.semmni) {
    542  1.27  christos 			SEM_PRINTF(("found public key\n"));
    543   1.7   hpeyerl 			if ((eval = ipcperm(cred, &sema[semid].sem_perm,
    544   1.7   hpeyerl 			    semflg & 0700)))
    545   1.6   mycroft 				return(eval);
    546   1.6   mycroft 			if (nsems > 0 && sema[semid].sem_nsems < nsems) {
    547  1.27  christos 				SEM_PRINTF(("too small\n"));
    548   1.6   mycroft 				return(EINVAL);
    549   1.6   mycroft 			}
    550   1.6   mycroft 			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
    551  1.27  christos 				SEM_PRINTF(("not exclusive\n"));
    552   1.6   mycroft 				return(EEXIST);
    553   1.6   mycroft 			}
    554   1.6   mycroft 			goto found;
    555   1.6   mycroft 		}
    556   1.6   mycroft 	}
    557   1.6   mycroft 
    558  1.27  christos 	SEM_PRINTF(("need to allocate the semid_ds\n"));
    559   1.6   mycroft 	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
    560   1.6   mycroft 		if (nsems <= 0 || nsems > seminfo.semmsl) {
    561  1.27  christos 			SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
    562  1.27  christos 			    seminfo.semmsl));
    563   1.6   mycroft 			return(EINVAL);
    564   1.6   mycroft 		}
    565   1.6   mycroft 		if (nsems > seminfo.semmns - semtot) {
    566  1.27  christos 			SEM_PRINTF(("not enough semaphores left (need %d, got %d)\n",
    567  1.27  christos 			    nsems, seminfo.semmns - semtot));
    568   1.6   mycroft 			return(ENOSPC);
    569   1.6   mycroft 		}
    570   1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    571   1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
    572   1.6   mycroft 				break;
    573   1.6   mycroft 		}
    574   1.6   mycroft 		if (semid == seminfo.semmni) {
    575  1.27  christos 			SEM_PRINTF(("no more semid_ds's available\n"));
    576   1.6   mycroft 			return(ENOSPC);
    577   1.6   mycroft 		}
    578  1.27  christos 		SEM_PRINTF(("semid %d is available\n", semid));
    579  1.33   thorpej 		sema[semid].sem_perm._key = key;
    580   1.6   mycroft 		sema[semid].sem_perm.cuid = cred->cr_uid;
    581   1.6   mycroft 		sema[semid].sem_perm.uid = cred->cr_uid;
    582   1.6   mycroft 		sema[semid].sem_perm.cgid = cred->cr_gid;
    583   1.6   mycroft 		sema[semid].sem_perm.gid = cred->cr_gid;
    584   1.6   mycroft 		sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
    585  1.33   thorpej 		sema[semid].sem_perm._seq =
    586  1.33   thorpej 		    (sema[semid].sem_perm._seq + 1) & 0x7fff;
    587   1.6   mycroft 		sema[semid].sem_nsems = nsems;
    588   1.6   mycroft 		sema[semid].sem_otime = 0;
    589   1.6   mycroft 		sema[semid].sem_ctime = time.tv_sec;
    590  1.33   thorpej 		sema[semid]._sem_base = &sem[semtot];
    591   1.6   mycroft 		semtot += nsems;
    592  1.33   thorpej 		memset(sema[semid]._sem_base, 0,
    593  1.33   thorpej 		    sizeof(sema[semid]._sem_base[0])*nsems);
    594  1.33   thorpej 		SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid]._sem_base,
    595  1.27  christos 		    &sem[semtot]));
    596   1.1       cgd 	} else {
    597  1.27  christos 		SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
    598   1.6   mycroft 		return(ENOENT);
    599   1.1       cgd 	}
    600   1.1       cgd 
    601   1.6   mycroft found:
    602   1.6   mycroft 	*retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
    603   1.6   mycroft 	return(0);
    604   1.1       cgd }
    605   1.1       cgd 
    606   1.1       cgd int
    607  1.24   mycroft sys_semop(p, v, retval)
    608   1.1       cgd 	struct proc *p;
    609  1.23   thorpej 	void *v;
    610  1.23   thorpej 	register_t *retval;
    611  1.23   thorpej {
    612  1.35  augustss 	struct sys_semop_args /* {
    613  1.10       cgd 		syscallarg(int) semid;
    614  1.10       cgd 		syscallarg(struct sembuf *) sops;
    615  1.29    kleink 		syscallarg(size_t) nsops;
    616  1.23   thorpej 	} */ *uap = v;
    617  1.10       cgd 	int semid = SCARG(uap, semid);
    618  1.10       cgd 	int nsops = SCARG(uap, nsops);
    619   1.6   mycroft 	struct sembuf sops[MAX_SOPS];
    620  1.35  augustss 	struct semid_ds *semaptr;
    621  1.35  augustss 	struct sembuf *sopptr = NULL;
    622  1.35  augustss 	struct __sem *semptr = NULL;
    623   1.6   mycroft 	struct sem_undo *suptr = NULL;
    624   1.6   mycroft 	struct ucred *cred = p->p_ucred;
    625   1.6   mycroft 	int i, j, eval;
    626  1.25  christos 	int do_wakeup, do_undos;
    627   1.1       cgd 
    628  1.27  christos 	SEM_PRINTF(("call to semop(%d, %p, %d)\n", semid, sops, nsops));
    629   1.1       cgd 
    630  1.17   mycroft 	semlock(p);
    631  1.12   mycroft 
    632   1.6   mycroft 	semid = IPCID_TO_IX(semid);	/* Convert back to zero origin */
    633   1.6   mycroft 
    634   1.6   mycroft 	if (semid < 0 || semid >= seminfo.semmsl)
    635   1.6   mycroft 		return(EINVAL);
    636   1.6   mycroft 
    637   1.6   mycroft 	semaptr = &sema[semid];
    638  1.11   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    639  1.33   thorpej 	    semaptr->sem_perm._seq != IPCID_TO_SEQ(SCARG(uap, semid)))
    640   1.6   mycroft 		return(EINVAL);
    641   1.6   mycroft 
    642   1.7   hpeyerl 	if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
    643  1.27  christos 		SEM_PRINTF(("eval = %d from ipaccess\n", eval));
    644   1.6   mycroft 		return(eval);
    645   1.6   mycroft 	}
    646   1.1       cgd 
    647   1.6   mycroft 	if (nsops > MAX_SOPS) {
    648  1.27  christos 		SEM_PRINTF(("too many sops (max=%d, nsops=%d)\n", MAX_SOPS, nsops));
    649   1.6   mycroft 		return(E2BIG);
    650   1.6   mycroft 	}
    651   1.1       cgd 
    652  1.10       cgd 	if ((eval = copyin(SCARG(uap, sops), sops, nsops * sizeof(sops[0])))
    653  1.10       cgd 	    != 0) {
    654  1.27  christos 		SEM_PRINTF(("eval = %d from copyin(%p, %p, %d)\n", eval,
    655  1.27  christos 		    SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
    656   1.6   mycroft 		return(eval);
    657   1.6   mycroft 	}
    658   1.1       cgd 
    659   1.6   mycroft 	/*
    660   1.6   mycroft 	 * Loop trying to satisfy the vector of requests.
    661   1.6   mycroft 	 * If we reach a point where we must wait, any requests already
    662   1.6   mycroft 	 * performed are rolled back and we go to sleep until some other
    663   1.6   mycroft 	 * process wakes us up.  At this point, we start all over again.
    664   1.6   mycroft 	 *
    665   1.6   mycroft 	 * This ensures that from the perspective of other tasks, a set
    666   1.6   mycroft 	 * of requests is atomic (never partially satisfied).
    667   1.6   mycroft 	 */
    668   1.6   mycroft 	do_undos = 0;
    669   1.1       cgd 
    670   1.6   mycroft 	for (;;) {
    671   1.6   mycroft 		do_wakeup = 0;
    672   1.1       cgd 
    673   1.6   mycroft 		for (i = 0; i < nsops; i++) {
    674   1.6   mycroft 			sopptr = &sops[i];
    675   1.1       cgd 
    676   1.6   mycroft 			if (sopptr->sem_num >= semaptr->sem_nsems)
    677   1.6   mycroft 				return(EFBIG);
    678   1.1       cgd 
    679  1.33   thorpej 			semptr = &semaptr->_sem_base[sopptr->sem_num];
    680   1.1       cgd 
    681  1.27  christos 			SEM_PRINTF(("semop:  semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
    682  1.33   thorpej 			    semaptr, semaptr->_sem_base, semptr,
    683   1.6   mycroft 			    sopptr->sem_num, semptr->semval, sopptr->sem_op,
    684  1.27  christos 			    (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait"));
    685   1.1       cgd 
    686   1.6   mycroft 			if (sopptr->sem_op < 0) {
    687  1.25  christos 				if ((int)(semptr->semval +
    688  1.25  christos 					  sopptr->sem_op) < 0) {
    689  1.27  christos 					SEM_PRINTF(("semop:  can't do it now\n"));
    690   1.6   mycroft 					break;
    691   1.6   mycroft 				} else {
    692   1.6   mycroft 					semptr->semval += sopptr->sem_op;
    693   1.6   mycroft 					if (semptr->semval == 0 &&
    694   1.6   mycroft 					    semptr->semzcnt > 0)
    695   1.6   mycroft 						do_wakeup = 1;
    696   1.6   mycroft 				}
    697   1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    698   1.6   mycroft 					do_undos = 1;
    699   1.6   mycroft 			} else if (sopptr->sem_op == 0) {
    700   1.6   mycroft 				if (semptr->semval > 0) {
    701  1.27  christos 					SEM_PRINTF(("semop:  not zero now\n"));
    702   1.6   mycroft 					break;
    703   1.6   mycroft 				}
    704   1.6   mycroft 			} else {
    705   1.6   mycroft 				if (semptr->semncnt > 0)
    706   1.6   mycroft 					do_wakeup = 1;
    707   1.6   mycroft 				semptr->semval += sopptr->sem_op;
    708   1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    709   1.6   mycroft 					do_undos = 1;
    710   1.6   mycroft 			}
    711   1.6   mycroft 		}
    712   1.1       cgd 
    713   1.6   mycroft 		/*
    714   1.6   mycroft 		 * Did we get through the entire vector?
    715   1.6   mycroft 		 */
    716   1.6   mycroft 		if (i >= nsops)
    717   1.6   mycroft 			goto done;
    718   1.1       cgd 
    719   1.6   mycroft 		/*
    720   1.6   mycroft 		 * No ... rollback anything that we've already done
    721   1.6   mycroft 		 */
    722  1.27  christos 		SEM_PRINTF(("semop:  rollback 0 through %d\n", i-1));
    723   1.6   mycroft 		for (j = 0; j < i; j++)
    724  1.33   thorpej 			semaptr->_sem_base[sops[j].sem_num].semval -=
    725   1.6   mycroft 			    sops[j].sem_op;
    726   1.1       cgd 
    727   1.6   mycroft 		/*
    728   1.6   mycroft 		 * If the request that we couldn't satisfy has the
    729   1.6   mycroft 		 * NOWAIT flag set then return with EAGAIN.
    730   1.6   mycroft 		 */
    731   1.6   mycroft 		if (sopptr->sem_flg & IPC_NOWAIT)
    732   1.6   mycroft 			return(EAGAIN);
    733   1.1       cgd 
    734   1.6   mycroft 		if (sopptr->sem_op == 0)
    735   1.6   mycroft 			semptr->semzcnt++;
    736   1.6   mycroft 		else
    737   1.6   mycroft 			semptr->semncnt++;
    738   1.1       cgd 
    739  1.27  christos 		SEM_PRINTF(("semop:  good night!\n"));
    740   1.6   mycroft 		eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
    741   1.6   mycroft 		    "semwait", 0);
    742  1.27  christos 		SEM_PRINTF(("semop:  good morning (eval=%d)!\n", eval));
    743   1.1       cgd 
    744   1.6   mycroft 		suptr = NULL;	/* sem_undo may have been reallocated */
    745   1.1       cgd 
    746   1.6   mycroft 		if (eval != 0)
    747   1.6   mycroft 			return(EINTR);
    748  1.27  christos 		SEM_PRINTF(("semop:  good morning!\n"));
    749   1.1       cgd 
    750   1.6   mycroft 		/*
    751   1.6   mycroft 		 * Make sure that the semaphore still exists
    752   1.6   mycroft 		 */
    753   1.6   mycroft 		if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    754  1.33   thorpej 		    semaptr->sem_perm._seq != IPCID_TO_SEQ(SCARG(uap, semid))) {
    755   1.6   mycroft 			/* The man page says to return EIDRM. */
    756   1.6   mycroft 			/* Unfortunately, BSD doesn't define that code! */
    757   1.1       cgd #ifdef EIDRM
    758   1.6   mycroft 			return(EIDRM);
    759   1.1       cgd #else
    760   1.6   mycroft 			return(EINVAL);
    761   1.1       cgd #endif
    762   1.6   mycroft 		}
    763   1.1       cgd 
    764   1.6   mycroft 		/*
    765   1.6   mycroft 		 * The semaphore is still alive.  Readjust the count of
    766   1.6   mycroft 		 * waiting processes.
    767   1.6   mycroft 		 */
    768   1.6   mycroft 		if (sopptr->sem_op == 0)
    769   1.6   mycroft 			semptr->semzcnt--;
    770   1.6   mycroft 		else
    771   1.6   mycroft 			semptr->semncnt--;
    772   1.6   mycroft 	}
    773   1.1       cgd 
    774   1.6   mycroft done:
    775   1.6   mycroft 	/*
    776   1.6   mycroft 	 * Process any SEM_UNDO requests.
    777   1.6   mycroft 	 */
    778   1.6   mycroft 	if (do_undos) {
    779   1.5   mycroft 		for (i = 0; i < nsops; i++) {
    780   1.6   mycroft 			/*
    781   1.6   mycroft 			 * We only need to deal with SEM_UNDO's for non-zero
    782   1.6   mycroft 			 * op's.
    783   1.6   mycroft 			 */
    784   1.6   mycroft 			int adjval;
    785   1.1       cgd 
    786   1.6   mycroft 			if ((sops[i].sem_flg & SEM_UNDO) == 0)
    787   1.6   mycroft 				continue;
    788   1.6   mycroft 			adjval = sops[i].sem_op;
    789   1.6   mycroft 			if (adjval == 0)
    790   1.6   mycroft 				continue;
    791   1.6   mycroft 			eval = semundo_adjust(p, &suptr, semid,
    792   1.6   mycroft 			    sops[i].sem_num, -adjval);
    793   1.6   mycroft 			if (eval == 0)
    794   1.6   mycroft 				continue;
    795   1.1       cgd 
    796   1.6   mycroft 			/*
    797   1.6   mycroft 			 * Oh-Oh!  We ran out of either sem_undo's or undo's.
    798   1.6   mycroft 			 * Rollback the adjustments to this point and then
    799   1.6   mycroft 			 * rollback the semaphore ups and down so we can return
    800   1.6   mycroft 			 * with an error with all structures restored.  We
    801   1.6   mycroft 			 * rollback the undo's in the exact reverse order that
    802   1.6   mycroft 			 * we applied them.  This guarantees that we won't run
    803   1.6   mycroft 			 * out of space as we roll things back out.
    804   1.6   mycroft 			 */
    805   1.6   mycroft 			for (j = i - 1; j >= 0; j--) {
    806   1.6   mycroft 				if ((sops[j].sem_flg & SEM_UNDO) == 0)
    807   1.6   mycroft 					continue;
    808   1.6   mycroft 				adjval = sops[j].sem_op;
    809   1.6   mycroft 				if (adjval == 0)
    810   1.6   mycroft 					continue;
    811   1.6   mycroft 				if (semundo_adjust(p, &suptr, semid,
    812   1.6   mycroft 				    sops[j].sem_num, adjval) != 0)
    813   1.1       cgd 					panic("semop - can't undo undos");
    814   1.6   mycroft 			}
    815   1.1       cgd 
    816   1.6   mycroft 			for (j = 0; j < nsops; j++)
    817  1.33   thorpej 				semaptr->_sem_base[sops[j].sem_num].semval -=
    818   1.6   mycroft 				    sops[j].sem_op;
    819   1.1       cgd 
    820  1.27  christos 			SEM_PRINTF(("eval = %d from semundo_adjust\n", eval));
    821   1.6   mycroft 			return(eval);
    822   1.1       cgd 		} /* loop through the sops */
    823   1.6   mycroft 	} /* if (do_undos) */
    824   1.1       cgd 
    825   1.6   mycroft 	/* We're definitely done - set the sempid's */
    826   1.6   mycroft 	for (i = 0; i < nsops; i++) {
    827   1.1       cgd 		sopptr = &sops[i];
    828  1.33   thorpej 		semptr = &semaptr->_sem_base[sopptr->sem_num];
    829   1.1       cgd 		semptr->sempid = p->p_pid;
    830   1.6   mycroft 	}
    831   1.1       cgd 
    832   1.6   mycroft 	/* Do a wakeup if any semaphore was up'd. */
    833   1.6   mycroft 	if (do_wakeup) {
    834  1.27  christos 		SEM_PRINTF(("semop:  doing wakeup\n"));
    835   1.1       cgd #ifdef SEM_WAKEUP
    836   1.4   mycroft 		sem_wakeup((caddr_t)semaptr);
    837   1.1       cgd #else
    838   1.4   mycroft 		wakeup((caddr_t)semaptr);
    839   1.1       cgd #endif
    840  1.27  christos 		SEM_PRINTF(("semop:  back from wakeup\n"));
    841   1.6   mycroft 	}
    842  1.27  christos 	SEM_PRINTF(("semop:  done\n"));
    843   1.6   mycroft 	*retval = 0;
    844   1.6   mycroft 	return(0);
    845   1.1       cgd }
    846   1.1       cgd 
    847   1.1       cgd /*
    848   1.6   mycroft  * Go through the undo structures for this process and apply the adjustments to
    849   1.6   mycroft  * semaphores.
    850   1.1       cgd  */
    851  1.25  christos void
    852   1.1       cgd semexit(p)
    853   1.6   mycroft 	struct proc *p;
    854   1.1       cgd {
    855  1.35  augustss 	struct sem_undo *suptr;
    856  1.35  augustss 	struct sem_undo **supptr;
    857   1.1       cgd 
    858   1.6   mycroft 	/*
    859  1.17   mycroft 	 * Go through the chain of undo vectors looking for one associated with
    860  1.17   mycroft 	 * this process.
    861  1.17   mycroft 	 */
    862  1.17   mycroft 
    863  1.17   mycroft 	for (supptr = &semu_list; (suptr = *supptr) != NULL;
    864  1.17   mycroft 	    supptr = &suptr->un_next) {
    865  1.17   mycroft 		if (suptr->un_proc == p)
    866  1.17   mycroft 			break;
    867  1.17   mycroft 	}
    868  1.17   mycroft 
    869  1.17   mycroft 	/*
    870  1.14   mycroft 	 * There are a few possibilities to consider here ...
    871  1.14   mycroft 	 *
    872  1.14   mycroft 	 * 1) The semaphore facility isn't currently locked.  In this case,
    873  1.14   mycroft 	 *    this call should proceed normally.
    874  1.14   mycroft 	 * 2) The semaphore facility is locked by this process (i.e. the one
    875  1.14   mycroft 	 *    that is exiting).  In this case, this call should proceed as
    876  1.14   mycroft 	 *    usual and the facility should be unlocked at the end of this
    877  1.14   mycroft 	 *    routine (since the locker is exiting).
    878  1.14   mycroft 	 * 3) The semaphore facility is locked by some other process and this
    879  1.14   mycroft 	 *    process doesn't have an undo structure allocated for it.  In this
    880  1.14   mycroft 	 *    case, this call should proceed normally (i.e. not accomplish
    881  1.14   mycroft 	 *    anything and, most importantly, not block since that is
    882  1.14   mycroft 	 *    unnecessary and could result in a LOT of processes blocking in
    883  1.14   mycroft 	 *    here if the facility is locked for a long time).
    884  1.14   mycroft 	 * 4) The semaphore facility is locked by some other process and this
    885  1.14   mycroft 	 *    process has an undo structure allocated for it.  In this case,
    886  1.14   mycroft 	 *    this call should block until the facility has been unlocked since
    887  1.14   mycroft 	 *    the holder of the lock may be examining this process's proc entry
    888  1.14   mycroft 	 *    (the ipcs utility does this when printing out the information
    889  1.14   mycroft 	 *    from the allocated sem undo elements).
    890  1.14   mycroft 	 *
    891  1.14   mycroft 	 * This leads to the conclusion that we should not block unless we
    892  1.14   mycroft 	 * discover that the someone else has the semaphore facility locked and
    893  1.14   mycroft 	 * this process has an undo structure.  Let's do that...
    894  1.14   mycroft 	 *
    895  1.14   mycroft 	 * Note that we do this in a separate pass from the one that processes
    896  1.14   mycroft 	 * any existing undo structure since we don't want to risk blocking at
    897  1.14   mycroft 	 * that time (it would make the actual unlinking of the element from
    898  1.14   mycroft 	 * the chain of allocated undo structures rather messy).
    899  1.14   mycroft 	 */
    900  1.14   mycroft 
    901  1.14   mycroft 	/*
    902  1.14   mycroft 	 * Does someone else hold the semaphore facility's lock?
    903  1.14   mycroft 	 */
    904  1.14   mycroft 
    905  1.14   mycroft 	if (semlock_holder != NULL && semlock_holder != p) {
    906  1.14   mycroft 		/*
    907  1.14   mycroft 		 * Yes (i.e. we are in case 3 or 4).
    908  1.14   mycroft 		 *
    909  1.15   mycroft 		 * If we didn't find an undo vector associated with this
    910  1.14   mycroft 		 * process than we can just return (i.e. we are in case 3).
    911  1.14   mycroft 		 *
    912  1.14   mycroft 		 * Note that we know that someone else is holding the lock so
    913  1.14   mycroft 		 * we don't even have to see if we're holding it...
    914  1.14   mycroft 		 */
    915  1.14   mycroft 
    916  1.14   mycroft 		if (suptr == NULL)
    917  1.14   mycroft 			return;
    918  1.14   mycroft 
    919  1.14   mycroft 		/*
    920  1.14   mycroft 		 * We are in case 4.
    921  1.14   mycroft 		 *
    922  1.14   mycroft 		 * Go to sleep as long as someone else is locking the semaphore
    923  1.14   mycroft 		 * facility (note that we won't get here if we are holding the
    924  1.14   mycroft 		 * lock so we don't need to check for that possibility).
    925  1.14   mycroft 		 */
    926  1.14   mycroft 
    927  1.14   mycroft 		while (semlock_holder != NULL)
    928  1.14   mycroft 			sleep((caddr_t)&semlock_holder, (PZERO - 4));
    929  1.14   mycroft 
    930  1.14   mycroft 		/*
    931  1.14   mycroft 		 * Nobody is holding the facility (i.e. we are now in case 1).
    932  1.14   mycroft 		 * We can proceed safely according to the argument outlined
    933  1.14   mycroft 		 * above.
    934  1.14   mycroft 		 *
    935  1.16   mycroft 		 * We look up the undo vector again, in case the list changed
    936  1.17   mycroft 		 * while we were asleep, and the parent is now different.
    937  1.14   mycroft 		 */
    938  1.16   mycroft 
    939  1.17   mycroft 		for (supptr = &semu_list; (suptr = *supptr) != NULL;
    940  1.17   mycroft 		    supptr = &suptr->un_next) {
    941  1.17   mycroft 			if (suptr->un_proc == p)
    942  1.17   mycroft 				break;
    943  1.17   mycroft 		}
    944  1.17   mycroft 
    945  1.17   mycroft 		if (suptr == NULL)
    946  1.17   mycroft 			panic("semexit: undo vector disappeared");
    947  1.17   mycroft 	} else {
    948  1.17   mycroft 		/*
    949  1.17   mycroft 		 * No (i.e. we are in case 1 or 2).
    950  1.17   mycroft 		 *
    951  1.17   mycroft 		 * If there is no undo vector, skip to the end and unlock the
    952  1.17   mycroft 		 * semaphore facility if necessary.
    953  1.17   mycroft 		 */
    954  1.14   mycroft 
    955  1.17   mycroft 		if (suptr == NULL)
    956  1.17   mycroft 			goto unlock;
    957  1.14   mycroft 	}
    958  1.15   mycroft 
    959  1.15   mycroft 	/*
    960  1.17   mycroft 	 * We are now in case 1 or 2, and we have an undo vector for this
    961  1.17   mycroft 	 * process.
    962  1.15   mycroft 	 */
    963   1.1       cgd 
    964  1.27  christos 	SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
    965  1.27  christos 	    suptr->un_cnt));
    966   1.1       cgd 
    967   1.5   mycroft 	/*
    968   1.5   mycroft 	 * If there are any active undo elements then process them.
    969   1.5   mycroft 	 */
    970   1.5   mycroft 	if (suptr->un_cnt > 0) {
    971   1.6   mycroft 		int ix;
    972   1.1       cgd 
    973   1.6   mycroft 		for (ix = 0; ix < suptr->un_cnt; ix++) {
    974   1.6   mycroft 			int semid = suptr->un_ent[ix].un_id;
    975   1.6   mycroft 			int semnum = suptr->un_ent[ix].un_num;
    976   1.6   mycroft 			int adjval = suptr->un_ent[ix].un_adjval;
    977   1.6   mycroft 			struct semid_ds *semaptr;
    978   1.6   mycroft 
    979   1.6   mycroft 			semaptr = &sema[semid];
    980   1.6   mycroft 			if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
    981   1.6   mycroft 				panic("semexit - semid not allocated");
    982   1.6   mycroft 			if (semnum >= semaptr->sem_nsems)
    983   1.6   mycroft 				panic("semexit - semnum out of range");
    984   1.6   mycroft 
    985  1.27  christos 			SEM_PRINTF(("semexit:  %p id=%d num=%d(adj=%d) ; sem=%d\n",
    986   1.6   mycroft 			    suptr->un_proc, suptr->un_ent[ix].un_id,
    987   1.6   mycroft 			    suptr->un_ent[ix].un_num,
    988   1.6   mycroft 			    suptr->un_ent[ix].un_adjval,
    989  1.33   thorpej 			    semaptr->_sem_base[semnum].semval));
    990   1.6   mycroft 
    991  1.14   mycroft 			if (adjval < 0 &&
    992  1.33   thorpej 			    semaptr->_sem_base[semnum].semval < -adjval)
    993  1.33   thorpej 				semaptr->_sem_base[semnum].semval = 0;
    994  1.14   mycroft 			else
    995  1.33   thorpej 				semaptr->_sem_base[semnum].semval += adjval;
    996   1.1       cgd 
    997   1.1       cgd #ifdef SEM_WAKEUP
    998   1.6   mycroft 			sem_wakeup((caddr_t)semaptr);
    999   1.1       cgd #else
   1000   1.6   mycroft 			wakeup((caddr_t)semaptr);
   1001   1.1       cgd #endif
   1002  1.27  christos 			SEM_PRINTF(("semexit:  back from wakeup\n"));
   1003   1.6   mycroft 		}
   1004   1.5   mycroft 	}
   1005   1.1       cgd 
   1006   1.5   mycroft 	/*
   1007   1.5   mycroft 	 * Deallocate the undo vector.
   1008   1.5   mycroft 	 */
   1009  1.27  christos 	SEM_PRINTF(("removing vector\n"));
   1010   1.5   mycroft 	suptr->un_proc = NULL;
   1011   1.5   mycroft 	*supptr = suptr->un_next;
   1012   1.1       cgd 
   1013   1.6   mycroft unlock:
   1014   1.6   mycroft 	/*
   1015   1.6   mycroft 	 * If the exiting process is holding the global semaphore facility
   1016  1.14   mycroft 	 * lock (i.e. we are in case 2) then release it.
   1017   1.6   mycroft 	 */
   1018   1.6   mycroft 	if (semlock_holder == p) {
   1019   1.6   mycroft 		semlock_holder = NULL;
   1020   1.6   mycroft 		wakeup((caddr_t)&semlock_holder);
   1021   1.6   mycroft 	}
   1022   1.1       cgd }
   1023