netbsd32_compat_14_sysv.c revision 1.1.2.2 1 /* $NetBSD: netbsd32_compat_14_sysv.c,v 1.1.2.2 2019/01/03 10:57:32 pgoyette Exp $ */
2
3 /*
4 * Copyright (c) 1999 Eduardo E. Horvath
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 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_14_sysv.c,v 1.1.2.2 2019/01/03 10:57:32 pgoyette Exp $");
33
34 #ifdef _KERNEL_OPT
35 #include "opt_sysv.h"
36 #include "opt_compat_netbsd.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/ipc.h>
41 #include <sys/systm.h>
42 #include <sys/module.h>
43 #include <sys/signal.h>
44 #include <sys/proc.h>
45 #include <sys/mount.h>
46 #include <sys/msg.h>
47 #include <sys/sem.h>
48 #include <sys/shm.h>
49
50 #include <sys/syscallargs.h>
51 #include <sys/syscallvar.h>
52
53 #include <compat/netbsd32/netbsd32.h>
54 #include <compat/netbsd32/netbsd32_syscall.h>
55 #include <compat/netbsd32/netbsd32_syscallargs.h>
56 #include <compat/sys/siginfo.h>
57 #include <compat/sys/shm.h>
58
59 #if defined(COMPAT_14)
60
61 #if defined(SYSVMSG)
62 static inline void
63 netbsd32_ipc_perm14_to_native(struct netbsd32_ipc_perm14 *operm, struct ipc_perm *perm)
64 {
65
66 #define CVT(x) perm->x = operm->x
67 CVT(uid);
68 CVT(gid);
69 CVT(cuid);
70 CVT(cgid);
71 CVT(mode);
72 #undef CVT
73 }
74
75 static inline void
76 native_to_netbsd32_ipc_perm14(struct ipc_perm *perm, struct netbsd32_ipc_perm14 *operm)
77 {
78
79 #define CVT(x) operm->x = perm->x
80 CVT(uid);
81 CVT(gid);
82 CVT(cuid);
83 CVT(cgid);
84 CVT(mode);
85 #undef CVT
86
87 /*
88 * Not part of the API, but some programs might look at it.
89 */
90 operm->seq = perm->_seq;
91 operm->key = (key_t)perm->_key;
92 }
93
94 static inline void
95 netbsd32_msqid_ds14_to_native(struct netbsd32_msqid_ds14 *omsqbuf, struct msqid_ds *msqbuf)
96 {
97
98 netbsd32_ipc_perm14_to_native(&omsqbuf->msg_perm, &msqbuf->msg_perm);
99
100 #define CVT(x) msqbuf->x = omsqbuf->x
101 CVT(msg_qnum);
102 CVT(msg_qbytes);
103 CVT(msg_lspid);
104 CVT(msg_lrpid);
105 CVT(msg_stime);
106 CVT(msg_rtime);
107 CVT(msg_ctime);
108 #undef CVT
109 }
110
111 static inline void
112 native_to_netbsd32_msqid_ds14(struct msqid_ds *msqbuf, struct netbsd32_msqid_ds14 *omsqbuf)
113 {
114
115 memset(omsqbuf, 0, sizeof(*omsqbuf));
116 native_to_netbsd32_ipc_perm14(&msqbuf->msg_perm, &omsqbuf->msg_perm);
117
118 #define CVT(x) omsqbuf->x = msqbuf->x
119 CVT(msg_qnum);
120 CVT(msg_qbytes);
121 CVT(msg_lspid);
122 CVT(msg_lrpid);
123 CVT(msg_stime);
124 CVT(msg_rtime);
125 CVT(msg_ctime);
126 #undef CVT
127
128 /*
129 * Not part of the API, but some programs might look at it.
130 */
131 omsqbuf->msg_cbytes = msqbuf->_msg_cbytes;
132 }
133 #endif
134
135 #if defined(SYSVSEM)
136 static inline void
137 netbsd32_semid_ds14_to_native(struct netbsd32_semid_ds14 *osembuf, struct semid_ds *sembuf)
138 {
139
140 netbsd32_ipc_perm14_to_native(&osembuf->sem_perm, &sembuf->sem_perm);
141
142 #define CVT(x) sembuf->x = osembuf->x
143 CVT(sem_nsems);
144 CVT(sem_otime);
145 CVT(sem_ctime);
146 #undef CVT
147 }
148
149 static inline void
150 native_to_netbsd32_semid_ds14(struct semid_ds *sembuf, struct netbsd32_semid_ds14 *osembuf)
151 {
152
153 native_to_netbsd32_ipc_perm14(&sembuf->sem_perm, &osembuf->sem_perm);
154
155 #define CVT(x) osembuf->x = sembuf->x
156 CVT(sem_nsems);
157 CVT(sem_otime);
158 CVT(sem_ctime);
159 #undef CVT
160 }
161
162 static inline void
163 netbsd32_shmid_ds14_to_native(struct netbsd32_shmid_ds14 *oshmbuf, struct shmid_ds *shmbuf)
164 {
165
166 netbsd32_ipc_perm14_to_native(&oshmbuf->shm_perm, &shmbuf->shm_perm);
167
168 #define CVT(x) shmbuf->x = oshmbuf->x
169 CVT(shm_segsz);
170 CVT(shm_lpid);
171 CVT(shm_cpid);
172 CVT(shm_nattch);
173 CVT(shm_atime);
174 CVT(shm_dtime);
175 CVT(shm_ctime);
176 #undef CVT
177 }
178
179 static inline void
180 native_to_netbsd32_shmid_ds14(struct shmid_ds *shmbuf, struct netbsd32_shmid_ds14 *oshmbuf)
181 {
182
183 native_to_netbsd32_ipc_perm14(&shmbuf->shm_perm, &oshmbuf->shm_perm);
184
185 #define CVT(x) oshmbuf->x = shmbuf->x
186 CVT(shm_segsz);
187 CVT(shm_lpid);
188 CVT(shm_cpid);
189 CVT(shm_nattch);
190 CVT(shm_atime);
191 CVT(shm_dtime);
192 CVT(shm_ctime);
193 #undef CVT
194 }
195
196 /*
197 * the compat_14 system calls
198 */
199 int
200 compat_14_netbsd32_msgctl(struct lwp *l, const struct compat_14_netbsd32_msgctl_args *uap, register_t *retval)
201 {
202 /* {
203 syscallarg(int) msqid;
204 syscallarg(int) cmd;
205 syscallarg(struct msqid_ds14 *) buf;
206 } */
207 struct msqid_ds msqbuf;
208 struct netbsd32_msqid_ds14 omsqbuf;
209 int cmd, error;
210
211 cmd = SCARG(uap, cmd);
212
213 if (cmd == IPC_SET) {
214 error = copyin(SCARG_P32(uap, buf),
215 &omsqbuf, sizeof(omsqbuf));
216 if (error)
217 return (error);
218 netbsd32_msqid_ds14_to_native(&omsqbuf, &msqbuf);
219 }
220
221 error = msgctl1(l, SCARG(uap, msqid), cmd,
222 (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
223
224 if (error == 0 && cmd == IPC_STAT) {
225 native_to_netbsd32_msqid_ds14(&msqbuf, &omsqbuf);
226 error = copyout(&omsqbuf,
227 SCARG_P32(uap, buf), sizeof(omsqbuf));
228 }
229
230 return (error);
231 }
232 #endif
233
234 #if defined(SYSVSEM)
235 int
236 compat_14_netbsd32___semctl(struct lwp *l, const struct compat_14_netbsd32___semctl_args *uap, register_t *retval)
237 {
238 /* {
239 syscallarg(int) semid;
240 syscallarg(int) semnum;
241 syscallarg(int) cmd;
242 syscallarg(union __semun *) arg;
243 } */
244 union __semun arg;
245 struct semid_ds sembuf;
246 struct netbsd32_semid_ds14 osembuf;
247 int cmd, error;
248 void *pass_arg = NULL;
249
250 cmd = SCARG(uap, cmd);
251
252 switch (cmd) {
253 case IPC_SET:
254 case IPC_STAT:
255 pass_arg = &sembuf;
256 break;
257
258 case GETALL:
259 case SETVAL:
260 case SETALL:
261 pass_arg = &arg;
262 break;
263 }
264
265 if (pass_arg != NULL) {
266 error = copyin(NETBSD32IPTR64(SCARG(uap, arg)), &arg,
267 sizeof(arg));
268 if (error)
269 return (error);
270 if (cmd == IPC_SET) {
271 error = copyin(arg.buf, &osembuf, sizeof(osembuf));
272 if (error)
273 return (error);
274 netbsd32_semid_ds14_to_native(&osembuf, &sembuf);
275 }
276 }
277
278 error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
279 pass_arg, retval);
280
281 if (error == 0 && cmd == IPC_STAT) {
282 native_to_netbsd32_semid_ds14(&sembuf, &osembuf);
283 error = copyout(&osembuf, arg.buf, sizeof(osembuf));
284 }
285
286 return (error);
287 }
288 #endif
289
290 #if defined(SYSVSHM)
291 int
292 compat_14_netbsd32_shmctl(struct lwp *l, const struct compat_14_netbsd32_shmctl_args *uap, register_t *retval)
293 {
294 /* {
295 syscallarg(int) shmid;
296 syscallarg(int) cmd;
297 syscallarg(struct netbsd32_shmid_ds14 *) buf;
298 } */
299 struct shmid_ds shmbuf;
300 struct netbsd32_shmid_ds14 oshmbuf;
301 int cmd, error;
302
303 cmd = SCARG(uap, cmd);
304
305 if (cmd == IPC_SET) {
306 error = copyin(SCARG_P32(uap, buf), &oshmbuf, sizeof(oshmbuf));
307 if (error)
308 return (error);
309 netbsd32_shmid_ds14_to_native(&oshmbuf, &shmbuf);
310 }
311
312 error = shmctl1(l, SCARG(uap, shmid), cmd,
313 (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
314
315 if (error == 0 && cmd == IPC_STAT) {
316 native_to_netbsd32_shmid_ds14(&shmbuf, &oshmbuf);
317 error = copyout(&oshmbuf, SCARG_P32(uap, buf), sizeof(oshmbuf));
318 }
319
320 return (error);
321 }
322 #endif
323
324 #define REQ1 "sysv_ipc,compat_sysv_14,"
325 #define REQ2 "compat_netbsd32,compat_netbsd32_sysvipc,"
326 #define REQ3 "compat_netbsd32_sysvipc_50"
327
328 #define _PKG_ENTRY(name) \
329 { NETBSD32_SYS_ ## name, 0, (sy_call_t *)name }
330
331 static const struct syscall_package compat_sysvipc_14_syscalls[] = {
332 #if defined(SYSVSEM)
333 _PKG_ENTRY(compat_14_netbsd32___semctl),
334 #endif
335 #if defined(SYSVSHM)
336 _PKG_ENTRY(compat_14_netbsd32_shmctl),
337 #endif
338 #if defined(SYSVMSG)
339 _PKG_ENTRY(compat_14_netbsd32_msgctl),
340 #endif
341 { 0, 0, NULL }
342 };
343
344 #define REQ1 "sysv_ipc,compat_sysv_14,"
345 #define REQ2 "compat_netbsd32,compat_netbsd32_sysvipc,"
346 #define REQ3 "compat_netbsd32_sysvipc_50"
347
348 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_sysvipc_14, REQ1 REQ2 REQ3 );
349
350 static int
351 compat_netbsd32_sysvipc_14_modcmd(modcmd_t cmd, void *arg)
352 {
353
354 switch (cmd) {
355 case MODULE_CMD_INIT:
356 return syscall_establish(&emul_netbsd32,
357 compat_sysvipc_14_syscalls);
358
359 case MODULE_CMD_FINI:
360 return syscall_disestablish(&emul_netbsd32,
361 compat_sysvipc_14_syscalls);
362
363 default:
364 return ENOTTY;
365 }
366 }
367
368 #endif /* COMPAT_14 */
369