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