Home | History | Annotate | Line # | Download | only in kern
uipc_sem.c revision 1.55
      1  1.55  christos /*	$NetBSD: uipc_sem.c,v 1.55 2019/03/01 03:03:19 christos Exp $	*/
      2   1.3   thorpej 
      3   1.3   thorpej /*-
      4  1.52   thorpej  * Copyright (c) 2011, 2019 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.52   thorpej  * by Mindaugas Rasiukevicius and Jason R. Thorpe.
      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.55  christos __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.55 2019/03/01 03:03:19 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.52   thorpej #include <sys/lwp.h>
     71   1.1  christos #include <sys/ksem.h>
     72   1.1  christos #include <sys/syscall.h>
     73   1.1  christos #include <sys/stat.h>
     74  1.21        ad #include <sys/kmem.h>
     75   1.1  christos #include <sys/fcntl.h>
     76  1.30     rmind #include <sys/file.h>
     77  1.30     rmind #include <sys/filedesc.h>
     78  1.14      elad #include <sys/kauth.h>
     79  1.27        ad #include <sys/module.h>
     80   1.1  christos #include <sys/mount.h>
     81  1.52   thorpej #include <sys/mutex.h>
     82  1.52   thorpej #include <sys/rwlock.h>
     83  1.45  dholland #include <sys/semaphore.h>
     84  1.27        ad #include <sys/syscall.h>
     85   1.1  christos #include <sys/syscallargs.h>
     86  1.27        ad #include <sys/syscallvar.h>
     87  1.43  pgoyette #include <sys/sysctl.h>
     88  1.55  christos #include <sys/uidinfo.h>
     89  1.52   thorpej #include <sys/cprng.h>
     90   1.1  christos 
     91  1.30     rmind MODULE(MODULE_CLASS_MISC, ksem, NULL);
     92  1.30     rmind 
     93  1.49  christos #define	SEM_MAX_NAMELEN		NAME_MAX
     94   1.1  christos 
     95  1.46  christos #define	SEM_NSEMS_MAX		256
     96  1.30     rmind #define	KS_UNLINKED		0x01
     97   1.4   thorpej 
     98  1.30     rmind static kmutex_t		ksem_lock	__cacheline_aligned;
     99  1.30     rmind static LIST_HEAD(,ksem)	ksem_head	__cacheline_aligned;
    100  1.34     rmind static u_int		nsems_total	__cacheline_aligned;
    101  1.30     rmind static u_int		nsems		__cacheline_aligned;
    102  1.30     rmind 
    103  1.52   thorpej static krwlock_t	ksem_pshared_lock __cacheline_aligned;
    104  1.52   thorpej static LIST_HEAD(, ksem) *ksem_pshared_hashtab __cacheline_aligned;
    105  1.52   thorpej static u_long		ksem_pshared_hashmask __read_mostly;
    106  1.52   thorpej 
    107  1.52   thorpej #define	KSEM_PSHARED_HASHSIZE	32
    108  1.52   thorpej 
    109  1.38      elad static kauth_listener_t	ksem_listener;
    110  1.38      elad 
    111  1.30     rmind static int		ksem_sysinit(void);
    112  1.30     rmind static int		ksem_sysfini(bool);
    113  1.30     rmind static int		ksem_modcmd(modcmd_t, void *);
    114  1.30     rmind static int		ksem_close_fop(file_t *);
    115  1.39  christos static int		ksem_stat_fop(file_t *, struct stat *);
    116  1.39  christos static int		ksem_read_fop(file_t *, off_t *, struct uio *,
    117  1.39  christos     kauth_cred_t, int);
    118  1.30     rmind 
    119  1.30     rmind static const struct fileops semops = {
    120  1.48  christos 	.fo_name = "sem",
    121  1.39  christos 	.fo_read = ksem_read_fop,
    122  1.30     rmind 	.fo_write = fbadop_write,
    123  1.30     rmind 	.fo_ioctl = fbadop_ioctl,
    124  1.30     rmind 	.fo_fcntl = fnullop_fcntl,
    125  1.30     rmind 	.fo_poll = fnullop_poll,
    126  1.39  christos 	.fo_stat = ksem_stat_fop,
    127  1.30     rmind 	.fo_close = ksem_close_fop,
    128  1.30     rmind 	.fo_kqfilter = fnullop_kqfilter,
    129  1.30     rmind 	.fo_restart = fnullop_restart,
    130  1.30     rmind };
    131  1.27        ad 
    132  1.27        ad static const struct syscall_package ksem_syscalls[] = {
    133  1.27        ad 	{ SYS__ksem_init, 0, (sy_call_t *)sys__ksem_init },
    134  1.27        ad 	{ SYS__ksem_open, 0, (sy_call_t *)sys__ksem_open },
    135  1.27        ad 	{ SYS__ksem_unlink, 0, (sy_call_t *)sys__ksem_unlink },
    136  1.27        ad 	{ SYS__ksem_close, 0, (sy_call_t *)sys__ksem_close },
    137  1.27        ad 	{ SYS__ksem_post, 0, (sy_call_t *)sys__ksem_post },
    138  1.27        ad 	{ SYS__ksem_wait, 0, (sy_call_t *)sys__ksem_wait },
    139  1.27        ad 	{ SYS__ksem_trywait, 0, (sy_call_t *)sys__ksem_trywait },
    140  1.27        ad 	{ SYS__ksem_getvalue, 0, (sy_call_t *)sys__ksem_getvalue },
    141  1.27        ad 	{ SYS__ksem_destroy, 0, (sy_call_t *)sys__ksem_destroy },
    142  1.36     joerg 	{ SYS__ksem_timedwait, 0, (sy_call_t *)sys__ksem_timedwait },
    143  1.27        ad 	{ 0, 0, NULL },
    144  1.27        ad };
    145   1.1  christos 
    146  1.43  pgoyette struct sysctllog *ksem_clog;
    147  1.43  pgoyette int ksem_max;
    148  1.43  pgoyette 
    149  1.30     rmind static int
    150  1.51  christos name_copyin(const char *uname, char **name)
    151  1.51  christos {
    152  1.51  christos 	*name = kmem_alloc(SEM_MAX_NAMELEN, KM_SLEEP);
    153  1.51  christos 
    154  1.51  christos 	int error = copyinstr(uname, *name, SEM_MAX_NAMELEN, NULL);
    155  1.51  christos 	if (error)
    156  1.51  christos 		kmem_free(*name, SEM_MAX_NAMELEN);
    157  1.51  christos 
    158  1.51  christos 	return error;
    159  1.51  christos }
    160  1.51  christos 
    161  1.51  christos static void
    162  1.51  christos name_destroy(char **name)
    163  1.51  christos {
    164  1.51  christos 	if (!*name)
    165  1.51  christos 		return;
    166  1.51  christos 
    167  1.51  christos 	kmem_free(*name, SEM_MAX_NAMELEN);
    168  1.51  christos 	*name = NULL;
    169  1.51  christos }
    170  1.51  christos 
    171  1.51  christos static int
    172  1.38      elad ksem_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    173  1.38      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    174  1.38      elad {
    175  1.38      elad 	ksem_t *ks;
    176  1.38      elad 	mode_t mode;
    177  1.38      elad 
    178  1.38      elad 	if (action != KAUTH_SYSTEM_SEMAPHORE)
    179  1.38      elad 		return KAUTH_RESULT_DEFER;
    180  1.38      elad 
    181  1.38      elad 	ks = arg1;
    182  1.38      elad 	mode = ks->ks_mode;
    183  1.38      elad 
    184  1.38      elad 	if ((kauth_cred_geteuid(cred) == ks->ks_uid && (mode & S_IWUSR) != 0) ||
    185  1.38      elad 	    (kauth_cred_getegid(cred) == ks->ks_gid && (mode & S_IWGRP) != 0) ||
    186  1.38      elad 	    (mode & S_IWOTH) != 0)
    187  1.38      elad 		return KAUTH_RESULT_ALLOW;
    188  1.38      elad 
    189  1.38      elad 	return KAUTH_RESULT_DEFER;
    190  1.38      elad }
    191  1.38      elad 
    192  1.38      elad static int
    193  1.30     rmind ksem_sysinit(void)
    194   1.3   thorpej {
    195  1.30     rmind 	int error;
    196  1.43  pgoyette 	const struct sysctlnode *rnode;
    197   1.1  christos 
    198  1.30     rmind 	mutex_init(&ksem_lock, MUTEX_DEFAULT, IPL_NONE);
    199  1.30     rmind 	LIST_INIT(&ksem_head);
    200  1.34     rmind 	nsems_total = 0;
    201  1.34     rmind 	nsems = 0;
    202  1.20        ad 
    203  1.52   thorpej 	rw_init(&ksem_pshared_lock);
    204  1.52   thorpej 	ksem_pshared_hashtab = hashinit(KSEM_PSHARED_HASHSIZE, HASH_LIST,
    205  1.52   thorpej 	    true, &ksem_pshared_hashmask);
    206  1.52   thorpej 	KASSERT(ksem_pshared_hashtab != NULL);
    207  1.52   thorpej 
    208  1.30     rmind 	error = syscall_establish(NULL, ksem_syscalls);
    209  1.30     rmind 	if (error) {
    210  1.30     rmind 		(void)ksem_sysfini(false);
    211   1.3   thorpej 	}
    212  1.38      elad 
    213  1.38      elad 	ksem_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    214  1.38      elad 	    ksem_listener_cb, NULL);
    215  1.38      elad 
    216  1.43  pgoyette 	/* Define module-specific sysctl tree */
    217  1.43  pgoyette 
    218  1.43  pgoyette 	ksem_max = KSEM_MAX;
    219  1.43  pgoyette 	ksem_clog = NULL;
    220  1.43  pgoyette 
    221  1.43  pgoyette 	sysctl_createv(&ksem_clog, 0, NULL, &rnode,
    222  1.43  pgoyette 			CTLFLAG_PERMANENT,
    223  1.43  pgoyette 			CTLTYPE_NODE, "posix",
    224  1.43  pgoyette 			SYSCTL_DESCR("POSIX options"),
    225  1.43  pgoyette 			NULL, 0, NULL, 0,
    226  1.43  pgoyette 			CTL_KERN, CTL_CREATE, CTL_EOL);
    227  1.43  pgoyette 	sysctl_createv(&ksem_clog, 0, &rnode, NULL,
    228  1.43  pgoyette 			CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    229  1.43  pgoyette 			CTLTYPE_INT, "semmax",
    230  1.43  pgoyette 			SYSCTL_DESCR("Maximal number of semaphores"),
    231  1.43  pgoyette 			NULL, 0, &ksem_max, 0,
    232  1.43  pgoyette 			CTL_CREATE, CTL_EOL);
    233  1.43  pgoyette 	sysctl_createv(&ksem_clog, 0, &rnode, NULL,
    234  1.44  pgoyette 			CTLFLAG_PERMANENT | CTLFLAG_READONLY,
    235  1.43  pgoyette 			CTLTYPE_INT, "semcnt",
    236  1.43  pgoyette 			SYSCTL_DESCR("Current number of semaphores"),
    237  1.43  pgoyette 			NULL, 0, &nsems, 0,
    238  1.43  pgoyette 			CTL_CREATE, CTL_EOL);
    239  1.43  pgoyette 
    240  1.30     rmind 	return error;
    241   1.3   thorpej }
    242   1.1  christos 
    243  1.30     rmind static int
    244  1.30     rmind ksem_sysfini(bool interface)
    245   1.1  christos {
    246  1.30     rmind 	int error;
    247   1.1  christos 
    248  1.30     rmind 	if (interface) {
    249  1.30     rmind 		error = syscall_disestablish(NULL, ksem_syscalls);
    250  1.30     rmind 		if (error != 0) {
    251  1.30     rmind 			return error;
    252  1.30     rmind 		}
    253  1.34     rmind 		/*
    254  1.34     rmind 		 * Make sure that no semaphores are in use.  Note: semops
    255  1.34     rmind 		 * must be unused at this point.
    256  1.34     rmind 		 */
    257  1.34     rmind 		if (nsems_total) {
    258  1.30     rmind 			error = syscall_establish(NULL, ksem_syscalls);
    259  1.30     rmind 			KASSERT(error == 0);
    260  1.30     rmind 			return EBUSY;
    261  1.30     rmind 		}
    262   1.3   thorpej 	}
    263  1.38      elad 	kauth_unlisten_scope(ksem_listener);
    264  1.52   thorpej 	hashdone(ksem_pshared_hashtab, HASH_LIST, ksem_pshared_hashmask);
    265  1.52   thorpej 	rw_destroy(&ksem_pshared_lock);
    266  1.30     rmind 	mutex_destroy(&ksem_lock);
    267  1.43  pgoyette 	sysctl_teardown(&ksem_clog);
    268  1.30     rmind 	return 0;
    269   1.3   thorpej }
    270   1.3   thorpej 
    271  1.30     rmind static int
    272  1.30     rmind ksem_modcmd(modcmd_t cmd, void *arg)
    273   1.3   thorpej {
    274   1.3   thorpej 
    275  1.30     rmind 	switch (cmd) {
    276  1.30     rmind 	case MODULE_CMD_INIT:
    277  1.30     rmind 		return ksem_sysinit();
    278   1.3   thorpej 
    279  1.30     rmind 	case MODULE_CMD_FINI:
    280  1.30     rmind 		return ksem_sysfini(true);
    281   1.1  christos 
    282  1.30     rmind 	default:
    283  1.30     rmind 		return ENOTTY;
    284  1.16   thorpej 	}
    285  1.16   thorpej }
    286  1.16   thorpej 
    287  1.30     rmind static ksem_t *
    288  1.30     rmind ksem_lookup(const char *name)
    289   1.3   thorpej {
    290  1.30     rmind 	ksem_t *ks;
    291   1.3   thorpej 
    292  1.30     rmind 	KASSERT(mutex_owned(&ksem_lock));
    293   1.3   thorpej 
    294  1.30     rmind 	LIST_FOREACH(ks, &ksem_head, ks_entry) {
    295  1.30     rmind 		if (strcmp(ks->ks_name, name) == 0) {
    296  1.30     rmind 			mutex_enter(&ks->ks_lock);
    297  1.30     rmind 			return ks;
    298   1.3   thorpej 		}
    299   1.1  christos 	}
    300  1.30     rmind 	return NULL;
    301   1.1  christos }
    302   1.1  christos 
    303   1.3   thorpej static int
    304  1.30     rmind ksem_perm(lwp_t *l, ksem_t *ks)
    305   1.3   thorpej {
    306  1.30     rmind 	kauth_cred_t uc = l->l_cred;
    307   1.3   thorpej 
    308  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    309  1.30     rmind 
    310  1.38      elad 	if (kauth_authorize_system(uc, KAUTH_SYSTEM_SEMAPHORE, 0, ks, NULL, NULL) != 0)
    311  1.38      elad 		return EACCES;
    312  1.38      elad 
    313  1.38      elad 	return 0;
    314   1.3   thorpej }
    315   1.3   thorpej 
    316  1.30     rmind /*
    317  1.52   thorpej  * Bits 1..23 are random, just pluck a few of those and assume the
    318  1.52   thorpej  * distribution is going to be pretty good.
    319  1.52   thorpej  */
    320  1.52   thorpej #define	KSEM_PSHARED_HASH(id)	(((id) >> 1) & ksem_pshared_hashmask)
    321  1.52   thorpej 
    322  1.52   thorpej static void
    323  1.52   thorpej ksem_remove_pshared(ksem_t *ksem)
    324  1.52   thorpej {
    325  1.52   thorpej 	rw_enter(&ksem_pshared_lock, RW_WRITER);
    326  1.52   thorpej 	LIST_REMOVE(ksem, ks_entry);
    327  1.52   thorpej 	rw_exit(&ksem_pshared_lock);
    328  1.52   thorpej }
    329  1.52   thorpej 
    330  1.52   thorpej static ksem_t *
    331  1.52   thorpej ksem_lookup_pshared_locked(intptr_t id)
    332  1.52   thorpej {
    333  1.52   thorpej 	u_long bucket = KSEM_PSHARED_HASH(id);
    334  1.52   thorpej 	ksem_t *ksem = NULL;
    335  1.52   thorpej 
    336  1.52   thorpej 	/* ksem_t is locked and referenced upon return. */
    337  1.52   thorpej 
    338  1.52   thorpej 	LIST_FOREACH(ksem, &ksem_pshared_hashtab[bucket], ks_entry) {
    339  1.52   thorpej 		if (ksem->ks_pshared_id == id) {
    340  1.52   thorpej 			mutex_enter(&ksem->ks_lock);
    341  1.52   thorpej 			if (ksem->ks_pshared_proc == NULL) {
    342  1.52   thorpej 				/*
    343  1.52   thorpej 				 * This entry is dead, and in the process
    344  1.52   thorpej 				 * of being torn down; skip it.
    345  1.52   thorpej 				 */
    346  1.52   thorpej 				mutex_exit(&ksem->ks_lock);
    347  1.52   thorpej 				continue;
    348  1.52   thorpej 			}
    349  1.52   thorpej 			ksem->ks_ref++;
    350  1.52   thorpej 			KASSERT(ksem->ks_ref != 0);
    351  1.52   thorpej 			return ksem;
    352  1.52   thorpej 		}
    353  1.52   thorpej 	}
    354  1.52   thorpej 
    355  1.52   thorpej 	return NULL;
    356  1.52   thorpej }
    357  1.52   thorpej 
    358  1.52   thorpej static ksem_t *
    359  1.52   thorpej ksem_lookup_pshared(intptr_t id)
    360  1.52   thorpej {
    361  1.52   thorpej 	rw_enter(&ksem_pshared_lock, RW_READER);
    362  1.52   thorpej 	ksem_t *ksem = ksem_lookup_pshared_locked(id);
    363  1.52   thorpej 	rw_exit(&ksem_pshared_lock);
    364  1.52   thorpej 	return ksem;
    365  1.52   thorpej }
    366  1.52   thorpej 
    367  1.52   thorpej static void
    368  1.52   thorpej ksem_alloc_pshared_id(ksem_t *ksem)
    369  1.52   thorpej {
    370  1.52   thorpej 	uint32_t try;
    371  1.52   thorpej 
    372  1.52   thorpej 	KASSERT(ksem->ks_pshared_proc != NULL);
    373  1.52   thorpej 
    374  1.52   thorpej 	rw_enter(&ksem_pshared_lock, RW_WRITER);
    375  1.52   thorpej 	for (;;) {
    376  1.52   thorpej 		try = (cprng_fast32() & ~KSEM_MARKER_MASK) |
    377  1.52   thorpej 		    KSEM_PSHARED_MARKER;
    378  1.52   thorpej 
    379  1.52   thorpej 		if (ksem_lookup_pshared_locked(try) == NULL) {
    380  1.52   thorpej 			/* Got it! */
    381  1.52   thorpej 			break;
    382  1.52   thorpej 		}
    383  1.52   thorpej 	}
    384  1.52   thorpej 	ksem->ks_pshared_id = try;
    385  1.52   thorpej 	u_long bucket = KSEM_PSHARED_HASH(ksem->ks_pshared_id);
    386  1.52   thorpej 	LIST_INSERT_HEAD(&ksem_pshared_hashtab[bucket], ksem, ks_entry);
    387  1.52   thorpej 	rw_exit(&ksem_pshared_lock);
    388  1.52   thorpej }
    389  1.52   thorpej 
    390  1.52   thorpej /*
    391  1.30     rmind  * ksem_get: get the semaphore from the descriptor.
    392  1.30     rmind  *
    393  1.52   thorpej  * => locks the semaphore, if found, and holds an extra reference.
    394  1.30     rmind  * => holds a reference on the file descriptor.
    395  1.30     rmind  */
    396  1.30     rmind static int
    397  1.52   thorpej ksem_get(intptr_t id, ksem_t **ksret, int *fdp)
    398  1.13      cube {
    399  1.30     rmind 	ksem_t *ks;
    400  1.52   thorpej 	int fd;
    401  1.52   thorpej 
    402  1.52   thorpej 	if ((id & KSEM_MARKER_MASK) == KSEM_PSHARED_MARKER) {
    403  1.52   thorpej 		/*
    404  1.52   thorpej 		 * ksem_lookup_pshared() returns the ksem_t *
    405  1.52   thorpej 		 * locked and referenced.
    406  1.52   thorpej 		 */
    407  1.52   thorpej 		ks = ksem_lookup_pshared(id);
    408  1.52   thorpej 		if (ks == NULL)
    409  1.52   thorpej 			return EINVAL;
    410  1.52   thorpej 		KASSERT(ks->ks_pshared_id == id);
    411  1.52   thorpej 		KASSERT(ks->ks_pshared_proc != NULL);
    412  1.52   thorpej 		fd = -1;
    413  1.52   thorpej 	} else if (id <= INT_MAX) {
    414  1.52   thorpej 		fd = (int)id;
    415  1.52   thorpej 		file_t *fp = fd_getfile(fd);
    416  1.13      cube 
    417  1.52   thorpej 		if (__predict_false(fp == NULL))
    418  1.52   thorpej 			return EINVAL;
    419  1.52   thorpej 		if (__predict_false(fp->f_type != DTYPE_SEM)) {
    420  1.52   thorpej 			fd_putfile(fd);
    421  1.52   thorpej 			return EINVAL;
    422  1.52   thorpej 		}
    423  1.52   thorpej 		ks = fp->f_ksem;
    424  1.52   thorpej 		mutex_enter(&ks->ks_lock);
    425  1.52   thorpej 		ks->ks_ref++;
    426  1.52   thorpej 	} else {
    427  1.37     joerg 		return EINVAL;
    428  1.13      cube 	}
    429  1.13      cube 
    430  1.30     rmind 	*ksret = ks;
    431  1.52   thorpej 	*fdp = fd;
    432  1.30     rmind 	return 0;
    433   1.1  christos }
    434   1.1  christos 
    435  1.30     rmind /*
    436  1.30     rmind  * ksem_create: allocate and setup a new semaphore structure.
    437  1.30     rmind  */
    438   1.1  christos static int
    439  1.30     rmind ksem_create(lwp_t *l, const char *name, ksem_t **ksret, mode_t mode, u_int val)
    440   1.1  christos {
    441  1.30     rmind 	ksem_t *ks;
    442  1.14      elad 	kauth_cred_t uc;
    443  1.30     rmind 	char *kname;
    444   1.1  christos 	size_t len;
    445   1.1  christos 
    446  1.30     rmind 	/* Pre-check for the limit. */
    447  1.30     rmind 	if (nsems >= ksem_max) {
    448  1.30     rmind 		return ENFILE;
    449  1.30     rmind 	}
    450  1.30     rmind 
    451  1.30     rmind 	if (val > SEM_VALUE_MAX) {
    452  1.30     rmind 		return EINVAL;
    453  1.30     rmind 	}
    454  1.30     rmind 
    455   1.1  christos 	if (name != NULL) {
    456   1.1  christos 		len = strlen(name);
    457   1.1  christos 		if (len > SEM_MAX_NAMELEN) {
    458  1.30     rmind 			return ENAMETOOLONG;
    459   1.1  christos 		}
    460  1.30     rmind 		/* Name must start with a '/' but not contain one. */
    461   1.1  christos 		if (*name != '/' || len < 2 || strchr(name + 1, '/') != NULL) {
    462  1.30     rmind 			return EINVAL;
    463   1.1  christos 		}
    464  1.30     rmind 		kname = kmem_alloc(++len, KM_SLEEP);
    465  1.30     rmind 		strlcpy(kname, name, len);
    466  1.30     rmind 	} else {
    467  1.30     rmind 		kname = NULL;
    468  1.30     rmind 		len = 0;
    469  1.30     rmind 	}
    470  1.30     rmind 
    471  1.55  christos 	u_int cnt;
    472  1.55  christos 	uid_t uid = kauth_cred_getuid(l->l_cred);
    473  1.55  christos 	if ((cnt = chgsemcnt(uid, 1)) > SEM_NSEMS_MAX) {
    474  1.55  christos 		chgsemcnt(uid, -1);
    475  1.47      maxv 		if (kname != NULL)
    476  1.47      maxv 			kmem_free(kname, len);
    477  1.54  christos 		return ENOSPC;
    478  1.47      maxv 	}
    479  1.46  christos 
    480  1.30     rmind 	ks = kmem_zalloc(sizeof(ksem_t), KM_SLEEP);
    481  1.30     rmind 	mutex_init(&ks->ks_lock, MUTEX_DEFAULT, IPL_NONE);
    482  1.30     rmind 	cv_init(&ks->ks_cv, "psem");
    483  1.30     rmind 	ks->ks_name = kname;
    484  1.30     rmind 	ks->ks_namelen = len;
    485  1.30     rmind 	ks->ks_mode = mode;
    486  1.30     rmind 	ks->ks_value = val;
    487  1.30     rmind 	ks->ks_ref = 1;
    488  1.30     rmind 
    489  1.30     rmind 	uc = l->l_cred;
    490  1.30     rmind 	ks->ks_uid = kauth_cred_geteuid(uc);
    491  1.30     rmind 	ks->ks_gid = kauth_cred_getegid(uc);
    492  1.30     rmind 
    493  1.34     rmind 	atomic_inc_uint(&nsems_total);
    494  1.30     rmind 	*ksret = ks;
    495  1.30     rmind 	return 0;
    496  1.30     rmind }
    497  1.30     rmind 
    498  1.30     rmind static void
    499  1.30     rmind ksem_free(ksem_t *ks)
    500  1.30     rmind {
    501   1.3   thorpej 
    502  1.34     rmind 	KASSERT(!cv_has_waiters(&ks->ks_cv));
    503  1.34     rmind 
    504  1.52   thorpej 	if (ks->ks_pshared_id) {
    505  1.52   thorpej 		KASSERT(ks->ks_pshared_proc == NULL);
    506  1.52   thorpej 		ksem_remove_pshared(ks);
    507  1.52   thorpej 	}
    508  1.30     rmind 	if (ks->ks_name) {
    509  1.30     rmind 		KASSERT(ks->ks_namelen > 0);
    510  1.30     rmind 		kmem_free(ks->ks_name, ks->ks_namelen);
    511  1.13      cube 	}
    512  1.30     rmind 	mutex_destroy(&ks->ks_lock);
    513  1.30     rmind 	cv_destroy(&ks->ks_cv);
    514  1.30     rmind 	kmem_free(ks, sizeof(ksem_t));
    515  1.34     rmind 
    516  1.34     rmind 	atomic_dec_uint(&nsems_total);
    517  1.55  christos 	chgsemcnt(kauth_cred_getuid(curproc->p_cred), -1);
    518   1.1  christos }
    519   1.1  christos 
    520  1.52   thorpej #define	KSEM_ID_IS_PSHARED(id)		\
    521  1.52   thorpej 	(((id) & KSEM_MARKER_MASK) == KSEM_PSHARED_MARKER)
    522  1.52   thorpej 
    523  1.52   thorpej static void
    524  1.52   thorpej ksem_release(ksem_t *ksem, int fd)
    525  1.52   thorpej {
    526  1.52   thorpej 	bool destroy = false;
    527  1.52   thorpej 
    528  1.52   thorpej 	KASSERT(mutex_owned(&ksem->ks_lock));
    529  1.52   thorpej 
    530  1.52   thorpej 	KASSERT(ksem->ks_ref > 0);
    531  1.52   thorpej 	if (--ksem->ks_ref == 0) {
    532  1.52   thorpej 		/*
    533  1.52   thorpej 		 * Destroy if the last reference and semaphore is unnamed,
    534  1.52   thorpej 		 * or unlinked (for named semaphore).
    535  1.52   thorpej 		 */
    536  1.52   thorpej 		destroy = (ksem->ks_flags & KS_UNLINKED) ||
    537  1.52   thorpej 		    (ksem->ks_name == NULL);
    538  1.52   thorpej 	}
    539  1.52   thorpej 	mutex_exit(&ksem->ks_lock);
    540  1.52   thorpej 
    541  1.52   thorpej 	if (destroy) {
    542  1.52   thorpej 		ksem_free(ksem);
    543  1.52   thorpej 	}
    544  1.52   thorpej 	if (fd != -1) {
    545  1.52   thorpej 		fd_putfile(fd);
    546  1.52   thorpej 	}
    547  1.52   thorpej }
    548  1.52   thorpej 
    549   1.1  christos int
    550  1.30     rmind sys__ksem_init(struct lwp *l, const struct sys__ksem_init_args *uap,
    551  1.30     rmind     register_t *retval)
    552   1.1  christos {
    553  1.23       dsl 	/* {
    554   1.1  christos 		unsigned int value;
    555  1.29        ad 		intptr_t *idp;
    556  1.23       dsl 	} */
    557  1.13      cube 
    558  1.52   thorpej 	return do_ksem_init(l, SCARG(uap, value), SCARG(uap, idp),
    559  1.52   thorpej 	    copyin, copyout);
    560  1.13      cube }
    561  1.13      cube 
    562  1.13      cube int
    563  1.52   thorpej do_ksem_init(lwp_t *l, u_int val, intptr_t *idp, copyin_t docopyin,
    564  1.52   thorpej     copyout_t docopyout)
    565  1.13      cube {
    566  1.30     rmind 	proc_t *p = l->l_proc;
    567  1.30     rmind 	ksem_t *ks;
    568  1.30     rmind 	file_t *fp;
    569  1.52   thorpej 	intptr_t id, arg;
    570  1.30     rmind 	int fd, error;
    571   1.1  christos 
    572  1.52   thorpej 	/*
    573  1.52   thorpej 	 * Newer versions of librt / libpthread pass us 'PSRD' in *idp to
    574  1.52   thorpej 	 * indicate that a pshared semaphore is wanted.  In that case we
    575  1.52   thorpej 	 * allocate globally unique ID and return that, rather than the
    576  1.52   thorpej 	 * process-scoped file descriptor ID.
    577  1.52   thorpej 	 */
    578  1.52   thorpej 	error = (*docopyin)(idp, &arg, sizeof(*idp));
    579  1.52   thorpej 	if (error) {
    580  1.52   thorpej 		return error;
    581  1.52   thorpej 	}
    582  1.52   thorpej 
    583  1.30     rmind 	error = fd_allocfile(&fp, &fd);
    584   1.1  christos 	if (error) {
    585  1.30     rmind 		return error;
    586   1.1  christos 	}
    587  1.30     rmind 	fp->f_type = DTYPE_SEM;
    588  1.30     rmind 	fp->f_flag = FREAD | FWRITE;
    589  1.30     rmind 	fp->f_ops = &semops;
    590   1.3   thorpej 
    591  1.52   thorpej 	if (fd >= KSEM_MARKER_MIN) {
    592  1.52   thorpej 		/*
    593  1.52   thorpej 		 * This is super-unlikely, but we check for it anyway
    594  1.52   thorpej 		 * because potential collisions with the pshared marker
    595  1.52   thorpej 		 * would be bad.
    596  1.52   thorpej 		 */
    597  1.52   thorpej 		fd_abort(p, fp, fd);
    598  1.52   thorpej 		return EMFILE;
    599  1.52   thorpej 	}
    600  1.52   thorpej 
    601  1.52   thorpej 	/* Note the mode does not matter for anonymous semaphores. */
    602  1.52   thorpej 	error = ksem_create(l, NULL, &ks, 0, val);
    603  1.30     rmind 	if (error) {
    604  1.30     rmind 		fd_abort(p, fp, fd);
    605  1.30     rmind 		return error;
    606  1.30     rmind 	}
    607   1.3   thorpej 
    608  1.52   thorpej 	if (arg == KSEM_PSHARED) {
    609  1.52   thorpej 		ks->ks_pshared_proc = curproc;
    610  1.52   thorpej 		ks->ks_pshared_fd = fd;
    611  1.52   thorpej 		ksem_alloc_pshared_id(ks);
    612  1.52   thorpej 		id = ks->ks_pshared_id;
    613  1.52   thorpej 	} else {
    614  1.52   thorpej 		id = (intptr_t)fd;
    615  1.52   thorpej 	}
    616  1.52   thorpej 
    617  1.52   thorpej 	error = (*docopyout)(&id, idp, sizeof(*idp));
    618  1.30     rmind 	if (error) {
    619  1.52   thorpej 		ksem_free(ks);
    620  1.30     rmind 		fd_abort(p, fp, fd);
    621  1.30     rmind 		return error;
    622  1.30     rmind 	}
    623  1.52   thorpej 
    624  1.42      matt 	fp->f_ksem = ks;
    625  1.30     rmind 	fd_affix(p, fp, fd);
    626  1.30     rmind 	return error;
    627   1.1  christos }
    628   1.1  christos 
    629   1.1  christos int
    630  1.30     rmind sys__ksem_open(struct lwp *l, const struct sys__ksem_open_args *uap,
    631  1.30     rmind     register_t *retval)
    632   1.1  christos {
    633  1.23       dsl 	/* {
    634   1.1  christos 		const char *name;
    635   1.1  christos 		int oflag;
    636   1.1  christos 		mode_t mode;
    637   1.1  christos 		unsigned int value;
    638  1.29        ad 		intptr_t *idp;
    639  1.23       dsl 	} */
    640  1.13      cube 
    641  1.13      cube 	return do_ksem_open(l, SCARG(uap, name), SCARG(uap, oflag),
    642  1.13      cube 	    SCARG(uap, mode), SCARG(uap, value), SCARG(uap, idp), copyout);
    643  1.13      cube }
    644  1.13      cube 
    645  1.13      cube int
    646  1.13      cube do_ksem_open(struct lwp *l, const char *semname, int oflag, mode_t mode,
    647  1.29        ad      unsigned int value, intptr_t *idp, copyout_t docopyout)
    648  1.13      cube {
    649  1.51  christos 	char *name;
    650  1.30     rmind 	proc_t *p = l->l_proc;
    651  1.30     rmind 	ksem_t *ksnew = NULL, *ks;
    652  1.30     rmind 	file_t *fp;
    653  1.29        ad 	intptr_t id;
    654  1.30     rmind 	int fd, error;
    655   1.1  christos 
    656  1.51  christos 	error = name_copyin(semname, &name);
    657  1.30     rmind 	if (error) {
    658  1.30     rmind 		return error;
    659  1.30     rmind 	}
    660  1.30     rmind 	error = fd_allocfile(&fp, &fd);
    661  1.30     rmind 	if (error) {
    662  1.51  christos 		name_destroy(&name);
    663  1.30     rmind 		return error;
    664  1.30     rmind 	}
    665  1.30     rmind 	fp->f_type = DTYPE_SEM;
    666  1.30     rmind 	fp->f_flag = FREAD | FWRITE;
    667  1.30     rmind 	fp->f_ops = &semops;
    668  1.30     rmind 
    669  1.52   thorpej 	if (fd >= KSEM_MARKER_MIN) {
    670  1.52   thorpej 		/*
    671  1.52   thorpej 		 * This is super-unlikely, but we check for it anyway
    672  1.52   thorpej 		 * because potential collisions with the pshared marker
    673  1.52   thorpej 		 * would be bad.
    674  1.52   thorpej 		 */
    675  1.52   thorpej 		fd_abort(p, fp, fd);
    676  1.52   thorpej 		return EMFILE;
    677  1.52   thorpej 	}
    678  1.52   thorpej 
    679  1.30     rmind 	/*
    680  1.30     rmind 	 * The ID (file descriptor number) can be stored early.
    681  1.30     rmind 	 * Note that zero is a special value for libpthread.
    682  1.30     rmind 	 */
    683  1.30     rmind 	id = (intptr_t)fd;
    684  1.30     rmind 	error = (*docopyout)(&id, idp, sizeof(*idp));
    685  1.30     rmind 	if (error) {
    686  1.30     rmind 		goto err;
    687  1.30     rmind 	}
    688  1.30     rmind 
    689  1.30     rmind 	if (oflag & O_CREAT) {
    690  1.30     rmind 		/* Create a new semaphore. */
    691  1.30     rmind 		error = ksem_create(l, name, &ksnew, mode, value);
    692  1.30     rmind 		if (error) {
    693  1.30     rmind 			goto err;
    694  1.30     rmind 		}
    695  1.30     rmind 		KASSERT(ksnew != NULL);
    696  1.30     rmind 	}
    697   1.1  christos 
    698  1.30     rmind 	/* Lookup for a semaphore with such name. */
    699  1.30     rmind 	mutex_enter(&ksem_lock);
    700  1.30     rmind 	ks = ksem_lookup(name);
    701  1.51  christos 	name_destroy(&name);
    702  1.30     rmind 	if (ks) {
    703  1.30     rmind 		KASSERT(mutex_owned(&ks->ks_lock));
    704  1.30     rmind 		mutex_exit(&ksem_lock);
    705   1.3   thorpej 
    706   1.3   thorpej 		/* Check for exclusive create. */
    707  1.13      cube 		if (oflag & O_EXCL) {
    708  1.30     rmind 			mutex_exit(&ks->ks_lock);
    709  1.30     rmind 			error = EEXIST;
    710  1.30     rmind 			goto err;
    711   1.1  christos 		}
    712   1.1  christos 		/*
    713  1.30     rmind 		 * Verify permissions.  If we can access it,
    714  1.30     rmind 		 * add the reference of this thread.
    715   1.1  christos 		 */
    716  1.15        ad 		error = ksem_perm(l, ks);
    717  1.30     rmind 		if (error == 0) {
    718  1.30     rmind 			ks->ks_ref++;
    719  1.30     rmind 		}
    720  1.30     rmind 		mutex_exit(&ks->ks_lock);
    721   1.1  christos 		if (error) {
    722  1.30     rmind 			goto err;
    723  1.30     rmind 		}
    724  1.30     rmind 	} else {
    725  1.30     rmind 		/* Fail if not found and not creating. */
    726  1.30     rmind 		if ((oflag & O_CREAT) == 0) {
    727  1.30     rmind 			mutex_exit(&ksem_lock);
    728  1.30     rmind 			KASSERT(ksnew == NULL);
    729  1.31     rmind 			error = ENOENT;
    730  1.31     rmind 			goto err;
    731   1.1  christos 		}
    732   1.3   thorpej 
    733  1.30     rmind 		/* Check for the limit locked. */
    734  1.30     rmind 		if (nsems >= ksem_max) {
    735  1.30     rmind 			mutex_exit(&ksem_lock);
    736  1.30     rmind 			error = ENFILE;
    737  1.30     rmind 			goto err;
    738  1.30     rmind 		}
    739   1.3   thorpej 
    740  1.30     rmind 		/*
    741  1.32     rmind 		 * Finally, insert semaphore into the list.
    742  1.30     rmind 		 * Note: it already has the initial reference.
    743  1.30     rmind 		 */
    744  1.30     rmind 		ks = ksnew;
    745  1.30     rmind 		LIST_INSERT_HEAD(&ksem_head, ks, ks_entry);
    746  1.30     rmind 		nsems++;
    747  1.30     rmind 		mutex_exit(&ksem_lock);
    748  1.30     rmind 
    749  1.30     rmind 		ksnew = NULL;
    750  1.30     rmind 	}
    751  1.30     rmind 	KASSERT(ks != NULL);
    752  1.42      matt 	fp->f_ksem = ks;
    753  1.30     rmind 	fd_affix(p, fp, fd);
    754  1.30     rmind err:
    755  1.51  christos 	name_destroy(&name);
    756  1.30     rmind 	if (error) {
    757  1.30     rmind 		fd_abort(p, fp, fd);
    758   1.3   thorpej 	}
    759  1.30     rmind 	if (ksnew) {
    760  1.30     rmind 		ksem_free(ksnew);
    761   1.1  christos 	}
    762  1.30     rmind 	return error;
    763  1.30     rmind }
    764   1.1  christos 
    765  1.30     rmind int
    766  1.30     rmind sys__ksem_close(struct lwp *l, const struct sys__ksem_close_args *uap,
    767  1.30     rmind     register_t *retval)
    768  1.30     rmind {
    769  1.30     rmind 	/* {
    770  1.30     rmind 		intptr_t id;
    771  1.30     rmind 	} */
    772  1.52   thorpej 	intptr_t id = SCARG(uap, id);
    773  1.52   thorpej 	int fd, error;
    774  1.52   thorpej 	ksem_t *ks;
    775  1.52   thorpej 
    776  1.52   thorpej 	error = ksem_get(id, &ks, &fd);
    777  1.52   thorpej 	if (error) {
    778  1.52   thorpej 		return error;
    779  1.52   thorpej 	}
    780  1.33     rmind 
    781  1.52   thorpej 	/* This is only for named semaphores. */
    782  1.52   thorpej 	if (ks->ks_name == NULL) {
    783  1.52   thorpej 		error = EINVAL;
    784  1.52   thorpej 	}
    785  1.52   thorpej 	ksem_release(ks, -1);
    786  1.52   thorpej 	if (error) {
    787  1.52   thorpej 		if (fd != -1)
    788  1.52   thorpej 			fd_putfile(fd);
    789  1.52   thorpej 		return error;
    790  1.33     rmind 	}
    791  1.33     rmind 	return fd_close(fd);
    792   1.1  christos }
    793   1.1  christos 
    794  1.30     rmind static int
    795  1.39  christos ksem_read_fop(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    796  1.39  christos     int flags)
    797  1.39  christos {
    798  1.39  christos 	size_t len;
    799  1.39  christos 	char *name;
    800  1.42      matt 	ksem_t *ks = fp->f_ksem;
    801  1.39  christos 
    802  1.39  christos 	mutex_enter(&ks->ks_lock);
    803  1.39  christos 	len = ks->ks_namelen;
    804  1.39  christos 	name = ks->ks_name;
    805  1.39  christos 	mutex_exit(&ks->ks_lock);
    806  1.39  christos 	if (name == NULL || len == 0)
    807  1.39  christos 		return 0;
    808  1.39  christos 	return uiomove(name, len, uio);
    809  1.39  christos }
    810  1.39  christos 
    811  1.39  christos static int
    812  1.39  christos ksem_stat_fop(file_t *fp, struct stat *ub)
    813  1.39  christos {
    814  1.42      matt 	ksem_t *ks = fp->f_ksem;
    815  1.39  christos 
    816  1.39  christos 	mutex_enter(&ks->ks_lock);
    817  1.39  christos 
    818  1.39  christos 	memset(ub, 0, sizeof(*ub));
    819  1.39  christos 
    820  1.39  christos 	ub->st_mode = ks->ks_mode | ((ks->ks_name && ks->ks_namelen)
    821  1.39  christos 	    ? _S_IFLNK : _S_IFREG);
    822  1.39  christos 	ub->st_uid = ks->ks_uid;
    823  1.39  christos 	ub->st_gid = ks->ks_gid;
    824  1.39  christos 	ub->st_size = ks->ks_value;
    825  1.39  christos 	ub->st_blocks = (ub->st_size) ? 1 : 0;
    826  1.39  christos 	ub->st_nlink = ks->ks_ref;
    827  1.39  christos 	ub->st_blksize = 4096;
    828  1.39  christos 
    829  1.39  christos 	nanotime(&ub->st_atimespec);
    830  1.39  christos 	ub->st_mtimespec = ub->st_ctimespec = ub->st_birthtimespec =
    831  1.39  christos 	    ub->st_atimespec;
    832  1.39  christos 
    833  1.39  christos 	/*
    834  1.39  christos 	 * Left as 0: st_dev, st_ino, st_rdev, st_flags, st_gen.
    835  1.39  christos 	 * XXX (st_dev, st_ino) should be unique.
    836  1.39  christos 	 */
    837  1.39  christos 	mutex_exit(&ks->ks_lock);
    838  1.39  christos 	return 0;
    839  1.39  christos }
    840  1.39  christos 
    841  1.39  christos static int
    842  1.30     rmind ksem_close_fop(file_t *fp)
    843   1.1  christos {
    844  1.42      matt 	ksem_t *ks = fp->f_ksem;
    845   1.1  christos 
    846  1.53   thorpej 	mutex_enter(&ks->ks_lock);
    847  1.53   thorpej 
    848  1.53   thorpej 	if (ks->ks_pshared_id) {
    849  1.53   thorpej 		if (ks->ks_pshared_proc != curproc) {
    850  1.53   thorpej 			/* Do nothing if this is not the creator. */
    851  1.53   thorpej 			mutex_exit(&ks->ks_lock);
    852  1.53   thorpej 			return 0;
    853  1.53   thorpej 		}
    854  1.53   thorpej 		/* Mark this semaphore as dead. */
    855  1.53   thorpej 		ks->ks_pshared_proc = NULL;
    856   1.1  christos 	}
    857   1.3   thorpej 
    858  1.52   thorpej 	ksem_release(ks, -1);
    859  1.30     rmind 	return 0;
    860   1.1  christos }
    861   1.1  christos 
    862   1.1  christos int
    863  1.30     rmind sys__ksem_unlink(struct lwp *l, const struct sys__ksem_unlink_args *uap,
    864  1.30     rmind     register_t *retval)
    865   1.1  christos {
    866  1.23       dsl 	/* {
    867   1.1  christos 		const char *name;
    868  1.23       dsl 	} */
    869  1.51  christos 	char *name;
    870  1.30     rmind 	ksem_t *ks;
    871  1.30     rmind 	u_int refcnt;
    872   1.1  christos 	int error;
    873   1.1  christos 
    874  1.51  christos 	error = name_copyin(SCARG(uap, name), &name);
    875   1.1  christos 	if (error)
    876   1.1  christos 		return error;
    877   1.1  christos 
    878  1.30     rmind 	mutex_enter(&ksem_lock);
    879  1.30     rmind 	ks = ksem_lookup(name);
    880  1.51  christos 	name_destroy(&name);
    881   1.3   thorpej 	if (ks == NULL) {
    882  1.30     rmind 		mutex_exit(&ksem_lock);
    883  1.30     rmind 		return ENOENT;
    884   1.1  christos 	}
    885  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    886   1.3   thorpej 
    887  1.30     rmind 	/* Verify permissions. */
    888  1.30     rmind 	error = ksem_perm(l, ks);
    889  1.30     rmind 	if (error) {
    890  1.30     rmind 		mutex_exit(&ks->ks_lock);
    891  1.30     rmind 		mutex_exit(&ksem_lock);
    892  1.30     rmind 		return error;
    893  1.30     rmind 	}
    894   1.3   thorpej 
    895  1.31     rmind 	/* Remove from the global list. */
    896   1.3   thorpej 	LIST_REMOVE(ks, ks_entry);
    897  1.30     rmind 	nsems--;
    898  1.31     rmind 	mutex_exit(&ksem_lock);
    899   1.3   thorpej 
    900  1.30     rmind 	refcnt = ks->ks_ref;
    901  1.30     rmind 	if (refcnt) {
    902  1.30     rmind 		/* Mark as unlinked, if there are references. */
    903  1.30     rmind 		ks->ks_flags |= KS_UNLINKED;
    904  1.30     rmind 	}
    905  1.30     rmind 	mutex_exit(&ks->ks_lock);
    906   1.3   thorpej 
    907  1.30     rmind 	if (refcnt == 0) {
    908   1.3   thorpej 		ksem_free(ks);
    909  1.30     rmind 	}
    910  1.30     rmind 	return 0;
    911   1.1  christos }
    912   1.1  christos 
    913   1.1  christos int
    914  1.30     rmind sys__ksem_post(struct lwp *l, const struct sys__ksem_post_args *uap,
    915  1.30     rmind     register_t *retval)
    916   1.1  christos {
    917  1.23       dsl 	/* {
    918  1.29        ad 		intptr_t id;
    919  1.23       dsl 	} */
    920  1.52   thorpej 	int fd, error;
    921  1.30     rmind 	ksem_t *ks;
    922   1.1  christos 
    923  1.52   thorpej 	error = ksem_get(SCARG(uap, id), &ks, &fd);
    924  1.30     rmind 	if (error) {
    925  1.30     rmind 		return error;
    926   1.3   thorpej 	}
    927  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    928   1.1  christos 	if (ks->ks_value == SEM_VALUE_MAX) {
    929   1.1  christos 		error = EOVERFLOW;
    930   1.3   thorpej 		goto out;
    931   1.1  christos 	}
    932  1.30     rmind 	ks->ks_value++;
    933  1.30     rmind 	if (ks->ks_waiters) {
    934  1.20        ad 		cv_broadcast(&ks->ks_cv);
    935  1.30     rmind 	}
    936  1.30     rmind out:
    937  1.52   thorpej 	ksem_release(ks, fd);
    938  1.30     rmind 	return error;
    939   1.3   thorpej }
    940   1.3   thorpej 
    941  1.36     joerg int
    942  1.41      matt do_ksem_wait(lwp_t *l, intptr_t id, bool try_p, struct timespec *abstime)
    943   1.3   thorpej {
    944  1.52   thorpej 	int fd, error, timeo;
    945  1.30     rmind 	ksem_t *ks;
    946   1.3   thorpej 
    947  1.52   thorpej 	error = ksem_get(id, &ks, &fd);
    948  1.30     rmind 	if (error) {
    949  1.30     rmind 		return error;
    950  1.30     rmind 	}
    951  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
    952   1.3   thorpej 	while (ks->ks_value == 0) {
    953   1.3   thorpej 		ks->ks_waiters++;
    954  1.41      matt 		if (!try_p && abstime != NULL) {
    955  1.40  christos 			error = ts2timo(CLOCK_REALTIME, TIMER_ABSTIME, abstime,
    956  1.40  christos 			    &timeo, NULL);
    957  1.36     joerg 			if (error != 0)
    958  1.36     joerg 				goto out;
    959  1.36     joerg 		} else {
    960  1.36     joerg 			timeo = 0;
    961  1.36     joerg 		}
    962  1.41      matt 		error = try_p ? EAGAIN : cv_timedwait_sig(&ks->ks_cv,
    963  1.36     joerg 		    &ks->ks_lock, timeo);
    964   1.3   thorpej 		ks->ks_waiters--;
    965   1.3   thorpej 		if (error)
    966   1.3   thorpej 			goto out;
    967   1.3   thorpej 	}
    968   1.3   thorpej 	ks->ks_value--;
    969  1.30     rmind out:
    970  1.52   thorpej 	ksem_release(ks, fd);
    971  1.30     rmind 	return error;
    972   1.1  christos }
    973   1.1  christos 
    974   1.1  christos int
    975  1.30     rmind sys__ksem_wait(struct lwp *l, const struct sys__ksem_wait_args *uap,
    976  1.30     rmind     register_t *retval)
    977   1.1  christos {
    978  1.23       dsl 	/* {
    979  1.29        ad 		intptr_t id;
    980  1.23       dsl 	} */
    981   1.1  christos 
    982  1.36     joerg 	return do_ksem_wait(l, SCARG(uap, id), false, NULL);
    983  1.36     joerg }
    984  1.36     joerg 
    985  1.36     joerg int
    986  1.36     joerg sys__ksem_timedwait(struct lwp *l, const struct sys__ksem_timedwait_args *uap,
    987  1.36     joerg     register_t *retval)
    988  1.36     joerg {
    989  1.36     joerg 	/* {
    990  1.36     joerg 		intptr_t id;
    991  1.36     joerg 		const struct timespec *abstime;
    992  1.36     joerg 	} */
    993  1.36     joerg 	struct timespec ts;
    994  1.36     joerg 	int error;
    995  1.36     joerg 
    996  1.36     joerg 	error = copyin(SCARG(uap, abstime), &ts, sizeof(ts));
    997  1.36     joerg 	if (error != 0)
    998  1.36     joerg 		return error;
    999  1.36     joerg 
   1000  1.36     joerg 	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
   1001  1.36     joerg 		return EINVAL;
   1002  1.36     joerg 
   1003  1.36     joerg 	error = do_ksem_wait(l, SCARG(uap, id), false, &ts);
   1004  1.36     joerg 	if (error == EWOULDBLOCK)
   1005  1.36     joerg 		error = ETIMEDOUT;
   1006  1.36     joerg 	return error;
   1007   1.1  christos }
   1008   1.1  christos 
   1009   1.1  christos int
   1010  1.30     rmind sys__ksem_trywait(struct lwp *l, const struct sys__ksem_trywait_args *uap,
   1011  1.30     rmind     register_t *retval)
   1012   1.1  christos {
   1013  1.23       dsl 	/* {
   1014  1.29        ad 		intptr_t id;
   1015  1.23       dsl 	} */
   1016   1.1  christos 
   1017  1.36     joerg 	return do_ksem_wait(l, SCARG(uap, id), true, NULL);
   1018   1.1  christos }
   1019   1.1  christos 
   1020   1.1  christos int
   1021  1.30     rmind sys__ksem_getvalue(struct lwp *l, const struct sys__ksem_getvalue_args *uap,
   1022  1.30     rmind     register_t *retval)
   1023   1.1  christos {
   1024  1.23       dsl 	/* {
   1025  1.29        ad 		intptr_t id;
   1026   1.1  christos 		unsigned int *value;
   1027  1.23       dsl 	} */
   1028  1.52   thorpej 	int fd, error;
   1029  1.30     rmind 	ksem_t *ks;
   1030   1.1  christos 	unsigned int val;
   1031   1.1  christos 
   1032  1.52   thorpej 	error = ksem_get(SCARG(uap, id), &ks, &fd);
   1033  1.30     rmind 	if (error) {
   1034  1.30     rmind 		return error;
   1035  1.30     rmind 	}
   1036  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
   1037   1.1  christos 	val = ks->ks_value;
   1038  1.52   thorpej 	ksem_release(ks, fd);
   1039   1.3   thorpej 
   1040  1.30     rmind 	return copyout(&val, SCARG(uap, value), sizeof(val));
   1041   1.1  christos }
   1042   1.1  christos 
   1043   1.1  christos int
   1044  1.30     rmind sys__ksem_destroy(struct lwp *l, const struct sys__ksem_destroy_args *uap,
   1045  1.30     rmind     register_t *retval)
   1046   1.1  christos {
   1047  1.23       dsl 	/* {
   1048  1.29        ad 		intptr_t id;
   1049  1.23       dsl 	} */
   1050  1.52   thorpej 	int fd, error;
   1051  1.30     rmind 	ksem_t *ks;
   1052   1.1  christos 
   1053  1.52   thorpej 	intptr_t id = SCARG(uap, id);
   1054  1.52   thorpej 
   1055  1.52   thorpej 	error = ksem_get(id, &ks, &fd);
   1056  1.30     rmind 	if (error) {
   1057  1.30     rmind 		return error;
   1058   1.3   thorpej 	}
   1059  1.30     rmind 	KASSERT(mutex_owned(&ks->ks_lock));
   1060   1.3   thorpej 
   1061  1.30     rmind 	/* Operation is only for unnamed semaphores. */
   1062   1.3   thorpej 	if (ks->ks_name != NULL) {
   1063  1.30     rmind 		error = EINVAL;
   1064  1.30     rmind 		goto out;
   1065   1.3   thorpej 	}
   1066  1.30     rmind 	/* Cannot destroy if there are waiters. */
   1067   1.3   thorpej 	if (ks->ks_waiters) {
   1068  1.30     rmind 		error = EBUSY;
   1069  1.30     rmind 		goto out;
   1070   1.3   thorpej 	}
   1071  1.52   thorpej 	if (KSEM_ID_IS_PSHARED(id)) {
   1072  1.52   thorpej 		/* Cannot destroy if we did't create it. */
   1073  1.52   thorpej 		KASSERT(fd == -1);
   1074  1.52   thorpej 		KASSERT(ks->ks_pshared_proc != NULL);
   1075  1.52   thorpej 		if (ks->ks_pshared_proc != curproc) {
   1076  1.52   thorpej 			error = EINVAL;
   1077  1.52   thorpej 			goto out;
   1078  1.52   thorpej 		}
   1079  1.52   thorpej 		fd = ks->ks_pshared_fd;
   1080  1.52   thorpej 
   1081  1.52   thorpej 		/* Mark it dead so subsequent lookups fail. */
   1082  1.52   thorpej 		ks->ks_pshared_proc = NULL;
   1083  1.52   thorpej 
   1084  1.52   thorpej 		/* Do an fd_getfile() to for the benefit of fd_close(). */
   1085  1.52   thorpej 		file_t *fp __diagused = fd_getfile(fd);
   1086  1.52   thorpej 		KASSERT(fp != NULL);
   1087  1.52   thorpej 		KASSERT(fp->f_ksem == ks);
   1088  1.52   thorpej 	}
   1089  1.30     rmind out:
   1090  1.52   thorpej 	ksem_release(ks, -1);
   1091  1.30     rmind 	if (error) {
   1092  1.52   thorpej 		if (!KSEM_ID_IS_PSHARED(id))
   1093  1.52   thorpej 			fd_putfile(fd);
   1094  1.27        ad 		return error;
   1095  1.27        ad 	}
   1096  1.32     rmind 	return fd_close(fd);
   1097  1.22     rmind }
   1098