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