svr4_ipc.c revision 43412
143412Snewton/*
243412Snewton *	Derived from:
343412Snewton *	$NetBSD: svr4_ipc.c,v 1.7 1998/10/19 22:43:00 tron Exp $	*/
443412Snewton
543412Snewton/*-
643412Snewton * Original copyright:
743412Snewton *
843412Snewton * Copyright (c) 1995 The NetBSD Foundation, Inc.
943412Snewton * All rights reserved.
1043412Snewton *
1143412Snewton * This code is derived from software contributed to The NetBSD Foundation
1243412Snewton * by Christos Zoulas.
1343412Snewton *
1443412Snewton * Redistribution and use in source and binary forms, with or without
1543412Snewton * modification, are permitted provided that the following conditions
1643412Snewton * are met:
1743412Snewton * 1. Redistributions of source code must retain the above copyright
1843412Snewton *    notice, this list of conditions and the following disclaimer.
1943412Snewton * 2. Redistributions in binary form must reproduce the above copyright
2043412Snewton *    notice, this list of conditions and the following disclaimer in the
2143412Snewton *    documentation and/or other materials provided with the distribution.
2243412Snewton * 3. All advertising materials mentioning features or use of this software
2343412Snewton *    must display the following acknowledgement:
2443412Snewton *        This product includes software developed by the NetBSD
2543412Snewton *        Foundation, Inc. and its contributors.
2643412Snewton * 4. Neither the name of The NetBSD Foundation nor the names of its
2743412Snewton *    contributors may be used to endorse or promote products derived
2843412Snewton *    from this software without specific prior written permission.
2943412Snewton *
3043412Snewton * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
3143412Snewton * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
3243412Snewton * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3343412Snewton * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3443412Snewton * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3543412Snewton * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3643412Snewton * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3743412Snewton * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3843412Snewton * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3943412Snewton * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4043412Snewton * POSSIBILITY OF SUCH DAMAGE.
4143412Snewton */
4243412Snewton
4343412Snewton/*
4443412Snewton * Portions of this code have been derived from software contributed
4543412Snewton * to the FreeBSD Project by Mark Newton.
4643412Snewton *
4743412Snewton * Copyright (c) 1999 Mark Newton
4843412Snewton * All rights reserved.
4943412Snewton *
5043412Snewton * Redistribution and use in source and binary forms, with or without
5143412Snewton * modification, are permitted provided that the following conditions
5243412Snewton * are met:
5343412Snewton * 1. Redistributions of source code must retain the above copyright
5443412Snewton *    notice, this list of conditions and the following disclaimer.
5543412Snewton * 2. Redistributions in binary form must reproduce the above copyright
5643412Snewton *    notice, this list of conditions and the following disclaimer in the
5743412Snewton *    documentation and/or other materials provided with the distribution.
5843412Snewton * 3. The name of the author may not be used to endorse or promote products
5943412Snewton *    derived from this software without specific prior written permission
6043412Snewton *
6143412Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
6243412Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6343412Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6443412Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
6543412Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
6643412Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6743412Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6843412Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6943412Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7043412Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7143412Snewton *
7243412Snewton * XXX- This code is presently a no-op on FreeBSD (and isn't compiled due
7343412Snewton * to preprocessor conditionals).  A nice project for a kernel hacking
7443412Snewton * novice might be to MakeItGo, but I have more important fish to fry
7543412Snewton * at present.
7643412Snewton *
7743412Snewton */
7843412Snewton
7943412Snewton#include <sys/types.h>
8043412Snewton#include <sys/param.h>
8143412Snewton#include <sys/kernel.h>
8243412Snewton#include <sys/shm.h>
8343412Snewton#include <sys/msg.h>
8443412Snewton#include <sys/sem.h>
8543412Snewton#include <sys/proc.h>
8643412Snewton#include <sys/uio.h>
8743412Snewton#include <sys/time.h>
8843412Snewton#include <sys/malloc.h>
8943412Snewton#include <sys/mman.h>
9043412Snewton#include <sys/systm.h>
9143412Snewton#include <sys/stat.h>
9243412Snewton
9343412Snewton#include <sys/mount.h>
9443412Snewton#include <sys/sysproto.h>
9543412Snewton
9643412Snewton#include <svr4/svr4_types.h>
9743412Snewton#include <svr4/svr4_signal.h>
9843412Snewton#include <svr4/svr4_proto.h>
9943412Snewton#include <svr4/svr4_util.h>
10043412Snewton#include <svr4/svr4_ipc.h>
10143412Snewton
10243412Snewton#if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
10343412Snewtonstatic void svr4_to_bsd_ipc_perm __P((const struct svr4_ipc_perm *,
10443412Snewton				      struct ipc_perm *));
10543412Snewtonstatic void bsd_to_svr4_ipc_perm __P((const struct ipc_perm *,
10643412Snewton				      struct svr4_ipc_perm *));
10743412Snewton#endif
10843412Snewton
10943412Snewton#ifdef SYSVSEM
11043412Snewtonstatic void bsd_to_svr4_semid_ds __P((const struct semid_ds *,
11143412Snewton				      struct svr4_semid_ds *));
11243412Snewtonstatic void svr4_to_bsd_semid_ds __P((const struct svr4_semid_ds *,
11343412Snewton				      struct semid_ds *));
11443412Snewtonstatic int svr4_setsemun __P((caddr_t *sgp, union semun **argp,
11543412Snewton			      union semun *usp));
11643412Snewtonstatic int svr4_semop __P((struct proc *, void *, register_t *));
11743412Snewtonstatic int svr4_semget __P((struct proc *, void *, register_t *));
11843412Snewtonstatic int svr4_semctl __P((struct proc *, void *, register_t *));
11943412Snewton#endif
12043412Snewton
12143412Snewton#ifdef SYSVMSG
12243412Snewtonstatic void bsd_to_svr4_msqid_ds __P((const struct msqid_ds *,
12343412Snewton				      struct svr4_msqid_ds *));
12443412Snewtonstatic void svr4_to_bsd_msqid_ds __P((const struct svr4_msqid_ds *,
12543412Snewton				      struct msqid_ds *));
12643412Snewtonstatic int svr4_msgsnd __P((struct proc *, void *, register_t *));
12743412Snewtonstatic int svr4_msgrcv __P((struct proc *, void *, register_t *));
12843412Snewtonstatic int svr4_msgget __P((struct proc *, void *, register_t *));
12943412Snewtonstatic int svr4_msgctl __P((struct proc *, void *, register_t *));
13043412Snewton#endif
13143412Snewton
13243412Snewton#ifdef SYSVSHM
13343412Snewtonstatic void bsd_to_svr4_shmid_ds __P((const struct shmid_ds *,
13443412Snewton				      struct svr4_shmid_ds *));
13543412Snewtonstatic void svr4_to_bsd_shmid_ds __P((const struct svr4_shmid_ds *,
13643412Snewton				      struct shmid_ds *));
13743412Snewtonstatic int svr4_shmat __P((struct proc *, void *, register_t *));
13843412Snewtonstatic int svr4_shmdt __P((struct proc *, void *, register_t *));
13943412Snewtonstatic int svr4_shmget __P((struct proc *, void *, register_t *));
14043412Snewtonstatic int svr4_shmctl __P((struct proc *, void *, register_t *));
14143412Snewton#endif
14243412Snewton
14343412Snewton#if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
14443412Snewton
14543412Snewtonstatic void
14643412Snewtonsvr4_to_bsd_ipc_perm(spp, bpp)
14743412Snewton	const struct svr4_ipc_perm *spp;
14843412Snewton	struct ipc_perm *bpp;
14943412Snewton{
15043412Snewton	bpp->key = spp->key;
15143412Snewton	bpp->uid = spp->uid;
15243412Snewton	bpp->gid = spp->gid;
15343412Snewton	bpp->cuid = spp->cuid;
15443412Snewton	bpp->cgid = spp->cgid;
15543412Snewton	bpp->mode = spp->mode;
15643412Snewton	bpp->seq = spp->seq;
15743412Snewton}
15843412Snewton
15943412Snewtonstatic void
16043412Snewtonbsd_to_svr4_ipc_perm(bpp, spp)
16143412Snewton	const struct ipc_perm *bpp;
16243412Snewton	struct svr4_ipc_perm *spp;
16343412Snewton{
16443412Snewton	spp->key = bpp->key;
16543412Snewton	spp->uid = bpp->uid;
16643412Snewton	spp->gid = bpp->gid;
16743412Snewton	spp->cuid = bpp->cuid;
16843412Snewton	spp->cgid = bpp->cgid;
16943412Snewton	spp->mode = bpp->mode;
17043412Snewton	spp->seq = bpp->seq;
17143412Snewton}
17243412Snewton#endif
17343412Snewton
17443412Snewton#ifdef SYSVSEM
17543412Snewtonstatic void
17643412Snewtonbsd_to_svr4_semid_ds(bds, sds)
17743412Snewton	const struct semid_ds *bds;
17843412Snewton	struct svr4_semid_ds *sds;
17943412Snewton{
18043412Snewton	bsd_to_svr4_ipc_perm(&bds->sem_perm, &sds->sem_perm);
18143412Snewton	sds->sem_base = (struct svr4_sem *) bds->sem_base;
18243412Snewton	sds->sem_nsems = bds->sem_nsems;
18343412Snewton	sds->sem_otime = bds->sem_otime;
18443412Snewton	sds->sem_pad1 = bds->sem_pad1;
18543412Snewton	sds->sem_ctime = bds->sem_ctime;
18643412Snewton	sds->sem_pad2 = bds->sem_pad2;
18743412Snewton}
18843412Snewton
18943412Snewtonstatic void
19043412Snewtonsvr4_to_bsd_semid_ds(sds, bds)
19143412Snewton	const struct svr4_semid_ds *sds;
19243412Snewton	struct semid_ds *bds;
19343412Snewton{
19443412Snewton	svr4_to_bsd_ipc_perm(&sds->sem_perm, &bds->sem_perm);
19543412Snewton	bds->sem_base = (struct sem *) bds->sem_base;
19643412Snewton	bds->sem_nsems = sds->sem_nsems;
19743412Snewton	bds->sem_otime = sds->sem_otime;
19843412Snewton	bds->sem_pad1 = sds->sem_pad1;
19943412Snewton	bds->sem_ctime = sds->sem_ctime;
20043412Snewton	bds->sem_pad2 = sds->sem_pad2;
20143412Snewton}
20243412Snewton
20343412Snewtonstatic int
20443412Snewtonsvr4_setsemun(sgp, argp, usp)
20543412Snewton	caddr_t *sgp;
20643412Snewton	union semun **argp;
20743412Snewton	union semun *usp;
20843412Snewton{
20943412Snewton	*argp = stackgap_alloc(sgp, sizeof(union semun));
21043412Snewton	return copyout((caddr_t)usp, *argp, sizeof(union semun));
21143412Snewton}
21243412Snewton
21343412Snewtonstruct svr4_sys_semctl_args {
21443412Snewton	syscallarg(int) what;
21543412Snewton	syscallarg(int) semid;
21643412Snewton	syscallarg(int) semnum;
21743412Snewton	syscallarg(int) cmd;
21843412Snewton	syscallarg(union semun) arg;
21943412Snewton};
22043412Snewton
22143412Snewtonstatic int
22243412Snewtonsvr4_semctl(p, v, retval)
22343412Snewton	struct proc *p;
22443412Snewton	void *v;
22543412Snewton	register_t *retval;
22643412Snewton{
22743412Snewton	int error;
22843412Snewton	struct svr4_sys_semctl_args *uap = v;
22943412Snewton	struct sys___semctl_args ap;
23043412Snewton	struct svr4_semid_ds ss;
23143412Snewton	struct semid_ds bs, *bsp;
23243412Snewton	caddr_t sg = stackgap_init(p->p_emul);
23343412Snewton
23443412Snewton	SCARG(&ap, semid) = SCARG(uap, semid);
23543412Snewton	SCARG(&ap, semnum) = SCARG(uap, semnum);
23643412Snewton
23743412Snewton	switch (SCARG(uap, cmd)) {
23843412Snewton	case SVR4_SEM_GETZCNT:
23943412Snewton	case SVR4_SEM_GETNCNT:
24043412Snewton	case SVR4_SEM_GETPID:
24143412Snewton	case SVR4_SEM_GETVAL:
24243412Snewton		switch (SCARG(uap, cmd)) {
24343412Snewton		case SVR4_SEM_GETZCNT:
24443412Snewton			SCARG(&ap, cmd) = GETZCNT;
24543412Snewton			break;
24643412Snewton		case SVR4_SEM_GETNCNT:
24743412Snewton			SCARG(&ap, cmd) = GETNCNT;
24843412Snewton			break;
24943412Snewton		case SVR4_SEM_GETPID:
25043412Snewton			SCARG(&ap, cmd) = GETPID;
25143412Snewton			break;
25243412Snewton		case SVR4_SEM_GETVAL:
25343412Snewton			SCARG(&ap, cmd) = GETVAL;
25443412Snewton			break;
25543412Snewton		}
25643412Snewton		return sys___semctl(p, &ap, retval);
25743412Snewton
25843412Snewton	case SVR4_SEM_SETVAL:
25943412Snewton		error = svr4_setsemun(&sg, &SCARG(&ap, arg), &SCARG(uap, arg));
26043412Snewton		if (error)
26143412Snewton			return error;
26243412Snewton		SCARG(&ap, cmd) = SETVAL;
26343412Snewton		return sys___semctl(p, &ap, retval);
26443412Snewton
26543412Snewton	case SVR4_SEM_GETALL:
26643412Snewton		error = svr4_setsemun(&sg, &SCARG(&ap, arg), &SCARG(uap, arg));
26743412Snewton		if (error)
26843412Snewton			return error;
26943412Snewton		SCARG(&ap, cmd) = GETVAL;
27043412Snewton		return sys___semctl(p, &ap, retval);
27143412Snewton
27243412Snewton	case SVR4_SEM_SETALL:
27343412Snewton		error = svr4_setsemun(&sg, &SCARG(&ap, arg), &SCARG(uap, arg));
27443412Snewton		if (error)
27543412Snewton			return error;
27643412Snewton		SCARG(&ap, cmd) = SETVAL;
27743412Snewton		return sys___semctl(p, &ap, retval);
27843412Snewton
27943412Snewton	case SVR4_IPC_STAT:
28043412Snewton                SCARG(&ap, cmd) = IPC_STAT;
28143412Snewton		bsp = stackgap_alloc(&sg, sizeof(bs));
28243412Snewton		error = svr4_setsemun(&sg, &SCARG(&ap, arg),
28343412Snewton				      (union semun *)&bsp);
28443412Snewton		if (error)
28543412Snewton			return error;
28643412Snewton                if ((error = sys___semctl(p, &ap, retval)) != 0)
28743412Snewton                        return error;
28843412Snewton		error = copyin((caddr_t)bsp, (caddr_t)&bs, sizeof(bs));
28943412Snewton                if (error)
29043412Snewton                        return error;
29143412Snewton                bsd_to_svr4_semid_ds(&bs, &ss);
29243412Snewton		return copyout(&ss, SCARG(uap, arg).buf, sizeof(ss));
29343412Snewton
29443412Snewton	case SVR4_IPC_SET:
29543412Snewton		SCARG(&ap, cmd) = IPC_SET;
29643412Snewton		bsp = stackgap_alloc(&sg, sizeof(bs));
29743412Snewton		error = svr4_setsemun(&sg, &SCARG(&ap, arg),
29843412Snewton				      (union semun *)&bsp);
29943412Snewton		if (error)
30043412Snewton			return error;
30143412Snewton		error = copyin(SCARG(uap, arg).buf, (caddr_t) &ss, sizeof ss);
30243412Snewton                if (error)
30343412Snewton                        return error;
30443412Snewton                svr4_to_bsd_semid_ds(&ss, &bs);
30543412Snewton		error = copyout(&bs, bsp, sizeof(bs));
30643412Snewton                if (error)
30743412Snewton                        return error;
30843412Snewton		return sys___semctl(p, &ap, retval);
30943412Snewton
31043412Snewton	case SVR4_IPC_RMID:
31143412Snewton		SCARG(&ap, cmd) = IPC_RMID;
31243412Snewton		bsp = stackgap_alloc(&sg, sizeof(bs));
31343412Snewton		error = svr4_setsemun(&sg, &SCARG(&ap, arg),
31443412Snewton				      (union semun *)&bsp);
31543412Snewton		if (error)
31643412Snewton			return error;
31743412Snewton		error = copyin(SCARG(uap, arg).buf, &ss, sizeof ss);
31843412Snewton                if (error)
31943412Snewton                        return error;
32043412Snewton                svr4_to_bsd_semid_ds(&ss, &bs);
32143412Snewton		error = copyout(&bs, bsp, sizeof(bs));
32243412Snewton		if (error)
32343412Snewton			return error;
32443412Snewton		return sys___semctl(p, &ap, retval);
32543412Snewton
32643412Snewton	default:
32743412Snewton		return EINVAL;
32843412Snewton	}
32943412Snewton}
33043412Snewton
33143412Snewtonstruct svr4_sys_semget_args {
33243412Snewton	syscallarg(int) what;
33343412Snewton	syscallarg(svr4_key_t) key;
33443412Snewton	syscallarg(int) nsems;
33543412Snewton	syscallarg(int) semflg;
33643412Snewton};
33743412Snewton
33843412Snewtonstatic int
33943412Snewtonsvr4_semget(p, v, retval)
34043412Snewton	struct proc *p;
34143412Snewton	void *v;
34243412Snewton	register_t *retval;
34343412Snewton{
34443412Snewton	struct svr4_sys_semget_args *uap = v;
34543412Snewton	struct sys_semget_args ap;
34643412Snewton
34743412Snewton	SCARG(&ap, key) = SCARG(uap, key);
34843412Snewton	SCARG(&ap, nsems) = SCARG(uap, nsems);
34943412Snewton	SCARG(&ap, semflg) = SCARG(uap, semflg);
35043412Snewton
35143412Snewton	return sys_semget(p, &ap, retval);
35243412Snewton}
35343412Snewton
35443412Snewtonstruct svr4_sys_semop_args {
35543412Snewton	syscallarg(int) what;
35643412Snewton	syscallarg(int) semid;
35743412Snewton	syscallarg(struct svr4_sembuf *) sops;
35843412Snewton	syscallarg(u_int) nsops;
35943412Snewton};
36043412Snewton
36143412Snewtonstatic int
36243412Snewtonsvr4_semop(p, v, retval)
36343412Snewton	struct proc *p;
36443412Snewton	void *v;
36543412Snewton	register_t *retval;
36643412Snewton{
36743412Snewton	struct svr4_sys_semop_args *uap = v;
36843412Snewton	struct sys_semop_args ap;
36943412Snewton
37043412Snewton	SCARG(&ap, semid) = SCARG(uap, semid);
37143412Snewton	/* These are the same */
37243412Snewton	SCARG(&ap, sops) = (struct sembuf *) SCARG(uap, sops);
37343412Snewton	SCARG(&ap, nsops) = SCARG(uap, nsops);
37443412Snewton
37543412Snewton	return sys_semop(p, &ap, retval);
37643412Snewton}
37743412Snewton
37843412Snewtonint
37943412Snewtonsvr4_sys_semsys(p, v, retval)
38043412Snewton	struct proc *p;
38143412Snewton	void *v;
38243412Snewton	register_t *retval;
38343412Snewton{
38443412Snewton	struct svr4_sys_semsys_args *uap = v;
38543412Snewton
38643412Snewton	DPRINTF(("svr4_semsys(%d)\n", SCARG(uap, what)));
38743412Snewton
38843412Snewton	switch (SCARG(uap, what)) {
38943412Snewton	case SVR4_semctl:
39043412Snewton		return svr4_semctl(p, v, retval);
39143412Snewton	case SVR4_semget:
39243412Snewton		return svr4_semget(p, v, retval);
39343412Snewton	case SVR4_semop:
39443412Snewton		return svr4_semop(p, v, retval);
39543412Snewton	default:
39643412Snewton		return EINVAL;
39743412Snewton	}
39843412Snewton}
39943412Snewton#endif
40043412Snewton
40143412Snewton#ifdef SYSVMSG
40243412Snewtonstatic void
40343412Snewtonbsd_to_svr4_msqid_ds(bds, sds)
40443412Snewton	const struct msqid_ds *bds;
40543412Snewton	struct svr4_msqid_ds *sds;
40643412Snewton{
40743412Snewton	bsd_to_svr4_ipc_perm(&bds->msg_perm, &sds->msg_perm);
40843412Snewton	sds->msg_first = (struct svr4_msg *) bds->msg_first;
40943412Snewton	sds->msg_last = (struct svr4_msg *) bds->msg_last;
41043412Snewton	sds->msg_cbytes = bds->msg_cbytes;
41143412Snewton	sds->msg_qnum = bds->msg_qnum;
41243412Snewton	sds->msg_qbytes = bds->msg_qbytes;
41343412Snewton	sds->msg_lspid = bds->msg_lspid;
41443412Snewton	sds->msg_lrpid = bds->msg_lrpid;
41543412Snewton	sds->msg_stime = bds->msg_stime;
41643412Snewton	sds->msg_pad1 = bds->msg_pad1;
41743412Snewton	sds->msg_rtime = bds->msg_rtime;
41843412Snewton	sds->msg_pad2 = bds->msg_pad2;
41943412Snewton	sds->msg_ctime = bds->msg_ctime;
42043412Snewton	sds->msg_pad3 = bds->msg_pad3;
42143412Snewton
42243412Snewton	/* use the padding for the rest of the fields */
42343412Snewton	{
42443412Snewton		const short *pad = (const short *) bds->msg_pad4;
42543412Snewton		sds->msg_cv = pad[0];
42643412Snewton		sds->msg_qnum_cv = pad[1];
42743412Snewton	}
42843412Snewton}
42943412Snewton
43043412Snewtonstatic void
43143412Snewtonsvr4_to_bsd_msqid_ds(sds, bds)
43243412Snewton	const struct svr4_msqid_ds *sds;
43343412Snewton	struct msqid_ds *bds;
43443412Snewton{
43543412Snewton	svr4_to_bsd_ipc_perm(&sds->msg_perm, &bds->msg_perm);
43643412Snewton	bds->msg_first = (struct msg *) sds->msg_first;
43743412Snewton	bds->msg_last = (struct msg *) sds->msg_last;
43843412Snewton	bds->msg_cbytes = sds->msg_cbytes;
43943412Snewton	bds->msg_qnum = sds->msg_qnum;
44043412Snewton	bds->msg_qbytes = sds->msg_qbytes;
44143412Snewton	bds->msg_lspid = sds->msg_lspid;
44243412Snewton	bds->msg_lrpid = sds->msg_lrpid;
44343412Snewton	bds->msg_stime = sds->msg_stime;
44443412Snewton	bds->msg_pad1 = sds->msg_pad1;
44543412Snewton	bds->msg_rtime = sds->msg_rtime;
44643412Snewton	bds->msg_pad2 = sds->msg_pad2;
44743412Snewton	bds->msg_ctime = sds->msg_ctime;
44843412Snewton	bds->msg_pad3 = sds->msg_pad3;
44943412Snewton
45043412Snewton	/* use the padding for the rest of the fields */
45143412Snewton	{
45243412Snewton		short *pad = (short *) bds->msg_pad4;
45343412Snewton		pad[0] = sds->msg_cv;
45443412Snewton		pad[1] = sds->msg_qnum_cv;
45543412Snewton	}
45643412Snewton}
45743412Snewton
45843412Snewtonstruct svr4_sys_msgsnd_args {
45943412Snewton	syscallarg(int) what;
46043412Snewton	syscallarg(int) msqid;
46143412Snewton	syscallarg(void *) msgp;
46243412Snewton	syscallarg(size_t) msgsz;
46343412Snewton	syscallarg(int) msgflg;
46443412Snewton};
46543412Snewton
46643412Snewtonstatic int
46743412Snewtonsvr4_msgsnd(p, v, retval)
46843412Snewton	struct proc *p;
46943412Snewton	void *v;
47043412Snewton	register_t *retval;
47143412Snewton{
47243412Snewton	struct svr4_sys_msgsnd_args *uap = v;
47343412Snewton	struct sys_msgsnd_args ap;
47443412Snewton
47543412Snewton	SCARG(&ap, msqid) = SCARG(uap, msqid);
47643412Snewton	SCARG(&ap, msgp) = SCARG(uap, msgp);
47743412Snewton	SCARG(&ap, msgsz) = SCARG(uap, msgsz);
47843412Snewton	SCARG(&ap, msgflg) = SCARG(uap, msgflg);
47943412Snewton
48043412Snewton	return sys_msgsnd(p, &ap, retval);
48143412Snewton}
48243412Snewton
48343412Snewtonstruct svr4_sys_msgrcv_args {
48443412Snewton	syscallarg(int) what;
48543412Snewton	syscallarg(int) msqid;
48643412Snewton	syscallarg(void *) msgp;
48743412Snewton	syscallarg(size_t) msgsz;
48843412Snewton	syscallarg(long) msgtyp;
48943412Snewton	syscallarg(int) msgflg;
49043412Snewton};
49143412Snewton
49243412Snewtonstatic int
49343412Snewtonsvr4_msgrcv(p, v, retval)
49443412Snewton	struct proc *p;
49543412Snewton	void *v;
49643412Snewton	register_t *retval;
49743412Snewton{
49843412Snewton	struct svr4_sys_msgrcv_args *uap = v;
49943412Snewton	struct sys_msgrcv_args ap;
50043412Snewton
50143412Snewton	SCARG(&ap, msqid) = SCARG(uap, msqid);
50243412Snewton	SCARG(&ap, msgp) = SCARG(uap, msgp);
50343412Snewton	SCARG(&ap, msgsz) = SCARG(uap, msgsz);
50443412Snewton	SCARG(&ap, msgtyp) = SCARG(uap, msgtyp);
50543412Snewton	SCARG(&ap, msgflg) = SCARG(uap, msgflg);
50643412Snewton
50743412Snewton	return sys_msgrcv(p, &ap, retval);
50843412Snewton}
50943412Snewton
51043412Snewtonstruct svr4_sys_msgget_args {
51143412Snewton	syscallarg(int) what;
51243412Snewton	syscallarg(svr4_key_t) key;
51343412Snewton	syscallarg(int) msgflg;
51443412Snewton};
51543412Snewton
51643412Snewtonstatic int
51743412Snewtonsvr4_msgget(p, v, retval)
51843412Snewton	struct proc *p;
51943412Snewton	void *v;
52043412Snewton	register_t *retval;
52143412Snewton{
52243412Snewton	struct svr4_sys_msgget_args *uap = v;
52343412Snewton	struct sys_msgget_args ap;
52443412Snewton
52543412Snewton	SCARG(&ap, key) = SCARG(uap, key);
52643412Snewton	SCARG(&ap, msgflg) = SCARG(uap, msgflg);
52743412Snewton
52843412Snewton	return sys_msgget(p, &ap, retval);
52943412Snewton}
53043412Snewton
53143412Snewtonstruct svr4_sys_msgctl_args {
53243412Snewton	syscallarg(int) what;
53343412Snewton	syscallarg(int) msqid;
53443412Snewton	syscallarg(int) cmd;
53543412Snewton	syscallarg(struct svr4_msqid_ds *) buf;
53643412Snewton};
53743412Snewton
53843412Snewtonstatic int
53943412Snewtonsvr4_msgctl(p, v, retval)
54043412Snewton	struct proc *p;
54143412Snewton	void *v;
54243412Snewton	register_t *retval;
54343412Snewton{
54443412Snewton	int error;
54543412Snewton	struct svr4_sys_msgctl_args *uap = v;
54643412Snewton	struct sys_msgctl_args ap;
54743412Snewton	struct svr4_msqid_ds ss;
54843412Snewton	struct msqid_ds bs;
54943412Snewton	caddr_t sg = stackgap_init(p->p_emul);
55043412Snewton
55143412Snewton	SCARG(&ap, msqid) = SCARG(uap, msqid);
55243412Snewton	SCARG(&ap, cmd) = SCARG(uap, cmd);
55343412Snewton	SCARG(&ap, buf) = stackgap_alloc(&sg, sizeof(bs));
55443412Snewton
55543412Snewton	switch (SCARG(uap, cmd)) {
55643412Snewton	case SVR4_IPC_STAT:
55743412Snewton		SCARG(&ap, cmd) = IPC_STAT;
55843412Snewton		if ((error = sys_msgctl(p, &ap, retval)) != 0)
55943412Snewton			return error;
56043412Snewton		error = copyin(&bs, SCARG(&ap, buf), sizeof bs);
56143412Snewton		if (error)
56243412Snewton			return error;
56343412Snewton		bsd_to_svr4_msqid_ds(&bs, &ss);
56443412Snewton		return copyout(&ss, SCARG(uap, buf), sizeof ss);
56543412Snewton
56643412Snewton	case SVR4_IPC_SET:
56743412Snewton		SCARG(&ap, cmd) = IPC_SET;
56843412Snewton		error = copyin(SCARG(uap, buf), &ss, sizeof ss);
56943412Snewton		if (error)
57043412Snewton			return error;
57143412Snewton		svr4_to_bsd_msqid_ds(&ss, &bs);
57243412Snewton		error = copyout(&bs, SCARG(&ap, buf), sizeof bs);
57343412Snewton		if (error)
57443412Snewton			return error;
57543412Snewton		return sys_msgctl(p, &ap, retval);
57643412Snewton
57743412Snewton	case SVR4_IPC_RMID:
57843412Snewton		SCARG(&ap, cmd) = IPC_RMID;
57943412Snewton		error = copyin(SCARG(uap, buf), &ss, sizeof ss);
58043412Snewton		if (error)
58143412Snewton			return error;
58243412Snewton		svr4_to_bsd_msqid_ds(&ss, &bs);
58343412Snewton		error = copyout(&bs, SCARG(&ap, buf), sizeof bs);
58443412Snewton		if (error)
58543412Snewton			return error;
58643412Snewton		return sys_msgctl(p, &ap, retval);
58743412Snewton
58843412Snewton	default:
58943412Snewton		return EINVAL;
59043412Snewton	}
59143412Snewton}
59243412Snewton
59343412Snewtonint
59443412Snewtonsvr4_sys_msgsys(p, v, retval)
59543412Snewton	struct proc *p;
59643412Snewton	void *v;
59743412Snewton	register_t *retval;
59843412Snewton{
59943412Snewton	struct svr4_sys_msgsys_args *uap = v;
60043412Snewton
60143412Snewton	DPRINTF(("svr4_msgsys(%d)\n", SCARG(uap, what)));
60243412Snewton
60343412Snewton	switch (SCARG(uap, what)) {
60443412Snewton	case SVR4_msgsnd:
60543412Snewton		return svr4_msgsnd(p, v, retval);
60643412Snewton	case SVR4_msgrcv:
60743412Snewton		return svr4_msgrcv(p, v, retval);
60843412Snewton	case SVR4_msgget:
60943412Snewton		return svr4_msgget(p, v, retval);
61043412Snewton	case SVR4_msgctl:
61143412Snewton		return svr4_msgctl(p, v, retval);
61243412Snewton	default:
61343412Snewton		return EINVAL;
61443412Snewton	}
61543412Snewton}
61643412Snewton#endif
61743412Snewton
61843412Snewton#ifdef SYSVSHM
61943412Snewton
62043412Snewtonstatic void
62143412Snewtonbsd_to_svr4_shmid_ds(bds, sds)
62243412Snewton	const struct shmid_ds *bds;
62343412Snewton	struct svr4_shmid_ds *sds;
62443412Snewton{
62543412Snewton	bsd_to_svr4_ipc_perm(&bds->shm_perm, &sds->shm_perm);
62643412Snewton	sds->shm_segsz = bds->shm_segsz;
62743412Snewton	sds->shm_lkcnt = 0;
62843412Snewton	sds->shm_lpid = bds->shm_lpid;
62943412Snewton	sds->shm_cpid = bds->shm_cpid;
63043412Snewton	sds->shm_amp = bds->shm_internal;
63143412Snewton	sds->shm_nattch = bds->shm_nattch;
63243412Snewton	sds->shm_cnattch = 0;
63343412Snewton	sds->shm_atime = bds->shm_atime;
63443412Snewton	sds->shm_pad1 = 0;
63543412Snewton	sds->shm_dtime = bds->shm_dtime;
63643412Snewton	sds->shm_pad2 = 0;
63743412Snewton	sds->shm_ctime = bds->shm_ctime;
63843412Snewton	sds->shm_pad3 = 0;
63943412Snewton}
64043412Snewton
64143412Snewtonstatic void
64243412Snewtonsvr4_to_bsd_shmid_ds(sds, bds)
64343412Snewton	const struct svr4_shmid_ds *sds;
64443412Snewton	struct shmid_ds *bds;
64543412Snewton{
64643412Snewton	svr4_to_bsd_ipc_perm(&sds->shm_perm, &bds->shm_perm);
64743412Snewton	bds->shm_segsz = sds->shm_segsz;
64843412Snewton	bds->shm_lpid = sds->shm_lpid;
64943412Snewton	bds->shm_cpid = sds->shm_cpid;
65043412Snewton	bds->shm_internal = sds->shm_amp;
65143412Snewton	bds->shm_nattch = sds->shm_nattch;
65243412Snewton	bds->shm_atime = sds->shm_atime;
65343412Snewton	bds->shm_dtime = sds->shm_dtime;
65443412Snewton	bds->shm_ctime = sds->shm_ctime;
65543412Snewton}
65643412Snewton
65743412Snewtonstruct svr4_sys_shmat_args {
65843412Snewton	syscallarg(int) what;
65943412Snewton	syscallarg(int) shmid;
66043412Snewton	syscallarg(void *) shmaddr;
66143412Snewton	syscallarg(int) shmflg;
66243412Snewton};
66343412Snewton
66443412Snewtonstatic int
66543412Snewtonsvr4_shmat(p, v, retval)
66643412Snewton	struct proc *p;
66743412Snewton	void *v;
66843412Snewton	register_t *retval;
66943412Snewton{
67043412Snewton	struct svr4_sys_shmat_args *uap = v;
67143412Snewton	struct sys_shmat_args ap;
67243412Snewton
67343412Snewton	SCARG(&ap, shmid) = SCARG(uap, shmid);
67443412Snewton	SCARG(&ap, shmaddr) = SCARG(uap, shmaddr);
67543412Snewton	SCARG(&ap, shmflg) = SCARG(uap, shmflg);
67643412Snewton
67743412Snewton	return sys_shmat(p, &ap, retval);
67843412Snewton}
67943412Snewton
68043412Snewtonstruct svr4_sys_shmdt_args {
68143412Snewton	syscallarg(int) what;
68243412Snewton	syscallarg(void *) shmaddr;
68343412Snewton};
68443412Snewton
68543412Snewtonstatic int
68643412Snewtonsvr4_shmdt(p, v, retval)
68743412Snewton	struct proc *p;
68843412Snewton	void *v;
68943412Snewton	register_t *retval;
69043412Snewton{
69143412Snewton	struct svr4_sys_shmdt_args *uap = v;
69243412Snewton	struct sys_shmdt_args ap;
69343412Snewton
69443412Snewton	SCARG(&ap, shmaddr) = SCARG(uap, shmaddr);
69543412Snewton
69643412Snewton	return sys_shmdt(p, &ap, retval);
69743412Snewton}
69843412Snewton
69943412Snewtonstruct svr4_sys_shmget_args {
70043412Snewton	syscallarg(int) what;
70143412Snewton	syscallarg(key_t) key;
70243412Snewton	syscallarg(int) size;
70343412Snewton	syscallarg(int) shmflg;
70443412Snewton};
70543412Snewton
70643412Snewtonstatic int
70743412Snewtonsvr4_shmget(p, v, retval)
70843412Snewton	struct proc *p;
70943412Snewton	void *v;
71043412Snewton	register_t *retval;
71143412Snewton{
71243412Snewton	struct svr4_sys_shmget_args *uap = v;
71343412Snewton	struct sys_shmget_args ap;
71443412Snewton
71543412Snewton	SCARG(&ap, key) = SCARG(uap, key);
71643412Snewton	SCARG(&ap, size) = SCARG(uap, size);
71743412Snewton	SCARG(&ap, shmflg) = SCARG(uap, shmflg);
71843412Snewton
71943412Snewton	return sys_shmget(p, &ap, retval);
72043412Snewton}
72143412Snewton
72243412Snewtonstruct svr4_sys_shmctl_args {
72343412Snewton	syscallarg(int) what;
72443412Snewton	syscallarg(int) shmid;
72543412Snewton	syscallarg(int) cmd;
72643412Snewton	syscallarg(struct svr4_shmid_ds *) buf;
72743412Snewton};
72843412Snewton
72943412Snewtonint
73043412Snewtonsvr4_shmctl(p, v, retval)
73143412Snewton	struct proc *p;
73243412Snewton	void *v;
73343412Snewton	register_t *retval;
73443412Snewton{
73543412Snewton	struct svr4_sys_shmctl_args *uap = v;
73643412Snewton	int error;
73743412Snewton	caddr_t sg = stackgap_init(p->p_emul);
73843412Snewton	struct sys_shmctl_args ap;
73943412Snewton	struct shmid_ds bs;
74043412Snewton	struct svr4_shmid_ds ss;
74143412Snewton
74243412Snewton	SCARG(&ap, shmid) = SCARG(uap, shmid);
74343412Snewton
74443412Snewton	if (SCARG(uap, buf) != NULL) {
74543412Snewton		SCARG(&ap, buf) = stackgap_alloc(&sg, sizeof (struct shmid_ds));
74643412Snewton		switch (SCARG(uap, cmd)) {
74743412Snewton		case SVR4_IPC_SET:
74843412Snewton		case SVR4_IPC_RMID:
74943412Snewton		case SVR4_SHM_LOCK:
75043412Snewton		case SVR4_SHM_UNLOCK:
75143412Snewton			error = copyin(SCARG(uap, buf), (caddr_t) &ss,
75243412Snewton			    sizeof ss);
75343412Snewton			if (error)
75443412Snewton				return error;
75543412Snewton			svr4_to_bsd_shmid_ds(&ss, &bs);
75643412Snewton			error = copyout(&bs, SCARG(&ap, buf), sizeof bs);
75743412Snewton			if (error)
75843412Snewton				return error;
75943412Snewton			break;
76043412Snewton		default:
76143412Snewton			break;
76243412Snewton		}
76343412Snewton	}
76443412Snewton	else
76543412Snewton		SCARG(&ap, buf) = NULL;
76643412Snewton
76743412Snewton
76843412Snewton	switch (SCARG(uap, cmd)) {
76943412Snewton	case SVR4_IPC_STAT:
77043412Snewton		SCARG(&ap, cmd) = IPC_STAT;
77143412Snewton		if ((error = sys_shmctl(p, &ap, retval)) != 0)
77243412Snewton			return error;
77343412Snewton		if (SCARG(uap, buf) == NULL)
77443412Snewton			return 0;
77543412Snewton		error = copyin(&bs, SCARG(&ap, buf), sizeof bs);
77643412Snewton		if (error)
77743412Snewton			return error;
77843412Snewton		bsd_to_svr4_shmid_ds(&bs, &ss);
77943412Snewton		return copyout(&ss, SCARG(uap, buf), sizeof ss);
78043412Snewton
78143412Snewton	case SVR4_IPC_SET:
78243412Snewton		SCARG(&ap, cmd) = IPC_SET;
78343412Snewton		return sys_shmctl(p, &ap, retval);
78443412Snewton
78543412Snewton	case SVR4_IPC_RMID:
78643412Snewton	case SVR4_SHM_LOCK:
78743412Snewton	case SVR4_SHM_UNLOCK:
78843412Snewton		switch (SCARG(uap, cmd)) {
78943412Snewton		case SVR4_IPC_RMID:
79043412Snewton			SCARG(&ap, cmd) = IPC_RMID;
79143412Snewton			break;
79243412Snewton		case SVR4_SHM_LOCK:
79343412Snewton			SCARG(&ap, cmd) = SHM_LOCK;
79443412Snewton			break;
79543412Snewton		case SVR4_SHM_UNLOCK:
79643412Snewton			SCARG(&ap, cmd) = SHM_UNLOCK;
79743412Snewton			break;
79843412Snewton		default:
79943412Snewton			return EINVAL;
80043412Snewton		}
80143412Snewton		return sys_shmctl(p, &ap, retval);
80243412Snewton
80343412Snewton	default:
80443412Snewton		return EINVAL;
80543412Snewton	}
80643412Snewton}
80743412Snewton
80843412Snewtonint
80943412Snewtonsvr4_sys_shmsys(p, v, retval)
81043412Snewton	struct proc *p;
81143412Snewton	void *v;
81243412Snewton	register_t *retval;
81343412Snewton{
81443412Snewton	struct svr4_sys_shmsys_args *uap = v;
81543412Snewton
81643412Snewton	DPRINTF(("svr4_shmsys(%d)\n", SCARG(uap, what)));
81743412Snewton
81843412Snewton	switch (SCARG(uap, what)) {
81943412Snewton	case SVR4_shmat:
82043412Snewton		return svr4_shmat(p, v, retval);
82143412Snewton	case SVR4_shmdt:
82243412Snewton		return svr4_shmdt(p, v, retval);
82343412Snewton	case SVR4_shmget:
82443412Snewton		return svr4_shmget(p, v, retval);
82543412Snewton	case SVR4_shmctl:
82643412Snewton		return svr4_shmctl(p, v, retval);
82743412Snewton	default:
82843412Snewton		return ENOSYS;
82943412Snewton	}
83043412Snewton}
83143412Snewton#endif /* SYSVSHM */
832