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