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/param.h>
45 #include <sys/types.h>
46 #include <sys/errno.h>
47 #include <sys/fcntl.h>
49 #include <sys/filedesc.h>
50 #include <sys/ucred.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mqueue.h>
56 #include <sys/queue.h>
57 #include <sys/event.h>
58 #include <sys/serialize.h>
59 #include <sys/signal.h>
60 #include <sys/signalvar.h>
61 #include <sys/spinlock.h>
62 #include <sys/spinlock2.h>
64 #include <sys/stdbool.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 tstohz(const struct timespec
*ts
);
97 /* File operations vector */
98 static struct fileops mqops
= {
99 .fo_read
= badfo_readwrite
,
100 .fo_write
= badfo_readwrite
,
101 .fo_ioctl
= badfo_ioctl
,
102 .fo_stat
= mq_stat_fop
,
103 .fo_close
= mq_close_fop
,
104 .fo_kqfilter
= mq_kqfilter_fop
,
105 .fo_shutdown
= badfo_shutdown
108 /* Define a new malloc type for message queues */
109 MALLOC_DECLARE(M_MQBUF
);
110 MALLOC_DEFINE(M_MQBUF
, "mqueues", "Buffers to message queues");
113 * Initialize POSIX message queue subsystem.
118 lockinit(&mqlist_mtx
, "mqlist_mtx", 0, LK_CANRECURSE
);
125 mqueue_freemsg(struct mq_msg
*msg
, const size_t size
)
131 * Destroy the message queue.
134 mqueue_destroy(struct mqueue
*mq
)
140 /* Note MQ_PQSIZE + 1. */
141 for (i
= 0; i
< MQ_PQSIZE
+ 1; i
++) {
142 while ((msg
= TAILQ_FIRST(&mq
->mq_head
[i
])) != NULL
) {
143 TAILQ_REMOVE(&mq
->mq_head
[i
], msg
, msg_queue
);
144 msz
= sizeof(struct mq_msg
) + msg
->msg_len
;
145 mqueue_freemsg(msg
, msz
);
148 lockuninit(&mq
->mq_mtx
);
153 * Lookup for file name in general list of message queues.
154 * => locks the message queue
157 mqueue_lookup(char *name
)
161 KKASSERT(lockstatus(&mqlist_mtx
, curthread
));
163 LIST_FOREACH(mq
, &mqueue_head
, mq_list
) {
164 if (strncmp(mq
->mq_name
, name
, MQ_NAMELEN
) == 0) {
165 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
174 * mqueue_get: get the mqueue from the descriptor.
175 * => locks the message queue, if found.
176 * => holds a reference on the file descriptor.
179 mqueue_get(struct lwp
*l
, mqd_t mqd
, file_t
**fpr
)
184 fp
= holdfp(curproc
->p_fd
, (int)mqd
, -1); /* XXX: Why -1 ? */
185 if (__predict_false(fp
== NULL
))
188 if (__predict_false(fp
->f_type
!= DTYPE_MQUEUE
)) {
193 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
200 * mqueue_linear_insert: perform linear insert according to the message
201 * priority into the reserved queue (MQ_PQRESQ). Reserved queue is a
202 * sorted list used only when mq_prio_max is increased via sysctl.
205 mqueue_linear_insert(struct mqueue
*mq
, struct mq_msg
*msg
)
209 TAILQ_FOREACH(mit
, &mq
->mq_head
[MQ_PQRESQ
], msg_queue
) {
210 if (msg
->msg_prio
> mit
->msg_prio
)
214 TAILQ_INSERT_TAIL(&mq
->mq_head
[MQ_PQRESQ
], msg
, msg_queue
);
216 TAILQ_INSERT_BEFORE(mit
, msg
, msg_queue
);
221 * Compute number of ticks in the specified amount of time.
224 tstohz(const struct timespec
*ts
)
229 * usec has great enough resolution for hz, so convert to a
230 * timeval and use tvtohz() above.
232 TIMESPEC_TO_TIMEVAL(&tv
, ts
);
233 return tvtohz_high(&tv
); /* XXX Why _high() and not _low() ? */
237 * Converter from struct timespec to the ticks.
238 * Used by mq_timedreceive(), mq_timedsend().
241 abstimeout2timo(struct timespec
*ts
, int *timo
)
246 error
= itimespecfix(ts
);
251 timespecsub(ts
, &tsd
);
252 if (ts
->tv_sec
< 0 || (ts
->tv_sec
== 0 && ts
->tv_nsec
<= 0)) {
256 KKASSERT(*timo
!= 0);
262 mq_stat_fop(file_t
*fp
, struct stat
*st
, struct ucred
*cred
)
264 struct mqueue
*mq
= fp
->f_data
;
266 (void)memset(st
, 0, sizeof(*st
));
268 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
269 st
->st_mode
= mq
->mq_mode
;
270 st
->st_uid
= mq
->mq_euid
;
271 st
->st_gid
= mq
->mq_egid
;
272 st
->st_atimespec
= mq
->mq_atime
;
273 st
->st_mtimespec
= mq
->mq_mtime
;
274 /*st->st_ctimespec = st->st_birthtimespec = mq->mq_btime;*/
275 st
->st_uid
= fp
->f_cred
->cr_uid
;
276 st
->st_gid
= fp
->f_cred
->cr_svgid
;
277 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
282 static struct filterops mqfiltops_read
=
283 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, mqfilter_read_detach
, mqfilter_read
};
284 static struct filterops mqfiltops_write
=
285 { FILTEROP_ISFD
|FILTEROP_MPSAFE
, NULL
, mqfilter_write_detach
, mqfilter_write
};
288 mq_kqfilter_fop(struct file
*fp
, struct knote
*kn
)
290 struct mqueue
*mq
= fp
->f_data
;
293 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
295 switch (kn
->kn_filter
) {
297 kn
->kn_fop
= &mqfiltops_read
;
298 kn
->kn_hook
= (caddr_t
)mq
;
299 klist
= &mq
->mq_rkq
.ki_note
;
302 kn
->kn_fop
= &mqfiltops_write
;
303 kn
->kn_hook
= (caddr_t
)mq
;
304 klist
= &mq
->mq_wkq
.ki_note
;
307 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
311 knote_insert(klist
, kn
);
312 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
318 mqfilter_read_detach(struct knote
*kn
)
320 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
322 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
323 struct klist
*klist
= &mq
->mq_rkq
.ki_note
;
324 knote_remove(klist
, kn
);
325 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
329 mqfilter_write_detach(struct knote
*kn
)
331 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
333 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
334 struct klist
*klist
= &mq
->mq_wkq
.ki_note
;
335 knote_remove(klist
, kn
);
336 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
340 mqfilter_read(struct knote
*kn
, long hint
)
342 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
343 struct mq_attr
*mqattr
;
346 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
347 mqattr
= &mq
->mq_attrib
;
348 /* Ready for receiving, if there are messages in the queue */
349 if (mqattr
->mq_curmsgs
)
351 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
357 mqfilter_write(struct knote
*kn
, long hint
)
359 struct mqueue
*mq
= (struct mqueue
*)kn
->kn_hook
;
360 struct mq_attr
*mqattr
;
363 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
364 mqattr
= &mq
->mq_attrib
;
365 /* Ready for sending, if the message queue is not full */
366 if (mqattr
->mq_curmsgs
< mqattr
->mq_maxmsg
)
368 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
374 mq_close_fop(file_t
*fp
)
376 struct proc
*p
= curproc
;
377 struct mqueue
*mq
= fp
->f_data
;
380 lockmgr(&mqlist_mtx
, LK_EXCLUSIVE
);
381 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
383 /* Decrease the counters */
387 /* Remove notification if registered for this process */
388 if (mq
->mq_notify_proc
== p
)
389 mq
->mq_notify_proc
= NULL
;
392 * If this is the last reference and mqueue is marked for unlink,
393 * remove and later destroy the message queue.
395 if (mq
->mq_refcnt
== 0 && (mq
->mq_attrib
.mq_flags
& MQ_UNLINK
)) {
396 LIST_REMOVE(mq
, mq_list
);
401 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
402 lockmgr(&mqlist_mtx
, LK_RELEASE
);
411 * General mqueue system calls.
415 sys_mq_open(struct mq_open_args
*uap
)
418 syscallarg(const char *) name;
419 syscallarg(int) oflag;
420 syscallarg(mode_t) mode;
421 syscallarg(struct mq_attr) attr;
423 struct thread
*td
= curthread
;
424 struct proc
*p
= td
->td_proc
;
425 struct filedesc
*fdp
= p
->p_fd
;
426 struct mqueue
*mq
, *mq_new
= NULL
;
429 int mqd
, error
, oflag
;
431 /* Check access mode flags */
432 oflag
= SCARG(uap
, oflag
);
433 if ((oflag
& O_ACCMODE
) == (O_WRONLY
| O_RDWR
)) {
437 /* Get the name from the user-space */
438 name
= kmalloc(MQ_NAMELEN
, M_MQBUF
, M_WAITOK
| M_ZERO
| M_NULLOK
);
441 error
= copyinstr(SCARG(uap
, name
), name
, MQ_NAMELEN
- 1, NULL
);
443 kfree(name
, M_MQBUF
);
447 if (oflag
& O_CREAT
) {
451 /* Check the limit */
452 if (p
->p_mqueue_cnt
== mq_open_max
) {
453 kfree(name
, M_MQBUF
);
457 /* Empty name is invalid */
458 if (name
[0] == '\0') {
459 kfree(name
, M_MQBUF
);
463 /* Check for mqueue attributes */
464 if (SCARG(uap
, attr
)) {
465 error
= copyin(SCARG(uap
, attr
), &attr
,
466 sizeof(struct mq_attr
));
468 kfree(name
, M_MQBUF
);
471 if (attr
.mq_maxmsg
<= 0 ||
472 attr
.mq_maxmsg
> mq_max_maxmsg
||
473 attr
.mq_msgsize
<= 0 ||
474 attr
.mq_msgsize
> mq_max_msgsize
) {
475 kfree(name
, M_MQBUF
);
480 memset(&attr
, 0, sizeof(struct mq_attr
));
481 attr
.mq_maxmsg
= mq_def_maxmsg
;
483 MQ_DEF_MSGSIZE
- sizeof(struct mq_msg
);
487 * Allocate new mqueue, initialize data structures,
488 * copy the name, attributes and set the flag.
490 mq_new
= kmalloc(sizeof(struct mqueue
), M_MQBUF
,
491 M_WAITOK
| M_ZERO
| M_NULLOK
);
492 if (mq_new
== NULL
) {
493 kfree(name
, M_MQBUF
);
497 lockinit(&mq_new
->mq_mtx
, "mq_new->mq_mtx", 0, LK_CANRECURSE
);
498 for (i
= 0; i
< (MQ_PQSIZE
+ 1); i
++) {
499 TAILQ_INIT(&mq_new
->mq_head
[i
]);
502 strlcpy(mq_new
->mq_name
, name
, MQ_NAMELEN
);
503 memcpy(&mq_new
->mq_attrib
, &attr
, sizeof(struct mq_attr
));
505 /*CTASSERT((O_MASK & (MQ_UNLINK | MQ_RECEIVE)) == 0);*/
506 /* mq_new->mq_attrib.mq_flags = (O_MASK & oflag); */
507 mq_new
->mq_attrib
.mq_flags
= oflag
;
509 /* Store mode and effective UID with GID */
510 mq_new
->mq_mode
= ((SCARG(uap
, mode
) &
511 ~p
->p_fd
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
512 mq_new
->mq_euid
= td
->td_ucred
->cr_uid
;
513 mq_new
->mq_egid
= td
->td_ucred
->cr_svgid
;
516 /* Allocate file structure and descriptor */
517 error
= falloc(td
->td_lwp
, &fp
, &mqd
);
520 mqueue_destroy(mq_new
);
521 kfree(name
, M_MQBUF
);
524 fp
->f_type
= DTYPE_MQUEUE
;
525 fp
->f_flag
= FFLAGS(oflag
) & (FREAD
| FWRITE
);
528 /* Look up for mqueue with such name */
529 lockmgr(&mqlist_mtx
, LK_EXCLUSIVE
);
530 mq
= mqueue_lookup(name
);
534 KKASSERT(lockstatus(&mq
->mq_mtx
, curthread
));
536 /* Check if mqueue is not marked as unlinking */
537 if (mq
->mq_attrib
.mq_flags
& MQ_UNLINK
) {
541 /* Fail if O_EXCL is set, and mqueue already exists */
542 if ((oflag
& O_CREAT
) && (oflag
& O_EXCL
)) {
548 * Check the permissions. Note the difference between
549 * VREAD/VWRITE and FREAD/FWRITE.
552 if (fp
->f_flag
& FREAD
) {
555 if (fp
->f_flag
& FWRITE
) {
558 if (vaccess(VNON
, mq
->mq_mode
, mq
->mq_euid
, mq
->mq_egid
,
559 acc_mode
, td
->td_ucred
)) {
565 /* Fail if mqueue neither exists, nor we create it */
566 if ((oflag
& O_CREAT
) == 0) {
567 lockmgr(&mqlist_mtx
, LK_RELEASE
);
568 KKASSERT(mq_new
== NULL
);
569 fsetfd(fdp
, NULL
, mqd
);
570 fp
->f_ops
= &badfileops
;
572 kfree(name
, M_MQBUF
);
576 /* Check the limit */
577 if (p
->p_mqueue_cnt
== mq_open_max
) {
582 /* Insert the queue to the list */
584 lockmgr(&mq
->mq_mtx
, LK_EXCLUSIVE
);
585 LIST_INSERT_HEAD(&mqueue_head
, mq
, mq_list
);
587 getnanotime(&mq
->mq_btime
);
588 mq
->mq_atime
= mq
->mq_mtime
= mq
->mq_btime
;
591 /* Increase the counters, and make descriptor ready */
596 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
597 lockmgr(&mqlist_mtx
, LK_RELEASE
);
600 mqueue_destroy(mq_new
);
602 fsetfd(fdp
, NULL
, mqd
);
603 fp
->f_ops
= &badfileops
;
605 fsetfd(fdp
, fp
, mqd
);
606 uap
->sysmsg_result
= mqd
;
609 kfree(name
, M_MQBUF
);
615 sys_mq_close(struct mq_close_args
*uap
)
617 return sys_close((void *)uap
);
621 * Primary mq_receive1() function.
624 mq_receive1(struct lwp
*l
, mqd_t mqdes
, void *msg_ptr
, size_t msg_len
,
625 unsigned *msg_prio
, struct timespec
*ts
, ssize_t
*mlen
)
629 struct mq_msg
*msg
= NULL
;
630 struct mq_attr
*mqattr
;
634 /* Get the message queue */
635 error
= mqueue_get(l
, mqdes
, &fp
);
640 if ((fp
->f_flag
& FREAD
) == 0) {
644 getnanotime(&mq
->mq_atime
);
645 mqattr
= &mq
->mq_attrib
;
647 /* Check the message size limits */
648 if (msg_len
< mqattr
->mq_msgsize
) {
653 /* Check if queue is empty */
654 while (mqattr
->mq_curmsgs
== 0) {
657 if (mqattr
->mq_flags
& O_NONBLOCK
) {
662 error
= abstimeout2timo(ts
, &t
);
668 * Block until someone sends the message.
669 * While doing this, notification should not be sent.
671 mqattr
->mq_flags
|= MQ_RECEIVE
;
672 error
= lksleep(&mq
->mq_send_cv
, &mq
->mq_mtx
, PCATCH
, "mqsend", t
);
673 mqattr
->mq_flags
&= ~MQ_RECEIVE
;
674 if (error
|| (mqattr
->mq_flags
& MQ_UNLINK
)) {
675 error
= (error
== EWOULDBLOCK
) ? ETIMEDOUT
: EINTR
;
682 * Find the highest priority message, and remove it from the queue.
683 * At first, reserved queue is checked, bitmap is next.
685 msg
= TAILQ_FIRST(&mq
->mq_head
[MQ_PQRESQ
]);
686 if (__predict_true(msg
== NULL
)) {
687 idx
= ffs(mq
->mq_bitmap
);
688 msg
= TAILQ_FIRST(&mq
->mq_head
[idx
]);
689 KKASSERT(msg
!= NULL
);
693 TAILQ_REMOVE(&mq
->mq_head
[idx
], msg
, msg_queue
);
695 /* Unmark the bit, if last message. */
696 if (__predict_true(idx
) && TAILQ_EMPTY(&mq
->mq_head
[idx
])) {
697 KKASSERT((MQ_PQSIZE
- idx
) == msg
->msg_prio
);
698 mq
->mq_bitmap
&= ~(1 << --idx
);
701 /* Decrement the counter and signal waiter, if any */
702 mqattr
->mq_curmsgs
--;
703 wakeup_one(&mq
->mq_recv_cv
);
705 /* Ready for sending now */
706 KNOTE(&mq
->mq_wkq
.ki_note
, 0);
708 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
714 * Copy the data to the user-space.
715 * Note: According to POSIX, no message should be removed from the
716 * queue in case of fail - this would be violated.
718 *mlen
= msg
->msg_len
;
719 error
= copyout(msg
->msg_ptr
, msg_ptr
, msg
->msg_len
);
720 if (error
== 0 && msg_prio
)
721 error
= copyout(&msg
->msg_prio
, msg_prio
, sizeof(unsigned));
722 mqueue_freemsg(msg
, sizeof(struct mq_msg
) + msg
->msg_len
);
728 sys_mq_receive(struct mq_receive_args
*uap
)
731 syscallarg(mqd_t) mqdes;
732 syscallarg(char *) msg_ptr;
733 syscallarg(size_t) msg_len;
734 syscallarg(unsigned *) msg_prio;
739 error
= mq_receive1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
740 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), 0, &mlen
);
742 uap
->sysmsg_result
= mlen
;
748 sys_mq_timedreceive(struct mq_timedreceive_args
*uap
)
751 syscallarg(mqd_t) mqdes;
752 syscallarg(char *) msg_ptr;
753 syscallarg(size_t) msg_len;
754 syscallarg(unsigned *) msg_prio;
755 syscallarg(const struct timespec *) abs_timeout;
759 struct timespec ts
, *tsp
;
761 /* Get and convert time value */
762 if (SCARG(uap
, abs_timeout
)) {
763 error
= copyin(SCARG(uap
, abs_timeout
), &ts
, sizeof(ts
));
771 error
= mq_receive1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
772 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), tsp
, &mlen
);
774 uap
->sysmsg_result
= mlen
;
780 * Primary mq_send1() function.
783 mq_send1(struct lwp
*l
, mqd_t mqdes
, const char *msg_ptr
, size_t msg_len
,
784 unsigned msg_prio
, struct timespec
*ts
)
789 struct mq_attr
*mqattr
;
790 struct proc
*notify
= NULL
;
795 /* Check the priority range */
796 if (msg_prio
>= mq_prio_max
)
799 /* Allocate a new message */
800 size
= sizeof(struct mq_msg
) + msg_len
;
801 if (size
> mq_max_msgsize
)
804 msg
= kmalloc(size
, M_MQBUF
, M_WAITOK
| M_NULLOK
);
809 /* Get the data from user-space */
810 error
= copyin(msg_ptr
, msg
->msg_ptr
, msg_len
);
812 mqueue_freemsg(msg
, size
);
815 msg
->msg_len
= msg_len
;
816 msg
->msg_prio
= msg_prio
;
819 error
= mqueue_get(l
, mqdes
, &fp
);
821 mqueue_freemsg(msg
, size
);
825 if ((fp
->f_flag
& FWRITE
) == 0) {
829 getnanotime(&mq
->mq_mtime
);
830 mqattr
= &mq
->mq_attrib
;
832 /* Check the message size limit */
833 if (msg_len
<= 0 || msg_len
> mqattr
->mq_msgsize
) {
838 /* Check if queue is full */
839 while (mqattr
->mq_curmsgs
>= mqattr
->mq_maxmsg
) {
842 if (mqattr
->mq_flags
& O_NONBLOCK
) {
847 error
= abstimeout2timo(ts
, &t
);
852 /* Block until queue becomes available */
853 error
= lksleep(&mq
->mq_recv_cv
, &mq
->mq_mtx
, PCATCH
, "mqrecv", t
);
854 if (error
|| (mqattr
->mq_flags
& MQ_UNLINK
)) {
855 error
= (error
== EWOULDBLOCK
) ? ETIMEDOUT
: error
;
859 KKASSERT(mq
->mq_attrib
.mq_curmsgs
< mq
->mq_attrib
.mq_maxmsg
);
862 * Insert message into the queue, according to the priority.
863 * Note the difference between index and priority.
865 if (__predict_true(msg_prio
< MQ_PQSIZE
)) {
866 u_int idx
= MQ_PQSIZE
- msg_prio
;
868 KKASSERT(idx
!= MQ_PQRESQ
);
869 TAILQ_INSERT_TAIL(&mq
->mq_head
[idx
], msg
, msg_queue
);
870 mq
->mq_bitmap
|= (1 << --idx
);
872 mqueue_linear_insert(mq
, msg
);
875 /* Check for the notify */
876 if (mqattr
->mq_curmsgs
== 0 && mq
->mq_notify_proc
&&
877 (mqattr
->mq_flags
& MQ_RECEIVE
) == 0 &&
878 mq
->mq_sig_notify
.sigev_notify
== SIGEV_SIGNAL
) {
879 /* Initialize the signal */
881 /*ksi.ksi_signo = mq->mq_sig_notify.sigev_signo;*/
882 /*ksi.ksi_code = SI_MESGQ;*/
883 /*ksi.ksi_value = mq->mq_sig_notify.sigev_value;*/
884 /* Unregister the process */
885 notify
= mq
->mq_notify_proc
;
886 mq
->mq_notify_proc
= NULL
;
889 /* Increment the counter and signal waiter, if any */
890 mqattr
->mq_curmsgs
++;
891 wakeup_one(&mq
->mq_send_cv
);
893 /* Ready for receiving now */
894 KNOTE(&mq
->mq_rkq
.ki_note
, 0);
897 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
899 mqueue_freemsg(msg
, size
);
902 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
904 /* Send the notify, if needed */
905 /*kpsignal(notify, &ksi, NULL);*/
906 ksignal(notify
, mq
->mq_sig_notify
.sigev_signo
);
909 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
916 sys_mq_send(struct mq_send_args
*uap
)
919 syscallarg(mqd_t) mqdes;
920 syscallarg(const char *) msg_ptr;
921 syscallarg(size_t) msg_len;
922 syscallarg(unsigned) msg_prio;
925 return mq_send1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
926 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), 0);
930 sys_mq_timedsend(struct mq_timedsend_args
*uap
)
933 syscallarg(mqd_t) mqdes;
934 syscallarg(const char *) msg_ptr;
935 syscallarg(size_t) msg_len;
936 syscallarg(unsigned) msg_prio;
937 syscallarg(const struct timespec *) abs_timeout;
939 struct timespec ts
, *tsp
;
942 /* Get and convert time value */
943 if (SCARG(uap
, abs_timeout
)) {
944 error
= copyin(SCARG(uap
, abs_timeout
), &ts
, sizeof(ts
));
952 return mq_send1(curthread
->td_lwp
, SCARG(uap
, mqdes
), SCARG(uap
, msg_ptr
),
953 SCARG(uap
, msg_len
), SCARG(uap
, msg_prio
), tsp
);
957 sys_mq_notify(struct mq_notify_args
*uap
)
960 syscallarg(mqd_t) mqdes;
961 syscallarg(const struct sigevent *) notification;
968 if (SCARG(uap
, notification
)) {
969 /* Get the signal from user-space */
970 error
= copyin(SCARG(uap
, notification
), &sig
,
971 sizeof(struct sigevent
));
974 if (sig
.sigev_notify
== SIGEV_SIGNAL
&&
975 (sig
.sigev_signo
<= 0 || sig
.sigev_signo
>= NSIG
))
979 error
= mqueue_get(curthread
->td_lwp
, SCARG(uap
, mqdes
), &fp
);
984 if (SCARG(uap
, notification
)) {
985 /* Register notification: set the signal and target process */
986 if (mq
->mq_notify_proc
== NULL
) {
987 memcpy(&mq
->mq_sig_notify
, &sig
,
988 sizeof(struct sigevent
));
989 mq
->mq_notify_proc
= curproc
;
991 /* Fail if someone else already registered */
995 /* Unregister the notification */
996 mq
->mq_notify_proc
= NULL
;
998 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1005 sys_mq_getattr(struct mq_getattr_args
*uap
)
1008 syscallarg(mqd_t) mqdes;
1009 syscallarg(struct mq_attr *) mqstat;
1013 struct mq_attr attr
;
1016 /* Get the message queue */
1017 error
= mqueue_get(curthread
->td_lwp
, SCARG(uap
, mqdes
), &fp
);
1021 memcpy(&attr
, &mq
->mq_attrib
, sizeof(struct mq_attr
));
1022 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1025 return copyout(&attr
, SCARG(uap
, mqstat
), sizeof(struct mq_attr
));
1029 sys_mq_setattr(struct mq_setattr_args
*uap
)
1032 syscallarg(mqd_t) mqdes;
1033 syscallarg(const struct mq_attr *) mqstat;
1034 syscallarg(struct mq_attr *) omqstat;
1038 struct mq_attr attr
;
1039 int error
, nonblock
;
1041 error
= copyin(SCARG(uap
, mqstat
), &attr
, sizeof(struct mq_attr
));
1044 nonblock
= (attr
.mq_flags
& O_NONBLOCK
);
1046 /* Get the message queue */
1047 error
= mqueue_get(curthread
->td_lwp
, SCARG(uap
, mqdes
), &fp
);
1052 /* Copy the old attributes, if needed */
1053 if (SCARG(uap
, omqstat
)) {
1054 memcpy(&attr
, &mq
->mq_attrib
, sizeof(struct mq_attr
));
1057 /* Ignore everything, except O_NONBLOCK */
1059 mq
->mq_attrib
.mq_flags
|= O_NONBLOCK
;
1061 mq
->mq_attrib
.mq_flags
&= ~O_NONBLOCK
;
1063 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1067 * Copy the data to the user-space.
1068 * Note: According to POSIX, the new attributes should not be set in
1069 * case of fail - this would be violated.
1071 if (SCARG(uap
, omqstat
))
1072 error
= copyout(&attr
, SCARG(uap
, omqstat
),
1073 sizeof(struct mq_attr
));
1079 sys_mq_unlink(struct mq_unlink_args
*uap
)
1082 syscallarg(const char *) name;
1084 struct thread
*td
= curthread
;
1087 int error
, refcnt
= 0;
1089 /* Get the name from the user-space */
1090 name
= kmalloc(MQ_NAMELEN
, M_MQBUF
, M_WAITOK
| M_ZERO
| M_NULLOK
);
1093 error
= copyinstr(SCARG(uap
, name
), name
, MQ_NAMELEN
- 1, NULL
);
1095 kfree(name
, M_MQBUF
);
1099 /* Lookup for this file */
1100 lockmgr(&mqlist_mtx
, LK_EXCLUSIVE
);
1101 mq
= mqueue_lookup(name
);
1107 /* Check the permissions */
1108 if (td
->td_ucred
->cr_uid
!= mq
->mq_euid
&&
1109 priv_check(td
, PRIV_ROOT
) != 0) {
1110 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1115 /* Mark message queue as unlinking, before leaving the window */
1116 mq
->mq_attrib
.mq_flags
|= MQ_UNLINK
;
1118 /* Wake up all waiters, if there are such */
1119 wakeup(&mq
->mq_send_cv
);
1120 wakeup(&mq
->mq_recv_cv
);
1122 KNOTE(&mq
->mq_rkq
.ki_note
, 0);
1123 KNOTE(&mq
->mq_wkq
.ki_note
, 0);
1125 refcnt
= mq
->mq_refcnt
;
1127 LIST_REMOVE(mq
, mq_list
);
1129 lockmgr(&mq
->mq_mtx
, LK_RELEASE
);
1131 lockmgr(&mqlist_mtx
, LK_RELEASE
);
1134 * If there are no references - destroy the message
1135 * queue, otherwise, the last mq_close() will do that.
1137 if (error
== 0 && refcnt
== 0)
1140 kfree(name
, M_MQBUF
);
1147 SYSCTL_NODE(_kern
, OID_AUTO
, mqueue
,
1148 CTLFLAG_RW
, 0, "Message queue options");
1150 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_open_max
,
1151 CTLFLAG_RW
, &mq_open_max
, 0,
1152 "Maximal number of message queue descriptors per process");
1154 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_prio_max
,
1155 CTLFLAG_RW
, &mq_prio_max
, 0,
1156 "Maximal priority of the message");
1158 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_max_msgsize
,
1159 CTLFLAG_RW
, &mq_max_msgsize
, 0,
1160 "Maximal allowed size of the message");
1162 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_def_maxmsg
,
1163 CTLFLAG_RW
, &mq_def_maxmsg
, 0,
1164 "Default maximal message count");
1166 SYSCTL_INT(_kern_mqueue
, OID_AUTO
, mq_max_maxmsg
,
1167 CTLFLAG_RW
, &mq_max_maxmsg
, 0,
1168 "Maximal allowed message count");
1170 SYSINIT(sys_mqueue_init
, SI_SUB_PRE_DRIVERS
, SI_ORDER_ANY
, mqueue_sysinit
, NULL
);