Home | History | Annotate | Line # | Download | only in kern
uipc_sem.c revision 1.8.2.4
      1  1.8.2.4     skrll /*	$NetBSD: uipc_sem.c,v 1.8.2.4 2004/09/18 14:53:04 skrll Exp $	*/
      2      1.3   thorpej 
      3      1.3   thorpej /*-
      4      1.3   thorpej  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5      1.3   thorpej  * All rights reserved.
      6      1.3   thorpej  *
      7      1.3   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.3   thorpej  * by Jason R. Thorpe of Wasabi Systems, Inc.
      9      1.3   thorpej  *
     10      1.3   thorpej  * Redistribution and use in source and binary forms, with or without
     11      1.3   thorpej  * modification, are permitted provided that the following conditions
     12      1.3   thorpej  * are met:
     13      1.3   thorpej  * 1. Redistributions of source code must retain the above copyright
     14      1.3   thorpej  *    notice, this list of conditions and the following disclaimer.
     15      1.3   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.3   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17      1.3   thorpej  *    documentation and/or other materials provided with the distribution.
     18      1.3   thorpej  * 3. All advertising materials mentioning features or use of this software
     19      1.3   thorpej  *    must display the following acknowledgement:
     20      1.3   thorpej  *        This product includes software developed by the NetBSD
     21      1.3   thorpej  *        Foundation, Inc. and its contributors.
     22      1.3   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.3   thorpej  *    contributors may be used to endorse or promote products derived
     24      1.3   thorpej  *    from this software without specific prior written permission.
     25      1.3   thorpej  *
     26      1.3   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.3   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.3   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.3   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.3   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.3   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.3   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.3   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.3   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.3   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.3   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37      1.3   thorpej  */
     38      1.1  christos 
     39      1.1  christos /*
     40      1.1  christos  * Copyright (c) 2002 Alfred Perlstein <alfred (at) FreeBSD.org>
     41      1.1  christos  * All rights reserved.
     42      1.1  christos  *
     43      1.1  christos  * Redistribution and use in source and binary forms, with or without
     44      1.1  christos  * modification, are permitted provided that the following conditions
     45      1.1  christos  * are met:
     46      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     47      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     48      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     49      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     50      1.1  christos  *    documentation and/or other materials provided with the distribution.
     51      1.1  christos  *
     52      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55      1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62      1.1  christos  * SUCH DAMAGE.
     63      1.1  christos  */
     64      1.1  christos 
     65  1.8.2.2     skrll #include <sys/cdefs.h>
     66  1.8.2.4     skrll __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.8.2.4 2004/09/18 14:53:04 skrll Exp $");
     67  1.8.2.2     skrll 
     68      1.1  christos #include "opt_posix.h"
     69      1.1  christos 
     70      1.1  christos #include <sys/param.h>
     71      1.1  christos #include <sys/systm.h>
     72      1.1  christos #include <sys/kernel.h>
     73      1.1  christos #include <sys/proc.h>
     74      1.1  christos #include <sys/lock.h>
     75      1.1  christos #include <sys/ksem.h>
     76      1.5      matt #include <sys/sa.h>
     77      1.1  christos #include <sys/syscall.h>
     78      1.1  christos #include <sys/stat.h>
     79      1.1  christos #include <sys/malloc.h>
     80      1.1  christos #include <sys/fcntl.h>
     81      1.1  christos 
     82      1.1  christos #include <sys/mount.h>
     83      1.1  christos 
     84      1.1  christos #include <sys/syscallargs.h>
     85      1.1  christos 
     86      1.1  christos #ifndef SEM_MAX
     87      1.1  christos #define SEM_MAX	30
     88      1.1  christos #endif
     89      1.1  christos 
     90      1.1  christos #define SEM_MAX_NAMELEN	14
     91      1.1  christos #define SEM_VALUE_MAX (~0U)
     92      1.1  christos 
     93      1.1  christos #define SEM_TO_ID(x)	((intptr_t)(x))
     94      1.4   thorpej 
     95      1.4   thorpej MALLOC_DEFINE(M_SEM, "p1003_1b_sem", "p1003_1b semaphores");
     96      1.1  christos 
     97      1.3   thorpej /*
     98      1.3   thorpej  * Note: to read the ks_name member, you need either the ks_interlock
     99      1.3   thorpej  * or the ksem_slock.  To write the ks_name member, you need both.  Make
    100      1.3   thorpej  * sure the order is ksem_slock -> ks_interlock.
    101      1.3   thorpej  */
    102      1.1  christos struct ksem {
    103      1.1  christos 	LIST_ENTRY(ksem) ks_entry;	/* global list entry */
    104      1.3   thorpej 	struct simplelock ks_interlock;	/* lock on this ksem */
    105      1.1  christos 	char *ks_name;			/* if named, this is the name */
    106      1.3   thorpej 	unsigned int ks_ref;		/* number of references */
    107      1.1  christos 	mode_t ks_mode;			/* protection bits */
    108      1.1  christos 	uid_t ks_uid;			/* creator uid */
    109      1.1  christos 	gid_t ks_gid;			/* creator gid */
    110      1.1  christos 	unsigned int ks_value;		/* current value */
    111      1.3   thorpej 	unsigned int ks_waiters;	/* number of waiters */
    112      1.3   thorpej };
    113      1.3   thorpej 
    114      1.3   thorpej struct ksem_ref {
    115      1.3   thorpej 	LIST_ENTRY(ksem_ref) ksr_list;
    116      1.3   thorpej 	struct ksem *ksr_ksem;
    117      1.3   thorpej };
    118      1.3   thorpej 
    119      1.3   thorpej struct ksem_proc {
    120      1.3   thorpej 	struct lock kp_lock;
    121      1.3   thorpej 	LIST_HEAD(, ksem_ref) kp_ksems;
    122      1.1  christos };
    123      1.1  christos 
    124      1.1  christos /*
    125      1.3   thorpej  * ksem_slock protects ksem_head and nsems.  Only named semaphores go
    126      1.3   thorpej  * onto ksem_head.
    127      1.1  christos  */
    128      1.1  christos static struct simplelock ksem_slock;
    129      1.3   thorpej static LIST_HEAD(, ksem) ksem_head = LIST_HEAD_INITIALIZER(&ksem_head);
    130      1.3   thorpej static int nsems = 0;
    131      1.1  christos 
    132      1.3   thorpej static void
    133      1.3   thorpej ksem_free(struct ksem *ks)
    134      1.3   thorpej {
    135      1.1  christos 
    136      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    137      1.3   thorpej 	/*
    138      1.3   thorpej 	 * If the ksem is anonymous (or has been unlinked), then
    139      1.3   thorpej 	 * this is the end if its life.
    140      1.3   thorpej 	 */
    141      1.3   thorpej 	if (ks->ks_name == NULL) {
    142      1.3   thorpej 		simple_unlock(&ks->ks_interlock);
    143      1.3   thorpej 		free(ks, M_SEM);
    144      1.1  christos 
    145      1.3   thorpej 		simple_lock(&ksem_slock);
    146      1.3   thorpej 		nsems--;
    147      1.3   thorpej 		simple_unlock(&ksem_slock);
    148      1.3   thorpej 		return;
    149      1.3   thorpej 	}
    150      1.3   thorpej 	simple_unlock(&ks->ks_interlock);
    151      1.3   thorpej }
    152      1.1  christos 
    153      1.1  christos static __inline void
    154      1.3   thorpej ksem_addref(struct ksem *ks)
    155      1.1  christos {
    156      1.1  christos 
    157      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    158      1.1  christos 	ks->ks_ref++;
    159      1.3   thorpej 	KASSERT(ks->ks_ref != 0);	/* XXX KDASSERT */
    160      1.1  christos }
    161      1.1  christos 
    162      1.1  christos static __inline void
    163      1.3   thorpej ksem_delref(struct ksem *ks)
    164      1.1  christos {
    165      1.1  christos 
    166      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    167      1.3   thorpej 	KASSERT(ks->ks_ref != 0);	/* XXX KDASSERT */
    168      1.3   thorpej 	if (--ks->ks_ref == 0) {
    169      1.1  christos 		ksem_free(ks);
    170      1.3   thorpej 		return;
    171      1.3   thorpej 	}
    172      1.3   thorpej 	simple_unlock(&ks->ks_interlock);
    173      1.3   thorpej }
    174      1.3   thorpej 
    175      1.3   thorpej static struct ksem_proc *
    176      1.3   thorpej ksem_proc_alloc(void)
    177      1.3   thorpej {
    178      1.3   thorpej 	struct ksem_proc *kp;
    179      1.3   thorpej 
    180      1.3   thorpej 	kp = malloc(sizeof(*kp), M_SEM, M_WAITOK);
    181      1.3   thorpej 	lockinit(&kp->kp_lock, PWAIT, "ksproc", 0, 0);
    182      1.3   thorpej 	LIST_INIT(&kp->kp_ksems);
    183      1.3   thorpej 
    184      1.3   thorpej 	return (kp);
    185      1.1  christos }
    186      1.1  christos 
    187      1.3   thorpej static void
    188      1.3   thorpej ksem_add_proc(struct proc *p, struct ksem *ks)
    189      1.3   thorpej {
    190      1.3   thorpej 	struct ksem_proc *kp;
    191      1.3   thorpej 	struct ksem_ref *ksr;
    192      1.3   thorpej 
    193      1.3   thorpej 	if (p->p_ksems == NULL) {
    194      1.3   thorpej 		kp = ksem_proc_alloc();
    195      1.3   thorpej 		p->p_ksems = kp;
    196      1.3   thorpej 	} else
    197      1.3   thorpej 		kp = p->p_ksems;
    198      1.3   thorpej 
    199      1.3   thorpej 	ksr = malloc(sizeof(*ksr), M_SEM, M_WAITOK);
    200      1.3   thorpej 	ksr->ksr_ksem = ks;
    201      1.3   thorpej 
    202      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_EXCLUSIVE, NULL);
    203      1.3   thorpej 	LIST_INSERT_HEAD(&kp->kp_ksems, ksr, ksr_list);
    204      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    205      1.3   thorpej }
    206      1.3   thorpej 
    207      1.3   thorpej /* We MUST have a write lock on the ksem_proc list! */
    208      1.3   thorpej static struct ksem_ref *
    209      1.3   thorpej ksem_drop_proc(struct ksem_proc *kp, struct ksem *ks)
    210      1.1  christos {
    211      1.3   thorpej 	struct ksem_ref *ksr;
    212      1.1  christos 
    213      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    214      1.3   thorpej 	LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
    215      1.3   thorpej 		if (ksr->ksr_ksem == ks) {
    216      1.3   thorpej 			ksem_delref(ks);
    217      1.3   thorpej 			LIST_REMOVE(ksr, ksr_list);
    218      1.3   thorpej 			return (ksr);
    219      1.3   thorpej 		}
    220      1.1  christos 	}
    221      1.3   thorpej #ifdef DIAGNOSTIC
    222      1.3   thorpej 	panic("ksem_drop_proc: ksem_proc %p ksem %p", kp, ks);
    223      1.3   thorpej #endif
    224      1.1  christos 	return (NULL);
    225      1.1  christos }
    226      1.1  christos 
    227      1.3   thorpej static int
    228      1.3   thorpej ksem_perm(struct proc *p, struct ksem *ks)
    229      1.3   thorpej {
    230      1.3   thorpej 	struct ucred *uc;
    231      1.3   thorpej 
    232      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    233      1.3   thorpej 	uc = p->p_ucred;
    234      1.3   thorpej 	if ((uc->cr_uid == ks->ks_uid && (ks->ks_mode & S_IWUSR) != 0) ||
    235      1.3   thorpej 	    (uc->cr_gid == ks->ks_gid && (ks->ks_mode & S_IWGRP) != 0) ||
    236      1.3   thorpej 	    (ks->ks_mode & S_IWOTH) != 0 || suser(uc, &p->p_acflag) == 0)
    237      1.3   thorpej 		return (0);
    238      1.3   thorpej 	return (EPERM);
    239      1.3   thorpej }
    240      1.3   thorpej 
    241      1.1  christos static struct ksem *
    242      1.3   thorpej ksem_lookup_byname(const char *name)
    243      1.1  christos {
    244      1.1  christos 	struct ksem *ks;
    245      1.1  christos 
    246      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ksem_slock));
    247      1.3   thorpej 	LIST_FOREACH(ks, &ksem_head, ks_entry) {
    248      1.3   thorpej 		if (strcmp(ks->ks_name, name) == 0) {
    249      1.3   thorpej 			simple_lock(&ks->ks_interlock);
    250      1.1  christos 			return (ks);
    251      1.3   thorpej 		}
    252      1.3   thorpej 	}
    253      1.1  christos 	return (NULL);
    254      1.1  christos }
    255      1.1  christos 
    256      1.1  christos static int
    257      1.3   thorpej ksem_create(struct proc *p, const char *name, struct ksem **ksret,
    258      1.3   thorpej     mode_t mode, unsigned int value)
    259      1.1  christos {
    260      1.1  christos 	struct ksem *ret;
    261      1.1  christos 	struct ucred *uc;
    262      1.1  christos 	size_t len;
    263      1.1  christos 
    264      1.1  christos 	uc = p->p_ucred;
    265      1.1  christos 	if (value > SEM_VALUE_MAX)
    266      1.1  christos 		return (EINVAL);
    267      1.1  christos 	ret = malloc(sizeof(*ret), M_SEM, M_WAITOK | M_ZERO);
    268      1.1  christos 	if (name != NULL) {
    269      1.1  christos 		len = strlen(name);
    270      1.1  christos 		if (len > SEM_MAX_NAMELEN) {
    271      1.1  christos 			free(ret, M_SEM);
    272      1.1  christos 			return (ENAMETOOLONG);
    273      1.1  christos 		}
    274      1.1  christos 		/* name must start with a '/' but not contain one. */
    275      1.1  christos 		if (*name != '/' || len < 2 || strchr(name + 1, '/') != NULL) {
    276      1.1  christos 			free(ret, M_SEM);
    277      1.1  christos 			return (EINVAL);
    278      1.1  christos 		}
    279      1.1  christos 		ret->ks_name = malloc(len + 1, M_SEM, M_WAITOK);
    280      1.6    itojun 		strlcpy(ret->ks_name, name, len + 1);
    281      1.3   thorpej 	} else
    282      1.1  christos 		ret->ks_name = NULL;
    283      1.1  christos 	ret->ks_mode = mode;
    284      1.1  christos 	ret->ks_value = value;
    285      1.1  christos 	ret->ks_ref = 1;
    286      1.1  christos 	ret->ks_waiters = 0;
    287      1.1  christos 	ret->ks_uid = uc->cr_uid;
    288      1.1  christos 	ret->ks_gid = uc->cr_gid;
    289      1.3   thorpej 	simple_lock_init(&ret->ks_interlock);
    290      1.3   thorpej 
    291      1.1  christos 	simple_lock(&ksem_slock);
    292      1.1  christos 	if (nsems >= SEM_MAX) {
    293      1.3   thorpej 		simple_unlock(&ksem_slock);
    294      1.3   thorpej 		if (ret->ks_name != NULL)
    295      1.3   thorpej 			free(ret->ks_name, M_SEM);
    296      1.3   thorpej 		free(ret, M_SEM);
    297      1.3   thorpej 		return (ENFILE);
    298      1.1  christos 	}
    299      1.3   thorpej 	nsems++;
    300      1.1  christos 	simple_unlock(&ksem_slock);
    301      1.3   thorpej 
    302      1.3   thorpej 	*ksret = ret;
    303      1.3   thorpej 	return (0);
    304      1.1  christos }
    305      1.1  christos 
    306      1.1  christos int
    307      1.2  christos sys__ksem_init(struct lwp *l, void *v, register_t *retval)
    308      1.1  christos {
    309      1.2  christos 	struct sys__ksem_init_args /* {
    310      1.1  christos 		unsigned int value;
    311      1.1  christos 		semid_t *idp;
    312      1.1  christos 	} */ *uap = v;
    313      1.1  christos 	struct ksem *ks;
    314      1.1  christos 	semid_t id;
    315      1.1  christos 	int error;
    316      1.1  christos 
    317      1.3   thorpej 	/* Note the mode does not matter for anonymous semaphores. */
    318      1.3   thorpej 	error = ksem_create(l->l_proc, NULL, &ks, 0, SCARG(uap, value));
    319      1.1  christos 	if (error)
    320      1.1  christos 		return (error);
    321      1.1  christos 	id = SEM_TO_ID(ks);
    322      1.1  christos 	error = copyout(&id, SCARG(uap, idp), sizeof(id));
    323      1.1  christos 	if (error) {
    324      1.3   thorpej 		simple_lock(&ks->ks_interlock);
    325      1.3   thorpej 		ksem_delref(ks);
    326      1.1  christos 		return (error);
    327      1.1  christos 	}
    328      1.3   thorpej 
    329      1.3   thorpej 	ksem_add_proc(l->l_proc, ks);
    330      1.3   thorpej 
    331      1.3   thorpej 	return (0);
    332      1.1  christos }
    333      1.1  christos 
    334      1.1  christos int
    335      1.2  christos sys__ksem_open(struct lwp *l, void *v, register_t *retval)
    336      1.1  christos {
    337      1.2  christos 	struct sys__ksem_open_args /* {
    338      1.1  christos 		const char *name;
    339      1.1  christos 		int oflag;
    340      1.1  christos 		mode_t mode;
    341      1.1  christos 		unsigned int value;
    342      1.1  christos 		semid_t *idp;
    343      1.1  christos 	} */ *uap = v;
    344      1.1  christos 	char name[SEM_MAX_NAMELEN + 1];
    345      1.1  christos 	size_t done;
    346      1.1  christos 	int error;
    347      1.1  christos 	struct ksem *ksnew, *ks;
    348      1.1  christos 	semid_t id;
    349      1.1  christos 
    350      1.1  christos 	error = copyinstr(SCARG(uap, name), name, sizeof(name), &done);
    351      1.1  christos 	if (error)
    352      1.1  christos 		return (error);
    353      1.1  christos 
    354      1.1  christos 	ksnew = NULL;
    355      1.1  christos 	simple_lock(&ksem_slock);
    356      1.1  christos 	ks = ksem_lookup_byname(name);
    357      1.3   thorpej 
    358      1.3   thorpej 	/* Found one? */
    359      1.3   thorpej 	if (ks != NULL) {
    360      1.3   thorpej 		/* Check for exclusive create. */
    361      1.3   thorpej 		if (SCARG(uap, oflag) & O_EXCL) {
    362      1.3   thorpej 			simple_unlock(&ks->ks_interlock);
    363      1.1  christos 			simple_unlock(&ksem_slock);
    364      1.3   thorpej 			return (EEXIST);
    365      1.1  christos 		}
    366      1.3   thorpej  found_one:
    367      1.1  christos 		/*
    368      1.3   thorpej 		 * Verify permissions.  If we can access it, add
    369      1.3   thorpej 		 * this process's reference.
    370      1.1  christos 		 */
    371      1.3   thorpej 		LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    372      1.1  christos 		error = ksem_perm(l->l_proc, ks);
    373      1.3   thorpej 		if (error == 0)
    374      1.3   thorpej 			ksem_addref(ks);
    375      1.3   thorpej 		simple_unlock(&ks->ks_interlock);
    376      1.1  christos 		simple_unlock(&ksem_slock);
    377      1.1  christos 		if (error)
    378      1.1  christos 			return (error);
    379      1.3   thorpej 
    380      1.1  christos 		id = SEM_TO_ID(ks);
    381      1.1  christos 		error = copyout(&id, SCARG(uap, idp), sizeof(id));
    382      1.1  christos 		if (error) {
    383      1.3   thorpej 			simple_lock(&ks->ks_interlock);
    384      1.3   thorpej 			ksem_delref(ks);
    385      1.1  christos 			return (error);
    386      1.1  christos 		}
    387      1.3   thorpej 
    388      1.3   thorpej 		ksem_add_proc(l->l_proc, ks);
    389      1.3   thorpej 
    390      1.3   thorpej 		return (0);
    391      1.3   thorpej 	}
    392      1.3   thorpej 
    393      1.3   thorpej 	/*
    394      1.3   thorpej 	 * didn't ask for creation? error.
    395      1.3   thorpej 	 */
    396      1.3   thorpej 	if ((SCARG(uap, oflag) & O_CREAT) == 0) {
    397      1.1  christos 		simple_unlock(&ksem_slock);
    398      1.3   thorpej 		return (ENOENT);
    399      1.1  christos 	}
    400      1.1  christos 
    401      1.3   thorpej 	/*
    402      1.3   thorpej 	 * We may block during creation, so drop the lock.
    403      1.3   thorpej 	 */
    404      1.3   thorpej 	simple_unlock(&ksem_slock);
    405      1.3   thorpej 	error = ksem_create(l->l_proc, name, &ksnew, SCARG(uap, mode),
    406      1.3   thorpej 	    SCARG(uap, value));
    407      1.3   thorpej 	if (error != 0)
    408      1.3   thorpej 		return (error);
    409      1.3   thorpej 
    410      1.3   thorpej 	id = SEM_TO_ID(ksnew);
    411      1.3   thorpej 	error = copyout(&id, SCARG(uap, idp), sizeof(id));
    412      1.3   thorpej 	if (error) {
    413      1.3   thorpej 		free(ksnew->ks_name, M_SEM);
    414      1.3   thorpej 		ksnew->ks_name = NULL;
    415      1.1  christos 
    416      1.3   thorpej 		simple_lock(&ksnew->ks_interlock);
    417      1.3   thorpej 		ksem_delref(ksnew);
    418      1.3   thorpej 		return (error);
    419      1.3   thorpej 	}
    420      1.1  christos 
    421      1.3   thorpej 	/*
    422      1.3   thorpej 	 * We need to make sure we haven't lost a race while
    423      1.3   thorpej 	 * allocating during creation.
    424      1.3   thorpej 	 */
    425      1.3   thorpej 	simple_lock(&ksem_slock);
    426      1.3   thorpej 	if ((ks = ksem_lookup_byname(name)) != NULL) {
    427      1.3   thorpej 		if (SCARG(uap, oflag) & O_EXCL) {
    428      1.3   thorpej 			simple_unlock(&ks->ks_interlock);
    429      1.3   thorpej 			simple_unlock(&ksem_slock);
    430      1.1  christos 
    431      1.3   thorpej 			free(ksnew->ks_name, M_SEM);
    432      1.3   thorpej 			ksnew->ks_name = NULL;
    433      1.1  christos 
    434      1.3   thorpej 			simple_lock(&ksnew->ks_interlock);
    435      1.3   thorpej 			ksem_delref(ksnew);
    436      1.3   thorpej 			return (EEXIST);
    437      1.3   thorpej 		}
    438      1.3   thorpej 		goto found_one;
    439      1.3   thorpej 	} else {
    440      1.3   thorpej 		/* ksnew already has its initial reference. */
    441      1.3   thorpej 		LIST_INSERT_HEAD(&ksem_head, ksnew, ks_entry);
    442      1.3   thorpej 		simple_unlock(&ksem_slock);
    443      1.1  christos 
    444      1.3   thorpej 		ksem_add_proc(l->l_proc, ksnew);
    445      1.1  christos 	}
    446      1.3   thorpej 	return (error);
    447      1.1  christos }
    448      1.1  christos 
    449      1.3   thorpej /* We must have a read lock on the ksem_proc list! */
    450      1.3   thorpej static struct ksem *
    451      1.3   thorpej ksem_lookup_proc(struct ksem_proc *kp, semid_t id)
    452      1.1  christos {
    453      1.3   thorpej 	struct ksem_ref *ksr;
    454      1.1  christos 
    455      1.3   thorpej 	LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
    456      1.3   thorpej 		if (id == (semid_t) ksr->ksr_ksem) {
    457      1.3   thorpej 			simple_lock(&ksr->ksr_ksem->ks_interlock);
    458      1.3   thorpej 			return (ksr->ksr_ksem);
    459      1.3   thorpej 		}
    460      1.1  christos 	}
    461      1.3   thorpej 
    462      1.3   thorpej 	return (NULL);
    463      1.1  christos }
    464      1.1  christos 
    465      1.1  christos int
    466      1.2  christos sys__ksem_unlink(struct lwp *l, void *v, register_t *retval)
    467      1.1  christos {
    468      1.2  christos 	struct sys__ksem_unlink_args /* {
    469      1.1  christos 		const char *name;
    470      1.1  christos 	} */ *uap = v;
    471      1.3   thorpej 	char name[SEM_MAX_NAMELEN + 1], *cp;
    472      1.1  christos 	size_t done;
    473      1.1  christos 	struct ksem *ks;
    474      1.1  christos 	int error;
    475      1.1  christos 
    476      1.1  christos 	error = copyinstr(SCARG(uap, name), name, sizeof(name), &done);
    477      1.1  christos 	if (error)
    478      1.1  christos 		return error;
    479      1.1  christos 
    480      1.1  christos 	simple_lock(&ksem_slock);
    481      1.1  christos 	ks = ksem_lookup_byname(name);
    482      1.3   thorpej 	if (ks == NULL) {
    483      1.3   thorpej 		simple_unlock(&ksem_slock);
    484      1.3   thorpej 		return (ENOENT);
    485      1.1  christos 	}
    486      1.3   thorpej 
    487      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    488      1.3   thorpej 
    489      1.3   thorpej 	LIST_REMOVE(ks, ks_entry);
    490      1.3   thorpej 	cp = ks->ks_name;
    491      1.3   thorpej 	ks->ks_name = NULL;
    492      1.3   thorpej 
    493      1.1  christos 	simple_unlock(&ksem_slock);
    494      1.3   thorpej 
    495      1.3   thorpej 	if (ks->ks_ref == 0)
    496      1.3   thorpej 		ksem_free(ks);
    497      1.3   thorpej 	else
    498      1.3   thorpej 		simple_unlock(&ks->ks_interlock);
    499      1.3   thorpej 
    500      1.3   thorpej 	free(cp, M_SEM);
    501      1.3   thorpej 
    502      1.3   thorpej 	return (0);
    503      1.1  christos }
    504      1.1  christos 
    505      1.1  christos int
    506      1.2  christos sys__ksem_close(struct lwp *l, void *v, register_t *retval)
    507      1.1  christos {
    508      1.2  christos 	struct sys__ksem_close_args /* {
    509      1.1  christos 		semid_t id;
    510      1.1  christos 	} */ *uap = v;
    511      1.3   thorpej 	struct ksem_proc *kp;
    512      1.3   thorpej 	struct ksem_ref *ksr;
    513      1.1  christos 	struct ksem *ks;
    514      1.1  christos 
    515      1.3   thorpej 	if ((kp = l->l_proc->p_ksems) == NULL)
    516      1.3   thorpej 		return (EINVAL);
    517      1.3   thorpej 
    518      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_EXCLUSIVE, NULL);
    519      1.3   thorpej 
    520      1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    521      1.3   thorpej 	if (ks == NULL) {
    522      1.3   thorpej 		lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    523      1.3   thorpej 		return (EINVAL);
    524      1.3   thorpej 	}
    525      1.3   thorpej 
    526      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    527      1.3   thorpej 	if (ks->ks_name == NULL) {
    528      1.3   thorpej 		simple_unlock(&ks->ks_interlock);
    529      1.3   thorpej 		lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    530      1.3   thorpej 		return (EINVAL);
    531      1.3   thorpej 	}
    532      1.3   thorpej 
    533      1.3   thorpej 	ksr = ksem_drop_proc(kp, ks);
    534      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    535      1.3   thorpej 	free(ksr, M_SEM);
    536      1.3   thorpej 
    537      1.3   thorpej 	return (0);
    538      1.1  christos }
    539      1.1  christos 
    540      1.1  christos int
    541      1.2  christos sys__ksem_post(struct lwp *l, void *v, register_t *retval)
    542      1.1  christos {
    543      1.2  christos 	struct sys__ksem_post_args /* {
    544      1.1  christos 		semid_t id;
    545      1.1  christos 	} */ *uap = v;
    546      1.3   thorpej 	struct ksem_proc *kp;
    547      1.1  christos 	struct ksem *ks;
    548      1.1  christos 	int error;
    549      1.1  christos 
    550      1.3   thorpej 	if ((kp = l->l_proc->p_ksems) == NULL)
    551      1.3   thorpej 		return (EINVAL);
    552      1.3   thorpej 
    553      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_SHARED, NULL);
    554      1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    555      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    556      1.3   thorpej 	if (ks == NULL)
    557      1.3   thorpej 		return (EINVAL);
    558      1.3   thorpej 
    559      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    560      1.1  christos 	if (ks->ks_value == SEM_VALUE_MAX) {
    561      1.1  christos 		error = EOVERFLOW;
    562      1.3   thorpej 		goto out;
    563      1.1  christos 	}
    564      1.1  christos 	++ks->ks_value;
    565      1.3   thorpej 	if (ks->ks_waiters)
    566      1.1  christos 		wakeup(ks);
    567      1.1  christos 	error = 0;
    568      1.3   thorpej  out:
    569      1.3   thorpej 	simple_unlock(&ks->ks_interlock);
    570      1.3   thorpej 	return (error);
    571      1.3   thorpej }
    572      1.3   thorpej 
    573      1.3   thorpej static int
    574      1.3   thorpej ksem_wait(struct lwp *l, semid_t id, int tryflag)
    575      1.3   thorpej {
    576      1.3   thorpej 	struct ksem_proc *kp;
    577      1.3   thorpej 	struct ksem *ks;
    578      1.3   thorpej 	int error;
    579      1.3   thorpej 
    580      1.3   thorpej 	if ((kp = l->l_proc->p_ksems) == NULL)
    581      1.3   thorpej 		return (EINVAL);
    582      1.3   thorpej 
    583      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_SHARED, NULL);
    584      1.3   thorpej 	ks = ksem_lookup_proc(kp, id);
    585      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    586      1.3   thorpej 	if (ks == NULL)
    587      1.3   thorpej 		return (EINVAL);
    588      1.3   thorpej 
    589      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    590      1.3   thorpej 	ksem_addref(ks);
    591      1.3   thorpej 	while (ks->ks_value == 0) {
    592      1.3   thorpej 		ks->ks_waiters++;
    593      1.3   thorpej 		error = tryflag ? EAGAIN : ltsleep(ks, PCATCH, "psem", 0,
    594      1.3   thorpej 		    &ks->ks_interlock);
    595      1.3   thorpej 		ks->ks_waiters--;
    596      1.3   thorpej 		if (error)
    597      1.3   thorpej 			goto out;
    598      1.3   thorpej 	}
    599      1.3   thorpej 	ks->ks_value--;
    600      1.3   thorpej 	error = 0;
    601      1.3   thorpej  out:
    602      1.3   thorpej 	ksem_delref(ks);
    603      1.1  christos 	return (error);
    604      1.1  christos }
    605      1.1  christos 
    606      1.1  christos int
    607      1.2  christos sys__ksem_wait(struct lwp *l, void *v, register_t *retval)
    608      1.1  christos {
    609      1.2  christos 	struct sys__ksem_wait_args /* {
    610      1.1  christos 		semid_t id;
    611      1.1  christos 	} */ *uap = v;
    612      1.1  christos 
    613      1.1  christos 	return ksem_wait(l, SCARG(uap, id), 0);
    614      1.1  christos }
    615      1.1  christos 
    616      1.1  christos int
    617      1.2  christos sys__ksem_trywait(struct lwp *l, void *v, register_t *retval)
    618      1.1  christos {
    619      1.2  christos 	struct sys__ksem_trywait_args /* {
    620      1.1  christos 		semid_t id;
    621      1.1  christos 	} */ *uap = v;
    622      1.1  christos 
    623      1.1  christos 	return ksem_wait(l, SCARG(uap, id), 1);
    624      1.1  christos }
    625      1.1  christos 
    626      1.1  christos int
    627      1.2  christos sys__ksem_getvalue(struct lwp *l, void *v, register_t *retval)
    628      1.1  christos {
    629      1.2  christos 	struct sys__ksem_getvalue_args /* {
    630      1.1  christos 		semid_t id;
    631      1.1  christos 		unsigned int *value;
    632      1.1  christos 	} */ *uap = v;
    633      1.3   thorpej 	struct ksem_proc *kp;
    634      1.1  christos 	struct ksem *ks;
    635      1.1  christos 	unsigned int val;
    636      1.1  christos 
    637      1.3   thorpej 	if ((kp = l->l_proc->p_ksems) == NULL)
    638      1.3   thorpej 		return (EINVAL);
    639      1.3   thorpej 
    640      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_SHARED, NULL);
    641      1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    642      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    643      1.3   thorpej 	if (ks == NULL)
    644      1.1  christos 		return (EINVAL);
    645      1.3   thorpej 
    646      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    647      1.1  christos 	val = ks->ks_value;
    648      1.3   thorpej 	simple_unlock(&ks->ks_interlock);
    649      1.3   thorpej 
    650      1.3   thorpej 	return (copyout(&val, SCARG(uap, value), sizeof(val)));
    651      1.1  christos }
    652      1.1  christos 
    653      1.1  christos int
    654      1.2  christos sys__ksem_destroy(struct lwp *l, void *v, register_t *retval)
    655      1.1  christos {
    656      1.2  christos 	struct sys__ksem_destroy_args /*{
    657      1.1  christos 		semid_t id;
    658      1.1  christos 	} */ *uap = v;
    659      1.3   thorpej 	struct ksem_proc *kp;
    660      1.3   thorpej 	struct ksem_ref *ksr;
    661      1.1  christos 	struct ksem *ks;
    662      1.1  christos 
    663      1.3   thorpej 	if ((kp = l->l_proc->p_ksems) == NULL)
    664      1.3   thorpej 		return (EINVAL);
    665      1.3   thorpej 
    666      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_EXCLUSIVE, NULL);
    667      1.3   thorpej 
    668      1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    669      1.3   thorpej 	if (ks == NULL) {
    670      1.3   thorpej 		lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    671      1.3   thorpej 		return (EINVAL);
    672      1.3   thorpej 	}
    673      1.3   thorpej 
    674      1.3   thorpej 	LOCK_ASSERT(simple_lock_held(&ks->ks_interlock));
    675      1.3   thorpej 
    676      1.3   thorpej 	/*
    677      1.3   thorpej 	 * XXX This misses named semaphores which have been unlink'd,
    678      1.3   thorpej 	 * XXX but since behavior of destroying a named semaphore is
    679      1.3   thorpej 	 * XXX undefined, this is technically allowed.
    680      1.3   thorpej 	 */
    681      1.3   thorpej 	if (ks->ks_name != NULL) {
    682      1.3   thorpej 		simple_unlock(&ks->ks_interlock);
    683      1.3   thorpej 		lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    684      1.3   thorpej 		return (EINVAL);
    685      1.3   thorpej 	}
    686      1.3   thorpej 
    687      1.3   thorpej 	if (ks->ks_waiters) {
    688      1.3   thorpej 		simple_unlock(&ks->ks_interlock);
    689      1.3   thorpej 		lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    690      1.3   thorpej 		return (EBUSY);
    691      1.3   thorpej 	}
    692      1.3   thorpej 
    693      1.3   thorpej 	ksr = ksem_drop_proc(kp, ks);
    694      1.3   thorpej 	lockmgr(&kp->kp_lock, LK_RELEASE, NULL);
    695      1.3   thorpej 	free(ksr, M_SEM);
    696      1.3   thorpej 
    697      1.3   thorpej 	return (0);
    698      1.3   thorpej }
    699      1.3   thorpej 
    700      1.3   thorpej static void
    701      1.3   thorpej ksem_forkhook(struct proc *p2, struct proc *p1)
    702      1.3   thorpej {
    703      1.3   thorpej 	struct ksem_proc *kp1, *kp2;
    704      1.3   thorpej 	struct ksem_ref *ksr, *ksr1;
    705      1.3   thorpej 
    706      1.3   thorpej 	if ((kp1 = p1->p_ksems) == NULL) {
    707      1.3   thorpej 		p2->p_ksems = NULL;
    708      1.3   thorpej 		return;
    709      1.3   thorpej 	}
    710      1.3   thorpej 
    711      1.3   thorpej 	p2->p_ksems = kp2 = ksem_proc_alloc();
    712      1.3   thorpej 
    713      1.3   thorpej 	lockmgr(&kp1->kp_lock, LK_SHARED, NULL);
    714      1.3   thorpej 
    715      1.3   thorpej 	if (!LIST_EMPTY(&kp1->kp_ksems)) {
    716      1.3   thorpej 		LIST_FOREACH(ksr, &kp1->kp_ksems, ksr_list) {
    717      1.3   thorpej 			ksr1 = malloc(sizeof(*ksr), M_SEM, M_WAITOK);
    718      1.3   thorpej 			ksr1->ksr_ksem = ksr->ksr_ksem;
    719      1.3   thorpej 			simple_lock(&ksr->ksr_ksem->ks_interlock);
    720      1.3   thorpej 			ksem_addref(ksr->ksr_ksem);
    721      1.3   thorpej 			simple_unlock(&ksr->ksr_ksem->ks_interlock);
    722      1.3   thorpej 			LIST_INSERT_HEAD(&kp2->kp_ksems, ksr1, ksr_list);
    723      1.3   thorpej 		}
    724      1.1  christos 	}
    725      1.3   thorpej 
    726      1.3   thorpej 	lockmgr(&kp1->kp_lock, LK_RELEASE, NULL);
    727      1.1  christos }
    728      1.1  christos 
    729      1.1  christos static void
    730  1.8.2.3     skrll ksem_exithook(struct proc *p, void *arg)
    731      1.1  christos {
    732      1.3   thorpej 	struct ksem_proc *kp;
    733      1.3   thorpej 	struct ksem_ref *ksr;
    734      1.3   thorpej 
    735  1.8.2.3     skrll 	if ((kp = p->p_ksems) == NULL)
    736      1.3   thorpej 		return;
    737      1.1  christos 
    738      1.3   thorpej 	/* Don't bother locking; process is dying. */
    739      1.3   thorpej 
    740      1.3   thorpej 	while ((ksr = LIST_FIRST(&kp->kp_ksems)) != NULL) {
    741      1.3   thorpej 		LIST_REMOVE(ksr, ksr_list);
    742      1.3   thorpej 		simple_lock(&ksr->ksr_ksem->ks_interlock);
    743      1.3   thorpej 		ksem_delref(ksr->ksr_ksem);
    744      1.3   thorpej 		free(ksr, M_SEM);
    745      1.1  christos 	}
    746      1.1  christos }
    747      1.1  christos 
    748      1.1  christos void
    749      1.1  christos ksem_init(void)
    750      1.1  christos {
    751      1.3   thorpej 
    752      1.1  christos 	simple_lock_init(&ksem_slock);
    753      1.1  christos 	exithook_establish(ksem_exithook, NULL);
    754      1.1  christos 	exechook_establish(ksem_exithook, NULL);
    755      1.3   thorpej 	forkhook_establish(ksem_forkhook);
    756      1.1  christos }
    757