Home | History | Annotate | Line # | Download | only in kern
sysv_sem.c revision 1.83.2.1
      1  1.83.2.1  wrstuden /*	$NetBSD: sysv_sem.c,v 1.83.2.1 2008/05/10 23:49:05 wrstuden Exp $	*/
      2      1.33   thorpej 
      3      1.33   thorpej /*-
      4      1.70        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.70        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  *
     20      1.33   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21      1.33   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22      1.33   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23      1.33   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24      1.33   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25      1.33   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26      1.33   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27      1.33   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28      1.33   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29      1.33   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30      1.33   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31      1.33   thorpej  */
     32       1.9       cgd 
     33       1.1       cgd /*
     34       1.1       cgd  * Implementation of SVID semaphores
     35       1.1       cgd  *
     36      1.33   thorpej  * Author: Daniel Boulet
     37       1.1       cgd  *
     38       1.1       cgd  * This software is provided ``AS IS'' without any warranties of any kind.
     39       1.1       cgd  */
     40      1.42     lukem 
     41      1.42     lukem #include <sys/cdefs.h>
     42  1.83.2.1  wrstuden __KERNEL_RCSID(0, "$NetBSD: sysv_sem.c,v 1.83.2.1 2008/05/10 23:49:05 wrstuden Exp $");
     43      1.31      tron 
     44      1.32      tron #define SYSVSEM
     45       1.1       cgd 
     46       1.3   mycroft #include <sys/param.h>
     47       1.3   mycroft #include <sys/kernel.h>
     48       1.3   mycroft #include <sys/sem.h>
     49      1.38    simonb #include <sys/sysctl.h>
     50      1.70        ad #include <sys/kmem.h>
     51      1.38    simonb #include <sys/mount.h>		/* XXX for <sys/syscallargs.h> */
     52  1.83.2.1  wrstuden #include <sys/sa.h>
     53      1.10       cgd #include <sys/syscallargs.h>
     54      1.61      elad #include <sys/kauth.h>
     55      1.25  christos 
     56      1.74     rmind /*
     57      1.74     rmind  * Memory areas:
     58      1.74     rmind  *  1st: Pool of semaphore identifiers
     59      1.74     rmind  *  2nd: Semaphores
     60      1.74     rmind  *  3rd: Conditional variables
     61      1.74     rmind  *  4th: Undo structures
     62      1.74     rmind  */
     63      1.74     rmind struct semid_ds		*sema;
     64      1.74     rmind static struct __sem	*sem;
     65      1.74     rmind static kcondvar_t	*semcv;
     66      1.74     rmind static int		*semu;
     67      1.74     rmind 
     68      1.74     rmind static kmutex_t	semlock;
     69      1.48  jdolecek static struct	sem_undo *semu_list;	/* list of active undo structures */
     70      1.74     rmind static u_int	semtot = 0;		/* total number of semaphores */
     71      1.74     rmind 
     72      1.74     rmind static u_int	sem_waiters = 0;	/* total number of semop waiters */
     73      1.74     rmind static bool	sem_realloc_state;
     74      1.74     rmind static kcondvar_t sem_realloc_cv;
     75      1.74     rmind 
     76      1.74     rmind /* Macro to find a particular sem_undo vector */
     77      1.74     rmind #define SEMU(s, ix)	((struct sem_undo *)(((long)s) + ix * seminfo.semusz))
     78       1.1       cgd 
     79      1.27  christos #ifdef SEM_DEBUG
     80      1.28  christos #define SEM_PRINTF(a) printf a
     81      1.27  christos #else
     82      1.27  christos #define SEM_PRINTF(a)
     83      1.27  christos #endif
     84      1.27  christos 
     85      1.53  junyoung struct sem_undo *semu_alloc(struct proc *);
     86      1.53  junyoung int semundo_adjust(struct proc *, struct sem_undo **, int, int, int);
     87      1.53  junyoung void semundo_clear(int, int);
     88      1.25  christos 
     89      1.25  christos void
     90      1.59   thorpej seminit(void)
     91       1.1       cgd {
     92      1.48  jdolecek 	int i, sz;
     93      1.48  jdolecek 	vaddr_t v;
     94       1.1       cgd 
     95      1.70        ad 	mutex_init(&semlock, MUTEX_DEFAULT, IPL_NONE);
     96      1.74     rmind 	cv_init(&sem_realloc_cv, "semrealc");
     97      1.74     rmind 	sem_realloc_state = false;
     98      1.70        ad 
     99      1.74     rmind 	/* Allocate the wired memory for our structures */
    100      1.74     rmind 	sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) +
    101      1.74     rmind 	    ALIGN(seminfo.semmns * sizeof(struct __sem)) +
    102      1.74     rmind 	    ALIGN(seminfo.semmni * sizeof(kcondvar_t)) +
    103      1.74     rmind 	    ALIGN(seminfo.semmnu * seminfo.semusz);
    104      1.56      yamt 	v = uvm_km_alloc(kernel_map, round_page(sz), 0,
    105      1.56      yamt 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    106      1.56      yamt 	if (v == 0)
    107      1.48  jdolecek 		panic("sysv_sem: cannot allocate memory");
    108      1.48  jdolecek 	sema = (void *)v;
    109      1.74     rmind 	sem = (void *)(ALIGN(sema) +
    110      1.74     rmind 	    seminfo.semmni * sizeof(struct semid_ds));
    111      1.74     rmind 	semcv = (void *)(ALIGN(sem) +
    112      1.74     rmind 	    seminfo.semmns * sizeof(struct __sem));
    113      1.74     rmind 	semu = (void *)(ALIGN(semcv) +
    114      1.74     rmind 	    seminfo.semmni * sizeof(kcondvar_t));
    115       1.5   mycroft 
    116       1.5   mycroft 	for (i = 0; i < seminfo.semmni; i++) {
    117      1.33   thorpej 		sema[i]._sem_base = 0;
    118       1.5   mycroft 		sema[i].sem_perm.mode = 0;
    119      1.70        ad 		cv_init(&semcv[i], "semwait");
    120       1.5   mycroft 	}
    121       1.5   mycroft 	for (i = 0; i < seminfo.semmnu; i++) {
    122      1.74     rmind 		struct sem_undo *suptr = SEMU(semu, i);
    123       1.5   mycroft 		suptr->un_proc = NULL;
    124       1.5   mycroft 	}
    125       1.5   mycroft 	semu_list = NULL;
    126      1.44  christos 	exithook_establish(semexit, NULL);
    127       1.1       cgd }
    128       1.1       cgd 
    129      1.74     rmind static int
    130      1.74     rmind semrealloc(int newsemmni, int newsemmns, int newsemmnu)
    131      1.74     rmind {
    132      1.74     rmind 	struct semid_ds *new_sema, *old_sema;
    133      1.74     rmind 	struct __sem *new_sem;
    134      1.74     rmind 	struct sem_undo *new_semu_list, *suptr, *nsuptr;
    135      1.74     rmind 	int *new_semu;
    136      1.74     rmind 	kcondvar_t *new_semcv;
    137      1.74     rmind 	vaddr_t v;
    138      1.74     rmind 	int i, j, lsemid, nmnus, sz;
    139      1.74     rmind 
    140      1.74     rmind 	if (newsemmni < 1 || newsemmns < 1 || newsemmnu < 1)
    141      1.74     rmind 		return EINVAL;
    142      1.74     rmind 
    143      1.74     rmind 	/* Allocate the wired memory for our structures */
    144      1.74     rmind 	sz = ALIGN(newsemmni * sizeof(struct semid_ds)) +
    145      1.74     rmind 	    ALIGN(newsemmns * sizeof(struct __sem)) +
    146      1.74     rmind 	    ALIGN(newsemmni * sizeof(kcondvar_t)) +
    147      1.74     rmind 	    ALIGN(newsemmnu * seminfo.semusz);
    148      1.74     rmind 	v = uvm_km_alloc(kernel_map, round_page(sz), 0,
    149      1.74     rmind 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    150      1.74     rmind 	if (v == 0)
    151      1.74     rmind 		return ENOMEM;
    152      1.74     rmind 
    153      1.74     rmind 	mutex_enter(&semlock);
    154      1.74     rmind 	if (sem_realloc_state) {
    155      1.74     rmind 		mutex_exit(&semlock);
    156      1.74     rmind 		uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
    157      1.74     rmind 		return EBUSY;
    158      1.74     rmind 	}
    159      1.74     rmind 	sem_realloc_state = true;
    160      1.74     rmind 	if (sem_waiters) {
    161      1.74     rmind 		/*
    162      1.74     rmind 		 * Mark reallocation state, wake-up all waiters,
    163      1.74     rmind 		 * and wait while they will all exit.
    164      1.74     rmind 		 */
    165      1.74     rmind 		for (i = 0; i < seminfo.semmni; i++)
    166      1.74     rmind 			cv_broadcast(&semcv[i]);
    167      1.74     rmind 		while (sem_waiters)
    168      1.74     rmind 			cv_wait(&sem_realloc_cv, &semlock);
    169      1.74     rmind 	}
    170      1.74     rmind 	old_sema = sema;
    171      1.74     rmind 
    172      1.74     rmind 	/* Get the number of last slot */
    173      1.74     rmind 	lsemid = 0;
    174      1.74     rmind 	for (i = 0; i < seminfo.semmni; i++)
    175      1.74     rmind 		if (sema[i].sem_perm.mode & SEM_ALLOC)
    176      1.74     rmind 			lsemid = i;
    177      1.74     rmind 
    178      1.74     rmind 	/* Get the number of currently used undo structures */
    179      1.74     rmind 	nmnus = 0;
    180      1.74     rmind 	for (i = 0; i < seminfo.semmnu; i++) {
    181      1.74     rmind 		suptr = SEMU(semu, i);
    182      1.74     rmind 		if (suptr->un_proc == NULL)
    183      1.74     rmind 			continue;
    184      1.74     rmind 		nmnus++;
    185      1.74     rmind 	}
    186      1.74     rmind 
    187      1.74     rmind 	/* We cannot reallocate less memory than we use */
    188      1.74     rmind 	if (lsemid >= newsemmni || semtot > newsemmns || nmnus > newsemmnu) {
    189      1.74     rmind 		mutex_exit(&semlock);
    190      1.74     rmind 		uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
    191      1.74     rmind 		return EBUSY;
    192      1.74     rmind 	}
    193      1.74     rmind 
    194      1.74     rmind 	new_sema = (void *)v;
    195      1.74     rmind 	new_sem = (void *)(ALIGN(new_sema) +
    196      1.74     rmind 	    newsemmni * sizeof(struct semid_ds));
    197      1.74     rmind 	new_semcv = (void *)(ALIGN(new_sem) +
    198      1.74     rmind 	    newsemmns * sizeof(struct __sem));
    199      1.74     rmind 	new_semu = (void *)(ALIGN(new_semcv) +
    200      1.74     rmind 	    newsemmni * sizeof(kcondvar_t));
    201      1.74     rmind 
    202      1.74     rmind 	/* Initialize all semaphore identifiers and condvars */
    203      1.74     rmind 	for (i = 0; i < newsemmni; i++) {
    204      1.74     rmind 		new_sema[i]._sem_base = 0;
    205      1.74     rmind 		new_sema[i].sem_perm.mode = 0;
    206      1.74     rmind 		cv_init(&new_semcv[i], "semwait");
    207      1.74     rmind 	}
    208      1.74     rmind 	for (i = 0; i < newsemmnu; i++) {
    209      1.74     rmind 		nsuptr = SEMU(new_semu, i);
    210      1.74     rmind 		nsuptr->un_proc = NULL;
    211      1.74     rmind 	}
    212      1.74     rmind 
    213      1.74     rmind 	/*
    214      1.74     rmind 	 * Copy all identifiers, semaphores and list of the
    215      1.74     rmind 	 * undo structures to the new memory allocation.
    216      1.74     rmind 	 */
    217      1.74     rmind 	j = 0;
    218      1.74     rmind 	for (i = 0; i <= lsemid; i++) {
    219      1.74     rmind 		if ((sema[i].sem_perm.mode & SEM_ALLOC) == 0)
    220      1.74     rmind 			continue;
    221      1.74     rmind 		memcpy(&new_sema[i], &sema[i], sizeof(struct semid_ds));
    222      1.74     rmind 		new_sema[i]._sem_base = &new_sem[j];
    223      1.74     rmind 		memcpy(new_sema[i]._sem_base, sema[i]._sem_base,
    224      1.74     rmind 		    (sizeof(struct __sem) * sema[i].sem_nsems));
    225      1.74     rmind 		j += sema[i].sem_nsems;
    226      1.74     rmind 	}
    227      1.74     rmind 	KASSERT(j == semtot);
    228      1.74     rmind 
    229      1.74     rmind 	j = 0;
    230      1.74     rmind 	new_semu_list = NULL;
    231      1.74     rmind 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
    232      1.74     rmind 		KASSERT(j < newsemmnu);
    233      1.74     rmind 		nsuptr = SEMU(new_semu, j);
    234      1.74     rmind 		memcpy(nsuptr, suptr, SEMUSZ);
    235      1.74     rmind 		nsuptr->un_next = new_semu_list;
    236      1.74     rmind 		new_semu_list = nsuptr;
    237      1.74     rmind 		j++;
    238      1.74     rmind 	}
    239      1.74     rmind 
    240      1.74     rmind 	for (i = 0; i < seminfo.semmni; i++) {
    241      1.74     rmind 		KASSERT(cv_has_waiters(&semcv[i]) == false);
    242      1.74     rmind 		cv_destroy(&semcv[i]);
    243      1.74     rmind 	}
    244      1.74     rmind 
    245      1.74     rmind 	sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) +
    246      1.74     rmind 	    ALIGN(seminfo.semmns * sizeof(struct __sem)) +
    247      1.74     rmind 	    ALIGN(seminfo.semmni * sizeof(kcondvar_t)) +
    248      1.74     rmind 	    ALIGN(seminfo.semmnu * seminfo.semusz);
    249      1.74     rmind 
    250      1.74     rmind 	/* Set the pointers and update the new values */
    251      1.74     rmind 	sema = new_sema;
    252      1.74     rmind 	sem = new_sem;
    253      1.74     rmind 	semcv = new_semcv;
    254      1.74     rmind 	semu = new_semu;
    255      1.74     rmind 	semu_list = new_semu_list;
    256      1.74     rmind 
    257      1.74     rmind 	seminfo.semmni = newsemmni;
    258      1.74     rmind 	seminfo.semmns = newsemmns;
    259      1.74     rmind 	seminfo.semmnu = newsemmnu;
    260      1.74     rmind 
    261      1.74     rmind 	/* Reallocation completed - notify all waiters, if any */
    262      1.74     rmind 	sem_realloc_state = false;
    263      1.74     rmind 	cv_broadcast(&sem_realloc_cv);
    264      1.74     rmind 	mutex_exit(&semlock);
    265      1.74     rmind 
    266      1.74     rmind 	uvm_km_free(kernel_map, (vaddr_t)old_sema, sz, UVM_KMF_WIRED);
    267      1.74     rmind 	return 0;
    268      1.74     rmind }
    269      1.74     rmind 
    270       1.1       cgd /*
    271      1.37  sommerfe  * Placebo.
    272       1.1       cgd  */
    273       1.1       cgd 
    274       1.1       cgd int
    275      1.78       dsl sys_semconfig(struct lwp *l, const struct sys_semconfig_args *uap, register_t *retval)
    276      1.23   thorpej {
    277      1.51     enami 
    278       1.5   mycroft 	*retval = 0;
    279      1.37  sommerfe 	return 0;
    280       1.1       cgd }
    281       1.1       cgd 
    282       1.1       cgd /*
    283       1.1       cgd  * Allocate a new sem_undo structure for a process
    284       1.1       cgd  * (returns ptr to structure or NULL if no more room)
    285       1.1       cgd  */
    286       1.1       cgd 
    287       1.1       cgd struct sem_undo *
    288      1.59   thorpej semu_alloc(struct proc *p)
    289       1.1       cgd {
    290      1.35  augustss 	int i;
    291      1.35  augustss 	struct sem_undo *suptr;
    292      1.35  augustss 	struct sem_undo **supptr;
    293       1.5   mycroft 	int attempt;
    294       1.1       cgd 
    295      1.70        ad 	KASSERT(mutex_owned(&semlock));
    296      1.70        ad 
    297       1.1       cgd 	/*
    298       1.5   mycroft 	 * Try twice to allocate something.
    299       1.5   mycroft 	 * (we'll purge any empty structures after the first pass so
    300       1.5   mycroft 	 * two passes are always enough)
    301       1.1       cgd 	 */
    302       1.1       cgd 
    303       1.5   mycroft 	for (attempt = 0; attempt < 2; attempt++) {
    304       1.5   mycroft 		/*
    305       1.5   mycroft 		 * Look for a free structure.
    306       1.5   mycroft 		 * Fill it in and return it if we find one.
    307       1.5   mycroft 		 */
    308       1.5   mycroft 
    309       1.5   mycroft 		for (i = 0; i < seminfo.semmnu; i++) {
    310      1.74     rmind 			suptr = SEMU(semu, i);
    311       1.5   mycroft 			if (suptr->un_proc == NULL) {
    312       1.5   mycroft 				suptr->un_next = semu_list;
    313       1.5   mycroft 				semu_list = suptr;
    314       1.5   mycroft 				suptr->un_cnt = 0;
    315       1.5   mycroft 				suptr->un_proc = p;
    316      1.51     enami 				return (suptr);
    317       1.5   mycroft 			}
    318       1.5   mycroft 		}
    319       1.1       cgd 
    320       1.5   mycroft 		/*
    321       1.5   mycroft 		 * We didn't find a free one, if this is the first attempt
    322       1.5   mycroft 		 * then try to free some structures.
    323       1.5   mycroft 		 */
    324       1.5   mycroft 
    325       1.5   mycroft 		if (attempt == 0) {
    326       1.5   mycroft 			/* All the structures are in use - try to free some */
    327       1.5   mycroft 			int did_something = 0;
    328       1.5   mycroft 
    329       1.5   mycroft 			supptr = &semu_list;
    330       1.5   mycroft 			while ((suptr = *supptr) != NULL) {
    331       1.5   mycroft 				if (suptr->un_cnt == 0)  {
    332       1.5   mycroft 					suptr->un_proc = NULL;
    333       1.5   mycroft 					*supptr = suptr->un_next;
    334       1.5   mycroft 					did_something = 1;
    335       1.5   mycroft 				} else
    336      1.52     enami 					supptr = &suptr->un_next;
    337       1.5   mycroft 			}
    338       1.5   mycroft 
    339       1.5   mycroft 			/* If we didn't free anything then just give-up */
    340       1.5   mycroft 			if (!did_something)
    341      1.51     enami 				return (NULL);
    342       1.5   mycroft 		} else {
    343       1.5   mycroft 			/*
    344       1.5   mycroft 			 * The second pass failed even though we freed
    345       1.5   mycroft 			 * something after the first pass!
    346       1.5   mycroft 			 * This is IMPOSSIBLE!
    347       1.5   mycroft 			 */
    348       1.5   mycroft 			panic("semu_alloc - second attempt failed");
    349       1.5   mycroft 		}
    350       1.1       cgd 	}
    351      1.25  christos 	return NULL;
    352       1.1       cgd }
    353       1.1       cgd 
    354       1.1       cgd /*
    355       1.1       cgd  * Adjust a particular entry for a particular proc
    356       1.1       cgd  */
    357       1.1       cgd 
    358       1.1       cgd int
    359      1.59   thorpej semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
    360      1.59   thorpej     int adjval)
    361       1.1       cgd {
    362      1.35  augustss 	struct sem_undo *suptr;
    363      1.35  augustss 	struct undo *sunptr;
    364       1.5   mycroft 	int i;
    365       1.1       cgd 
    366      1.70        ad 	KASSERT(mutex_owned(&semlock));
    367      1.70        ad 
    368      1.51     enami 	/*
    369      1.51     enami 	 * Look for and remember the sem_undo if the caller doesn't
    370      1.51     enami 	 * provide it
    371      1.51     enami 	 */
    372       1.1       cgd 
    373       1.5   mycroft 	suptr = *supptr;
    374       1.4   mycroft 	if (suptr == NULL) {
    375      1.52     enami 		for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
    376      1.52     enami 			if (suptr->un_proc == p)
    377       1.5   mycroft 				break;
    378      1.52     enami 
    379       1.5   mycroft 		if (suptr == NULL) {
    380       1.5   mycroft 			suptr = semu_alloc(p);
    381       1.5   mycroft 			if (suptr == NULL)
    382      1.51     enami 				return (ENOSPC);
    383       1.5   mycroft 		}
    384      1.52     enami 		*supptr = suptr;
    385       1.1       cgd 	}
    386       1.1       cgd 
    387       1.6   mycroft 	/*
    388      1.51     enami 	 * Look for the requested entry and adjust it (delete if
    389      1.51     enami 	 * adjval becomes 0).
    390       1.6   mycroft 	 */
    391       1.6   mycroft 	sunptr = &suptr->un_ent[0];
    392       1.5   mycroft 	for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
    393       1.6   mycroft 		if (sunptr->un_id != semid || sunptr->un_num != semnum)
    394       1.6   mycroft 			continue;
    395      1.52     enami 		sunptr->un_adjval += adjval;
    396       1.6   mycroft 		if (sunptr->un_adjval == 0) {
    397       1.6   mycroft 			suptr->un_cnt--;
    398       1.6   mycroft 			if (i < suptr->un_cnt)
    399       1.6   mycroft 				suptr->un_ent[i] =
    400       1.6   mycroft 				    suptr->un_ent[suptr->un_cnt];
    401       1.5   mycroft 		}
    402      1.51     enami 		return (0);
    403       1.1       cgd 	}
    404       1.1       cgd 
    405       1.5   mycroft 	/* Didn't find the right entry - create it */
    406      1.11   mycroft 	if (suptr->un_cnt == SEMUME)
    407      1.51     enami 		return (EINVAL);
    408      1.11   mycroft 
    409      1.11   mycroft 	sunptr = &suptr->un_ent[suptr->un_cnt];
    410      1.11   mycroft 	suptr->un_cnt++;
    411      1.11   mycroft 	sunptr->un_adjval = adjval;
    412      1.11   mycroft 	sunptr->un_id = semid;
    413      1.11   mycroft 	sunptr->un_num = semnum;
    414      1.51     enami 	return (0);
    415       1.1       cgd }
    416       1.1       cgd 
    417       1.1       cgd void
    418      1.59   thorpej semundo_clear(int semid, int semnum)
    419       1.1       cgd {
    420      1.35  augustss 	struct sem_undo *suptr;
    421      1.52     enami 	struct undo *sunptr, *sunend;
    422       1.1       cgd 
    423      1.70        ad 	KASSERT(mutex_owned(&semlock));
    424      1.70        ad 
    425      1.52     enami 	for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
    426      1.52     enami 		for (sunptr = &suptr->un_ent[0],
    427      1.52     enami 		    sunend = sunptr + suptr->un_cnt; sunptr < sunend;) {
    428       1.6   mycroft 			if (sunptr->un_id == semid) {
    429       1.6   mycroft 				if (semnum == -1 || sunptr->un_num == semnum) {
    430       1.6   mycroft 					suptr->un_cnt--;
    431      1.52     enami 					sunend--;
    432      1.52     enami 					if (sunptr != sunend)
    433      1.52     enami 						*sunptr = *sunend;
    434      1.52     enami 					if (semnum != -1)
    435      1.52     enami 						break;
    436      1.52     enami 					else
    437      1.52     enami 						continue;
    438       1.6   mycroft 				}
    439       1.6   mycroft 			}
    440      1.52     enami 			sunptr++;
    441       1.6   mycroft 		}
    442       1.1       cgd }
    443       1.1       cgd 
    444       1.1       cgd int
    445      1.78       dsl sys_____semctl13(struct lwp *l, const struct sys_____semctl13_args *uap, register_t *retval)
    446      1.23   thorpej {
    447      1.78       dsl 	/* {
    448      1.10       cgd 		syscallarg(int) semid;
    449      1.10       cgd 		syscallarg(int) semnum;
    450      1.10       cgd 		syscallarg(int) cmd;
    451      1.34  christos 		syscallarg(union __semun *) arg;
    452      1.78       dsl 	} */
    453      1.33   thorpej 	struct semid_ds sembuf;
    454      1.33   thorpej 	int cmd, error;
    455      1.34  christos 	void *pass_arg;
    456      1.34  christos 	union __semun karg;
    457      1.33   thorpej 
    458      1.33   thorpej 	cmd = SCARG(uap, cmd);
    459      1.33   thorpej 
    460      1.69       dsl 	pass_arg = get_semctl_arg(cmd, &sembuf, &karg);
    461      1.33   thorpej 
    462      1.34  christos 	if (pass_arg) {
    463      1.34  christos 		error = copyin(SCARG(uap, arg), &karg, sizeof(karg));
    464      1.33   thorpej 		if (error)
    465      1.34  christos 			return error;
    466      1.34  christos 		if (cmd == IPC_SET) {
    467      1.34  christos 			error = copyin(karg.buf, &sembuf, sizeof(sembuf));
    468      1.34  christos 			if (error)
    469      1.34  christos 				return (error);
    470      1.34  christos 		}
    471      1.33   thorpej 	}
    472      1.33   thorpej 
    473      1.63        ad 	error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    474      1.33   thorpej 	    pass_arg, retval);
    475      1.33   thorpej 
    476      1.33   thorpej 	if (error == 0 && cmd == IPC_STAT)
    477      1.34  christos 		error = copyout(&sembuf, karg.buf, sizeof(sembuf));
    478      1.33   thorpej 
    479      1.33   thorpej 	return (error);
    480      1.33   thorpej }
    481      1.33   thorpej 
    482      1.33   thorpej int
    483      1.63        ad semctl1(struct lwp *l, int semid, int semnum, int cmd, void *v,
    484      1.59   thorpej     register_t *retval)
    485      1.33   thorpej {
    486      1.63        ad 	kauth_cred_t cred = l->l_cred;
    487      1.33   thorpej 	union __semun *arg = v;
    488      1.33   thorpej 	struct semid_ds *sembuf = v, *semaptr;
    489      1.33   thorpej 	int i, error, ix;
    490       1.1       cgd 
    491      1.27  christos 	SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
    492      1.33   thorpej 	    semid, semnum, cmd, v));
    493       1.1       cgd 
    494      1.70        ad 	mutex_enter(&semlock);
    495      1.70        ad 
    496      1.33   thorpej 	ix = IPCID_TO_IX(semid);
    497      1.70        ad 	if (ix < 0 || ix >= seminfo.semmni) {
    498      1.70        ad 		mutex_exit(&semlock);
    499      1.33   thorpej 		return (EINVAL);
    500      1.70        ad 	}
    501       1.6   mycroft 
    502      1.33   thorpej 	semaptr = &sema[ix];
    503       1.6   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    504      1.70        ad 	    semaptr->sem_perm._seq != IPCID_TO_SEQ(semid)) {
    505      1.70        ad 		mutex_exit(&semlock);
    506      1.33   thorpej 		return (EINVAL);
    507      1.70        ad 	}
    508       1.1       cgd 
    509       1.6   mycroft 	switch (cmd) {
    510       1.6   mycroft 	case IPC_RMID:
    511      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
    512      1.70        ad 			break;
    513      1.61      elad 		semaptr->sem_perm.cuid = kauth_cred_geteuid(cred);
    514      1.61      elad 		semaptr->sem_perm.uid = kauth_cred_geteuid(cred);
    515       1.6   mycroft 		semtot -= semaptr->sem_nsems;
    516      1.33   thorpej 		for (i = semaptr->_sem_base - sem; i < semtot; i++)
    517       1.6   mycroft 			sem[i] = sem[i + semaptr->sem_nsems];
    518       1.6   mycroft 		for (i = 0; i < seminfo.semmni; i++) {
    519       1.6   mycroft 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
    520      1.33   thorpej 			    sema[i]._sem_base > semaptr->_sem_base)
    521      1.33   thorpej 				sema[i]._sem_base -= semaptr->sem_nsems;
    522       1.6   mycroft 		}
    523       1.6   mycroft 		semaptr->sem_perm.mode = 0;
    524      1.33   thorpej 		semundo_clear(ix, -1);
    525      1.70        ad 		cv_broadcast(&semcv[ix]);
    526       1.6   mycroft 		break;
    527       1.1       cgd 
    528       1.6   mycroft 	case IPC_SET:
    529      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
    530      1.70        ad 			break;
    531      1.64  christos 		KASSERT(sembuf != NULL);
    532      1.33   thorpej 		semaptr->sem_perm.uid = sembuf->sem_perm.uid;
    533      1.33   thorpej 		semaptr->sem_perm.gid = sembuf->sem_perm.gid;
    534       1.6   mycroft 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
    535      1.33   thorpej 		    (sembuf->sem_perm.mode & 0777);
    536      1.62    kardel 		semaptr->sem_ctime = time_second;
    537       1.6   mycroft 		break;
    538       1.1       cgd 
    539       1.6   mycroft 	case IPC_STAT:
    540      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    541      1.70        ad 			break;
    542      1.64  christos 		KASSERT(sembuf != NULL);
    543      1.33   thorpej 		memcpy(sembuf, semaptr, sizeof(struct semid_ds));
    544      1.80     njoly 		sembuf->sem_perm.mode &= 0777;
    545       1.6   mycroft 		break;
    546       1.1       cgd 
    547       1.6   mycroft 	case GETNCNT:
    548      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    549      1.70        ad 			break;
    550      1.70        ad 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    551      1.70        ad 			error = EINVAL;
    552      1.70        ad 			break;
    553      1.70        ad 		}
    554      1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semncnt;
    555       1.6   mycroft 		break;
    556       1.1       cgd 
    557       1.6   mycroft 	case GETPID:
    558      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    559      1.70        ad 			break;
    560      1.70        ad 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    561      1.70        ad 			error = EINVAL;
    562      1.70        ad 			break;
    563      1.70        ad 		}
    564      1.33   thorpej 		*retval = semaptr->_sem_base[semnum].sempid;
    565       1.6   mycroft 		break;
    566       1.1       cgd 
    567       1.6   mycroft 	case GETVAL:
    568      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    569      1.70        ad 			break;
    570      1.70        ad 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    571      1.70        ad 			error = EINVAL;
    572      1.70        ad 			break;
    573      1.70        ad 		}
    574      1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semval;
    575       1.6   mycroft 		break;
    576       1.1       cgd 
    577       1.6   mycroft 	case GETALL:
    578      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    579      1.70        ad 			break;
    580      1.60  christos 		KASSERT(arg != NULL);
    581       1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    582      1.33   thorpej 			error = copyout(&semaptr->_sem_base[i].semval,
    583      1.33   thorpej 			    &arg->array[i], sizeof(arg->array[i]));
    584      1.33   thorpej 			if (error != 0)
    585       1.6   mycroft 				break;
    586       1.6   mycroft 		}
    587       1.6   mycroft 		break;
    588       1.1       cgd 
    589       1.6   mycroft 	case GETZCNT:
    590      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
    591      1.70        ad 			break;
    592      1.70        ad 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    593      1.70        ad 			error = EINVAL;
    594      1.70        ad 			break;
    595      1.70        ad 		}
    596      1.33   thorpej 		*retval = semaptr->_sem_base[semnum].semzcnt;
    597       1.6   mycroft 		break;
    598       1.1       cgd 
    599       1.6   mycroft 	case SETVAL:
    600      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    601      1.70        ad 			break;
    602      1.70        ad 		if (semnum < 0 || semnum >= semaptr->sem_nsems) {
    603      1.70        ad 			error = EINVAL;
    604      1.70        ad 			break;
    605      1.70        ad 		}
    606      1.60  christos 		KASSERT(arg != NULL);
    607      1.83     njoly 		if ((unsigned int)arg->val > seminfo.semvmx) {
    608      1.83     njoly 			error = ERANGE;
    609      1.83     njoly 			break;
    610      1.83     njoly 		}
    611      1.33   thorpej 		semaptr->_sem_base[semnum].semval = arg->val;
    612      1.33   thorpej 		semundo_clear(ix, semnum);
    613      1.70        ad 		cv_broadcast(&semcv[ix]);
    614       1.6   mycroft 		break;
    615       1.1       cgd 
    616       1.6   mycroft 	case SETALL:
    617      1.33   thorpej 		if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
    618      1.70        ad 			break;
    619      1.60  christos 		KASSERT(arg != NULL);
    620       1.6   mycroft 		for (i = 0; i < semaptr->sem_nsems; i++) {
    621      1.83     njoly 			unsigned short semval;
    622      1.83     njoly 			error = copyin(&arg->array[i], &semval,
    623      1.33   thorpej 			    sizeof(arg->array[i]));
    624      1.33   thorpej 			if (error != 0)
    625       1.6   mycroft 				break;
    626      1.83     njoly 			if ((unsigned int)semval > seminfo.semvmx) {
    627      1.83     njoly 				error = ERANGE;
    628      1.83     njoly 				break;
    629      1.83     njoly 			}
    630      1.83     njoly 			semaptr->_sem_base[i].semval = semval;
    631       1.6   mycroft 		}
    632      1.33   thorpej 		semundo_clear(ix, -1);
    633      1.70        ad 		cv_broadcast(&semcv[ix]);
    634       1.6   mycroft 		break;
    635       1.1       cgd 
    636       1.6   mycroft 	default:
    637      1.70        ad 		error = EINVAL;
    638      1.70        ad 		break;
    639       1.6   mycroft 	}
    640       1.4   mycroft 
    641      1.70        ad 	mutex_exit(&semlock);
    642      1.33   thorpej 	return (error);
    643       1.1       cgd }
    644       1.1       cgd 
    645       1.1       cgd int
    646      1.78       dsl sys_semget(struct lwp *l, const struct sys_semget_args *uap, register_t *retval)
    647      1.23   thorpej {
    648      1.78       dsl 	/* {
    649      1.10       cgd 		syscallarg(key_t) key;
    650      1.10       cgd 		syscallarg(int) nsems;
    651      1.10       cgd 		syscallarg(int) semflg;
    652      1.78       dsl 	} */
    653      1.70        ad 	int semid, error = 0;
    654      1.10       cgd 	int key = SCARG(uap, key);
    655      1.10       cgd 	int nsems = SCARG(uap, nsems);
    656      1.10       cgd 	int semflg = SCARG(uap, semflg);
    657      1.63        ad 	kauth_cred_t cred = l->l_cred;
    658       1.1       cgd 
    659      1.27  christos 	SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
    660       1.1       cgd 
    661      1.70        ad 	mutex_enter(&semlock);
    662      1.70        ad 
    663       1.6   mycroft 	if (key != IPC_PRIVATE) {
    664       1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    665       1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
    666      1.33   thorpej 			    sema[semid].sem_perm._key == key)
    667       1.6   mycroft 				break;
    668       1.6   mycroft 		}
    669       1.6   mycroft 		if (semid < seminfo.semmni) {
    670      1.27  christos 			SEM_PRINTF(("found public key\n"));
    671      1.70        ad 			if ((error = ipcperm(cred, &sema[semid].sem_perm,
    672       1.7   hpeyerl 			    semflg & 0700)))
    673      1.70        ad 			    	goto out;
    674       1.6   mycroft 			if (nsems > 0 && sema[semid].sem_nsems < nsems) {
    675      1.27  christos 				SEM_PRINTF(("too small\n"));
    676      1.70        ad 				error = EINVAL;
    677      1.70        ad 				goto out;
    678       1.6   mycroft 			}
    679       1.6   mycroft 			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
    680      1.27  christos 				SEM_PRINTF(("not exclusive\n"));
    681      1.70        ad 				error = EEXIST;
    682      1.70        ad 				goto out;
    683       1.6   mycroft 			}
    684       1.6   mycroft 			goto found;
    685       1.6   mycroft 		}
    686       1.6   mycroft 	}
    687       1.6   mycroft 
    688      1.27  christos 	SEM_PRINTF(("need to allocate the semid_ds\n"));
    689       1.6   mycroft 	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
    690       1.6   mycroft 		if (nsems <= 0 || nsems > seminfo.semmsl) {
    691      1.27  christos 			SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
    692      1.27  christos 			    seminfo.semmsl));
    693      1.70        ad 			error = EINVAL;
    694      1.70        ad 			goto out;
    695       1.6   mycroft 		}
    696       1.6   mycroft 		if (nsems > seminfo.semmns - semtot) {
    697      1.51     enami 			SEM_PRINTF(("not enough semaphores left "
    698      1.51     enami 			    "(need %d, got %d)\n",
    699      1.27  christos 			    nsems, seminfo.semmns - semtot));
    700      1.70        ad 			error = ENOSPC;
    701      1.70        ad 			goto out;
    702       1.6   mycroft 		}
    703       1.6   mycroft 		for (semid = 0; semid < seminfo.semmni; semid++) {
    704       1.6   mycroft 			if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
    705       1.6   mycroft 				break;
    706       1.6   mycroft 		}
    707       1.6   mycroft 		if (semid == seminfo.semmni) {
    708      1.27  christos 			SEM_PRINTF(("no more semid_ds's available\n"));
    709      1.70        ad 			error = ENOSPC;
    710      1.70        ad 			goto out;
    711       1.6   mycroft 		}
    712      1.27  christos 		SEM_PRINTF(("semid %d is available\n", semid));
    713      1.33   thorpej 		sema[semid].sem_perm._key = key;
    714      1.61      elad 		sema[semid].sem_perm.cuid = kauth_cred_geteuid(cred);
    715      1.61      elad 		sema[semid].sem_perm.uid = kauth_cred_geteuid(cred);
    716      1.61      elad 		sema[semid].sem_perm.cgid = kauth_cred_getegid(cred);
    717      1.61      elad 		sema[semid].sem_perm.gid = kauth_cred_getegid(cred);
    718       1.6   mycroft 		sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
    719      1.33   thorpej 		sema[semid].sem_perm._seq =
    720      1.33   thorpej 		    (sema[semid].sem_perm._seq + 1) & 0x7fff;
    721       1.6   mycroft 		sema[semid].sem_nsems = nsems;
    722       1.6   mycroft 		sema[semid].sem_otime = 0;
    723      1.62    kardel 		sema[semid].sem_ctime = time_second;
    724      1.33   thorpej 		sema[semid]._sem_base = &sem[semtot];
    725       1.6   mycroft 		semtot += nsems;
    726      1.33   thorpej 		memset(sema[semid]._sem_base, 0,
    727      1.51     enami 		    sizeof(sema[semid]._sem_base[0]) * nsems);
    728      1.33   thorpej 		SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid]._sem_base,
    729      1.27  christos 		    &sem[semtot]));
    730       1.1       cgd 	} else {
    731      1.27  christos 		SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
    732      1.70        ad 		error = ENOENT;
    733      1.70        ad 		goto out;
    734       1.1       cgd 	}
    735       1.1       cgd 
    736      1.70        ad  found:
    737       1.6   mycroft 	*retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
    738      1.70        ad  out:
    739      1.70        ad 	mutex_exit(&semlock);
    740      1.70        ad 	return (error);
    741       1.1       cgd }
    742       1.1       cgd 
    743      1.57       chs #define SMALL_SOPS 8
    744      1.57       chs 
    745       1.1       cgd int
    746      1.78       dsl sys_semop(struct lwp *l, const struct sys_semop_args *uap, register_t *retval)
    747      1.23   thorpej {
    748      1.78       dsl 	/* {
    749      1.10       cgd 		syscallarg(int) semid;
    750      1.10       cgd 		syscallarg(struct sembuf *) sops;
    751      1.29    kleink 		syscallarg(size_t) nsops;
    752      1.78       dsl 	} */
    753      1.45   thorpej 	struct proc *p = l->l_proc;
    754      1.52     enami 	int semid = SCARG(uap, semid), seq;
    755      1.41  jdolecek 	size_t nsops = SCARG(uap, nsops);
    756      1.57       chs 	struct sembuf small_sops[SMALL_SOPS];
    757      1.57       chs 	struct sembuf *sops;
    758      1.35  augustss 	struct semid_ds *semaptr;
    759      1.35  augustss 	struct sembuf *sopptr = NULL;
    760      1.35  augustss 	struct __sem *semptr = NULL;
    761       1.6   mycroft 	struct sem_undo *suptr = NULL;
    762      1.63        ad 	kauth_cred_t cred = l->l_cred;
    763      1.70        ad 	int i, error;
    764      1.25  christos 	int do_wakeup, do_undos;
    765       1.1       cgd 
    766      1.58  christos 	SEM_PRINTF(("call to semop(%d, %p, %zd)\n", semid, SCARG(uap,sops), nsops));
    767      1.81        ad 
    768      1.81        ad 	if (__predict_false((p->p_flag & PK_SYSVSEM) == 0)) {
    769      1.81        ad 		mutex_enter(p->p_lock);
    770      1.81        ad 		p->p_flag |= PK_SYSVSEM;
    771      1.81        ad 		mutex_exit(p->p_lock);
    772      1.81        ad 	}
    773      1.81        ad 
    774      1.76     rmind restart:
    775      1.70        ad 	if (nsops <= SMALL_SOPS) {
    776      1.70        ad 		sops = small_sops;
    777      1.71        ad 	} else if (nsops <= seminfo.semopm) {
    778      1.70        ad 		sops = kmem_alloc(nsops * sizeof(*sops), KM_SLEEP);
    779      1.71        ad 	} else {
    780      1.70        ad 		SEM_PRINTF(("too many sops (max=%d, nsops=%zd)\n",
    781      1.70        ad 		    seminfo.semopm, nsops));
    782      1.70        ad 		return (E2BIG);
    783      1.70        ad 	}
    784      1.70        ad 
    785      1.77        ad 	error = copyin(SCARG(uap, sops), sops, nsops * sizeof(sops[0]));
    786      1.77        ad 	if (error) {
    787      1.77        ad 		SEM_PRINTF(("error = %d from copyin(%p, %p, %zd)\n", error,
    788      1.77        ad 		    SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
    789      1.79        ad 		if (sops != small_sops)
    790      1.77        ad 			kmem_free(sops, nsops * sizeof(*sops));
    791      1.77        ad 		return error;
    792      1.77        ad 	}
    793      1.77        ad 
    794      1.70        ad 	mutex_enter(&semlock);
    795      1.76     rmind 	/* In case of reallocation, we will wait for completion */
    796      1.76     rmind 	while (__predict_false(sem_realloc_state))
    797      1.76     rmind 		cv_wait(&sem_realloc_cv, &semlock);
    798      1.70        ad 
    799       1.6   mycroft 	semid = IPCID_TO_IX(semid);	/* Convert back to zero origin */
    800      1.70        ad 	if (semid < 0 || semid >= seminfo.semmni) {
    801      1.70        ad 		error = EINVAL;
    802      1.70        ad 		goto out;
    803      1.70        ad 	}
    804       1.6   mycroft 
    805       1.6   mycroft 	semaptr = &sema[semid];
    806      1.52     enami 	seq = IPCID_TO_SEQ(SCARG(uap, semid));
    807      1.11   mycroft 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    808      1.70        ad 	    semaptr->sem_perm._seq != seq) {
    809      1.70        ad 		error = EINVAL;
    810      1.70        ad 		goto out;
    811       1.6   mycroft 	}
    812       1.1       cgd 
    813      1.70        ad 	if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
    814      1.70        ad 		SEM_PRINTF(("error = %d from ipaccess\n", error));
    815      1.70        ad 		goto out;
    816       1.6   mycroft 	}
    817       1.1       cgd 
    818      1.52     enami 	for (i = 0; i < nsops; i++)
    819      1.57       chs 		if (sops[i].sem_num >= semaptr->sem_nsems) {
    820      1.70        ad 			error = EFBIG;
    821      1.57       chs 			goto out;
    822      1.57       chs 		}
    823      1.52     enami 
    824      1.51     enami 	/*
    825       1.6   mycroft 	 * Loop trying to satisfy the vector of requests.
    826       1.6   mycroft 	 * If we reach a point where we must wait, any requests already
    827       1.6   mycroft 	 * performed are rolled back and we go to sleep until some other
    828       1.6   mycroft 	 * process wakes us up.  At this point, we start all over again.
    829       1.6   mycroft 	 *
    830       1.6   mycroft 	 * This ensures that from the perspective of other tasks, a set
    831       1.6   mycroft 	 * of requests is atomic (never partially satisfied).
    832       1.6   mycroft 	 */
    833       1.6   mycroft 	do_undos = 0;
    834       1.1       cgd 
    835       1.6   mycroft 	for (;;) {
    836       1.6   mycroft 		do_wakeup = 0;
    837       1.1       cgd 
    838       1.6   mycroft 		for (i = 0; i < nsops; i++) {
    839       1.6   mycroft 			sopptr = &sops[i];
    840      1.33   thorpej 			semptr = &semaptr->_sem_base[sopptr->sem_num];
    841       1.1       cgd 
    842      1.51     enami 			SEM_PRINTF(("semop:  semaptr=%p, sem_base=%p, "
    843      1.51     enami 			    "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
    844      1.33   thorpej 			    semaptr, semaptr->_sem_base, semptr,
    845       1.6   mycroft 			    sopptr->sem_num, semptr->semval, sopptr->sem_op,
    846      1.51     enami 			    (sopptr->sem_flg & IPC_NOWAIT) ?
    847      1.51     enami 			    "nowait" : "wait"));
    848       1.1       cgd 
    849       1.6   mycroft 			if (sopptr->sem_op < 0) {
    850      1.25  christos 				if ((int)(semptr->semval +
    851      1.51     enami 				    sopptr->sem_op) < 0) {
    852      1.51     enami 					SEM_PRINTF(("semop:  "
    853      1.51     enami 					    "can't do it now\n"));
    854       1.6   mycroft 					break;
    855       1.6   mycroft 				} else {
    856       1.6   mycroft 					semptr->semval += sopptr->sem_op;
    857       1.6   mycroft 					if (semptr->semval == 0 &&
    858       1.6   mycroft 					    semptr->semzcnt > 0)
    859       1.6   mycroft 						do_wakeup = 1;
    860       1.6   mycroft 				}
    861       1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    862       1.6   mycroft 					do_undos = 1;
    863       1.6   mycroft 			} else if (sopptr->sem_op == 0) {
    864       1.6   mycroft 				if (semptr->semval > 0) {
    865      1.27  christos 					SEM_PRINTF(("semop:  not zero now\n"));
    866       1.6   mycroft 					break;
    867       1.6   mycroft 				}
    868       1.6   mycroft 			} else {
    869       1.6   mycroft 				if (semptr->semncnt > 0)
    870       1.6   mycroft 					do_wakeup = 1;
    871       1.6   mycroft 				semptr->semval += sopptr->sem_op;
    872       1.6   mycroft 				if (sopptr->sem_flg & SEM_UNDO)
    873       1.6   mycroft 					do_undos = 1;
    874       1.6   mycroft 			}
    875       1.6   mycroft 		}
    876       1.1       cgd 
    877       1.6   mycroft 		/*
    878       1.6   mycroft 		 * Did we get through the entire vector?
    879       1.6   mycroft 		 */
    880       1.6   mycroft 		if (i >= nsops)
    881       1.6   mycroft 			goto done;
    882       1.1       cgd 
    883       1.6   mycroft 		/*
    884       1.6   mycroft 		 * No ... rollback anything that we've already done
    885       1.6   mycroft 		 */
    886      1.51     enami 		SEM_PRINTF(("semop:  rollback 0 through %d\n", i - 1));
    887      1.52     enami 		while (i-- > 0)
    888      1.52     enami 			semaptr->_sem_base[sops[i].sem_num].semval -=
    889      1.52     enami 			    sops[i].sem_op;
    890       1.1       cgd 
    891       1.6   mycroft 		/*
    892       1.6   mycroft 		 * If the request that we couldn't satisfy has the
    893       1.6   mycroft 		 * NOWAIT flag set then return with EAGAIN.
    894       1.6   mycroft 		 */
    895      1.57       chs 		if (sopptr->sem_flg & IPC_NOWAIT) {
    896      1.70        ad 			error = EAGAIN;
    897      1.57       chs 			goto out;
    898      1.57       chs 		}
    899       1.1       cgd 
    900       1.6   mycroft 		if (sopptr->sem_op == 0)
    901       1.6   mycroft 			semptr->semzcnt++;
    902       1.6   mycroft 		else
    903       1.6   mycroft 			semptr->semncnt++;
    904       1.1       cgd 
    905      1.74     rmind 		sem_waiters++;
    906      1.27  christos 		SEM_PRINTF(("semop:  good night!\n"));
    907      1.70        ad 		error = cv_wait_sig(&semcv[semid], &semlock);
    908      1.70        ad 		SEM_PRINTF(("semop:  good morning (error=%d)!\n", error));
    909      1.74     rmind 		sem_waiters--;
    910      1.74     rmind 
    911      1.76     rmind 		/* Notify reallocator, if it is waiting */
    912      1.76     rmind 		cv_broadcast(&sem_realloc_cv);
    913       1.1       cgd 
    914       1.6   mycroft 		/*
    915       1.6   mycroft 		 * Make sure that the semaphore still exists
    916       1.6   mycroft 		 */
    917       1.6   mycroft 		if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
    918      1.52     enami 		    semaptr->sem_perm._seq != seq) {
    919      1.70        ad 			error = EIDRM;
    920      1.57       chs 			goto out;
    921       1.6   mycroft 		}
    922       1.1       cgd 
    923       1.6   mycroft 		/*
    924       1.6   mycroft 		 * The semaphore is still alive.  Readjust the count of
    925       1.6   mycroft 		 * waiting processes.
    926       1.6   mycroft 		 */
    927      1.51     enami 		semptr = &semaptr->_sem_base[sopptr->sem_num];
    928       1.6   mycroft 		if (sopptr->sem_op == 0)
    929       1.6   mycroft 			semptr->semzcnt--;
    930       1.6   mycroft 		else
    931       1.6   mycroft 			semptr->semncnt--;
    932      1.74     rmind 
    933      1.76     rmind 		/* In case of such state, restart the call */
    934      1.76     rmind 		if (sem_realloc_state) {
    935      1.76     rmind 			mutex_exit(&semlock);
    936      1.76     rmind 			goto restart;
    937      1.76     rmind 		}
    938      1.76     rmind 
    939      1.74     rmind 		/* Is it really morning, or was our sleep interrupted? */
    940      1.76     rmind 		if (error != 0) {
    941      1.70        ad 			error = EINTR;
    942      1.57       chs 			goto out;
    943      1.57       chs 		}
    944      1.50  christos 		SEM_PRINTF(("semop:  good morning!\n"));
    945       1.6   mycroft 	}
    946       1.1       cgd 
    947       1.6   mycroft done:
    948       1.6   mycroft 	/*
    949       1.6   mycroft 	 * Process any SEM_UNDO requests.
    950       1.6   mycroft 	 */
    951       1.6   mycroft 	if (do_undos) {
    952       1.5   mycroft 		for (i = 0; i < nsops; i++) {
    953       1.6   mycroft 			/*
    954       1.6   mycroft 			 * We only need to deal with SEM_UNDO's for non-zero
    955       1.6   mycroft 			 * op's.
    956       1.6   mycroft 			 */
    957       1.6   mycroft 			int adjval;
    958       1.1       cgd 
    959       1.6   mycroft 			if ((sops[i].sem_flg & SEM_UNDO) == 0)
    960       1.6   mycroft 				continue;
    961       1.6   mycroft 			adjval = sops[i].sem_op;
    962       1.6   mycroft 			if (adjval == 0)
    963       1.6   mycroft 				continue;
    964      1.70        ad 			error = semundo_adjust(p, &suptr, semid,
    965       1.6   mycroft 			    sops[i].sem_num, -adjval);
    966      1.70        ad 			if (error == 0)
    967       1.6   mycroft 				continue;
    968       1.1       cgd 
    969       1.6   mycroft 			/*
    970       1.6   mycroft 			 * Oh-Oh!  We ran out of either sem_undo's or undo's.
    971       1.6   mycroft 			 * Rollback the adjustments to this point and then
    972       1.6   mycroft 			 * rollback the semaphore ups and down so we can return
    973       1.6   mycroft 			 * with an error with all structures restored.  We
    974       1.6   mycroft 			 * rollback the undo's in the exact reverse order that
    975       1.6   mycroft 			 * we applied them.  This guarantees that we won't run
    976       1.6   mycroft 			 * out of space as we roll things back out.
    977       1.6   mycroft 			 */
    978      1.52     enami 			while (i-- > 0) {
    979      1.52     enami 				if ((sops[i].sem_flg & SEM_UNDO) == 0)
    980       1.6   mycroft 					continue;
    981      1.52     enami 				adjval = sops[i].sem_op;
    982       1.6   mycroft 				if (adjval == 0)
    983       1.6   mycroft 					continue;
    984       1.6   mycroft 				if (semundo_adjust(p, &suptr, semid,
    985      1.52     enami 				    sops[i].sem_num, adjval) != 0)
    986       1.1       cgd 					panic("semop - can't undo undos");
    987       1.6   mycroft 			}
    988       1.1       cgd 
    989      1.54     enami 			for (i = 0; i < nsops; i++)
    990      1.54     enami 				semaptr->_sem_base[sops[i].sem_num].semval -=
    991      1.54     enami 				    sops[i].sem_op;
    992       1.1       cgd 
    993      1.70        ad 			SEM_PRINTF(("error = %d from semundo_adjust\n", error));
    994      1.57       chs 			goto out;
    995       1.1       cgd 		} /* loop through the sops */
    996       1.6   mycroft 	} /* if (do_undos) */
    997       1.1       cgd 
    998       1.6   mycroft 	/* We're definitely done - set the sempid's */
    999       1.6   mycroft 	for (i = 0; i < nsops; i++) {
   1000       1.1       cgd 		sopptr = &sops[i];
   1001      1.33   thorpej 		semptr = &semaptr->_sem_base[sopptr->sem_num];
   1002       1.1       cgd 		semptr->sempid = p->p_pid;
   1003       1.6   mycroft 	}
   1004       1.1       cgd 
   1005      1.55    briggs 	/* Update sem_otime */
   1006      1.62    kardel 	semaptr->sem_otime = time_second;
   1007      1.55    briggs 
   1008       1.6   mycroft 	/* Do a wakeup if any semaphore was up'd. */
   1009       1.6   mycroft 	if (do_wakeup) {
   1010      1.27  christos 		SEM_PRINTF(("semop:  doing wakeup\n"));
   1011      1.70        ad 		cv_broadcast(&semcv[semid]);
   1012      1.27  christos 		SEM_PRINTF(("semop:  back from wakeup\n"));
   1013       1.6   mycroft 	}
   1014      1.27  christos 	SEM_PRINTF(("semop:  done\n"));
   1015       1.6   mycroft 	*retval = 0;
   1016      1.57       chs 
   1017      1.70        ad  out:
   1018      1.70        ad 	mutex_exit(&semlock);
   1019      1.79        ad 	if (sops != small_sops)
   1020      1.70        ad 		kmem_free(sops, nsops * sizeof(*sops));
   1021      1.70        ad 	return error;
   1022       1.1       cgd }
   1023       1.1       cgd 
   1024       1.1       cgd /*
   1025      1.51     enami  * Go through the undo structures for this process and apply the
   1026      1.51     enami  * adjustments to semaphores.
   1027       1.1       cgd  */
   1028      1.44  christos /*ARGSUSED*/
   1029      1.25  christos void
   1030      1.66      yamt semexit(struct proc *p, void *v)
   1031       1.1       cgd {
   1032      1.35  augustss 	struct sem_undo *suptr;
   1033      1.35  augustss 	struct sem_undo **supptr;
   1034       1.1       cgd 
   1035      1.81        ad 	if ((p->p_flag & PK_SYSVSEM) == 0)
   1036      1.81        ad 		return;
   1037      1.81        ad 
   1038      1.70        ad 	mutex_enter(&semlock);
   1039      1.70        ad 
   1040       1.6   mycroft 	/*
   1041      1.51     enami 	 * Go through the chain of undo vectors looking for one
   1042      1.51     enami 	 * associated with this process.
   1043      1.17   mycroft 	 */
   1044      1.17   mycroft 
   1045      1.17   mycroft 	for (supptr = &semu_list; (suptr = *supptr) != NULL;
   1046      1.17   mycroft 	    supptr = &suptr->un_next) {
   1047      1.17   mycroft 		if (suptr->un_proc == p)
   1048      1.17   mycroft 			break;
   1049      1.17   mycroft 	}
   1050      1.17   mycroft 
   1051      1.17   mycroft 	/*
   1052      1.37  sommerfe 	 * If there is no undo vector, skip to the end.
   1053      1.14   mycroft 	 */
   1054      1.14   mycroft 
   1055      1.70        ad 	if (suptr == NULL) {
   1056      1.70        ad 		mutex_exit(&semlock);
   1057      1.37  sommerfe 		return;
   1058      1.70        ad 	}
   1059      1.51     enami 
   1060      1.14   mycroft 	/*
   1061      1.37  sommerfe 	 * We now have an undo vector for this process.
   1062      1.15   mycroft 	 */
   1063       1.1       cgd 
   1064      1.27  christos 	SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
   1065      1.27  christos 	    suptr->un_cnt));
   1066       1.1       cgd 
   1067       1.5   mycroft 	/*
   1068       1.5   mycroft 	 * If there are any active undo elements then process them.
   1069       1.5   mycroft 	 */
   1070       1.5   mycroft 	if (suptr->un_cnt > 0) {
   1071       1.6   mycroft 		int ix;
   1072       1.1       cgd 
   1073       1.6   mycroft 		for (ix = 0; ix < suptr->un_cnt; ix++) {
   1074       1.6   mycroft 			int semid = suptr->un_ent[ix].un_id;
   1075       1.6   mycroft 			int semnum = suptr->un_ent[ix].un_num;
   1076       1.6   mycroft 			int adjval = suptr->un_ent[ix].un_adjval;
   1077       1.6   mycroft 			struct semid_ds *semaptr;
   1078       1.6   mycroft 
   1079       1.6   mycroft 			semaptr = &sema[semid];
   1080       1.6   mycroft 			if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
   1081       1.6   mycroft 				panic("semexit - semid not allocated");
   1082       1.6   mycroft 			if (semnum >= semaptr->sem_nsems)
   1083       1.6   mycroft 				panic("semexit - semnum out of range");
   1084       1.6   mycroft 
   1085      1.51     enami 			SEM_PRINTF(("semexit:  %p id=%d num=%d(adj=%d) ; "
   1086      1.51     enami 			    "sem=%d\n",
   1087       1.6   mycroft 			    suptr->un_proc, suptr->un_ent[ix].un_id,
   1088       1.6   mycroft 			    suptr->un_ent[ix].un_num,
   1089       1.6   mycroft 			    suptr->un_ent[ix].un_adjval,
   1090      1.33   thorpej 			    semaptr->_sem_base[semnum].semval));
   1091       1.6   mycroft 
   1092      1.14   mycroft 			if (adjval < 0 &&
   1093      1.33   thorpej 			    semaptr->_sem_base[semnum].semval < -adjval)
   1094      1.33   thorpej 				semaptr->_sem_base[semnum].semval = 0;
   1095      1.14   mycroft 			else
   1096      1.33   thorpej 				semaptr->_sem_base[semnum].semval += adjval;
   1097       1.1       cgd 
   1098      1.70        ad 			cv_broadcast(&semcv[semid]);
   1099      1.27  christos 			SEM_PRINTF(("semexit:  back from wakeup\n"));
   1100       1.6   mycroft 		}
   1101       1.5   mycroft 	}
   1102       1.1       cgd 
   1103       1.5   mycroft 	/*
   1104       1.5   mycroft 	 * Deallocate the undo vector.
   1105       1.5   mycroft 	 */
   1106      1.27  christos 	SEM_PRINTF(("removing vector\n"));
   1107       1.5   mycroft 	suptr->un_proc = NULL;
   1108       1.5   mycroft 	*supptr = suptr->un_next;
   1109      1.70        ad 	mutex_exit(&semlock);
   1110       1.1       cgd }
   1111      1.74     rmind 
   1112      1.74     rmind /*
   1113      1.74     rmind  * Sysctl initialization and nodes.
   1114      1.74     rmind  */
   1115      1.74     rmind 
   1116      1.74     rmind static int
   1117      1.74     rmind sysctl_ipc_semmni(SYSCTLFN_ARGS)
   1118      1.74     rmind {
   1119      1.74     rmind 	int newsize, error;
   1120      1.74     rmind 	struct sysctlnode node;
   1121      1.74     rmind 	node = *rnode;
   1122      1.74     rmind 	node.sysctl_data = &newsize;
   1123      1.74     rmind 
   1124      1.74     rmind 	newsize = seminfo.semmni;
   1125      1.74     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1126      1.74     rmind 	if (error || newp == NULL)
   1127      1.74     rmind 		return error;
   1128      1.74     rmind 
   1129      1.74     rmind 	return semrealloc(newsize, seminfo.semmns, seminfo.semmnu);
   1130      1.74     rmind }
   1131      1.74     rmind 
   1132      1.74     rmind static int
   1133      1.74     rmind sysctl_ipc_semmns(SYSCTLFN_ARGS)
   1134      1.74     rmind {
   1135      1.74     rmind 	int newsize, error;
   1136      1.74     rmind 	struct sysctlnode node;
   1137      1.74     rmind 	node = *rnode;
   1138      1.74     rmind 	node.sysctl_data = &newsize;
   1139      1.74     rmind 
   1140      1.74     rmind 	newsize = seminfo.semmns;
   1141      1.74     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1142      1.74     rmind 	if (error || newp == NULL)
   1143      1.74     rmind 		return error;
   1144      1.74     rmind 
   1145      1.74     rmind 	return semrealloc(seminfo.semmni, newsize, seminfo.semmnu);
   1146      1.74     rmind }
   1147      1.74     rmind 
   1148      1.74     rmind static int
   1149      1.74     rmind sysctl_ipc_semmnu(SYSCTLFN_ARGS)
   1150      1.74     rmind {
   1151      1.74     rmind 	int newsize, error;
   1152      1.74     rmind 	struct sysctlnode node;
   1153      1.74     rmind 	node = *rnode;
   1154      1.74     rmind 	node.sysctl_data = &newsize;
   1155      1.74     rmind 
   1156      1.74     rmind 	newsize = seminfo.semmnu;
   1157      1.74     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1158      1.74     rmind 	if (error || newp == NULL)
   1159      1.74     rmind 		return error;
   1160      1.74     rmind 
   1161      1.74     rmind 	return semrealloc(seminfo.semmni, seminfo.semmns, newsize);
   1162      1.74     rmind }
   1163      1.74     rmind 
   1164      1.74     rmind SYSCTL_SETUP(sysctl_ipc_sem_setup, "sysctl kern.ipc subtree setup")
   1165      1.74     rmind {
   1166      1.74     rmind 	const struct sysctlnode *node = NULL;
   1167      1.74     rmind 
   1168      1.74     rmind 	sysctl_createv(clog, 0, NULL, NULL,
   1169      1.74     rmind 		CTLFLAG_PERMANENT,
   1170      1.74     rmind 		CTLTYPE_NODE, "kern", NULL,
   1171      1.74     rmind 		NULL, 0, NULL, 0,
   1172      1.74     rmind 		CTL_KERN, CTL_EOL);
   1173      1.74     rmind 	sysctl_createv(clog, 0, NULL, &node,
   1174      1.74     rmind 		CTLFLAG_PERMANENT,
   1175      1.74     rmind 		CTLTYPE_NODE, "ipc",
   1176      1.74     rmind 		SYSCTL_DESCR("SysV IPC options"),
   1177      1.74     rmind 		NULL, 0, NULL, 0,
   1178      1.74     rmind 		CTL_KERN, KERN_SYSVIPC, CTL_EOL);
   1179      1.74     rmind 
   1180      1.74     rmind 	if (node == NULL)
   1181      1.74     rmind 		return;
   1182      1.74     rmind 
   1183      1.74     rmind 	sysctl_createv(clog, 0, &node, NULL,
   1184      1.74     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1185      1.74     rmind 		CTLTYPE_INT, "semmni",
   1186      1.74     rmind 		SYSCTL_DESCR("Max number of number of semaphore identifiers"),
   1187      1.74     rmind 		sysctl_ipc_semmni, 0, &seminfo.semmni, 0,
   1188      1.74     rmind 		CTL_CREATE, CTL_EOL);
   1189      1.74     rmind 	sysctl_createv(clog, 0, &node, NULL,
   1190      1.74     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1191      1.74     rmind 		CTLTYPE_INT, "semmns",
   1192      1.74     rmind 		SYSCTL_DESCR("Max number of number of semaphores in system"),
   1193      1.74     rmind 		sysctl_ipc_semmns, 0, &seminfo.semmns, 0,
   1194      1.74     rmind 		CTL_CREATE, CTL_EOL);
   1195      1.74     rmind 	sysctl_createv(clog, 0, &node, NULL,
   1196      1.74     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1197      1.74     rmind 		CTLTYPE_INT, "semmnu",
   1198      1.74     rmind 		SYSCTL_DESCR("Max number of undo structures in system"),
   1199      1.74     rmind 		sysctl_ipc_semmnu, 0, &seminfo.semmnu, 0,
   1200      1.74     rmind 		CTL_CREATE, CTL_EOL);
   1201      1.74     rmind }
   1202