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