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