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