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/mqueue.h>
57 #include <sys/queue.h>
58 #include <sys/event.h>
59 #include <sys/serialize.h>
60 #include <sys/signal.h>
61 #include <sys/signalvar.h>
62 #include <sys/spinlock.h>
63 #include <sys/spinlock2.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysproto.h>
67 #include <sys/systm.h>
69 #include <sys/unistd.h>
70 #include <sys/vnode.h>
72 /* System-wide limits. */
73 static u_int mq_open_max
= MQ_OPEN_MAX
;
74 static u_int mq_prio_max
= MQ_PRIO_MAX
;
75 static u_int mq_max_msgsize
= 16 * MQ_DEF_MSGSIZE
;
76 static u_int mq_def_maxmsg
= 32;
77 static u_int mq_max_maxmsg
= 16 * 32;
79 struct lock mqlist_mtx
;
80 static LIST_HEAD(, mqueue
) mqueue_head
=
81 LIST_HEAD_INITIALIZER(mqueue_head
);
83 typedef struct file file_t
; /* XXX: Should we put this in sys/types.h ? */
85 /* Function prototypes */
86 static int mq_stat_fop(file_t
*, struct stat
*, struct ucred
*cred
);
87 static int mq_close_fop(file_t
*);
88 static int mq_kqfilter_fop(struct file
*fp
, struct knote
*kn
);
89 static void mqfilter_read_detach(struct knote
*kn
);
90 static void mqfilter_write_detach(struct knote
*kn
);
91 static int mqfilter_read(struct knote
*kn
, long hint
);
92 static int mqfilter_write(struct knote
*kn
, long hint
);
94 /* Some time-related utility functions */
95 static int itimespecfix(struct timespec
*ts
);
96 static int tstohz(const struct timespec
*ts
);
98 /* File operations vector */
99 static struct fileops mqops
= {
100 .fo_read
= badfo_readwrite
,
101 .fo_write
= badfo_readwrite
,
102 .fo_ioctl
= badfo_ioctl
,
103 .fo_stat
= mq_stat_fop
,
104 .fo_close
= mq_close_fop
,
105 .fo_kqfilter
= mq_kqfilter_fop
,
106 .fo_shutdown
= badfo_shutdown
109 /* Define a new malloc type for message queues */
110 MALLOC_DECLARE(M_MQBUF
);
111 MALLOC_DEFINE(M_MQBUF
, "mqueues", "Buffers to message queues");
114 * Initialize POSIX message queue subsystem.
119 lockinit(&mqlist_mtx
, "mqlist_mtx", 0, LK_CANRECURSE
);
126 mqueue_freemsg(struct mq_msg
*msg
, const size_t size
)
132 * Destroy the message queue.
135 mqueue_destroy(struct mqueue
*mq
)
141 /* Note MQ_PQSIZE + 1. */
142 for (i
= 0; i
< MQ_PQSIZE
+ 1; i
++) {
143 while ((msg
= TAILQ_FIRST(&mq
->mq_head
[i
])) != NULL
) {
144 TAILQ_REMOVE(&mq
->mq_head
[i
], msg
, msg_queue
);
145 msz
= sizeof(struct mq_msg
) + msg
->msg_len
;
146 mqueue_freemsg(msg
, msz
);
149 lockuninit(&mq
->mq_mtx
);
154 * Lookup for file name in general list of message queues.
155 * => locks the message queue
158 mqueue_lookup(char *name
)
162 KKASSERT(lockstatus(&mqlist_mtx
, curthread
));
164 LIST_FOREACH(mq
, &mqueue_head
, mq_list
) {
165 if (strncmp(mq
->mq_name
, name
, MQ_NAMELEN
) == 0) {
166 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
175 * mqueue_get: get the mqueue from the descriptor.
176 * => locks the message queue, if found.
177 * => holds a reference on the file descriptor.
180 mqueue_get(struct lwp
*l
, mqd_t mqd
, file_t
**fpr
)
185 fp
= holdfp(curproc
->p_fd
, (int)mqd
, -1); /* XXX: Why -1 ? */
186 if (__predict_false(fp
== NULL
))
189 if (__predict_false(fp
->f_type
!= DTYPE_MQUEUE
)) {
194 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
201 * mqueue_linear_insert: perform linear insert according to the message
202 * priority into the reserved queue (MQ_PQRESQ). Reserved queue is a
203 * sorted list used only when mq_prio_max is increased via sysctl.
206 mqueue_linear_insert(struct mqueue
*mq
, struct mq_msg
*msg
)
210 TAILQ_FOREACH(mit
, &mq
->mq_head
[MQ_PQRESQ
], msg_queue
) {
211 if (msg
->msg_prio
> mit
->msg_prio
)
215 TAILQ_INSERT_TAIL(&mq
->mq_head
[MQ_PQRESQ
], msg
, msg_queue
);
217 TAILQ_INSERT_BEFORE(mit
, msg
, msg_queue
);
225 itimespecfix(struct timespec
*ts
)
227 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0 || ts
->tv_nsec
>= 1000000000)
229 if (ts
->tv_sec
== 0 && ts
->tv_nsec
!= 0 && ts
->tv_nsec
< nstick
)
230 ts
->tv_nsec
= nstick
;
235 * Compute number of ticks in the specified amount of time.
238 tstohz(const struct timespec
*ts
)
243 * usec has great enough resolution for hz, so convert to a
244 * timeval and use tvtohz() above.
246 TIMESPEC_TO_TIMEVAL(&tv
, ts
);
247 return tvtohz_high(&tv
); /* XXX Why _high() and not _low() ? */
251 * Converter from struct timespec to the ticks.
252 * Used by mq_timedreceive(), mq_timedsend().
255 abstimeout2timo(struct timespec
*ts
, int *timo
)
260 error
= itimespecfix(ts
);
265 timespecsub(ts
, &tsd
);
266 if (ts
->tv_sec
< 0 || (ts
->tv_sec
== 0 && ts
->tv_nsec
<= 0)) {
270 KKASSERT(*timo
!= 0);
276 mq_stat_fop(file_t
*fp
, struct stat
*st
, struct ucred
*cred
)
278 struct mqueue
*mq
= fp
->f_data
;
280 (void)memset(st
, 0, sizeof(*st
));
282 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
283 st
->st_mode
= mq
->mq_mode
;
284 st
->st_uid
= mq
->mq_euid
;
285 st
->st_gid
= mq
->mq_egid
;
286 st
->st_atimespec
= mq
->mq_atime
;
287 st
->st_mtimespec
= mq
->mq_mtime
;
288 /*st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;*/
289 st
->st_uid
= fp
->f_cred
->cr_uid
;
290 st
->st_gid
= fp
->f_cred
->cr_svgid
;
291 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
296 static struct filterops mqfiltops_read
=
297 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, mqfilter_read_detach
, mqfilter_read
};
298 static struct filterops mqfiltops_write
=
299 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, mqfilter_write_detach
, mqfilter_write
};
302 mq_kqfilter_fop(struct file
*fp
, struct knote
*kn
)
304 struct mqueue
*mq
= fp
->f_data
;
307 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
309 switch (kn
->kn_filter
) {
311 kn
->kn_fop
= &mqfiltops_read
;
312 kn
->kn_hook
= (caddr_t
)mq
;
313 klist
= &mq
->mq_rkq
.ki_note
;
316 kn
->kn_fop
= &mqfiltops_write
;
317 kn
->kn_hook
= (caddr_t
)mq
;
318 klist
= &mq
->mq_wkq
.ki_note
;
321 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
325 knote_insert(klist
, kn
);
326 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
332 mqfilter_read_detach(struct knote
*kn
)
334 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
336 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
337 struct klist
*klist
= &mq
->mq_rkq
.ki_note
;
338 knote_remove(klist
, kn
);
339 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
343 mqfilter_write_detach(struct knote
*kn
)
345 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
347 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
348 struct klist
*klist
= &mq
->mq_wkq
.ki_note
;
349 knote_remove(klist
, kn
);
350 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
354 mqfilter_read(struct knote
*kn
, long hint
)
356 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
357 struct mq_attr
*mqattr
;
360 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
361 mqattr
= &mq
->mq_attrib
;
362 /* Ready for receiving, if there are messages in the queue */
363 if (mqattr
->mq_curmsgs
)
365 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
371 mqfilter_write(struct knote
*kn
, long hint
)
373 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
374 struct mq_attr
*mqattr
;
377 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
378 mqattr
= &mq
->mq_attrib
;
379 /* Ready for sending, if the message queue is not full */
380 if (mqattr
->mq_curmsgs
< mqattr
->mq_maxmsg
)
382 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
388 mq_close_fop(file_t
*fp
)
390 struct proc
*p
= curproc
;
391 struct mqueue
*mq
= fp
->f_data
;
394 lockmgr(&mqlist_mtx
, LK_EXCLUSIVE
);
395 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
397 /* Decrease the counters */
401 /* Remove notification if registered for this process */
402 if (mq
->mq_notify_proc
== p
)
403 mq
->mq_notify_proc
= NULL
;
406 * If this is the last reference and mqueue is marked for unlink,
407 * remove and later destroy the message queue.
409 if (mq
->mq_refcnt
== 0 && (mq
->mq_attrib
.mq_flags
& MQ_UNLINK
)) {
410 LIST_REMOVE(mq
, mq_list
);
415 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
416 lockmgr(&mqlist_mtx
, LK_RELEASE
);
425 * General mqueue system calls.
429 sys_mq_open(struct mq_open_args
*uap
)
432 syscallarg(const char *) name;
433 syscallarg(int) oflag;
434 syscallarg(mode_t) mode;
435 syscallarg(struct mq_attr) attr;
437 struct thread
*td
= curthread
;
438 struct proc
*p
= td
->td_proc
;
439 struct filedesc
*fdp
= p
->p_fd
;
440 struct mqueue
*mq
, *mq_new
= NULL
;
443 int mqd
, error
, oflag
;
445 /* Check access mode flags */
446 oflag
= SCARG(uap
, oflag
);
447 if ((oflag
& O_ACCMODE
) == (O_WRONLY
| O_RDWR
)) {
451 /* Get the name from the user-space */
452 name
= kmalloc(MQ_NAMELEN
, M_MQBUF
, M_WAITOK
| M_ZERO
| M_NULLOK
);
455 error
= copyinstr(SCARG(uap
, name
), name
, MQ_NAMELEN
- 1, NULL
);
457 kfree(name
, M_MQBUF
);
461 if (oflag
& O_CREAT
) {
465 /* Check the limit */
466 if (p
->p_mqueue_cnt
== mq_open_max
) {
467 kfree(name
, M_MQBUF
);
471 /* Empty name is invalid */
472 if (name
[0] == '\0') {
473 kfree(name
, M_MQBUF
);
477 /* Check for mqueue attributes */
478 if (SCARG(uap
, attr
)) {
479 error
= copyin(SCARG(uap
, attr
), &attr
,
480 sizeof(struct mq_attr
));
482 kfree(name
, M_MQBUF
);
485 if (attr
.mq_maxmsg
<= 0 ||
486 attr
.mq_maxmsg
> mq_max_maxmsg
||
487 attr
.mq_msgsize
<= 0 ||
488 attr
.mq_msgsize
> mq_max_msgsize
) {
489 kfree(name
, M_MQBUF
);
494 memset(&attr
, 0, sizeof(struct mq_attr
));
495 attr
.mq_maxmsg
= mq_def_maxmsg
;
497 MQ_DEF_MSGSIZE
- sizeof(struct mq_msg
);
501 * Allocate new mqueue, initialize data structures,
502 * copy the name, attributes and set the flag.
504 mq_new
= kmalloc(sizeof(struct mqueue
), M_MQBUF
,
505 M_WAITOK
| M_ZERO
| M_NULLOK
);
506 if (mq_new
== NULL
) {
507 kfree(name
, M_MQBUF
);
511 lockinit(&mq_new
->mq_mtx
, "mq_new->mq_mtx", 0, LK_CANRECURSE
);
512 for (i
= 0; i
< (MQ_PQSIZE
+ 1); i
++) {
513 TAILQ_INIT(&mq_new
->mq_head
[i
]);
516 strlcpy(mq_new
->mq_name
, name
, MQ_NAMELEN
);
517 memcpy(&mq_new
->mq_attrib
, &attr
, sizeof(struct mq_attr
));
519 /*CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);*/
520 /* mq_new->mq_attrib.mq_flags = (O_MASK & oflag); */
521 mq_new
->mq_attrib
.mq_flags
= oflag
;
523 /* Store mode and effective UID with GID */
524 mq_new
->mq_mode
= ((SCARG(uap
, mode
) &
525 ~p
->p_fd
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
526 mq_new
->mq_euid
= td
->td_ucred
->cr_uid
;
527 mq_new
->mq_egid
= td
->td_ucred
->cr_svgid
;
530 /* Allocate file structure and descriptor */
531 error
= falloc(td
->td_lwp
, &fp
, &mqd
);
534 mqueue_destroy(mq_new
);
535 kfree(name
, M_MQBUF
);
538 fp
->f_type
= DTYPE_MQUEUE
;
539 fp
->f_flag
= FFLAGS(oflag
) & (FREAD
| FWRITE
);
542 /* Look up for mqueue with such name */
543 lockmgr(&mqlist_mtx
, LK_EXCLUSIVE
);
544 mq
= mqueue_lookup(name
);
548 KKASSERT(lockstatus(&mq
->mq_mtx
, curthread
));
550 /* Check if mqueue is not marked as unlinking */
551 if (mq
->mq_attrib
.mq_flags
& MQ_UNLINK
) {
555 /* Fail if O_EXCL is set, and mqueue already exists */
556 if ((oflag
& O_CREAT
) && (oflag
& O_EXCL
)) {
562 * Check the permissions. Note the difference between
563 * VREAD/VWRITE and FREAD/FWRITE.
566 if (fp
->f_flag
& FREAD
) {
569 if (fp
->f_flag
& FWRITE
) {
572 if (vaccess(VNON
, mq
->mq_mode
, mq
->mq_euid
, mq
->mq_egid
,
573 acc_mode
, td
->td_ucred
)) {
579 /* Fail if mqueue neither exists, nor we create it */
580 if ((oflag
& O_CREAT
) == 0) {
581 lockmgr(&mqlist_mtx
, LK_RELEASE
);
582 KKASSERT(mq_new
== NULL
);
583 fsetfd(fdp
, NULL
, mqd
);
584 fp
->f_ops
= &badfileops
;
586 kfree(name
, M_MQBUF
);
590 /* Check the limit */
591 if (p
->p_mqueue_cnt
== mq_open_max
) {
596 /* Insert the queue to the list */
598 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
599 LIST_INSERT_HEAD(&mqueue_head
, mq
, mq_list
);
601 getnanotime(&mq
->mq_btime
);
602 mq
->mq_atime
= mq
->mq_mtime
= mq
->mq_btime
;
605 /* Increase the counters, and make descriptor ready */
610 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
611 lockmgr(&mqlist_mtx
, LK_RELEASE
);
614 mqueue_destroy(mq_new
);
616 fsetfd(fdp
, NULL
, mqd
);
617 fp
->f_ops
= &badfileops
;
619 fsetfd(fdp
, fp
, mqd
);
620 uap
->sysmsg_result
= mqd
;
623 kfree(name
, M_MQBUF
);
629 sys_mq_close(struct mq_close_args
*uap
)
631 return sys_close((void *)uap
);
635 * Primary mq_receive1() function.
638 mq_receive1(struct lwp
*l
, mqd_t mqdes
, void *msg_ptr
, size_t msg_len
,
639 unsigned *msg_prio
, struct timespec
*ts
, ssize_t
*mlen
)
643 struct mq_msg
*msg
= NULL
;
644 struct mq_attr
*mqattr
;
648 /* Get the message queue */
649 error
= mqueue_get(l
, mqdes
, &fp
);
654 if ((fp
->f_flag
& FREAD
) == 0) {
658 getnanotime(&mq
->mq_atime
);
659 mqattr
= &mq
->mq_attrib
;
661 /* Check the message size limits */
662 if (msg_len
< mqattr
->mq_msgsize
) {
667 /* Check if queue is empty */
668 while (mqattr
->mq_curmsgs
== 0) {
671 if (mqattr
->mq_flags
& O_NONBLOCK
) {
676 error
= abstimeout2timo(ts
, &t
);
682 * Block until someone sends the message.
683 * While doing this, notification should not be sent.
685 mqattr
->mq_flags
|= MQ_RECEIVE
;
686 error
= lksleep(&mq
->mq_send_cv
, &mq
->mq_mtx
, PCATCH
, "mqsend", t
);
687 mqattr
->mq_flags
&= ~MQ_RECEIVE
;
688 if (error
|| (mqattr
->mq_flags
& MQ_UNLINK
)) {
689 error
= (error
== EWOULDBLOCK
) ? ETIMEDOUT
: EINTR
;
696 * Find the highest priority message, and remove it from the queue.
697 * At first, reserved queue is checked, bitmap is next.
699 msg
= TAILQ_FIRST(&mq
->mq_head
[MQ_PQRESQ
]);
700 if (__predict_true(msg
== NULL
)) {
701 idx
= ffs(mq
->mq_bitmap
);
702 msg
= TAILQ_FIRST(&mq
->mq_head
[idx
]);
703 KKASSERT(msg
!= NULL
);
707 TAILQ_REMOVE(&mq
->mq_head
[idx
], msg
, msg_queue
);
709 /* Unmark the bit, if last message. */
710 if (__predict_true(idx
) && TAILQ_EMPTY(&mq
->mq_head
[idx
])) {
711 KKASSERT((MQ_PQSIZE
- idx
) == msg
->msg_prio
);
712 mq
->mq_bitmap
&= ~(1 << --idx
);
715 /* Decrement the counter and signal waiter, if any */
716 mqattr
->mq_curmsgs
--;
717 wakeup_one(&mq
->mq_recv_cv
);
719 /* Ready for sending now */
720 KNOTE(&mq
->mq_wkq
.ki_note
, 0);
722 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
728 * Copy the data to the user-space.
729 * Note: According to POSIX, no message should be removed from the
730 * queue in case of fail - this would be violated.
732 *mlen
= msg
->msg_len
;
733 error
= copyout(msg
->msg_ptr
, msg_ptr
, msg
->msg_len
);
734 if (error
== 0 && msg_prio
)
735 error
= copyout(&msg
->msg_prio
, msg_prio
, sizeof(unsigned));
736 mqueue_freemsg(msg
, sizeof(struct mq_msg
) + msg
->msg_len
);
742 sys_mq_receive(struct mq_receive_args
*uap
)
745 syscallarg(mqd_t) mqdes;
746 syscallarg(char *) msg_ptr;
747 syscallarg(size_t) msg_len;
748 syscallarg(unsigned *) msg_prio;
753 error
= mq_receive1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
754 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), 0, &mlen
);
756 uap
->sysmsg_result
= mlen
;
762 sys_mq_timedreceive(struct mq_timedreceive_args
*uap
)
765 syscallarg(mqd_t) mqdes;
766 syscallarg(char *) msg_ptr;
767 syscallarg(size_t) msg_len;
768 syscallarg(unsigned *) msg_prio;
769 syscallarg(const struct timespec *) abs_timeout;
773 struct timespec ts
, *tsp
;
775 /* Get and convert time value */
776 if (SCARG(uap
, abs_timeout
)) {
777 error
= copyin(SCARG(uap
, abs_timeout
), &ts
, sizeof(ts
));
785 error
= mq_receive1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
786 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), tsp
, &mlen
);
788 uap
->sysmsg_result
= mlen
;
794 * Primary mq_send1() function.
797 mq_send1(struct lwp
*l
, mqd_t mqdes
, const char *msg_ptr
, size_t msg_len
,
798 unsigned msg_prio
, struct timespec
*ts
)
803 struct mq_attr
*mqattr
;
804 struct proc
*notify
= NULL
;
809 /* Check the priority range */
810 if (msg_prio
>= mq_prio_max
)
813 /* Allocate a new message */
814 size
= sizeof(struct mq_msg
) + msg_len
;
815 if (size
> mq_max_msgsize
)
818 msg
= kmalloc(size
, M_MQBUF
, M_WAITOK
| M_NULLOK
);
823 /* Get the data from user-space */
824 error
= copyin(msg_ptr
, msg
->msg_ptr
, msg_len
);
826 mqueue_freemsg(msg
, size
);
829 msg
->msg_len
= msg_len
;
830 msg
->msg_prio
= msg_prio
;
833 error
= mqueue_get(l
, mqdes
, &fp
);
835 mqueue_freemsg(msg
, size
);
839 if ((fp
->f_flag
& FWRITE
) == 0) {
843 getnanotime(&mq
->mq_mtime
);
844 mqattr
= &mq
->mq_attrib
;
846 /* Check the message size limit */
847 if (msg_len
<= 0 || msg_len
> mqattr
->mq_msgsize
) {
852 /* Check if queue is full */
853 while (mqattr
->mq_curmsgs
>= mqattr
->mq_maxmsg
) {
856 if (mqattr
->mq_flags
& O_NONBLOCK
) {
861 error
= abstimeout2timo(ts
, &t
);
866 /* Block until queue becomes available */
867 error
= lksleep(&mq
->mq_recv_cv
, &mq
->mq_mtx
, PCATCH
, "mqrecv", t
);
868 if (error
|| (mqattr
->mq_flags
& MQ_UNLINK
)) {
869 error
= (error
== EWOULDBLOCK
) ? ETIMEDOUT
: error
;
873 KKASSERT(mq
->mq_attrib
.mq_curmsgs
< mq
->mq_attrib
.mq_maxmsg
);
876 * Insert message into the queue, according to the priority.
877 * Note the difference between index and priority.
879 if (__predict_true(msg_prio
< MQ_PQSIZE
)) {
880 u_int idx
= MQ_PQSIZE
- msg_prio
;
882 KKASSERT(idx
!= MQ_PQRESQ
);
883 TAILQ_INSERT_TAIL(&mq
->mq_head
[idx
], msg
, msg_queue
);
884 mq
->mq_bitmap
|= (1 << --idx
);
886 mqueue_linear_insert(mq
, msg
);
889 /* Check for the notify */
890 if (mqattr
->mq_curmsgs
== 0 && mq
->mq_notify_proc
&&
891 (mqattr
->mq_flags
& MQ_RECEIVE
) == 0 &&
892 mq
->mq_sig_notify
.sigev_notify
== SIGEV_SIGNAL
) {
893 /* Initialize the signal */
895 /*ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;*/
896 /*ksi.ksi_code = SI_MESGQ;*/
897 /*ksi.ksi_value = mq->mq_sig_notify.sigev_value;*/
898 /* Unregister the process */
899 notify
= mq
->mq_notify_proc
;
900 mq
->mq_notify_proc
= NULL
;
903 /* Increment the counter and signal waiter, if any */
904 mqattr
->mq_curmsgs
++;
905 wakeup_one(&mq
->mq_send_cv
);
907 /* Ready for receiving now */
908 KNOTE(&mq
->mq_rkq
.ki_note
, 0);
910 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
914 mqueue_freemsg(msg
, size
);
916 /* Send the notify, if needed */
917 lwkt_gettoken(&proc_token
);
918 /*kpsignal(notify, &ksi, NULL);*/
919 ksignal(notify
, mq
->mq_sig_notify
.sigev_signo
);
920 lwkt_reltoken(&proc_token
);
927 sys_mq_send(struct mq_send_args
*uap
)
930 syscallarg(mqd_t) mqdes;
931 syscallarg(const char *) msg_ptr;
932 syscallarg(size_t) msg_len;
933 syscallarg(unsigned) msg_prio;
936 return mq_send1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
937 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), 0);
941 sys_mq_timedsend(struct mq_timedsend_args
*uap
)
944 syscallarg(mqd_t) mqdes;
945 syscallarg(const char *) msg_ptr;
946 syscallarg(size_t) msg_len;
947 syscallarg(unsigned) msg_prio;
948 syscallarg(const struct timespec *) abs_timeout;
950 struct timespec ts
, *tsp
;
953 /* Get and convert time value */
954 if (SCARG(uap
, abs_timeout
)) {
955 error
= copyin(SCARG(uap
, abs_timeout
), &ts
, sizeof(ts
));
963 return mq_send1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
964 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), tsp
);
968 sys_mq_notify(struct mq_notify_args
*uap
)
971 syscallarg(mqd_t) mqdes;
972 syscallarg(const struct sigevent *) notification;
979 if (SCARG(uap
, notification
)) {
980 /* Get the signal from user-space */
981 error
= copyin(SCARG(uap
, notification
), &sig
,
982 sizeof(struct sigevent
));
985 if (sig
.sigev_notify
== SIGEV_SIGNAL
&&
986 (sig
.sigev_signo
<= 0 || sig
.sigev_signo
>= NSIG
))
990 error
= mqueue_get(curthread
->td_lwp
, SCARG(uap
, mqdes
), &fp
);
995 if (SCARG(uap
, notification
)) {
996 /* Register notification: set the signal and target process */
997 if (mq
->mq_notify_proc
== NULL
) {
998 memcpy(&mq
->mq_sig_notify
, &sig
,
999 sizeof(struct sigevent
));
1000 mq
->mq_notify_proc
= curproc
;
1002 /* Fail if someone else already registered */
1006 /* Unregister the notification */
1007 mq
->mq_notify_proc
= NULL
;
1009 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1016 sys_mq_getattr(struct mq_getattr_args
*uap
)
1019 syscallarg(mqd_t) mqdes;
1020 syscallarg(struct mq_attr *) mqstat;
1024 struct mq_attr attr
;
1027 /* Get the message queue */
1028 error
= mqueue_get(curthread
->td_lwp
, SCARG(uap
, mqdes
), &fp
);
1032 memcpy(&attr
, &mq
->mq_attrib
, sizeof(struct mq_attr
));
1033 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1036 return copyout(&attr
, SCARG(uap
, mqstat
), sizeof(struct mq_attr
));
1040 sys_mq_setattr(struct mq_setattr_args
*uap
)
1043 syscallarg(mqd_t) mqdes;
1044 syscallarg(const struct mq_attr *) mqstat;
1045 syscallarg(struct mq_attr *) omqstat;
1049 struct mq_attr attr
;
1050 int error
, nonblock
;
1052 error
= copyin(SCARG(uap
, mqstat
), &attr
, sizeof(struct mq_attr
));
1055 nonblock
= (attr
.mq_flags
& O_NONBLOCK
);
1057 /* Get the message queue */
1058 error
= mqueue_get(curthread
->td_lwp
, SCARG(uap
, mqdes
), &fp
);
1063 /* Copy the old attributes, if needed */
1064 if (SCARG(uap
, omqstat
)) {
1065 memcpy(&attr
, &mq
->mq_attrib
, sizeof(struct mq_attr
));
1068 /* Ignore everything, except O_NONBLOCK */
1070 mq
->mq_attrib
.mq_flags
|= O_NONBLOCK
;
1072 mq
->mq_attrib
.mq_flags
&= ~O_NONBLOCK
;
1074 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1078 * Copy the data to the user-space.
1079 * Note: According to POSIX, the new attributes should not be set in
1080 * case of fail - this would be violated.
1082 if (SCARG(uap
, omqstat
))
1083 error
= copyout(&attr
, SCARG(uap
, omqstat
),
1084 sizeof(struct mq_attr
));
1090 sys_mq_unlink(struct mq_unlink_args
*uap
)
1093 syscallarg(const char *) name;
1095 struct thread
*td
= curthread
;
1098 int error
, refcnt
= 0;
1100 /* Get the name from the user-space */
1101 name
= kmalloc(MQ_NAMELEN
, M_MQBUF
, M_WAITOK
| M_ZERO
| M_NULLOK
);
1104 error
= copyinstr(SCARG(uap
, name
), name
, MQ_NAMELEN
- 1, NULL
);
1106 kfree(name
, M_MQBUF
);
1110 /* Lookup for this file */
1111 lockmgr(&mqlist_mtx
, LK_EXCLUSIVE
);
1112 mq
= mqueue_lookup(name
);
1118 /* Check the permissions */
1119 if (td
->td_ucred
->cr_uid
!= mq
->mq_euid
&&
1120 priv_check(td
, PRIV_ROOT
) != 0) {
1121 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1126 /* Mark message queue as unlinking, before leaving the window */
1127 mq
->mq_attrib
.mq_flags
|= MQ_UNLINK
;
1129 /* Wake up all waiters, if there are such */
1130 wakeup(&mq
->mq_send_cv
);
1131 wakeup(&mq
->mq_recv_cv
);
1133 KNOTE(&mq
->mq_rkq
.ki_note
, 0);
1134 KNOTE(&mq
->mq_wkq
.ki_note
, 0);
1136 refcnt
= mq
->mq_refcnt
;
1138 LIST_REMOVE(mq
, mq_list
);
1140 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1142 lockmgr(&mqlist_mtx
, LK_RELEASE
);
1145 * If there are no references - destroy the message
1146 * queue, otherwise, the last mq_close() will do that.
1148 if (error
== 0 && refcnt
== 0)
1151 kfree(name
, M_MQBUF
);
1158 SYSCTL_NODE(_kern
, OID_AUTO
, mqueue
,
1159 CTLFLAG_RW
, 0, "Message queue options");
1161 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_open_max
,
1162 CTLFLAG_RW
, &mq_open_max
, 0,
1163 "Maximal number of message queue descriptors per process");
1165 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_prio_max
,
1166 CTLFLAG_RW
, &mq_prio_max
, 0,
1167 "Maximal priority of the message");
1169 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_max_msgsize
,
1170 CTLFLAG_RW
, &mq_max_msgsize
, 0,
1171 "Maximal allowed size of the message");
1173 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_def_maxmsg
,
1174 CTLFLAG_RW
, &mq_def_maxmsg
, 0,
1175 "Default maximal message count");
1177 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_max_maxmsg
,
1178 CTLFLAG_RW
, &mq_max_maxmsg
, 0,
1179 "Maximal allowed message count");
1181 SYSINIT(sys_mqueue_init
, SI_SUB_PRE_DRIVERS
, SI_ORDER_ANY
, mqueue_sysinit
, NULL
);