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