linux_ipc.c revision 1.22.2.3 1 /* $NetBSD: linux_ipc.c,v 1.22.2.3 2001/11/14 19:13:11 nathanw 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 <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: linux_ipc.c,v 1.22.2.3 2001/11/14 19:13:11 nathanw Exp $");
41
42 #if defined(_KERNEL_OPT)
43 #include "opt_sysv.h"
44 #endif
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/shm.h>
49 #include <sys/sem.h>
50 #include <sys/msg.h>
51 #include <sys/lwp.h>
52 #include <sys/proc.h>
53 #include <sys/systm.h>
54
55 #include <sys/mount.h>
56 #include <sys/syscallargs.h>
57
58 #include <compat/linux/common/linux_types.h>
59 #include <compat/linux/common/linux_signal.h>
60 #include <compat/linux/common/linux_util.h>
61
62 #include <compat/linux/linux_syscallargs.h>
63 #include <compat/linux/linux_syscall.h>
64
65 #include <compat/linux/common/linux_ipc.h>
66 #include <compat/linux/common/linux_msg.h>
67 #include <compat/linux/common/linux_shm.h>
68 #include <compat/linux/common/linux_sem.h>
69 #include <compat/linux/common/linux_ipccall.h>
70
71 /*
72 * Note: Not all linux architechtures have explicit versions
73 * of the SYSV* syscalls. On the ones that don't
74 * we pretend that they are defined anyway. *_args and
75 * prototypes are defined in individual headers;
76 * syscalls.master lists those syscalls as NOARGS.
77 *
78 * The functions in multiarch are the ones that just need
79 * the arguments shuffled around and then use the
80 * normal NetBSD syscall.
81 *
82 * Function in multiarch:
83 * linux_sys_ipc : linux_ipccall.c
84 * liunx_semop : linux_ipccall.c
85 * linux_semget : linux_ipccall.c
86 * linux_msgsnd : linux_ipccall.c
87 * linux_msgrcv : linux_ipccall.c
88 * linux_msgget : linux_ipccall.c
89 * linux_shmdt : linux_ipccall.c
90 * linux_shmget : linux_ipccall.c
91 */
92
93 #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
94 /*
95 * Convert between Linux and NetBSD ipc_perm structures. Only the
96 * order of the fields is different.
97 */
98 void
99 linux_to_bsd_ipc_perm(lpp, bpp)
100 struct linux_ipc_perm *lpp;
101 struct ipc_perm *bpp;
102 {
103
104 bpp->_key = lpp->l_key;
105 bpp->uid = lpp->l_uid;
106 bpp->gid = lpp->l_gid;
107 bpp->cuid = lpp->l_cuid;
108 bpp->cgid = lpp->l_cgid;
109 bpp->mode = lpp->l_mode;
110 bpp->_seq = lpp->l_seq;
111 }
112
113 void
114 bsd_to_linux_ipc_perm(bpp, lpp)
115 struct ipc_perm *bpp;
116 struct linux_ipc_perm *lpp;
117 {
118
119 lpp->l_key = bpp->_key;
120 lpp->l_uid = bpp->uid;
121 lpp->l_gid = bpp->gid;
122 lpp->l_cuid = bpp->cuid;
123 lpp->l_cgid = bpp->cgid;
124 lpp->l_mode = bpp->mode;
125 lpp->l_seq = bpp->_seq;
126 }
127 #endif
128
129 #ifdef SYSVSEM
130 /*
131 * Semaphore operations. Most constants and structures are the same on
132 * both systems. Only semctl() needs some extra work.
133 */
134
135 /*
136 * Convert between Linux and NetBSD semid_ds structures.
137 */
138 void
139 bsd_to_linux_semid_ds(bs, ls)
140 struct semid_ds *bs;
141 struct linux_semid_ds *ls;
142 {
143
144 bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
145 ls->l_sem_otime = bs->sem_otime;
146 ls->l_sem_ctime = bs->sem_ctime;
147 ls->l_sem_nsems = bs->sem_nsems;
148 ls->l_sem_base = bs->_sem_base;
149 }
150
151 void
152 linux_to_bsd_semid_ds(ls, bs)
153 struct linux_semid_ds *ls;
154 struct semid_ds *bs;
155 {
156
157 linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
158 bs->sem_otime = ls->l_sem_otime;
159 bs->sem_ctime = ls->l_sem_ctime;
160 bs->sem_nsems = ls->l_sem_nsems;
161 bs->_sem_base = ls->l_sem_base;
162 }
163
164 /*
165 * Most of this can be handled by directly passing the arguments on; we
166 * just need to frob the `cmd' and convert the semid_ds and semun.
167 */
168 int
169 linux_sys_semctl(l, v, retval)
170 struct lwp *l;
171 void *v;
172 register_t *retval;
173 {
174 struct linux_sys_semctl_args /* {
175 syscallarg(int) semid;
176 syscallarg(int) semnum;
177 syscallarg(int) cmd;
178 syscallarg(union linux_semun) arg;
179 } */ *uap = v;
180 struct proc *p = l->l_proc;
181 struct semid_ds sembuf;
182 struct linux_semid_ds lsembuf;
183 union __semun semun;
184 int cmd, error;
185 void *pass_arg = NULL;
186
187 cmd = SCARG(uap, cmd);
188
189 switch (cmd) {
190 case LINUX_IPC_SET:
191 pass_arg = &sembuf;
192 cmd = IPC_SET;
193 break;
194
195 case LINUX_IPC_STAT:
196 pass_arg = &sembuf;
197 cmd = IPC_STAT;
198 break;
199
200 case LINUX_IPC_RMID:
201 cmd = IPC_RMID;
202 break;
203
204 case LINUX_GETVAL:
205 cmd = GETVAL;
206 break;
207
208 case LINUX_GETPID:
209 cmd = GETPID;
210 break;
211
212 case LINUX_GETNCNT:
213 cmd = GETNCNT;
214 break;
215
216 case LINUX_GETZCNT:
217 cmd = GETZCNT;
218 break;
219
220 case LINUX_GETALL:
221 pass_arg = &semun;
222 semun.array = SCARG(uap, arg).l_array;
223 cmd = GETALL;
224 break;
225
226 case LINUX_SETVAL:
227 pass_arg = &semun;
228 semun.val = SCARG(uap, arg).l_val;
229 cmd = SETVAL;
230 break;
231
232 case LINUX_SETALL:
233 pass_arg = &semun;
234 semun.array = SCARG(uap, arg).l_array;
235 cmd = SETALL;
236 break;
237
238 default:
239 return (EINVAL);
240 }
241
242 if (cmd == IPC_SET) {
243 error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
244 sizeof(lsembuf));
245 if (error)
246 return (error);
247 linux_to_bsd_semid_ds(&lsembuf, &sembuf);
248 }
249
250 error = semctl1(p, SCARG(uap, semid), SCARG(uap, semnum), cmd,
251 pass_arg, retval);
252
253 if (error == 0 && cmd == IPC_STAT) {
254 bsd_to_linux_semid_ds(&sembuf, &lsembuf);
255 error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
256 sizeof(lsembuf));
257 }
258
259 return (error);
260 }
261 #endif /* SYSVSEM */
262
263 #ifdef SYSVMSG
264
265 void
266 linux_to_bsd_msqid_ds(lmp, bmp)
267 struct linux_msqid_ds *lmp;
268 struct msqid_ds *bmp;
269 {
270
271 linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
272 bmp->_msg_first = lmp->l_msg_first;
273 bmp->_msg_last = lmp->l_msg_last;
274 bmp->_msg_cbytes = lmp->l_msg_cbytes;
275 bmp->msg_qnum = lmp->l_msg_qnum;
276 bmp->msg_qbytes = lmp->l_msg_qbytes;
277 bmp->msg_lspid = lmp->l_msg_lspid;
278 bmp->msg_lrpid = lmp->l_msg_lrpid;
279 bmp->msg_stime = lmp->l_msg_stime;
280 bmp->msg_rtime = lmp->l_msg_rtime;
281 bmp->msg_ctime = lmp->l_msg_ctime;
282 }
283
284 void
285 bsd_to_linux_msqid_ds(bmp, lmp)
286 struct msqid_ds *bmp;
287 struct linux_msqid_ds *lmp;
288 {
289
290 bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
291 lmp->l_msg_first = bmp->_msg_first;
292 lmp->l_msg_last = bmp->_msg_last;
293 lmp->l_msg_cbytes = bmp->_msg_cbytes;
294 lmp->l_msg_qnum = bmp->msg_qnum;
295 lmp->l_msg_qbytes = bmp->msg_qbytes;
296 lmp->l_msg_lspid = bmp->msg_lspid;
297 lmp->l_msg_lrpid = bmp->msg_lrpid;
298 lmp->l_msg_stime = bmp->msg_stime;
299 lmp->l_msg_rtime = bmp->msg_rtime;
300 lmp->l_msg_ctime = bmp->msg_ctime;
301 }
302
303 int
304 linux_sys_msgctl(l, v, retval)
305 struct lwp *l;
306 void *v;
307 register_t *retval;
308 {
309 struct linux_sys_msgctl_args /* {
310 syscallarg(int) msqid;
311 syscallarg(int) cmd;
312 syscallarg(struct linux_msqid_ds *) buf;
313 } */ *uap = v;
314 struct proc *p = l->l_proc;
315 caddr_t sg;
316 struct sys___msgctl13_args nua;
317 struct msqid_ds *bmp, bm;
318 struct linux_msqid_ds lm;
319 int error;
320
321 SCARG(&nua, msqid) = SCARG(uap, msqid);
322 switch (SCARG(uap, cmd)) {
323 case LINUX_IPC_STAT:
324 sg = stackgap_init(p->p_emul);
325 bmp = stackgap_alloc(&sg, sizeof (struct msqid_ds));
326 SCARG(&nua, cmd) = IPC_STAT;
327 SCARG(&nua, buf) = bmp;
328 if ((error = sys___msgctl13(l, &nua, retval)))
329 return error;
330 if ((error = copyin(bmp, &bm, sizeof bm)))
331 return error;
332 bsd_to_linux_msqid_ds(&bm, &lm);
333 return copyout(&lm, SCARG(uap, buf), sizeof lm);
334 case LINUX_IPC_SET:
335 if ((error = copyin(SCARG(uap, buf), &lm, sizeof lm)))
336 return error;
337 linux_to_bsd_msqid_ds(&lm, &bm);
338 sg = stackgap_init(p->p_emul);
339 bmp = stackgap_alloc(&sg, sizeof bm);
340 if ((error = copyout(&bm, bmp, sizeof bm)))
341 return error;
342 SCARG(&nua, cmd) = IPC_SET;
343 SCARG(&nua, buf) = bmp;
344 break;
345 case LINUX_IPC_RMID:
346 SCARG(&nua, cmd) = IPC_RMID;
347 SCARG(&nua, buf) = NULL;
348 break;
349 default:
350 return EINVAL;
351 }
352 return sys___msgctl13(l, &nua, retval);
353 }
354 #endif /* SYSVMSG */
355
356 #ifdef SYSVSHM
357 /*
358 * shmat(2). Very straightforward, except that Linux passes a pointer
359 * in which the return value is to be passed. This is subsequently
360 * handled by libc, apparently.
361 */
362 int
363 linux_sys_shmat(l, v, retval)
364 struct lwp *l;
365 void *v;
366 register_t *retval;
367 {
368 struct linux_sys_shmat_args /* {
369 syscallarg(int) shmid;
370 syscallarg(void *) shmaddr;
371 syscallarg(int) shmflg;
372 syscallarg(u_long *) raddr;
373 } */ *uap = v;
374 int error;
375
376 if ((error = sys_shmat(l, uap, retval)))
377 return error;
378
379 if ((error = copyout(&retval[0], (caddr_t) SCARG(uap, raddr),
380 sizeof retval[0])))
381 return error;
382
383 retval[0] = 0;
384 return 0;
385 }
386
387 /*
388 * Convert between Linux and NetBSD shmid_ds structures.
389 * The order of the fields is once again the difference, and
390 * we also need a place to store the internal data pointer
391 * in, which is unfortunately stored in this structure.
392 *
393 * We abuse a Linux internal field for that.
394 */
395 void
396 linux_to_bsd_shmid_ds(lsp, bsp)
397 struct linux_shmid_ds *lsp;
398 struct shmid_ds *bsp;
399 {
400
401 linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
402 bsp->shm_segsz = lsp->l_shm_segsz;
403 bsp->shm_lpid = lsp->l_shm_lpid;
404 bsp->shm_cpid = lsp->l_shm_cpid;
405 bsp->shm_nattch = lsp->l_shm_nattch;
406 bsp->shm_atime = lsp->l_shm_atime;
407 bsp->shm_dtime = lsp->l_shm_dtime;
408 bsp->shm_ctime = lsp->l_shm_ctime;
409 bsp->_shm_internal = lsp->l_private2; /* XXX Oh well. */
410 }
411
412 void
413 bsd_to_linux_shmid_ds(bsp, lsp)
414 struct shmid_ds *bsp;
415 struct linux_shmid_ds *lsp;
416 {
417
418 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
419 lsp->l_shm_segsz = bsp->shm_segsz;
420 lsp->l_shm_lpid = bsp->shm_lpid;
421 lsp->l_shm_cpid = bsp->shm_cpid;
422 lsp->l_shm_nattch = bsp->shm_nattch;
423 lsp->l_shm_atime = bsp->shm_atime;
424 lsp->l_shm_dtime = bsp->shm_dtime;
425 lsp->l_shm_ctime = bsp->shm_ctime;
426 lsp->l_private2 = bsp->_shm_internal; /* XXX */
427 }
428
429 /*
430 * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT
431 * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
432 * by NetBSD itself.
433 *
434 * The usual structure conversion and massaging is done.
435 */
436 int
437 linux_sys_shmctl(l, v, retval)
438 struct lwp *l;
439 void *v;
440 register_t *retval;
441 {
442 struct linux_sys_shmctl_args /* {
443 syscallarg(int) shmid;
444 syscallarg(int) cmd;
445 syscallarg(struct linux_shmid_ds *) buf;
446 } */ *uap = v;
447 struct proc *p = l->l_proc;
448 caddr_t sg;
449 struct sys___shmctl13_args nua;
450 struct shmid_ds *bsp, bs;
451 struct linux_shmid_ds ls;
452 int error;
453
454 SCARG(&nua, shmid) = SCARG(uap, shmid);
455 switch (SCARG(uap, cmd)) {
456 case LINUX_IPC_STAT:
457 sg = stackgap_init(p->p_emul);
458 bsp = stackgap_alloc(&sg, sizeof(struct shmid_ds));
459 SCARG(&nua, cmd) = IPC_STAT;
460 SCARG(&nua, buf) = bsp;
461 if ((error = sys___shmctl13(l, &nua, retval)))
462 return error;
463 if ((error = copyin(SCARG(&nua, buf), &bs, sizeof bs)))
464 return error;
465 bsd_to_linux_shmid_ds(&bs, &ls);
466 return copyout(&ls, SCARG(uap, buf), sizeof ls);
467 case LINUX_IPC_SET:
468 if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
469 return error;
470 linux_to_bsd_shmid_ds(&ls, &bs);
471 sg = stackgap_init(p->p_emul);
472 bsp = stackgap_alloc(&sg, sizeof bs);
473 if ((error = copyout(&bs, bsp, sizeof bs)))
474 return error;
475 SCARG(&nua, cmd) = IPC_SET;
476 SCARG(&nua, buf) = bsp;
477 break;
478 case LINUX_IPC_RMID:
479 SCARG(&nua, cmd) = IPC_RMID;
480 SCARG(&nua, buf) = NULL;
481 break;
482 case LINUX_SHM_LOCK:
483 SCARG(&nua, cmd) = SHM_LOCK;
484 SCARG(&nua, buf) = NULL;
485 break;
486 case LINUX_SHM_UNLOCK:
487 SCARG(&nua, cmd) = SHM_UNLOCK;
488 SCARG(&nua, buf) = NULL;
489 break;
490 case LINUX_IPC_INFO:
491 case LINUX_SHM_STAT:
492 case LINUX_SHM_INFO:
493 default:
494 return EINVAL;
495 }
496 return sys___shmctl13(l, &nua, retval);
497 }
498 #endif /* SYSVSHM */
499