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