Home | History | Annotate | Line # | Download | only in kern
sys_mqueue.c revision 1.40.4.1
      1  1.40.4.1  christos /*	$NetBSD: sys_mqueue.c,v 1.40.4.1 2019/06/10 22:09:03 christos Exp $	*/
      2       1.1     rmind 
      3       1.1     rmind /*
      4      1.32     rmind  * Copyright (c) 2007-2011 Mindaugas Rasiukevicius <rmind at NetBSD org>
      5       1.5     rmind  * All rights reserved.
      6       1.1     rmind  *
      7       1.1     rmind  * Redistribution and use in source and binary forms, with or without
      8       1.1     rmind  * modification, are permitted provided that the following conditions
      9       1.1     rmind  * are met:
     10       1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     11       1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     12       1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     14       1.1     rmind  *    documentation and/or other materials provided with the distribution.
     15       1.1     rmind  *
     16       1.8     rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.8     rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.8     rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.8     rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.8     rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.8     rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.8     rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.8     rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.8     rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.8     rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.8     rmind  * SUCH DAMAGE.
     27       1.1     rmind  */
     28       1.1     rmind 
     29       1.1     rmind /*
     30       1.1     rmind  * Implementation of POSIX message queues.
     31       1.1     rmind  * Defined in the Base Definitions volume of IEEE Std 1003.1-2001.
     32      1.12     rmind  *
     33      1.12     rmind  * Locking
     34      1.32     rmind  *
     35      1.32     rmind  * Global list of message queues (mqueue_head) is protected by mqlist_lock.
     36      1.32     rmind  * Each message queue and its members are protected by mqueue::mq_mtx.
     37      1.32     rmind  * Note that proc_t::p_mqueue_cnt is updated atomically.
     38      1.32     rmind  *
     39       1.1     rmind  * Lock order:
     40      1.32     rmind  *
     41      1.32     rmind  *	mqlist_lock ->
     42      1.32     rmind  *		mqueue::mq_mtx
     43       1.1     rmind  */
     44       1.1     rmind 
     45       1.1     rmind #include <sys/cdefs.h>
     46  1.40.4.1  christos __KERNEL_RCSID(0, "$NetBSD: sys_mqueue.c,v 1.40.4.1 2019/06/10 22:09:03 christos Exp $");
     47       1.1     rmind 
     48       1.1     rmind #include <sys/param.h>
     49       1.1     rmind #include <sys/types.h>
     50      1.33    martin #include <sys/atomic.h>
     51      1.32     rmind 
     52       1.1     rmind #include <sys/file.h>
     53       1.1     rmind #include <sys/filedesc.h>
     54       1.1     rmind #include <sys/kauth.h>
     55       1.1     rmind #include <sys/lwp.h>
     56       1.1     rmind #include <sys/mqueue.h>
     57      1.24     rmind #include <sys/module.h>
     58       1.8     rmind #include <sys/poll.h>
     59       1.8     rmind #include <sys/select.h>
     60       1.1     rmind #include <sys/signal.h>
     61       1.1     rmind #include <sys/signalvar.h>
     62      1.12     rmind #include <sys/stat.h>
     63       1.1     rmind #include <sys/sysctl.h>
     64      1.24     rmind #include <sys/syscall.h>
     65      1.24     rmind #include <sys/syscallvar.h>
     66       1.1     rmind #include <sys/syscallargs.h>
     67       1.1     rmind 
     68      1.19      elad #include <miscfs/genfs/genfs.h>
     69      1.19      elad 
     70      1.24     rmind MODULE(MODULE_CLASS_MISC, mqueue, NULL);
     71      1.24     rmind 
     72       1.1     rmind /* System-wide limits. */
     73       1.1     rmind static u_int			mq_open_max = MQ_OPEN_MAX;
     74       1.1     rmind static u_int			mq_prio_max = MQ_PRIO_MAX;
     75       1.1     rmind static u_int			mq_max_msgsize = 16 * MQ_DEF_MSGSIZE;
     76       1.1     rmind static u_int			mq_def_maxmsg = 32;
     77      1.28  drochner static u_int			mq_max_maxmsg = 16 * 32;
     78       1.1     rmind 
     79      1.32     rmind static pool_cache_t		mqmsg_cache	__read_mostly;
     80      1.32     rmind static kmutex_t			mqlist_lock	__cacheline_aligned;
     81      1.32     rmind static LIST_HEAD(, mqueue)	mqueue_head	__cacheline_aligned;
     82      1.32     rmind static struct sysctllog *	mqsysctl_log;
     83       1.1     rmind 
     84      1.34      elad static kauth_listener_t		mq_listener;
     85      1.34      elad 
     86      1.24     rmind static int	mqueue_sysinit(void);
     87      1.24     rmind static int	mqueue_sysfini(bool);
     88      1.30    jruoho static int	mqueue_sysctl_init(void);
     89       1.8     rmind static int	mq_poll_fop(file_t *, int);
     90      1.15  christos static int	mq_stat_fop(file_t *, struct stat *);
     91       1.7        ad static int	mq_close_fop(file_t *);
     92       1.1     rmind 
     93       1.1     rmind static const struct fileops mqops = {
     94      1.40  christos 	.fo_name = "mq",
     95      1.14        ad 	.fo_read = fbadop_read,
     96      1.14        ad 	.fo_write = fbadop_write,
     97      1.14        ad 	.fo_ioctl = fbadop_ioctl,
     98      1.14        ad 	.fo_fcntl = fnullop_fcntl,
     99      1.14        ad 	.fo_poll = mq_poll_fop,
    100      1.15  christos 	.fo_stat = mq_stat_fop,
    101      1.14        ad 	.fo_close = mq_close_fop,
    102      1.14        ad 	.fo_kqfilter = fnullop_kqfilter,
    103      1.29       dsl 	.fo_restart = fnullop_restart,
    104       1.1     rmind };
    105       1.1     rmind 
    106      1.24     rmind static const struct syscall_package mqueue_syscalls[] = {
    107      1.24     rmind 	{ SYS_mq_open, 0, (sy_call_t *)sys_mq_open },
    108      1.24     rmind 	{ SYS_mq_close, 0, (sy_call_t *)sys_mq_close },
    109      1.24     rmind 	{ SYS_mq_unlink, 0, (sy_call_t *)sys_mq_unlink },
    110      1.24     rmind 	{ SYS_mq_getattr, 0, (sy_call_t *)sys_mq_getattr },
    111      1.24     rmind 	{ SYS_mq_setattr, 0, (sy_call_t *)sys_mq_setattr },
    112      1.24     rmind 	{ SYS_mq_notify, 0, (sy_call_t *)sys_mq_notify },
    113      1.24     rmind 	{ SYS_mq_send, 0, (sy_call_t *)sys_mq_send },
    114      1.24     rmind 	{ SYS_mq_receive, 0, (sy_call_t *)sys_mq_receive },
    115      1.24     rmind 	{ SYS___mq_timedsend50, 0, (sy_call_t *)sys___mq_timedsend50 },
    116      1.24     rmind 	{ SYS___mq_timedreceive50, 0, (sy_call_t *)sys___mq_timedreceive50 },
    117      1.24     rmind 	{ 0, 0, NULL }
    118      1.24     rmind };
    119      1.24     rmind 
    120      1.34      elad static int
    121      1.34      elad mq_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    122      1.34      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    123      1.34      elad {
    124      1.34      elad 	mqueue_t *mq;
    125      1.34      elad 	int result;
    126      1.34      elad 
    127      1.34      elad 	if (action != KAUTH_SYSTEM_MQUEUE)
    128      1.34      elad 		return KAUTH_RESULT_DEFER;
    129      1.34      elad 
    130      1.34      elad 	result = KAUTH_RESULT_DEFER;
    131      1.34      elad 
    132      1.34      elad 	mq = arg1;
    133      1.34      elad 
    134      1.34      elad 	if (kauth_cred_geteuid(cred) == mq->mq_euid)
    135      1.34      elad 		result = KAUTH_RESULT_ALLOW;
    136      1.34      elad 
    137      1.34      elad 	return result;
    138      1.34      elad }
    139      1.34      elad 
    140       1.1     rmind /*
    141      1.24     rmind  * Initialisation and unloading of POSIX message queue subsystem.
    142       1.1     rmind  */
    143      1.24     rmind 
    144      1.24     rmind static int
    145       1.1     rmind mqueue_sysinit(void)
    146       1.1     rmind {
    147      1.24     rmind 	int error;
    148       1.1     rmind 
    149       1.9        ad 	mqmsg_cache = pool_cache_init(MQ_DEF_MSGSIZE, coherency_unit,
    150      1.12     rmind 	    0, 0, "mqmsgpl", NULL, IPL_NONE, NULL, NULL, NULL);
    151      1.32     rmind 	mutex_init(&mqlist_lock, MUTEX_DEFAULT, IPL_NONE);
    152      1.22     rmind 	LIST_INIT(&mqueue_head);
    153      1.24     rmind 
    154      1.30    jruoho 	error = mqueue_sysctl_init();
    155      1.30    jruoho 	if (error) {
    156      1.30    jruoho 		(void)mqueue_sysfini(false);
    157      1.30    jruoho 		return error;
    158      1.30    jruoho 	}
    159      1.24     rmind 	error = syscall_establish(NULL, mqueue_syscalls);
    160      1.24     rmind 	if (error) {
    161      1.24     rmind 		(void)mqueue_sysfini(false);
    162      1.24     rmind 	}
    163      1.34      elad 	mq_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    164      1.34      elad 	    mq_listener_cb, NULL);
    165      1.24     rmind 	return error;
    166      1.24     rmind }
    167      1.24     rmind 
    168      1.24     rmind static int
    169      1.24     rmind mqueue_sysfini(bool interface)
    170      1.24     rmind {
    171      1.24     rmind 
    172      1.24     rmind 	if (interface) {
    173      1.24     rmind 		int error;
    174      1.24     rmind 		bool inuse;
    175      1.24     rmind 
    176      1.24     rmind 		/* Stop syscall activity. */
    177      1.24     rmind 		error = syscall_disestablish(NULL, mqueue_syscalls);
    178      1.24     rmind 		if (error)
    179      1.24     rmind 			return error;
    180      1.32     rmind 		/* Check if there are any message queues in use. */
    181      1.32     rmind 		mutex_enter(&mqlist_lock);
    182      1.24     rmind 		inuse = !LIST_EMPTY(&mqueue_head);
    183      1.32     rmind 		mutex_exit(&mqlist_lock);
    184      1.24     rmind 		if (inuse) {
    185      1.24     rmind 			error = syscall_establish(NULL, mqueue_syscalls);
    186      1.24     rmind 			KASSERT(error == 0);
    187      1.24     rmind 			return EBUSY;
    188      1.24     rmind 		}
    189      1.24     rmind 	}
    190      1.30    jruoho 
    191      1.30    jruoho 	if (mqsysctl_log != NULL)
    192      1.30    jruoho 		sysctl_teardown(&mqsysctl_log);
    193      1.30    jruoho 
    194      1.34      elad 	kauth_unlisten_scope(mq_listener);
    195      1.34      elad 
    196      1.32     rmind 	mutex_destroy(&mqlist_lock);
    197      1.24     rmind 	pool_cache_destroy(mqmsg_cache);
    198      1.24     rmind 	return 0;
    199      1.24     rmind }
    200      1.24     rmind 
    201      1.24     rmind /*
    202      1.24     rmind  * Module interface.
    203      1.24     rmind  */
    204      1.24     rmind static int
    205      1.24     rmind mqueue_modcmd(modcmd_t cmd, void *arg)
    206      1.24     rmind {
    207      1.24     rmind 
    208      1.24     rmind 	switch (cmd) {
    209      1.24     rmind 	case MODULE_CMD_INIT:
    210      1.24     rmind 		return mqueue_sysinit();
    211      1.24     rmind 	case MODULE_CMD_FINI:
    212      1.24     rmind 		return mqueue_sysfini(true);
    213      1.24     rmind 	default:
    214      1.24     rmind 		return ENOTTY;
    215      1.24     rmind 	}
    216       1.1     rmind }
    217       1.1     rmind 
    218       1.1     rmind /*
    219       1.1     rmind  * Free the message.
    220       1.1     rmind  */
    221       1.1     rmind static void
    222       1.1     rmind mqueue_freemsg(struct mq_msg *msg, const size_t size)
    223       1.1     rmind {
    224       1.1     rmind 
    225      1.22     rmind 	if (size > MQ_DEF_MSGSIZE) {
    226       1.1     rmind 		kmem_free(msg, size);
    227      1.22     rmind 	} else {
    228       1.8     rmind 		pool_cache_put(mqmsg_cache, msg);
    229      1.22     rmind 	}
    230       1.1     rmind }
    231       1.1     rmind 
    232       1.1     rmind /*
    233       1.1     rmind  * Destroy the message queue.
    234       1.1     rmind  */
    235       1.1     rmind static void
    236       1.1     rmind mqueue_destroy(struct mqueue *mq)
    237       1.1     rmind {
    238       1.1     rmind 	struct mq_msg *msg;
    239      1.22     rmind 	size_t msz;
    240      1.22     rmind 	u_int i;
    241       1.1     rmind 
    242      1.23     rmind 	/* Note MQ_PQSIZE + 1. */
    243      1.23     rmind 	for (i = 0; i <= MQ_PQSIZE; i++) {
    244      1.22     rmind 		while ((msg = TAILQ_FIRST(&mq->mq_head[i])) != NULL) {
    245      1.22     rmind 			TAILQ_REMOVE(&mq->mq_head[i], msg, msg_queue);
    246      1.22     rmind 			msz = sizeof(struct mq_msg) + msg->msg_len;
    247      1.22     rmind 			mqueue_freemsg(msg, msz);
    248      1.22     rmind 		}
    249       1.1     rmind 	}
    250      1.32     rmind 	if (mq->mq_name) {
    251      1.32     rmind 		kmem_free(mq->mq_name, MQ_NAMELEN);
    252      1.32     rmind 	}
    253       1.8     rmind 	seldestroy(&mq->mq_rsel);
    254       1.8     rmind 	seldestroy(&mq->mq_wsel);
    255       1.1     rmind 	cv_destroy(&mq->mq_send_cv);
    256       1.1     rmind 	cv_destroy(&mq->mq_recv_cv);
    257       1.1     rmind 	mutex_destroy(&mq->mq_mtx);
    258       1.1     rmind 	kmem_free(mq, sizeof(struct mqueue));
    259       1.1     rmind }
    260       1.1     rmind 
    261       1.1     rmind /*
    262      1.32     rmind  * mqueue_lookup: lookup for file name in general list of message queues.
    263      1.32     rmind  *
    264      1.32     rmind  * => locks the message queue on success
    265       1.1     rmind  */
    266      1.32     rmind static mqueue_t *
    267      1.32     rmind mqueue_lookup(const char *name)
    268       1.1     rmind {
    269      1.32     rmind 	mqueue_t *mq;
    270      1.32     rmind 
    271      1.32     rmind 	KASSERT(mutex_owned(&mqlist_lock));
    272       1.1     rmind 
    273       1.1     rmind 	LIST_FOREACH(mq, &mqueue_head, mq_list) {
    274       1.1     rmind 		if (strncmp(mq->mq_name, name, MQ_NAMELEN) == 0) {
    275       1.1     rmind 			mutex_enter(&mq->mq_mtx);
    276       1.1     rmind 			return mq;
    277       1.1     rmind 		}
    278       1.1     rmind 	}
    279       1.1     rmind 	return NULL;
    280       1.1     rmind }
    281       1.1     rmind 
    282       1.1     rmind /*
    283      1.18     rmind  * mqueue_get: get the mqueue from the descriptor.
    284      1.32     rmind  *
    285      1.32     rmind  * => locks the message queue, if found.
    286      1.32     rmind  * => holds a reference on the file descriptor.
    287       1.1     rmind  */
    288      1.38    martin int
    289      1.32     rmind mqueue_get(mqd_t mqd, int fflag, mqueue_t **mqret)
    290       1.1     rmind {
    291      1.32     rmind 	const int fd = (int)mqd;
    292      1.32     rmind 	mqueue_t *mq;
    293       1.7        ad 	file_t *fp;
    294       1.1     rmind 
    295      1.32     rmind 	fp = fd_getfile(fd);
    296      1.18     rmind 	if (__predict_false(fp == NULL)) {
    297      1.18     rmind 		return EBADF;
    298      1.18     rmind 	}
    299      1.18     rmind 	if (__predict_false(fp->f_type != DTYPE_MQUEUE)) {
    300      1.32     rmind 		fd_putfile(fd);
    301      1.32     rmind 		return EBADF;
    302      1.32     rmind 	}
    303      1.32     rmind 	if (fflag && (fp->f_flag & fflag) == 0) {
    304      1.32     rmind 		fd_putfile(fd);
    305       1.1     rmind 		return EBADF;
    306      1.18     rmind 	}
    307      1.37      matt 	mq = fp->f_mqueue;
    308       1.1     rmind 	mutex_enter(&mq->mq_mtx);
    309       1.1     rmind 
    310      1.32     rmind 	*mqret = mq;
    311       1.1     rmind 	return 0;
    312       1.1     rmind }
    313       1.1     rmind 
    314       1.1     rmind /*
    315      1.22     rmind  * mqueue_linear_insert: perform linear insert according to the message
    316      1.23     rmind  * priority into the reserved queue (MQ_PQRESQ).  Reserved queue is a
    317      1.23     rmind  * sorted list used only when mq_prio_max is increased via sysctl.
    318      1.22     rmind  */
    319      1.22     rmind static inline void
    320      1.22     rmind mqueue_linear_insert(struct mqueue *mq, struct mq_msg *msg)
    321      1.22     rmind {
    322      1.22     rmind 	struct mq_msg *mit;
    323      1.22     rmind 
    324      1.23     rmind 	TAILQ_FOREACH(mit, &mq->mq_head[MQ_PQRESQ], msg_queue) {
    325      1.22     rmind 		if (msg->msg_prio > mit->msg_prio)
    326      1.22     rmind 			break;
    327      1.22     rmind 	}
    328      1.22     rmind 	if (mit == NULL) {
    329      1.23     rmind 		TAILQ_INSERT_TAIL(&mq->mq_head[MQ_PQRESQ], msg, msg_queue);
    330      1.22     rmind 	} else {
    331      1.22     rmind 		TAILQ_INSERT_BEFORE(mit, msg, msg_queue);
    332      1.22     rmind 	}
    333      1.22     rmind }
    334      1.22     rmind 
    335       1.1     rmind static int
    336      1.15  christos mq_stat_fop(file_t *fp, struct stat *st)
    337      1.15  christos {
    338      1.37      matt 	struct mqueue *mq = fp->f_mqueue;
    339      1.15  christos 
    340      1.22     rmind 	memset(st, 0, sizeof(*st));
    341      1.16  christos 
    342      1.16  christos 	mutex_enter(&mq->mq_mtx);
    343      1.15  christos 	st->st_mode = mq->mq_mode;
    344      1.15  christos 	st->st_uid = mq->mq_euid;
    345      1.15  christos 	st->st_gid = mq->mq_egid;
    346      1.15  christos 	st->st_atimespec = mq->mq_atime;
    347      1.15  christos 	st->st_mtimespec = mq->mq_mtime;
    348      1.15  christos 	st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;
    349      1.16  christos 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    350      1.16  christos 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    351      1.16  christos 	mutex_exit(&mq->mq_mtx);
    352      1.16  christos 
    353      1.15  christos 	return 0;
    354      1.15  christos }
    355      1.15  christos 
    356      1.15  christos static int
    357       1.8     rmind mq_poll_fop(file_t *fp, int events)
    358       1.8     rmind {
    359      1.37      matt 	struct mqueue *mq = fp->f_mqueue;
    360      1.22     rmind 	struct mq_attr *mqattr;
    361       1.8     rmind 	int revents = 0;
    362       1.8     rmind 
    363       1.8     rmind 	mutex_enter(&mq->mq_mtx);
    364      1.22     rmind 	mqattr = &mq->mq_attrib;
    365       1.8     rmind 	if (events & (POLLIN | POLLRDNORM)) {
    366      1.31     rmind 		/* Ready for receiving, if there are messages in the queue. */
    367      1.22     rmind 		if (mqattr->mq_curmsgs)
    368      1.31     rmind 			revents |= events & (POLLIN | POLLRDNORM);
    369       1.8     rmind 		else
    370       1.8     rmind 			selrecord(curlwp, &mq->mq_rsel);
    371       1.8     rmind 	}
    372       1.8     rmind 	if (events & (POLLOUT | POLLWRNORM)) {
    373      1.31     rmind 		/* Ready for sending, if the message queue is not full. */
    374      1.22     rmind 		if (mqattr->mq_curmsgs < mqattr->mq_maxmsg)
    375      1.31     rmind 			revents |= events & (POLLOUT | POLLWRNORM);
    376       1.8     rmind 		else
    377       1.8     rmind 			selrecord(curlwp, &mq->mq_wsel);
    378       1.8     rmind 	}
    379       1.8     rmind 	mutex_exit(&mq->mq_mtx);
    380       1.8     rmind 
    381       1.8     rmind 	return revents;
    382       1.8     rmind }
    383       1.8     rmind 
    384       1.8     rmind static int
    385       1.7        ad mq_close_fop(file_t *fp)
    386       1.1     rmind {
    387      1.32     rmind 	proc_t *p = curproc;
    388      1.37      matt 	mqueue_t *mq = fp->f_mqueue;
    389      1.32     rmind 	bool destroy = false;
    390       1.1     rmind 
    391       1.1     rmind 	mutex_enter(&mq->mq_mtx);
    392      1.32     rmind 	KASSERT(mq->mq_refcnt > 0);
    393      1.32     rmind 	if (--mq->mq_refcnt == 0) {
    394      1.32     rmind 		/* Destroy if the last reference and unlinked. */
    395      1.32     rmind 		destroy = (mq->mq_attrib.mq_flags & MQ_UNLINKED) != 0;
    396      1.32     rmind 	}
    397       1.1     rmind 	mutex_exit(&mq->mq_mtx);
    398       1.1     rmind 
    399      1.32     rmind 	if (destroy) {
    400       1.1     rmind 		mqueue_destroy(mq);
    401      1.32     rmind 	}
    402      1.32     rmind 	atomic_dec_uint(&p->p_mqueue_cnt);
    403       1.1     rmind 	return 0;
    404       1.1     rmind }
    405       1.1     rmind 
    406      1.20      elad static int
    407      1.32     rmind mqueue_access(mqueue_t *mq, int access, kauth_cred_t cred)
    408      1.20      elad {
    409      1.32     rmind 	mode_t acc_mode = 0;
    410      1.22     rmind 
    411      1.32     rmind 	/* Note the difference between VREAD/VWRITE and FREAD/FWRITE. */
    412      1.32     rmind 	if (access & FREAD) {
    413      1.32     rmind 		acc_mode |= VREAD;
    414      1.32     rmind 	}
    415      1.32     rmind 	if (access & FWRITE) {
    416      1.32     rmind 		acc_mode |= VWRITE;
    417      1.32     rmind 	}
    418      1.20      elad 	if (genfs_can_access(VNON, mq->mq_mode, mq->mq_euid,
    419      1.32     rmind 	    mq->mq_egid, acc_mode, cred)) {
    420      1.20      elad 		return EACCES;
    421      1.20      elad 	}
    422      1.32     rmind 	return 0;
    423      1.32     rmind }
    424      1.20      elad 
    425      1.32     rmind static int
    426      1.38    martin mqueue_create(lwp_t *l, char *name, struct mq_attr *attr, mode_t mode,
    427      1.32     rmind     int oflag, mqueue_t **mqret)
    428      1.32     rmind {
    429      1.32     rmind 	proc_t *p = l->l_proc;
    430      1.32     rmind 	struct cwdinfo *cwdi = p->p_cwdi;
    431      1.32     rmind 	mqueue_t *mq;
    432      1.32     rmind 	u_int i;
    433      1.32     rmind 
    434      1.32     rmind 	/* Empty name is invalid. */
    435      1.32     rmind 	if (name[0] == '\0') {
    436      1.32     rmind 		return EINVAL;
    437      1.32     rmind 	}
    438      1.32     rmind 
    439      1.32     rmind 	/* Check for mqueue attributes. */
    440      1.38    martin 	if (attr) {
    441      1.38    martin 		if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > mq_max_maxmsg ||
    442      1.38    martin 		    attr->mq_msgsize <= 0 ||
    443      1.38    martin 		    attr->mq_msgsize > mq_max_msgsize) {
    444      1.32     rmind 			return EINVAL;
    445      1.32     rmind 		}
    446      1.38    martin 		attr->mq_curmsgs = 0;
    447      1.32     rmind 	}
    448      1.32     rmind 
    449      1.32     rmind 	/*
    450      1.32     rmind 	 * Allocate new message queue, initialize data structures, copy the
    451      1.32     rmind 	 * name attributes.  Note that the initial reference is set here.
    452      1.32     rmind 	 */
    453      1.32     rmind 	mq = kmem_zalloc(sizeof(mqueue_t), KM_SLEEP);
    454      1.32     rmind 
    455      1.32     rmind 	mutex_init(&mq->mq_mtx, MUTEX_DEFAULT, IPL_NONE);
    456      1.32     rmind 	cv_init(&mq->mq_send_cv, "mqsendcv");
    457      1.32     rmind 	cv_init(&mq->mq_recv_cv, "mqrecvcv");
    458      1.32     rmind 	for (i = 0; i < (MQ_PQSIZE + 1); i++) {
    459      1.32     rmind 		TAILQ_INIT(&mq->mq_head[i]);
    460      1.32     rmind 	}
    461      1.32     rmind 	selinit(&mq->mq_rsel);
    462      1.32     rmind 	selinit(&mq->mq_wsel);
    463      1.32     rmind 	mq->mq_name = name;
    464      1.32     rmind 	mq->mq_refcnt = 1;
    465      1.32     rmind 
    466      1.38    martin 	if (attr != NULL) {
    467      1.38    martin 		memcpy(&mq->mq_attrib, attr, sizeof(struct mq_attr));
    468      1.38    martin 	} else {
    469      1.38    martin 		memset(&mq->mq_attrib, 0, sizeof(struct mq_attr));
    470      1.38    martin 		mq->mq_attrib.mq_maxmsg = mq_def_maxmsg;
    471      1.38    martin 		mq->mq_attrib.mq_msgsize = MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
    472      1.38    martin 	}
    473      1.32     rmind 
    474      1.32     rmind 	CTASSERT((O_MASK & (MQ_UNLINKED | MQ_RECEIVE)) == 0);
    475      1.32     rmind 	mq->mq_attrib.mq_flags = (O_MASK & oflag);
    476      1.32     rmind 
    477      1.32     rmind 	/* Store mode and effective UID with GID. */
    478      1.32     rmind 	mq->mq_mode = ((mode & ~cwdi->cwdi_cmask) & ALLPERMS) & ~S_ISTXT;
    479      1.32     rmind 	mq->mq_euid = kauth_cred_geteuid(l->l_cred);
    480      1.32     rmind 	mq->mq_egid = kauth_cred_getegid(l->l_cred);
    481      1.32     rmind 
    482      1.32     rmind 	*mqret = mq;
    483      1.20      elad 	return 0;
    484      1.20      elad }
    485      1.20      elad 
    486       1.1     rmind /*
    487      1.38    martin  * Helper function for mq_open() - note that "u_name" is a userland pointer,
    488      1.38    martin  * while "attr" is a kernel pointer!
    489       1.1     rmind  */
    490       1.1     rmind int
    491      1.38    martin mq_handle_open(struct lwp *l, const char *u_name, int oflag, mode_t mode,
    492      1.38    martin     struct mq_attr *attr, register_t *retval)
    493       1.1     rmind {
    494       1.1     rmind 	struct proc *p = l->l_proc;
    495       1.1     rmind 	struct mqueue *mq, *mq_new = NULL;
    496      1.38    martin 	int mqd, error;
    497       1.7        ad 	file_t *fp;
    498       1.1     rmind 	char *name;
    499       1.1     rmind 
    500      1.32     rmind 	/* Get the name from the user-space. */
    501      1.32     rmind 	name = kmem_alloc(MQ_NAMELEN, KM_SLEEP);
    502      1.38    martin 	error = copyinstr(u_name, name, MQ_NAMELEN - 1, NULL);
    503       1.1     rmind 	if (error) {
    504       1.1     rmind 		kmem_free(name, MQ_NAMELEN);
    505       1.1     rmind 		return error;
    506       1.1     rmind 	}
    507       1.1     rmind 
    508      1.32     rmind 	/* Allocate file structure and descriptor. */
    509       1.7        ad 	error = fd_allocfile(&fp, &mqd);
    510       1.1     rmind 	if (error) {
    511       1.1     rmind 		kmem_free(name, MQ_NAMELEN);
    512       1.1     rmind 		return error;
    513       1.1     rmind 	}
    514  1.40.4.1  christos 
    515  1.40.4.1  christos 	/* Account and check for the limit. */
    516  1.40.4.1  christos 	if (atomic_inc_uint_nv(&p->p_mqueue_cnt) > mq_open_max) {
    517  1.40.4.1  christos 		atomic_dec_uint(&p->p_mqueue_cnt);
    518  1.40.4.1  christos 		error = EMFILE;
    519  1.40.4.1  christos 		goto err;
    520  1.40.4.1  christos 	}
    521  1.40.4.1  christos 
    522       1.1     rmind 	fp->f_type = DTYPE_MQUEUE;
    523      1.11     rmind 	fp->f_flag = FFLAGS(oflag) & (FREAD | FWRITE);
    524       1.1     rmind 	fp->f_ops = &mqops;
    525       1.1     rmind 
    526      1.32     rmind 	if (oflag & O_CREAT) {
    527      1.32     rmind 		/* Create a new message queue. */
    528      1.38    martin 		error = mqueue_create(l, name, attr, mode, oflag, &mq_new);
    529      1.32     rmind 		if (error) {
    530      1.32     rmind 			goto err;
    531      1.32     rmind 		}
    532      1.32     rmind 		KASSERT(mq_new != NULL);
    533      1.32     rmind 	}
    534      1.32     rmind 
    535      1.32     rmind 	/* Lookup for a message queue with such name. */
    536      1.32     rmind 	mutex_enter(&mqlist_lock);
    537       1.1     rmind 	mq = mqueue_lookup(name);
    538       1.1     rmind 	if (mq) {
    539      1.12     rmind 		KASSERT(mutex_owned(&mq->mq_mtx));
    540      1.32     rmind 		mutex_exit(&mqlist_lock);
    541      1.11     rmind 
    542      1.32     rmind 		/* Check for exclusive create. */
    543      1.32     rmind 		if (oflag & O_EXCL) {
    544      1.32     rmind 			mutex_exit(&mq->mq_mtx);
    545       1.1     rmind 			error = EEXIST;
    546      1.32     rmind 			goto err;
    547       1.1     rmind 		}
    548      1.18     rmind 
    549      1.32     rmind 		/* Verify permissions. */
    550      1.32     rmind 		if (mqueue_access(mq, fp->f_flag, l->l_cred) != 0) {
    551      1.32     rmind 			mutex_exit(&mq->mq_mtx);
    552       1.1     rmind 			error = EACCES;
    553      1.32     rmind 			goto err;
    554       1.1     rmind 		}
    555      1.32     rmind 
    556      1.32     rmind 		/* If we have the access, add a new reference. */
    557      1.32     rmind 		mq->mq_refcnt++;
    558      1.32     rmind 		mutex_exit(&mq->mq_mtx);
    559       1.1     rmind 	} else {
    560      1.32     rmind 		/* Fail if not found and not creating. */
    561       1.1     rmind 		if ((oflag & O_CREAT) == 0) {
    562      1.32     rmind 			mutex_exit(&mqlist_lock);
    563       1.1     rmind 			KASSERT(mq_new == NULL);
    564      1.32     rmind 			error = ENOENT;
    565      1.32     rmind 			goto err;
    566       1.1     rmind 		}
    567       1.1     rmind 
    568      1.32     rmind 		/* Initial timestamps. */
    569       1.1     rmind 		mq = mq_new;
    570      1.32     rmind 		getnanotime(&mq->mq_btime);
    571      1.32     rmind 		mq->mq_atime = mq->mq_mtime = mq->mq_btime;
    572      1.32     rmind 
    573      1.32     rmind 		/*
    574      1.32     rmind 		 * Finally, insert message queue into the list.
    575      1.32     rmind 		 * Note: it already has the initial reference.
    576      1.32     rmind 		 */
    577       1.1     rmind 		LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
    578      1.32     rmind 		mutex_exit(&mqlist_lock);
    579      1.32     rmind 
    580       1.1     rmind 		mq_new = NULL;
    581      1.32     rmind 		name = NULL;
    582       1.1     rmind 	}
    583      1.32     rmind 	KASSERT(mq != NULL);
    584      1.37      matt 	fp->f_mqueue = mq;
    585      1.32     rmind 	fd_affix(p, fp, mqd);
    586      1.32     rmind 	*retval = mqd;
    587      1.32     rmind err:
    588       1.1     rmind 	if (error) {
    589       1.7        ad 		fd_abort(p, fp, mqd);
    590       1.7        ad 	}
    591      1.32     rmind 	if (mq_new) {
    592      1.32     rmind 		/* Note: will free the 'name'. */
    593      1.32     rmind 		mqueue_destroy(mq_new);
    594      1.32     rmind 	} else if (name) {
    595      1.32     rmind 		kmem_free(name, MQ_NAMELEN);
    596      1.32     rmind 	}
    597       1.1     rmind 	return error;
    598       1.1     rmind }
    599       1.1     rmind 
    600      1.38    martin /*
    601      1.38    martin  * General mqueue system calls.
    602      1.38    martin  */
    603      1.38    martin 
    604      1.38    martin int
    605      1.38    martin sys_mq_open(struct lwp *l, const struct sys_mq_open_args *uap,
    606      1.38    martin     register_t *retval)
    607      1.38    martin {
    608      1.38    martin 	/* {
    609      1.38    martin 		syscallarg(const char *) name;
    610      1.38    martin 		syscallarg(int) oflag;
    611      1.38    martin 		syscallarg(mode_t) mode;
    612      1.38    martin 		syscallarg(struct mq_attr) attr;
    613      1.38    martin 	} */
    614      1.38    martin 	struct mq_attr *attr = NULL, a;
    615      1.38    martin 	int error;
    616      1.38    martin 
    617      1.39  christos 	if ((SCARG(uap, oflag) & O_CREAT) != 0 && SCARG(uap, attr) != NULL) {
    618      1.39  christos 		error = copyin(SCARG(uap, attr), &a, sizeof(a));
    619      1.38    martin 		if (error)
    620      1.38    martin 			return error;
    621      1.38    martin 		attr = &a;
    622      1.38    martin 	}
    623      1.38    martin 
    624      1.38    martin 	return mq_handle_open(l, SCARG(uap, name), SCARG(uap, oflag),
    625      1.38    martin 	    SCARG(uap, mode), attr, retval);
    626      1.38    martin }
    627      1.38    martin 
    628       1.1     rmind int
    629       1.8     rmind sys_mq_close(struct lwp *l, const struct sys_mq_close_args *uap,
    630       1.8     rmind     register_t *retval)
    631       1.1     rmind {
    632       1.1     rmind 
    633       1.6       dsl 	return sys_close(l, (const void *)uap, retval);
    634       1.1     rmind }
    635       1.1     rmind 
    636       1.1     rmind /*
    637      1.25     rmind  * Primary mq_recv1() function.
    638       1.1     rmind  */
    639      1.13  christos int
    640      1.25     rmind mq_recv1(mqd_t mqdes, void *msg_ptr, size_t msg_len, u_int *msg_prio,
    641      1.25     rmind     struct timespec *ts, ssize_t *mlen)
    642       1.1     rmind {
    643       1.1     rmind 	struct mqueue *mq;
    644       1.1     rmind 	struct mq_msg *msg = NULL;
    645      1.22     rmind 	struct mq_attr *mqattr;
    646      1.23     rmind 	u_int idx;
    647       1.1     rmind 	int error;
    648       1.1     rmind 
    649      1.32     rmind 	error = mqueue_get(mqdes, FREAD, &mq);
    650      1.21     rmind 	if (error) {
    651       1.1     rmind 		return error;
    652      1.21     rmind 	}
    653      1.21     rmind 	getnanotime(&mq->mq_atime);
    654      1.22     rmind 	mqattr = &mq->mq_attrib;
    655       1.1     rmind 
    656       1.1     rmind 	/* Check the message size limits */
    657      1.22     rmind 	if (msg_len < mqattr->mq_msgsize) {
    658       1.1     rmind 		error = EMSGSIZE;
    659       1.1     rmind 		goto error;
    660       1.1     rmind 	}
    661       1.1     rmind 
    662       1.1     rmind 	/* Check if queue is empty */
    663      1.22     rmind 	while (mqattr->mq_curmsgs == 0) {
    664      1.25     rmind 		int t;
    665      1.25     rmind 
    666      1.22     rmind 		if (mqattr->mq_flags & O_NONBLOCK) {
    667       1.1     rmind 			error = EAGAIN;
    668       1.1     rmind 			goto error;
    669       1.1     rmind 		}
    670      1.28  drochner 		if (ts) {
    671      1.35  christos 			error = ts2timo(CLOCK_REALTIME, TIMER_ABSTIME, ts, &t,
    672      1.35  christos 			    NULL);
    673      1.28  drochner 			if (error)
    674      1.28  drochner 				goto error;
    675      1.28  drochner 		} else
    676      1.28  drochner 			t = 0;
    677       1.1     rmind 		/*
    678       1.1     rmind 		 * Block until someone sends the message.
    679       1.1     rmind 		 * While doing this, notification should not be sent.
    680       1.1     rmind 		 */
    681      1.22     rmind 		mqattr->mq_flags |= MQ_RECEIVE;
    682       1.1     rmind 		error = cv_timedwait_sig(&mq->mq_send_cv, &mq->mq_mtx, t);
    683      1.22     rmind 		mqattr->mq_flags &= ~MQ_RECEIVE;
    684      1.32     rmind 		if (error || (mqattr->mq_flags & MQ_UNLINKED)) {
    685       1.3     rmind 			error = (error == EWOULDBLOCK) ? ETIMEDOUT : EINTR;
    686       1.1     rmind 			goto error;
    687       1.1     rmind 		}
    688       1.1     rmind 	}
    689       1.1     rmind 
    690      1.23     rmind 	/*
    691      1.23     rmind 	 * Find the highest priority message, and remove it from the queue.
    692      1.23     rmind 	 * At first, reserved queue is checked, bitmap is next.
    693      1.23     rmind 	 */
    694      1.23     rmind 	msg = TAILQ_FIRST(&mq->mq_head[MQ_PQRESQ]);
    695      1.23     rmind 	if (__predict_true(msg == NULL)) {
    696      1.23     rmind 		idx = ffs(mq->mq_bitmap);
    697      1.23     rmind 		msg = TAILQ_FIRST(&mq->mq_head[idx]);
    698      1.23     rmind 		KASSERT(msg != NULL);
    699      1.23     rmind 	} else {
    700      1.23     rmind 		idx = MQ_PQRESQ;
    701      1.23     rmind 	}
    702      1.23     rmind 	TAILQ_REMOVE(&mq->mq_head[idx], msg, msg_queue);
    703      1.23     rmind 
    704      1.23     rmind 	/* Unmark the bit, if last message. */
    705      1.23     rmind 	if (__predict_true(idx) && TAILQ_EMPTY(&mq->mq_head[idx])) {
    706      1.23     rmind 		KASSERT((MQ_PQSIZE - idx) == msg->msg_prio);
    707  1.40.4.1  christos 		mq->mq_bitmap &= ~(1U << --idx);
    708      1.22     rmind 	}
    709       1.1     rmind 
    710       1.1     rmind 	/* Decrement the counter and signal waiter, if any */
    711      1.22     rmind 	mqattr->mq_curmsgs--;
    712       1.1     rmind 	cv_signal(&mq->mq_recv_cv);
    713       1.8     rmind 
    714       1.8     rmind 	/* Ready for sending now */
    715       1.8     rmind 	selnotify(&mq->mq_wsel, POLLOUT | POLLWRNORM, 0);
    716       1.1     rmind error:
    717       1.1     rmind 	mutex_exit(&mq->mq_mtx);
    718       1.7        ad 	fd_putfile((int)mqdes);
    719       1.1     rmind 	if (error)
    720       1.1     rmind 		return error;
    721       1.1     rmind 
    722       1.1     rmind 	/*
    723       1.1     rmind 	 * Copy the data to the user-space.
    724       1.1     rmind 	 * Note: According to POSIX, no message should be removed from the
    725       1.1     rmind 	 * queue in case of fail - this would be violated.
    726       1.1     rmind 	 */
    727       1.1     rmind 	*mlen = msg->msg_len;
    728       1.1     rmind 	error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
    729       1.1     rmind 	if (error == 0 && msg_prio)
    730       1.1     rmind 		error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
    731       1.1     rmind 	mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
    732       1.1     rmind 
    733       1.1     rmind 	return error;
    734       1.1     rmind }
    735       1.1     rmind 
    736       1.1     rmind int
    737       1.8     rmind sys_mq_receive(struct lwp *l, const struct sys_mq_receive_args *uap,
    738       1.8     rmind     register_t *retval)
    739       1.1     rmind {
    740       1.6       dsl 	/* {
    741       1.1     rmind 		syscallarg(mqd_t) mqdes;
    742       1.1     rmind 		syscallarg(char *) msg_ptr;
    743       1.1     rmind 		syscallarg(size_t) msg_len;
    744       1.1     rmind 		syscallarg(unsigned *) msg_prio;
    745       1.6       dsl 	} */
    746      1.25     rmind 	ssize_t mlen;
    747       1.1     rmind 	int error;
    748       1.1     rmind 
    749      1.25     rmind 	error = mq_recv1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    750      1.25     rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), NULL, &mlen);
    751       1.1     rmind 	if (error == 0)
    752       1.1     rmind 		*retval = mlen;
    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.13  christos sys___mq_timedreceive50(struct lwp *l,
    759      1.13  christos     const struct sys___mq_timedreceive50_args *uap, register_t *retval)
    760       1.1     rmind {
    761       1.6       dsl 	/* {
    762       1.1     rmind 		syscallarg(mqd_t) mqdes;
    763       1.1     rmind 		syscallarg(char *) msg_ptr;
    764       1.1     rmind 		syscallarg(size_t) msg_len;
    765       1.1     rmind 		syscallarg(unsigned *) msg_prio;
    766       1.1     rmind 		syscallarg(const struct timespec *) abs_timeout;
    767       1.6       dsl 	} */
    768      1.25     rmind 	struct timespec ts, *tsp;
    769       1.1     rmind 	ssize_t mlen;
    770      1.25     rmind 	int error;
    771       1.1     rmind 
    772       1.1     rmind 	/* Get and convert time value */
    773       1.1     rmind 	if (SCARG(uap, abs_timeout)) {
    774      1.13  christos 		error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
    775      1.13  christos 		if (error)
    776      1.13  christos 			return error;
    777      1.25     rmind 		tsp = &ts;
    778      1.25     rmind 	} else {
    779      1.25     rmind 		tsp = NULL;
    780      1.25     rmind 	}
    781      1.13  christos 
    782      1.25     rmind 	error = mq_recv1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    783      1.25     rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
    784       1.1     rmind 	if (error == 0)
    785       1.1     rmind 		*retval = mlen;
    786       1.1     rmind 
    787       1.1     rmind 	return error;
    788       1.1     rmind }
    789       1.1     rmind 
    790       1.1     rmind /*
    791       1.1     rmind  * Primary mq_send1() function.
    792       1.1     rmind  */
    793      1.13  christos int
    794      1.25     rmind mq_send1(mqd_t mqdes, const char *msg_ptr, size_t msg_len, u_int msg_prio,
    795      1.25     rmind     struct timespec *ts)
    796       1.1     rmind {
    797       1.1     rmind 	struct mqueue *mq;
    798      1.22     rmind 	struct mq_msg *msg;
    799      1.22     rmind 	struct mq_attr *mqattr;
    800       1.1     rmind 	struct proc *notify = NULL;
    801       1.1     rmind 	ksiginfo_t ksi;
    802       1.1     rmind 	size_t size;
    803       1.1     rmind 	int error;
    804       1.1     rmind 
    805       1.1     rmind 	/* Check the priority range */
    806       1.1     rmind 	if (msg_prio >= mq_prio_max)
    807       1.1     rmind 		return EINVAL;
    808       1.1     rmind 
    809       1.1     rmind 	/* Allocate a new message */
    810  1.40.4.1  christos 	if (msg_len > mq_max_msgsize)
    811  1.40.4.1  christos 		return EMSGSIZE;
    812       1.1     rmind 	size = sizeof(struct mq_msg) + msg_len;
    813       1.1     rmind 	if (size > mq_max_msgsize)
    814       1.1     rmind 		return EMSGSIZE;
    815       1.1     rmind 
    816      1.22     rmind 	if (size > MQ_DEF_MSGSIZE) {
    817       1.1     rmind 		msg = kmem_alloc(size, KM_SLEEP);
    818      1.22     rmind 	} else {
    819       1.8     rmind 		msg = pool_cache_get(mqmsg_cache, PR_WAITOK);
    820      1.22     rmind 	}
    821       1.1     rmind 
    822       1.1     rmind 	/* Get the data from user-space */
    823       1.1     rmind 	error = copyin(msg_ptr, msg->msg_ptr, msg_len);
    824       1.1     rmind 	if (error) {
    825       1.1     rmind 		mqueue_freemsg(msg, size);
    826       1.1     rmind 		return error;
    827       1.1     rmind 	}
    828       1.1     rmind 	msg->msg_len = msg_len;
    829       1.1     rmind 	msg->msg_prio = msg_prio;
    830       1.1     rmind 
    831      1.32     rmind 	error = mqueue_get(mqdes, FWRITE, &mq);
    832       1.1     rmind 	if (error) {
    833       1.1     rmind 		mqueue_freemsg(msg, size);
    834       1.1     rmind 		return error;
    835       1.1     rmind 	}
    836      1.15  christos 	getnanotime(&mq->mq_mtime);
    837      1.22     rmind 	mqattr = &mq->mq_attrib;
    838      1.15  christos 
    839       1.1     rmind 	/* Check the message size limit */
    840      1.22     rmind 	if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
    841       1.1     rmind 		error = EMSGSIZE;
    842       1.1     rmind 		goto error;
    843       1.1     rmind 	}
    844       1.1     rmind 
    845       1.1     rmind 	/* Check if queue is full */
    846      1.22     rmind 	while (mqattr->mq_curmsgs >= mqattr->mq_maxmsg) {
    847      1.25     rmind 		int t;
    848      1.25     rmind 
    849      1.22     rmind 		if (mqattr->mq_flags & O_NONBLOCK) {
    850       1.1     rmind 			error = EAGAIN;
    851       1.1     rmind 			goto error;
    852       1.1     rmind 		}
    853      1.28  drochner 		if (ts) {
    854      1.35  christos 			error = ts2timo(CLOCK_REALTIME, TIMER_ABSTIME, ts, &t,
    855      1.35  christos 			    NULL);
    856      1.28  drochner 			if (error)
    857      1.28  drochner 				goto error;
    858      1.28  drochner 		} else
    859      1.28  drochner 			t = 0;
    860       1.1     rmind 		/* Block until queue becomes available */
    861       1.1     rmind 		error = cv_timedwait_sig(&mq->mq_recv_cv, &mq->mq_mtx, t);
    862      1.32     rmind 		if (error || (mqattr->mq_flags & MQ_UNLINKED)) {
    863       1.1     rmind 			error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
    864       1.1     rmind 			goto error;
    865       1.1     rmind 		}
    866       1.1     rmind 	}
    867      1.22     rmind 	KASSERT(mqattr->mq_curmsgs < mqattr->mq_maxmsg);
    868       1.1     rmind 
    869      1.23     rmind 	/*
    870      1.23     rmind 	 * Insert message into the queue, according to the priority.
    871      1.23     rmind 	 * Note the difference between index and priority.
    872      1.23     rmind 	 */
    873      1.22     rmind 	if (__predict_true(msg_prio < MQ_PQSIZE)) {
    874      1.23     rmind 		u_int idx = MQ_PQSIZE - msg_prio;
    875      1.23     rmind 
    876      1.23     rmind 		KASSERT(idx != MQ_PQRESQ);
    877      1.23     rmind 		TAILQ_INSERT_TAIL(&mq->mq_head[idx], msg, msg_queue);
    878  1.40.4.1  christos 		mq->mq_bitmap |= (1U << --idx);
    879      1.22     rmind 	} else {
    880      1.22     rmind 		mqueue_linear_insert(mq, msg);
    881      1.22     rmind 	}
    882       1.1     rmind 
    883       1.1     rmind 	/* Check for the notify */
    884      1.22     rmind 	if (mqattr->mq_curmsgs == 0 && mq->mq_notify_proc &&
    885      1.28  drochner 	    (mqattr->mq_flags & MQ_RECEIVE) == 0 &&
    886      1.28  drochner 	    mq->mq_sig_notify.sigev_notify == SIGEV_SIGNAL) {
    887       1.1     rmind 		/* Initialize the signal */
    888       1.1     rmind 		KSI_INIT(&ksi);
    889       1.1     rmind 		ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;
    890       1.1     rmind 		ksi.ksi_code = SI_MESGQ;
    891       1.1     rmind 		ksi.ksi_value = mq->mq_sig_notify.sigev_value;
    892       1.1     rmind 		/* Unregister the process */
    893       1.1     rmind 		notify = mq->mq_notify_proc;
    894       1.1     rmind 		mq->mq_notify_proc = NULL;
    895       1.1     rmind 	}
    896       1.1     rmind 
    897       1.1     rmind 	/* Increment the counter and signal waiter, if any */
    898      1.22     rmind 	mqattr->mq_curmsgs++;
    899       1.1     rmind 	cv_signal(&mq->mq_send_cv);
    900       1.8     rmind 
    901       1.8     rmind 	/* Ready for receiving now */
    902       1.8     rmind 	selnotify(&mq->mq_rsel, POLLIN | POLLRDNORM, 0);
    903       1.1     rmind error:
    904       1.1     rmind 	mutex_exit(&mq->mq_mtx);
    905       1.7        ad 	fd_putfile((int)mqdes);
    906       1.1     rmind 
    907       1.1     rmind 	if (error) {
    908       1.1     rmind 		mqueue_freemsg(msg, size);
    909       1.1     rmind 	} else if (notify) {
    910       1.1     rmind 		/* Send the notify, if needed */
    911      1.10        ad 		mutex_enter(proc_lock);
    912       1.1     rmind 		kpsignal(notify, &ksi, NULL);
    913      1.10        ad 		mutex_exit(proc_lock);
    914       1.1     rmind 	}
    915       1.1     rmind 	return error;
    916       1.1     rmind }
    917       1.1     rmind 
    918       1.1     rmind int
    919       1.8     rmind sys_mq_send(struct lwp *l, const struct sys_mq_send_args *uap,
    920       1.8     rmind     register_t *retval)
    921       1.1     rmind {
    922       1.6       dsl 	/* {
    923       1.1     rmind 		syscallarg(mqd_t) mqdes;
    924       1.1     rmind 		syscallarg(const char *) msg_ptr;
    925       1.1     rmind 		syscallarg(size_t) msg_len;
    926       1.1     rmind 		syscallarg(unsigned) msg_prio;
    927       1.6       dsl 	} */
    928       1.1     rmind 
    929      1.25     rmind 	return mq_send1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    930      1.25     rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), NULL);
    931       1.1     rmind }
    932       1.1     rmind 
    933       1.1     rmind int
    934      1.13  christos sys___mq_timedsend50(struct lwp *l, const struct sys___mq_timedsend50_args *uap,
    935       1.8     rmind     register_t *retval)
    936       1.1     rmind {
    937       1.6       dsl 	/* {
    938       1.1     rmind 		syscallarg(mqd_t) mqdes;
    939       1.1     rmind 		syscallarg(const char *) msg_ptr;
    940       1.1     rmind 		syscallarg(size_t) msg_len;
    941       1.1     rmind 		syscallarg(unsigned) msg_prio;
    942       1.1     rmind 		syscallarg(const struct timespec *) abs_timeout;
    943       1.6       dsl 	} */
    944      1.25     rmind 	struct timespec ts, *tsp;
    945      1.13  christos 	int error;
    946       1.1     rmind 
    947       1.1     rmind 	/* Get and convert time value */
    948       1.1     rmind 	if (SCARG(uap, abs_timeout)) {
    949      1.13  christos 		error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
    950      1.13  christos 		if (error)
    951      1.13  christos 			return error;
    952      1.25     rmind 		tsp = &ts;
    953      1.25     rmind 	} else {
    954      1.25     rmind 		tsp = NULL;
    955      1.25     rmind 	}
    956       1.1     rmind 
    957      1.25     rmind 	return mq_send1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
    958      1.25     rmind 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
    959       1.1     rmind }
    960       1.1     rmind 
    961       1.1     rmind int
    962       1.8     rmind sys_mq_notify(struct lwp *l, const struct sys_mq_notify_args *uap,
    963       1.8     rmind     register_t *retval)
    964       1.1     rmind {
    965       1.6       dsl 	/* {
    966       1.1     rmind 		syscallarg(mqd_t) mqdes;
    967       1.1     rmind 		syscallarg(const struct sigevent *) notification;
    968       1.6       dsl 	} */
    969       1.1     rmind 	struct mqueue *mq;
    970       1.1     rmind 	struct sigevent sig;
    971       1.1     rmind 	int error;
    972       1.1     rmind 
    973       1.1     rmind 	if (SCARG(uap, notification)) {
    974       1.1     rmind 		/* Get the signal from user-space */
    975       1.1     rmind 		error = copyin(SCARG(uap, notification), &sig,
    976       1.1     rmind 		    sizeof(struct sigevent));
    977       1.1     rmind 		if (error)
    978       1.1     rmind 			return error;
    979      1.28  drochner 		if (sig.sigev_notify == SIGEV_SIGNAL &&
    980      1.28  drochner 		    (sig.sigev_signo <=0 || sig.sigev_signo >= NSIG))
    981      1.28  drochner 			return EINVAL;
    982       1.1     rmind 	}
    983       1.1     rmind 
    984      1.32     rmind 	error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
    985      1.32     rmind 	if (error) {
    986       1.1     rmind 		return error;
    987      1.32     rmind 	}
    988       1.1     rmind 	if (SCARG(uap, notification)) {
    989       1.1     rmind 		/* Register notification: set the signal and target process */
    990       1.1     rmind 		if (mq->mq_notify_proc == NULL) {
    991       1.1     rmind 			memcpy(&mq->mq_sig_notify, &sig,
    992       1.1     rmind 			    sizeof(struct sigevent));
    993       1.1     rmind 			mq->mq_notify_proc = l->l_proc;
    994       1.1     rmind 		} else {
    995       1.1     rmind 			/* Fail if someone else already registered */
    996       1.1     rmind 			error = EBUSY;
    997       1.1     rmind 		}
    998       1.1     rmind 	} else {
    999       1.1     rmind 		/* Unregister the notification */
   1000       1.1     rmind 		mq->mq_notify_proc = NULL;
   1001       1.1     rmind 	}
   1002       1.1     rmind 	mutex_exit(&mq->mq_mtx);
   1003       1.7        ad 	fd_putfile((int)SCARG(uap, mqdes));
   1004       1.1     rmind 
   1005       1.1     rmind 	return error;
   1006       1.1     rmind }
   1007       1.1     rmind 
   1008       1.1     rmind int
   1009       1.8     rmind sys_mq_getattr(struct lwp *l, const struct sys_mq_getattr_args *uap,
   1010       1.8     rmind     register_t *retval)
   1011       1.1     rmind {
   1012       1.6       dsl 	/* {
   1013       1.1     rmind 		syscallarg(mqd_t) mqdes;
   1014       1.1     rmind 		syscallarg(struct mq_attr *) mqstat;
   1015       1.6       dsl 	} */
   1016       1.1     rmind 	struct mqueue *mq;
   1017       1.1     rmind 	struct mq_attr attr;
   1018       1.1     rmind 	int error;
   1019       1.1     rmind 
   1020      1.32     rmind 	error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
   1021      1.32     rmind 	if (error) {
   1022       1.1     rmind 		return error;
   1023      1.32     rmind 	}
   1024       1.1     rmind 	memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
   1025       1.1     rmind 	mutex_exit(&mq->mq_mtx);
   1026       1.7        ad 	fd_putfile((int)SCARG(uap, mqdes));
   1027       1.1     rmind 
   1028       1.1     rmind 	return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
   1029       1.1     rmind }
   1030       1.1     rmind 
   1031       1.1     rmind int
   1032       1.8     rmind sys_mq_setattr(struct lwp *l, const struct sys_mq_setattr_args *uap,
   1033       1.8     rmind     register_t *retval)
   1034       1.1     rmind {
   1035       1.6       dsl 	/* {
   1036       1.1     rmind 		syscallarg(mqd_t) mqdes;
   1037       1.1     rmind 		syscallarg(const struct mq_attr *) mqstat;
   1038       1.1     rmind 		syscallarg(struct mq_attr *) omqstat;
   1039       1.6       dsl 	} */
   1040       1.1     rmind 	struct mqueue *mq;
   1041       1.1     rmind 	struct mq_attr attr;
   1042       1.1     rmind 	int error, nonblock;
   1043       1.1     rmind 
   1044       1.1     rmind 	error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
   1045       1.1     rmind 	if (error)
   1046       1.1     rmind 		return error;
   1047       1.1     rmind 	nonblock = (attr.mq_flags & O_NONBLOCK);
   1048       1.1     rmind 
   1049      1.32     rmind 	error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
   1050      1.32     rmind 	if (error) {
   1051       1.1     rmind 		return error;
   1052      1.32     rmind 	}
   1053       1.1     rmind 
   1054       1.1     rmind 	/* Copy the old attributes, if needed */
   1055      1.22     rmind 	if (SCARG(uap, omqstat)) {
   1056       1.1     rmind 		memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
   1057      1.22     rmind 	}
   1058       1.1     rmind 
   1059       1.1     rmind 	/* Ignore everything, except O_NONBLOCK */
   1060       1.1     rmind 	if (nonblock)
   1061       1.1     rmind 		mq->mq_attrib.mq_flags |= O_NONBLOCK;
   1062       1.1     rmind 	else
   1063       1.1     rmind 		mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
   1064       1.1     rmind 
   1065       1.1     rmind 	mutex_exit(&mq->mq_mtx);
   1066       1.7        ad 	fd_putfile((int)SCARG(uap, mqdes));
   1067       1.1     rmind 
   1068       1.1     rmind 	/*
   1069       1.1     rmind 	 * Copy the data to the user-space.
   1070       1.1     rmind 	 * Note: According to POSIX, the new attributes should not be set in
   1071       1.1     rmind 	 * case of fail - this would be violated.
   1072       1.1     rmind 	 */
   1073       1.1     rmind 	if (SCARG(uap, omqstat))
   1074       1.1     rmind 		error = copyout(&attr, SCARG(uap, omqstat),
   1075       1.1     rmind 		    sizeof(struct mq_attr));
   1076       1.1     rmind 
   1077       1.1     rmind 	return error;
   1078       1.1     rmind }
   1079       1.1     rmind 
   1080       1.1     rmind int
   1081       1.8     rmind sys_mq_unlink(struct lwp *l, const struct sys_mq_unlink_args *uap,
   1082       1.8     rmind     register_t *retval)
   1083       1.1     rmind {
   1084       1.6       dsl 	/* {
   1085       1.1     rmind 		syscallarg(const char *) name;
   1086       1.6       dsl 	} */
   1087      1.32     rmind 	mqueue_t *mq;
   1088       1.1     rmind 	char *name;
   1089       1.1     rmind 	int error, refcnt = 0;
   1090       1.1     rmind 
   1091       1.1     rmind 	/* Get the name from the user-space */
   1092      1.32     rmind 	name = kmem_alloc(MQ_NAMELEN, KM_SLEEP);
   1093       1.1     rmind 	error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
   1094       1.1     rmind 	if (error) {
   1095       1.1     rmind 		kmem_free(name, MQ_NAMELEN);
   1096       1.1     rmind 		return error;
   1097       1.1     rmind 	}
   1098       1.1     rmind 
   1099      1.32     rmind 	mutex_enter(&mqlist_lock);
   1100       1.1     rmind 	mq = mqueue_lookup(name);
   1101       1.1     rmind 	if (mq == NULL) {
   1102       1.1     rmind 		error = ENOENT;
   1103      1.32     rmind 		goto err;
   1104       1.1     rmind 	}
   1105      1.32     rmind 	KASSERT(mutex_owned(&mq->mq_mtx));
   1106       1.1     rmind 
   1107      1.32     rmind 	/* Verify permissions. */
   1108      1.34      elad 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MQUEUE, 0, mq,
   1109      1.34      elad 	    NULL, NULL)) {
   1110       1.1     rmind 		mutex_exit(&mq->mq_mtx);
   1111       1.1     rmind 		error = EACCES;
   1112      1.32     rmind 		goto err;
   1113       1.1     rmind 	}
   1114       1.1     rmind 
   1115      1.32     rmind 	/* Remove and destroy if no references. */
   1116      1.32     rmind 	LIST_REMOVE(mq, mq_list);
   1117      1.32     rmind 	refcnt = mq->mq_refcnt;
   1118      1.32     rmind 	if (refcnt) {
   1119      1.32     rmind 		/* Mark as unlinked, if there are references. */
   1120      1.32     rmind 		mq->mq_attrib.mq_flags |= MQ_UNLINKED;
   1121      1.32     rmind 	}
   1122       1.1     rmind 
   1123      1.32     rmind 	/* Wake up waiters, if there are any. */
   1124       1.1     rmind 	cv_broadcast(&mq->mq_send_cv);
   1125       1.1     rmind 	cv_broadcast(&mq->mq_recv_cv);
   1126       1.1     rmind 
   1127       1.8     rmind 	selnotify(&mq->mq_rsel, POLLHUP, 0);
   1128       1.8     rmind 	selnotify(&mq->mq_wsel, POLLHUP, 0);
   1129       1.8     rmind 
   1130       1.1     rmind 	mutex_exit(&mq->mq_mtx);
   1131      1.32     rmind err:
   1132      1.32     rmind 	mutex_exit(&mqlist_lock);
   1133       1.1     rmind 	/*
   1134      1.32     rmind 	 * If last reference - destroy the message queue.  Otherwise,
   1135      1.32     rmind 	 * the last mq_close() call will do that.
   1136       1.1     rmind 	 */
   1137      1.32     rmind 	if (!error && refcnt == 0) {
   1138       1.1     rmind 		mqueue_destroy(mq);
   1139      1.32     rmind 	}
   1140      1.32     rmind 	kmem_free(name, MQ_NAMELEN);
   1141       1.1     rmind 
   1142       1.1     rmind 	return error;
   1143       1.1     rmind }
   1144       1.1     rmind 
   1145       1.1     rmind /*
   1146      1.22     rmind  * System control nodes.
   1147       1.1     rmind  */
   1148      1.30    jruoho static int
   1149      1.30    jruoho mqueue_sysctl_init(void)
   1150       1.1     rmind {
   1151       1.1     rmind 	const struct sysctlnode *node = NULL;
   1152       1.1     rmind 
   1153      1.30    jruoho 	mqsysctl_log = NULL;
   1154      1.30    jruoho 
   1155      1.30    jruoho 	sysctl_createv(&mqsysctl_log, 0, NULL, NULL,
   1156       1.1     rmind 		CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
   1157       1.1     rmind 		CTLTYPE_INT, "posix_msg",
   1158       1.1     rmind 		SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
   1159       1.1     rmind 			     "Message Passing option to which the "
   1160       1.1     rmind 			     "system attempts to conform"),
   1161       1.1     rmind 		NULL, _POSIX_MESSAGE_PASSING, NULL, 0,
   1162       1.1     rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
   1163      1.30    jruoho 	sysctl_createv(&mqsysctl_log, 0, NULL, &node,
   1164       1.1     rmind 		CTLFLAG_PERMANENT,
   1165       1.1     rmind 		CTLTYPE_NODE, "mqueue",
   1166       1.1     rmind 		SYSCTL_DESCR("Message queue options"),
   1167       1.1     rmind 		NULL, 0, NULL, 0,
   1168       1.1     rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
   1169       1.1     rmind 
   1170       1.1     rmind 	if (node == NULL)
   1171      1.30    jruoho 		return ENXIO;
   1172       1.1     rmind 
   1173      1.30    jruoho 	sysctl_createv(&mqsysctl_log, 0, &node, NULL,
   1174       1.1     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1175       1.1     rmind 		CTLTYPE_INT, "mq_open_max",
   1176       1.1     rmind 		SYSCTL_DESCR("Maximal number of message queue descriptors "
   1177       1.1     rmind 			     "that process could open"),
   1178       1.1     rmind 		NULL, 0, &mq_open_max, 0,
   1179       1.1     rmind 		CTL_CREATE, CTL_EOL);
   1180      1.30    jruoho 	sysctl_createv(&mqsysctl_log, 0, &node, NULL,
   1181       1.1     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1182       1.1     rmind 		CTLTYPE_INT, "mq_prio_max",
   1183       1.1     rmind 		SYSCTL_DESCR("Maximal priority of the message"),
   1184       1.1     rmind 		NULL, 0, &mq_prio_max, 0,
   1185       1.1     rmind 		CTL_CREATE, CTL_EOL);
   1186      1.30    jruoho 	sysctl_createv(&mqsysctl_log, 0, &node, NULL,
   1187       1.1     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1188       1.1     rmind 		CTLTYPE_INT, "mq_max_msgsize",
   1189       1.1     rmind 		SYSCTL_DESCR("Maximal allowed size of the message"),
   1190       1.1     rmind 		NULL, 0, &mq_max_msgsize, 0,
   1191       1.1     rmind 		CTL_CREATE, CTL_EOL);
   1192      1.30    jruoho 	sysctl_createv(&mqsysctl_log, 0, &node, NULL,
   1193       1.1     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1194       1.1     rmind 		CTLTYPE_INT, "mq_def_maxmsg",
   1195       1.1     rmind 		SYSCTL_DESCR("Default maximal message count"),
   1196       1.1     rmind 		NULL, 0, &mq_def_maxmsg, 0,
   1197       1.1     rmind 		CTL_CREATE, CTL_EOL);
   1198      1.30    jruoho 	sysctl_createv(&mqsysctl_log, 0, &node, NULL,
   1199      1.28  drochner 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1200      1.28  drochner 		CTLTYPE_INT, "mq_max_maxmsg",
   1201      1.28  drochner 		SYSCTL_DESCR("Maximal allowed message count"),
   1202      1.28  drochner 		NULL, 0, &mq_max_maxmsg, 0,
   1203      1.28  drochner 		CTL_CREATE, CTL_EOL);
   1204      1.30    jruoho 
   1205      1.30    jruoho 	return 0;
   1206       1.1     rmind }
   1207       1.1     rmind 
   1208       1.1     rmind /*
   1209       1.1     rmind  * Debugging.
   1210       1.1     rmind  */
   1211       1.1     rmind #if defined(DDB)
   1212       1.1     rmind 
   1213       1.1     rmind void
   1214       1.1     rmind mqueue_print_list(void (*pr)(const char *, ...))
   1215       1.1     rmind {
   1216       1.1     rmind 	struct mqueue *mq;
   1217       1.1     rmind 
   1218       1.1     rmind 	(*pr)("Global list of the message queues:\n");
   1219       1.1     rmind 	(*pr)("%20s %10s %8s %8s %3s %4s %4s %4s\n",
   1220       1.1     rmind 	    "Name", "Ptr", "Mode", "Flags",  "Ref",
   1221       1.1     rmind 	    "MaxMsg", "MsgSze", "CurMsg");
   1222       1.1     rmind 	LIST_FOREACH(mq, &mqueue_head, mq_list) {
   1223       1.1     rmind 		(*pr)("%20s %10p %8x %8x %3u %6lu %6lu %6lu\n",
   1224       1.1     rmind 		    mq->mq_name, mq, mq->mq_mode,
   1225       1.1     rmind 		    mq->mq_attrib.mq_flags, mq->mq_refcnt,
   1226       1.1     rmind 		    mq->mq_attrib.mq_maxmsg, mq->mq_attrib.mq_msgsize,
   1227       1.1     rmind 		    mq->mq_attrib.mq_curmsgs);
   1228       1.1     rmind 	}
   1229       1.1     rmind }
   1230       1.1     rmind 
   1231       1.1     rmind #endif /* defined(DDB) */
   1232