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