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