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