svr4_ipc.c revision 65302
1/*
2 * $FreeBSD: head/sys/compat/svr4/svr4_ipc.c 65302 2000-08-31 22:54:09Z obrien $
3 *	Derived from:
4 *	$NetBSD: svr4_ipc.c,v 1.7 1998/10/19 22:43:00 tron Exp $	*/
5
6/*-
7 * Original copyright:
8 *
9 * Copyright (c) 1995 The NetBSD Foundation, Inc.
10 * All rights reserved.
11 *
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Christos Zoulas.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 *    must display the following acknowledgement:
25 *        This product includes software developed by the NetBSD
26 *        Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 *    contributors may be used to endorse or promote products derived
29 *    from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44/*
45 * Portions of this code have been derived from software contributed
46 * to the FreeBSD Project by Mark Newton.
47 *
48 * Copyright (c) 1999 Mark Newton
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 *    notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 *    notice, this list of conditions and the following disclaimer in the
58 *    documentation and/or other materials provided with the distribution.
59 * 3. The name of the author may not be used to endorse or promote products
60 *    derived from this software without specific prior written permission
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
63 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
64 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
65 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
66 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
67 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
68 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
69 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
71 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72 *
73 * XXX- This code is presently a no-op on FreeBSD (and isn't compiled due
74 * to preprocessor conditionals).  A nice project for a kernel hacking
75 * novice might be to MakeItGo, but I have more important fish to fry
76 * at present.
77 *
78 */
79
80#include <sys/types.h>
81#include <sys/param.h>
82#include <sys/proc.h>
83#include <sys/time.h>
84
85#include <compat/svr4/svr4.h>
86#include <compat/svr4/svr4_types.h>
87#include <compat/svr4/svr4_signal.h>
88#include <compat/svr4/svr4_proto.h>
89#include <compat/svr4/svr4_util.h>
90#include <compat/svr4/svr4_ipc.h>
91
92#if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
93static void svr4_to_bsd_ipc_perm __P((const struct svr4_ipc_perm *,
94				      struct ipc_perm *));
95static void bsd_to_svr4_ipc_perm __P((const struct ipc_perm *,
96				      struct svr4_ipc_perm *));
97#endif
98
99#ifdef SYSVSEM
100static void bsd_to_svr4_semid_ds __P((const struct semid_ds *,
101				      struct svr4_semid_ds *));
102static void svr4_to_bsd_semid_ds __P((const struct svr4_semid_ds *,
103				      struct semid_ds *));
104static int svr4_setsemun __P((caddr_t *sgp, union semun **argp,
105			      union semun *usp));
106static int svr4_semop __P((struct proc *, void *, register_t *));
107static int svr4_semget __P((struct proc *, void *, register_t *));
108static int svr4_semctl __P((struct proc *, void *, register_t *));
109#endif
110
111#ifdef SYSVMSG
112static void bsd_to_svr4_msqid_ds __P((const struct msqid_ds *,
113				      struct svr4_msqid_ds *));
114static void svr4_to_bsd_msqid_ds __P((const struct svr4_msqid_ds *,
115				      struct msqid_ds *));
116static int svr4_msgsnd __P((struct proc *, void *, register_t *));
117static int svr4_msgrcv __P((struct proc *, void *, register_t *));
118static int svr4_msgget __P((struct proc *, void *, register_t *));
119static int svr4_msgctl __P((struct proc *, void *, register_t *));
120#endif
121
122#ifdef SYSVSHM
123static void bsd_to_svr4_shmid_ds __P((const struct shmid_ds *,
124				      struct svr4_shmid_ds *));
125static void svr4_to_bsd_shmid_ds __P((const struct svr4_shmid_ds *,
126				      struct shmid_ds *));
127static int svr4_shmat __P((struct proc *, void *, register_t *));
128static int svr4_shmdt __P((struct proc *, void *, register_t *));
129static int svr4_shmget __P((struct proc *, void *, register_t *));
130static int svr4_shmctl __P((struct proc *, void *, register_t *));
131#endif
132
133#if defined(SYSVMSG) || defined(SYSVSHM) || defined(SYSVSEM)
134
135static void
136svr4_to_bsd_ipc_perm(spp, bpp)
137	const struct svr4_ipc_perm *spp;
138	struct ipc_perm *bpp;
139{
140	bpp->key = spp->key;
141	bpp->uid = spp->uid;
142	bpp->gid = spp->gid;
143	bpp->cuid = spp->cuid;
144	bpp->cgid = spp->cgid;
145	bpp->mode = spp->mode;
146	bpp->seq = spp->seq;
147}
148
149static void
150bsd_to_svr4_ipc_perm(bpp, spp)
151	const struct ipc_perm *bpp;
152	struct svr4_ipc_perm *spp;
153{
154	spp->key = bpp->key;
155	spp->uid = bpp->uid;
156	spp->gid = bpp->gid;
157	spp->cuid = bpp->cuid;
158	spp->cgid = bpp->cgid;
159	spp->mode = bpp->mode;
160	spp->seq = bpp->seq;
161}
162#endif
163
164#ifdef SYSVSEM
165static void
166bsd_to_svr4_semid_ds(bds, sds)
167	const struct semid_ds *bds;
168	struct svr4_semid_ds *sds;
169{
170	bsd_to_svr4_ipc_perm(&bds->sem_perm, &sds->sem_perm);
171	sds->sem_base = (struct svr4_sem *) bds->sem_base;
172	sds->sem_nsems = bds->sem_nsems;
173	sds->sem_otime = bds->sem_otime;
174	sds->sem_pad1 = bds->sem_pad1;
175	sds->sem_ctime = bds->sem_ctime;
176	sds->sem_pad2 = bds->sem_pad2;
177}
178
179static void
180svr4_to_bsd_semid_ds(sds, bds)
181	const struct svr4_semid_ds *sds;
182	struct semid_ds *bds;
183{
184	svr4_to_bsd_ipc_perm(&sds->sem_perm, &bds->sem_perm);
185	bds->sem_base = (struct sem *) bds->sem_base;
186	bds->sem_nsems = sds->sem_nsems;
187	bds->sem_otime = sds->sem_otime;
188	bds->sem_pad1 = sds->sem_pad1;
189	bds->sem_ctime = sds->sem_ctime;
190	bds->sem_pad2 = sds->sem_pad2;
191}
192
193static int
194svr4_setsemun(sgp, argp, usp)
195	caddr_t *sgp;
196	union semun **argp;
197	union semun *usp;
198{
199	*argp = stackgap_alloc(sgp, sizeof(union semun));
200	return copyout((caddr_t)usp, *argp, sizeof(union semun));
201}
202
203struct svr4_sys_semctl_args {
204	syscallarg(int) what;
205	syscallarg(int) semid;
206	syscallarg(int) semnum;
207	syscallarg(int) cmd;
208	syscallarg(union semun) arg;
209};
210
211static int
212svr4_semctl(p, v, retval)
213	struct proc *p;
214	void *v;
215	register_t *retval;
216{
217	int error;
218	struct svr4_sys_semctl_args *uap = v;
219	struct sys___semctl_args ap;
220	struct svr4_semid_ds ss;
221	struct semid_ds bs, *bsp;
222	caddr_t sg = stackgap_init(p->p_emul);
223
224	SCARG(&ap, semid) = SCARG(uap, semid);
225	SCARG(&ap, semnum) = SCARG(uap, semnum);
226
227	switch (SCARG(uap, cmd)) {
228	case SVR4_SEM_GETZCNT:
229	case SVR4_SEM_GETNCNT:
230	case SVR4_SEM_GETPID:
231	case SVR4_SEM_GETVAL:
232		switch (SCARG(uap, cmd)) {
233		case SVR4_SEM_GETZCNT:
234			SCARG(&ap, cmd) = GETZCNT;
235			break;
236		case SVR4_SEM_GETNCNT:
237			SCARG(&ap, cmd) = GETNCNT;
238			break;
239		case SVR4_SEM_GETPID:
240			SCARG(&ap, cmd) = GETPID;
241			break;
242		case SVR4_SEM_GETVAL:
243			SCARG(&ap, cmd) = GETVAL;
244			break;
245		}
246		return sys___semctl(p, &ap, retval);
247
248	case SVR4_SEM_SETVAL:
249		error = svr4_setsemun(&sg, &SCARG(&ap, arg), &SCARG(uap, arg));
250		if (error)
251			return error;
252		SCARG(&ap, cmd) = SETVAL;
253		return sys___semctl(p, &ap, retval);
254
255	case SVR4_SEM_GETALL:
256		error = svr4_setsemun(&sg, &SCARG(&ap, arg), &SCARG(uap, arg));
257		if (error)
258			return error;
259		SCARG(&ap, cmd) = GETVAL;
260		return sys___semctl(p, &ap, retval);
261
262	case SVR4_SEM_SETALL:
263		error = svr4_setsemun(&sg, &SCARG(&ap, arg), &SCARG(uap, arg));
264		if (error)
265			return error;
266		SCARG(&ap, cmd) = SETVAL;
267		return sys___semctl(p, &ap, retval);
268
269	case SVR4_IPC_STAT:
270                SCARG(&ap, cmd) = IPC_STAT;
271		bsp = stackgap_alloc(&sg, sizeof(bs));
272		error = svr4_setsemun(&sg, &SCARG(&ap, arg),
273				      (union semun *)&bsp);
274		if (error)
275			return error;
276                if ((error = sys___semctl(p, &ap, retval)) != 0)
277                        return error;
278		error = copyin((caddr_t)bsp, (caddr_t)&bs, sizeof(bs));
279                if (error)
280                        return error;
281                bsd_to_svr4_semid_ds(&bs, &ss);
282		return copyout(&ss, SCARG(uap, arg).buf, sizeof(ss));
283
284	case SVR4_IPC_SET:
285		SCARG(&ap, cmd) = IPC_SET;
286		bsp = stackgap_alloc(&sg, sizeof(bs));
287		error = svr4_setsemun(&sg, &SCARG(&ap, arg),
288				      (union semun *)&bsp);
289		if (error)
290			return error;
291		error = copyin(SCARG(uap, arg).buf, (caddr_t) &ss, sizeof ss);
292                if (error)
293                        return error;
294                svr4_to_bsd_semid_ds(&ss, &bs);
295		error = copyout(&bs, bsp, sizeof(bs));
296                if (error)
297                        return error;
298		return sys___semctl(p, &ap, retval);
299
300	case SVR4_IPC_RMID:
301		SCARG(&ap, cmd) = IPC_RMID;
302		bsp = stackgap_alloc(&sg, sizeof(bs));
303		error = svr4_setsemun(&sg, &SCARG(&ap, arg),
304				      (union semun *)&bsp);
305		if (error)
306			return error;
307		error = copyin(SCARG(uap, arg).buf, &ss, sizeof ss);
308                if (error)
309                        return error;
310                svr4_to_bsd_semid_ds(&ss, &bs);
311		error = copyout(&bs, bsp, sizeof(bs));
312		if (error)
313			return error;
314		return sys___semctl(p, &ap, retval);
315
316	default:
317		return EINVAL;
318	}
319}
320
321struct svr4_sys_semget_args {
322	syscallarg(int) what;
323	syscallarg(svr4_key_t) key;
324	syscallarg(int) nsems;
325	syscallarg(int) semflg;
326};
327
328static int
329svr4_semget(p, v, retval)
330	struct proc *p;
331	void *v;
332	register_t *retval;
333{
334	struct svr4_sys_semget_args *uap = v;
335	struct sys_semget_args ap;
336
337	SCARG(&ap, key) = SCARG(uap, key);
338	SCARG(&ap, nsems) = SCARG(uap, nsems);
339	SCARG(&ap, semflg) = SCARG(uap, semflg);
340
341	return sys_semget(p, &ap, retval);
342}
343
344struct svr4_sys_semop_args {
345	syscallarg(int) what;
346	syscallarg(int) semid;
347	syscallarg(struct svr4_sembuf *) sops;
348	syscallarg(u_int) nsops;
349};
350
351static int
352svr4_semop(p, v, retval)
353	struct proc *p;
354	void *v;
355	register_t *retval;
356{
357	struct svr4_sys_semop_args *uap = v;
358	struct sys_semop_args ap;
359
360	SCARG(&ap, semid) = SCARG(uap, semid);
361	/* These are the same */
362	SCARG(&ap, sops) = (struct sembuf *) SCARG(uap, sops);
363	SCARG(&ap, nsops) = SCARG(uap, nsops);
364
365	return sys_semop(p, &ap, retval);
366}
367
368int
369svr4_sys_semsys(p, v, retval)
370	struct proc *p;
371	void *v;
372	register_t *retval;
373{
374	struct svr4_sys_semsys_args *uap = v;
375
376	DPRINTF(("svr4_semsys(%d)\n", SCARG(uap, what)));
377
378	switch (SCARG(uap, what)) {
379	case SVR4_semctl:
380		return svr4_semctl(p, v, retval);
381	case SVR4_semget:
382		return svr4_semget(p, v, retval);
383	case SVR4_semop:
384		return svr4_semop(p, v, retval);
385	default:
386		return EINVAL;
387	}
388}
389#endif
390
391#ifdef SYSVMSG
392static void
393bsd_to_svr4_msqid_ds(bds, sds)
394	const struct msqid_ds *bds;
395	struct svr4_msqid_ds *sds;
396{
397	bsd_to_svr4_ipc_perm(&bds->msg_perm, &sds->msg_perm);
398	sds->msg_first = (struct svr4_msg *) bds->msg_first;
399	sds->msg_last = (struct svr4_msg *) bds->msg_last;
400	sds->msg_cbytes = bds->msg_cbytes;
401	sds->msg_qnum = bds->msg_qnum;
402	sds->msg_qbytes = bds->msg_qbytes;
403	sds->msg_lspid = bds->msg_lspid;
404	sds->msg_lrpid = bds->msg_lrpid;
405	sds->msg_stime = bds->msg_stime;
406	sds->msg_pad1 = bds->msg_pad1;
407	sds->msg_rtime = bds->msg_rtime;
408	sds->msg_pad2 = bds->msg_pad2;
409	sds->msg_ctime = bds->msg_ctime;
410	sds->msg_pad3 = bds->msg_pad3;
411
412	/* use the padding for the rest of the fields */
413	{
414		const short *pad = (const short *) bds->msg_pad4;
415		sds->msg_cv = pad[0];
416		sds->msg_qnum_cv = pad[1];
417	}
418}
419
420static void
421svr4_to_bsd_msqid_ds(sds, bds)
422	const struct svr4_msqid_ds *sds;
423	struct msqid_ds *bds;
424{
425	svr4_to_bsd_ipc_perm(&sds->msg_perm, &bds->msg_perm);
426	bds->msg_first = (struct msg *) sds->msg_first;
427	bds->msg_last = (struct msg *) sds->msg_last;
428	bds->msg_cbytes = sds->msg_cbytes;
429	bds->msg_qnum = sds->msg_qnum;
430	bds->msg_qbytes = sds->msg_qbytes;
431	bds->msg_lspid = sds->msg_lspid;
432	bds->msg_lrpid = sds->msg_lrpid;
433	bds->msg_stime = sds->msg_stime;
434	bds->msg_pad1 = sds->msg_pad1;
435	bds->msg_rtime = sds->msg_rtime;
436	bds->msg_pad2 = sds->msg_pad2;
437	bds->msg_ctime = sds->msg_ctime;
438	bds->msg_pad3 = sds->msg_pad3;
439
440	/* use the padding for the rest of the fields */
441	{
442		short *pad = (short *) bds->msg_pad4;
443		pad[0] = sds->msg_cv;
444		pad[1] = sds->msg_qnum_cv;
445	}
446}
447
448struct svr4_sys_msgsnd_args {
449	syscallarg(int) what;
450	syscallarg(int) msqid;
451	syscallarg(void *) msgp;
452	syscallarg(size_t) msgsz;
453	syscallarg(int) msgflg;
454};
455
456static int
457svr4_msgsnd(p, v, retval)
458	struct proc *p;
459	void *v;
460	register_t *retval;
461{
462	struct svr4_sys_msgsnd_args *uap = v;
463	struct sys_msgsnd_args ap;
464
465	SCARG(&ap, msqid) = SCARG(uap, msqid);
466	SCARG(&ap, msgp) = SCARG(uap, msgp);
467	SCARG(&ap, msgsz) = SCARG(uap, msgsz);
468	SCARG(&ap, msgflg) = SCARG(uap, msgflg);
469
470	return sys_msgsnd(p, &ap, retval);
471}
472
473struct svr4_sys_msgrcv_args {
474	syscallarg(int) what;
475	syscallarg(int) msqid;
476	syscallarg(void *) msgp;
477	syscallarg(size_t) msgsz;
478	syscallarg(long) msgtyp;
479	syscallarg(int) msgflg;
480};
481
482static int
483svr4_msgrcv(p, v, retval)
484	struct proc *p;
485	void *v;
486	register_t *retval;
487{
488	struct svr4_sys_msgrcv_args *uap = v;
489	struct sys_msgrcv_args ap;
490
491	SCARG(&ap, msqid) = SCARG(uap, msqid);
492	SCARG(&ap, msgp) = SCARG(uap, msgp);
493	SCARG(&ap, msgsz) = SCARG(uap, msgsz);
494	SCARG(&ap, msgtyp) = SCARG(uap, msgtyp);
495	SCARG(&ap, msgflg) = SCARG(uap, msgflg);
496
497	return sys_msgrcv(p, &ap, retval);
498}
499
500struct svr4_sys_msgget_args {
501	syscallarg(int) what;
502	syscallarg(svr4_key_t) key;
503	syscallarg(int) msgflg;
504};
505
506static int
507svr4_msgget(p, v, retval)
508	struct proc *p;
509	void *v;
510	register_t *retval;
511{
512	struct svr4_sys_msgget_args *uap = v;
513	struct sys_msgget_args ap;
514
515	SCARG(&ap, key) = SCARG(uap, key);
516	SCARG(&ap, msgflg) = SCARG(uap, msgflg);
517
518	return sys_msgget(p, &ap, retval);
519}
520
521struct svr4_sys_msgctl_args {
522	syscallarg(int) what;
523	syscallarg(int) msqid;
524	syscallarg(int) cmd;
525	syscallarg(struct svr4_msqid_ds *) buf;
526};
527
528static int
529svr4_msgctl(p, v, retval)
530	struct proc *p;
531	void *v;
532	register_t *retval;
533{
534	int error;
535	struct svr4_sys_msgctl_args *uap = v;
536	struct sys_msgctl_args ap;
537	struct svr4_msqid_ds ss;
538	struct msqid_ds bs;
539	caddr_t sg = stackgap_init(p->p_emul);
540
541	SCARG(&ap, msqid) = SCARG(uap, msqid);
542	SCARG(&ap, cmd) = SCARG(uap, cmd);
543	SCARG(&ap, buf) = stackgap_alloc(&sg, sizeof(bs));
544
545	switch (SCARG(uap, cmd)) {
546	case SVR4_IPC_STAT:
547		SCARG(&ap, cmd) = IPC_STAT;
548		if ((error = sys_msgctl(p, &ap, retval)) != 0)
549			return error;
550		error = copyin(&bs, SCARG(&ap, buf), sizeof bs);
551		if (error)
552			return error;
553		bsd_to_svr4_msqid_ds(&bs, &ss);
554		return copyout(&ss, SCARG(uap, buf), sizeof ss);
555
556	case SVR4_IPC_SET:
557		SCARG(&ap, cmd) = IPC_SET;
558		error = copyin(SCARG(uap, buf), &ss, sizeof ss);
559		if (error)
560			return error;
561		svr4_to_bsd_msqid_ds(&ss, &bs);
562		error = copyout(&bs, SCARG(&ap, buf), sizeof bs);
563		if (error)
564			return error;
565		return sys_msgctl(p, &ap, retval);
566
567	case SVR4_IPC_RMID:
568		SCARG(&ap, cmd) = IPC_RMID;
569		error = copyin(SCARG(uap, buf), &ss, sizeof ss);
570		if (error)
571			return error;
572		svr4_to_bsd_msqid_ds(&ss, &bs);
573		error = copyout(&bs, SCARG(&ap, buf), sizeof bs);
574		if (error)
575			return error;
576		return sys_msgctl(p, &ap, retval);
577
578	default:
579		return EINVAL;
580	}
581}
582
583int
584svr4_sys_msgsys(p, v, retval)
585	struct proc *p;
586	void *v;
587	register_t *retval;
588{
589	struct svr4_sys_msgsys_args *uap = v;
590
591	DPRINTF(("svr4_msgsys(%d)\n", SCARG(uap, what)));
592
593	switch (SCARG(uap, what)) {
594	case SVR4_msgsnd:
595		return svr4_msgsnd(p, v, retval);
596	case SVR4_msgrcv:
597		return svr4_msgrcv(p, v, retval);
598	case SVR4_msgget:
599		return svr4_msgget(p, v, retval);
600	case SVR4_msgctl:
601		return svr4_msgctl(p, v, retval);
602	default:
603		return EINVAL;
604	}
605}
606#endif
607
608#ifdef SYSVSHM
609
610static void
611bsd_to_svr4_shmid_ds(bds, sds)
612	const struct shmid_ds *bds;
613	struct svr4_shmid_ds *sds;
614{
615	bsd_to_svr4_ipc_perm(&bds->shm_perm, &sds->shm_perm);
616	sds->shm_segsz = bds->shm_segsz;
617	sds->shm_lkcnt = 0;
618	sds->shm_lpid = bds->shm_lpid;
619	sds->shm_cpid = bds->shm_cpid;
620	sds->shm_amp = bds->shm_internal;
621	sds->shm_nattch = bds->shm_nattch;
622	sds->shm_cnattch = 0;
623	sds->shm_atime = bds->shm_atime;
624	sds->shm_pad1 = 0;
625	sds->shm_dtime = bds->shm_dtime;
626	sds->shm_pad2 = 0;
627	sds->shm_ctime = bds->shm_ctime;
628	sds->shm_pad3 = 0;
629}
630
631static void
632svr4_to_bsd_shmid_ds(sds, bds)
633	const struct svr4_shmid_ds *sds;
634	struct shmid_ds *bds;
635{
636	svr4_to_bsd_ipc_perm(&sds->shm_perm, &bds->shm_perm);
637	bds->shm_segsz = sds->shm_segsz;
638	bds->shm_lpid = sds->shm_lpid;
639	bds->shm_cpid = sds->shm_cpid;
640	bds->shm_internal = sds->shm_amp;
641	bds->shm_nattch = sds->shm_nattch;
642	bds->shm_atime = sds->shm_atime;
643	bds->shm_dtime = sds->shm_dtime;
644	bds->shm_ctime = sds->shm_ctime;
645}
646
647struct svr4_sys_shmat_args {
648	syscallarg(int) what;
649	syscallarg(int) shmid;
650	syscallarg(void *) shmaddr;
651	syscallarg(int) shmflg;
652};
653
654static int
655svr4_shmat(p, v, retval)
656	struct proc *p;
657	void *v;
658	register_t *retval;
659{
660	struct svr4_sys_shmat_args *uap = v;
661	struct sys_shmat_args ap;
662
663	SCARG(&ap, shmid) = SCARG(uap, shmid);
664	SCARG(&ap, shmaddr) = SCARG(uap, shmaddr);
665	SCARG(&ap, shmflg) = SCARG(uap, shmflg);
666
667	return sys_shmat(p, &ap, retval);
668}
669
670struct svr4_sys_shmdt_args {
671	syscallarg(int) what;
672	syscallarg(void *) shmaddr;
673};
674
675static int
676svr4_shmdt(p, v, retval)
677	struct proc *p;
678	void *v;
679	register_t *retval;
680{
681	struct svr4_sys_shmdt_args *uap = v;
682	struct sys_shmdt_args ap;
683
684	SCARG(&ap, shmaddr) = SCARG(uap, shmaddr);
685
686	return sys_shmdt(p, &ap, retval);
687}
688
689struct svr4_sys_shmget_args {
690	syscallarg(int) what;
691	syscallarg(key_t) key;
692	syscallarg(int) size;
693	syscallarg(int) shmflg;
694};
695
696static int
697svr4_shmget(p, v, retval)
698	struct proc *p;
699	void *v;
700	register_t *retval;
701{
702	struct svr4_sys_shmget_args *uap = v;
703	struct sys_shmget_args ap;
704
705	SCARG(&ap, key) = SCARG(uap, key);
706	SCARG(&ap, size) = SCARG(uap, size);
707	SCARG(&ap, shmflg) = SCARG(uap, shmflg);
708
709	return sys_shmget(p, &ap, retval);
710}
711
712struct svr4_sys_shmctl_args {
713	syscallarg(int) what;
714	syscallarg(int) shmid;
715	syscallarg(int) cmd;
716	syscallarg(struct svr4_shmid_ds *) buf;
717};
718
719int
720svr4_shmctl(p, v, retval)
721	struct proc *p;
722	void *v;
723	register_t *retval;
724{
725	struct svr4_sys_shmctl_args *uap = v;
726	int error;
727	caddr_t sg = stackgap_init(p->p_emul);
728	struct sys_shmctl_args ap;
729	struct shmid_ds bs;
730	struct svr4_shmid_ds ss;
731
732	SCARG(&ap, shmid) = SCARG(uap, shmid);
733
734	if (SCARG(uap, buf) != NULL) {
735		SCARG(&ap, buf) = stackgap_alloc(&sg, sizeof (struct shmid_ds));
736		switch (SCARG(uap, cmd)) {
737		case SVR4_IPC_SET:
738		case SVR4_IPC_RMID:
739		case SVR4_SHM_LOCK:
740		case SVR4_SHM_UNLOCK:
741			error = copyin(SCARG(uap, buf), (caddr_t) &ss,
742			    sizeof ss);
743			if (error)
744				return error;
745			svr4_to_bsd_shmid_ds(&ss, &bs);
746			error = copyout(&bs, SCARG(&ap, buf), sizeof bs);
747			if (error)
748				return error;
749			break;
750		default:
751			break;
752		}
753	}
754	else
755		SCARG(&ap, buf) = NULL;
756
757
758	switch (SCARG(uap, cmd)) {
759	case SVR4_IPC_STAT:
760		SCARG(&ap, cmd) = IPC_STAT;
761		if ((error = sys_shmctl(p, &ap, retval)) != 0)
762			return error;
763		if (SCARG(uap, buf) == NULL)
764			return 0;
765		error = copyin(&bs, SCARG(&ap, buf), sizeof bs);
766		if (error)
767			return error;
768		bsd_to_svr4_shmid_ds(&bs, &ss);
769		return copyout(&ss, SCARG(uap, buf), sizeof ss);
770
771	case SVR4_IPC_SET:
772		SCARG(&ap, cmd) = IPC_SET;
773		return sys_shmctl(p, &ap, retval);
774
775	case SVR4_IPC_RMID:
776	case SVR4_SHM_LOCK:
777	case SVR4_SHM_UNLOCK:
778		switch (SCARG(uap, cmd)) {
779		case SVR4_IPC_RMID:
780			SCARG(&ap, cmd) = IPC_RMID;
781			break;
782		case SVR4_SHM_LOCK:
783			SCARG(&ap, cmd) = SHM_LOCK;
784			break;
785		case SVR4_SHM_UNLOCK:
786			SCARG(&ap, cmd) = SHM_UNLOCK;
787			break;
788		default:
789			return EINVAL;
790		}
791		return sys_shmctl(p, &ap, retval);
792
793	default:
794		return EINVAL;
795	}
796}
797
798int
799svr4_sys_shmsys(p, v, retval)
800	struct proc *p;
801	void *v;
802	register_t *retval;
803{
804	struct svr4_sys_shmsys_args *uap = v;
805
806	DPRINTF(("svr4_shmsys(%d)\n", SCARG(uap, what)));
807
808	switch (SCARG(uap, what)) {
809	case SVR4_shmat:
810		return svr4_shmat(p, v, retval);
811	case SVR4_shmdt:
812		return svr4_shmdt(p, v, retval);
813	case SVR4_shmget:
814		return svr4_shmget(p, v, retval);
815	case SVR4_shmctl:
816		return svr4_shmctl(p, v, retval);
817	default:
818		return ENOSYS;
819	}
820}
821#endif /* SYSVSHM */
822