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