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