1 /* $NetBSD: sys_mqueue.c,v 1.16 2009/04/11 23:05:26 christos Exp $ */
4 * Copyright (c) 2007, 2008 Mindaugas Rasiukevicius <rmind at NetBSD org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Implementation of POSIX message queues.
31 * Defined in the Base Definitions volume of IEEE Std 1003.1-2001.
35 * Global list of message queues (mqueue_head) and proc_t::p_mqueue_cnt
36 * counter are protected by mqlist_mtx lock. The very message queue and
37 * its members are protected by mqueue::mq_mtx.
44 #include <sys/cdefs.h>
45 /*__KERNEL_RCSID(0, "$NetBSD: sys_mqueue.c,v 1.16 2009/04/11 23:05:26 christos Exp $");*/
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/errno.h>
51 #include <sys/fcntl.h>
53 #include <sys/filedesc.h>
54 #include <sys/ucred.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/mqueue.h>
59 #include <sys/objcache.h>
62 #include <sys/queue.h>
63 #include <sys/select.h>
64 #include <sys/serialize.h>
65 #include <sys/signal.h>
66 #include <sys/signalvar.h>
67 #include <sys/spinlock.h>
68 #include <sys/spinlock2.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysproto.h>
72 #include <sys/systm.h>
74 #include <sys/unistd.h>
75 #include <sys/vnode.h>
77 /* System-wide limits. */
78 static u_int mq_open_max = MQ_OPEN_MAX;
79 static u_int mq_prio_max = MQ_PRIO_MAX;
80 static u_int mq_max_msgsize = 16 * MQ_DEF_MSGSIZE;
81 static u_int mq_def_maxmsg = 32;
83 struct lock mqlist_mtx;
84 static struct objcache * mqmsg_cache;
85 static LIST_HEAD(, mqueue) mqueue_head =
86 LIST_HEAD_INITIALIZER(mqueue_head);
88 typedef struct file file_t; /* XXX: Should we put this in sys/types.h ? */
90 /* Function prototypes */
91 static int mq_poll_fop(file_t *, int, struct ucred *cred);
92 static int mq_stat_fop(file_t *, struct stat *, struct ucred *cred);
93 static int mq_close_fop(file_t *);
95 /* Some time-related utility functions */
96 static int itimespecfix(struct timespec *ts);
97 static int tstohz(const struct timespec *ts);
99 /* File operations vector */
100 static struct fileops mqops = {
101 .fo_read = badfo_readwrite,
102 .fo_write = badfo_readwrite,
103 .fo_ioctl = badfo_ioctl,
104 .fo_poll = mq_poll_fop,
105 .fo_stat = mq_stat_fop,
106 .fo_close = mq_close_fop,
107 .fo_kqfilter = badfo_kqfilter,
108 .fo_shutdown = badfo_shutdown
111 /* Define a new malloc type for message queues */
112 MALLOC_DECLARE(M_MQBUF);
113 MALLOC_DEFINE(M_MQBUF, "mqueues", "Buffers to message queues");
115 /* Malloc arguments for object cache */
116 struct objcache_malloc_args mqueue_malloc_args = {
117 sizeof(struct mqueue), M_MQBUF };
119 /* Spinlock around the process list */
120 extern struct spinlock allproc_spin;
123 * Initialize POSIX message queue subsystem.
128 mqmsg_cache = objcache_create("mqmsg_cache",
129 0, /* infinite depot's capacity */
130 0, /* default magazine's capacity */
131 NULL, /* constructor */
132 NULL, /* deconstructor */
134 objcache_malloc_alloc,
135 objcache_malloc_free,
136 &mqueue_malloc_args);
138 lockinit(&mqlist_mtx, "mqlist_mtx", 0, LK_CANRECURSE);
145 mqueue_freemsg(struct mq_msg *msg, const size_t size)
148 if (size > MQ_DEF_MSGSIZE)
151 objcache_put(mqmsg_cache, msg);
155 * Destroy the message queue.
158 mqueue_destroy(struct mqueue *mq)
164 /* Note MQ_PQSIZE + 1. */
165 for (i = 0; i < MQ_PQSIZE + 1; i++) {
166 while ((msg = TAILQ_FIRST(&mq->mq_head[i])) != NULL) {
167 TAILQ_REMOVE(&mq->mq_head[i], msg, msg_queue);
168 msz = sizeof(struct mq_msg) + msg->msg_len;
169 mqueue_freemsg(msg, msz);
172 lockuninit(&mq->mq_mtx);
177 * Lookup for file name in general list of message queues.
178 * => locks the message queue
181 mqueue_lookup(char *name)
185 KKASSERT(lockstatus(&mqlist_mtx, curthread));
187 LIST_FOREACH(mq, &mqueue_head, mq_list) {
188 if (strncmp(mq->mq_name, name, MQ_NAMELEN) == 0) {
189 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
198 * mqueue_get: get the mqueue from the descriptor.
199 * => locks the message queue, if found.
200 * => hold a reference on the file descriptor.
203 mqueue_get(struct lwp *l, mqd_t mqd, file_t **fpr)
208 fp = holdfp(curproc->p_fd, (int)mqd, -1); /* XXX: Why -1 ? */
209 if (__predict_false(fp == NULL))
212 if (__predict_false(fp->f_type != DTYPE_MQUEUE)) {
217 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
224 * mqueue_linear_insert: perform linear insert according to the message
225 * priority into the reserved queue (MQ_PQRESQ). Reserved queue is a
226 * sorted list used only when mq_prio_max is increased via sysctl.
229 mqueue_linear_insert(struct mqueue *mq, struct mq_msg *msg)
233 TAILQ_FOREACH(mit, &mq->mq_head[MQ_PQRESQ], msg_queue) {
234 if (msg->msg_prio > mit->msg_prio)
238 TAILQ_INSERT_TAIL(&mq->mq_head[MQ_PQRESQ], msg, msg_queue);
240 TAILQ_INSERT_BEFORE(mit, msg, msg_queue);
248 itimespecfix(struct timespec *ts)
250 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
252 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
253 ts->tv_nsec = tick * 1000;
258 * Compute number of ticks in the specified amount of time.
261 tstohz(const struct timespec *ts)
266 * usec has great enough resolution for hz, so convert to a
267 * timeval and use tvtohz() above.
269 TIMESPEC_TO_TIMEVAL(&tv, ts);
270 return tvtohz_high(&tv); /* XXX Why _high() and not _low() ? */
274 * Converter from struct timespec to the ticks.
275 * Used by mq_timedreceive(), mq_timedsend().
278 abstimeout2timo(struct timespec *ts, int *timo)
284 timespecsub(ts, &tsd);
285 if (ts->tv_sec < 0 || (ts->tv_sec == 0 && ts->tv_nsec <= 0)) {
288 error = itimespecfix(ts);
293 KKASSERT(*timo != 0);
299 mq_stat_fop(file_t *fp, struct stat *st, struct ucred *cred)
301 struct mqueue *mq = fp->f_data;
303 (void)memset(st, 0, sizeof(*st));
305 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
306 st->st_mode = mq->mq_mode;
307 st->st_uid = mq->mq_euid;
308 st->st_gid = mq->mq_egid;
309 st->st_atimespec = mq->mq_atime;
310 st->st_mtimespec = mq->mq_mtime;
311 /*st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;*/
312 st->st_uid = fp->f_cred->cr_uid;
313 st->st_gid = fp->f_cred->cr_svgid;
314 lockmgr(&mq->mq_mtx, LK_RELEASE);
320 mq_poll_fop(file_t *fp, int events, struct ucred *cred)
322 struct mqueue *mq = fp->f_data;
325 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
326 if (events & (POLLIN | POLLRDNORM)) {
327 /* Ready for receiving, if there are messages in the queue */
328 if (mq->mq_attrib.mq_curmsgs)
329 revents |= (POLLIN | POLLRDNORM);
331 selrecord(curthread, &mq->mq_rsel);
333 if (events & (POLLOUT | POLLWRNORM)) {
334 /* Ready for sending, if the message queue is not full */
335 if (mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg)
336 revents |= (POLLOUT | POLLWRNORM);
338 selrecord(curthread, &mq->mq_wsel);
340 lockmgr(&mq->mq_mtx, LK_RELEASE);
346 mq_close_fop(file_t *fp)
348 struct proc *p = curproc;
349 struct mqueue *mq = fp->f_data;
352 lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
353 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
355 /* Decrease the counters */
359 /* Remove notification if registered for this process */
360 if (mq->mq_notify_proc == p)
361 mq->mq_notify_proc = NULL;
364 * If this is the last reference and mqueue is marked for unlink,
365 * remove and later destroy the message queue.
367 if (mq->mq_refcnt == 0 && (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
368 LIST_REMOVE(mq, mq_list);
373 lockmgr(&mq->mq_mtx, LK_RELEASE);
374 lockmgr(&mqlist_mtx, LK_RELEASE);
383 * General mqueue system calls.
387 sys_mq_open(struct mq_open_args *uap)
390 syscallarg(const char *) name;
391 syscallarg(int) oflag;
392 syscallarg(mode_t) mode;
393 syscallarg(struct mq_attr) attr;
395 struct proc *p = curproc;
396 struct mqueue *mq, *mq_new = NULL;
399 int mqd, error, oflag;
401 /* Check access mode flags */
402 oflag = SCARG(uap, oflag);
403 if ((oflag & O_ACCMODE) == (O_WRONLY | O_RDWR)) {
407 /* Get the name from the user-space */
408 name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
409 error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
411 kfree(name, M_MQBUF);
415 if (oflag & O_CREAT) {
419 /* Check the limit */
420 if (p->p_mqueue_cnt == mq_open_max) {
421 kfree(name, M_MQBUF);
425 /* Empty name is invalid */
426 if (name[0] == '\0') {
427 kfree(name, M_MQBUF);
431 /* Check for mqueue attributes */
432 if (SCARG(uap, attr)) {
433 error = copyin(SCARG(uap, attr), &attr,
434 sizeof(struct mq_attr));
436 kfree(name, M_MQBUF);
439 if (attr.mq_maxmsg <= 0 || attr.mq_msgsize <= 0 ||
440 attr.mq_msgsize > mq_max_msgsize) {
441 kfree(name, M_MQBUF);
446 memset(&attr, 0, sizeof(struct mq_attr));
447 attr.mq_maxmsg = mq_def_maxmsg;
449 MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
453 * Allocate new mqueue, initialize data structures,
454 * copy the name, attributes and set the flag.
456 mq_new = kmalloc(sizeof(struct mqueue), M_MQBUF, M_WAITOK | M_ZERO);
458 lockinit(&mq_new->mq_mtx, "mq_new->mq_mtx", 0, LK_CANRECURSE);
459 for (i = 0; i < (MQ_PQSIZE + 1); i++) {
460 TAILQ_INIT(&mq_new->mq_head[i]);
463 strlcpy(mq_new->mq_name, name, MQ_NAMELEN);
464 memcpy(&mq_new->mq_attrib, &attr, sizeof(struct mq_attr));
466 /*CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);*/
467 /* mq_new->mq_attrib.mq_flags = (O_MASK & oflag); */
468 mq_new->mq_attrib.mq_flags = oflag;
470 /* Store mode and effective UID with GID */
471 mq_new->mq_mode = ((SCARG(uap, mode) &
472 ~p->p_fd->fd_cmask) & ALLPERMS) & ~S_ISTXT;
473 mq_new->mq_euid = curproc->p_ucred->cr_uid;
474 mq_new->mq_egid = curproc->p_ucred->cr_svgid;
477 /* Allocate file structure and descriptor */
478 error = falloc(curproc, &fp, &mqd);
481 mqueue_destroy(mq_new);
482 kfree(name, M_MQBUF);
485 fp->f_type = DTYPE_MQUEUE;
486 fp->f_flag = FFLAGS(oflag) & (FREAD | FWRITE);
489 /* Look up for mqueue with such name */
490 lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
491 mq = mqueue_lookup(name);
495 KKASSERT(lockstatus(&mq->mq_mtx, curthread));
497 /* Check if mqueue is not marked as unlinking */
498 if (mq->mq_attrib.mq_flags & MQ_UNLINK) {
502 /* Fail if O_EXCL is set, and mqueue already exists */
503 if ((oflag & O_CREAT) && (oflag & O_EXCL)) {
509 * Check the permissions. Note the difference between
510 * VREAD/VWRITE and FREAD/FWRITE.
513 if (fp->f_flag & FREAD) {
516 if (fp->f_flag & FWRITE) {
519 if (vaccess(VNON, mq->mq_mode, mq->mq_euid, mq->mq_egid,
520 acc_mode, curproc->p_ucred)) {
526 /* Fail if mqueue neither exists, nor we create it */
527 if ((oflag & O_CREAT) == 0) {
528 lockmgr(&mqlist_mtx, LK_RELEASE);
529 KKASSERT(mq_new == NULL);
530 fsetfd(curproc, NULL, mqd);
531 fp->f_ops = &badfileops;
533 kfree(name, M_MQBUF);
537 /* Check the limit */
538 if (p->p_mqueue_cnt == mq_open_max) {
543 /* Insert the queue to the list */
545 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
546 LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
548 getnanotime(&mq->mq_btime);
549 mq->mq_atime = mq->mq_mtime = mq->mq_btime;
552 /* Increase the counters, and make descriptor ready */
557 lockmgr(&mq->mq_mtx, LK_RELEASE);
558 lockmgr(&mqlist_mtx, LK_RELEASE);
561 mqueue_destroy(mq_new);
563 fsetfd(curproc, NULL, mqd);
564 fp->f_ops = &badfileops;
567 uap->sysmsg_result = mqd;
570 kfree(name, M_MQBUF);
576 sys_mq_close(struct mq_close_args *uap)
578 return sys_close((void *)uap);
582 * Primary mq_receive1() function.
585 mq_receive1(struct lwp *l, mqd_t mqdes, void *msg_ptr, size_t msg_len,
586 unsigned *msg_prio, struct timespec *ts, ssize_t *mlen)
590 struct mq_msg *msg = NULL;
591 struct mq_attr *mqattr;
595 /* Get the message queue */
596 error = mqueue_get(l, mqdes, &fp);
601 if ((fp->f_flag & FREAD) == 0) {
605 getnanotime(&mq->mq_atime);
606 mqattr = &mq->mq_attrib;
608 /* Check the message size limits */
609 if (msg_len < mqattr->mq_msgsize) {
614 /* Check if queue is empty */
615 while (mqattr->mq_curmsgs == 0) {
618 if (mqattr->mq_flags & O_NONBLOCK) {
622 error = abstimeout2timo(ts, &t);
627 * Block until someone sends the message.
628 * While doing this, notification should not be sent.
630 mqattr->mq_flags |= MQ_RECEIVE;
631 error = tsleep(&mq->mq_send_cv, PCATCH, "mqsend", t);
632 mqattr->mq_flags &= ~MQ_RECEIVE;
633 if (error || (mqattr->mq_flags & MQ_UNLINK)) {
634 error = (error == EWOULDBLOCK) ? ETIMEDOUT : EINTR;
641 * Find the highest priority message, and remove it from the queue.
642 * At first, reserved queue is checked, bitmap is next.
644 msg = TAILQ_FIRST(&mq->mq_head[MQ_PQRESQ]);
645 if (__predict_true(msg == NULL)) {
646 idx = ffs(mq->mq_bitmap);
647 msg = TAILQ_FIRST(&mq->mq_head[idx]);
648 KKASSERT(msg != NULL);
652 TAILQ_REMOVE(&mq->mq_head[idx], msg, msg_queue);
654 /* Unmark the bit, if last message. */
655 if (__predict_true(idx) && TAILQ_EMPTY(&mq->mq_head[idx])) {
656 KKASSERT((MQ_PQSIZE - idx) == msg->msg_prio);
657 mq->mq_bitmap &= ~(1 << --idx);
660 /* Decrement the counter and signal waiter, if any */
661 mqattr->mq_curmsgs--;
662 wakeup_one(&mq->mq_recv_cv);
664 /* Ready for sending now */
665 selwakeup(&mq->mq_wsel);
667 lockmgr(&mq->mq_mtx, LK_RELEASE);
673 * Copy the data to the user-space.
674 * Note: According to POSIX, no message should be removed from the
675 * queue in case of fail - this would be violated.
677 *mlen = msg->msg_len;
678 error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
679 if (error == 0 && msg_prio)
680 error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
681 mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
687 sys_mq_receive(struct mq_receive_args *uap)
690 syscallarg(mqd_t) mqdes;
691 syscallarg(char *) msg_ptr;
692 syscallarg(size_t) msg_len;
693 syscallarg(unsigned *) msg_prio;
698 error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
699 SCARG(uap, msg_len), SCARG(uap, msg_prio), 0, &mlen);
701 uap->sysmsg_result = mlen;
707 sys_mq_timedreceive(struct mq_timedreceive_args *uap)
710 syscallarg(mqd_t) mqdes;
711 syscallarg(char *) msg_ptr;
712 syscallarg(size_t) msg_len;
713 syscallarg(unsigned *) msg_prio;
714 syscallarg(const struct timespec *) abs_timeout;
718 struct timespec ts, *tsp;
720 /* Get and convert time value */
721 if (SCARG(uap, abs_timeout)) {
722 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
730 error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
731 SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
733 uap->sysmsg_result = mlen;
739 * Primary mq_send1() function.
742 mq_send1(struct lwp *l, mqd_t mqdes, const char *msg_ptr, size_t msg_len,
743 unsigned msg_prio, struct timespec *ts)
748 struct mq_attr *mqattr;
749 struct proc *notify = NULL;
754 /* Check the priority range */
755 if (msg_prio >= mq_prio_max)
758 /* Allocate a new message */
759 size = sizeof(struct mq_msg) + msg_len;
760 if (size > mq_max_msgsize)
763 if (size > MQ_DEF_MSGSIZE) {
764 msg = kmalloc(size, M_MQBUF, M_WAITOK);
766 msg = objcache_get(mqmsg_cache, M_WAITOK);
769 /* Get the data from user-space */
770 error = copyin(msg_ptr, msg->msg_ptr, msg_len);
772 mqueue_freemsg(msg, size);
775 msg->msg_len = msg_len;
776 msg->msg_prio = msg_prio;
779 error = mqueue_get(l, mqdes, &fp);
781 mqueue_freemsg(msg, size);
785 if ((fp->f_flag & FWRITE) == 0) {
789 getnanotime(&mq->mq_mtime);
790 mqattr = &mq->mq_attrib;
792 /* Check the message size limit */
793 if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
798 /* Check if queue is full */
799 while (mqattr->mq_curmsgs >= mqattr->mq_maxmsg) {
802 if (mqattr->mq_flags & O_NONBLOCK) {
806 error = abstimeout2timo(ts, &t);
810 /* Block until queue becomes available */
811 error = tsleep(&mq->mq_recv_cv, PCATCH, "mqrecv", t);
812 if (error || (mqattr->mq_flags & MQ_UNLINK)) {
813 error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
817 KKASSERT(mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg);
820 * Insert message into the queue, according to the priority.
821 * Note the difference between index and priority.
823 if (__predict_true(msg_prio < MQ_PQSIZE)) {
824 u_int idx = MQ_PQSIZE - msg_prio;
826 KKASSERT(idx != MQ_PQRESQ);
827 TAILQ_INSERT_TAIL(&mq->mq_head[idx], msg, msg_queue);
828 mq->mq_bitmap |= (1 << --idx);
830 mqueue_linear_insert(mq, msg);
833 /* Check for the notify */
834 if (mqattr->mq_curmsgs == 0 && mq->mq_notify_proc &&
835 (mqattr->mq_flags & MQ_RECEIVE) == 0) {
836 /* Initialize the signal */
838 /*ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;*/
839 /*ksi.ksi_code = SI_MESGQ;*/
840 /*ksi.ksi_value = mq->mq_sig_notify.sigev_value;*/
841 /* Unregister the process */
842 notify = mq->mq_notify_proc;
843 mq->mq_notify_proc = NULL;
846 /* Increment the counter and signal waiter, if any */
847 mqattr->mq_curmsgs++;
848 wakeup_one(&mq->mq_send_cv);
850 /* Ready for receiving now */
851 selwakeup(&mq->mq_rsel);
853 lockmgr(&mq->mq_mtx, LK_RELEASE);
857 mqueue_freemsg(msg, size);
859 /* Send the notify, if needed */
860 spin_lock_wr(&allproc_spin);
861 /*kpsignal(notify, &ksi, NULL);*/
862 ksignal(notify, mq->mq_sig_notify.sigev_signo);
863 spin_unlock_wr(&allproc_spin);
870 sys_mq_send(struct mq_send_args *uap)
873 syscallarg(mqd_t) mqdes;
874 syscallarg(const char *) msg_ptr;
875 syscallarg(size_t) msg_len;
876 syscallarg(unsigned) msg_prio;
879 return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
880 SCARG(uap, msg_len), SCARG(uap, msg_prio), 0);
884 sys_mq_timedsend(struct mq_timedsend_args *uap)
887 syscallarg(mqd_t) mqdes;
888 syscallarg(const char *) msg_ptr;
889 syscallarg(size_t) msg_len;
890 syscallarg(unsigned) msg_prio;
891 syscallarg(const struct timespec *) abs_timeout;
893 struct timespec ts, *tsp;
896 /* Get and convert time value */
897 if (SCARG(uap, abs_timeout)) {
898 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
906 return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
907 SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
911 sys_mq_notify(struct mq_notify_args *uap)
914 syscallarg(mqd_t) mqdes;
915 syscallarg(const struct sigevent *) notification;
922 if (SCARG(uap, notification)) {
923 /* Get the signal from user-space */
924 error = copyin(SCARG(uap, notification), &sig,
925 sizeof(struct sigevent));
930 error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
935 if (SCARG(uap, notification)) {
936 /* Register notification: set the signal and target process */
937 if (mq->mq_notify_proc == NULL) {
938 memcpy(&mq->mq_sig_notify, &sig,
939 sizeof(struct sigevent));
940 mq->mq_notify_proc = curproc;
942 /* Fail if someone else already registered */
946 /* Unregister the notification */
947 mq->mq_notify_proc = NULL;
949 lockmgr(&mq->mq_mtx, LK_RELEASE);
956 sys_mq_getattr(struct mq_getattr_args *uap)
959 syscallarg(mqd_t) mqdes;
960 syscallarg(struct mq_attr *) mqstat;
967 /* Get the message queue */
968 error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
972 memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
973 lockmgr(&mq->mq_mtx, LK_RELEASE);
976 return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
980 sys_mq_setattr(struct mq_setattr_args *uap)
983 syscallarg(mqd_t) mqdes;
984 syscallarg(const struct mq_attr *) mqstat;
985 syscallarg(struct mq_attr *) omqstat;
992 error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
995 nonblock = (attr.mq_flags & O_NONBLOCK);
997 /* Get the message queue */
998 error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
1003 /* Copy the old attributes, if needed */
1004 if (SCARG(uap, omqstat))
1005 memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
1007 /* Ignore everything, except O_NONBLOCK */
1009 mq->mq_attrib.mq_flags |= O_NONBLOCK;
1011 mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
1013 lockmgr(&mq->mq_mtx, LK_RELEASE);
1017 * Copy the data to the user-space.
1018 * Note: According to POSIX, the new attributes should not be set in
1019 * case of fail - this would be violated.
1021 if (SCARG(uap, omqstat))
1022 error = copyout(&attr, SCARG(uap, omqstat),
1023 sizeof(struct mq_attr));
1029 sys_mq_unlink(struct mq_unlink_args *uap)
1032 syscallarg(const char *) name;
1036 int error, refcnt = 0;
1038 /* Get the name from the user-space */
1039 name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
1040 error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
1042 kfree(name, M_MQBUF);
1046 /* Lookup for this file */
1047 lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
1048 mq = mqueue_lookup(name);
1054 /* Check the permissions */
1055 if (curproc->p_ucred->cr_uid != mq->mq_euid &&
1056 priv_check(curthread, PRIV_ROOT) != 0) {
1057 lockmgr(&mq->mq_mtx, LK_RELEASE);
1062 /* Mark message queue as unlinking, before leaving the window */
1063 mq->mq_attrib.mq_flags |= MQ_UNLINK;
1065 /* Wake up all waiters, if there are such */
1066 wakeup(&mq->mq_send_cv);
1067 wakeup(&mq->mq_recv_cv);
1069 selwakeup(&mq->mq_rsel);
1070 selwakeup(&mq->mq_wsel);
1072 refcnt = mq->mq_refcnt;
1074 LIST_REMOVE(mq, mq_list);
1076 lockmgr(&mq->mq_mtx, LK_RELEASE);
1078 lockmgr(&mqlist_mtx, LK_RELEASE);
1081 * If there are no references - destroy the message
1082 * queue, otherwise, the last mq_close() will do that.
1084 if (error == 0 && refcnt == 0)
1087 kfree(name, M_MQBUF);
1094 SYSCTL_NODE(_kern, OID_AUTO, mqueue,
1095 CTLFLAG_RW, 0, "Message queue options");
1097 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_open_max,
1098 CTLFLAG_RW, &mq_open_max, 0,
1099 "Maximal number of message queue descriptors per process");
1101 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_prio_max,
1102 CTLFLAG_RW, &mq_prio_max, 0,
1103 "Maximal priority of the message");
1105 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_max_msgsize,
1106 CTLFLAG_RW, &mq_max_msgsize, 0,
1107 "Maximal allowed size of the message");
1109 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_def_maxmsg,
1110 CTLFLAG_RW, &mq_def_maxmsg, 0,
1111 "Default maximal message count");
1113 SYSINIT(sys_mqueue_init, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, mqueue_sysinit, NULL);