Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / emulation / ibcs2 / i386 / ibcs2_ipc.c
1 /*
2  * Copyright (c) 1995 Scott Bartram
3  * Copyright (c) 1995 Steven Wallace
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/i386/ibcs2/ibcs2_ipc.c,v 1.15 1999/08/28 00:43:57 peter Exp $
26  * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_ipc.c,v 1.2 2003/06/17 04:28:35 dillon Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/msg.h>
32 #include <sys/sem.h>
33 #include <sys/shm.h>
34 #include <sys/sysproto.h>
35
36 #include <i386/ibcs2/ibcs2_types.h>
37 #include <i386/ibcs2/ibcs2_signal.h>
38 #include <i386/ibcs2/ibcs2_proto.h>
39 #include <i386/ibcs2/ibcs2_util.h>
40 #include <i386/ibcs2/ibcs2_ipc.h>
41
42 #define IBCS2_IPC_RMID  0
43 #define IBCS2_IPC_SET   1
44 #define IBCS2_IPC_STAT  2
45 #define IBCS2_SETVAL    8
46
47
48
49 static void cvt_msqid2imsqid __P((struct msqid_ds *, struct ibcs2_msqid_ds *));
50 static void cvt_imsqid2msqid __P((struct ibcs2_msqid_ds *, struct msqid_ds *));
51 #ifdef unused
52 static void cvt_sem2isem     __P((struct sem *, struct ibcs2_sem *));
53 static void cvt_isem2sem     __P((struct ibcs2_sem *, struct sem *));
54 #endif
55 static void cvt_semid2isemid __P((struct semid_ds *, struct ibcs2_semid_ds *));
56 static void cvt_isemid2semid __P((struct ibcs2_semid_ds *, struct semid_ds *));
57 static void cvt_shmid2ishmid __P((struct shmid_ds *, struct ibcs2_shmid_ds *));
58 static void cvt_ishmid2shmid __P((struct ibcs2_shmid_ds *, struct shmid_ds *));
59 static void cvt_perm2iperm   __P((struct ipc_perm *, struct ibcs2_ipc_perm *));
60 static void cvt_iperm2perm   __P((struct ibcs2_ipc_perm *, struct ipc_perm *));
61
62
63 /*
64  * iBCS2 msgsys call
65  */
66
67 static void
68 cvt_msqid2imsqid(bp, ibp)
69 struct msqid_ds *bp;
70 struct ibcs2_msqid_ds *ibp;
71 {
72         cvt_perm2iperm(&bp->msg_perm, &ibp->msg_perm);
73         ibp->msg_first = bp->msg_first;
74         ibp->msg_last = bp->msg_last;
75         ibp->msg_cbytes = (u_short)bp->msg_cbytes;
76         ibp->msg_qnum = (u_short)bp->msg_qnum;
77         ibp->msg_qbytes = (u_short)bp->msg_qbytes;
78         ibp->msg_lspid = (u_short)bp->msg_lspid;
79         ibp->msg_lrpid = (u_short)bp->msg_lrpid;
80         ibp->msg_stime = bp->msg_stime;
81         ibp->msg_rtime = bp->msg_rtime;
82         ibp->msg_ctime = bp->msg_ctime;
83         return;
84 }
85
86 static void
87 cvt_imsqid2msqid(ibp, bp)
88 struct ibcs2_msqid_ds *ibp;
89 struct msqid_ds *bp;
90 {
91         cvt_iperm2perm(&ibp->msg_perm, &bp->msg_perm);
92         bp->msg_first = ibp->msg_first;
93         bp->msg_last = ibp->msg_last;
94         bp->msg_cbytes = ibp->msg_cbytes;
95         bp->msg_qnum = ibp->msg_qnum;
96         bp->msg_qbytes = ibp->msg_qbytes;
97         bp->msg_lspid = ibp->msg_lspid;
98         bp->msg_lrpid = ibp->msg_lrpid;
99         bp->msg_stime = ibp->msg_stime;
100         bp->msg_rtime = ibp->msg_rtime;
101         bp->msg_ctime = ibp->msg_ctime;
102         return;
103 }
104
105 int
106 ibcs2_msgsys(p, uap)
107         struct proc *p;
108         struct ibcs2_msgsys_args *uap;
109 {
110         switch (SCARG(uap, which)) {
111         case 0:                         /* msgget */
112                 SCARG(uap, which) = 1;
113                 return msgsys(p, (struct msgsys_args *)uap);
114         case 1: {                       /* msgctl */
115                 int error;
116                 struct msgsys_args margs;
117                 caddr_t sg = stackgap_init();
118
119                 SCARG(&margs, which) = 0;
120                 SCARG(&margs, a2) = SCARG(uap, a2);
121                 SCARG(&margs, a4) =
122                     (int)stackgap_alloc(&sg, sizeof(struct msqid_ds));
123                 SCARG(&margs, a3) = SCARG(uap, a3);
124                 switch (SCARG(&margs, a3)) {
125                 case IBCS2_IPC_STAT:
126                         error = msgsys(p, &margs);
127                         if (!error)
128                                 cvt_msqid2imsqid(
129                                     (struct msqid_ds *)SCARG(&margs, a4),
130                                     (struct ibcs2_msqid_ds *)SCARG(uap, a4));
131                         return error;
132                 case IBCS2_IPC_SET:
133                         cvt_imsqid2msqid((struct ibcs2_msqid_ds *)SCARG(uap,
134                                                                         a4),
135                                          (struct msqid_ds *)SCARG(&margs, a4));
136                         return msgsys(p, &margs);
137                 case IBCS2_IPC_RMID:
138                         return msgsys(p, &margs);
139                 }
140                 return EINVAL;
141         }
142         case 2:                         /* msgrcv */
143                 SCARG(uap, which) = 3;
144                 return msgsys(p, (struct msgsys_args *)uap);
145         case 3:                         /* msgsnd */
146                 SCARG(uap, which) = 2;
147                 return msgsys(p, (struct msgsys_args *)uap);
148         default:
149                 return EINVAL;
150         }
151 }
152
153 /*
154  * iBCS2 semsys call
155  */
156 #ifdef unused
157 static void
158 cvt_sem2isem(bp, ibp)
159 struct sem *bp;
160 struct ibcs2_sem *ibp;
161 {
162         ibp->semval = bp->semval;
163         ibp->sempid = bp->sempid;
164         ibp->semncnt = bp->semncnt;
165         ibp->semzcnt = bp->semzcnt;
166         return;
167 }
168
169 static void
170 cvt_isem2sem(ibp, bp)
171 struct ibcs2_sem *ibp;
172 struct sem *bp;
173 {
174         bp->semval = ibp->semval;
175         bp->sempid = ibp->sempid;
176         bp->semncnt = ibp->semncnt;
177         bp->semzcnt = ibp->semzcnt;
178         return;
179 }
180 #endif
181
182 static void
183 cvt_iperm2perm(ipp, pp)
184 struct ibcs2_ipc_perm *ipp;
185 struct ipc_perm *pp;
186 {
187         pp->uid = ipp->uid;
188         pp->gid = ipp->gid;
189         pp->cuid = ipp->cuid;
190         pp->cgid = ipp->cgid;
191         pp->mode = ipp->mode;
192         pp->seq = ipp->seq;
193         pp->key = ipp->key;
194 }
195
196 static void
197 cvt_perm2iperm(pp, ipp)
198 struct ipc_perm *pp;
199 struct ibcs2_ipc_perm *ipp;
200 {
201         ipp->uid = pp->uid;
202         ipp->gid = pp->gid;
203         ipp->cuid = pp->cuid;
204         ipp->cgid = pp->cgid;
205         ipp->mode = pp->mode;
206         ipp->seq = pp->seq;
207         ipp->key = pp->key;
208 }
209
210 static void
211 cvt_semid2isemid(bp, ibp)
212 struct semid_ds *bp;
213 struct ibcs2_semid_ds *ibp;
214 {
215         cvt_perm2iperm(&bp->sem_perm, &ibp->sem_perm);
216         ibp->sem_base = (struct ibcs2_sem *)bp->sem_base;
217         ibp->sem_nsems = bp->sem_nsems;
218         ibp->sem_otime = bp->sem_otime;
219         ibp->sem_ctime = bp->sem_ctime;
220         return;
221 }
222
223 static void
224 cvt_isemid2semid(ibp, bp)
225 struct ibcs2_semid_ds *ibp;
226 struct semid_ds *bp;
227 {
228         cvt_iperm2perm(&ibp->sem_perm, &bp->sem_perm);
229         bp->sem_base = (struct sem *)ibp->sem_base;
230         bp->sem_nsems = ibp->sem_nsems;
231         bp->sem_otime = ibp->sem_otime;
232         bp->sem_ctime = ibp->sem_ctime;
233         return;
234 }
235
236 int
237 ibcs2_semsys(p, uap)
238         struct proc *p;
239         struct ibcs2_semsys_args *uap;
240 {
241         int error;
242
243         switch (SCARG(uap, which)) {
244         case 0:                                 /* semctl */
245                 switch(SCARG(uap, a4)) {
246                 case IBCS2_IPC_STAT:
247                     {
248                         struct ibcs2_semid_ds *isp;
249                         struct semid_ds *sp;
250                         union semun *sup, ssu;
251                         caddr_t sg = stackgap_init();
252
253
254                         ssu = (union semun) SCARG(uap, a5);
255                         sp = stackgap_alloc(&sg, sizeof(struct semid_ds));
256                         sup = stackgap_alloc(&sg, sizeof(union semun));
257                         sup->buf = sp;
258                         SCARG(uap, a5) = (int)sup;
259                         error = semsys(p, (struct semsys_args *)uap);
260                         if (!error) {
261                                 SCARG(uap, a5) = (int)ssu.buf;
262                                 isp = stackgap_alloc(&sg, sizeof(*isp));
263                                 cvt_semid2isemid(sp, isp);
264                                 error = copyout((caddr_t)isp,
265                                                 (caddr_t)ssu.buf,
266                                                 sizeof(*isp));
267                         }
268                         return error;
269                     }
270                 case IBCS2_IPC_SET:
271                     {
272                         struct ibcs2_semid_ds *isp;
273                         struct semid_ds *sp;
274                         caddr_t sg = stackgap_init();
275
276                         isp = stackgap_alloc(&sg, sizeof(*isp));
277                         sp = stackgap_alloc(&sg, sizeof(*sp));
278                         error = copyin((caddr_t)SCARG(uap, a5), (caddr_t)isp,
279                                        sizeof(*isp));
280                         if (error)
281                                 return error;
282                         cvt_isemid2semid(isp, sp);
283                         SCARG(uap, a5) = (int)sp;
284                         return semsys(p, (struct semsys_args *)uap);
285                     }
286                 case IBCS2_SETVAL:
287                     {
288                         union semun *sp;
289                         caddr_t sg = stackgap_init();
290
291                         sp = stackgap_alloc(&sg, sizeof(*sp));
292                         sp->val = (int) SCARG(uap, a5);
293                         SCARG(uap, a5) = (int)sp;
294                         return semsys(p, (struct semsys_args *)uap);
295                     }
296                 }
297
298                 return semsys(p, (struct semsys_args *)uap);
299
300         case 1:                         /* semget */
301                 return semsys(p, (struct semsys_args *)uap);
302
303         case 2:                         /* semop */
304                 return semsys(p, (struct semsys_args *)uap);
305         }
306         return EINVAL;
307 }
308
309
310 /*
311  * iBCS2 shmsys call
312  */
313
314 static void
315 cvt_shmid2ishmid(bp, ibp)
316 struct shmid_ds *bp;
317 struct ibcs2_shmid_ds *ibp;
318 {
319         cvt_perm2iperm(&bp->shm_perm, &ibp->shm_perm);
320         ibp->shm_segsz = bp->shm_segsz;
321         ibp->shm_lpid = bp->shm_lpid;
322         ibp->shm_cpid = bp->shm_cpid;
323         ibp->shm_nattch = bp->shm_nattch;
324         ibp->shm_cnattch = 0;                   /* ignored anyway */
325         ibp->shm_atime = bp->shm_atime;
326         ibp->shm_dtime = bp->shm_dtime;
327         ibp->shm_ctime = bp->shm_ctime;
328         return;
329 }
330
331 static void
332 cvt_ishmid2shmid(ibp, bp)
333 struct ibcs2_shmid_ds *ibp;
334 struct shmid_ds *bp;
335 {
336         cvt_iperm2perm(&ibp->shm_perm, &bp->shm_perm);
337         bp->shm_segsz = ibp->shm_segsz;
338         bp->shm_lpid = ibp->shm_lpid;
339         bp->shm_cpid = ibp->shm_cpid;
340         bp->shm_nattch = ibp->shm_nattch;
341         bp->shm_atime = ibp->shm_atime;
342         bp->shm_dtime = ibp->shm_dtime;
343         bp->shm_ctime = ibp->shm_ctime;
344         bp->shm_internal = (void *)0;           /* ignored anyway */
345         return;
346 }
347
348 int
349 ibcs2_shmsys(p, uap)
350         struct proc *p;
351         struct ibcs2_shmsys_args *uap;
352 {
353         int error;
354
355         switch (SCARG(uap, which)) {
356         case 0:                                         /* shmat */
357                 return shmsys(p, (struct shmsys_args *)uap);
358
359         case 1:                                         /* shmctl */
360                 switch(SCARG(uap, a3)) {
361                 case IBCS2_IPC_STAT:
362                     {
363                         struct ibcs2_shmid_ds *isp;
364                         struct shmid_ds *sp;
365                         caddr_t sg = stackgap_init();
366
367                         isp = (struct ibcs2_shmid_ds *)SCARG(uap, a4);
368                         sp = stackgap_alloc(&sg, sizeof(*sp));
369                         SCARG(uap, a4) = (int)sp;
370                         error = shmsys(p, (struct shmsys_args *)uap);
371                         if (!error) {
372                                 SCARG(uap, a4) = (int)isp;
373                                 isp = stackgap_alloc(&sg, sizeof(*isp));
374                                 cvt_shmid2ishmid(sp, isp);
375                                 error = copyout((caddr_t)isp,
376                                                 (caddr_t)SCARG(uap, a4),
377                                                 sizeof(*isp));
378                         }
379                         return error;
380                     }
381                 case IBCS2_IPC_SET:
382                     {
383                         struct ibcs2_shmid_ds *isp;
384                         struct shmid_ds *sp;
385                         caddr_t sg = stackgap_init();
386
387                         isp = stackgap_alloc(&sg, sizeof(*isp));
388                         sp = stackgap_alloc(&sg, sizeof(*sp));
389                         error = copyin((caddr_t)SCARG(uap, a4), (caddr_t)isp,
390                                        sizeof(*isp));
391                         if (error)
392                                 return error;
393                         cvt_ishmid2shmid(isp, sp);
394                         SCARG(uap, a4) = (int)sp;
395                         return shmsys(p, (struct shmsys_args *)uap);
396                     }
397                 }
398
399                 return shmsys(p, (struct shmsys_args *)uap);
400
401         case 2:                                         /* shmdt */
402                 return shmsys(p, (struct shmsys_args *)uap);
403
404         case 3:                                         /* shmget */
405                 return shmsys(p, (struct shmsys_args *)uap);
406         }
407         return EINVAL;
408 }