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