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.
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/errno.h>
48 #include <sys/fcntl.h>
50 #include <sys/filedesc.h>
51 #include <sys/ucred.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mplock2.h>
56 #include <sys/mqueue.h>
57 #include <sys/objcache.h>
59 #include <sys/queue.h>
60 #include <sys/select.h>
61 #include <sys/serialize.h>
62 #include <sys/signal.h>
63 #include <sys/signalvar.h>
64 #include <sys/spinlock.h>
65 #include <sys/spinlock2.h>
67 #include <sys/sysctl.h>
68 #include <sys/sysproto.h>
69 #include <sys/systm.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
74 /* System-wide limits. */
75 static u_int mq_open_max = MQ_OPEN_MAX;
76 static u_int mq_prio_max = MQ_PRIO_MAX;
77 static u_int mq_max_msgsize = 16 * MQ_DEF_MSGSIZE;
78 static u_int mq_def_maxmsg = 32;
79 static u_int mq_max_maxmsg = 16 * 32;
81 struct lock mqlist_mtx;
82 static struct objcache * mqmsg_cache;
83 static LIST_HEAD(, mqueue) mqueue_head =
84 LIST_HEAD_INITIALIZER(mqueue_head);
86 typedef struct file file_t; /* XXX: Should we put this in sys/types.h ? */
88 /* Function prototypes */
89 static int mq_stat_fop(file_t *, struct stat *, struct ucred *cred);
90 static int mq_close_fop(file_t *);
92 /* Some time-related utility functions */
93 static int itimespecfix(struct timespec *ts);
94 static int tstohz(const struct timespec *ts);
96 /* File operations vector */
97 static struct fileops mqops = {
98 .fo_read = badfo_readwrite,
99 .fo_write = badfo_readwrite,
100 .fo_ioctl = badfo_ioctl,
101 .fo_stat = mq_stat_fop,
102 .fo_close = mq_close_fop,
103 .fo_kqfilter = badfo_kqfilter,
104 .fo_shutdown = badfo_shutdown
107 /* Define a new malloc type for message queues */
108 MALLOC_DECLARE(M_MQBUF);
109 MALLOC_DEFINE(M_MQBUF, "mqueues", "Buffers to message queues");
111 /* Malloc arguments for object cache */
112 struct objcache_malloc_args mqueue_malloc_args = {
113 sizeof(struct mqueue), M_MQBUF };
116 * Initialize POSIX message queue subsystem.
121 mqmsg_cache = objcache_create("mqmsg_cache",
122 0, /* infinite depot's capacity */
123 0, /* default magazine's capacity */
124 NULL, /* constructor */
125 NULL, /* deconstructor */
127 objcache_malloc_alloc,
128 objcache_malloc_free,
129 &mqueue_malloc_args);
131 lockinit(&mqlist_mtx, "mqlist_mtx", 0, LK_CANRECURSE);
138 mqueue_freemsg(struct mq_msg *msg, const size_t size)
141 if (size > MQ_DEF_MSGSIZE) {
144 objcache_put(mqmsg_cache, msg);
149 * Destroy the message queue.
152 mqueue_destroy(struct mqueue *mq)
158 /* Note MQ_PQSIZE + 1. */
159 for (i = 0; i < MQ_PQSIZE + 1; i++) {
160 while ((msg = TAILQ_FIRST(&mq->mq_head[i])) != NULL) {
161 TAILQ_REMOVE(&mq->mq_head[i], msg, msg_queue);
162 msz = sizeof(struct mq_msg) + msg->msg_len;
163 mqueue_freemsg(msg, msz);
166 lockuninit(&mq->mq_mtx);
171 * Lookup for file name in general list of message queues.
172 * => locks the message queue
175 mqueue_lookup(char *name)
179 KKASSERT(lockstatus(&mqlist_mtx, curthread));
181 LIST_FOREACH(mq, &mqueue_head, mq_list) {
182 if (strncmp(mq->mq_name, name, MQ_NAMELEN) == 0) {
183 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
192 * mqueue_get: get the mqueue from the descriptor.
193 * => locks the message queue, if found.
194 * => holds a reference on the file descriptor.
197 mqueue_get(struct lwp *l, mqd_t mqd, file_t **fpr)
202 fp = holdfp(curproc->p_fd, (int)mqd, -1); /* XXX: Why -1 ? */
203 if (__predict_false(fp == NULL))
206 if (__predict_false(fp->f_type != DTYPE_MQUEUE)) {
211 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
218 * mqueue_linear_insert: perform linear insert according to the message
219 * priority into the reserved queue (MQ_PQRESQ). Reserved queue is a
220 * sorted list used only when mq_prio_max is increased via sysctl.
223 mqueue_linear_insert(struct mqueue *mq, struct mq_msg *msg)
227 TAILQ_FOREACH(mit, &mq->mq_head[MQ_PQRESQ], msg_queue) {
228 if (msg->msg_prio > mit->msg_prio)
232 TAILQ_INSERT_TAIL(&mq->mq_head[MQ_PQRESQ], msg, msg_queue);
234 TAILQ_INSERT_BEFORE(mit, msg, msg_queue);
242 itimespecfix(struct timespec *ts)
244 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
246 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < nstick)
247 ts->tv_nsec = nstick;
252 * Compute number of ticks in the specified amount of time.
255 tstohz(const struct timespec *ts)
260 * usec has great enough resolution for hz, so convert to a
261 * timeval and use tvtohz() above.
263 TIMESPEC_TO_TIMEVAL(&tv, ts);
264 return tvtohz_high(&tv); /* XXX Why _high() and not _low() ? */
268 * Converter from struct timespec to the ticks.
269 * Used by mq_timedreceive(), mq_timedsend().
272 abstimeout2timo(struct timespec *ts, int *timo)
277 error = itimespecfix(ts);
282 timespecsub(ts, &tsd);
283 if (ts->tv_sec < 0 || (ts->tv_sec == 0 && ts->tv_nsec <= 0)) {
287 KKASSERT(*timo != 0);
293 mq_stat_fop(file_t *fp, struct stat *st, struct ucred *cred)
295 struct mqueue *mq = fp->f_data;
297 (void)memset(st, 0, sizeof(*st));
299 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
300 st->st_mode = mq->mq_mode;
301 st->st_uid = mq->mq_euid;
302 st->st_gid = mq->mq_egid;
303 st->st_atimespec = mq->mq_atime;
304 st->st_mtimespec = mq->mq_mtime;
305 /*st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;*/
306 st->st_uid = fp->f_cred->cr_uid;
307 st->st_gid = fp->f_cred->cr_svgid;
308 lockmgr(&mq->mq_mtx, LK_RELEASE);
314 mq_close_fop(file_t *fp)
316 struct proc *p = curproc;
317 struct mqueue *mq = fp->f_data;
320 lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
321 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
323 /* Decrease the counters */
327 /* Remove notification if registered for this process */
328 if (mq->mq_notify_proc == p)
329 mq->mq_notify_proc = NULL;
332 * If this is the last reference and mqueue is marked for unlink,
333 * remove and later destroy the message queue.
335 if (mq->mq_refcnt == 0 && (mq->mq_attrib.mq_flags & MQ_UNLINK)) {
336 LIST_REMOVE(mq, mq_list);
341 lockmgr(&mq->mq_mtx, LK_RELEASE);
342 lockmgr(&mqlist_mtx, LK_RELEASE);
351 * General mqueue system calls.
355 sys_mq_open(struct mq_open_args *uap)
358 syscallarg(const char *) name;
359 syscallarg(int) oflag;
360 syscallarg(mode_t) mode;
361 syscallarg(struct mq_attr) attr;
363 struct thread *td = curthread;
364 struct proc *p = td->td_proc;
365 struct filedesc *fdp = p->p_fd;
366 struct mqueue *mq, *mq_new = NULL;
369 int mqd, error, oflag;
371 /* Check access mode flags */
372 oflag = SCARG(uap, oflag);
373 if ((oflag & O_ACCMODE) == (O_WRONLY | O_RDWR)) {
377 /* Get the name from the user-space */
378 name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
379 error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
381 kfree(name, M_MQBUF);
385 if (oflag & O_CREAT) {
389 /* Check the limit */
390 if (p->p_mqueue_cnt == mq_open_max) {
391 kfree(name, M_MQBUF);
395 /* Empty name is invalid */
396 if (name[0] == '\0') {
397 kfree(name, M_MQBUF);
401 /* Check for mqueue attributes */
402 if (SCARG(uap, attr)) {
403 error = copyin(SCARG(uap, attr), &attr,
404 sizeof(struct mq_attr));
406 kfree(name, M_MQBUF);
409 if (attr.mq_maxmsg <= 0 ||
410 attr.mq_maxmsg > mq_max_maxmsg ||
411 attr.mq_msgsize <= 0 ||
412 attr.mq_msgsize > mq_max_msgsize) {
413 kfree(name, M_MQBUF);
418 memset(&attr, 0, sizeof(struct mq_attr));
419 attr.mq_maxmsg = mq_def_maxmsg;
421 MQ_DEF_MSGSIZE - sizeof(struct mq_msg);
425 * Allocate new mqueue, initialize data structures,
426 * copy the name, attributes and set the flag.
428 mq_new = kmalloc(sizeof(struct mqueue), M_MQBUF, M_WAITOK | M_ZERO);
430 lockinit(&mq_new->mq_mtx, "mq_new->mq_mtx", 0, LK_CANRECURSE);
431 for (i = 0; i < (MQ_PQSIZE + 1); i++) {
432 TAILQ_INIT(&mq_new->mq_head[i]);
435 strlcpy(mq_new->mq_name, name, MQ_NAMELEN);
436 memcpy(&mq_new->mq_attrib, &attr, sizeof(struct mq_attr));
438 /*CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);*/
439 /* mq_new->mq_attrib.mq_flags = (O_MASK & oflag); */
440 mq_new->mq_attrib.mq_flags = oflag;
442 /* Store mode and effective UID with GID */
443 mq_new->mq_mode = ((SCARG(uap, mode) &
444 ~p->p_fd->fd_cmask) & ALLPERMS) & ~S_ISTXT;
445 mq_new->mq_euid = td->td_ucred->cr_uid;
446 mq_new->mq_egid = td->td_ucred->cr_svgid;
449 /* Allocate file structure and descriptor */
450 error = falloc(td->td_lwp, &fp, &mqd);
453 mqueue_destroy(mq_new);
454 kfree(name, M_MQBUF);
457 fp->f_type = DTYPE_MQUEUE;
458 fp->f_flag = FFLAGS(oflag) & (FREAD | FWRITE);
461 /* Look up for mqueue with such name */
462 lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
463 mq = mqueue_lookup(name);
467 KKASSERT(lockstatus(&mq->mq_mtx, curthread));
469 /* Check if mqueue is not marked as unlinking */
470 if (mq->mq_attrib.mq_flags & MQ_UNLINK) {
474 /* Fail if O_EXCL is set, and mqueue already exists */
475 if ((oflag & O_CREAT) && (oflag & O_EXCL)) {
481 * Check the permissions. Note the difference between
482 * VREAD/VWRITE and FREAD/FWRITE.
485 if (fp->f_flag & FREAD) {
488 if (fp->f_flag & FWRITE) {
491 if (vaccess(VNON, mq->mq_mode, mq->mq_euid, mq->mq_egid,
492 acc_mode, td->td_ucred)) {
498 /* Fail if mqueue neither exists, nor we create it */
499 if ((oflag & O_CREAT) == 0) {
500 lockmgr(&mqlist_mtx, LK_RELEASE);
501 KKASSERT(mq_new == NULL);
502 fsetfd(fdp, NULL, mqd);
503 fp->f_ops = &badfileops;
505 kfree(name, M_MQBUF);
509 /* Check the limit */
510 if (p->p_mqueue_cnt == mq_open_max) {
515 /* Insert the queue to the list */
517 lockmgr(&mq->mq_mtx, LK_EXCLUSIVE);
518 LIST_INSERT_HEAD(&mqueue_head, mq, mq_list);
520 getnanotime(&mq->mq_btime);
521 mq->mq_atime = mq->mq_mtime = mq->mq_btime;
524 /* Increase the counters, and make descriptor ready */
529 lockmgr(&mq->mq_mtx, LK_RELEASE);
530 lockmgr(&mqlist_mtx, LK_RELEASE);
533 mqueue_destroy(mq_new);
535 fsetfd(fdp, NULL, mqd);
536 fp->f_ops = &badfileops;
538 fsetfd(fdp, fp, mqd);
539 uap->sysmsg_result = mqd;
542 kfree(name, M_MQBUF);
548 sys_mq_close(struct mq_close_args *uap)
550 return sys_close((void *)uap);
554 * Primary mq_receive1() function.
557 mq_receive1(struct lwp *l, mqd_t mqdes, void *msg_ptr, size_t msg_len,
558 unsigned *msg_prio, struct timespec *ts, ssize_t *mlen)
562 struct mq_msg *msg = NULL;
563 struct mq_attr *mqattr;
567 /* Get the message queue */
568 error = mqueue_get(l, mqdes, &fp);
573 if ((fp->f_flag & FREAD) == 0) {
577 getnanotime(&mq->mq_atime);
578 mqattr = &mq->mq_attrib;
580 /* Check the message size limits */
581 if (msg_len < mqattr->mq_msgsize) {
586 /* Check if queue is empty */
587 while (mqattr->mq_curmsgs == 0) {
590 if (mqattr->mq_flags & O_NONBLOCK) {
595 error = abstimeout2timo(ts, &t);
601 * Block until someone sends the message.
602 * While doing this, notification should not be sent.
604 mqattr->mq_flags |= MQ_RECEIVE;
605 error = lksleep(&mq->mq_send_cv, &mq->mq_mtx, PCATCH, "mqsend", t);
606 mqattr->mq_flags &= ~MQ_RECEIVE;
607 if (error || (mqattr->mq_flags & MQ_UNLINK)) {
608 error = (error == EWOULDBLOCK) ? ETIMEDOUT : EINTR;
615 * Find the highest priority message, and remove it from the queue.
616 * At first, reserved queue is checked, bitmap is next.
618 msg = TAILQ_FIRST(&mq->mq_head[MQ_PQRESQ]);
619 if (__predict_true(msg == NULL)) {
620 idx = ffs(mq->mq_bitmap);
621 msg = TAILQ_FIRST(&mq->mq_head[idx]);
622 KKASSERT(msg != NULL);
626 TAILQ_REMOVE(&mq->mq_head[idx], msg, msg_queue);
628 /* Unmark the bit, if last message. */
629 if (__predict_true(idx) && TAILQ_EMPTY(&mq->mq_head[idx])) {
630 KKASSERT((MQ_PQSIZE - idx) == msg->msg_prio);
631 mq->mq_bitmap &= ~(1 << --idx);
634 /* Decrement the counter and signal waiter, if any */
635 mqattr->mq_curmsgs--;
636 wakeup_one(&mq->mq_recv_cv);
638 /* Ready for sending now */
640 KNOTE(&mq->mq_wsel.si_note, 0);
643 lockmgr(&mq->mq_mtx, LK_RELEASE);
649 * Copy the data to the user-space.
650 * Note: According to POSIX, no message should be removed from the
651 * queue in case of fail - this would be violated.
653 *mlen = msg->msg_len;
654 error = copyout(msg->msg_ptr, msg_ptr, msg->msg_len);
655 if (error == 0 && msg_prio)
656 error = copyout(&msg->msg_prio, msg_prio, sizeof(unsigned));
657 mqueue_freemsg(msg, sizeof(struct mq_msg) + msg->msg_len);
663 sys_mq_receive(struct mq_receive_args *uap)
666 syscallarg(mqd_t) mqdes;
667 syscallarg(char *) msg_ptr;
668 syscallarg(size_t) msg_len;
669 syscallarg(unsigned *) msg_prio;
674 error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
675 SCARG(uap, msg_len), SCARG(uap, msg_prio), 0, &mlen);
677 uap->sysmsg_result = mlen;
683 sys_mq_timedreceive(struct mq_timedreceive_args *uap)
686 syscallarg(mqd_t) mqdes;
687 syscallarg(char *) msg_ptr;
688 syscallarg(size_t) msg_len;
689 syscallarg(unsigned *) msg_prio;
690 syscallarg(const struct timespec *) abs_timeout;
694 struct timespec ts, *tsp;
696 /* Get and convert time value */
697 if (SCARG(uap, abs_timeout)) {
698 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
706 error = mq_receive1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
707 SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
709 uap->sysmsg_result = mlen;
715 * Primary mq_send1() function.
718 mq_send1(struct lwp *l, mqd_t mqdes, const char *msg_ptr, size_t msg_len,
719 unsigned msg_prio, struct timespec *ts)
724 struct mq_attr *mqattr;
725 struct proc *notify = NULL;
730 /* Check the priority range */
731 if (msg_prio >= mq_prio_max)
734 /* Allocate a new message */
735 size = sizeof(struct mq_msg) + msg_len;
736 if (size > mq_max_msgsize)
739 if (size > MQ_DEF_MSGSIZE) {
740 msg = kmalloc(size, M_MQBUF, M_WAITOK);
742 msg = objcache_get(mqmsg_cache, M_WAITOK);
745 /* Get the data from user-space */
746 error = copyin(msg_ptr, msg->msg_ptr, msg_len);
748 mqueue_freemsg(msg, size);
751 msg->msg_len = msg_len;
752 msg->msg_prio = msg_prio;
755 error = mqueue_get(l, mqdes, &fp);
757 mqueue_freemsg(msg, size);
761 if ((fp->f_flag & FWRITE) == 0) {
765 getnanotime(&mq->mq_mtime);
766 mqattr = &mq->mq_attrib;
768 /* Check the message size limit */
769 if (msg_len <= 0 || msg_len > mqattr->mq_msgsize) {
774 /* Check if queue is full */
775 while (mqattr->mq_curmsgs >= mqattr->mq_maxmsg) {
778 if (mqattr->mq_flags & O_NONBLOCK) {
783 error = abstimeout2timo(ts, &t);
788 /* Block until queue becomes available */
789 error = lksleep(&mq->mq_recv_cv, &mq->mq_mtx, PCATCH, "mqrecv", t);
790 if (error || (mqattr->mq_flags & MQ_UNLINK)) {
791 error = (error == EWOULDBLOCK) ? ETIMEDOUT : error;
795 KKASSERT(mq->mq_attrib.mq_curmsgs < mq->mq_attrib.mq_maxmsg);
798 * Insert message into the queue, according to the priority.
799 * Note the difference between index and priority.
801 if (__predict_true(msg_prio < MQ_PQSIZE)) {
802 u_int idx = MQ_PQSIZE - msg_prio;
804 KKASSERT(idx != MQ_PQRESQ);
805 TAILQ_INSERT_TAIL(&mq->mq_head[idx], msg, msg_queue);
806 mq->mq_bitmap |= (1 << --idx);
808 mqueue_linear_insert(mq, msg);
811 /* Check for the notify */
812 if (mqattr->mq_curmsgs == 0 && mq->mq_notify_proc &&
813 (mqattr->mq_flags & MQ_RECEIVE) == 0 &&
814 mq->mq_sig_notify.sigev_notify == SIGEV_SIGNAL) {
815 /* Initialize the signal */
817 /*ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;*/
818 /*ksi.ksi_code = SI_MESGQ;*/
819 /*ksi.ksi_value = mq->mq_sig_notify.sigev_value;*/
820 /* Unregister the process */
821 notify = mq->mq_notify_proc;
822 mq->mq_notify_proc = NULL;
825 /* Increment the counter and signal waiter, if any */
826 mqattr->mq_curmsgs++;
827 wakeup_one(&mq->mq_send_cv);
829 /* Ready for receiving now */
831 KNOTE(&mq->mq_rsel.si_note, 0);
834 lockmgr(&mq->mq_mtx, LK_RELEASE);
838 mqueue_freemsg(msg, size);
840 /* Send the notify, if needed */
841 lwkt_gettoken(&proc_token);
842 /*kpsignal(notify, &ksi, NULL);*/
843 ksignal(notify, mq->mq_sig_notify.sigev_signo);
844 lwkt_reltoken(&proc_token);
851 sys_mq_send(struct mq_send_args *uap)
854 syscallarg(mqd_t) mqdes;
855 syscallarg(const char *) msg_ptr;
856 syscallarg(size_t) msg_len;
857 syscallarg(unsigned) msg_prio;
860 return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
861 SCARG(uap, msg_len), SCARG(uap, msg_prio), 0);
865 sys_mq_timedsend(struct mq_timedsend_args *uap)
868 syscallarg(mqd_t) mqdes;
869 syscallarg(const char *) msg_ptr;
870 syscallarg(size_t) msg_len;
871 syscallarg(unsigned) msg_prio;
872 syscallarg(const struct timespec *) abs_timeout;
874 struct timespec ts, *tsp;
877 /* Get and convert time value */
878 if (SCARG(uap, abs_timeout)) {
879 error = copyin(SCARG(uap, abs_timeout), &ts, sizeof(ts));
887 return mq_send1(curthread->td_lwp, SCARG(uap, mqdes), SCARG(uap, msg_ptr),
888 SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
892 sys_mq_notify(struct mq_notify_args *uap)
895 syscallarg(mqd_t) mqdes;
896 syscallarg(const struct sigevent *) notification;
903 if (SCARG(uap, notification)) {
904 /* Get the signal from user-space */
905 error = copyin(SCARG(uap, notification), &sig,
906 sizeof(struct sigevent));
909 if (sig.sigev_notify == SIGEV_SIGNAL &&
910 (sig.sigev_signo <= 0 || sig.sigev_signo >= NSIG))
914 error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
919 if (SCARG(uap, notification)) {
920 /* Register notification: set the signal and target process */
921 if (mq->mq_notify_proc == NULL) {
922 memcpy(&mq->mq_sig_notify, &sig,
923 sizeof(struct sigevent));
924 mq->mq_notify_proc = curproc;
926 /* Fail if someone else already registered */
930 /* Unregister the notification */
931 mq->mq_notify_proc = NULL;
933 lockmgr(&mq->mq_mtx, LK_RELEASE);
940 sys_mq_getattr(struct mq_getattr_args *uap)
943 syscallarg(mqd_t) mqdes;
944 syscallarg(struct mq_attr *) mqstat;
951 /* Get the message queue */
952 error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
956 memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
957 lockmgr(&mq->mq_mtx, LK_RELEASE);
960 return copyout(&attr, SCARG(uap, mqstat), sizeof(struct mq_attr));
964 sys_mq_setattr(struct mq_setattr_args *uap)
967 syscallarg(mqd_t) mqdes;
968 syscallarg(const struct mq_attr *) mqstat;
969 syscallarg(struct mq_attr *) omqstat;
976 error = copyin(SCARG(uap, mqstat), &attr, sizeof(struct mq_attr));
979 nonblock = (attr.mq_flags & O_NONBLOCK);
981 /* Get the message queue */
982 error = mqueue_get(curthread->td_lwp, SCARG(uap, mqdes), &fp);
987 /* Copy the old attributes, if needed */
988 if (SCARG(uap, omqstat)) {
989 memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
992 /* Ignore everything, except O_NONBLOCK */
994 mq->mq_attrib.mq_flags |= O_NONBLOCK;
996 mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
998 lockmgr(&mq->mq_mtx, LK_RELEASE);
1002 * Copy the data to the user-space.
1003 * Note: According to POSIX, the new attributes should not be set in
1004 * case of fail - this would be violated.
1006 if (SCARG(uap, omqstat))
1007 error = copyout(&attr, SCARG(uap, omqstat),
1008 sizeof(struct mq_attr));
1014 sys_mq_unlink(struct mq_unlink_args *uap)
1017 syscallarg(const char *) name;
1019 struct thread *td = curthread;
1022 int error, refcnt = 0;
1024 /* Get the name from the user-space */
1025 name = kmalloc(MQ_NAMELEN, M_MQBUF, M_WAITOK | M_ZERO);
1026 error = copyinstr(SCARG(uap, name), name, MQ_NAMELEN - 1, NULL);
1028 kfree(name, M_MQBUF);
1032 /* Lookup for this file */
1033 lockmgr(&mqlist_mtx, LK_EXCLUSIVE);
1034 mq = mqueue_lookup(name);
1040 /* Check the permissions */
1041 if (td->td_ucred->cr_uid != mq->mq_euid &&
1042 priv_check(td, PRIV_ROOT) != 0) {
1043 lockmgr(&mq->mq_mtx, LK_RELEASE);
1048 /* Mark message queue as unlinking, before leaving the window */
1049 mq->mq_attrib.mq_flags |= MQ_UNLINK;
1051 /* Wake up all waiters, if there are such */
1052 wakeup(&mq->mq_send_cv);
1053 wakeup(&mq->mq_recv_cv);
1056 KNOTE(&mq->mq_rsel.si_note, 0);
1057 KNOTE(&mq->mq_wsel.si_note, 0);
1060 refcnt = mq->mq_refcnt;
1062 LIST_REMOVE(mq, mq_list);
1064 lockmgr(&mq->mq_mtx, LK_RELEASE);
1066 lockmgr(&mqlist_mtx, LK_RELEASE);
1069 * If there are no references - destroy the message
1070 * queue, otherwise, the last mq_close() will do that.
1072 if (error == 0 && refcnt == 0)
1075 kfree(name, M_MQBUF);
1082 SYSCTL_NODE(_kern, OID_AUTO, mqueue,
1083 CTLFLAG_RW, 0, "Message queue options");
1085 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_open_max,
1086 CTLFLAG_RW, &mq_open_max, 0,
1087 "Maximal number of message queue descriptors per process");
1089 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_prio_max,
1090 CTLFLAG_RW, &mq_prio_max, 0,
1091 "Maximal priority of the message");
1093 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_max_msgsize,
1094 CTLFLAG_RW, &mq_max_msgsize, 0,
1095 "Maximal allowed size of the message");
1097 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_def_maxmsg,
1098 CTLFLAG_RW, &mq_def_maxmsg, 0,
1099 "Default maximal message count");
1101 SYSCTL_INT(_kern_mqueue, OID_AUTO, mq_max_maxmsg,
1102 CTLFLAG_RW, &mq_max_maxmsg, 0,
1103 "Maximal allowed message count");
1105 SYSINIT(sys_mqueue_init, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, mqueue_sysinit, NULL);