linux_ipc.c revision 1.21 1 /* $NetBSD: linux_ipc.c,v 1.21 1999/08/25 04:52:44 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; we
160 * just need to frob the `cmd' and convert the semid_ds and semun.
161 */
162 int
163 linux_sys_semctl(p, v, retval)
164 struct proc *p;
165 void *v;
166 register_t *retval;
167 {
168 struct linux_sys_semctl_args /* {
169 syscallarg(int) semid;
170 syscallarg(int) semnum;
171 syscallarg(int) cmd;
172 syscallarg(union linux_semun) arg;
173 } */ *uap = v;
174 struct semid_ds sembuf;
175 struct linux_semid_ds lsembuf;
176 union __semun semun;
177 int cmd, error;
178 void *pass_arg = NULL;
179
180 cmd = SCARG(uap, cmd);
181
182 switch (cmd) {
183 case LINUX_IPC_SET:
184 pass_arg = &sembuf;
185 cmd = IPC_SET;
186 break;
187
188 case LINUX_IPC_STAT:
189 pass_arg = &sembuf;
190 cmd = IPC_STAT;
191 break;
192
193 case LINUX_IPC_RMID:
194 cmd = IPC_RMID;
195 break;
196
197 case LINUX_GETVAL:
198 cmd = GETVAL;
199 break;
200
201 case LINUX_GETPID:
202 cmd = GETPID;
203 break;
204
205 case LINUX_GETNCNT:
206 cmd = GETNCNT;
207 break;
208
209 case LINUX_GETZCNT:
210 cmd = GETZCNT;
211 break;
212
213 case LINUX_GETALL:
214 pass_arg = &semun;
215 semun.array = SCARG(uap, arg).l_array;
216 cmd = GETALL;
217 break;
218
219 case LINUX_SETVAL:
220 pass_arg = &semun;
221 semun.val = SCARG(uap, arg).l_val;
222 cmd = SETVAL;
223 break;
224
225 case LINUX_SETALL:
226 pass_arg = &semun;
227 semun.array = SCARG(uap, arg).l_array;
228 cmd = SETALL;
229 break;
230
231 default:
232 return (EINVAL);
233 }
234
235 if (cmd == IPC_SET) {
236 error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
237 sizeof(lsembuf));
238 if (error)
239 return (error);
240 linux_to_bsd_semid_ds(&lsembuf, &sembuf);
241 }
242
243 error = semctl1(p, SCARG(uap, semid), SCARG(uap, semnum), cmd,
244 pass_arg, retval);
245
246 if (error == 0 && cmd == IPC_STAT) {
247 bsd_to_linux_semid_ds(&sembuf, &lsembuf);
248 error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
249 sizeof(lsembuf));
250 }
251
252 return (error);
253 }
254 #endif /* SYSVSEM */
255
256 #ifdef SYSVMSG
257
258 void
259 linux_to_bsd_msqid_ds(lmp, bmp)
260 struct linux_msqid_ds *lmp;
261 struct msqid_ds *bmp;
262 {
263
264 linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
265 bmp->_msg_first = lmp->l_msg_first;
266 bmp->_msg_last = lmp->l_msg_last;
267 bmp->_msg_cbytes = lmp->l_msg_cbytes;
268 bmp->msg_qnum = lmp->l_msg_qnum;
269 bmp->msg_qbytes = lmp->l_msg_qbytes;
270 bmp->msg_lspid = lmp->l_msg_lspid;
271 bmp->msg_lrpid = lmp->l_msg_lrpid;
272 bmp->msg_stime = lmp->l_msg_stime;
273 bmp->msg_rtime = lmp->l_msg_rtime;
274 bmp->msg_ctime = lmp->l_msg_ctime;
275 }
276
277 void
278 bsd_to_linux_msqid_ds(bmp, lmp)
279 struct msqid_ds *bmp;
280 struct linux_msqid_ds *lmp;
281 {
282
283 bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
284 lmp->l_msg_first = bmp->_msg_first;
285 lmp->l_msg_last = bmp->_msg_last;
286 lmp->l_msg_cbytes = bmp->_msg_cbytes;
287 lmp->l_msg_qnum = bmp->msg_qnum;
288 lmp->l_msg_qbytes = bmp->msg_qbytes;
289 lmp->l_msg_lspid = bmp->msg_lspid;
290 lmp->l_msg_lrpid = bmp->msg_lrpid;
291 lmp->l_msg_stime = bmp->msg_stime;
292 lmp->l_msg_rtime = bmp->msg_rtime;
293 lmp->l_msg_ctime = bmp->msg_ctime;
294 }
295
296 int
297 linux_sys_msgctl(p, v, retval)
298 struct proc *p;
299 void *v;
300 register_t *retval;
301 {
302 struct linux_sys_msgctl_args /* {
303 syscallarg(int) msqid;
304 syscallarg(int) cmd;
305 syscallarg(struct linux_msqid_ds *) buf;
306 } */ *uap = v;
307 caddr_t sg;
308 struct sys___msgctl13_args nua;
309 struct msqid_ds *bmp, bm;
310 struct linux_msqid_ds lm;
311 int error;
312
313 SCARG(&nua, msqid) = SCARG(uap, msqid);
314 switch (SCARG(uap, cmd)) {
315 case LINUX_IPC_STAT:
316 sg = stackgap_init(p->p_emul);
317 bmp = stackgap_alloc(&sg, sizeof (struct msqid_ds));
318 SCARG(&nua, cmd) = IPC_STAT;
319 SCARG(&nua, buf) = bmp;
320 if ((error = sys___msgctl13(p, &nua, retval)))
321 return error;
322 if ((error = copyin(bmp, &bm, sizeof bm)))
323 return error;
324 bsd_to_linux_msqid_ds(&bm, &lm);
325 return copyout(&lm, SCARG(uap, buf), sizeof lm);
326 case LINUX_IPC_SET:
327 if ((error = copyin(SCARG(uap, buf), &lm, sizeof lm)))
328 return error;
329 linux_to_bsd_msqid_ds(&lm, &bm);
330 sg = stackgap_init(p->p_emul);
331 bmp = stackgap_alloc(&sg, sizeof bm);
332 if ((error = copyout(&bm, bmp, sizeof bm)))
333 return error;
334 SCARG(&nua, cmd) = IPC_SET;
335 SCARG(&nua, buf) = bmp;
336 break;
337 case LINUX_IPC_RMID:
338 SCARG(&nua, cmd) = IPC_RMID;
339 SCARG(&nua, buf) = NULL;
340 break;
341 default:
342 return EINVAL;
343 }
344 return sys___msgctl13(p, &nua, retval);
345 }
346 #endif /* SYSVMSG */
347
348 #ifdef SYSVSHM
349 /*
350 * shmat(2). Very straightforward, except that Linux passes a pointer
351 * in which the return value is to be passed. This is subsequently
352 * handled by libc, apparently.
353 */
354 int
355 linux_sys_shmat(p, v, retval)
356 struct proc *p;
357 void *v;
358 register_t *retval;
359 {
360 struct linux_sys_shmat_args /* {
361 syscallarg(int) shmid;
362 syscallarg(void *) shmaddr;
363 syscallarg(int) shmflg;
364 syscallarg(u_long *) raddr;
365 } */ *uap = v;
366 int error;
367
368 if ((error = sys_shmat(p, uap, retval)))
369 return error;
370
371 if ((error = copyout(&retval[0], (caddr_t) SCARG(uap, raddr),
372 sizeof retval[0])))
373 return error;
374
375 retval[0] = 0;
376 return 0;
377 }
378
379 /*
380 * Convert between Linux and NetBSD shmid_ds structures.
381 * The order of the fields is once again the difference, and
382 * we also need a place to store the internal data pointer
383 * in, which is unfortunately stored in this structure.
384 *
385 * We abuse a Linux internal field for that.
386 */
387 void
388 linux_to_bsd_shmid_ds(lsp, bsp)
389 struct linux_shmid_ds *lsp;
390 struct shmid_ds *bsp;
391 {
392
393 linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
394 bsp->shm_segsz = lsp->l_shm_segsz;
395 bsp->shm_lpid = lsp->l_shm_lpid;
396 bsp->shm_cpid = lsp->l_shm_cpid;
397 bsp->shm_nattch = lsp->l_shm_nattch;
398 bsp->shm_atime = lsp->l_shm_atime;
399 bsp->shm_dtime = lsp->l_shm_dtime;
400 bsp->shm_ctime = lsp->l_shm_ctime;
401 bsp->_shm_internal = lsp->l_private2; /* XXX Oh well. */
402 }
403
404 void
405 bsd_to_linux_shmid_ds(bsp, lsp)
406 struct shmid_ds *bsp;
407 struct linux_shmid_ds *lsp;
408 {
409
410 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
411 lsp->l_shm_segsz = bsp->shm_segsz;
412 lsp->l_shm_lpid = bsp->shm_lpid;
413 lsp->l_shm_cpid = bsp->shm_cpid;
414 lsp->l_shm_nattch = bsp->shm_nattch;
415 lsp->l_shm_atime = bsp->shm_atime;
416 lsp->l_shm_dtime = bsp->shm_dtime;
417 lsp->l_shm_ctime = bsp->shm_ctime;
418 lsp->l_private2 = bsp->_shm_internal; /* XXX */
419 }
420
421 /*
422 * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT
423 * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
424 * by NetBSD itself.
425 *
426 * The usual structure conversion and massaging is done.
427 */
428 int
429 linux_sys_shmctl(p, v, retval)
430 struct proc *p;
431 void *v;
432 register_t *retval;
433 {
434 struct linux_sys_shmctl_args /* {
435 syscallarg(int) shmid;
436 syscallarg(int) cmd;
437 syscallarg(struct linux_shmid_ds *) buf;
438 } */ *uap = v;
439 caddr_t sg;
440 struct sys___shmctl13_args nua;
441 struct shmid_ds *bsp, bs;
442 struct linux_shmid_ds ls;
443 int error;
444
445 SCARG(&nua, shmid) = SCARG(uap, shmid);
446 switch (SCARG(uap, cmd)) {
447 case LINUX_IPC_STAT:
448 sg = stackgap_init(p->p_emul);
449 bsp = stackgap_alloc(&sg, sizeof(struct shmid_ds));
450 SCARG(&nua, cmd) = IPC_STAT;
451 SCARG(&nua, buf) = bsp;
452 if ((error = sys___shmctl13(p, &nua, retval)))
453 return error;
454 if ((error = copyin(SCARG(&nua, buf), &bs, sizeof bs)))
455 return error;
456 bsd_to_linux_shmid_ds(&bs, &ls);
457 return copyout(&ls, SCARG(uap, buf), sizeof ls);
458 case LINUX_IPC_SET:
459 if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
460 return error;
461 linux_to_bsd_shmid_ds(&ls, &bs);
462 sg = stackgap_init(p->p_emul);
463 bsp = stackgap_alloc(&sg, sizeof bs);
464 if ((error = copyout(&bs, bsp, sizeof bs)))
465 return error;
466 SCARG(&nua, cmd) = IPC_SET;
467 SCARG(&nua, buf) = bsp;
468 break;
469 case LINUX_IPC_RMID:
470 SCARG(&nua, cmd) = IPC_RMID;
471 SCARG(&nua, buf) = NULL;
472 break;
473 case LINUX_SHM_LOCK:
474 SCARG(&nua, cmd) = SHM_LOCK;
475 SCARG(&nua, buf) = NULL;
476 break;
477 case LINUX_SHM_UNLOCK:
478 SCARG(&nua, cmd) = SHM_UNLOCK;
479 SCARG(&nua, buf) = NULL;
480 break;
481 case LINUX_IPC_INFO:
482 case LINUX_SHM_STAT:
483 case LINUX_SHM_INFO:
484 default:
485 return EINVAL;
486 }
487 return sys___shmctl13(p, &nua, retval);
488 }
489 #endif /* SYSVSHM */
490