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