1 /* $NetBSD: svr4_32_ipc.c,v 1.19 2007/12/20 23:03:06 dsl Exp $ */
4 * Copyright (c) 1995 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: svr4_32_ipc.c,v 1.19 2007/12/20 23:03:06 dsl Exp $");
35 #if defined(_KERNEL_OPT)
39 #include <sys/param.h>
40 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/systm.h>
52 #include <sys/mount.h>
53 #include <sys/syscallargs.h>
55 #include <compat/svr4_32/svr4_32_types.h>
56 #include <compat/svr4_32/svr4_32_signal.h>
57 #include <compat/svr4_32/svr4_32_lwp.h>
58 #include <compat/svr4_32/svr4_32_ucontext.h>
59 #include <compat/svr4_32/svr4_32_syscallargs.h>
60 #include <compat/svr4_32/svr4_32_util.h>
61 #include <compat/svr4_32/svr4_32_ipc.h>
63 #if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
64 static void svr4_32_to_bsd_ipc_perm(const struct svr4_32_ipc_perm
*,
66 static void bsd_to_svr4_32_ipc_perm(const struct ipc_perm
*,
67 struct svr4_32_ipc_perm
*);
71 static void bsd_to_svr4_32_semid_ds(const struct semid_ds
*,
72 struct svr4_32_semid_ds
*);
73 static void svr4_32_to_bsd_semid_ds(const struct svr4_32_semid_ds
*,
78 static void bsd_to_svr4_32_msqid_ds(const struct msqid_ds
*,
79 struct svr4_32_msqid_ds
*);
80 static void svr4_32_to_bsd_msqid_ds(const struct svr4_32_msqid_ds
*,
85 static void bsd_to_svr4_32_shmid_ds(const struct shmid_ds
*,
86 struct svr4_32_shmid_ds
*);
87 static void svr4_32_to_bsd_shmid_ds(const struct svr4_32_shmid_ds
*,
91 #if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
94 svr4_32_to_bsd_ipc_perm(const struct svr4_32_ipc_perm
*spp
, struct ipc_perm
*bpp
)
99 bpp
->cuid
= spp
->cuid
;
100 bpp
->cgid
= spp
->cgid
;
101 bpp
->mode
= spp
->mode
;
102 bpp
->_seq
= spp
->seq
;
106 bsd_to_svr4_32_ipc_perm(const struct ipc_perm
*bpp
, struct svr4_32_ipc_perm
*spp
)
108 spp
->key
= bpp
->_key
;
111 spp
->cuid
= bpp
->cuid
;
112 spp
->cgid
= bpp
->cgid
;
113 spp
->mode
= bpp
->mode
;
114 spp
->seq
= bpp
->_seq
;
120 bsd_to_svr4_32_semid_ds(const struct semid_ds
*bds
, struct svr4_32_semid_ds
*sds
)
122 bsd_to_svr4_32_ipc_perm(&bds
->sem_perm
, &sds
->sem_perm
);
123 NETBSD32PTR32(sds
->sem_base
, bds
->_sem_base
);
124 sds
->sem_nsems
= bds
->sem_nsems
;
125 sds
->sem_otime
= bds
->sem_otime
;
126 sds
->sem_ctime
= bds
->sem_ctime
;
130 svr4_32_to_bsd_semid_ds(const struct svr4_32_semid_ds
*sds
, struct semid_ds
*bds
)
132 svr4_32_to_bsd_ipc_perm(&sds
->sem_perm
, &bds
->sem_perm
);
133 bds
->_sem_base
= NETBSD32PTR64(sds
->sem_base
);
134 bds
->sem_nsems
= sds
->sem_nsems
;
135 bds
->sem_otime
= sds
->sem_otime
;
136 bds
->sem_ctime
= sds
->sem_ctime
;
139 struct svr4_32_sys_semctl_args
{
140 syscallarg(int) what
;
141 syscallarg(int) semid
;
142 syscallarg(int) semnum
;
144 syscallarg(union netbsd32_semun
) arg
;
148 svr4_32_semctl(struct lwp
*l
, const struct svr4_32_sys_semctl_args
*uap
, register_t
*retval
)
150 struct semid_ds sembuf
;
151 struct svr4_32_semid_ds ssembuf
;
153 void *pass_arg
= NULL
;
154 union netbsd32_semun arg
;
156 cmd
= SCARG(uap
, cmd
);
173 case SVR4_SEM_GETVAL
:
177 case SVR4_SEM_GETPID
:
181 case SVR4_SEM_GETNCNT
:
185 case SVR4_SEM_GETZCNT
:
189 case SVR4_SEM_GETALL
:
190 arg
= SCARG(uap
, arg
);
195 case SVR4_SEM_SETVAL
:
196 arg
= SCARG(uap
, arg
);
201 case SVR4_SEM_SETALL
:
202 arg
= SCARG(uap
, arg
);
211 if (cmd
== IPC_SET
) {
212 error
= copyin(NETBSD32PTR64(SCARG(uap
, arg
).buf
),
213 &ssembuf
, sizeof(ssembuf
));
216 svr4_32_to_bsd_semid_ds(&ssembuf
, &sembuf
);
219 error
= semctl1(l
, SCARG(uap
, semid
), SCARG(uap
, semnum
), cmd
,
222 if (error
== 0 && cmd
== IPC_STAT
) {
223 bsd_to_svr4_32_semid_ds(&sembuf
, &ssembuf
);
224 error
= copyout(&ssembuf
, NETBSD32PTR64(SCARG(uap
, arg
).buf
),
231 struct svr4_32_sys_semget_args
{
232 syscallarg(int) what
;
233 syscallarg(svr4_key_t
) key
;
234 syscallarg(int) nsems
;
235 syscallarg(int) semflg
;
239 svr4_32_semget(struct lwp
*l
, const struct svr4_32_sys_semget_args
*uap
, register_t
*retval
)
241 struct sys_semget_args ap
;
243 SCARG(&ap
, key
) = SCARG(uap
, key
);
244 SCARG(&ap
, nsems
) = SCARG(uap
, nsems
);
245 SCARG(&ap
, semflg
) = SCARG(uap
, semflg
);
247 return sys_semget(l
, &ap
, retval
);
250 struct svr4_32_sys_semop_args
{
251 syscallarg(int) what
;
252 syscallarg(int) semid
;
253 syscallarg(svr4_32_sembufp
) sops
;
254 syscallarg(u_int
) nsops
;
258 svr4_32_semop(struct lwp
*l
, const struct svr4_32_sys_semop_args
*uap
, register_t
*retval
)
260 struct sys_semop_args ap
;
262 SCARG(&ap
, semid
) = SCARG(uap
, semid
);
263 /* These are the same */
264 SCARG(&ap
, sops
) = SCARG_P32(uap
, sops
);
265 SCARG(&ap
, nsops
) = SCARG(uap
, nsops
);
267 return sys_semop(l
, &ap
, retval
);
271 svr4_32_sys_semsys(struct lwp
*l
, const struct svr4_32_sys_semsys_args
*uap
, register_t
*retval
)
274 DPRINTF(("svr4_32_semsys(%d)\n", SCARG(uap
, what
)));
276 switch (SCARG(uap
, what
)) {
278 return svr4_32_semctl(l
, (const void *)uap
, retval
);
280 return svr4_32_semget(l
, (const void *)uap
, retval
);
282 return svr4_32_semop(l
, (const void *)uap
, retval
);
291 bsd_to_svr4_32_msqid_ds(const struct msqid_ds
*bds
, struct svr4_32_msqid_ds
*sds
)
293 bsd_to_svr4_32_ipc_perm(&bds
->msg_perm
, &sds
->msg_perm
);
294 NETBSD32PTR32(sds
->msg_first
, bds
->_msg_first
);
295 NETBSD32PTR32(sds
->msg_last
, bds
->_msg_last
);
296 sds
->msg_cbytes
= bds
->_msg_cbytes
;
297 sds
->msg_qnum
= bds
->msg_qnum
;
298 sds
->msg_qbytes
= bds
->msg_qbytes
;
299 sds
->msg_lspid
= bds
->msg_lspid
;
300 sds
->msg_lrpid
= bds
->msg_lrpid
;
301 sds
->msg_stime
= bds
->msg_stime
;
302 sds
->msg_rtime
= bds
->msg_rtime
;
303 sds
->msg_ctime
= bds
->msg_ctime
;
306 /* XXX What to put here? */
308 sds
->msg_qnum_cv
= 0;
313 svr4_32_to_bsd_msqid_ds(const struct svr4_32_msqid_ds
*sds
, struct msqid_ds
*bds
)
315 svr4_32_to_bsd_ipc_perm(&sds
->msg_perm
, &bds
->msg_perm
);
316 bds
->_msg_first
= NETBSD32PTR64(sds
->msg_first
);
317 bds
->_msg_last
= NETBSD32PTR64(sds
->msg_last
);
318 bds
->_msg_cbytes
= sds
->msg_cbytes
;
319 bds
->msg_qnum
= sds
->msg_qnum
;
320 bds
->msg_qbytes
= sds
->msg_qbytes
;
321 bds
->msg_lspid
= sds
->msg_lspid
;
322 bds
->msg_lrpid
= sds
->msg_lrpid
;
323 bds
->msg_stime
= sds
->msg_stime
;
324 bds
->msg_rtime
= sds
->msg_rtime
;
325 bds
->msg_ctime
= sds
->msg_ctime
;
333 struct svr4_32_sys_msgsnd_args
{
334 syscallarg(int) what
;
335 syscallarg(int) msqid
;
336 syscallarg(netbsd32_voidp
) msgp
;
337 syscallarg(netbsd32_size_t
) msgsz
;
338 syscallarg(int) msgflg
;
342 svr4_32_msgsnd(struct lwp
*l
, const struct svr4_32_sys_msgsnd_args
*uap
, register_t
*retval
)
344 struct sys_msgsnd_args ap
;
346 SCARG(&ap
, msqid
) = SCARG(uap
, msqid
);
347 SCARG(&ap
, msgp
) = SCARG_P32(uap
, msgp
);
348 SCARG(&ap
, msgsz
) = SCARG(uap
, msgsz
);
349 SCARG(&ap
, msgflg
) = SCARG(uap
, msgflg
);
351 return sys_msgsnd(l
, &ap
, retval
);
354 struct svr4_32_sys_msgrcv_args
{
355 syscallarg(int) what
;
356 syscallarg(int) msqid
;
357 syscallarg(netbsd32_voidp
) msgp
;
358 syscallarg(netbsd32_size_t
) msgsz
;
359 syscallarg(netbsd32_long
) msgtyp
;
360 syscallarg(int) msgflg
;
364 svr4_32_msgrcv(struct lwp
*l
, const struct svr4_32_sys_msgrcv_args
*uap
, register_t
*retval
)
366 struct sys_msgrcv_args ap
;
368 SCARG(&ap
, msqid
) = SCARG(uap
, msqid
);
369 SCARG(&ap
, msgp
) = SCARG_P32(uap
, msgp
);
370 SCARG(&ap
, msgsz
) = SCARG(uap
, msgsz
);
371 SCARG(&ap
, msgtyp
) = SCARG(uap
, msgtyp
);
372 SCARG(&ap
, msgflg
) = SCARG(uap
, msgflg
);
374 return sys_msgrcv(l
, &ap
, retval
);
377 struct svr4_32_sys_msgget_args
{
378 syscallarg(int) what
;
379 syscallarg(svr4_key_t
) key
;
380 syscallarg(int) msgflg
;
384 svr4_32_msgget(struct lwp
*l
, const struct svr4_32_sys_msgget_args
*uap
, register_t
*retval
)
386 struct sys_msgget_args ap
;
388 SCARG(&ap
, key
) = SCARG(uap
, key
);
389 SCARG(&ap
, msgflg
) = SCARG(uap
, msgflg
);
391 return sys_msgget(l
, &ap
, retval
);
394 struct svr4_32_sys_msgctl_args
{
395 syscallarg(int) what
;
396 syscallarg(int) msqid
;
398 syscallarg(svr4_32_msqid_dsp
) buf
;
402 svr4_32_msgctl(struct lwp
*l
, const struct svr4_32_sys_msgctl_args
*uap
, register_t
*retval
)
404 struct svr4_32_msqid_ds ss
;
408 switch (SCARG(uap
, cmd
)) {
410 error
= msgctl1(l
, SCARG(uap
, msqid
), IPC_STAT
, &bs
);
412 bsd_to_svr4_32_msqid_ds(&bs
, &ss
);
413 error
= copyout(&ss
, SCARG_P32(uap
, buf
), sizeof ss
);
418 error
= copyin(SCARG_P32(uap
, buf
), &ss
, sizeof ss
);
421 svr4_32_to_bsd_msqid_ds(&ss
, &bs
);
422 return msgctl1(l
, SCARG(uap
, msqid
), IPC_SET
, &bs
);
425 return msgctl1(l
, SCARG(uap
, msqid
), IPC_RMID
, NULL
);
433 svr4_32_sys_msgsys(struct lwp
*l
, const struct svr4_32_sys_msgsys_args
*uap
, register_t
*retval
)
436 DPRINTF(("svr4_32_msgsys(%d)\n", SCARG(uap
, what
)));
438 switch (SCARG(uap
, what
)) {
440 return svr4_32_msgsnd(l
, (const void *)uap
, retval
);
442 return svr4_32_msgrcv(l
, (const void *)uap
, retval
);
444 return svr4_32_msgget(l
, (const void *)uap
, retval
);
446 return svr4_32_msgctl(l
, (const void *)uap
, retval
);
456 bsd_to_svr4_32_shmid_ds(const struct shmid_ds
*bds
, struct svr4_32_shmid_ds
*sds
)
458 bsd_to_svr4_32_ipc_perm(&bds
->shm_perm
, &sds
->shm_perm
);
459 sds
->shm_segsz
= bds
->shm_segsz
;
461 sds
->shm_lpid
= bds
->shm_lpid
;
462 sds
->shm_cpid
= bds
->shm_cpid
;
463 NETBSD32PTR32(sds
->shm_amp
, bds
->_shm_internal
);
464 sds
->shm_nattch
= bds
->shm_nattch
;
465 sds
->shm_cnattch
= 0;
466 sds
->shm_atime
= bds
->shm_atime
;
468 sds
->shm_dtime
= bds
->shm_dtime
;
470 sds
->shm_ctime
= bds
->shm_ctime
;
475 svr4_32_to_bsd_shmid_ds(const struct svr4_32_shmid_ds
*sds
, struct shmid_ds
*bds
)
477 svr4_32_to_bsd_ipc_perm(&sds
->shm_perm
, &bds
->shm_perm
);
478 bds
->shm_segsz
= sds
->shm_segsz
;
479 bds
->shm_lpid
= sds
->shm_lpid
;
480 bds
->shm_cpid
= sds
->shm_cpid
;
481 bds
->_shm_internal
= NETBSD32PTR64(sds
->shm_amp
);
482 bds
->shm_nattch
= sds
->shm_nattch
;
483 bds
->shm_atime
= sds
->shm_atime
;
484 bds
->shm_dtime
= sds
->shm_dtime
;
485 bds
->shm_ctime
= sds
->shm_ctime
;
488 struct svr4_32_sys_shmat_args
{
489 syscallarg(int) what
;
490 syscallarg(int) shmid
;
491 syscallarg(netbsd32_voidp
) shmaddr
;
492 syscallarg(int) shmflg
;
496 svr4_32_shmat(struct lwp
*l
, const struct svr4_32_sys_shmat_args
*uap
, register_t
*retval
)
498 struct sys_shmat_args ap
;
500 SCARG(&ap
, shmid
) = SCARG(uap
, shmid
);
501 SCARG(&ap
, shmaddr
) = SCARG_P32(uap
, shmaddr
);
502 SCARG(&ap
, shmflg
) = SCARG(uap
, shmflg
);
504 return sys_shmat(l
, &ap
, retval
);
507 struct svr4_32_sys_shmdt_args
{
508 syscallarg(int) what
;
509 syscallarg(netbsd32_voidp
) shmaddr
;
513 svr4_32_shmdt(struct lwp
*l
, const struct svr4_32_sys_shmdt_args
*uap
, register_t
*retval
)
515 struct sys_shmdt_args ap
;
517 SCARG(&ap
, shmaddr
) = SCARG_P32(uap
, shmaddr
);
519 return sys_shmdt(l
, &ap
, retval
);
522 struct svr4_32_sys_shmget_args
{
523 syscallarg(int) what
;
524 syscallarg(netbsd32_key_t
) key
;
525 syscallarg(int) size
;
526 syscallarg(int) shmflg
;
530 svr4_32_shmget(struct lwp
*l
, const struct svr4_32_sys_shmget_args
*uap
, register_t
*retval
)
532 struct sys_shmget_args ap
;
534 SCARG(&ap
, key
) = SCARG(uap
, key
);
535 SCARG(&ap
, size
) = SCARG(uap
, size
);
536 SCARG(&ap
, shmflg
) = SCARG(uap
, shmflg
);
538 return sys_shmget(l
, &ap
, retval
);
541 struct svr4_32_sys_shmctl_args
{
542 syscallarg(int) what
;
543 syscallarg(int) shmid
;
545 syscallarg(svr4_32_shmid_dsp
) buf
;
549 svr4_32_shmctl(struct lwp
*l
, const struct svr4_32_sys_shmctl_args
*uap
, register_t
*retval
)
552 struct svr4_32_shmid_ds ss
;
555 switch (SCARG(uap
, cmd
)) {
557 error
= shmctl1(l
, SCARG(uap
, shmid
), IPC_STAT
, &bs
);
559 bsd_to_svr4_32_shmid_ds(&bs
, &ss
);
560 error
= copyout(&ss
, SCARG_P32(uap
, buf
), sizeof ss
);
565 error
= copyin(SCARG_P32(uap
, buf
), &ss
, sizeof ss
);
568 svr4_32_to_bsd_shmid_ds(&ss
, &bs
);
569 return shmctl1(l
, SCARG(uap
, shmid
), IPC_SET
, &bs
);
572 return shmctl1(l
, SCARG(uap
, shmid
), IPC_RMID
, NULL
);
575 return shmctl1(l
, SCARG(uap
, shmid
), SHM_LOCK
, NULL
);
577 case SVR4_SHM_UNLOCK
:
578 return shmctl1(l
, SCARG(uap
, shmid
), SHM_UNLOCK
, NULL
);
586 svr4_32_sys_shmsys(struct lwp
*l
, const struct svr4_32_sys_shmsys_args
*uap
, register_t
*retval
)
589 DPRINTF(("svr4_32_shmsys(%d)\n", SCARG(uap
, what
)));
591 switch (SCARG(uap
, what
)) {
593 return svr4_32_shmat(l
, (const void *)uap
, retval
);
595 return svr4_32_shmdt(l
, (const void *)uap
, retval
);
597 return svr4_32_shmget(l
, (const void *)uap
, retval
);
599 return svr4_32_shmctl(l
, (const void *)uap
, retval
);