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