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