Home | History | Annotate | Line # | Download | only in kern
uipc_sem.c revision 1.38
      1  1.38      elad /*	$NetBSD: uipc_sem.c,v 1.38 2012/03/13 18:40:55 elad Exp $	*/
      2   1.3   thorpej 
      3   1.3   thorpej /*-
      4  1.30     rmind  * Copyright (c) 2011 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.30     rmind  * by Mindaugas Rasiukevicius.
      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.30     rmind /*
     59  1.30     rmind  * Implementation of POSIX semaphore.
     60  1.30     rmind  */
     61  1.30     rmind 
     62   1.9     lukem #include <sys/cdefs.h>
     63  1.38      elad __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.38 2012/03/13 18:40:55 elad Exp $");
     64   1.1  christos 
     65   1.1  christos #include <sys/param.h>
     66   1.1  christos #include <sys/kernel.h>
     67  1.34     rmind 
     68  1.34     rmind #include <sys/atomic.h>
     69   1.1  christos #include <sys/proc.h>
     70   1.1  christos #include <sys/ksem.h>
     71   1.1  christos #include <sys/syscall.h>
     72   1.1  christos #include <sys/stat.h>
     73  1.21        ad #include <sys/kmem.h>
     74   1.1  christos #include <sys/fcntl.h>
     75  1.30     rmind #include <sys/file.h>
     76  1.30     rmind #include <sys/filedesc.h>
     77  1.14      elad #include <sys/kauth.h>
     78  1.27        ad #include <sys/module.h>
     79   1.1  christos #include <sys/mount.h>
     80  1.27        ad #include <sys/syscall.h>
     81   1.1  christos #include <sys/syscallargs.h>
     82  1.27        ad #include <sys/syscallvar.h>
     83   1.1  christos 
     84  1.30     rmind MODULE(MODULE_CLASS_MISC, ksem, NULL);
     85  1.30     rmind 
     86  1.30     rmind #define	SEM_MAX_NAMELEN		14
     87  1.30     rmind #define	SEM_VALUE_MAX		(~0U)
     88   1.1  christos 
     89  1.30     rmind #define	KS_UNLINKED		0x01
     90   1.4   thorpej 
     91  1.30     rmind typedef struct ksem {
     92  1.30     rmind 	LIST_ENTRY(ksem)	ks_entry;	/* global list entry */
     93  1.30     rmind 	kmutex_t		ks_lock;	/* lock on this ksem */
     94  1.30     rmind 	kcondvar_t		ks_cv;		/* condition variable */
     95  1.30     rmind 	u_int			ks_ref;		/* number of references */
     96  1.30     rmind 	u_int			ks_value;	/* current value */
     97  1.30     rmind 	u_int			ks_waiters;	/* number of waiters */
     98  1.30     rmind 	char *			ks_name;	/* name, if named */
     99  1.30     rmind 	size_t			ks_namelen;	/* length of name */
    100  1.30     rmind 	int			ks_flags;	/* for KS_UNLINKED */
    101  1.30     rmind 	mode_t			ks_mode;	/* protection bits */
    102  1.30     rmind 	uid_t			ks_uid;		/* creator uid */
    103  1.30     rmind 	gid_t			ks_gid;		/* creator gid */
    104  1.30     rmind } ksem_t;
    105  1.30     rmind 
    106  1.30     rmind static kmutex_t		ksem_lock	__cacheline_aligned;
    107  1.30     rmind static LIST_HEAD(,ksem)	ksem_head	__cacheline_aligned;
    108  1.34     rmind static u_int		nsems_total	__cacheline_aligned;
    109  1.30     rmind static u_int		nsems		__cacheline_aligned;
    110  1.30     rmind 
    111  1.38      elad static kauth_listener_t	ksem_listener;
    112  1.38      elad 
    113  1.30     rmind static int		ksem_sysinit(void);
    114  1.30     rmind static int		ksem_sysfini(bool);
    115  1.30     rmind static int		ksem_modcmd(modcmd_t, void *);
    116  1.30     rmind static int		ksem_close_fop(file_t *);
    117  1.30     rmind 
    118  1.30     rmind static const struct fileops semops = {
    119  1.30     rmind 	.fo_read = fbadop_read,
    120  1.30     rmind 	.fo_write = fbadop_write,
    121  1.30     rmind 	.fo_ioctl = fbadop_ioctl,
    122  1.30     rmind 	.fo_fcntl = fnullop_fcntl,
    123  1.30     rmind 	.fo_poll = fnullop_poll,
    124  1.30     rmind 	.fo_stat = fbadop_stat,
    125  1.30     rmind 	.fo_close = ksem_close_fop,
    126  1.30     rmind 	.fo_kqfilter = fnullop_kqfilter,
    127  1.30     rmind 	.fo_restart = fnullop_restart,
    128  1.30     rmind };
    129  1.27        ad 
    130  1.27        ad static const struct syscall_package ksem_syscalls[] = {
    131  1.27        ad 	{ SYS__ksem_init, 0, (sy_call_t *)sys__ksem_init },
    132  1.27        ad 	{ SYS__ksem_open, 0, (sy_call_t *)sys__ksem_open },
    133  1.27        ad 	{ SYS__ksem_unlink, 0, (sy_call_t *)sys__ksem_unlink },
    134  1.27        ad 	{ SYS__ksem_close, 0, (sy_call_t *)sys__ksem_close },
    135  1.27        ad 	{ SYS__ksem_post, 0, (sy_call_t *)sys__ksem_post },
    136  1.27        ad 	{ SYS__ksem_wait, 0, (sy_call_t *)sys__ksem_wait },
    137  1.27        ad 	{ SYS__ksem_trywait, 0, (sy_call_t *)sys__ksem_trywait },
    138  1.27        ad 	{ SYS__ksem_getvalue, 0, (sy_call_t *)sys__ksem_getvalue },
    139  1.27        ad 	{ SYS__ksem_destroy, 0, (sy_call_t *)sys__ksem_destroy },
    140  1.36     joerg 	{ SYS__ksem_timedwait, 0, (sy_call_t *)sys__ksem_timedwait },
    141  1.27        ad 	{ 0, 0, NULL },
    142  1.27        ad };
    143   1.1  christos 
    144  1.30     rmind static int
    145  1.38      elad ksem_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    146  1.38      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    147  1.38      elad {
    148  1.38      elad 	ksem_t *ks;
    149  1.38      elad 	mode_t mode;
    150  1.38      elad 
    151  1.38      elad 	if (action != KAUTH_SYSTEM_SEMAPHORE)
    152  1.38      elad 		return KAUTH_RESULT_DEFER;
    153  1.38      elad 
    154  1.38      elad 	ks = arg1;
    155  1.38      elad 	mode = ks->ks_mode;
    156  1.38      elad 
    157  1.38      elad 	if ((kauth_cred_geteuid(cred) == ks->ks_uid && (mode & S_IWUSR) != 0) ||
    158  1.38      elad 	    (kauth_cred_getegid(cred) == ks->ks_gid && (mode & S_IWGRP) != 0) ||
    159  1.38      elad 	    (mode & S_IWOTH) != 0)
    160  1.38      elad 		return KAUTH_RESULT_ALLOW;
    161  1.38      elad 
    162  1.38      elad 	return KAUTH_RESULT_DEFER;
    163  1.38      elad }
    164  1.38      elad 
    165  1.38      elad static int
    166  1.30     rmind ksem_sysinit(void)
    167   1.3   thorpej {
    168  1.30     rmind 	int error;
    169   1.1  christos 
    170  1.30     rmind 	mutex_init(&ksem_lock, MUTEX_DEFAULT, IPL_NONE);
    171  1.30     rmind 	LIST_INIT(&ksem_head);
    172  1.34     rmind 	nsems_total = 0;
    173  1.34     rmind 	nsems = 0;
    174  1.20        ad 
    175  1.30     rmind 	error = syscall_establish(NULL, ksem_syscalls);
    176  1.30     rmind 	if (error) {
    177  1.30     rmind 		(void)ksem_sysfini(false);
    178   1.3   thorpej 	}
    179  1.38      elad 
    180  1.38      elad 	ksem_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    181  1.38      elad 	    ksem_listener_cb, NULL);
    182  1.38      elad 
    183  1.30     rmind 	return error;
    184   1.3   thorpej }
    185   1.1  christos 
    186  1.30     rmind static int
    187  1.30     rmind ksem_sysfini(bool interface)
    188   1.1  christos {
    189  1.30     rmind 	int error;
    190   1.1  christos 
    191  1.30     rmind 	if (interface) {
    192  1.30     rmind 		error = syscall_disestablish(NULL, ksem_syscalls);
    193  1.30     rmind 		if (error != 0) {
    194  1.30     rmind 			return error;
    195  1.30     rmind 		}
    196  1.34     rmind 		/*
    197  1.34     rmind 		 * Make sure that no semaphores are in use.  Note: semops
    198  1.34     rmind 		 * must be unused at this point.
    199  1.34     rmind 		 */
    200  1.34     rmind 		if (nsems_total) {
    201  1.30     rmind 			error = syscall_establish(NULL, ksem_syscalls);
    202  1.30     rmind 			KASSERT(error == 0);
    203  1.30     rmind 			return EBUSY;
    204  1.30     rmind 		}
    205   1.3   thorpej 	}
    206  1.38      elad 	kauth_unlisten_scope(ksem_listener);
    207  1.30     rmind 	mutex_destroy(&ksem_lock);
    208  1.30     rmind 	return 0;
    209   1.3   thorpej }
    210   1.3   thorpej 
    211  1.30     rmind static int
    212  1.30     rmind ksem_modcmd(modcmd_t cmd, void *arg)
    213   1.3   thorpej {
    214   1.3   thorpej 
    215  1.30     rmind 	switch (cmd) {
    216  1.30     rmind 	case MODULE_CMD_INIT:
    217  1.30     rmind 		return ksem_sysinit();
    218   1.3   thorpej 
    219  1.30     rmind 	case MODULE_CMD_FINI:
    220  1.30     rmind 		return ksem_sysfini(true);
    221   1.1  christos 
    222  1.30     rmind 	default:
    223  1.30     rmind 		return ENOTTY;
    224  1.16   thorpej 	}
    225  1.16   thorpej }
    226  1.16   thorpej 
    227  1.30     rmind static ksem_t *
    228  1.30     rmind ksem_lookup(const char *name)
    229   1.3   thorpej {
    230  1.30     rmind 	ksem_t *ks;
    231   1.3   thorpej 
    232  1.30     rmind 	KASSERT(mutex_owned(&ksem_lock));
    233   1.3   thorpej 
    234  1.30     rmind 	LIST_FOREACH(ks, &ksem_head, ks_entry) {
    235  1.30     rmind 		if (strcmp(ks->ks_name, name) == 0) {
    236  1.30     rmind 			mutex_enter(&ks->ks_lock);
    237  1.30     rmind 			return ks;
    238   1.3   thorpej 		}
    239   1.1  christos 	}
    240  1.30     rmind 	return NULL;
    241   1.1  christos }
    242   1.1  christos 
    243   1.3   thorpej static int
    244  1.30     rmind ksem_perm(lwp_t *l, ksem_t *ks)
    245   1.3   thorpej {
    246  1.30     rmind 	kauth_cred_t uc = l->l_cred;
    247   1.3   thorpej 
    248  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    249  1.30     rmind 
    250  1.38      elad 	if (kauth_authorize_system(uc, KAUTH_SYSTEM_SEMAPHORE, 0, ks, NULL, NULL) != 0)
    251  1.38      elad 		return EACCES;
    252  1.38      elad 
    253  1.38      elad 	return 0;
    254   1.3   thorpej }
    255   1.3   thorpej 
    256  1.30     rmind /*
    257  1.30     rmind  * ksem_get: get the semaphore from the descriptor.
    258  1.30     rmind  *
    259  1.30     rmind  * => locks the semaphore, if found.
    260  1.30     rmind  * => holds a reference on the file descriptor.
    261  1.30     rmind  */
    262  1.30     rmind static int
    263  1.30     rmind ksem_get(int fd, ksem_t **ksret)
    264  1.13      cube {
    265  1.30     rmind 	ksem_t *ks;
    266  1.30     rmind 	file_t *fp;
    267  1.13      cube 
    268  1.30     rmind 	fp = fd_getfile(fd);
    269  1.37     joerg 	if (__predict_false(fp == NULL))
    270  1.37     joerg 		return EINVAL;
    271  1.30     rmind 	if (__predict_false(fp->f_type != DTYPE_SEM)) {
    272  1.30     rmind 		fd_putfile(fd);
    273  1.37     joerg 		return EINVAL;
    274  1.13      cube 	}
    275  1.30     rmind 	ks = fp->f_data;
    276  1.30     rmind 	mutex_enter(&ks->ks_lock);
    277  1.13      cube 
    278  1.30     rmind 	*ksret = ks;
    279  1.30     rmind 	return 0;
    280   1.1  christos }
    281   1.1  christos 
    282  1.30     rmind /*
    283  1.30     rmind  * ksem_create: allocate and setup a new semaphore structure.
    284  1.30     rmind  */
    285   1.1  christos static int
    286  1.30     rmind ksem_create(lwp_t *l, const char *name, ksem_t **ksret, mode_t mode, u_int val)
    287   1.1  christos {
    288  1.30     rmind 	ksem_t *ks;
    289  1.14      elad 	kauth_cred_t uc;
    290  1.30     rmind 	char *kname;
    291   1.1  christos 	size_t len;
    292   1.1  christos 
    293  1.30     rmind 	/* Pre-check for the limit. */
    294  1.30     rmind 	if (nsems >= ksem_max) {
    295  1.30     rmind 		return ENFILE;
    296  1.30     rmind 	}
    297  1.30     rmind 
    298  1.30     rmind 	if (val > SEM_VALUE_MAX) {
    299  1.30     rmind 		return EINVAL;
    300  1.30     rmind 	}
    301  1.30     rmind 
    302   1.1  christos 	if (name != NULL) {
    303   1.1  christos 		len = strlen(name);
    304   1.1  christos 		if (len > SEM_MAX_NAMELEN) {
    305  1.30     rmind 			return ENAMETOOLONG;
    306   1.1  christos 		}
    307  1.30     rmind 		/* Name must start with a '/' but not contain one. */
    308   1.1  christos 		if (*name != '/' || len < 2 || strchr(name + 1, '/') != NULL) {
    309  1.30     rmind 			return EINVAL;
    310   1.1  christos 		}
    311  1.30     rmind 		kname = kmem_alloc(++len, KM_SLEEP);
    312  1.30     rmind 		strlcpy(kname, name, len);
    313  1.30     rmind 	} else {
    314  1.30     rmind 		kname = NULL;
    315  1.30     rmind 		len = 0;
    316  1.30     rmind 	}
    317  1.30     rmind 
    318  1.30     rmind 	ks = kmem_zalloc(sizeof(ksem_t), KM_SLEEP);
    319  1.30     rmind 	mutex_init(&ks->ks_lock, MUTEX_DEFAULT, IPL_NONE);
    320  1.30     rmind 	cv_init(&ks->ks_cv, "psem");
    321  1.30     rmind 	ks->ks_name = kname;
    322  1.30     rmind 	ks->ks_namelen = len;
    323  1.30     rmind 	ks->ks_mode = mode;
    324  1.30     rmind 	ks->ks_value = val;
    325  1.30     rmind 	ks->ks_ref = 1;
    326  1.30     rmind 
    327  1.30     rmind 	uc = l->l_cred;
    328  1.30     rmind 	ks->ks_uid = kauth_cred_geteuid(uc);
    329  1.30     rmind 	ks->ks_gid = kauth_cred_getegid(uc);
    330  1.30     rmind 
    331  1.34     rmind 	atomic_inc_uint(&nsems_total);
    332  1.30     rmind 	*ksret = ks;
    333  1.30     rmind 	return 0;
    334  1.30     rmind }
    335  1.30     rmind 
    336  1.30     rmind static void
    337  1.30     rmind ksem_free(ksem_t *ks)
    338  1.30     rmind {
    339   1.3   thorpej 
    340  1.34     rmind 	KASSERT(!cv_has_waiters(&ks->ks_cv));
    341  1.34     rmind 
    342  1.30     rmind 	if (ks->ks_name) {
    343  1.30     rmind 		KASSERT(ks->ks_namelen > 0);
    344  1.30     rmind 		kmem_free(ks->ks_name, ks->ks_namelen);
    345  1.13      cube 	}
    346  1.30     rmind 	mutex_destroy(&ks->ks_lock);
    347  1.30     rmind 	cv_destroy(&ks->ks_cv);
    348  1.30     rmind 	kmem_free(ks, sizeof(ksem_t));
    349  1.34     rmind 
    350  1.34     rmind 	atomic_dec_uint(&nsems_total);
    351   1.1  christos }
    352   1.1  christos 
    353   1.1  christos int
    354  1.30     rmind sys__ksem_init(struct lwp *l, const struct sys__ksem_init_args *uap,
    355  1.30     rmind     register_t *retval)
    356   1.1  christos {
    357  1.23       dsl 	/* {
    358   1.1  christos 		unsigned int value;
    359  1.29        ad 		intptr_t *idp;
    360  1.23       dsl 	} */
    361  1.13      cube 
    362  1.13      cube 	return do_ksem_init(l, SCARG(uap, value), SCARG(uap, idp), copyout);
    363  1.13      cube }
    364  1.13      cube 
    365  1.13      cube int
    366  1.30     rmind do_ksem_init(lwp_t *l, u_int val, intptr_t *idp, copyout_t docopyout)
    367  1.13      cube {
    368  1.30     rmind 	proc_t *p = l->l_proc;
    369  1.30     rmind 	ksem_t *ks;
    370  1.30     rmind 	file_t *fp;
    371  1.29        ad 	intptr_t id;
    372  1.30     rmind 	int fd, error;
    373   1.1  christos 
    374  1.30     rmind 	error = fd_allocfile(&fp, &fd);
    375   1.1  christos 	if (error) {
    376  1.30     rmind 		return error;
    377   1.1  christos 	}
    378  1.30     rmind 	fp->f_type = DTYPE_SEM;
    379  1.30     rmind 	fp->f_flag = FREAD | FWRITE;
    380  1.30     rmind 	fp->f_ops = &semops;
    381   1.3   thorpej 
    382  1.30     rmind 	id = (intptr_t)fd;
    383  1.30     rmind 	error = (*docopyout)(&id, idp, sizeof(*idp));
    384  1.30     rmind 	if (error) {
    385  1.30     rmind 		fd_abort(p, fp, fd);
    386  1.30     rmind 		return error;
    387  1.30     rmind 	}
    388   1.3   thorpej 
    389  1.30     rmind 	/* Note the mode does not matter for anonymous semaphores. */
    390  1.30     rmind 	error = ksem_create(l, NULL, &ks, 0, val);
    391  1.30     rmind 	if (error) {
    392  1.30     rmind 		fd_abort(p, fp, fd);
    393  1.30     rmind 		return error;
    394  1.30     rmind 	}
    395  1.30     rmind 	fp->f_data = ks;
    396  1.30     rmind 	fd_affix(p, fp, fd);
    397  1.30     rmind 	return error;
    398   1.1  christos }
    399   1.1  christos 
    400   1.1  christos int
    401  1.30     rmind sys__ksem_open(struct lwp *l, const struct sys__ksem_open_args *uap,
    402  1.30     rmind     register_t *retval)
    403   1.1  christos {
    404  1.23       dsl 	/* {
    405   1.1  christos 		const char *name;
    406   1.1  christos 		int oflag;
    407   1.1  christos 		mode_t mode;
    408   1.1  christos 		unsigned int value;
    409  1.29        ad 		intptr_t *idp;
    410  1.23       dsl 	} */
    411  1.13      cube 
    412  1.13      cube 	return do_ksem_open(l, SCARG(uap, name), SCARG(uap, oflag),
    413  1.13      cube 	    SCARG(uap, mode), SCARG(uap, value), SCARG(uap, idp), copyout);
    414  1.13      cube }
    415  1.13      cube 
    416  1.13      cube int
    417  1.13      cube do_ksem_open(struct lwp *l, const char *semname, int oflag, mode_t mode,
    418  1.29        ad      unsigned int value, intptr_t *idp, copyout_t docopyout)
    419  1.13      cube {
    420   1.1  christos 	char name[SEM_MAX_NAMELEN + 1];
    421  1.30     rmind 	proc_t *p = l->l_proc;
    422  1.30     rmind 	ksem_t *ksnew = NULL, *ks;
    423  1.30     rmind 	file_t *fp;
    424  1.29        ad 	intptr_t id;
    425  1.30     rmind 	int fd, error;
    426   1.1  christos 
    427  1.30     rmind 	error = copyinstr(semname, name, sizeof(name), NULL);
    428  1.30     rmind 	if (error) {
    429  1.30     rmind 		return error;
    430  1.30     rmind 	}
    431  1.30     rmind 	error = fd_allocfile(&fp, &fd);
    432  1.30     rmind 	if (error) {
    433  1.30     rmind 		return error;
    434  1.30     rmind 	}
    435  1.30     rmind 	fp->f_type = DTYPE_SEM;
    436  1.30     rmind 	fp->f_flag = FREAD | FWRITE;
    437  1.30     rmind 	fp->f_ops = &semops;
    438  1.30     rmind 
    439  1.30     rmind 	/*
    440  1.30     rmind 	 * The ID (file descriptor number) can be stored early.
    441  1.30     rmind 	 * Note that zero is a special value for libpthread.
    442  1.30     rmind 	 */
    443  1.30     rmind 	id = (intptr_t)fd;
    444  1.30     rmind 	error = (*docopyout)(&id, idp, sizeof(*idp));
    445  1.30     rmind 	if (error) {
    446  1.30     rmind 		goto err;
    447  1.30     rmind 	}
    448  1.30     rmind 
    449  1.30     rmind 	if (oflag & O_CREAT) {
    450  1.30     rmind 		/* Create a new semaphore. */
    451  1.30     rmind 		error = ksem_create(l, name, &ksnew, mode, value);
    452  1.30     rmind 		if (error) {
    453  1.30     rmind 			goto err;
    454  1.30     rmind 		}
    455  1.30     rmind 		KASSERT(ksnew != NULL);
    456  1.30     rmind 	}
    457   1.1  christos 
    458  1.30     rmind 	/* Lookup for a semaphore with such name. */
    459  1.30     rmind 	mutex_enter(&ksem_lock);
    460  1.30     rmind 	ks = ksem_lookup(name);
    461  1.30     rmind 	if (ks) {
    462  1.30     rmind 		KASSERT(mutex_owned(&ks->ks_lock));
    463  1.30     rmind 		mutex_exit(&ksem_lock);
    464   1.3   thorpej 
    465   1.3   thorpej 		/* Check for exclusive create. */
    466  1.13      cube 		if (oflag & O_EXCL) {
    467  1.30     rmind 			mutex_exit(&ks->ks_lock);
    468  1.30     rmind 			error = EEXIST;
    469  1.30     rmind 			goto err;
    470   1.1  christos 		}
    471   1.1  christos 		/*
    472  1.30     rmind 		 * Verify permissions.  If we can access it,
    473  1.30     rmind 		 * add the reference of this thread.
    474   1.1  christos 		 */
    475  1.15        ad 		error = ksem_perm(l, ks);
    476  1.30     rmind 		if (error == 0) {
    477  1.30     rmind 			ks->ks_ref++;
    478  1.30     rmind 		}
    479  1.30     rmind 		mutex_exit(&ks->ks_lock);
    480   1.1  christos 		if (error) {
    481  1.30     rmind 			goto err;
    482  1.30     rmind 		}
    483  1.30     rmind 	} else {
    484  1.30     rmind 		/* Fail if not found and not creating. */
    485  1.30     rmind 		if ((oflag & O_CREAT) == 0) {
    486  1.30     rmind 			mutex_exit(&ksem_lock);
    487  1.30     rmind 			KASSERT(ksnew == NULL);
    488  1.31     rmind 			error = ENOENT;
    489  1.31     rmind 			goto err;
    490   1.1  christos 		}
    491   1.3   thorpej 
    492  1.30     rmind 		/* Check for the limit locked. */
    493  1.30     rmind 		if (nsems >= ksem_max) {
    494  1.30     rmind 			mutex_exit(&ksem_lock);
    495  1.30     rmind 			error = ENFILE;
    496  1.30     rmind 			goto err;
    497  1.30     rmind 		}
    498   1.3   thorpej 
    499  1.30     rmind 		/*
    500  1.32     rmind 		 * Finally, insert semaphore into the list.
    501  1.30     rmind 		 * Note: it already has the initial reference.
    502  1.30     rmind 		 */
    503  1.30     rmind 		ks = ksnew;
    504  1.30     rmind 		LIST_INSERT_HEAD(&ksem_head, ks, ks_entry);
    505  1.30     rmind 		nsems++;
    506  1.30     rmind 		mutex_exit(&ksem_lock);
    507  1.30     rmind 
    508  1.30     rmind 		ksnew = NULL;
    509  1.30     rmind 	}
    510  1.30     rmind 	KASSERT(ks != NULL);
    511  1.30     rmind 	fp->f_data = ks;
    512  1.30     rmind 	fd_affix(p, fp, fd);
    513  1.30     rmind err:
    514  1.30     rmind 	if (error) {
    515  1.30     rmind 		fd_abort(p, fp, fd);
    516   1.3   thorpej 	}
    517  1.30     rmind 	if (ksnew) {
    518  1.30     rmind 		ksem_free(ksnew);
    519   1.1  christos 	}
    520  1.30     rmind 	return error;
    521  1.30     rmind }
    522   1.1  christos 
    523  1.30     rmind int
    524  1.30     rmind sys__ksem_close(struct lwp *l, const struct sys__ksem_close_args *uap,
    525  1.30     rmind     register_t *retval)
    526  1.30     rmind {
    527  1.30     rmind 	/* {
    528  1.30     rmind 		intptr_t id;
    529  1.30     rmind 	} */
    530  1.33     rmind 	int fd = (int)SCARG(uap, id);
    531  1.33     rmind 
    532  1.33     rmind 	if (fd_getfile(fd) == NULL) {
    533  1.33     rmind 		return EBADF;
    534  1.33     rmind 	}
    535  1.33     rmind 	return fd_close(fd);
    536   1.1  christos }
    537   1.1  christos 
    538  1.30     rmind static int
    539  1.30     rmind ksem_close_fop(file_t *fp)
    540   1.1  christos {
    541  1.30     rmind 	ksem_t *ks = fp->f_data;
    542  1.30     rmind 	bool destroy = false;
    543   1.1  christos 
    544  1.30     rmind 	mutex_enter(&ks->ks_lock);
    545  1.30     rmind 	KASSERT(ks->ks_ref > 0);
    546  1.30     rmind 	if (--ks->ks_ref == 0) {
    547  1.30     rmind 		/*
    548  1.30     rmind 		 * Destroy if the last reference and semaphore is unnamed,
    549  1.30     rmind 		 * or unlinked (for named semaphore).
    550  1.30     rmind 		 */
    551  1.30     rmind 		destroy = (ks->ks_flags & KS_UNLINKED) || (ks->ks_name == NULL);
    552   1.1  christos 	}
    553  1.30     rmind 	mutex_exit(&ks->ks_lock);
    554   1.3   thorpej 
    555  1.30     rmind 	if (destroy) {
    556  1.30     rmind 		ksem_free(ks);
    557  1.30     rmind 	}
    558  1.30     rmind 	return 0;
    559   1.1  christos }
    560   1.1  christos 
    561   1.1  christos int
    562  1.30     rmind sys__ksem_unlink(struct lwp *l, const struct sys__ksem_unlink_args *uap,
    563  1.30     rmind     register_t *retval)
    564   1.1  christos {
    565  1.23       dsl 	/* {
    566   1.1  christos 		const char *name;
    567  1.23       dsl 	} */
    568  1.30     rmind 	char name[SEM_MAX_NAMELEN + 1];
    569  1.30     rmind 	ksem_t *ks;
    570  1.30     rmind 	u_int refcnt;
    571   1.1  christos 	int error;
    572   1.1  christos 
    573  1.30     rmind 	error = copyinstr(SCARG(uap, name), name, sizeof(name), NULL);
    574   1.1  christos 	if (error)
    575   1.1  christos 		return error;
    576   1.1  christos 
    577  1.30     rmind 	mutex_enter(&ksem_lock);
    578  1.30     rmind 	ks = ksem_lookup(name);
    579   1.3   thorpej 	if (ks == NULL) {
    580  1.30     rmind 		mutex_exit(&ksem_lock);
    581  1.30     rmind 		return ENOENT;
    582   1.1  christos 	}
    583  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    584   1.3   thorpej 
    585  1.30     rmind 	/* Verify permissions. */
    586  1.30     rmind 	error = ksem_perm(l, ks);
    587  1.30     rmind 	if (error) {
    588  1.30     rmind 		mutex_exit(&ks->ks_lock);
    589  1.30     rmind 		mutex_exit(&ksem_lock);
    590  1.30     rmind 		return error;
    591  1.30     rmind 	}
    592   1.3   thorpej 
    593  1.31     rmind 	/* Remove from the global list. */
    594   1.3   thorpej 	LIST_REMOVE(ks, ks_entry);
    595  1.30     rmind 	nsems--;
    596  1.31     rmind 	mutex_exit(&ksem_lock);
    597   1.3   thorpej 
    598  1.30     rmind 	refcnt = ks->ks_ref;
    599  1.30     rmind 	if (refcnt) {
    600  1.30     rmind 		/* Mark as unlinked, if there are references. */
    601  1.30     rmind 		ks->ks_flags |= KS_UNLINKED;
    602  1.30     rmind 	}
    603  1.30     rmind 	mutex_exit(&ks->ks_lock);
    604   1.3   thorpej 
    605  1.30     rmind 	if (refcnt == 0) {
    606   1.3   thorpej 		ksem_free(ks);
    607  1.30     rmind 	}
    608  1.30     rmind 	return 0;
    609   1.1  christos }
    610   1.1  christos 
    611   1.1  christos int
    612  1.30     rmind sys__ksem_post(struct lwp *l, const struct sys__ksem_post_args *uap,
    613  1.30     rmind     register_t *retval)
    614   1.1  christos {
    615  1.23       dsl 	/* {
    616  1.29        ad 		intptr_t id;
    617  1.23       dsl 	} */
    618  1.30     rmind 	int fd = (int)SCARG(uap, id), error;
    619  1.30     rmind 	ksem_t *ks;
    620   1.1  christos 
    621  1.30     rmind 	error = ksem_get(fd, &ks);
    622  1.30     rmind 	if (error) {
    623  1.30     rmind 		return error;
    624   1.3   thorpej 	}
    625  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    626   1.1  christos 	if (ks->ks_value == SEM_VALUE_MAX) {
    627   1.1  christos 		error = EOVERFLOW;
    628   1.3   thorpej 		goto out;
    629   1.1  christos 	}
    630  1.30     rmind 	ks->ks_value++;
    631  1.30     rmind 	if (ks->ks_waiters) {
    632  1.20        ad 		cv_broadcast(&ks->ks_cv);
    633  1.30     rmind 	}
    634  1.30     rmind out:
    635  1.30     rmind 	mutex_exit(&ks->ks_lock);
    636  1.30     rmind 	fd_putfile(fd);
    637  1.30     rmind 	return error;
    638   1.3   thorpej }
    639   1.3   thorpej 
    640  1.36     joerg int
    641  1.36     joerg do_ksem_wait(lwp_t *l, intptr_t id, bool try, struct timespec *abstime)
    642   1.3   thorpej {
    643  1.36     joerg 	int fd = (int)id, error, timeo;
    644  1.30     rmind 	ksem_t *ks;
    645   1.3   thorpej 
    646  1.30     rmind 	error = ksem_get(fd, &ks);
    647  1.30     rmind 	if (error) {
    648  1.30     rmind 		return error;
    649  1.30     rmind 	}
    650  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    651   1.3   thorpej 	while (ks->ks_value == 0) {
    652   1.3   thorpej 		ks->ks_waiters++;
    653  1.36     joerg 		if (!try && abstime != NULL) {
    654  1.36     joerg 			error = abstimeout2timo(abstime, &timeo);
    655  1.36     joerg 			if (error != 0)
    656  1.36     joerg 				goto out;
    657  1.36     joerg 		} else {
    658  1.36     joerg 			timeo = 0;
    659  1.36     joerg 		}
    660  1.36     joerg 		error = try ? EAGAIN : cv_timedwait_sig(&ks->ks_cv,
    661  1.36     joerg 		    &ks->ks_lock, timeo);
    662   1.3   thorpej 		ks->ks_waiters--;
    663   1.3   thorpej 		if (error)
    664   1.3   thorpej 			goto out;
    665   1.3   thorpej 	}
    666   1.3   thorpej 	ks->ks_value--;
    667  1.30     rmind out:
    668  1.30     rmind 	mutex_exit(&ks->ks_lock);
    669  1.30     rmind 	fd_putfile(fd);
    670  1.30     rmind 	return error;
    671   1.1  christos }
    672   1.1  christos 
    673   1.1  christos int
    674  1.30     rmind sys__ksem_wait(struct lwp *l, const struct sys__ksem_wait_args *uap,
    675  1.30     rmind     register_t *retval)
    676   1.1  christos {
    677  1.23       dsl 	/* {
    678  1.29        ad 		intptr_t id;
    679  1.23       dsl 	} */
    680   1.1  christos 
    681  1.36     joerg 	return do_ksem_wait(l, SCARG(uap, id), false, NULL);
    682  1.36     joerg }
    683  1.36     joerg 
    684  1.36     joerg int
    685  1.36     joerg sys__ksem_timedwait(struct lwp *l, const struct sys__ksem_timedwait_args *uap,
    686  1.36     joerg     register_t *retval)
    687  1.36     joerg {
    688  1.36     joerg 	/* {
    689  1.36     joerg 		intptr_t id;
    690  1.36     joerg 		const struct timespec *abstime;
    691  1.36     joerg 	} */
    692  1.36     joerg 	struct timespec ts;
    693  1.36     joerg 	int error;
    694  1.36     joerg 
    695  1.36     joerg 	error = copyin(SCARG(uap, abstime), &ts, sizeof(ts));
    696  1.36     joerg 	if (error != 0)
    697  1.36     joerg 		return error;
    698  1.36     joerg 
    699  1.36     joerg 	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
    700  1.36     joerg 		return EINVAL;
    701  1.36     joerg 
    702  1.36     joerg 	error = do_ksem_wait(l, SCARG(uap, id), false, &ts);
    703  1.36     joerg 	if (error == EWOULDBLOCK)
    704  1.36     joerg 		error = ETIMEDOUT;
    705  1.36     joerg 	return error;
    706   1.1  christos }
    707   1.1  christos 
    708   1.1  christos int
    709  1.30     rmind sys__ksem_trywait(struct lwp *l, const struct sys__ksem_trywait_args *uap,
    710  1.30     rmind     register_t *retval)
    711   1.1  christos {
    712  1.23       dsl 	/* {
    713  1.29        ad 		intptr_t id;
    714  1.23       dsl 	} */
    715   1.1  christos 
    716  1.36     joerg 	return do_ksem_wait(l, SCARG(uap, id), true, NULL);
    717   1.1  christos }
    718   1.1  christos 
    719   1.1  christos int
    720  1.30     rmind sys__ksem_getvalue(struct lwp *l, const struct sys__ksem_getvalue_args *uap,
    721  1.30     rmind     register_t *retval)
    722   1.1  christos {
    723  1.23       dsl 	/* {
    724  1.29        ad 		intptr_t id;
    725   1.1  christos 		unsigned int *value;
    726  1.23       dsl 	} */
    727  1.30     rmind 	int fd = (int)SCARG(uap, id), error;
    728  1.30     rmind 	ksem_t *ks;
    729   1.1  christos 	unsigned int val;
    730   1.1  christos 
    731  1.30     rmind 	error = ksem_get(fd, &ks);
    732  1.30     rmind 	if (error) {
    733  1.30     rmind 		return error;
    734  1.30     rmind 	}
    735  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    736   1.1  christos 	val = ks->ks_value;
    737  1.30     rmind 	mutex_exit(&ks->ks_lock);
    738  1.30     rmind 	fd_putfile(fd);
    739   1.3   thorpej 
    740  1.30     rmind 	return copyout(&val, SCARG(uap, value), sizeof(val));
    741   1.1  christos }
    742   1.1  christos 
    743   1.1  christos int
    744  1.30     rmind sys__ksem_destroy(struct lwp *l, const struct sys__ksem_destroy_args *uap,
    745  1.30     rmind     register_t *retval)
    746   1.1  christos {
    747  1.23       dsl 	/* {
    748  1.29        ad 		intptr_t id;
    749  1.23       dsl 	} */
    750  1.30     rmind 	int fd = (int)SCARG(uap, id), error;
    751  1.30     rmind 	ksem_t *ks;
    752   1.1  christos 
    753  1.30     rmind 	error = ksem_get(fd, &ks);
    754  1.30     rmind 	if (error) {
    755  1.30     rmind 		return error;
    756   1.3   thorpej 	}
    757  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    758   1.3   thorpej 
    759  1.30     rmind 	/* Operation is only for unnamed semaphores. */
    760   1.3   thorpej 	if (ks->ks_name != NULL) {
    761  1.30     rmind 		error = EINVAL;
    762  1.30     rmind 		goto out;
    763   1.3   thorpej 	}
    764  1.30     rmind 	/* Cannot destroy if there are waiters. */
    765   1.3   thorpej 	if (ks->ks_waiters) {
    766  1.30     rmind 		error = EBUSY;
    767  1.30     rmind 		goto out;
    768   1.3   thorpej 	}
    769  1.30     rmind out:
    770  1.30     rmind 	mutex_exit(&ks->ks_lock);
    771  1.30     rmind 	if (error) {
    772  1.32     rmind 		fd_putfile(fd);
    773  1.27        ad 		return error;
    774  1.27        ad 	}
    775  1.32     rmind 	return fd_close(fd);
    776  1.22     rmind }
    777