Home | History | Annotate | Line # | Download | only in kern
sys_mqueue.c revision 1.1
      1  1.1  rmind /*	$NetBSD: sys_mqueue.c,v 1.1 2007/09/07 18:56:09 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*
      4  1.1  rmind  * Copyright (c) 2007, Mindaugas Rasiukevicius <rmind at NetBSD org>
      5  1.1  rmind  *
      6  1.1  rmind  * Redistribution and use in source and binary forms, with or without
      7  1.1  rmind  * modification, are permitted provided that the following conditions
      8  1.1  rmind  * are met:
      9  1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     10  1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     11  1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  rmind  *    documentation and/or other materials provided with the distribution.
     14  1.1  rmind  *
     15  1.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  1.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  1.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  1.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  1.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  1.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  1.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  1.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  1.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  1.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     26  1.1  rmind  */
     27  1.1  rmind 
     28  1.1  rmind /*
     29  1.1  rmind  * Implementation of POSIX message queues.
     30  1.1  rmind  * Defined in the Base Definitions volume of IEEE Std 1003.1-2001.
     31  1.1  rmind  *
     32  1.1  rmind  * Locking
     33  1.1  rmind  *  Global list of message queues and proc::p_mqueue_cnt counter are protected
     34  1.1  rmind  *  by mqlist_mtx lock.  Concrete message queue and its members are protected
     35  1.1  rmind  *  by mqueue::mq_mtx.
     36  1.1  rmind  *
     37  1.1  rmind  * Lock order:
     38  1.1  rmind  * 	mqlist_mtx
     39  1.1  rmind  * 	  -> mqueue::mq_mtx
     40  1.1  rmind  *
     41  1.1  rmind  * TODO:
     42  1.1  rmind  *  - Hashing or RB-tree for the global list.
     43  1.1  rmind  *  - Support for select(), poll() and perhaps kqueue().
     44  1.1  rmind  */
     45  1.1  rmind 
     46  1.1  rmind #include <sys/cdefs.h>
     47  1.1  rmind __KERNEL_RCSID(0, "$NetBSD: sys_mqueue.c,v 1.1 2007/09/07 18:56:09 rmind Exp $");
     48  1.1  rmind 
     49  1.1  rmind #include <sys/param.h>
     50  1.1  rmind #include <sys/types.h>
     51  1.1  rmind 
     52  1.1  rmind #include <sys/condvar.h>
     53  1.1  rmind #include <sys/errno.h>
     54  1.1  rmind #include <sys/fcntl.h>
     55  1.1  rmind #include <sys/file.h>
     56  1.1  rmind #include <sys/filedesc.h>
     57  1.1  rmind #include <sys/kauth.h>
     58  1.1  rmind #include <sys/kernel.h>
     59  1.1  rmind #include <sys/kmem.h>
     60  1.1  rmind #include <sys/lwp.h>
     61  1.1  rmind #include <sys/mqueue.h>
     62  1.1  rmind #include <sys/mutex.h>
     63  1.1  rmind #include <sys/pool.h>
     64  1.1  rmind #include <sys/proc.h>
     65  1.1  rmind #include <sys/queue.h>
     66  1.1  rmind #include <sys/signal.h>
     67  1.1  rmind #include <sys/signalvar.h>
     68  1.1  rmind #include <sys/sysctl.h>
     69  1.1  rmind #include <sys/syscallargs.h>
     70  1.1  rmind #include <sys/systm.h>
     71  1.1  rmind #include <sys/unistd.h>
     72  1.1  rmind #include <sys/vnode.h>
     73  1.1  rmind 
     74  1.1  rmind /* System-wide limits. */
     75  1.1  rmind static u_int			mq_open_max = MQ_OPEN_MAX;
     76  1.1  rmind static u_int			mq_prio_max = MQ_PRIO_MAX;
     77  1.1  rmind 
     78  1.1  rmind static u_int			mq_max_msgsize = 16 * MQ_DEF_MSGSIZE;
     79  1.1  rmind static u_int			mq_def_maxmsg = 32;
     80  1.1  rmind 
     81  1.1  rmind static kmutex_t			mqlist_mtx;
     82  1.1  rmind static struct pool		mqmsg_poll;
     83  1.1  rmind static LIST_HEAD(, mqueue)	mqueue_head;
     84  1.1  rmind 
     85  1.1  rmind static int	mq_close_fop(struct file *, struct lwp *);
     86  1.1  rmind 
     87  1.1  rmind static const struct fileops mqops = {
     88  1.1  rmind 	fbadop_read, fbadop_write, fbadop_ioctl, fnullop_fcntl, fnullop_poll,
     89  1.1  rmind 	fbadop_stat, mq_close_fop, fnullop_kqfilter
     90  1.1  rmind };
     91  1.1  rmind 
     92  1.1  rmind /*
     93  1.1  rmind  * Initialize POSIX message queue subsystem.
     94  1.1  rmind  */
     95  1.1  rmind void
     96  1.1  rmind mqueue_sysinit(void)
     97  1.1  rmind {
     98  1.1  rmind 
     99  1.1  rmind 	pool_init(&mqmsg_poll, MQ_DEF_MSGSIZE, 0, 0, 0,
    100  1.1  rmind 	    "mqmsg_poll", &pool_allocator_nointr, IPL_NONE);
    101  1.1  rmind 	mutex_init(&mqlist_mtx, MUTEX_DEFAULT, IPL_NONE);
    102  1.1  rmind 	LIST_INIT(&mqueue_head);
    103  1.1  rmind }
    104  1.1  rmind 
    105  1.1  rmind /*
    106  1.1  rmind  * Free the message.
    107  1.1  rmind  */
    108  1.1  rmind static void
    109  1.1  rmind mqueue_freemsg(struct mq_msg *msg, const size_t size)
    110  1.1  rmind {
    111  1.1  rmind 
    112  1.1  rmind 	if (size > MQ_DEF_MSGSIZE)
    113  1.1  rmind 		kmem_free(msg, size);
    114  1.1  rmind 	else
    115  1.1  rmind 		pool_put(&mqmsg_poll, msg);
    116  1.1  rmind }
    117  1.1  rmind 
    118  1.1  rmind /*
    119  1.1  rmind  * Destroy the message queue.
    120  1.1  rmind  */
    121  1.1  rmind static void
    122  1.1  rmind mqueue_destroy(struct mqueue *mq)
    123  1.1  rmind {
    124  1.1  rmind 	struct mq_msg *msg;
    125  1.1  rmind 
    126  1.1  rmind 	while ((msg = TAILQ_FIRST(&mq->mq_head)) != NULL) {
    127  1.1  rmind 		TAILQ_REMOVE(&mq->mq_head, msg, msg_queue);
    128  1.1  rmind 		mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
    129  1.1  rmind 	}
    130  1.1  rmind 	cv_destroy(&mq->mq_send_cv);
    131  1.1  rmind 	cv_destroy(&mq->mq_recv_cv);
    132  1.1  rmind 	mutex_destroy(&mq->mq_mtx);
    133  1.1  rmind 	kmem_free(mq, sizeof(struct mqueue));
    134  1.1  rmind }
    135  1.1  rmind 
    136  1.1  rmind /*
    137  1.1  rmind  * Lookup for file name in general list of message queues.
    138  1.1  rmind  *  => locks the message queue
    139  1.1  rmind  */
    140  1.1  rmind static void *
    141  1.1  rmind mqueue_lookup(char *name)
    142  1.1  rmind {
    143  1.1  rmind 	struct mqueue *mq;
    144  1.1  rmind 	KASSERT(mutex_owned(&mqlist_mtx));
    145  1.1  rmind 
    146  1.1  rmind 	LIST_FOREACH(mq, &mqueue_head, mq_list) {
    147  1.1  rmind 		if (strncmp(mq->mq_name, name, MQ_NAMELEN) == 0) {
    148  1.1  rmind 			mutex_enter(&mq->mq_mtx);
    149  1.1  rmind 			return mq;
    150  1.1  rmind 		}
    151  1.1  rmind 	}
    152  1.1  rmind 
    153  1.1  rmind 	return NULL;
    154  1.1  rmind }
    155  1.1  rmind 
    156  1.1  rmind /*
    157  1.1  rmind  * Check access against message queue.
    158  1.1  rmind  */
    159  1.1  rmind static inline int
    160  1.1  rmind mqueue_access(struct lwp *l, struct mqueue *mq, mode_t acc_mode)
    161  1.1  rmind {
    162  1.1  rmind 
    163  1.1  rmind 	KASSERT(mutex_owned(&mq->mq_mtx));
    164  1.1  rmind 	return vaccess(VNON, mq->mq_mode, mq->mq_euid, mq->mq_egid,
    165  1.1  rmind 	    acc_mode, l->l_cred);
    166  1.1  rmind }
    167  1.1  rmind 
    168  1.1  rmind /*
    169  1.1  rmind  * Get the mqueue from the descriptor.
    170  1.1  rmind  *  => locks the message queue
    171  1.1  rmind  *  => increments the reference on file entry
    172  1.1  rmind  */
    173  1.1  rmind static int
    174  1.1  rmind mqueue_get(struct lwp *l, mqd_t mqd, mode_t acc_mode, struct file **fpr)
    175  1.1  rmind {
    176  1.1  rmind 	const struct proc *p = l->l_proc;
    177  1.1  rmind 	struct file *fp;
    178  1.1  rmind 	struct mqueue *mq;
    179  1.1  rmind 
    180  1.1  rmind 	/* Get the file and descriptor */
    181  1.1  rmind 	fp = fd_getfile(p->p_fd, (int)mqd);
    182  1.1  rmind 	if (fp == NULL)
    183  1.1  rmind 		return EBADF;
    184  1.1  rmind 
    185  1.1  rmind 	/* Increment the reference of file entry, and lock the mqueue */
    186  1.1  rmind 	FILE_USE(fp);
    187  1.1  rmind 	mq = fp->f_data;
    188  1.1  rmind 	*fpr = fp;
    189  1.1  rmind 	mutex_enter(&mq->mq_mtx);
    190  1.1  rmind 
    191  1.1  rmind 	/* Check the access mode and permission if needed */
    192  1.1  rmind 	if (acc_mode == VNOVAL)
    193  1.1  rmind 		return 0;
    194  1.1  rmind 	if ((acc_mode & fp->f_flag) == 0 || mqueue_access(l, mq, acc_mode)) {
    195  1.1  rmind 		mutex_exit(&mq->mq_mtx);
    196  1.1  rmind 		FILE_UNUSE(fp, l);
    197  1.1  rmind 		return EPERM;
    198  1.1  rmind 	}
    199  1.1  rmind 
    200  1.1  rmind 	return 0;
    201  1.1  rmind }
    202  1.1  rmind 
    203  1.1  rmind /*
    204  1.1  rmind  * Converter from struct timespec to the ticks.
    205  1.1  rmind  * Used by mq_timedreceive(), mq_timedsend().
    206  1.1  rmind  */
    207  1.1  rmind static int
    208  1.1  rmind abstimeout2timo(const void *uaddr, int *timo)
    209  1.1  rmind {
    210  1.1  rmind 	struct timespec ts;
    211  1.1  rmind 	int error;
    212  1.1  rmind 
    213  1.1  rmind 	error = copyin(uaddr, &ts, sizeof(struct timespec));
    214  1.1  rmind 	if (error)
    215  1.1  rmind 		return error;
    216  1.1  rmind 
    217  1.1  rmind 	/*
    218  1.1  rmind 	 * According to POSIX, validation check is needed only in case of
    219  1.1  rmind 	 * blocking.  Thus, set the invalid value right now, and fail latter.
    220  1.1  rmind 	 */
    221  1.1  rmind 	error = itimespecfix(&ts);
    222  1.1  rmind 	*timo = (error == 0) ? tstohz(&ts) : -1;
    223  1.1  rmind 
    224  1.1  rmind 	return 0;
    225  1.1  rmind }
    226  1.1  rmind 
    227  1.1  rmind static int
    228  1.1  rmind mq_close_fop(struct file *fp, struct lwp *l)
    229  1.1  rmind {
    230  1.1  rmind 	struct proc *p = l->l_proc;
    231  1.1  rmind 	struct mqueue *mq = fp->f_data;
    232  1.1  rmind 	bool destroy;
    233  1.1  rmind 
    234  1.1  rmind 	mutex_enter(&mqlist_mtx);
    235  1.1  rmind 	mutex_enter(&mq->mq_mtx);
    236  1.1  rmind 
    237  1.1  rmind 	/* Decrease the counters */
    238  1.1  rmind 	p->p_mqueue_cnt--;
    239  1.1  rmind 	mq->mq_refcnt--;
    240  1.1  rmind 
    241  1.1  rmind 	/* Remove notification if registered for this process */
    242  1.1  rmind 	if (mq->mq_notify_proc == p)
    243  1.1  rmind 		mq->mq_notify_proc = NULL;
    244  1.1  rmind 
    245  1.1  rmind 	/*
    246  1.1  rmind 	 * If this is the last reference and mqueue is marked for unlink,
    247  1.1  rmind 	 * remove and later destroy the message queue.
    248  1.1  rmind 	 */
    249  1.1  rmind 	if (mq->mq_refcnt == 0 && (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
    250  1.1  rmind 		LIST_REMOVE(mq, mq_list);
    251  1.1  rmind 		destroy = true;
    252  1.1  rmind 	} else
    253  1.1  rmind 		destroy = false;
    254  1.1  rmind 
    255  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    256  1.1  rmind 	mutex_exit(&mqlist_mtx);
    257  1.1  rmind 
    258  1.1  rmind 	if (destroy)
    259  1.1  rmind 		mqueue_destroy(mq);
    260  1.1  rmind 
    261  1.1  rmind 	return 0;
    262  1.1  rmind }
    263  1.1  rmind 
    264  1.1  rmind /*
    265  1.1  rmind  * General mqueue system calls.
    266  1.1  rmind  */
    267  1.1  rmind 
    268  1.1  rmind int
    269  1.1  rmind sys_mq_open(struct lwp *l, void *v, register_t *retval)
    270  1.1  rmind {
    271  1.1  rmind 	struct sys_mq_open_args /* {
    272  1.1  rmind 		syscallarg(const char *) name;
    273  1.1  rmind 		syscallarg(int) oflag;
    274  1.1  rmind 		syscallarg(mode_t) mode;
    275  1.1  rmind 		syscallarg(struct mq_attr) attr;
    276  1.1  rmind 	} */ *uap = v;
    277  1.1  rmind 	struct proc *p = l->l_proc;
    278  1.1  rmind 	struct mqueue *mq, *mq_new = NULL;
    279  1.1  rmind 	struct file *fp;
    280  1.1  rmind 	char *name;
    281  1.1  rmind 	int mqd, error, oflag;
    282  1.1  rmind 
    283  1.1  rmind 	/* Check access mode flags */
    284  1.1  rmind 	oflag = SCARG(uap, oflag);
    285  1.1  rmind 	if ((oflag & (O_RDWR | O_RDONLY | O_WRONLY)) == 0)
    286  1.1  rmind 		return EINVAL;
    287  1.1  rmind 
    288  1.1  rmind 	/* Get the name from the user-space */
    289  1.1  rmind 	name = kmem_zalloc(MQ_NAMELEN, KM_SLEEP);
    290  1.1  rmind 	error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
    291  1.1  rmind 	if (error) {
    292  1.1  rmind 		kmem_free(name, MQ_NAMELEN);
    293  1.1  rmind 		return error;
    294  1.1  rmind 	}
    295  1.1  rmind 
    296  1.1  rmind 	if (oflag & O_CREAT) {
    297  1.1  rmind 		struct mq_attr attr;
    298  1.1  rmind 
    299  1.1  rmind 		/* Check the limit */
    300  1.1  rmind 		if (p->p_mqueue_cnt == mq_open_max) {
    301  1.1  rmind 			kmem_free(name, MQ_NAMELEN);
    302  1.1  rmind 			return EMFILE;
    303  1.1  rmind 		}
    304  1.1  rmind 
    305  1.1  rmind 		/* Check for mqueue attributes */
    306  1.1  rmind 		if (SCARG(uap, attr)) {
    307  1.1  rmind 			error = copyin(SCARG(uap, attr), &attr,
    308  1.1  rmind 				sizeof(struct mq_attr));
    309  1.1  rmind 			if (error) {
    310  1.1  rmind 				kmem_free(name, MQ_NAMELEN);
    311  1.1  rmind 				return error;
    312  1.1  rmind 			}
    313  1.1  rmind 			if (attr.mq_maxmsg <= 0 || attr.mq_msgsize <= 0 ||
    314  1.1  rmind 			    attr.mq_msgsize > mq_max_msgsize) {
    315  1.1  rmind 				kmem_free(name, MQ_NAMELEN);
    316  1.1  rmind 				return EINVAL;
    317  1.1  rmind 			}
    318  1.1  rmind 			attr.mq_curmsgs = 0;
    319  1.1  rmind 		} else {
    320  1.1  rmind 			memset(&attr, 0, sizeof(struct mq_attr));
    321  1.1  rmind 			attr.mq_maxmsg = mq_def_maxmsg;
    322  1.1  rmind 			attr.mq_msgsize =
    323  1.1  rmind 			    MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
    324  1.1  rmind 		}
    325  1.1  rmind 
    326  1.1  rmind 		/*
    327  1.1  rmind 		 * Allocate new mqueue, initialize data structures,
    328  1.1  rmind 		 * copy the name, attributes and set the flag.
    329  1.1  rmind 		 */
    330  1.1  rmind 		mq_new = kmem_zalloc(sizeof(struct mqueue), KM_SLEEP);
    331  1.1  rmind 
    332  1.1  rmind 		mutex_init(&mq_new->mq_mtx, MUTEX_DEFAULT, IPL_NONE);
    333  1.1  rmind 		cv_init(&mq_new->mq_send_cv, "mqsendcv");
    334  1.1  rmind 		cv_init(&mq_new->mq_recv_cv, "mqrecvcv");
    335  1.1  rmind 		TAILQ_INIT(&mq_new->mq_head);
    336  1.1  rmind 
    337  1.1  rmind 		strlcpy(mq_new->mq_name, name, MQ_NAMELEN);
    338  1.1  rmind 		memcpy(&mq_new->mq_attrib, &attr, sizeof(struct mq_attr));
    339  1.1  rmind 		mq_new->mq_attrib.mq_flags = oflag;
    340  1.1  rmind 
    341  1.1  rmind 		/* Store mode and effective UID with GID */
    342  1.1  rmind 		mq_new->mq_mode = SCARG(uap, mode);
    343  1.1  rmind 		mq_new->mq_euid = kauth_cred_geteuid(l->l_cred);
    344  1.1  rmind 		mq_new->mq_egid = kauth_cred_getegid(l->l_cred);
    345  1.1  rmind 	}
    346  1.1  rmind 
    347  1.1  rmind 	/* Allocate file structure and descriptor */
    348  1.1  rmind 	error = falloc(l, &fp, &mqd);
    349  1.1  rmind 	if (error) {
    350  1.1  rmind 		if (mq_new)
    351  1.1  rmind 			mqueue_destroy(mq_new);
    352  1.1  rmind 		kmem_free(name, MQ_NAMELEN);
    353  1.1  rmind 		return error;
    354  1.1  rmind 	}
    355  1.1  rmind 	fp->f_type = DTYPE_MQUEUE;
    356  1.1  rmind 	fp->f_flag = (oflag & O_RDWR) ? (VREAD | VWRITE) :
    357  1.1  rmind 	    ((oflag & O_RDONLY) ? VREAD : VWRITE);
    358  1.1  rmind 	fp->f_ops = &mqops;
    359  1.1  rmind 
    360  1.1  rmind 	/* Look up for mqueue with such name */
    361  1.1  rmind 	mutex_enter(&mqlist_mtx);
    362  1.1  rmind 	mq = mqueue_lookup(name);
    363  1.1  rmind 	if (mq) {
    364  1.1  rmind 		KASSERT(mutex_owned(&mq->mq_mtx));
    365  1.1  rmind 		/* Check if mqueue is not marked as unlinking */
    366  1.1  rmind 		if (mq->mq_attrib.mq_flags & MQ_UNLINK) {
    367  1.1  rmind 			error = EACCES;
    368  1.1  rmind 			goto exit;
    369  1.1  rmind 		}
    370  1.1  rmind 		/* Fail if O_EXCL is set, and mqueue already exists */
    371  1.1  rmind 		if ((oflag & O_CREAT) && (oflag & O_EXCL)) {
    372  1.1  rmind 			error = EEXIST;
    373  1.1  rmind 			goto exit;
    374  1.1  rmind 		}
    375  1.1  rmind 		/* Check the permission */
    376  1.1  rmind 		if (mqueue_access(l, mq, fp->f_flag)) {
    377  1.1  rmind 			error = EACCES;
    378  1.1  rmind 			goto exit;
    379  1.1  rmind 		}
    380  1.1  rmind 	} else {
    381  1.1  rmind 		/* Fail if mqueue neither exists, nor we create it */
    382  1.1  rmind 		if ((oflag & O_CREAT) == 0) {
    383  1.1  rmind 			mutex_exit(&mqlist_mtx);
    384  1.1  rmind 			KASSERT(mq_new == NULL);
    385  1.1  rmind 			FILE_UNUSE(fp, l);
    386  1.1  rmind 			ffree(fp);
    387  1.1  rmind 			fdremove(p->p_fd, mqd);
    388  1.1  rmind 			kmem_free(name, MQ_NAMELEN);
    389  1.1  rmind 			return ENOENT;
    390  1.1  rmind 		}
    391  1.1  rmind 
    392  1.1  rmind 		/* Check the limit */
    393  1.1  rmind 		if (p->p_mqueue_cnt == mq_open_max) {
    394  1.1  rmind 			error = EMFILE;
    395  1.1  rmind 			goto exit;
    396  1.1  rmind 		}
    397  1.1  rmind 
    398  1.1  rmind 		/* Insert the queue to the list */
    399  1.1  rmind 		mq = mq_new;
    400  1.1  rmind 		mutex_enter(&mq->mq_mtx);
    401  1.1  rmind 		LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
    402  1.1  rmind 		mq_new = NULL;
    403  1.1  rmind 	}
    404  1.1  rmind 
    405  1.1  rmind 	/* Increase the counters, and make descriptor ready */
    406  1.1  rmind 	p->p_mqueue_cnt++;
    407  1.1  rmind 	mq->mq_refcnt++;
    408  1.1  rmind 	fp->f_data = mq;
    409  1.1  rmind 	FILE_SET_MATURE(fp);
    410  1.1  rmind exit:
    411  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    412  1.1  rmind 	mutex_exit(&mqlist_mtx);
    413  1.1  rmind 	FILE_UNUSE(fp, l);
    414  1.1  rmind 
    415  1.1  rmind 	if (mq_new)
    416  1.1  rmind 		mqueue_destroy(mq_new);
    417  1.1  rmind 	if (error) {
    418  1.1  rmind 		ffree(fp);
    419  1.1  rmind 		fdremove(p->p_fd, mqd);
    420  1.1  rmind 	} else
    421  1.1  rmind 		*retval = mqd;
    422  1.1  rmind 	kmem_free(name, MQ_NAMELEN);
    423  1.1  rmind 
    424  1.1  rmind 	return error;
    425  1.1  rmind }
    426  1.1  rmind 
    427  1.1  rmind int
    428  1.1  rmind sys_mq_close(struct lwp *l, void *v, register_t *retval)
    429  1.1  rmind {
    430  1.1  rmind 
    431  1.1  rmind 	return sys_close(l, v, retval);
    432  1.1  rmind }
    433  1.1  rmind 
    434  1.1  rmind /*
    435  1.1  rmind  * Primary mq_receive1() function.
    436  1.1  rmind  */
    437  1.1  rmind static int
    438  1.1  rmind mq_receive1(struct lwp *l, mqd_t mqdes, void *msg_ptr, size_t msg_len,
    439  1.1  rmind     unsigned *msg_prio, int t, ssize_t *mlen)
    440  1.1  rmind {
    441  1.1  rmind 	struct file *fp = NULL;
    442  1.1  rmind 	struct mqueue *mq;
    443  1.1  rmind 	struct mq_msg *msg = NULL;
    444  1.1  rmind 	int error;
    445  1.1  rmind 
    446  1.1  rmind 	/* Get the message queue */
    447  1.1  rmind 	error = mqueue_get(l, mqdes, VREAD, &fp);
    448  1.1  rmind 	if (error)
    449  1.1  rmind 		return error;
    450  1.1  rmind 	mq = fp->f_data;
    451  1.1  rmind 
    452  1.1  rmind 	/* Check the message size limits */
    453  1.1  rmind 	if (msg_len < mq->mq_attrib.mq_msgsize) {
    454  1.1  rmind 		error = EMSGSIZE;
    455  1.1  rmind 		goto error;
    456  1.1  rmind 	}
    457  1.1  rmind 
    458  1.1  rmind 	/* Check if queue is empty */
    459  1.1  rmind 	if (TAILQ_EMPTY(&mq->mq_head)) {
    460  1.1  rmind 		if (mq->mq_attrib.mq_flags & O_NONBLOCK) {
    461  1.1  rmind 			error = EAGAIN;
    462  1.1  rmind 			goto error;
    463  1.1  rmind 		}
    464  1.1  rmind 		if (t < 0) {
    465  1.1  rmind 			error = EINVAL;
    466  1.1  rmind 			goto error;
    467  1.1  rmind 		}
    468  1.1  rmind 		/*
    469  1.1  rmind 		 * Block until someone sends the message.
    470  1.1  rmind 		 * While doing this, notification should not be sent.
    471  1.1  rmind 		 */
    472  1.1  rmind 		mq->mq_attrib.mq_flags |= MQ_RECEIVE;
    473  1.1  rmind 		error = cv_timedwait_sig(&mq->mq_send_cv, &mq->mq_mtx, t);
    474  1.1  rmind 		mq->mq_attrib.mq_flags &= ~MQ_RECEIVE;
    475  1.1  rmind 		if (error || (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
    476  1.1  rmind 			if (error == EWOULDBLOCK)
    477  1.1  rmind 				error = ETIMEDOUT;
    478  1.1  rmind 			goto error;
    479  1.1  rmind 		}
    480  1.1  rmind 		KASSERT(!TAILQ_EMPTY(&mq->mq_head));
    481  1.1  rmind 	}
    482  1.1  rmind 
    483  1.1  rmind 	/* Remove the message from the queue */
    484  1.1  rmind 	msg = TAILQ_FIRST(&mq->mq_head);
    485  1.1  rmind 	KASSERT(msg != NULL);
    486  1.1  rmind 	TAILQ_REMOVE(&mq->mq_head, msg, msg_queue);
    487  1.1  rmind 
    488  1.1  rmind 	/* Decrement the counter and signal waiter, if any */
    489  1.1  rmind 	mq->mq_attrib.mq_curmsgs--;
    490  1.1  rmind 	cv_signal(&mq->mq_recv_cv);
    491  1.1  rmind error:
    492  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    493  1.1  rmind 	FILE_UNUSE(fp, l);
    494  1.1  rmind 	if (error)
    495  1.1  rmind 		return error;
    496  1.1  rmind 
    497  1.1  rmind 	/*
    498  1.1  rmind 	 * Copy the data to the user-space.
    499  1.1  rmind 	 * Note: According to POSIX, no message should be removed from the
    500  1.1  rmind 	 * queue in case of fail - this would be violated.
    501  1.1  rmind 	 */
    502  1.1  rmind 	*mlen = msg->msg_len;
    503  1.1  rmind 	error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
    504  1.1  rmind 	if (error == 0 && msg_prio)
    505  1.1  rmind 		error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
    506  1.1  rmind 	mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
    507  1.1  rmind 
    508  1.1  rmind 	return error;
    509  1.1  rmind }
    510  1.1  rmind 
    511  1.1  rmind int
    512  1.1  rmind sys_mq_receive(struct lwp *l, void *v, register_t *retval)
    513  1.1  rmind {
    514  1.1  rmind 	struct sys_mq_receive_args /* {
    515  1.1  rmind 		syscallarg(mqd_t) mqdes;
    516  1.1  rmind 		syscallarg(char *) msg_ptr;
    517  1.1  rmind 		syscallarg(size_t) msg_len;
    518  1.1  rmind 		syscallarg(unsigned *) msg_prio;
    519  1.1  rmind 	} */ *uap = v;
    520  1.1  rmind 	int error;
    521  1.1  rmind 	ssize_t mlen;
    522  1.1  rmind 
    523  1.1  rmind 	error = mq_receive1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    524  1.1  rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), 0, &mlen);
    525  1.1  rmind 	if (error == 0)
    526  1.1  rmind 		*retval = mlen;
    527  1.1  rmind 
    528  1.1  rmind 	return error;
    529  1.1  rmind }
    530  1.1  rmind 
    531  1.1  rmind int
    532  1.1  rmind sys_mq_timedreceive(struct lwp *l, void *v, register_t *retval)
    533  1.1  rmind {
    534  1.1  rmind 	struct sys_mq_timedreceive_args /* {
    535  1.1  rmind 		syscallarg(mqd_t) mqdes;
    536  1.1  rmind 		syscallarg(char *) msg_ptr;
    537  1.1  rmind 		syscallarg(size_t) msg_len;
    538  1.1  rmind 		syscallarg(unsigned *) msg_prio;
    539  1.1  rmind 		syscallarg(const struct timespec *) abs_timeout;
    540  1.1  rmind 	} */ *uap = v;
    541  1.1  rmind 	int error, t;
    542  1.1  rmind 	ssize_t mlen;
    543  1.1  rmind 
    544  1.1  rmind 	/* Get and convert time value */
    545  1.1  rmind 	if (SCARG(uap, abs_timeout)) {
    546  1.1  rmind 		error = abstimeout2timo(SCARG(uap, abs_timeout), &t);
    547  1.1  rmind 		if (error)
    548  1.1  rmind 			return error;
    549  1.1  rmind 	} else
    550  1.1  rmind 		t = 0;
    551  1.1  rmind 
    552  1.1  rmind 	error = mq_receive1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    553  1.1  rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), t, &mlen);
    554  1.1  rmind 	if (error == 0)
    555  1.1  rmind 		*retval = mlen;
    556  1.1  rmind 
    557  1.1  rmind 	return error;
    558  1.1  rmind }
    559  1.1  rmind 
    560  1.1  rmind /*
    561  1.1  rmind  * Primary mq_send1() function.
    562  1.1  rmind  */
    563  1.1  rmind static int
    564  1.1  rmind mq_send1(struct lwp *l, mqd_t mqdes, const char *msg_ptr, size_t msg_len,
    565  1.1  rmind     unsigned msg_prio, int t)
    566  1.1  rmind {
    567  1.1  rmind 	struct file *fp = NULL;
    568  1.1  rmind 	struct mqueue *mq;
    569  1.1  rmind 	struct mq_msg *msg, *pos_msg;
    570  1.1  rmind 	struct proc *notify = NULL;
    571  1.1  rmind 	ksiginfo_t ksi;
    572  1.1  rmind 	size_t size;
    573  1.1  rmind 	int error;
    574  1.1  rmind 
    575  1.1  rmind 	/* Check the priority range */
    576  1.1  rmind 	if (msg_prio >= mq_prio_max)
    577  1.1  rmind 		return EINVAL;
    578  1.1  rmind 
    579  1.1  rmind 	/* Allocate a new message */
    580  1.1  rmind 	size = sizeof(struct mq_msg) + msg_len;
    581  1.1  rmind 	if (size > mq_max_msgsize)
    582  1.1  rmind 		return EMSGSIZE;
    583  1.1  rmind 
    584  1.1  rmind 	if (size > MQ_DEF_MSGSIZE)
    585  1.1  rmind 		msg = kmem_alloc(size, KM_SLEEP);
    586  1.1  rmind 	else
    587  1.1  rmind 		msg = pool_get(&mqmsg_poll, PR_WAITOK);
    588  1.1  rmind 
    589  1.1  rmind 	/* Get the data from user-space */
    590  1.1  rmind 	error = copyin(msg_ptr, msg->msg_ptr, msg_len);
    591  1.1  rmind 	if (error) {
    592  1.1  rmind 		mqueue_freemsg(msg, size);
    593  1.1  rmind 		return error;
    594  1.1  rmind 	}
    595  1.1  rmind 	msg->msg_len = msg_len;
    596  1.1  rmind 	msg->msg_prio = msg_prio;
    597  1.1  rmind 
    598  1.1  rmind 	/* Get the mqueue */
    599  1.1  rmind 	error = mqueue_get(l, mqdes, VWRITE, &fp);
    600  1.1  rmind 	if (error) {
    601  1.1  rmind 		mqueue_freemsg(msg, size);
    602  1.1  rmind 		return error;
    603  1.1  rmind 	}
    604  1.1  rmind 	mq = fp->f_data;
    605  1.1  rmind 
    606  1.1  rmind 	/* Check the message size limit */
    607  1.1  rmind 	if (msg_len <= 0 || msg_len > mq->mq_attrib.mq_msgsize) {
    608  1.1  rmind 		error = EMSGSIZE;
    609  1.1  rmind 		goto error;
    610  1.1  rmind 	}
    611  1.1  rmind 
    612  1.1  rmind 	/* Check if queue is full */
    613  1.1  rmind 	if (mq->mq_attrib.mq_curmsgs == mq->mq_attrib.mq_maxmsg) {
    614  1.1  rmind 		if (mq->mq_attrib.mq_flags & O_NONBLOCK) {
    615  1.1  rmind 			error = EAGAIN;
    616  1.1  rmind 			goto error;
    617  1.1  rmind 		}
    618  1.1  rmind 		if (t < 0) {
    619  1.1  rmind 			error = EINVAL;
    620  1.1  rmind 			goto error;
    621  1.1  rmind 		}
    622  1.1  rmind 		/* Block until queue becomes available */
    623  1.1  rmind 		mq->mq_attrib.mq_flags |= MQ_SEND;
    624  1.1  rmind 		error = cv_timedwait_sig(&mq->mq_recv_cv, &mq->mq_mtx, t);
    625  1.1  rmind 		mq->mq_attrib.mq_flags &= ~MQ_SEND;
    626  1.1  rmind 		if (error || (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
    627  1.1  rmind 			error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
    628  1.1  rmind 			goto error;
    629  1.1  rmind 		}
    630  1.1  rmind 	}
    631  1.1  rmind 	KASSERT(mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg);
    632  1.1  rmind 
    633  1.1  rmind 	/* Insert message into the queue, according to the priority */
    634  1.1  rmind 	TAILQ_FOREACH(pos_msg, &mq->mq_head, msg_queue)
    635  1.1  rmind 		if (msg->msg_prio > pos_msg->msg_prio)
    636  1.1  rmind 			break;
    637  1.1  rmind 	if (pos_msg == NULL)
    638  1.1  rmind 		TAILQ_INSERT_TAIL(&mq->mq_head, msg, msg_queue);
    639  1.1  rmind 	else
    640  1.1  rmind 		TAILQ_INSERT_BEFORE(pos_msg, msg, msg_queue);
    641  1.1  rmind 
    642  1.1  rmind 	/* Check for the notify */
    643  1.1  rmind 	if (mq->mq_attrib.mq_curmsgs == 0 && mq->mq_notify_proc &&
    644  1.1  rmind 	    (mq->mq_attrib.mq_flags & MQ_RECEIVE) == 0) {
    645  1.1  rmind 		/* Initialize the signal */
    646  1.1  rmind 		KSI_INIT(&ksi);
    647  1.1  rmind 		ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;
    648  1.1  rmind 		ksi.ksi_code = SI_MESGQ;
    649  1.1  rmind 		ksi.ksi_value = mq->mq_sig_notify.sigev_value;
    650  1.1  rmind 		/* Unregister the process */
    651  1.1  rmind 		notify = mq->mq_notify_proc;
    652  1.1  rmind 		mq->mq_notify_proc = NULL;
    653  1.1  rmind 	}
    654  1.1  rmind 
    655  1.1  rmind 	/* Increment the counter and signal waiter, if any */
    656  1.1  rmind 	mq->mq_attrib.mq_curmsgs++;
    657  1.1  rmind 	cv_signal(&mq->mq_send_cv);
    658  1.1  rmind error:
    659  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    660  1.1  rmind 	FILE_UNUSE(fp, l);
    661  1.1  rmind 
    662  1.1  rmind 	if (error) {
    663  1.1  rmind 		mqueue_freemsg(msg, size);
    664  1.1  rmind 	} else if (notify) {
    665  1.1  rmind 		/* Send the notify, if needed */
    666  1.1  rmind 		mutex_enter(&proclist_mutex);
    667  1.1  rmind 		kpsignal(notify, &ksi, NULL);
    668  1.1  rmind 		mutex_exit(&proclist_mutex);
    669  1.1  rmind 	}
    670  1.1  rmind 
    671  1.1  rmind 	return error;
    672  1.1  rmind }
    673  1.1  rmind 
    674  1.1  rmind int
    675  1.1  rmind sys_mq_send(struct lwp *l, void *v, register_t *retval)
    676  1.1  rmind {
    677  1.1  rmind 	struct sys_mq_send_args /* {
    678  1.1  rmind 		syscallarg(mqd_t) mqdes;
    679  1.1  rmind 		syscallarg(const char *) msg_ptr;
    680  1.1  rmind 		syscallarg(size_t) msg_len;
    681  1.1  rmind 		syscallarg(unsigned) msg_prio;
    682  1.1  rmind 	} */ *uap = v;
    683  1.1  rmind 
    684  1.1  rmind 	return mq_send1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    685  1.1  rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), 0);
    686  1.1  rmind }
    687  1.1  rmind 
    688  1.1  rmind int
    689  1.1  rmind sys_mq_timedsend(struct lwp *l, void *v, register_t *retval)
    690  1.1  rmind {
    691  1.1  rmind 	struct sys_mq_timedsend_args /* {
    692  1.1  rmind 		syscallarg(mqd_t) mqdes;
    693  1.1  rmind 		syscallarg(const char *) msg_ptr;
    694  1.1  rmind 		syscallarg(size_t) msg_len;
    695  1.1  rmind 		syscallarg(unsigned) msg_prio;
    696  1.1  rmind 		syscallarg(const struct timespec *) abs_timeout;
    697  1.1  rmind 	} */ *uap = v;
    698  1.1  rmind 	int t;
    699  1.1  rmind 
    700  1.1  rmind 	/* Get and convert time value */
    701  1.1  rmind 	if (SCARG(uap, abs_timeout)) {
    702  1.1  rmind 		int error = abstimeout2timo(SCARG(uap, abs_timeout), &t);
    703  1.1  rmind 		if (error)
    704  1.1  rmind 			return error;
    705  1.1  rmind 	} else
    706  1.1  rmind 		t = 0;
    707  1.1  rmind 
    708  1.1  rmind 	return mq_send1(l, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    709  1.1  rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), t);
    710  1.1  rmind }
    711  1.1  rmind 
    712  1.1  rmind int
    713  1.1  rmind sys_mq_notify(struct lwp *l, void *v, register_t *retval)
    714  1.1  rmind {
    715  1.1  rmind 	struct sys_mq_notify_args /* {
    716  1.1  rmind 		syscallarg(mqd_t) mqdes;
    717  1.1  rmind 		syscallarg(const struct sigevent *) notification;
    718  1.1  rmind 	} */ *uap = v;
    719  1.1  rmind 	struct file *fp = NULL;
    720  1.1  rmind 	struct mqueue *mq;
    721  1.1  rmind 	struct sigevent sig;
    722  1.1  rmind 	int error;
    723  1.1  rmind 
    724  1.1  rmind 	if (SCARG(uap, notification)) {
    725  1.1  rmind 		/* Get the signal from user-space */
    726  1.1  rmind 		error = copyin(SCARG(uap, notification), &sig,
    727  1.1  rmind 		    sizeof(struct sigevent));
    728  1.1  rmind 		if (error)
    729  1.1  rmind 			return error;
    730  1.1  rmind 	}
    731  1.1  rmind 
    732  1.1  rmind 	error = mqueue_get(l, SCARG(uap, mqdes), VNOVAL, &fp);
    733  1.1  rmind 	if (error)
    734  1.1  rmind 		return error;
    735  1.1  rmind 	mq = fp->f_data;
    736  1.1  rmind 
    737  1.1  rmind 	if (SCARG(uap, notification)) {
    738  1.1  rmind 		/* Register notification: set the signal and target process */
    739  1.1  rmind 		if (mq->mq_notify_proc == NULL) {
    740  1.1  rmind 			memcpy(&mq->mq_sig_notify, &sig,
    741  1.1  rmind 			    sizeof(struct sigevent));
    742  1.1  rmind 			mq->mq_notify_proc = l->l_proc;
    743  1.1  rmind 		} else {
    744  1.1  rmind 			/* Fail if someone else already registered */
    745  1.1  rmind 			error = EBUSY;
    746  1.1  rmind 		}
    747  1.1  rmind 	} else {
    748  1.1  rmind 		/* Unregister the notification */
    749  1.1  rmind 		mq->mq_notify_proc = NULL;
    750  1.1  rmind 	}
    751  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    752  1.1  rmind 	FILE_UNUSE(fp, l);
    753  1.1  rmind 
    754  1.1  rmind 	return error;
    755  1.1  rmind }
    756  1.1  rmind 
    757  1.1  rmind int
    758  1.1  rmind sys_mq_getattr(struct lwp *l, void *v, register_t *retval)
    759  1.1  rmind {
    760  1.1  rmind 	struct sys_mq_getattr_args /* {
    761  1.1  rmind 		syscallarg(mqd_t) mqdes;
    762  1.1  rmind 		syscallarg(struct mq_attr *) mqstat;
    763  1.1  rmind 	} */ *uap = v;
    764  1.1  rmind 	struct file *fp = NULL;
    765  1.1  rmind 	struct mqueue *mq;
    766  1.1  rmind 	struct mq_attr attr;
    767  1.1  rmind 	int error;
    768  1.1  rmind 
    769  1.1  rmind 	/* Get the message queue */
    770  1.1  rmind 	error = mqueue_get(l, SCARG(uap, mqdes), VNOVAL, &fp);
    771  1.1  rmind 	if (error)
    772  1.1  rmind 		return error;
    773  1.1  rmind 	mq = fp->f_data;
    774  1.1  rmind 	memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
    775  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    776  1.1  rmind 	FILE_UNUSE(fp, l);
    777  1.1  rmind 
    778  1.1  rmind 	return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
    779  1.1  rmind }
    780  1.1  rmind 
    781  1.1  rmind int
    782  1.1  rmind sys_mq_setattr(struct lwp *l, void *v, register_t *retval)
    783  1.1  rmind {
    784  1.1  rmind 	struct sys_mq_setattr_args /* {
    785  1.1  rmind 		syscallarg(mqd_t) mqdes;
    786  1.1  rmind 		syscallarg(const struct mq_attr *) mqstat;
    787  1.1  rmind 		syscallarg(struct mq_attr *) omqstat;
    788  1.1  rmind 	} */ *uap = v;
    789  1.1  rmind 	struct file *fp = NULL;
    790  1.1  rmind 	struct mqueue *mq;
    791  1.1  rmind 	struct mq_attr attr;
    792  1.1  rmind 	int error, nonblock;
    793  1.1  rmind 
    794  1.1  rmind 	error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
    795  1.1  rmind 	if (error)
    796  1.1  rmind 		return error;
    797  1.1  rmind 	nonblock = (attr.mq_flags & O_NONBLOCK);
    798  1.1  rmind 
    799  1.1  rmind 	/* Get the message queue */
    800  1.1  rmind 	error = mqueue_get(l, SCARG(uap, mqdes), VNOVAL, &fp);
    801  1.1  rmind 	if (error)
    802  1.1  rmind 		return error;
    803  1.1  rmind 	mq = fp->f_data;
    804  1.1  rmind 
    805  1.1  rmind 	/* Copy the old attributes, if needed */
    806  1.1  rmind 	if (SCARG(uap, omqstat))
    807  1.1  rmind 		memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
    808  1.1  rmind 
    809  1.1  rmind 	/* Ignore everything, except O_NONBLOCK */
    810  1.1  rmind 	if (nonblock)
    811  1.1  rmind 		mq->mq_attrib.mq_flags |= O_NONBLOCK;
    812  1.1  rmind 	else
    813  1.1  rmind 		mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
    814  1.1  rmind 
    815  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    816  1.1  rmind 	FILE_UNUSE(fp, l);
    817  1.1  rmind 
    818  1.1  rmind 	/*
    819  1.1  rmind 	 * Copy the data to the user-space.
    820  1.1  rmind 	 * Note: According to POSIX, the new attributes should not be set in
    821  1.1  rmind 	 * case of fail - this would be violated.
    822  1.1  rmind 	 */
    823  1.1  rmind 	if (SCARG(uap, omqstat))
    824  1.1  rmind 		error = copyout(&attr, SCARG(uap, omqstat),
    825  1.1  rmind 		    sizeof(struct mq_attr));
    826  1.1  rmind 
    827  1.1  rmind 	return error;
    828  1.1  rmind }
    829  1.1  rmind 
    830  1.1  rmind int
    831  1.1  rmind sys_mq_unlink(struct lwp *l, void *v, register_t *retval)
    832  1.1  rmind {
    833  1.1  rmind 	struct sys_mq_unlink_args /* {
    834  1.1  rmind 		syscallarg(const char *) name;
    835  1.1  rmind 	} */ *uap = v;
    836  1.1  rmind 	struct mqueue *mq;
    837  1.1  rmind 	char *name;
    838  1.1  rmind 	int error, refcnt = 0;
    839  1.1  rmind 
    840  1.1  rmind 	/* Get the name from the user-space */
    841  1.1  rmind 	name = kmem_zalloc(MQ_NAMELEN, KM_SLEEP);
    842  1.1  rmind 	error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
    843  1.1  rmind 	if (error) {
    844  1.1  rmind 		kmem_free(name, MQ_NAMELEN);
    845  1.1  rmind 		return error;
    846  1.1  rmind 	}
    847  1.1  rmind 
    848  1.1  rmind 	/* Lookup for this file */
    849  1.1  rmind 	mutex_enter(&mqlist_mtx);
    850  1.1  rmind 	mq = mqueue_lookup(name);
    851  1.1  rmind 	if (mq == NULL) {
    852  1.1  rmind 		error = ENOENT;
    853  1.1  rmind 		goto error;
    854  1.1  rmind 	}
    855  1.1  rmind 
    856  1.1  rmind 	/* Check the permissions */
    857  1.1  rmind 	if (mqueue_access(l, mq, VWRITE)) {
    858  1.1  rmind 		mutex_exit(&mq->mq_mtx);
    859  1.1  rmind 		error = EACCES;
    860  1.1  rmind 		goto error;
    861  1.1  rmind 	}
    862  1.1  rmind 
    863  1.1  rmind 	/* Mark message queue as unlinking, before leaving the window */
    864  1.1  rmind 	mq->mq_attrib.mq_flags |= MQ_UNLINK;
    865  1.1  rmind 
    866  1.1  rmind 	/* Wake up all waiters, if there are such */
    867  1.1  rmind 	cv_broadcast(&mq->mq_send_cv);
    868  1.1  rmind 	cv_broadcast(&mq->mq_recv_cv);
    869  1.1  rmind 
    870  1.1  rmind 	refcnt = mq->mq_refcnt;
    871  1.1  rmind 	if (refcnt == 0)
    872  1.1  rmind 		LIST_REMOVE(mq, mq_list);
    873  1.1  rmind 
    874  1.1  rmind 	mutex_exit(&mq->mq_mtx);
    875  1.1  rmind error:
    876  1.1  rmind 	mutex_exit(&mqlist_mtx);
    877  1.1  rmind 
    878  1.1  rmind 	/*
    879  1.1  rmind 	 * If there are no references - destroy the message
    880  1.1  rmind 	 * queue, otherwise, the last mq_close() will do that.
    881  1.1  rmind 	 */
    882  1.1  rmind 	if (error == 0 && refcnt == 0)
    883  1.1  rmind 		mqueue_destroy(mq);
    884  1.1  rmind 
    885  1.1  rmind 	kmem_free(name, MQ_NAMELEN);
    886  1.1  rmind 	return error;
    887  1.1  rmind }
    888  1.1  rmind 
    889  1.1  rmind /*
    890  1.1  rmind  * SysCtl.
    891  1.1  rmind  */
    892  1.1  rmind 
    893  1.1  rmind SYSCTL_SETUP(sysctl_mqueue_setup, "sysctl mqueue setup")
    894  1.1  rmind {
    895  1.1  rmind 	const struct sysctlnode *node = NULL;
    896  1.1  rmind 
    897  1.1  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    898  1.1  rmind 		CTLFLAG_PERMANENT,
    899  1.1  rmind 		CTLTYPE_NODE, "kern", NULL,
    900  1.1  rmind 		NULL, 0, NULL, 0,
    901  1.1  rmind 		CTL_KERN, CTL_EOL);
    902  1.1  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    903  1.1  rmind 		CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    904  1.1  rmind 		CTLTYPE_INT, "posix_msg",
    905  1.1  rmind 		SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    906  1.1  rmind 			     "Message Passing option to which the "
    907  1.1  rmind 			     "system attempts to conform"),
    908  1.1  rmind 		NULL, _POSIX_MESSAGE_PASSING, NULL, 0,
    909  1.1  rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    910  1.1  rmind 	sysctl_createv(clog, 0, NULL, &node,
    911  1.1  rmind 		CTLFLAG_PERMANENT,
    912  1.1  rmind 		CTLTYPE_NODE, "mqueue",
    913  1.1  rmind 		SYSCTL_DESCR("Message queue options"),
    914  1.1  rmind 		NULL, 0, NULL, 0,
    915  1.1  rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    916  1.1  rmind 
    917  1.1  rmind 	if (node == NULL)
    918  1.1  rmind 		return;
    919  1.1  rmind 
    920  1.1  rmind 	sysctl_createv(clog, 0, &node, NULL,
    921  1.1  rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    922  1.1  rmind 		CTLTYPE_INT, "mq_open_max",
    923  1.1  rmind 		SYSCTL_DESCR("Maximal number of message queue descriptors "
    924  1.1  rmind 			     "that process could open"),
    925  1.1  rmind 		NULL, 0, &mq_open_max, 0,
    926  1.1  rmind 		CTL_CREATE, CTL_EOL);
    927  1.1  rmind 	sysctl_createv(clog, 0, &node, NULL,
    928  1.1  rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    929  1.1  rmind 		CTLTYPE_INT, "mq_prio_max",
    930  1.1  rmind 		SYSCTL_DESCR("Maximal priority of the message"),
    931  1.1  rmind 		NULL, 0, &mq_prio_max, 0,
    932  1.1  rmind 		CTL_CREATE, CTL_EOL);
    933  1.1  rmind 	sysctl_createv(clog, 0, &node, NULL,
    934  1.1  rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    935  1.1  rmind 		CTLTYPE_INT, "mq_max_msgsize",
    936  1.1  rmind 		SYSCTL_DESCR("Maximal allowed size of the message"),
    937  1.1  rmind 		NULL, 0, &mq_max_msgsize, 0,
    938  1.1  rmind 		CTL_CREATE, CTL_EOL);
    939  1.1  rmind 	sysctl_createv(clog, 0, &node, NULL,
    940  1.1  rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    941  1.1  rmind 		CTLTYPE_INT, "mq_def_maxmsg",
    942  1.1  rmind 		SYSCTL_DESCR("Default maximal message count"),
    943  1.1  rmind 		NULL, 0, &mq_def_maxmsg, 0,
    944  1.1  rmind 		CTL_CREATE, CTL_EOL);
    945  1.1  rmind }
    946  1.1  rmind 
    947  1.1  rmind /*
    948  1.1  rmind  * Debugging.
    949  1.1  rmind  */
    950  1.1  rmind #if defined(DDB)
    951  1.1  rmind 
    952  1.1  rmind void
    953  1.1  rmind mqueue_print_list(void (*pr)(const char *, ...))
    954  1.1  rmind {
    955  1.1  rmind 	struct mqueue *mq;
    956  1.1  rmind 
    957  1.1  rmind 	(*pr)("Global list of the message queues:\n");
    958  1.1  rmind 	(*pr)("%20s %10s %8s %8s %3s %4s %4s %4s\n",
    959  1.1  rmind 	    "Name", "Ptr", "Mode", "Flags",  "Ref",
    960  1.1  rmind 	    "MaxMsg", "MsgSze", "CurMsg");
    961  1.1  rmind 	LIST_FOREACH(mq, &mqueue_head, mq_list) {
    962  1.1  rmind 		(*pr)("%20s %10p %8x %8x %3u %6lu %6lu %6lu\n",
    963  1.1  rmind 		    mq->mq_name, mq, mq->mq_mode,
    964  1.1  rmind 		    mq->mq_attrib.mq_flags, mq->mq_refcnt,
    965  1.1  rmind 		    mq->mq_attrib.mq_maxmsg, mq->mq_attrib.mq_msgsize,
    966  1.1  rmind 		    mq->mq_attrib.mq_curmsgs);
    967  1.1  rmind 	}
    968  1.1  rmind }
    969  1.1  rmind 
    970  1.1  rmind #endif /* defined(DDB) */
    971