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