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