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