linux_ipc.c revision 1.19.4.1 1 /* $NetBSD: linux_ipc.c,v 1.19.4.1 1999/06/21 01:07:37 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "opt_sysv.h"
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/shm.h>
44 #include <sys/sem.h>
45 #include <sys/msg.h>
46 #include <sys/proc.h>
47 #include <sys/systm.h>
48
49 #include <sys/mount.h>
50 #include <sys/syscallargs.h>
51
52 #include <compat/linux/common/linux_types.h>
53 #include <compat/linux/common/linux_signal.h>
54 #include <compat/linux/common/linux_util.h>
55
56 #include <compat/linux/linux_syscallargs.h>
57 #include <compat/linux/linux_syscall.h>
58
59 #include <compat/linux/common/linux_ipc.h>
60 #include <compat/linux/common/linux_msg.h>
61 #include <compat/linux/common/linux_shm.h>
62 #include <compat/linux/common/linux_sem.h>
63 #include <compat/linux/common/linux_ipccall.h>
64
65 /*
66 * Note: Not all linux architechtures have explicit versions
67 * of the SYSV* syscalls. On the ones that don't
68 * we pretend that they are defined anyway. *_args and
69 * prototypes are defined in individual headers;
70 * syscalls.master lists those syscalls as NOARGS.
71 *
72 * The functions in multiarch are the ones that just need
73 * the arguments shuffled around and then use the
74 * normal NetBSD syscall.
75 *
76 * Function in multiarch:
77 * linux_sys_ipc : linux_ipccall.c
78 * liunx_semop : linux_ipccall.c
79 * linux_semget : linux_ipccall.c
80 * linux_msgsnd : linux_ipccall.c
81 * linux_msgrcv : linux_ipccall.c
82 * linux_msgget : linux_ipccall.c
83 * linux_shmdt : linux_ipccall.c
84 * linux_shmget : linux_ipccall.c
85 */
86
87 #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
88 /*
89 * Convert between Linux and NetBSD ipc_perm structures. Only the
90 * order of the fields is different.
91 */
92 void
93 linux_to_bsd_ipc_perm(lpp, bpp)
94 struct linux_ipc_perm *lpp;
95 struct ipc_perm *bpp;
96 {
97
98 bpp->key = lpp->l_key;
99 bpp->uid = lpp->l_uid;
100 bpp->gid = lpp->l_gid;
101 bpp->cuid = lpp->l_cuid;
102 bpp->cgid = lpp->l_cgid;
103 bpp->mode = lpp->l_mode;
104 bpp->seq = lpp->l_seq;
105 }
106
107 void
108 bsd_to_linux_ipc_perm(bpp, lpp)
109 struct ipc_perm *bpp;
110 struct linux_ipc_perm *lpp;
111 {
112
113 lpp->l_key = bpp->key;
114 lpp->l_uid = bpp->uid;
115 lpp->l_gid = bpp->gid;
116 lpp->l_cuid = bpp->cuid;
117 lpp->l_cgid = bpp->cgid;
118 lpp->l_mode = bpp->mode;
119 lpp->l_seq = bpp->seq;
120 }
121 #endif
122
123 #ifdef SYSVSEM
124 /*
125 * Semaphore operations. Most constants and structures are the same on
126 * both systems. Only semctl() needs some extra work.
127 */
128
129 /*
130 * Convert between Linux and NetBSD semid_ds structures.
131 */
132 void
133 bsd_to_linux_semid_ds(bs, ls)
134 struct semid_ds *bs;
135 struct linux_semid_ds *ls;
136 {
137
138 bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
139 ls->l_sem_otime = bs->sem_otime;
140 ls->l_sem_ctime = bs->sem_ctime;
141 ls->l_sem_nsems = bs->sem_nsems;
142 ls->l_sem_base = bs->sem_base;
143 }
144
145 void
146 linux_to_bsd_semid_ds(ls, bs)
147 struct linux_semid_ds *ls;
148 struct semid_ds *bs;
149 {
150
151 linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
152 bs->sem_otime = ls->l_sem_otime;
153 bs->sem_ctime = ls->l_sem_ctime;
154 bs->sem_nsems = ls->l_sem_nsems;
155 bs->sem_base = ls->l_sem_base;
156 }
157
158 /*
159 * Most of this can be handled by directly passing the arguments on,
160 * but IPC_* require a lot of copy{in,out} because of the extra indirection
161 * (we need to pass a pointer to a union cointaining a pointer to a semid_ds
162 * structure. Linux actually handles this better than we do.)
163 */
164 int
165 linux_sys_semctl(p, v, retval)
166 struct proc *p;
167 void *v;
168 register_t *retval;
169 {
170 struct linux_sys_semctl_args /* {
171 syscallarg(int) semid;
172 syscallarg(int) semnum;
173 syscallarg(int) cmd;
174 syscallarg(union linux_semun) arg;
175 } */ *uap = v;
176 caddr_t sg;
177 struct sys___semctl_args nua;
178 struct semid_ds *bmp, bm;
179 struct linux_semid_ds lm;
180 union semun *bup;
181 int error;
182
183 SCARG(&nua, semid) = SCARG(uap, semid);
184 SCARG(&nua, semnum) = SCARG(uap, semnum);
185 switch (SCARG(uap, cmd)) {
186 case LINUX_IPC_STAT:
187 sg = stackgap_init(p->p_emul);
188 bup = stackgap_alloc(&sg, sizeof (union semun));
189 bmp = stackgap_alloc(&sg, sizeof (struct semid_ds));
190 if ((error = copyout(&bmp, bup, sizeof bmp)))
191 return error;
192 SCARG(&nua, cmd) = IPC_STAT;
193 SCARG(&nua, arg) = bup;
194 if ((error = sys___semctl(p, &nua, retval)))
195 return error;
196 if ((error = copyin(bmp, &bm, sizeof bm)))
197 return error;
198 bsd_to_linux_semid_ds(&bm, &lm);
199 return copyout(&lm, SCARG(uap, arg).l_buf, sizeof lm);
200 case LINUX_IPC_SET:
201 if ((error = copyin(SCARG(uap, arg).l_buf, &lm, sizeof lm)))
202 return error;
203 linux_to_bsd_semid_ds(&lm, &bm);
204 sg = stackgap_init(p->p_emul);
205 bup = stackgap_alloc(&sg, sizeof (union semun));
206 bmp = stackgap_alloc(&sg, sizeof (struct semid_ds));
207 if ((error = copyout(&bm, bmp, sizeof bm)))
208 return error;
209 if ((error = copyout(&bmp, bup, sizeof bmp)))
210 return error;
211 SCARG(&nua, cmd) = IPC_SET;
212 SCARG(&nua, arg) = bup;
213 break;
214 case LINUX_IPC_RMID:
215 SCARG(&nua, cmd) = IPC_RMID;
216 break;
217 case LINUX_GETVAL:
218 SCARG(&nua, cmd) = GETVAL;
219 break;
220 case LINUX_GETPID:
221 SCARG(&nua, cmd) = GETPID;
222 break;
223 case LINUX_GETNCNT:
224 SCARG(&nua, cmd) = GETNCNT;
225 break;
226 case LINUX_GETZCNT:
227 SCARG(&nua, cmd) = GETZCNT;
228 break;
229 case LINUX_SETVAL:
230 SCARG(&nua, cmd) = SETVAL;
231 sg = stackgap_init(p->p_emul);
232 bup = stackgap_alloc(&sg, sizeof(union semun));
233 bup->val = SCARG(uap, arg).l_val;
234 SCARG(&nua, arg) = bup;
235 break;
236 case LINUX_GETALL:
237 SCARG(&nua, cmd) = GETALL;
238 sg = stackgap_init(p->p_emul);
239 bup = stackgap_alloc(&sg, sizeof(union semun));
240 bup->array = SCARG(uap, arg).l_array;
241 SCARG(&nua, arg) = bup;
242 break;
243 case LINUX_SETALL:
244 SCARG(&nua, cmd) = SETALL;
245 sg = stackgap_init(p->p_emul);
246 bup = stackgap_alloc(&sg, sizeof(union semun));
247 bup->array = SCARG(uap, arg).l_array;
248 SCARG(&nua, arg) = bup;
249 break;
250 default:
251 return EINVAL;
252 }
253 return sys___semctl(p, &nua, retval);
254 }
255 #endif /* SYSVSEM */
256
257 #ifdef SYSVMSG
258
259 void
260 linux_to_bsd_msqid_ds(lmp, bmp)
261 struct linux_msqid_ds *lmp;
262 struct msqid_ds *bmp;
263 {
264
265 linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
266 bmp->msg_first = lmp->l_msg_first;
267 bmp->msg_last = lmp->l_msg_last;
268 bmp->msg_cbytes = lmp->l_msg_cbytes;
269 bmp->msg_qnum = lmp->l_msg_qnum;
270 bmp->msg_qbytes = lmp->l_msg_qbytes;
271 bmp->msg_lspid = lmp->l_msg_lspid;
272 bmp->msg_lrpid = lmp->l_msg_lrpid;
273 bmp->msg_stime = lmp->l_msg_stime;
274 bmp->msg_rtime = lmp->l_msg_rtime;
275 bmp->msg_ctime = lmp->l_msg_ctime;
276 }
277
278 void
279 bsd_to_linux_msqid_ds(bmp, lmp)
280 struct msqid_ds *bmp;
281 struct linux_msqid_ds *lmp;
282 {
283
284 bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
285 lmp->l_msg_first = bmp->msg_first;
286 lmp->l_msg_last = bmp->msg_last;
287 lmp->l_msg_cbytes = bmp->msg_cbytes;
288 lmp->l_msg_qnum = bmp->msg_qnum;
289 lmp->l_msg_qbytes = bmp->msg_qbytes;
290 lmp->l_msg_lspid = bmp->msg_lspid;
291 lmp->l_msg_lrpid = bmp->msg_lrpid;
292 lmp->l_msg_stime = bmp->msg_stime;
293 lmp->l_msg_rtime = bmp->msg_rtime;
294 lmp->l_msg_ctime = bmp->msg_ctime;
295 }
296
297 int
298 linux_sys_msgctl(p, v, retval)
299 struct proc *p;
300 void *v;
301 register_t *retval;
302 {
303 struct linux_sys_msgctl_args /* {
304 syscallarg(int) msqid;
305 syscallarg(int) cmd;
306 syscallarg(struct linux_msqid_ds *) buf;
307 } */ *uap = v;
308 caddr_t sg;
309 struct sys_msgctl_args nua;
310 struct msqid_ds *bmp, bm;
311 struct linux_msqid_ds lm;
312 int error;
313
314 SCARG(&nua, msqid) = SCARG(uap, msqid);
315 switch (SCARG(uap, cmd)) {
316 case LINUX_IPC_STAT:
317 sg = stackgap_init(p->p_emul);
318 bmp = stackgap_alloc(&sg, sizeof (struct msqid_ds));
319 SCARG(&nua, cmd) = IPC_STAT;
320 SCARG(&nua, buf) = bmp;
321 if ((error = sys_msgctl(p, &nua, retval)))
322 return error;
323 if ((error = copyin(bmp, &bm, sizeof bm)))
324 return error;
325 bsd_to_linux_msqid_ds(&bm, &lm);
326 return copyout(&lm, SCARG(uap, buf), sizeof lm);
327 case LINUX_IPC_SET:
328 if ((error = copyin(SCARG(uap, buf), &lm, sizeof lm)))
329 return error;
330 linux_to_bsd_msqid_ds(&lm, &bm);
331 sg = stackgap_init(p->p_emul);
332 bmp = stackgap_alloc(&sg, sizeof bm);
333 if ((error = copyout(&bm, bmp, sizeof bm)))
334 return error;
335 SCARG(&nua, cmd) = IPC_SET;
336 SCARG(&nua, buf) = bmp;
337 break;
338 case LINUX_IPC_RMID:
339 SCARG(&nua, cmd) = IPC_RMID;
340 SCARG(&nua, buf) = NULL;
341 break;
342 default:
343 return EINVAL;
344 }
345 return sys_msgctl(p, &nua, retval);
346 }
347 #endif /* SYSVMSG */
348
349 #ifdef SYSVSHM
350 /*
351 * shmat(2). Very straightforward, except that Linux passes a pointer
352 * in which the return value is to be passed. This is subsequently
353 * handled by libc, apparently.
354 */
355 int
356 linux_sys_shmat(p, v, retval)
357 struct proc *p;
358 void *v;
359 register_t *retval;
360 {
361 struct linux_sys_shmat_args /* {
362 syscallarg(int) shmid;
363 syscallarg(void *) shmaddr;
364 syscallarg(int) shmflg;
365 syscallarg(u_long *) raddr;
366 } */ *uap = v;
367 int error;
368
369 if ((error = sys_shmat(p, uap, retval)))
370 return error;
371
372 if ((error = copyout(&retval[0], (caddr_t) SCARG(uap, raddr),
373 sizeof retval[0])))
374 return error;
375
376 retval[0] = 0;
377 return 0;
378 }
379
380 /*
381 * Convert between Linux and NetBSD shmid_ds structures.
382 * The order of the fields is once again the difference, and
383 * we also need a place to store the internal data pointer
384 * in, which is unfortunately stored in this structure.
385 *
386 * We abuse a Linux internal field for that.
387 */
388 void
389 linux_to_bsd_shmid_ds(lsp, bsp)
390 struct linux_shmid_ds *lsp;
391 struct shmid_ds *bsp;
392 {
393
394 linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
395 bsp->shm_segsz = lsp->l_shm_segsz;
396 bsp->shm_lpid = lsp->l_shm_lpid;
397 bsp->shm_cpid = lsp->l_shm_cpid;
398 bsp->shm_nattch = lsp->l_shm_nattch;
399 bsp->shm_atime = lsp->l_shm_atime;
400 bsp->shm_dtime = lsp->l_shm_dtime;
401 bsp->shm_ctime = lsp->l_shm_ctime;
402 bsp->shm_internal = lsp->l_private2; /* XXX Oh well. */
403 }
404
405 void
406 bsd_to_linux_shmid_ds(bsp, lsp)
407 struct shmid_ds *bsp;
408 struct linux_shmid_ds *lsp;
409 {
410
411 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
412 lsp->l_shm_segsz = bsp->shm_segsz;
413 lsp->l_shm_lpid = bsp->shm_lpid;
414 lsp->l_shm_cpid = bsp->shm_cpid;
415 lsp->l_shm_nattch = bsp->shm_nattch;
416 lsp->l_shm_atime = bsp->shm_atime;
417 lsp->l_shm_dtime = bsp->shm_dtime;
418 lsp->l_shm_ctime = bsp->shm_ctime;
419 lsp->l_private2 = bsp->shm_internal; /* XXX */
420 }
421
422 /*
423 * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT
424 * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
425 * by NetBSD itself.
426 *
427 * The usual structure conversion and massaging is done.
428 */
429 int
430 linux_sys_shmctl(p, v, retval)
431 struct proc *p;
432 void *v;
433 register_t *retval;
434 {
435 struct linux_sys_shmctl_args /* {
436 syscallarg(int) shmid;
437 syscallarg(int) cmd;
438 syscallarg(struct linux_shmid_ds *) buf;
439 } */ *uap = v;
440 caddr_t sg;
441 struct sys_shmctl_args nua;
442 struct shmid_ds *bsp, bs;
443 struct linux_shmid_ds ls;
444 int error;
445
446 SCARG(&nua, shmid) = SCARG(uap, shmid);
447 switch (SCARG(uap, cmd)) {
448 case LINUX_IPC_STAT:
449 sg = stackgap_init(p->p_emul);
450 bsp = stackgap_alloc(&sg, sizeof(struct shmid_ds));
451 SCARG(&nua, cmd) = IPC_STAT;
452 SCARG(&nua, buf) = bsp;
453 if ((error = sys_shmctl(p, &nua, retval)))
454 return error;
455 if ((error = copyin(SCARG(&nua, buf), &bs, sizeof bs)))
456 return error;
457 bsd_to_linux_shmid_ds(&bs, &ls);
458 return copyout(&ls, SCARG(uap, buf), sizeof ls);
459 case LINUX_IPC_SET:
460 if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
461 return error;
462 linux_to_bsd_shmid_ds(&ls, &bs);
463 sg = stackgap_init(p->p_emul);
464 bsp = stackgap_alloc(&sg, sizeof bs);
465 if ((error = copyout(&bs, bsp, sizeof bs)))
466 return error;
467 SCARG(&nua, cmd) = IPC_SET;
468 SCARG(&nua, buf) = bsp;
469 break;
470 case LINUX_IPC_RMID:
471 SCARG(&nua, cmd) = IPC_RMID;
472 SCARG(&nua, buf) = NULL;
473 break;
474 case LINUX_SHM_LOCK:
475 SCARG(&nua, cmd) = SHM_LOCK;
476 SCARG(&nua, buf) = NULL;
477 break;
478 case LINUX_SHM_UNLOCK:
479 SCARG(&nua, cmd) = SHM_UNLOCK;
480 SCARG(&nua, buf) = NULL;
481 break;
482 case LINUX_IPC_INFO:
483 case LINUX_SHM_STAT:
484 case LINUX_SHM_INFO:
485 default:
486 return EINVAL;
487 }
488 return sys_shmctl(p, &nua, retval);
489 }
490 #endif /* SYSVSHM */
491