1 /*        $NetBSD: netbsd32_compat_14_sysv.c,v 1.4 2021/01/19 03:20:13 simonb 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.4 2021/01/19 03:20:13 simonb 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
netbsd32_ipc_perm14_to_native(struct netbsd32_ipc_perm14 * operm,struct ipc_perm * perm)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
native_to_netbsd32_ipc_perm14(struct ipc_perm * perm,struct netbsd32_ipc_perm14 * operm)76 native_to_netbsd32_ipc_perm14(struct ipc_perm *perm, struct netbsd32_ipc_perm14 *operm)
77 {
78 
79           memset(operm, 0, sizeof *operm);
80 #define   CVT(x)    operm->x = perm->x
81           CVT(uid);
82           CVT(gid);
83           CVT(cuid);
84           CVT(cgid);
85           CVT(mode);
86 #undef CVT
87 
88           /*
89            * Not part of the API, but some programs might look at it.
90            */
91           operm->seq = perm->_seq;
92           operm->key = (key_t)perm->_key;
93 }
94 
95 static inline void
netbsd32_msqid_ds14_to_native(struct netbsd32_msqid_ds14 * omsqbuf,struct msqid_ds * msqbuf)96 netbsd32_msqid_ds14_to_native(struct netbsd32_msqid_ds14 *omsqbuf, struct msqid_ds *msqbuf)
97 {
98 
99           netbsd32_ipc_perm14_to_native(&omsqbuf->msg_perm, &msqbuf->msg_perm);
100 
101 #define   CVT(x)    msqbuf->x = omsqbuf->x
102           CVT(msg_qnum);
103           CVT(msg_qbytes);
104           CVT(msg_lspid);
105           CVT(msg_lrpid);
106           CVT(msg_stime);
107           CVT(msg_rtime);
108           CVT(msg_ctime);
109 #undef CVT
110 }
111 
112 static inline void
native_to_netbsd32_msqid_ds14(struct msqid_ds * msqbuf,struct netbsd32_msqid_ds14 * omsqbuf)113 native_to_netbsd32_msqid_ds14(struct msqid_ds *msqbuf, struct netbsd32_msqid_ds14 *omsqbuf)
114 {
115 
116           memset(omsqbuf, 0, sizeof *omsqbuf);
117           native_to_netbsd32_ipc_perm14(&msqbuf->msg_perm, &omsqbuf->msg_perm);
118 
119 #define   CVT(x)    omsqbuf->x = msqbuf->x
120           CVT(msg_qnum);
121           CVT(msg_qbytes);
122           CVT(msg_lspid);
123           CVT(msg_lrpid);
124           CVT(msg_stime);
125           CVT(msg_rtime);
126           CVT(msg_ctime);
127 #undef CVT
128 
129           /*
130            * Not part of the API, but some programs might look at it.
131            */
132           omsqbuf->msg_cbytes = msqbuf->_msg_cbytes;
133 }
134 #endif
135 
136 #if defined(SYSVSEM)
137 static inline void
netbsd32_semid_ds14_to_native(struct netbsd32_semid_ds14 * osembuf,struct semid_ds * sembuf)138 netbsd32_semid_ds14_to_native(struct netbsd32_semid_ds14 *osembuf, struct semid_ds *sembuf)
139 {
140 
141           netbsd32_ipc_perm14_to_native(&osembuf->sem_perm, &sembuf->sem_perm);
142 
143 #define   CVT(x)    sembuf->x = osembuf->x
144           CVT(sem_nsems);
145           CVT(sem_otime);
146           CVT(sem_ctime);
147 #undef CVT
148 }
149 
150 static inline void
native_to_netbsd32_semid_ds14(struct semid_ds * sembuf,struct netbsd32_semid_ds14 * osembuf)151 native_to_netbsd32_semid_ds14(struct semid_ds *sembuf, struct netbsd32_semid_ds14 *osembuf)
152 {
153 
154           memset(osembuf, 0, sizeof *osembuf);
155           native_to_netbsd32_ipc_perm14(&sembuf->sem_perm, &osembuf->sem_perm);
156 
157 #define   CVT(x)    osembuf->x = sembuf->x
158           CVT(sem_nsems);
159           CVT(sem_otime);
160           CVT(sem_ctime);
161 #undef CVT
162 }
163 
164 static inline void
netbsd32_shmid_ds14_to_native(struct netbsd32_shmid_ds14 * oshmbuf,struct shmid_ds * shmbuf)165 netbsd32_shmid_ds14_to_native(struct netbsd32_shmid_ds14 *oshmbuf, struct shmid_ds *shmbuf)
166 {
167 
168           netbsd32_ipc_perm14_to_native(&oshmbuf->shm_perm, &shmbuf->shm_perm);
169 
170 #define   CVT(x)    shmbuf->x = oshmbuf->x
171           CVT(shm_segsz);
172           CVT(shm_lpid);
173           CVT(shm_cpid);
174           CVT(shm_nattch);
175           CVT(shm_atime);
176           CVT(shm_dtime);
177           CVT(shm_ctime);
178 #undef CVT
179 }
180 
181 static inline void
native_to_netbsd32_shmid_ds14(struct shmid_ds * shmbuf,struct netbsd32_shmid_ds14 * oshmbuf)182 native_to_netbsd32_shmid_ds14(struct shmid_ds *shmbuf, struct netbsd32_shmid_ds14 *oshmbuf)
183 {
184 
185           memset(oshmbuf, 0, sizeof *oshmbuf);
186           native_to_netbsd32_ipc_perm14(&shmbuf->shm_perm, &oshmbuf->shm_perm);
187 
188 #define   CVT(x)    oshmbuf->x = shmbuf->x
189           CVT(shm_segsz);
190           CVT(shm_lpid);
191           CVT(shm_cpid);
192           CVT(shm_nattch);
193           CVT(shm_atime);
194           CVT(shm_dtime);
195           CVT(shm_ctime);
196 #undef CVT
197 }
198 
199 /*
200  * the compat_14 system calls
201  */
202 int
compat_14_netbsd32_msgctl(struct lwp * l,const struct compat_14_netbsd32_msgctl_args * uap,register_t * retval)203 compat_14_netbsd32_msgctl(struct lwp *l, const struct compat_14_netbsd32_msgctl_args *uap, register_t *retval)
204 {
205           /* {
206                     syscallarg(int) msqid;
207                     syscallarg(int) cmd;
208                     syscallarg(struct msqid_ds14 *) buf;
209           } */
210           struct msqid_ds msqbuf;
211           struct netbsd32_msqid_ds14 omsqbuf;
212           int cmd, error;
213 
214           cmd = SCARG(uap, cmd);
215 
216           if (cmd == IPC_SET) {
217                     error = copyin(SCARG_P32(uap, buf),
218                         &omsqbuf, sizeof(omsqbuf));
219                     if (error)
220                               return error;
221                     netbsd32_msqid_ds14_to_native(&omsqbuf, &msqbuf);
222           }
223 
224           error = msgctl1(l, SCARG(uap, msqid), cmd,
225               (cmd == IPC_SET || cmd == IPC_STAT) ? &msqbuf : NULL);
226 
227           if (error == 0 && cmd == IPC_STAT) {
228                     native_to_netbsd32_msqid_ds14(&msqbuf, &omsqbuf);
229                     error = copyout(&omsqbuf,
230                         SCARG_P32(uap, buf), sizeof(omsqbuf));
231           }
232 
233           return error;
234 }
235 #endif
236 
237 #if defined(SYSVSEM)
238 int
compat_14_netbsd32___semctl(struct lwp * l,const struct compat_14_netbsd32___semctl_args * uap,register_t * retval)239 compat_14_netbsd32___semctl(struct lwp *l, const struct compat_14_netbsd32___semctl_args *uap, register_t *retval)
240 {
241           /* {
242                     syscallarg(int) semid;
243                     syscallarg(int) semnum;
244                     syscallarg(int) cmd;
245                     syscallarg(union __semun *) arg;
246           } */
247           union __semun arg;
248           struct semid_ds sembuf;
249           struct netbsd32_semid_ds14 osembuf;
250           int cmd, error;
251           void *pass_arg = NULL;
252 
253           cmd = SCARG(uap, cmd);
254 
255           switch (cmd) {
256           case IPC_SET:
257           case IPC_STAT:
258                     pass_arg = &sembuf;
259                     break;
260 
261           case GETALL:
262           case SETVAL:
263           case SETALL:
264                     pass_arg = &arg;
265                     break;
266           }
267 
268           if (pass_arg != NULL) {
269                     error = copyin(NETBSD32IPTR64(SCARG(uap, arg)), &arg,
270                         sizeof(arg));
271                     if (error)
272                               return error;
273                     if (cmd == IPC_SET) {
274                               error = copyin(arg.buf, &osembuf, sizeof(osembuf));
275                               if (error)
276                                         return error;
277                               netbsd32_semid_ds14_to_native(&osembuf, &sembuf);
278                     }
279           }
280 
281           error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
282               pass_arg, retval);
283 
284           if (error == 0 && cmd == IPC_STAT) {
285                     native_to_netbsd32_semid_ds14(&sembuf, &osembuf);
286                     error = copyout(&osembuf, arg.buf, sizeof(osembuf));
287           }
288 
289           return error;
290 }
291 #endif
292 
293 #if defined(SYSVSHM)
294 int
compat_14_netbsd32_shmctl(struct lwp * l,const struct compat_14_netbsd32_shmctl_args * uap,register_t * retval)295 compat_14_netbsd32_shmctl(struct lwp *l, const struct compat_14_netbsd32_shmctl_args *uap, register_t *retval)
296 {
297           /* {
298                     syscallarg(int) shmid;
299                     syscallarg(int) cmd;
300                     syscallarg(struct netbsd32_shmid_ds14 *) buf;
301           } */
302           struct shmid_ds shmbuf;
303           struct netbsd32_shmid_ds14 oshmbuf;
304           int cmd, error;
305 
306           cmd = SCARG(uap, cmd);
307 
308           if (cmd == IPC_SET) {
309                     error = copyin(SCARG_P32(uap, buf), &oshmbuf, sizeof(oshmbuf));
310                     if (error)
311                               return error;
312                     netbsd32_shmid_ds14_to_native(&oshmbuf, &shmbuf);
313           }
314 
315           error = shmctl1(l, SCARG(uap, shmid), cmd,
316               (cmd == IPC_SET || cmd == IPC_STAT) ? &shmbuf : NULL);
317 
318           if (error == 0 && cmd == IPC_STAT) {
319                     native_to_netbsd32_shmid_ds14(&shmbuf, &oshmbuf);
320                     error = copyout(&oshmbuf, SCARG_P32(uap, buf), sizeof(oshmbuf));
321           }
322 
323           return error;
324 }
325 #endif
326 
327 #define REQ1    "sysv_ipc,compat_sysv_14,"
328 #define REQ2    "compat_netbsd32,compat_netbsd32_sysvipc,"
329 #define REQ3    "compat_netbsd32_sysvipc_50"
330 
331 #define _PKG_ENTRY(name)        \
332           { NETBSD32_SYS_ ## name, 0, (sy_call_t *)name }
333 
334 static const struct syscall_package compat_sysvipc_14_syscalls[] = {
335 #if defined(SYSVSEM)
336           _PKG_ENTRY(compat_14_netbsd32___semctl),
337 #endif
338 #if defined(SYSVSHM)
339           _PKG_ENTRY(compat_14_netbsd32_shmctl),
340 #endif
341 #if defined(SYSVMSG)
342           _PKG_ENTRY(compat_14_netbsd32_msgctl),
343 #endif
344           { 0, 0, NULL }
345 };
346 
347 #define REQ1    "sysv_ipc,compat_sysv_14,"
348 #define REQ2    "compat_netbsd32,compat_netbsd32_sysvipc,"
349 #define REQ3    "compat_netbsd32_sysvipc_50"
350 
351 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_sysvipc_14, REQ1 REQ2 REQ3 );
352 
353 static int
compat_netbsd32_sysvipc_14_modcmd(modcmd_t cmd,void * arg)354 compat_netbsd32_sysvipc_14_modcmd(modcmd_t cmd, void *arg)
355 {
356 
357           switch (cmd) {
358           case MODULE_CMD_INIT:
359                     return syscall_establish(&emul_netbsd32,
360                         compat_sysvipc_14_syscalls);
361 
362           case MODULE_CMD_FINI:
363                     return syscall_disestablish(&emul_netbsd32,
364                         compat_sysvipc_14_syscalls);
365 
366           default:
367                     return ENOTTY;
368           }
369 }
370 
371 #endif /* COMPAT_14 */
372