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