xref: /freebsd/lib/librt/mq.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 David Xu <davidxu@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <sys/syscall.h>
34 #include <sys/mqueue.h>
35 
36 #include "namespace.h"
37 #include <errno.h>
38 #include <pthread.h>
39 #include <stddef.h>
40 #include <stdlib.h>
41 #include <signal.h>
42 #include "sigev_thread.h"
43 #include "un-namespace.h"
44 #include "libc_private.h"
45 
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);
57 
58 struct __mq {
59 	int oshandle;
60 	struct sigev_node *node;
61 };
62 
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);
83 
84 mqd_t
85 __mq_open(const char *name, int oflag, mode_t mode,
86 	const struct mq_attr *attr)
87 {
88 	struct __mq *mq;
89 	int err;
90 
91 	mq = malloc(sizeof(struct __mq));
92 	if (mq == NULL)
93 		return (NULL);
94 
95 	mq->oshandle = __sys_kmq_open(name, oflag, mode, attr);
96 	if (mq->oshandle != -1) {
97 		mq->node = NULL;
98 		return (mq);
99 	}
100 	err = errno;
101 	free(mq);
102 	errno = err;
103 	return ((mqd_t)-1L);
104 }
105 
106 int
107 __mq_close(mqd_t mqd)
108 {
109 	int h;
110 
111 	if (mqd->node != NULL) {
112 		__sigev_list_lock();
113 		__sigev_delete_node(mqd->node);
114 		__sigev_list_unlock();
115 	}
116 	h = mqd->oshandle;
117 	free(mqd);
118 	return (__sys_close(h));
119 }
120 
121 typedef void (*mq_func)(union sigval val);
122 
123 static void
124 mq_dispatch(struct sigev_node *sn)
125 {
126 	mq_func f = sn->sn_func;
127 
128 	/*
129 	 * Check generation before calling user function,
130 	 * this should avoid expired notification.
131 	 */
132 	if (sn->sn_gen == sn->sn_info.si_value.sival_int)
133 		f(sn->sn_value);
134 }
135 
136 int
137 __mq_notify(mqd_t mqd, const struct sigevent *evp)
138 {
139 	struct sigevent ev;
140 	struct sigev_node *sn;
141 	int ret;
142 
143 	if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
144 		if (mqd->node != NULL) {
145 			__sigev_list_lock();
146 			__sigev_delete_node(mqd->node);
147 			mqd->node = NULL;
148 			__sigev_list_unlock();
149 		}
150 		return __sys_kmq_notify(mqd->oshandle, evp);
151 	}
152 
153 	if (__sigev_check_init()) {
154 		/*
155 		 * Thread library is not enabled.
156 		 */
157 		errno = EINVAL;
158 		return (-1);
159 	}
160 
161 	sn = __sigev_alloc(SI_MESGQ, evp, mqd->node, 1);
162 	if (sn == NULL) {
163 		errno = EAGAIN;
164 		return (-1);
165 	}
166 
167 	sn->sn_id = mqd->oshandle;
168 	sn->sn_dispatch = mq_dispatch;
169 	__sigev_get_sigevent(sn, &ev, sn->sn_gen);
170 	__sigev_list_lock();
171 	if (mqd->node != NULL)
172 		__sigev_delete_node(mqd->node);
173 	mqd->node = sn;
174 	__sigev_register(sn);
175 	ret = __sys_kmq_notify(mqd->oshandle, &ev);
176 	__sigev_list_unlock();
177 	return (ret);
178 }
179 
180 int
181 __mq_getattr(mqd_t mqd, struct mq_attr *attr)
182 {
183 
184 	return __sys_kmq_setattr(mqd->oshandle, NULL, attr);
185 }
186 
187 int
188 __mq_setattr(mqd_t mqd, const struct mq_attr *newattr, struct mq_attr *oldattr)
189 {
190 
191 	return __sys_kmq_setattr(mqd->oshandle, newattr, oldattr);
192 }
193 
194 ssize_t
195 __mq_timedreceive(mqd_t mqd, char *buf, size_t len,
196 	unsigned *prio, const struct timespec *timeout)
197 {
198 
199 	return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
200 }
201 
202 ssize_t
203 __mq_timedreceive_cancel(mqd_t mqd, char *buf, size_t len,
204 	unsigned *prio, const struct timespec *timeout)
205 {
206 	int ret;
207 
208 	_pthread_cancel_enter(1);
209 	ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
210 	_pthread_cancel_leave(ret == -1);
211 	return (ret);
212 }
213 
214 ssize_t
215 __mq_receive(mqd_t mqd, char *buf, size_t len, unsigned *prio)
216 {
217 
218 	return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
219 }
220 
221 ssize_t
222 __mq_receive_cancel(mqd_t mqd, char *buf, size_t len, unsigned *prio)
223 {
224 	int ret;
225 
226 	_pthread_cancel_enter(1);
227 	ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
228 	_pthread_cancel_leave(ret == -1);
229 	return (ret);
230 }
231 ssize_t
232 __mq_timedsend(mqd_t mqd, char *buf, size_t len,
233 	unsigned prio, const struct timespec *timeout)
234 {
235 
236 	return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
237 }
238 
239 ssize_t
240 __mq_timedsend_cancel(mqd_t mqd, char *buf, size_t len,
241 	unsigned prio, const struct timespec *timeout)
242 {
243 	int ret;
244 
245 	_pthread_cancel_enter(1);
246 	ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
247 	_pthread_cancel_leave(ret == -1);
248 	return (ret);
249 }
250 
251 ssize_t
252 __mq_send(mqd_t mqd, char *buf, size_t len, unsigned prio)
253 {
254 
255 	return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
256 }
257 
258 
259 ssize_t
260 __mq_send_cancel(mqd_t mqd, char *buf, size_t len, unsigned prio)
261 {
262 	int ret;
263 
264 	_pthread_cancel_enter(1);
265 	ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
266 	_pthread_cancel_leave(ret == -1);
267 	return (ret);
268 }
269 
270 int
271 __mq_unlink(const char *path)
272 {
273 
274 	return __sys_kmq_unlink(path);
275 }
276 
277 #pragma weak mq_getfd_np
278 int
279 mq_getfd_np(mqd_t mqd)
280 {
281 
282 	return (mqd->oshandle);
283 }
284