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