xref: /freebsd/lib/librt/mq.c (revision aa339f1d5df9e38f36a34eb522355c4eebcae6c4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #include <sys/types.h>
30 #include <sys/syscall.h>
31 #include <sys/mqueue.h>
32 
33 #include "namespace.h"
34 #include <errno.h>
35 #include <pthread.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <signal.h>
39 #include "sigev_thread.h"
40 #include "un-namespace.h"
41 #include "libc_private.h"
42 
43 extern int	__sys_kmq_notify(int, const struct sigevent *);
44 extern int	__sys_kmq_open(const char *, int, mode_t,
45 		    const struct mq_attr *);
46 extern int	__sys_kmq_setattr(int, const struct mq_attr *__restrict,
47 		    struct mq_attr *__restrict);
48 extern ssize_t	__sys_kmq_timedreceive(int, char *__restrict, size_t,
49 		    unsigned *__restrict, const struct timespec *__restrict);
50 extern int	__sys_kmq_timedsend(int, const char *, size_t, unsigned,
51 		    const struct timespec *);
52 extern int	__sys_kmq_unlink(const char *);
53 extern int	__sys_close(int fd);
54 
55 struct __mq {
56 	int oshandle;
57 	struct sigev_node *node;
58 };
59 
60 __weak_reference(__mq_open, mq_open);
61 __weak_reference(__mq_open, _mq_open);
62 __weak_reference(__mq_close, mq_close);
63 __weak_reference(__mq_close, _mq_close);
64 __weak_reference(__mq_notify, mq_notify);
65 __weak_reference(__mq_notify, _mq_notify);
66 __weak_reference(__mq_getattr, mq_getattr);
67 __weak_reference(__mq_getattr, _mq_getattr);
68 __weak_reference(__mq_setattr, mq_setattr);
69 __weak_reference(__mq_setattr, _mq_setattr);
70 __weak_reference(__mq_timedreceive_cancel, mq_timedreceive);
71 __weak_reference(__mq_timedreceive, _mq_timedreceive);
72 __weak_reference(__mq_timedsend_cancel, mq_timedsend);
73 __weak_reference(__mq_timedsend, _mq_timedsend);
74 __weak_reference(__mq_unlink, mq_unlink);
75 __weak_reference(__mq_unlink, _mq_unlink);
76 __weak_reference(__mq_send_cancel, mq_send);
77 __weak_reference(__mq_send, _mq_send);
78 __weak_reference(__mq_receive_cancel, mq_receive);
79 __weak_reference(__mq_receive, _mq_receive);
80 
81 mqd_t
82 __mq_open(const char *name, int oflag, mode_t mode,
83 	const struct mq_attr *attr)
84 {
85 	struct __mq *mq;
86 	int err;
87 
88 	mq = malloc(sizeof(struct __mq));
89 	if (mq == NULL)
90 		return (NULL);
91 
92 	mq->oshandle = __sys_kmq_open(name, oflag, mode, attr);
93 	if (mq->oshandle != -1) {
94 		mq->node = NULL;
95 		return (mq);
96 	}
97 	err = errno;
98 	free(mq);
99 	errno = err;
100 	return ((mqd_t)-1L);
101 }
102 
103 int
104 __mq_close(mqd_t mqd)
105 {
106 	int h;
107 
108 	if (mqd->node != NULL) {
109 		__sigev_list_lock();
110 		__sigev_delete_node(mqd->node);
111 		__sigev_list_unlock();
112 	}
113 	h = mqd->oshandle;
114 	free(mqd);
115 	return (__sys_close(h));
116 }
117 
118 typedef void (*mq_func)(union sigval val);
119 
120 static void
121 mq_dispatch(struct sigev_node *sn)
122 {
123 	mq_func f = sn->sn_func;
124 
125 	/*
126 	 * Check generation before calling user function,
127 	 * this should avoid expired notification.
128 	 */
129 	if (sn->sn_gen == sn->sn_info.si_value.sival_int)
130 		f(sn->sn_value);
131 }
132 
133 int
134 __mq_notify(mqd_t mqd, const struct sigevent *evp)
135 {
136 	struct sigevent ev;
137 	struct sigev_node *sn;
138 	int ret;
139 
140 	if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
141 		if (mqd->node != NULL) {
142 			__sigev_list_lock();
143 			__sigev_delete_node(mqd->node);
144 			mqd->node = NULL;
145 			__sigev_list_unlock();
146 		}
147 		return __sys_kmq_notify(mqd->oshandle, evp);
148 	}
149 
150 	if (__sigev_check_init()) {
151 		/*
152 		 * Thread library is not enabled.
153 		 */
154 		errno = EINVAL;
155 		return (-1);
156 	}
157 
158 	sn = __sigev_alloc(SI_MESGQ, evp, mqd->node, 1);
159 	if (sn == NULL) {
160 		errno = EAGAIN;
161 		return (-1);
162 	}
163 
164 	sn->sn_id = mqd->oshandle;
165 	sn->sn_dispatch = mq_dispatch;
166 	__sigev_get_sigevent(sn, &ev, sn->sn_gen);
167 	__sigev_list_lock();
168 	if (mqd->node != NULL)
169 		__sigev_delete_node(mqd->node);
170 	mqd->node = sn;
171 	__sigev_register(sn);
172 	ret = __sys_kmq_notify(mqd->oshandle, &ev);
173 	__sigev_list_unlock();
174 	return (ret);
175 }
176 
177 int
178 __mq_getattr(mqd_t mqd, struct mq_attr *attr)
179 {
180 
181 	return __sys_kmq_setattr(mqd->oshandle, NULL, attr);
182 }
183 
184 int
185 __mq_setattr(mqd_t mqd, const struct mq_attr *newattr, struct mq_attr *oldattr)
186 {
187 
188 	return __sys_kmq_setattr(mqd->oshandle, newattr, oldattr);
189 }
190 
191 ssize_t
192 __mq_timedreceive(mqd_t mqd, char *buf, size_t len,
193 	unsigned *prio, const struct timespec *timeout)
194 {
195 
196 	return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
197 }
198 
199 ssize_t
200 __mq_timedreceive_cancel(mqd_t mqd, char *buf, size_t len,
201 	unsigned *prio, const struct timespec *timeout)
202 {
203 	int ret;
204 
205 	_pthread_cancel_enter(1);
206 	ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
207 	_pthread_cancel_leave(ret == -1);
208 	return (ret);
209 }
210 
211 ssize_t
212 __mq_receive(mqd_t mqd, char *buf, size_t len, unsigned *prio)
213 {
214 
215 	return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
216 }
217 
218 ssize_t
219 __mq_receive_cancel(mqd_t mqd, char *buf, size_t len, unsigned *prio)
220 {
221 	int ret;
222 
223 	_pthread_cancel_enter(1);
224 	ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
225 	_pthread_cancel_leave(ret == -1);
226 	return (ret);
227 }
228 ssize_t
229 __mq_timedsend(mqd_t mqd, char *buf, size_t len,
230 	unsigned prio, const struct timespec *timeout)
231 {
232 
233 	return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
234 }
235 
236 ssize_t
237 __mq_timedsend_cancel(mqd_t mqd, char *buf, size_t len,
238 	unsigned prio, const struct timespec *timeout)
239 {
240 	int ret;
241 
242 	_pthread_cancel_enter(1);
243 	ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
244 	_pthread_cancel_leave(ret == -1);
245 	return (ret);
246 }
247 
248 ssize_t
249 __mq_send(mqd_t mqd, char *buf, size_t len, unsigned prio)
250 {
251 
252 	return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
253 }
254 
255 
256 ssize_t
257 __mq_send_cancel(mqd_t mqd, char *buf, size_t len, unsigned prio)
258 {
259 	int ret;
260 
261 	_pthread_cancel_enter(1);
262 	ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
263 	_pthread_cancel_leave(ret == -1);
264 	return (ret);
265 }
266 
267 int
268 __mq_unlink(const char *path)
269 {
270 
271 	return __sys_kmq_unlink(path);
272 }
273 
274 #pragma weak mq_getfd_np
275 int
276 mq_getfd_np(mqd_t mqd)
277 {
278 
279 	return (mqd->oshandle);
280 }
281