1*0124a941Schristos /* $NetBSD */
2*0124a941Schristos
3*0124a941Schristos /*-
4*0124a941Schristos * Copyright (c) 2024 The NetBSD Foundation, Inc.
5*0124a941Schristos * All rights reserved.
6*0124a941Schristos *
7*0124a941Schristos * Redistribution and use in source and binary forms, with or without
8*0124a941Schristos * modification, are permitted provided that the following conditions
9*0124a941Schristos * are met:
10*0124a941Schristos * 1. Redistributions of source code must retain the above copyright
11*0124a941Schristos * notice, this list of conditions and the following disclaimer.
12*0124a941Schristos * 2. Redistributions in binary form must reproduce the above copyright
13*0124a941Schristos * notice, this list of conditions and the following disclaimer in the
14*0124a941Schristos * documentation and/or other materials provided with the distribution.
15*0124a941Schristos *
16*0124a941Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*0124a941Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*0124a941Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*0124a941Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*0124a941Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*0124a941Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*0124a941Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*0124a941Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*0124a941Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*0124a941Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*0124a941Schristos * POSSIBILITY OF SUCH DAMAGE.
27*0124a941Schristos */
28*0124a941Schristos
29*0124a941Schristos #include <sys/cdefs.h>
30*0124a941Schristos __KERNEL_RCSID(0, "$NetBSD: linux_mqueue.c,v 1.1 2024/07/01 01:35:53 christos Exp $");
31*0124a941Schristos
32*0124a941Schristos #include <sys/param.h>
33*0124a941Schristos #include <sys/filedesc.h>
34*0124a941Schristos #include <sys/fcntl.h>
35*0124a941Schristos #include <sys/mqueue.h>
36*0124a941Schristos #include <sys/syscallargs.h>
37*0124a941Schristos
38*0124a941Schristos #include <compat/linux/common/linux_types.h>
39*0124a941Schristos #include <compat/linux/common/linux_sched.h>
40*0124a941Schristos #include <compat/linux/common/linux_fcntl.h>
41*0124a941Schristos #include <compat/linux/common/linux_ipc.h>
42*0124a941Schristos #include <compat/linux/common/linux_sem.h>
43*0124a941Schristos #include <compat/linux/common/linux_signal.h>
44*0124a941Schristos #include <compat/linux/common/linux_sigevent.h>
45*0124a941Schristos #include <compat/linux/common/linux_util.h>
46*0124a941Schristos #include <compat/linux/common/linux_mqueue.h>
47*0124a941Schristos
48*0124a941Schristos #include <compat/linux/linux_syscallargs.h>
49*0124a941Schristos #include <compat/linux/linux_syscall.h>
50*0124a941Schristos
51*0124a941Schristos static void
linux_to_bsd_mq_attr(const struct linux_mq_attr * lmp,struct mq_attr * bmp)52*0124a941Schristos linux_to_bsd_mq_attr(const struct linux_mq_attr *lmp, struct mq_attr *bmp)
53*0124a941Schristos {
54*0124a941Schristos memset(bmp, 0, sizeof(*bmp));
55*0124a941Schristos bmp->mq_flags = cvtto_bsd_mask(lmp->mq_flags, LINUX_O_NONBLOCK,
56*0124a941Schristos O_NONBLOCK);
57*0124a941Schristos bmp->mq_maxmsg = lmp->mq_maxmsg;
58*0124a941Schristos bmp->mq_msgsize = lmp->mq_msgsize;
59*0124a941Schristos bmp->mq_curmsgs = lmp->mq_curmsgs;
60*0124a941Schristos }
61*0124a941Schristos
62*0124a941Schristos static void
bsd_to_linux_mq_attr(const struct mq_attr * bmp,struct linux_mq_attr * lmp)63*0124a941Schristos bsd_to_linux_mq_attr(const struct mq_attr *bmp, struct linux_mq_attr *lmp)
64*0124a941Schristos {
65*0124a941Schristos memset(lmp, 0, sizeof(*lmp));
66*0124a941Schristos lmp->mq_flags = cvtto_linux_mask(bmp->mq_flags, O_NONBLOCK,
67*0124a941Schristos LINUX_O_NONBLOCK);
68*0124a941Schristos lmp->mq_maxmsg = bmp->mq_maxmsg;
69*0124a941Schristos lmp->mq_msgsize = bmp->mq_msgsize;
70*0124a941Schristos lmp->mq_curmsgs = bmp->mq_curmsgs;
71*0124a941Schristos }
72*0124a941Schristos
73*0124a941Schristos /* Adapted from sys_mq_open */
74*0124a941Schristos int
linux_sys_mq_open(struct lwp * l,const struct linux_sys_mq_open_args * uap,register_t * retval)75*0124a941Schristos linux_sys_mq_open(struct lwp *l, const struct linux_sys_mq_open_args *uap,
76*0124a941Schristos register_t *retval)
77*0124a941Schristos {
78*0124a941Schristos /* {
79*0124a941Schristos syscallarg(const char *) name;
80*0124a941Schristos syscallarg(int) oflag;
81*0124a941Schristos syscallarg(linux_umode_t) mode;
82*0124a941Schristos syscallarg(struct linux_mq_attr *) attr;
83*0124a941Schristos } */
84*0124a941Schristos struct linux_mq_attr lattr;
85*0124a941Schristos struct mq_attr *attr = NULL, a;
86*0124a941Schristos int error, oflag;
87*0124a941Schristos
88*0124a941Schristos oflag = linux_to_bsd_ioflags(SCARG(uap, oflag));
89*0124a941Schristos
90*0124a941Schristos if ((oflag & O_CREAT) != 0 && SCARG(uap, attr) != NULL) {
91*0124a941Schristos error = copyin(SCARG(uap, attr), &lattr, sizeof(lattr));
92*0124a941Schristos if (error)
93*0124a941Schristos return error;
94*0124a941Schristos linux_to_bsd_mq_attr(&lattr, &a);
95*0124a941Schristos attr = &a;
96*0124a941Schristos }
97*0124a941Schristos
98*0124a941Schristos return mq_handle_open(l, SCARG(uap, name), oflag,
99*0124a941Schristos (mode_t)SCARG(uap, mode), attr, retval);
100*0124a941Schristos }
101*0124a941Schristos
102*0124a941Schristos int
linux_sys_mq_unlink(struct lwp * l,const struct linux_sys_mq_unlink_args * uap,register_t * retval)103*0124a941Schristos linux_sys_mq_unlink(struct lwp *l, const struct linux_sys_mq_unlink_args *uap,
104*0124a941Schristos register_t *retval)
105*0124a941Schristos {
106*0124a941Schristos /* {
107*0124a941Schristos syscallarg(const char *) name;
108*0124a941Schristos } */
109*0124a941Schristos struct sys_mq_unlink_args args;
110*0124a941Schristos
111*0124a941Schristos SCARG(&args, name) = SCARG(uap, name);
112*0124a941Schristos
113*0124a941Schristos return sys_mq_unlink(l, &args, retval);
114*0124a941Schristos }
115*0124a941Schristos
116*0124a941Schristos /* Adapted from sys___mq_timedsend50 */
117*0124a941Schristos int
linux_sys_mq_timedsend(struct lwp * l,const struct linux_sys_mq_timedsend_args * uap,register_t * retval)118*0124a941Schristos linux_sys_mq_timedsend(struct lwp *l, const struct linux_sys_mq_timedsend_args *uap,
119*0124a941Schristos register_t *retval)
120*0124a941Schristos {
121*0124a941Schristos /* {
122*0124a941Schristos syscallarg(linux_mqd_t) mqdes;
123*0124a941Schristos syscallarg(const char *) msg_ptr;
124*0124a941Schristos syscallarg(size_t) msg_len;
125*0124a941Schristos syscallarg(unsigned int) msg_prio;
126*0124a941Schristos syscallarg(const struct linux_timespec *) abs_timeout;
127*0124a941Schristos } */
128*0124a941Schristos struct linux_timespec lts;
129*0124a941Schristos struct timespec ts, *tsp;
130*0124a941Schristos int error;
131*0124a941Schristos
132*0124a941Schristos /* Get and convert time value */
133*0124a941Schristos if (SCARG(uap, abs_timeout)) {
134*0124a941Schristos error = copyin(SCARG(uap, abs_timeout), <s, sizeof(lts));
135*0124a941Schristos if (error)
136*0124a941Schristos return error;
137*0124a941Schristos linux_to_native_timespec(&ts, <s);
138*0124a941Schristos tsp = &ts;
139*0124a941Schristos } else {
140*0124a941Schristos tsp = NULL;
141*0124a941Schristos }
142*0124a941Schristos
143*0124a941Schristos return mq_send1((mqd_t)SCARG(uap, mqdes), SCARG(uap, msg_ptr),
144*0124a941Schristos SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
145*0124a941Schristos }
146*0124a941Schristos
147*0124a941Schristos /* Adapted from sys___mq_timedreceive50 */
148*0124a941Schristos int
linux_sys_mq_timedreceive(struct lwp * l,const struct linux_sys_mq_timedreceive_args * uap,register_t * retval)149*0124a941Schristos linux_sys_mq_timedreceive(struct lwp *l,
150*0124a941Schristos const struct linux_sys_mq_timedreceive_args *uap, register_t *retval)
151*0124a941Schristos {
152*0124a941Schristos /* {
153*0124a941Schristos syscallarg(linux_mqd_t) mqdes;
154*0124a941Schristos syscallarg(char *) msg_ptr;
155*0124a941Schristos syscallarg(size_t) msg_len;
156*0124a941Schristos syscallarg(unsigned int *) msg_prio;
157*0124a941Schristos syscallarg(const struct linux_timespec *) abs_timeout;
158*0124a941Schristos }; */
159*0124a941Schristos struct linux_timespec lts;
160*0124a941Schristos struct timespec ts, *tsp;
161*0124a941Schristos ssize_t mlen;
162*0124a941Schristos int error;
163*0124a941Schristos
164*0124a941Schristos /* Get and convert time value */
165*0124a941Schristos if (SCARG(uap, abs_timeout)) {
166*0124a941Schristos error = copyin(SCARG(uap, abs_timeout), <s, sizeof(lts));
167*0124a941Schristos if (error)
168*0124a941Schristos return error;
169*0124a941Schristos linux_to_native_timespec(&ts, <s);
170*0124a941Schristos tsp = &ts;
171*0124a941Schristos } else {
172*0124a941Schristos tsp = NULL;
173*0124a941Schristos }
174*0124a941Schristos
175*0124a941Schristos error = mq_recv1((mqd_t)SCARG(uap, mqdes), SCARG(uap, msg_ptr),
176*0124a941Schristos SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
177*0124a941Schristos if (error == 0)
178*0124a941Schristos *retval = mlen;
179*0124a941Schristos
180*0124a941Schristos return error;
181*0124a941Schristos }
182*0124a941Schristos
183*0124a941Schristos /* Adapted from sys_mq_notify */
184*0124a941Schristos int
linux_sys_mq_notify(struct lwp * l,const struct linux_sys_mq_notify_args * uap,register_t * retval)185*0124a941Schristos linux_sys_mq_notify(struct lwp *l, const struct linux_sys_mq_notify_args *uap,
186*0124a941Schristos register_t *retval)
187*0124a941Schristos {
188*0124a941Schristos /* {
189*0124a941Schristos syscallarg(linux_mqd_t) mqdes;
190*0124a941Schristos syscallarg(const struct linux_sigevent *) sevp;
191*0124a941Schristos } */
192*0124a941Schristos struct mqueue *mq;
193*0124a941Schristos struct sigevent sig;
194*0124a941Schristos int error;
195*0124a941Schristos
196*0124a941Schristos if (SCARG(uap, sevp)) {
197*0124a941Schristos /* Get the signal from user-space */
198*0124a941Schristos error = linux_sigevent_copyin(SCARG(uap, sevp), &sig,
199*0124a941Schristos sizeof(sig));
200*0124a941Schristos if (error)
201*0124a941Schristos return error;
202*0124a941Schristos if (sig.sigev_notify == SIGEV_SIGNAL &&
203*0124a941Schristos (sig.sigev_signo <= 0 || sig.sigev_signo >= NSIG))
204*0124a941Schristos return EINVAL;
205*0124a941Schristos }
206*0124a941Schristos
207*0124a941Schristos error = mqueue_get((mqd_t)SCARG(uap, mqdes), 0, &mq);
208*0124a941Schristos if (error)
209*0124a941Schristos return error;
210*0124a941Schristos if (SCARG(uap, sevp)) {
211*0124a941Schristos /* Register notification: set the signal and target process */
212*0124a941Schristos if (mq->mq_notify_proc == NULL) {
213*0124a941Schristos memcpy(&mq->mq_sig_notify, &sig,
214*0124a941Schristos sizeof(struct sigevent));
215*0124a941Schristos mq->mq_notify_proc = l->l_proc;
216*0124a941Schristos } else {
217*0124a941Schristos /* Fail if someone else already registered */
218*0124a941Schristos error = EBUSY;
219*0124a941Schristos }
220*0124a941Schristos } else {
221*0124a941Schristos /* Unregister the notification */
222*0124a941Schristos mq->mq_notify_proc = NULL;
223*0124a941Schristos }
224*0124a941Schristos mutex_exit(&mq->mq_mtx);
225*0124a941Schristos fd_putfile((int)SCARG(uap, mqdes));
226*0124a941Schristos
227*0124a941Schristos return error;
228*0124a941Schristos }
229*0124a941Schristos
230*0124a941Schristos /* Adapted from sys_mq_getattr */
231*0124a941Schristos static int
linux_sys_mq_getattr(struct lwp * l,const struct linux_sys_mq_getsetattr_args * uap,register_t * retval)232*0124a941Schristos linux_sys_mq_getattr(struct lwp *l,
233*0124a941Schristos const struct linux_sys_mq_getsetattr_args *uap, register_t *retval)
234*0124a941Schristos {
235*0124a941Schristos /* {
236*0124a941Schristos syscallarg(linux_mqd_t) mqdes;
237*0124a941Schristos syscallarg(const struct linux_mq_attr *) newattr;
238*0124a941Schristos syscallarg(struct linux_mq_attr *) oldattr;
239*0124a941Schristos } */
240*0124a941Schristos struct linux_mq_attr lattr;
241*0124a941Schristos struct mq_attr attr;
242*0124a941Schristos struct mqueue *mq;
243*0124a941Schristos int error;
244*0124a941Schristos
245*0124a941Schristos error = mqueue_get((mqd_t)SCARG(uap, mqdes), 0, &mq);
246*0124a941Schristos if (error)
247*0124a941Schristos return error;
248*0124a941Schristos memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
249*0124a941Schristos bsd_to_linux_mq_attr(&attr, &lattr);
250*0124a941Schristos mutex_exit(&mq->mq_mtx);
251*0124a941Schristos fd_putfile((int)SCARG(uap, mqdes));
252*0124a941Schristos
253*0124a941Schristos return copyout(&lattr, SCARG(uap, oldattr), sizeof(lattr));
254*0124a941Schristos }
255*0124a941Schristos
256*0124a941Schristos /* Adapted from sys_mq_setattr */
257*0124a941Schristos static int
linux_sys_mq_setattr(struct lwp * l,const struct linux_sys_mq_getsetattr_args * uap,register_t * retval)258*0124a941Schristos linux_sys_mq_setattr(struct lwp *l,
259*0124a941Schristos const struct linux_sys_mq_getsetattr_args *uap, register_t *retval)
260*0124a941Schristos {
261*0124a941Schristos /* {
262*0124a941Schristos syscallarg(linux_mqd_t) mqdes;
263*0124a941Schristos syscallarg(const struct linux_mq_attr *) newattr;
264*0124a941Schristos syscallarg(struct linux_mq_attr *) oldattr;
265*0124a941Schristos } */
266*0124a941Schristos struct linux_mq_attr lattr;
267*0124a941Schristos struct mq_attr attr;
268*0124a941Schristos struct mqueue *mq;
269*0124a941Schristos int error, nonblock;
270*0124a941Schristos
271*0124a941Schristos error = copyin(SCARG(uap, newattr), &lattr, sizeof(lattr));
272*0124a941Schristos if (error)
273*0124a941Schristos return error;
274*0124a941Schristos linux_to_bsd_mq_attr(&lattr, &attr);
275*0124a941Schristos nonblock = (attr.mq_flags & O_NONBLOCK);
276*0124a941Schristos
277*0124a941Schristos error = mqueue_get((mqd_t)SCARG(uap, mqdes), 0, &mq);
278*0124a941Schristos if (error)
279*0124a941Schristos return error;
280*0124a941Schristos
281*0124a941Schristos /* Copy the old attributes, if needed */
282*0124a941Schristos if (SCARG(uap, oldattr)) {
283*0124a941Schristos memcpy(&attr, &mq->mq_attrib, sizeof(struct mq_attr));
284*0124a941Schristos bsd_to_linux_mq_attr(&attr, &lattr);
285*0124a941Schristos }
286*0124a941Schristos
287*0124a941Schristos /* Ignore everything, except O_NONBLOCK */
288*0124a941Schristos if (nonblock)
289*0124a941Schristos mq->mq_attrib.mq_flags |= O_NONBLOCK;
290*0124a941Schristos else
291*0124a941Schristos mq->mq_attrib.mq_flags &= ~O_NONBLOCK;
292*0124a941Schristos
293*0124a941Schristos mutex_exit(&mq->mq_mtx);
294*0124a941Schristos fd_putfile((int)SCARG(uap, mqdes));
295*0124a941Schristos
296*0124a941Schristos /* Copy the data to the user-space. */
297*0124a941Schristos if (SCARG(uap, oldattr))
298*0124a941Schristos return copyout(&lattr, SCARG(uap, oldattr), sizeof(lattr));
299*0124a941Schristos
300*0124a941Schristos return 0;
301*0124a941Schristos }
302*0124a941Schristos
303*0124a941Schristos int
linux_sys_mq_getsetattr(struct lwp * l,const struct linux_sys_mq_getsetattr_args * uap,register_t * retval)304*0124a941Schristos linux_sys_mq_getsetattr(struct lwp *l,
305*0124a941Schristos const struct linux_sys_mq_getsetattr_args *uap, register_t *retval)
306*0124a941Schristos {
307*0124a941Schristos /* {
308*0124a941Schristos syscallarg(linux_mqd_t) mqdes;
309*0124a941Schristos syscallarg(const struct linux_mq_attr *) newattr;
310*0124a941Schristos syscallarg(struct linux_mq_attr *) oldattr;
311*0124a941Schristos } */
312*0124a941Schristos if (SCARG(uap, newattr) == NULL)
313*0124a941Schristos return linux_sys_mq_getattr(l, uap, retval);
314*0124a941Schristos return linux_sys_mq_setattr(l, uap, retval);
315*0124a941Schristos }
316