2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 David Xu <davidxu@freebsd.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
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <sys/syscall.h>
34 #include <sys/mqueue.h>
36 #include "namespace.h"
42 #include "sigev_thread.h"
43 #include "un-namespace.h"
44 #include "libc_private.h"
46 extern int __sys_kmq_notify(int, const struct sigevent *);
47 extern int __sys_kmq_open(const char *, int, mode_t,
48 const struct mq_attr *);
49 extern int __sys_kmq_setattr(int, const struct mq_attr *__restrict,
50 struct mq_attr *__restrict);
51 extern ssize_t __sys_kmq_timedreceive(int, char *__restrict, size_t,
52 unsigned *__restrict, const struct timespec *__restrict);
53 extern int __sys_kmq_timedsend(int, const char *, size_t, unsigned,
54 const struct timespec *);
55 extern int __sys_kmq_unlink(const char *);
56 extern int __sys_close(int fd);
60 struct sigev_node *node;
63 __weak_reference(__mq_open, mq_open);
64 __weak_reference(__mq_open, _mq_open);
65 __weak_reference(__mq_close, mq_close);
66 __weak_reference(__mq_close, _mq_close);
67 __weak_reference(__mq_notify, mq_notify);
68 __weak_reference(__mq_notify, _mq_notify);
69 __weak_reference(__mq_getattr, mq_getattr);
70 __weak_reference(__mq_getattr, _mq_getattr);
71 __weak_reference(__mq_setattr, mq_setattr);
72 __weak_reference(__mq_setattr, _mq_setattr);
73 __weak_reference(__mq_timedreceive_cancel, mq_timedreceive);
74 __weak_reference(__mq_timedreceive, _mq_timedreceive);
75 __weak_reference(__mq_timedsend_cancel, mq_timedsend);
76 __weak_reference(__mq_timedsend, _mq_timedsend);
77 __weak_reference(__mq_unlink, mq_unlink);
78 __weak_reference(__mq_unlink, _mq_unlink);
79 __weak_reference(__mq_send_cancel, mq_send);
80 __weak_reference(__mq_send, _mq_send);
81 __weak_reference(__mq_receive_cancel, mq_receive);
82 __weak_reference(__mq_receive, _mq_receive);
85 __mq_open(const char *name, int oflag, mode_t mode,
86 const struct mq_attr *attr)
91 mq = malloc(sizeof(struct __mq));
95 mq->oshandle = __sys_kmq_open(name, oflag, mode, attr);
96 if (mq->oshandle != -1) {
107 __mq_close(mqd_t mqd)
111 if (mqd->node != NULL) {
113 __sigev_delete_node(mqd->node);
114 __sigev_list_unlock();
118 return (__sys_close(h));
121 typedef void (*mq_func)(union sigval val);
124 mq_dispatch(struct sigev_node *sn)
126 mq_func f = sn->sn_func;
129 * Check generation before calling user function,
130 * this should avoid expired notification.
132 if (sn->sn_gen == sn->sn_info.si_value.sival_int)
137 __mq_notify(mqd_t mqd, const struct sigevent *evp)
140 struct sigev_node *sn;
143 if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
144 if (mqd->node != NULL) {
146 __sigev_delete_node(mqd->node);
148 __sigev_list_unlock();
150 return __sys_kmq_notify(mqd->oshandle, evp);
153 if (__sigev_check_init()) {
155 * Thread library is not enabled.
161 sn = __sigev_alloc(SI_MESGQ, evp, mqd->node, 1);
167 sn->sn_id = mqd->oshandle;
168 sn->sn_dispatch = mq_dispatch;
169 __sigev_get_sigevent(sn, &ev, sn->sn_gen);
171 if (mqd->node != NULL)
172 __sigev_delete_node(mqd->node);
174 __sigev_register(sn);
175 ret = __sys_kmq_notify(mqd->oshandle, &ev);
176 __sigev_list_unlock();
181 __mq_getattr(mqd_t mqd, struct mq_attr *attr)
184 return __sys_kmq_setattr(mqd->oshandle, NULL, attr);
188 __mq_setattr(mqd_t mqd, const struct mq_attr *newattr, struct mq_attr *oldattr)
191 return __sys_kmq_setattr(mqd->oshandle, newattr, oldattr);
195 __mq_timedreceive(mqd_t mqd, char *buf, size_t len,
196 unsigned *prio, const struct timespec *timeout)
199 return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
203 __mq_timedreceive_cancel(mqd_t mqd, char *buf, size_t len,
204 unsigned *prio, const struct timespec *timeout)
208 _pthread_cancel_enter(1);
209 ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
210 _pthread_cancel_leave(ret == -1);
215 __mq_receive(mqd_t mqd, char *buf, size_t len, unsigned *prio)
218 return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
222 __mq_receive_cancel(mqd_t mqd, char *buf, size_t len, unsigned *prio)
226 _pthread_cancel_enter(1);
227 ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
228 _pthread_cancel_leave(ret == -1);
232 __mq_timedsend(mqd_t mqd, char *buf, size_t len,
233 unsigned prio, const struct timespec *timeout)
236 return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
240 __mq_timedsend_cancel(mqd_t mqd, char *buf, size_t len,
241 unsigned prio, const struct timespec *timeout)
245 _pthread_cancel_enter(1);
246 ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
247 _pthread_cancel_leave(ret == -1);
252 __mq_send(mqd_t mqd, char *buf, size_t len, unsigned prio)
255 return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
260 __mq_send_cancel(mqd_t mqd, char *buf, size_t len, unsigned prio)
264 _pthread_cancel_enter(1);
265 ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
266 _pthread_cancel_leave(ret == -1);
271 __mq_unlink(const char *path)
274 return __sys_kmq_unlink(path);
277 #pragma weak mq_getfd_np
279 mq_getfd_np(mqd_t mqd)
282 return (mqd->oshandle);