xref: /freebsd/lib/librt/mq.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*-
2  * Copyright (c) 2006 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/syscall.h>
32 #include <sys/mqueue.h>
33 
34 #include "namespace.h"
35 #include <errno.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <signal.h>
39 #include "sigev_thread.h"
40 #include "un-namespace.h"
41 
42 extern int	__sys_kmq_notify(int, const struct sigevent *);
43 extern int	__sys_kmq_open(const char *, int, mode_t,
44 		    const struct mq_attr *);
45 extern int	__sys_kmq_setattr(int, const struct mq_attr *__restrict,
46 		    struct mq_attr *__restrict);
47 extern ssize_t	__sys_kmq_timedreceive(int, char *__restrict, size_t,
48 		    unsigned *__restrict, const struct timespec *__restrict);
49 extern int	__sys_kmq_timedsend(int, const char *, size_t, unsigned,
50 		    const struct timespec *);
51 extern int	__sys_kmq_unlink(const char *);
52 extern int	__sys_close(int fd);
53 
54 struct __mq {
55 	int oshandle;
56 	struct sigev_node *node;
57 };
58 
59 __weak_reference(__mq_open, mq_open);
60 __weak_reference(__mq_open, _mq_open);
61 __weak_reference(__mq_close, mq_close);
62 __weak_reference(__mq_close, _mq_close);
63 __weak_reference(__mq_notify, mq_notify);
64 __weak_reference(__mq_notify, _mq_notify);
65 __weak_reference(__mq_getattr, mq_getattr);
66 __weak_reference(__mq_getattr, _mq_getattr);
67 __weak_reference(__mq_setattr, mq_setattr);
68 __weak_reference(__mq_setattr, _mq_setattr);
69 __weak_reference(__mq_timedreceive, mq_timedreceive);
70 __weak_reference(__mq_timedreceive, _mq_timedreceive);
71 __weak_reference(__mq_timedsend, mq_timedsend);
72 __weak_reference(__mq_timedsend, _mq_timedsend);
73 __weak_reference(__mq_unlink, mq_unlink);
74 __weak_reference(__mq_unlink, _mq_unlink);
75 __weak_reference(__mq_send, mq_send);
76 __weak_reference(__mq_send, _mq_send);
77 __weak_reference(__mq_receive, mq_receive);
78 __weak_reference(__mq_receive, _mq_receive);
79 
80 mqd_t
81 __mq_open(const char *name, int oflag, mode_t mode,
82 	const struct mq_attr *attr)
83 {
84 	struct __mq *mq;
85 	int err;
86 
87 	mq = malloc(sizeof(struct __mq));
88 	if (mq == NULL)
89 		return (NULL);
90 
91 	mq->oshandle = __sys_kmq_open(name, oflag, mode, attr);
92 	if (mq->oshandle != -1) {
93 		mq->node = NULL;
94 		return (mq);
95 	}
96 	err = errno;
97 	free(mq);
98 	errno = err;
99 	return ((mqd_t)-1L);
100 }
101 
102 int
103 __mq_close(mqd_t mqd)
104 {
105 	int h;
106 
107 	if (mqd->node != NULL) {
108 		__sigev_list_lock();
109 		__sigev_delete_node(mqd->node);
110 		__sigev_list_unlock();
111 	}
112 	h = mqd->oshandle;
113 	free(mqd);
114 	return (__sys_close(h));
115 }
116 
117 typedef void (*mq_func)(union sigval val);
118 
119 static void
120 mq_dispatch(struct sigev_node *sn)
121 {
122 	mq_func f = sn->sn_func;
123 
124 	/*
125 	 * Check generation before calling user function,
126 	 * this should avoid expired notification.
127 	 */
128 	if (sn->sn_gen == sn->sn_info.si_value.sival_int)
129 		f(sn->sn_value);
130 }
131 
132 int
133 __mq_notify(mqd_t mqd, const struct sigevent *evp)
134 {
135 	struct sigevent ev;
136 	struct sigev_node *sn;
137 	int ret;
138 
139 	if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
140 		if (mqd->node != NULL) {
141 			__sigev_list_lock();
142 			__sigev_delete_node(mqd->node);
143 			mqd->node = NULL;
144 			__sigev_list_unlock();
145 		}
146 		return __sys_kmq_notify(mqd->oshandle, evp);
147 	}
148 
149 	if (__sigev_check_init()) {
150 		/*
151 		 * Thread library is not enabled.
152 		 */
153 		errno = EINVAL;
154 		return (-1);
155 	}
156 
157 	sn = __sigev_alloc(SI_MESGQ, evp, mqd->node, 1);
158 	if (sn == NULL) {
159 		errno = EAGAIN;
160 		return (-1);
161 	}
162 
163 	sn->sn_id = mqd->oshandle;
164 	sn->sn_dispatch = mq_dispatch;
165 	__sigev_get_sigevent(sn, &ev, sn->sn_gen);
166 	__sigev_list_lock();
167 	if (mqd->node != NULL)
168 		__sigev_delete_node(mqd->node);
169 	mqd->node = sn;
170 	__sigev_register(sn);
171 	ret = __sys_kmq_notify(mqd->oshandle, &ev);
172 	__sigev_list_unlock();
173 	return (ret);
174 }
175 
176 int
177 __mq_getattr(mqd_t mqd, struct mq_attr *attr)
178 {
179 
180 	return __sys_kmq_setattr(mqd->oshandle, NULL, attr);
181 }
182 
183 int
184 __mq_setattr(mqd_t mqd, const struct mq_attr *newattr, struct mq_attr *oldattr)
185 {
186 
187 	return __sys_kmq_setattr(mqd->oshandle, newattr, oldattr);
188 }
189 
190 ssize_t
191 __mq_timedreceive(mqd_t mqd, char *buf, size_t len,
192 	unsigned *prio, const struct timespec *timeout)
193 {
194 
195 	return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
196 }
197 
198 ssize_t
199 __mq_receive(mqd_t mqd, char *buf, size_t len, unsigned *prio)
200 {
201 
202 	return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
203 }
204 
205 ssize_t
206 __mq_timedsend(mqd_t mqd, char *buf, size_t len,
207 	unsigned prio, const struct timespec *timeout)
208 {
209 
210 	return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
211 }
212 
213 ssize_t
214 __mq_send(mqd_t mqd, char *buf, size_t len, unsigned prio)
215 {
216 
217 	return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
218 }
219 
220 int
221 __mq_unlink(const char *path)
222 {
223 
224 	return __sys_kmq_unlink(path);
225 }
226 
227 int
228 __mq_oshandle(mqd_t mqd)
229 {
230 
231 	return (mqd->oshandle);
232 }
233