Home | History | Annotate | Line # | Download | only in kern
uipc_sem.c revision 1.24.10.2
      1  1.24.10.2      yamt /*	$NetBSD: uipc_sem.c,v 1.24.10.2 2009/05/04 08:13:49 yamt Exp $	*/
      2        1.3   thorpej 
      3        1.3   thorpej /*-
      4  1.24.10.2      yamt  * Copyright (c) 2003, 2007, 2008 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.21        ad  * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran.
      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  *
     19        1.3   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.3   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.3   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.3   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.3   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.3   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.3   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.3   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.3   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.3   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.3   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30        1.3   thorpej  */
     31        1.1  christos 
     32        1.1  christos /*
     33        1.1  christos  * Copyright (c) 2002 Alfred Perlstein <alfred (at) FreeBSD.org>
     34        1.1  christos  * All rights reserved.
     35        1.1  christos  *
     36        1.1  christos  * Redistribution and use in source and binary forms, with or without
     37        1.1  christos  * modification, are permitted provided that the following conditions
     38        1.1  christos  * are met:
     39        1.1  christos  * 1. Redistributions of source code must retain the above copyright
     40        1.1  christos  *    notice, this list of conditions and the following disclaimer.
     41        1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     42        1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     43        1.1  christos  *    documentation and/or other materials provided with the distribution.
     44        1.1  christos  *
     45        1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46        1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47        1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48        1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49        1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50        1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51        1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52        1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53        1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54        1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55        1.1  christos  * SUCH DAMAGE.
     56        1.1  christos  */
     57        1.9     lukem 
     58        1.9     lukem #include <sys/cdefs.h>
     59  1.24.10.2      yamt __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.24.10.2 2009/05/04 08:13:49 yamt Exp $");
     60        1.1  christos 
     61        1.1  christos #include <sys/param.h>
     62        1.1  christos #include <sys/systm.h>
     63        1.1  christos #include <sys/kernel.h>
     64        1.1  christos #include <sys/proc.h>
     65        1.1  christos #include <sys/ksem.h>
     66        1.1  christos #include <sys/syscall.h>
     67        1.1  christos #include <sys/stat.h>
     68       1.21        ad #include <sys/kmem.h>
     69        1.1  christos #include <sys/fcntl.h>
     70       1.14      elad #include <sys/kauth.h>
     71  1.24.10.2      yamt #include <sys/module.h>
     72        1.1  christos #include <sys/mount.h>
     73  1.24.10.2      yamt #include <sys/syscall.h>
     74        1.1  christos #include <sys/syscallargs.h>
     75  1.24.10.2      yamt #include <sys/syscallvar.h>
     76        1.1  christos 
     77        1.1  christos #define SEM_MAX_NAMELEN	14
     78        1.1  christos #define SEM_VALUE_MAX (~0U)
     79       1.13      cube #define SEM_HASHTBL_SIZE 13
     80        1.1  christos 
     81       1.13      cube #define SEM_TO_ID(x)	(((x)->ks_id))
     82       1.13      cube #define SEM_HASH(id)	((id) % SEM_HASHTBL_SIZE)
     83        1.4   thorpej 
     84  1.24.10.2      yamt MODULE(MODULE_CLASS_MISC, ksem, NULL);
     85  1.24.10.2      yamt 
     86  1.24.10.2      yamt static const struct syscall_package ksem_syscalls[] = {
     87  1.24.10.2      yamt 	{ SYS__ksem_init, 0, (sy_call_t *)sys__ksem_init },
     88  1.24.10.2      yamt 	{ SYS__ksem_open, 0, (sy_call_t *)sys__ksem_open },
     89  1.24.10.2      yamt 	{ SYS__ksem_unlink, 0, (sy_call_t *)sys__ksem_unlink },
     90  1.24.10.2      yamt 	{ SYS__ksem_close, 0, (sy_call_t *)sys__ksem_close },
     91  1.24.10.2      yamt 	{ SYS__ksem_post, 0, (sy_call_t *)sys__ksem_post },
     92  1.24.10.2      yamt 	{ SYS__ksem_wait, 0, (sy_call_t *)sys__ksem_wait },
     93  1.24.10.2      yamt 	{ SYS__ksem_trywait, 0, (sy_call_t *)sys__ksem_trywait },
     94  1.24.10.2      yamt 	{ SYS__ksem_getvalue, 0, (sy_call_t *)sys__ksem_getvalue },
     95  1.24.10.2      yamt 	{ SYS__ksem_destroy, 0, (sy_call_t *)sys__ksem_destroy },
     96  1.24.10.2      yamt 	{ 0, 0, NULL },
     97  1.24.10.2      yamt };
     98        1.1  christos 
     99        1.3   thorpej /*
    100        1.3   thorpej  * Note: to read the ks_name member, you need either the ks_interlock
    101  1.24.10.2      yamt  * or the ksem_mutex.  To write the ks_name member, you need both.  Make
    102  1.24.10.2      yamt  * sure the order is ksem_mutex -> ks_interlock.
    103        1.3   thorpej  */
    104        1.1  christos struct ksem {
    105        1.1  christos 	LIST_ENTRY(ksem) ks_entry;	/* global list entry */
    106       1.13      cube 	LIST_ENTRY(ksem) ks_hash;	/* hash list entry */
    107       1.20        ad 	kmutex_t ks_interlock;		/* lock on this ksem */
    108       1.20        ad 	kcondvar_t ks_cv;		/* condition variable */
    109       1.21        ad 	unsigned int ks_ref;		/* number of references */
    110        1.1  christos 	char *ks_name;			/* if named, this is the name */
    111       1.21        ad 	size_t ks_namelen;		/* length of name */
    112        1.1  christos 	mode_t ks_mode;			/* protection bits */
    113        1.1  christos 	uid_t ks_uid;			/* creator uid */
    114        1.1  christos 	gid_t ks_gid;			/* creator gid */
    115        1.1  christos 	unsigned int ks_value;		/* current value */
    116        1.3   thorpej 	unsigned int ks_waiters;	/* number of waiters */
    117  1.24.10.2      yamt 	intptr_t ks_id;			/* unique identifier */
    118        1.3   thorpej };
    119        1.3   thorpej 
    120        1.3   thorpej struct ksem_ref {
    121        1.3   thorpej 	LIST_ENTRY(ksem_ref) ksr_list;
    122        1.3   thorpej 	struct ksem *ksr_ksem;
    123        1.3   thorpej };
    124        1.3   thorpej 
    125        1.3   thorpej struct ksem_proc {
    126       1.20        ad 	krwlock_t kp_lock;
    127        1.3   thorpej 	LIST_HEAD(, ksem_ref) kp_ksems;
    128        1.1  christos };
    129        1.1  christos 
    130       1.13      cube LIST_HEAD(ksem_list, ksem);
    131       1.13      cube 
    132        1.1  christos /*
    133  1.24.10.2      yamt  * ksem_mutex protects ksem_head and nsems.  Only named semaphores go
    134        1.3   thorpej  * onto ksem_head.
    135        1.1  christos  */
    136       1.20        ad static kmutex_t ksem_mutex;
    137       1.13      cube static struct ksem_list ksem_head = LIST_HEAD_INITIALIZER(&ksem_head);
    138       1.13      cube static struct ksem_list ksem_hash[SEM_HASHTBL_SIZE];
    139        1.3   thorpej static int nsems = 0;
    140        1.1  christos 
    141       1.13      cube /*
    142  1.24.10.2      yamt  * ksem_counter is the last assigned intptr_t.  It needs to be COMPAT_NETBSD32
    143  1.24.10.2      yamt  * friendly, even though intptr_t itself is defined as uintptr_t.
    144       1.13      cube  */
    145       1.13      cube static uint32_t ksem_counter = 1;
    146       1.13      cube 
    147       1.16   thorpej static specificdata_key_t ksem_specificdata_key;
    148  1.24.10.2      yamt static void *ksem_ehook;
    149  1.24.10.2      yamt static void *ksem_fhook;
    150       1.13      cube 
    151        1.3   thorpej static void
    152        1.3   thorpej ksem_free(struct ksem *ks)
    153        1.3   thorpej {
    154        1.1  christos 
    155       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    156       1.20        ad 
    157        1.3   thorpej 	/*
    158        1.3   thorpej 	 * If the ksem is anonymous (or has been unlinked), then
    159        1.3   thorpej 	 * this is the end if its life.
    160        1.3   thorpej 	 */
    161        1.3   thorpej 	if (ks->ks_name == NULL) {
    162       1.20        ad 		mutex_exit(&ks->ks_interlock);
    163       1.20        ad 		mutex_destroy(&ks->ks_interlock);
    164       1.20        ad 		cv_destroy(&ks->ks_cv);
    165        1.1  christos 
    166       1.20        ad 		mutex_enter(&ksem_mutex);
    167        1.3   thorpej 		nsems--;
    168       1.13      cube 		LIST_REMOVE(ks, ks_hash);
    169       1.20        ad 		mutex_exit(&ksem_mutex);
    170       1.13      cube 
    171       1.21        ad 		kmem_free(ks, sizeof(*ks));
    172        1.3   thorpej 		return;
    173        1.3   thorpej 	}
    174       1.20        ad 	mutex_exit(&ks->ks_interlock);
    175        1.3   thorpej }
    176        1.1  christos 
    177       1.12     perry static inline void
    178        1.3   thorpej ksem_addref(struct ksem *ks)
    179        1.1  christos {
    180        1.1  christos 
    181       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    182        1.1  christos 	ks->ks_ref++;
    183       1.21        ad 	KASSERT(ks->ks_ref != 0);
    184        1.1  christos }
    185        1.1  christos 
    186       1.12     perry static inline void
    187        1.3   thorpej ksem_delref(struct ksem *ks)
    188        1.1  christos {
    189        1.1  christos 
    190       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    191       1.21        ad 	KASSERT(ks->ks_ref != 0);
    192        1.3   thorpej 	if (--ks->ks_ref == 0) {
    193        1.1  christos 		ksem_free(ks);
    194        1.3   thorpej 		return;
    195        1.3   thorpej 	}
    196       1.20        ad 	mutex_exit(&ks->ks_interlock);
    197        1.3   thorpej }
    198        1.3   thorpej 
    199        1.3   thorpej static struct ksem_proc *
    200        1.3   thorpej ksem_proc_alloc(void)
    201        1.3   thorpej {
    202        1.3   thorpej 	struct ksem_proc *kp;
    203        1.3   thorpej 
    204       1.21        ad 	kp = kmem_alloc(sizeof(*kp), KM_SLEEP);
    205       1.20        ad 	rw_init(&kp->kp_lock);
    206        1.3   thorpej 	LIST_INIT(&kp->kp_ksems);
    207        1.3   thorpej 
    208        1.3   thorpej 	return (kp);
    209        1.1  christos }
    210        1.1  christos 
    211        1.3   thorpej static void
    212       1.16   thorpej ksem_proc_dtor(void *arg)
    213       1.16   thorpej {
    214       1.16   thorpej 	struct ksem_proc *kp = arg;
    215       1.16   thorpej 	struct ksem_ref *ksr;
    216       1.16   thorpej 
    217       1.20        ad 	rw_enter(&kp->kp_lock, RW_WRITER);
    218       1.16   thorpej 
    219       1.16   thorpej 	while ((ksr = LIST_FIRST(&kp->kp_ksems)) != NULL) {
    220       1.16   thorpej 		LIST_REMOVE(ksr, ksr_list);
    221       1.20        ad 		mutex_enter(&ksr->ksr_ksem->ks_interlock);
    222       1.16   thorpej 		ksem_delref(ksr->ksr_ksem);
    223       1.21        ad 		kmem_free(ksr, sizeof(*ksr));
    224       1.16   thorpej 	}
    225       1.16   thorpej 
    226       1.20        ad 	rw_exit(&kp->kp_lock);
    227       1.20        ad 	rw_destroy(&kp->kp_lock);
    228       1.21        ad 	kmem_free(kp, sizeof(*kp));
    229       1.16   thorpej }
    230       1.16   thorpej 
    231       1.16   thorpej static void
    232        1.3   thorpej ksem_add_proc(struct proc *p, struct ksem *ks)
    233        1.3   thorpej {
    234        1.3   thorpej 	struct ksem_proc *kp;
    235        1.3   thorpej 	struct ksem_ref *ksr;
    236        1.3   thorpej 
    237       1.16   thorpej 	kp = proc_getspecific(p, ksem_specificdata_key);
    238       1.16   thorpej 	if (kp == NULL) {
    239        1.3   thorpej 		kp = ksem_proc_alloc();
    240       1.16   thorpej 		proc_setspecific(p, ksem_specificdata_key, kp);
    241       1.16   thorpej 	}
    242        1.3   thorpej 
    243       1.21        ad 	ksr = kmem_alloc(sizeof(*ksr), KM_SLEEP);
    244        1.3   thorpej 	ksr->ksr_ksem = ks;
    245        1.3   thorpej 
    246       1.20        ad 	rw_enter(&kp->kp_lock, RW_WRITER);
    247        1.3   thorpej 	LIST_INSERT_HEAD(&kp->kp_ksems, ksr, ksr_list);
    248       1.20        ad 	rw_exit(&kp->kp_lock);
    249        1.3   thorpej }
    250        1.3   thorpej 
    251        1.3   thorpej /* We MUST have a write lock on the ksem_proc list! */
    252        1.3   thorpej static struct ksem_ref *
    253        1.3   thorpej ksem_drop_proc(struct ksem_proc *kp, struct ksem *ks)
    254        1.1  christos {
    255        1.3   thorpej 	struct ksem_ref *ksr;
    256        1.1  christos 
    257       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    258        1.3   thorpej 	LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
    259        1.3   thorpej 		if (ksr->ksr_ksem == ks) {
    260        1.3   thorpej 			ksem_delref(ks);
    261        1.3   thorpej 			LIST_REMOVE(ksr, ksr_list);
    262        1.3   thorpej 			return (ksr);
    263        1.3   thorpej 		}
    264        1.1  christos 	}
    265        1.3   thorpej #ifdef DIAGNOSTIC
    266        1.3   thorpej 	panic("ksem_drop_proc: ksem_proc %p ksem %p", kp, ks);
    267        1.3   thorpej #endif
    268        1.1  christos 	return (NULL);
    269        1.1  christos }
    270        1.1  christos 
    271        1.3   thorpej static int
    272       1.15        ad ksem_perm(struct lwp *l, struct ksem *ks)
    273        1.3   thorpej {
    274       1.14      elad 	kauth_cred_t uc;
    275        1.3   thorpej 
    276       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    277       1.15        ad 	uc = l->l_cred;
    278       1.14      elad 	if ((kauth_cred_geteuid(uc) == ks->ks_uid && (ks->ks_mode & S_IWUSR) != 0) ||
    279       1.14      elad 	    (kauth_cred_getegid(uc) == ks->ks_gid && (ks->ks_mode & S_IWGRP) != 0) ||
    280       1.14      elad 	    (ks->ks_mode & S_IWOTH) != 0 ||
    281       1.19      elad 	    kauth_authorize_generic(uc, KAUTH_GENERIC_ISSUSER, NULL) == 0)
    282        1.3   thorpej 		return (0);
    283        1.3   thorpej 	return (EPERM);
    284        1.3   thorpej }
    285        1.3   thorpej 
    286        1.1  christos static struct ksem *
    287  1.24.10.2      yamt ksem_lookup_byid(intptr_t id)
    288       1.13      cube {
    289       1.13      cube 	struct ksem *ks;
    290       1.13      cube 
    291       1.21        ad 	KASSERT(mutex_owned(&ksem_mutex));
    292       1.13      cube 	LIST_FOREACH(ks, &ksem_hash[SEM_HASH(id)], ks_hash) {
    293       1.13      cube 		if (ks->ks_id == id)
    294       1.13      cube 			return ks;
    295       1.13      cube 	}
    296       1.13      cube 	return NULL;
    297       1.13      cube }
    298       1.13      cube 
    299       1.13      cube static struct ksem *
    300        1.3   thorpej ksem_lookup_byname(const char *name)
    301        1.1  christos {
    302        1.1  christos 	struct ksem *ks;
    303        1.1  christos 
    304       1.21        ad 	KASSERT(mutex_owned(&ksem_mutex));
    305        1.3   thorpej 	LIST_FOREACH(ks, &ksem_head, ks_entry) {
    306        1.3   thorpej 		if (strcmp(ks->ks_name, name) == 0) {
    307       1.20        ad 			mutex_enter(&ks->ks_interlock);
    308        1.1  christos 			return (ks);
    309        1.3   thorpej 		}
    310        1.3   thorpej 	}
    311        1.1  christos 	return (NULL);
    312        1.1  christos }
    313        1.1  christos 
    314        1.1  christos static int
    315       1.15        ad ksem_create(struct lwp *l, const char *name, struct ksem **ksret,
    316        1.3   thorpej     mode_t mode, unsigned int value)
    317        1.1  christos {
    318        1.1  christos 	struct ksem *ret;
    319       1.14      elad 	kauth_cred_t uc;
    320        1.1  christos 	size_t len;
    321        1.1  christos 
    322       1.15        ad 	uc = l->l_cred;
    323        1.1  christos 	if (value > SEM_VALUE_MAX)
    324        1.1  christos 		return (EINVAL);
    325       1.21        ad 	ret = kmem_zalloc(sizeof(*ret), KM_SLEEP);
    326        1.1  christos 	if (name != NULL) {
    327        1.1  christos 		len = strlen(name);
    328        1.1  christos 		if (len > SEM_MAX_NAMELEN) {
    329       1.21        ad 			kmem_free(ret, sizeof(*ret));
    330        1.1  christos 			return (ENAMETOOLONG);
    331        1.1  christos 		}
    332        1.1  christos 		/* name must start with a '/' but not contain one. */
    333        1.1  christos 		if (*name != '/' || len < 2 || strchr(name + 1, '/') != NULL) {
    334       1.21        ad 			kmem_free(ret, sizeof(*ret));
    335        1.1  christos 			return (EINVAL);
    336        1.1  christos 		}
    337       1.21        ad 		ret->ks_namelen = len + 1;
    338       1.21        ad 		ret->ks_name = kmem_alloc(ret->ks_namelen, KM_SLEEP);
    339        1.6    itojun 		strlcpy(ret->ks_name, name, len + 1);
    340        1.3   thorpej 	} else
    341        1.1  christos 		ret->ks_name = NULL;
    342        1.1  christos 	ret->ks_mode = mode;
    343        1.1  christos 	ret->ks_value = value;
    344        1.1  christos 	ret->ks_ref = 1;
    345        1.1  christos 	ret->ks_waiters = 0;
    346       1.14      elad 	ret->ks_uid = kauth_cred_geteuid(uc);
    347       1.14      elad 	ret->ks_gid = kauth_cred_getegid(uc);
    348       1.20        ad 	mutex_init(&ret->ks_interlock, MUTEX_DEFAULT, IPL_NONE);
    349       1.20        ad 	cv_init(&ret->ks_cv, "psem");
    350        1.3   thorpej 
    351       1.20        ad 	mutex_enter(&ksem_mutex);
    352  1.24.10.2      yamt 	if (nsems >= ksem_max) {
    353       1.20        ad 		mutex_exit(&ksem_mutex);
    354        1.3   thorpej 		if (ret->ks_name != NULL)
    355       1.21        ad 			kmem_free(ret->ks_name, ret->ks_namelen);
    356       1.21        ad 		kmem_free(ret, sizeof(*ret));
    357        1.3   thorpej 		return (ENFILE);
    358        1.1  christos 	}
    359        1.3   thorpej 	nsems++;
    360       1.13      cube 	while (ksem_lookup_byid(ksem_counter) != NULL) {
    361       1.13      cube 		ksem_counter++;
    362       1.13      cube 		/* 0 is a special value for libpthread */
    363       1.13      cube 		if (ksem_counter == 0)
    364       1.13      cube 			ksem_counter++;
    365       1.13      cube 	}
    366       1.13      cube 	ret->ks_id = ksem_counter;
    367       1.13      cube 	LIST_INSERT_HEAD(&ksem_hash[SEM_HASH(ret->ks_id)], ret, ks_hash);
    368       1.20        ad 	mutex_exit(&ksem_mutex);
    369        1.3   thorpej 
    370        1.3   thorpej 	*ksret = ret;
    371        1.3   thorpej 	return (0);
    372        1.1  christos }
    373        1.1  christos 
    374        1.1  christos int
    375       1.23       dsl sys__ksem_init(struct lwp *l, const struct sys__ksem_init_args *uap, register_t *retval)
    376        1.1  christos {
    377       1.23       dsl 	/* {
    378        1.1  christos 		unsigned int value;
    379  1.24.10.2      yamt 		intptr_t *idp;
    380       1.23       dsl 	} */
    381       1.13      cube 
    382       1.13      cube 	return do_ksem_init(l, SCARG(uap, value), SCARG(uap, idp), copyout);
    383       1.13      cube }
    384       1.13      cube 
    385       1.13      cube int
    386  1.24.10.2      yamt do_ksem_init(struct lwp *l, unsigned int value, intptr_t *idp,
    387       1.13      cube     copyout_t docopyout)
    388       1.13      cube {
    389        1.1  christos 	struct ksem *ks;
    390  1.24.10.2      yamt 	intptr_t id;
    391        1.1  christos 	int error;
    392        1.1  christos 
    393        1.3   thorpej 	/* Note the mode does not matter for anonymous semaphores. */
    394       1.15        ad 	error = ksem_create(l, NULL, &ks, 0, value);
    395        1.1  christos 	if (error)
    396        1.1  christos 		return (error);
    397        1.1  christos 	id = SEM_TO_ID(ks);
    398       1.13      cube 	error = (*docopyout)(&id, idp, sizeof(id));
    399        1.1  christos 	if (error) {
    400       1.20        ad 		mutex_enter(&ks->ks_interlock);
    401        1.3   thorpej 		ksem_delref(ks);
    402        1.1  christos 		return (error);
    403        1.1  christos 	}
    404        1.3   thorpej 
    405        1.3   thorpej 	ksem_add_proc(l->l_proc, ks);
    406        1.3   thorpej 
    407        1.3   thorpej 	return (0);
    408        1.1  christos }
    409        1.1  christos 
    410        1.1  christos int
    411       1.23       dsl sys__ksem_open(struct lwp *l, const struct sys__ksem_open_args *uap, register_t *retval)
    412        1.1  christos {
    413       1.23       dsl 	/* {
    414        1.1  christos 		const char *name;
    415        1.1  christos 		int oflag;
    416        1.1  christos 		mode_t mode;
    417        1.1  christos 		unsigned int value;
    418  1.24.10.2      yamt 		intptr_t *idp;
    419       1.23       dsl 	} */
    420       1.13      cube 
    421       1.13      cube 	return do_ksem_open(l, SCARG(uap, name), SCARG(uap, oflag),
    422       1.13      cube 	    SCARG(uap, mode), SCARG(uap, value), SCARG(uap, idp), copyout);
    423       1.13      cube }
    424       1.13      cube 
    425       1.13      cube int
    426       1.13      cube do_ksem_open(struct lwp *l, const char *semname, int oflag, mode_t mode,
    427  1.24.10.2      yamt      unsigned int value, intptr_t *idp, copyout_t docopyout)
    428       1.13      cube {
    429        1.1  christos 	char name[SEM_MAX_NAMELEN + 1];
    430        1.1  christos 	size_t done;
    431        1.1  christos 	int error;
    432        1.1  christos 	struct ksem *ksnew, *ks;
    433  1.24.10.2      yamt 	intptr_t id;
    434        1.1  christos 
    435       1.13      cube 	error = copyinstr(semname, name, sizeof(name), &done);
    436        1.1  christos 	if (error)
    437        1.1  christos 		return (error);
    438        1.1  christos 
    439        1.1  christos 	ksnew = NULL;
    440       1.20        ad 	mutex_enter(&ksem_mutex);
    441        1.1  christos 	ks = ksem_lookup_byname(name);
    442        1.3   thorpej 
    443        1.3   thorpej 	/* Found one? */
    444        1.3   thorpej 	if (ks != NULL) {
    445        1.3   thorpej 		/* Check for exclusive create. */
    446       1.13      cube 		if (oflag & O_EXCL) {
    447       1.20        ad 			mutex_exit(&ks->ks_interlock);
    448       1.20        ad 			mutex_exit(&ksem_mutex);
    449        1.3   thorpej 			return (EEXIST);
    450        1.1  christos 		}
    451        1.3   thorpej  found_one:
    452        1.1  christos 		/*
    453        1.3   thorpej 		 * Verify permissions.  If we can access it, add
    454        1.3   thorpej 		 * this process's reference.
    455        1.1  christos 		 */
    456       1.21        ad 		KASSERT(mutex_owned(&ks->ks_interlock));
    457       1.15        ad 		error = ksem_perm(l, ks);
    458        1.3   thorpej 		if (error == 0)
    459        1.3   thorpej 			ksem_addref(ks);
    460       1.20        ad 		mutex_exit(&ks->ks_interlock);
    461       1.20        ad 		mutex_exit(&ksem_mutex);
    462        1.1  christos 		if (error)
    463        1.1  christos 			return (error);
    464        1.3   thorpej 
    465        1.1  christos 		id = SEM_TO_ID(ks);
    466       1.13      cube 		error = (*docopyout)(&id, idp, sizeof(id));
    467        1.1  christos 		if (error) {
    468       1.20        ad 			mutex_enter(&ks->ks_interlock);
    469        1.3   thorpej 			ksem_delref(ks);
    470        1.1  christos 			return (error);
    471        1.1  christos 		}
    472        1.3   thorpej 
    473        1.3   thorpej 		ksem_add_proc(l->l_proc, ks);
    474        1.3   thorpej 
    475        1.3   thorpej 		return (0);
    476        1.3   thorpej 	}
    477        1.3   thorpej 
    478        1.3   thorpej 	/*
    479        1.3   thorpej 	 * didn't ask for creation? error.
    480        1.3   thorpej 	 */
    481       1.13      cube 	if ((oflag & O_CREAT) == 0) {
    482       1.20        ad 		mutex_exit(&ksem_mutex);
    483        1.3   thorpej 		return (ENOENT);
    484        1.1  christos 	}
    485        1.1  christos 
    486        1.3   thorpej 	/*
    487        1.3   thorpej 	 * We may block during creation, so drop the lock.
    488        1.3   thorpej 	 */
    489       1.20        ad 	mutex_exit(&ksem_mutex);
    490       1.15        ad 	error = ksem_create(l, name, &ksnew, mode, value);
    491        1.3   thorpej 	if (error != 0)
    492        1.3   thorpej 		return (error);
    493        1.3   thorpej 
    494        1.3   thorpej 	id = SEM_TO_ID(ksnew);
    495       1.13      cube 	error = (*docopyout)(&id, idp, sizeof(id));
    496        1.3   thorpej 	if (error) {
    497       1.21        ad 		kmem_free(ksnew->ks_name, ksnew->ks_namelen);
    498        1.3   thorpej 		ksnew->ks_name = NULL;
    499        1.1  christos 
    500       1.20        ad 		mutex_enter(&ksnew->ks_interlock);
    501        1.3   thorpej 		ksem_delref(ksnew);
    502        1.3   thorpej 		return (error);
    503        1.3   thorpej 	}
    504        1.1  christos 
    505        1.3   thorpej 	/*
    506        1.3   thorpej 	 * We need to make sure we haven't lost a race while
    507        1.3   thorpej 	 * allocating during creation.
    508        1.3   thorpej 	 */
    509       1.20        ad 	mutex_enter(&ksem_mutex);
    510        1.3   thorpej 	if ((ks = ksem_lookup_byname(name)) != NULL) {
    511       1.13      cube 		if (oflag & O_EXCL) {
    512       1.20        ad 			mutex_exit(&ks->ks_interlock);
    513       1.20        ad 			mutex_exit(&ksem_mutex);
    514        1.1  christos 
    515       1.21        ad 			kmem_free(ksnew->ks_name, ksnew->ks_namelen);
    516        1.3   thorpej 			ksnew->ks_name = NULL;
    517        1.1  christos 
    518       1.20        ad 			mutex_enter(&ksnew->ks_interlock);
    519        1.3   thorpej 			ksem_delref(ksnew);
    520        1.3   thorpej 			return (EEXIST);
    521        1.3   thorpej 		}
    522        1.3   thorpej 		goto found_one;
    523        1.3   thorpej 	} else {
    524        1.3   thorpej 		/* ksnew already has its initial reference. */
    525       1.10     perry 		LIST_INSERT_HEAD(&ksem_head, ksnew, ks_entry);
    526       1.20        ad 		mutex_exit(&ksem_mutex);
    527        1.1  christos 
    528        1.3   thorpej 		ksem_add_proc(l->l_proc, ksnew);
    529        1.1  christos 	}
    530        1.3   thorpej 	return (error);
    531        1.1  christos }
    532        1.1  christos 
    533        1.3   thorpej /* We must have a read lock on the ksem_proc list! */
    534        1.3   thorpej static struct ksem *
    535  1.24.10.2      yamt ksem_lookup_proc(struct ksem_proc *kp, intptr_t id)
    536        1.1  christos {
    537        1.3   thorpej 	struct ksem_ref *ksr;
    538        1.1  christos 
    539        1.3   thorpej 	LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
    540       1.13      cube 		if (id == SEM_TO_ID(ksr->ksr_ksem)) {
    541       1.20        ad 			mutex_enter(&ksr->ksr_ksem->ks_interlock);
    542        1.3   thorpej 			return (ksr->ksr_ksem);
    543        1.3   thorpej 		}
    544        1.1  christos 	}
    545        1.3   thorpej 
    546        1.3   thorpej 	return (NULL);
    547        1.1  christos }
    548        1.1  christos 
    549        1.1  christos int
    550       1.23       dsl sys__ksem_unlink(struct lwp *l, const struct sys__ksem_unlink_args *uap, register_t *retval)
    551        1.1  christos {
    552       1.23       dsl 	/* {
    553        1.1  christos 		const char *name;
    554       1.23       dsl 	} */
    555        1.3   thorpej 	char name[SEM_MAX_NAMELEN + 1], *cp;
    556       1.21        ad 	size_t done, len;
    557        1.1  christos 	struct ksem *ks;
    558        1.1  christos 	int error;
    559        1.1  christos 
    560        1.1  christos 	error = copyinstr(SCARG(uap, name), name, sizeof(name), &done);
    561        1.1  christos 	if (error)
    562        1.1  christos 		return error;
    563        1.1  christos 
    564       1.20        ad 	mutex_enter(&ksem_mutex);
    565        1.1  christos 	ks = ksem_lookup_byname(name);
    566        1.3   thorpej 	if (ks == NULL) {
    567       1.20        ad 		mutex_exit(&ksem_mutex);
    568        1.3   thorpej 		return (ENOENT);
    569        1.1  christos 	}
    570        1.3   thorpej 
    571       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    572        1.3   thorpej 
    573        1.3   thorpej 	LIST_REMOVE(ks, ks_entry);
    574        1.3   thorpej 	cp = ks->ks_name;
    575       1.21        ad 	len = ks->ks_namelen;
    576        1.3   thorpej 	ks->ks_name = NULL;
    577        1.3   thorpej 
    578       1.20        ad 	mutex_exit(&ksem_mutex);
    579        1.3   thorpej 
    580        1.3   thorpej 	if (ks->ks_ref == 0)
    581        1.3   thorpej 		ksem_free(ks);
    582        1.3   thorpej 	else
    583       1.20        ad 		mutex_exit(&ks->ks_interlock);
    584        1.3   thorpej 
    585       1.21        ad 	kmem_free(cp, len);
    586        1.3   thorpej 
    587        1.3   thorpej 	return (0);
    588        1.1  christos }
    589        1.1  christos 
    590        1.1  christos int
    591       1.23       dsl sys__ksem_close(struct lwp *l, const struct sys__ksem_close_args *uap, register_t *retval)
    592        1.1  christos {
    593       1.23       dsl 	/* {
    594  1.24.10.2      yamt 		intptr_t id;
    595       1.23       dsl 	} */
    596        1.3   thorpej 	struct ksem_proc *kp;
    597        1.3   thorpej 	struct ksem_ref *ksr;
    598        1.1  christos 	struct ksem *ks;
    599        1.1  christos 
    600       1.16   thorpej 	kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
    601       1.16   thorpej 	if (kp == NULL)
    602        1.3   thorpej 		return (EINVAL);
    603        1.3   thorpej 
    604       1.20        ad 	rw_enter(&kp->kp_lock, RW_WRITER);
    605        1.3   thorpej 
    606        1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    607        1.3   thorpej 	if (ks == NULL) {
    608       1.20        ad 		rw_exit(&kp->kp_lock);
    609        1.3   thorpej 		return (EINVAL);
    610        1.3   thorpej 	}
    611        1.3   thorpej 
    612       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    613        1.3   thorpej 	if (ks->ks_name == NULL) {
    614       1.20        ad 		mutex_exit(&ks->ks_interlock);
    615       1.20        ad 		rw_exit(&kp->kp_lock);
    616        1.3   thorpej 		return (EINVAL);
    617        1.3   thorpej 	}
    618        1.3   thorpej 
    619        1.3   thorpej 	ksr = ksem_drop_proc(kp, ks);
    620       1.20        ad 	rw_exit(&kp->kp_lock);
    621       1.21        ad 	kmem_free(ksr, sizeof(*ksr));
    622        1.3   thorpej 
    623        1.3   thorpej 	return (0);
    624        1.1  christos }
    625        1.1  christos 
    626        1.1  christos int
    627       1.23       dsl sys__ksem_post(struct lwp *l, const struct sys__ksem_post_args *uap, register_t *retval)
    628        1.1  christos {
    629       1.23       dsl 	/* {
    630  1.24.10.2      yamt 		intptr_t id;
    631       1.23       dsl 	} */
    632        1.3   thorpej 	struct ksem_proc *kp;
    633        1.1  christos 	struct ksem *ks;
    634        1.1  christos 	int error;
    635        1.1  christos 
    636       1.16   thorpej 	kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
    637       1.16   thorpej 	if (kp == NULL)
    638        1.3   thorpej 		return (EINVAL);
    639        1.3   thorpej 
    640       1.20        ad 	rw_enter(&kp->kp_lock, RW_READER);
    641        1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    642       1.20        ad 	rw_exit(&kp->kp_lock);
    643        1.3   thorpej 	if (ks == NULL)
    644        1.3   thorpej 		return (EINVAL);
    645        1.3   thorpej 
    646       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    647        1.1  christos 	if (ks->ks_value == SEM_VALUE_MAX) {
    648        1.1  christos 		error = EOVERFLOW;
    649        1.3   thorpej 		goto out;
    650        1.1  christos 	}
    651        1.1  christos 	++ks->ks_value;
    652        1.3   thorpej 	if (ks->ks_waiters)
    653       1.20        ad 		cv_broadcast(&ks->ks_cv);
    654        1.1  christos 	error = 0;
    655        1.3   thorpej  out:
    656       1.20        ad 	mutex_exit(&ks->ks_interlock);
    657        1.3   thorpej 	return (error);
    658        1.3   thorpej }
    659        1.3   thorpej 
    660        1.3   thorpej static int
    661  1.24.10.2      yamt ksem_wait(struct lwp *l, intptr_t id, int tryflag)
    662        1.3   thorpej {
    663        1.3   thorpej 	struct ksem_proc *kp;
    664        1.3   thorpej 	struct ksem *ks;
    665        1.3   thorpej 	int error;
    666        1.3   thorpej 
    667       1.16   thorpej 	kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
    668       1.16   thorpej 	if (kp == NULL)
    669        1.3   thorpej 		return (EINVAL);
    670        1.3   thorpej 
    671       1.20        ad 	rw_enter(&kp->kp_lock, RW_READER);
    672        1.3   thorpej 	ks = ksem_lookup_proc(kp, id);
    673       1.20        ad 	rw_exit(&kp->kp_lock);
    674        1.3   thorpej 	if (ks == NULL)
    675        1.3   thorpej 		return (EINVAL);
    676        1.3   thorpej 
    677       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    678        1.3   thorpej 	ksem_addref(ks);
    679        1.3   thorpej 	while (ks->ks_value == 0) {
    680        1.3   thorpej 		ks->ks_waiters++;
    681       1.20        ad 		if (tryflag)
    682       1.20        ad 			error = EAGAIN;
    683       1.20        ad 		else
    684       1.20        ad 			error = cv_wait_sig(&ks->ks_cv, &ks->ks_interlock);
    685        1.3   thorpej 		ks->ks_waiters--;
    686        1.3   thorpej 		if (error)
    687        1.3   thorpej 			goto out;
    688        1.3   thorpej 	}
    689        1.3   thorpej 	ks->ks_value--;
    690        1.3   thorpej 	error = 0;
    691        1.3   thorpej  out:
    692        1.3   thorpej 	ksem_delref(ks);
    693        1.1  christos 	return (error);
    694        1.1  christos }
    695        1.1  christos 
    696        1.1  christos int
    697       1.23       dsl sys__ksem_wait(struct lwp *l, const struct sys__ksem_wait_args *uap, register_t *retval)
    698        1.1  christos {
    699       1.23       dsl 	/* {
    700  1.24.10.2      yamt 		intptr_t id;
    701       1.23       dsl 	} */
    702        1.1  christos 
    703        1.1  christos 	return ksem_wait(l, SCARG(uap, id), 0);
    704        1.1  christos }
    705        1.1  christos 
    706        1.1  christos int
    707       1.23       dsl sys__ksem_trywait(struct lwp *l, const struct sys__ksem_trywait_args *uap, register_t *retval)
    708        1.1  christos {
    709       1.23       dsl 	/* {
    710  1.24.10.2      yamt 		intptr_t id;
    711       1.23       dsl 	} */
    712        1.1  christos 
    713        1.1  christos 	return ksem_wait(l, SCARG(uap, id), 1);
    714        1.1  christos }
    715        1.1  christos 
    716        1.1  christos int
    717       1.23       dsl sys__ksem_getvalue(struct lwp *l, const struct sys__ksem_getvalue_args *uap, register_t *retval)
    718        1.1  christos {
    719       1.23       dsl 	/* {
    720  1.24.10.2      yamt 		intptr_t id;
    721        1.1  christos 		unsigned int *value;
    722       1.23       dsl 	} */
    723        1.3   thorpej 	struct ksem_proc *kp;
    724        1.1  christos 	struct ksem *ks;
    725        1.1  christos 	unsigned int val;
    726        1.1  christos 
    727       1.16   thorpej 	kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
    728       1.16   thorpej 	if (kp == NULL)
    729        1.3   thorpej 		return (EINVAL);
    730        1.3   thorpej 
    731       1.20        ad 	rw_enter(&kp->kp_lock, RW_READER);
    732        1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    733       1.20        ad 	rw_exit(&kp->kp_lock);
    734        1.3   thorpej 	if (ks == NULL)
    735        1.1  christos 		return (EINVAL);
    736        1.3   thorpej 
    737       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    738        1.1  christos 	val = ks->ks_value;
    739       1.20        ad 	mutex_exit(&ks->ks_interlock);
    740        1.3   thorpej 
    741        1.3   thorpej 	return (copyout(&val, SCARG(uap, value), sizeof(val)));
    742        1.1  christos }
    743        1.1  christos 
    744        1.1  christos int
    745       1.23       dsl sys__ksem_destroy(struct lwp *l, const struct sys__ksem_destroy_args *uap, register_t *retval)
    746        1.1  christos {
    747       1.23       dsl 	/* {
    748  1.24.10.2      yamt 		intptr_t id;
    749       1.23       dsl 	} */
    750        1.3   thorpej 	struct ksem_proc *kp;
    751        1.3   thorpej 	struct ksem_ref *ksr;
    752        1.1  christos 	struct ksem *ks;
    753        1.1  christos 
    754       1.16   thorpej 	kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
    755       1.16   thorpej 	if (kp == NULL)
    756        1.3   thorpej 		return (EINVAL);
    757        1.3   thorpej 
    758       1.20        ad 	rw_enter(&kp->kp_lock, RW_WRITER);
    759        1.3   thorpej 
    760        1.3   thorpej 	ks = ksem_lookup_proc(kp, SCARG(uap, id));
    761        1.3   thorpej 	if (ks == NULL) {
    762       1.20        ad 		rw_exit(&kp->kp_lock);
    763        1.3   thorpej 		return (EINVAL);
    764        1.3   thorpej 	}
    765        1.3   thorpej 
    766       1.21        ad 	KASSERT(mutex_owned(&ks->ks_interlock));
    767        1.3   thorpej 
    768        1.3   thorpej 	/*
    769        1.3   thorpej 	 * XXX This misses named semaphores which have been unlink'd,
    770        1.3   thorpej 	 * XXX but since behavior of destroying a named semaphore is
    771        1.3   thorpej 	 * XXX undefined, this is technically allowed.
    772        1.3   thorpej 	 */
    773        1.3   thorpej 	if (ks->ks_name != NULL) {
    774       1.20        ad 		mutex_exit(&ks->ks_interlock);
    775       1.20        ad 		rw_exit(&kp->kp_lock);
    776        1.3   thorpej 		return (EINVAL);
    777        1.3   thorpej 	}
    778        1.3   thorpej 
    779        1.3   thorpej 	if (ks->ks_waiters) {
    780       1.20        ad 		mutex_exit(&ks->ks_interlock);
    781       1.20        ad 		rw_exit(&kp->kp_lock);
    782        1.3   thorpej 		return (EBUSY);
    783        1.3   thorpej 	}
    784        1.3   thorpej 
    785        1.3   thorpej 	ksr = ksem_drop_proc(kp, ks);
    786       1.20        ad 	rw_exit(&kp->kp_lock);
    787       1.21        ad 	kmem_free(ksr, sizeof(*ksr));
    788        1.3   thorpej 
    789        1.3   thorpej 	return (0);
    790        1.3   thorpej }
    791        1.3   thorpej 
    792        1.3   thorpej static void
    793        1.3   thorpej ksem_forkhook(struct proc *p2, struct proc *p1)
    794        1.3   thorpej {
    795        1.3   thorpej 	struct ksem_proc *kp1, *kp2;
    796        1.3   thorpej 	struct ksem_ref *ksr, *ksr1;
    797        1.3   thorpej 
    798       1.16   thorpej 	kp1 = proc_getspecific(p1, ksem_specificdata_key);
    799       1.16   thorpej 	if (kp1 == NULL)
    800        1.3   thorpej 		return;
    801        1.3   thorpej 
    802       1.16   thorpej 	kp2 = ksem_proc_alloc();
    803        1.3   thorpej 
    804       1.20        ad 	rw_enter(&kp1->kp_lock, RW_READER);
    805        1.3   thorpej 
    806        1.3   thorpej 	if (!LIST_EMPTY(&kp1->kp_ksems)) {
    807        1.3   thorpej 		LIST_FOREACH(ksr, &kp1->kp_ksems, ksr_list) {
    808       1.21        ad 			ksr1 = kmem_alloc(sizeof(*ksr), KM_SLEEP);
    809        1.3   thorpej 			ksr1->ksr_ksem = ksr->ksr_ksem;
    810       1.20        ad 			mutex_enter(&ksr->ksr_ksem->ks_interlock);
    811        1.3   thorpej 			ksem_addref(ksr->ksr_ksem);
    812       1.20        ad 			mutex_exit(&ksr->ksr_ksem->ks_interlock);
    813        1.3   thorpej 			LIST_INSERT_HEAD(&kp2->kp_ksems, ksr1, ksr_list);
    814        1.3   thorpej 		}
    815        1.1  christos 	}
    816        1.3   thorpej 
    817       1.20        ad 	rw_exit(&kp1->kp_lock);
    818       1.16   thorpej 	proc_setspecific(p2, ksem_specificdata_key, kp2);
    819        1.1  christos }
    820        1.1  christos 
    821        1.1  christos static void
    822       1.18      yamt ksem_exechook(struct proc *p, void *arg)
    823        1.1  christos {
    824        1.3   thorpej 	struct ksem_proc *kp;
    825        1.1  christos 
    826       1.16   thorpej 	kp = proc_getspecific(p, ksem_specificdata_key);
    827       1.16   thorpej 	if (kp != NULL) {
    828       1.16   thorpej 		proc_setspecific(p, ksem_specificdata_key, NULL);
    829       1.16   thorpej 		ksem_proc_dtor(kp);
    830        1.1  christos 	}
    831        1.1  christos }
    832        1.1  christos 
    833  1.24.10.2      yamt static int
    834  1.24.10.2      yamt ksem_fini(bool interface)
    835  1.24.10.2      yamt {
    836  1.24.10.2      yamt 	int error;
    837  1.24.10.2      yamt 
    838  1.24.10.2      yamt 	if (interface) {
    839  1.24.10.2      yamt 		error = syscall_disestablish(NULL, ksem_syscalls);
    840  1.24.10.2      yamt 		if (error != 0) {
    841  1.24.10.2      yamt 			return error;
    842  1.24.10.2      yamt 		}
    843  1.24.10.2      yamt 		if (nsems != 0) {
    844  1.24.10.2      yamt 			error = syscall_establish(NULL, ksem_syscalls);
    845  1.24.10.2      yamt 			KASSERT(error == 0);
    846  1.24.10.2      yamt 			return EBUSY;
    847  1.24.10.2      yamt 		}
    848  1.24.10.2      yamt 	}
    849  1.24.10.2      yamt 	exechook_disestablish(ksem_ehook);
    850  1.24.10.2      yamt 	forkhook_disestablish(ksem_fhook);
    851  1.24.10.2      yamt 	proc_specific_key_delete(ksem_specificdata_key);
    852  1.24.10.2      yamt 	mutex_destroy(&ksem_mutex);
    853  1.24.10.2      yamt 	return 0;
    854  1.24.10.2      yamt }
    855  1.24.10.2      yamt 
    856  1.24.10.2      yamt static int
    857        1.1  christos ksem_init(void)
    858        1.1  christos {
    859  1.24.10.2      yamt 	int error, i;
    860        1.3   thorpej 
    861       1.20        ad 	mutex_init(&ksem_mutex, MUTEX_DEFAULT, IPL_NONE);
    862       1.13      cube 	for (i = 0; i < SEM_HASHTBL_SIZE; i++)
    863       1.13      cube 		LIST_INIT(&ksem_hash[i]);
    864       1.16   thorpej 	error = proc_specific_key_create(&ksem_specificdata_key,
    865  1.24.10.2      yamt 	    ksem_proc_dtor);
    866  1.24.10.2      yamt 	if (error != 0) {
    867  1.24.10.2      yamt 		mutex_destroy(&ksem_mutex);
    868  1.24.10.2      yamt 		return error;
    869  1.24.10.2      yamt 	}
    870  1.24.10.2      yamt 	ksem_ehook = exechook_establish(ksem_exechook, NULL);
    871  1.24.10.2      yamt 	ksem_fhook = forkhook_establish(ksem_forkhook);
    872  1.24.10.2      yamt 	error = syscall_establish(NULL, ksem_syscalls);
    873  1.24.10.2      yamt 	if (error != 0) {
    874  1.24.10.2      yamt 		(void)ksem_fini(false);
    875  1.24.10.2      yamt 	}
    876  1.24.10.2      yamt 	return error;
    877        1.1  christos }
    878       1.22     rmind 
    879  1.24.10.2      yamt static int
    880  1.24.10.2      yamt ksem_modcmd(modcmd_t cmd, void *arg)
    881       1.22     rmind {
    882       1.22     rmind 
    883  1.24.10.2      yamt 	switch (cmd) {
    884  1.24.10.2      yamt 	case MODULE_CMD_INIT:
    885  1.24.10.2      yamt 		return ksem_init();
    886       1.22     rmind 
    887  1.24.10.2      yamt 	case MODULE_CMD_FINI:
    888  1.24.10.2      yamt 		return ksem_fini(true);
    889       1.22     rmind 
    890  1.24.10.2      yamt 	default:
    891  1.24.10.2      yamt 		return ENOTTY;
    892  1.24.10.2      yamt 	}
    893       1.22     rmind }
    894