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