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