netbsd32_mqueue.c revision 1.3 1 1.3 martin /* $NetBSD: netbsd32_mqueue.c,v 1.3 2015/06/21 08:47:15 martin Exp $ */
2 1.1 martin
3 1.1 martin /*-
4 1.1 martin * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 martin * All rights reserved.
6 1.1 martin *
7 1.1 martin * This code is derived from software developed for The NetBSD Foundation.
8 1.1 martin *
9 1.1 martin * Redistribution and use in source and binary forms, with or without
10 1.1 martin * modification, are permitted provided that the following conditions
11 1.1 martin * are met:
12 1.1 martin * 1. Redistributions of source code must retain the above copyright
13 1.1 martin * notice, this list of conditions and the following disclaimer.
14 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 martin * notice, this list of conditions and the following disclaimer in the
16 1.1 martin * documentation and/or other materials provided with the distribution.
17 1.1 martin *
18 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
29 1.1 martin */
30 1.1 martin
31 1.1 martin #include <sys/cdefs.h>
32 1.3 martin __KERNEL_RCSID(0, "$NetBSD: netbsd32_mqueue.c,v 1.3 2015/06/21 08:47:15 martin Exp $");
33 1.1 martin
34 1.1 martin #if defined(_KERNEL_OPT)
35 1.1 martin #include "opt_compat_netbsd.h"
36 1.1 martin #endif
37 1.1 martin
38 1.1 martin #include <sys/param.h>
39 1.1 martin #include <sys/dirent.h>
40 1.1 martin #include <sys/filedesc.h>
41 1.1 martin #include <sys/fcntl.h>
42 1.1 martin #include <sys/module.h>
43 1.1 martin
44 1.1 martin #include <compat/netbsd32/netbsd32.h>
45 1.1 martin #include <compat/netbsd32/netbsd32_syscall.h>
46 1.1 martin #include <compat/netbsd32/netbsd32_syscallargs.h>
47 1.1 martin #include <compat/netbsd32/netbsd32_conv.h>
48 1.1 martin
49 1.1 martin
50 1.1 martin int
51 1.1 martin netbsd32_mq_open(struct lwp *l, const struct netbsd32_mq_open_args *uap,
52 1.1 martin register_t *retval)
53 1.1 martin {
54 1.1 martin /* {
55 1.1 martin syscallarg(const netbsd32_charp) name;
56 1.1 martin syscallarg(int) oflag;
57 1.1 martin syscallarg(mode_t) mode;
58 1.1 martin syscallarg(struct netbsd32_mq_attrp_t) attr;
59 1.1 martin } */
60 1.1 martin struct netbsd32_mq_attr attr32;
61 1.1 martin struct mq_attr *attr = NULL, a;
62 1.1 martin int error;
63 1.1 martin
64 1.1 martin if ((SCARG(uap, oflag) & O_CREAT) && (SCARG_P32(uap,attr) != NULL)) {
65 1.1 martin error = copyin(&attr32, SCARG_P32(uap,attr), sizeof(attr32));
66 1.1 martin if (error)
67 1.1 martin return error;
68 1.1 martin netbsd32_to_mq_attr(&attr32, &a);
69 1.1 martin attr = &a;
70 1.1 martin }
71 1.1 martin
72 1.1 martin return mq_handle_open(l, SCARG_P32(uap, name), SCARG(uap, oflag),
73 1.1 martin SCARG(uap, mode), attr, retval);
74 1.1 martin }
75 1.1 martin
76 1.1 martin int
77 1.1 martin netbsd32_mq_close(struct lwp *l, const struct netbsd32_mq_close_args *uap,
78 1.1 martin register_t *retval)
79 1.1 martin {
80 1.1 martin /* {
81 1.1 martin syscallarg(mqd_t) mqdes;
82 1.1 martin } */
83 1.1 martin
84 1.1 martin return netbsd32_close(l, (const void*)uap, retval);
85 1.1 martin }
86 1.1 martin
87 1.1 martin int
88 1.1 martin netbsd32_mq_unlink(struct lwp *l, const struct netbsd32_mq_unlink_args *uap,
89 1.1 martin register_t *retval)
90 1.1 martin {
91 1.1 martin /* {
92 1.1 martin syscallarg(const netbsd32_charp) name;
93 1.1 martin } */
94 1.1 martin struct sys_mq_unlink_args ua;
95 1.1 martin
96 1.1 martin NETBSD32TOP_UAP(name, const char);
97 1.1 martin return sys_mq_unlink(l, &ua, retval);
98 1.1 martin }
99 1.1 martin
100 1.1 martin int
101 1.1 martin netbsd32_mq_getattr(struct lwp *l, const struct netbsd32_mq_getattr_args *uap,
102 1.1 martin register_t *retval)
103 1.1 martin {
104 1.1 martin /* {
105 1.1 martin syscallarg(mqd_t) mqdes;
106 1.1 martin syscallarg(netbsd32_mq_attrp_t) mqstat;
107 1.1 martin } */
108 1.1 martin struct mqueue *mq;
109 1.1 martin struct mq_attr attr;
110 1.1 martin struct netbsd32_mq_attr a32;
111 1.1 martin int error;
112 1.1 martin
113 1.1 martin error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
114 1.1 martin if (error)
115 1.1 martin return error;
116 1.1 martin
117 1.1 martin memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
118 1.1 martin mutex_exit(&mq->mq_mtx);
119 1.1 martin fd_putfile((int)SCARG(uap, mqdes));
120 1.1 martin netbsd32_from_mq_attr(&attr, &a32);
121 1.1 martin return copyout(&a32, SCARG_P32(uap,mqstat), sizeof(a32));
122 1.1 martin }
123 1.1 martin
124 1.1 martin int
125 1.1 martin netbsd32_mq_setattr(struct lwp *l, const struct netbsd32_mq_setattr_args *uap,
126 1.1 martin register_t *retval)
127 1.1 martin {
128 1.1 martin /* {
129 1.1 martin syscallarg(mqd_t) mqdes;
130 1.1 martin syscallarg(const netbsd32_mq_attrp_t) mqstat;
131 1.1 martin syscallarg(netbsd32_mq_attrp_t) omqstat;
132 1.1 martin } */
133 1.1 martin struct mqueue *mq;
134 1.1 martin struct netbsd32_mq_attr attr32;
135 1.1 martin struct mq_attr attr;
136 1.1 martin int error, nonblock;
137 1.1 martin
138 1.1 martin error = copyin(SCARG_P32(uap, mqstat), &attr32, sizeof(attr32));
139 1.1 martin if (error)
140 1.1 martin return error;
141 1.1 martin netbsd32_to_mq_attr(&attr32, &attr);
142 1.1 martin nonblock = (attr.mq_flags & O_NONBLOCK);
143 1.1 martin
144 1.1 martin error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
145 1.1 martin if (error)
146 1.1 martin return error;
147 1.1 martin
148 1.1 martin /* Copy the old attributes, if needed */
149 1.1 martin if (SCARG_P32(uap, omqstat))
150 1.1 martin memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
151 1.1 martin
152 1.1 martin /* Ignore everything, except O_NONBLOCK */
153 1.1 martin if (nonblock)
154 1.1 martin mq->mq_attrib.mq_flags |= O_NONBLOCK;
155 1.1 martin else
156 1.1 martin mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
157 1.1 martin
158 1.1 martin mutex_exit(&mq->mq_mtx);
159 1.1 martin fd_putfile((int)SCARG(uap, mqdes));
160 1.1 martin
161 1.1 martin /*
162 1.1 martin * Copy the data to the user-space.
163 1.1 martin * Note: According to POSIX, the new attributes should not be set in
164 1.1 martin * case of fail - this would be violated.
165 1.1 martin */
166 1.1 martin if (SCARG_P32(uap, omqstat)) {
167 1.1 martin netbsd32_from_mq_attr(&attr, &attr32);
168 1.1 martin error = copyout(&attr32, SCARG_P32(uap, omqstat),
169 1.1 martin sizeof(attr32));
170 1.1 martin }
171 1.1 martin
172 1.1 martin return error;
173 1.1 martin }
174 1.1 martin
175 1.1 martin int
176 1.1 martin netbsd32_mq_notify(struct lwp *l, const struct netbsd32_mq_notify_args *uap,
177 1.1 martin register_t *result)
178 1.1 martin {
179 1.1 martin /* {
180 1.1 martin syscallarg(mqd_t) mqdes;
181 1.1 martin syscallarg(const netbsd32_sigeventp_t) notification;
182 1.1 martin } */
183 1.1 martin struct mqueue *mq;
184 1.1 martin struct netbsd32_sigevent sig32;
185 1.1 martin int error;
186 1.1 martin
187 1.1 martin if (SCARG_P32(uap, notification)) {
188 1.1 martin /* Get the signal from user-space */
189 1.1 martin error = copyin(SCARG_P32(uap, notification), &sig32,
190 1.1 martin sizeof(sig32));
191 1.1 martin if (error)
192 1.1 martin return error;
193 1.1 martin if (sig32.sigev_notify == SIGEV_SIGNAL &&
194 1.1 martin (sig32.sigev_signo <=0 || sig32.sigev_signo >= NSIG))
195 1.1 martin return EINVAL;
196 1.1 martin }
197 1.1 martin
198 1.1 martin error = mqueue_get(SCARG(uap, mqdes), 0, &mq);
199 1.1 martin if (error) {
200 1.1 martin return error;
201 1.1 martin }
202 1.1 martin if (SCARG_P32(uap, notification)) {
203 1.1 martin /* Register notification: set the signal and target process */
204 1.1 martin if (mq->mq_notify_proc == NULL) {
205 1.1 martin netbsd32_to_sigevent(&sig32, &mq->mq_sig_notify);
206 1.1 martin mq->mq_notify_proc = l->l_proc;
207 1.1 martin } else {
208 1.1 martin /* Fail if someone else already registered */
209 1.1 martin error = EBUSY;
210 1.1 martin }
211 1.1 martin } else {
212 1.1 martin /* Unregister the notification */
213 1.1 martin mq->mq_notify_proc = NULL;
214 1.1 martin }
215 1.1 martin mutex_exit(&mq->mq_mtx);
216 1.1 martin fd_putfile((int)SCARG(uap, mqdes));
217 1.1 martin
218 1.1 martin return error;
219 1.1 martin }
220 1.1 martin
221 1.1 martin int
222 1.1 martin netbsd32_mq_send(struct lwp *l, const struct netbsd32_mq_send_args *uap,
223 1.1 martin register_t *result)
224 1.1 martin {
225 1.1 martin /* {
226 1.1 martin syscallarg(mqd_t) mqdes;
227 1.1 martin syscallarg(const netbsd32_charp) msg_ptr;
228 1.1 martin syscallarg(netbsd32_size_t) msg_len;
229 1.1 martin syscallarg(unsigned) msg_prio;
230 1.1 martin } */
231 1.1 martin
232 1.1 martin
233 1.1 martin return mq_send1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
234 1.1 martin SCARG(uap, msg_len), SCARG(uap, msg_prio), NULL);
235 1.1 martin }
236 1.1 martin
237 1.1 martin int
238 1.1 martin netbsd32_mq_receive(struct lwp *l, const struct netbsd32_mq_receive_args *uap,
239 1.1 martin register_t *retval)
240 1.1 martin {
241 1.1 martin /* {
242 1.1 martin syscallarg(mqd_t) mqdes;
243 1.1 martin syscallarg(netbsd32_charp) msg_ptr;
244 1.1 martin syscallarg(netbsd32_size_t) msg_len;
245 1.1 martin syscallarg(netbsd32_uintp) msg_prio;
246 1.1 martin } */
247 1.1 martin ssize_t mlen;
248 1.1 martin int error;
249 1.1 martin
250 1.1 martin error = mq_recv1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
251 1.1 martin SCARG(uap, msg_len), SCARG_P32(uap, msg_prio), NULL, &mlen);
252 1.1 martin if (error == 0)
253 1.1 martin *retval = mlen;
254 1.1 martin
255 1.1 martin return error;
256 1.1 martin }
257 1.1 martin
258 1.2 martin int
259 1.2 martin netbsd32___mq_timedsend50(struct lwp *l,
260 1.2 martin const struct netbsd32___mq_timedsend50_args *uap, register_t *retval)
261 1.2 martin {
262 1.2 martin /* {
263 1.2 martin syscallarg(mqd_t) mqdes;
264 1.2 martin syscallarg(const netbsd32_charp) msg_ptr;
265 1.2 martin syscallarg(netbsd32_size_t) msg_len;
266 1.2 martin syscallarg(unsigned) msg_prio;
267 1.2 martin syscallarg(const netbsd32_timespecp_t) abs_timeout;
268 1.2 martin } */
269 1.2 martin struct timespec ts, *tsp;
270 1.2 martin struct netbsd32_timespec ts32;
271 1.2 martin int error;
272 1.2 martin
273 1.2 martin /* Get and convert time value */
274 1.2 martin if (SCARG_P32(uap, abs_timeout)) {
275 1.2 martin error = copyin(SCARG_P32(uap, abs_timeout), &ts32,
276 1.2 martin sizeof(ts32));
277 1.2 martin if (error)
278 1.2 martin return error;
279 1.2 martin netbsd32_to_timespec(&ts32, &ts);
280 1.2 martin tsp = &ts;
281 1.2 martin } else {
282 1.2 martin tsp = NULL;
283 1.2 martin }
284 1.2 martin
285 1.2 martin return mq_send1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
286 1.2 martin SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
287 1.2 martin }
288 1.2 martin
289 1.2 martin int
290 1.2 martin netbsd32___mq_timedreceive50(struct lwp *l,
291 1.2 martin const struct netbsd32___mq_timedreceive50_args *uap, register_t *retval)
292 1.2 martin {
293 1.2 martin /* {
294 1.2 martin syscallarg(mqd_t) mqdes;
295 1.2 martin syscallarg(netbsd32_charp) msg_ptr;
296 1.2 martin syscallarg(netbsd32_size_t) msg_len;
297 1.2 martin syscallarg(netbsd32_uintp) msg_prio;
298 1.2 martin syscallarg(const netbsd32_timespecp_t) abs_timeout;
299 1.2 martin } */
300 1.2 martin struct timespec ts, *tsp;
301 1.2 martin struct netbsd32_timespec ts32;
302 1.2 martin ssize_t mlen;
303 1.2 martin int error;
304 1.2 martin
305 1.2 martin /* Get and convert time value */
306 1.2 martin if (SCARG_P32(uap, abs_timeout)) {
307 1.2 martin error = copyin(SCARG_P32(uap, abs_timeout), &ts32,
308 1.2 martin sizeof(ts32));
309 1.2 martin if (error)
310 1.2 martin return error;
311 1.2 martin netbsd32_to_timespec(&ts32, &ts);
312 1.2 martin tsp = &ts;
313 1.2 martin } else {
314 1.2 martin tsp = NULL;
315 1.2 martin }
316 1.2 martin
317 1.2 martin error = mq_recv1(SCARG(uap, mqdes), SCARG_P32(uap, msg_ptr),
318 1.2 martin SCARG(uap, msg_len), SCARG_P32(uap, msg_prio), tsp, &mlen);
319 1.2 martin if (error == 0)
320 1.2 martin *retval = mlen;
321 1.2 martin
322 1.2 martin return error;
323 1.2 martin }
324 1.1 martin
325