linux_ipc.c revision 1.40.14.1 1 /* $NetBSD: linux_ipc.c,v 1.40.14.1 2008/02/18 21:05:27 mjf 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.40.14.1 2008/02/18 21:05:27 mjf Exp $");
41
42 #if defined(_KERNEL_OPT)
43 #include "opt_sysv.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/shm.h>
48 #include <sys/sem.h>
49 #include <sys/msg.h>
50 #include <sys/proc.h>
51 #include <sys/systm.h>
52 #include <sys/vnode.h>
53
54 #include <sys/mount.h>
55 #include <sys/syscallargs.h>
56
57 #include <compat/linux/common/linux_types.h>
58 #include <compat/linux/common/linux_signal.h>
59 #include <compat/linux/common/linux_util.h>
60 #include <compat/linux/common/linux_ipc.h>
61 #include <compat/linux/common/linux_msg.h>
62 #include <compat/linux/common/linux_shm.h>
63 #include <compat/linux/common/linux_sem.h>
64
65 #include <compat/linux/linux_syscallargs.h>
66 #include <compat/linux/linux_syscall.h>
67
68 #include <compat/linux/common/linux_ipccall.h>
69 #include <compat/linux/common/linux_machdep.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 linux_to_bsd_ipc64_perm(lpp, bpp)
115 struct linux_ipc64_perm *lpp;
116 struct ipc_perm *bpp;
117 {
118 bpp->_key = lpp->l_key;
119 bpp->uid = lpp->l_uid;
120 bpp->gid = lpp->l_gid;
121 bpp->cuid = lpp->l_cuid;
122 bpp->cgid = lpp->l_cgid;
123 bpp->mode = lpp->l_mode;
124 bpp->_seq = lpp->l_seq;
125 }
126
127 void
128 bsd_to_linux_ipc_perm(bpp, lpp)
129 struct ipc_perm *bpp;
130 struct linux_ipc_perm *lpp;
131 {
132
133 lpp->l_key = bpp->_key;
134 lpp->l_uid = bpp->uid;
135 lpp->l_gid = bpp->gid;
136 lpp->l_cuid = bpp->cuid;
137 lpp->l_cgid = bpp->cgid;
138 lpp->l_mode = bpp->mode;
139 lpp->l_seq = bpp->_seq;
140 }
141
142 void
143 bsd_to_linux_ipc64_perm(bpp, lpp)
144 struct ipc_perm *bpp;
145 struct linux_ipc64_perm *lpp;
146 {
147 lpp->l_key = bpp->_key;
148 lpp->l_uid = bpp->uid;
149 lpp->l_gid = bpp->gid;
150 lpp->l_cuid = bpp->cuid;
151 lpp->l_cgid = bpp->cgid;
152 lpp->l_mode = bpp->mode;
153 lpp->l_seq = bpp->_seq;
154 }
155
156 #endif
157
158 #ifdef SYSVSEM
159 /*
160 * Semaphore operations. Most constants and structures are the same on
161 * both systems. Only semctl() needs some extra work.
162 */
163
164 /*
165 * Convert between Linux and NetBSD semid_ds structures.
166 */
167 void
168 bsd_to_linux_semid_ds(bs, ls)
169 struct semid_ds *bs;
170 struct linux_semid_ds *ls;
171 {
172
173 bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
174 ls->l_sem_otime = bs->sem_otime;
175 ls->l_sem_ctime = bs->sem_ctime;
176 ls->l_sem_nsems = bs->sem_nsems;
177 ls->l_sem_base = bs->_sem_base;
178 }
179
180 void
181 linux_to_bsd_semid_ds(ls, bs)
182 struct linux_semid_ds *ls;
183 struct semid_ds *bs;
184 {
185
186 linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
187 bs->sem_otime = ls->l_sem_otime;
188 bs->sem_ctime = ls->l_sem_ctime;
189 bs->sem_nsems = ls->l_sem_nsems;
190 bs->_sem_base = ls->l_sem_base;
191 }
192
193 /*
194 * Most of this can be handled by directly passing the arguments on; we
195 * just need to frob the `cmd' and convert the semid_ds and semun.
196 */
197 int
198 linux_sys_semctl(l, v, retval)
199 struct lwp *l;
200 void *v;
201 register_t *retval;
202 {
203 struct linux_sys_semctl_args /* {
204 syscallarg(int) semid;
205 syscallarg(int) semnum;
206 syscallarg(int) cmd;
207 syscallarg(union linux_semun) arg;
208 } */ *uap = v;
209 struct semid_ds sembuf;
210 struct linux_semid_ds lsembuf;
211 union __semun semun;
212 int cmd, error;
213 void *pass_arg = NULL;
214
215 cmd = SCARG(uap, cmd);
216
217 switch (cmd) {
218 case LINUX_IPC_SET:
219 pass_arg = &sembuf;
220 cmd = IPC_SET;
221 break;
222
223 case LINUX_IPC_STAT:
224 pass_arg = &sembuf;
225 cmd = IPC_STAT;
226 break;
227
228 case LINUX_IPC_RMID:
229 cmd = IPC_RMID;
230 break;
231
232 case LINUX_GETVAL:
233 cmd = GETVAL;
234 break;
235
236 case LINUX_GETPID:
237 cmd = GETPID;
238 break;
239
240 case LINUX_GETNCNT:
241 cmd = GETNCNT;
242 break;
243
244 case LINUX_GETZCNT:
245 cmd = GETZCNT;
246 break;
247
248 case LINUX_GETALL:
249 pass_arg = &semun;
250 semun.array = SCARG(uap, arg).l_array;
251 cmd = GETALL;
252 break;
253
254 case LINUX_SETVAL:
255 pass_arg = &semun;
256 semun.val = SCARG(uap, arg).l_val;
257 cmd = SETVAL;
258 break;
259
260 case LINUX_SETALL:
261 pass_arg = &semun;
262 semun.array = SCARG(uap, arg).l_array;
263 cmd = SETALL;
264 break;
265
266 default:
267 return (EINVAL);
268 }
269
270 if (cmd == IPC_SET) {
271 error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
272 sizeof(lsembuf));
273 if (error)
274 return (error);
275 linux_to_bsd_semid_ds(&lsembuf, &sembuf);
276 }
277
278 error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
279 pass_arg, retval);
280
281 if (error == 0 && cmd == IPC_STAT) {
282 bsd_to_linux_semid_ds(&sembuf, &lsembuf);
283 error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
284 sizeof(lsembuf));
285 }
286
287 return (error);
288 }
289 #endif /* SYSVSEM */
290
291 #ifdef SYSVMSG
292
293 void
294 linux_to_bsd_msqid_ds(lmp, bmp)
295 struct linux_msqid_ds *lmp;
296 struct msqid_ds *bmp;
297 {
298
299 linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
300 bmp->_msg_first = lmp->l_msg_first;
301 bmp->_msg_last = lmp->l_msg_last;
302 bmp->_msg_cbytes = lmp->l_msg_cbytes;
303 bmp->msg_qnum = lmp->l_msg_qnum;
304 bmp->msg_qbytes = lmp->l_msg_qbytes;
305 bmp->msg_lspid = lmp->l_msg_lspid;
306 bmp->msg_lrpid = lmp->l_msg_lrpid;
307 bmp->msg_stime = lmp->l_msg_stime;
308 bmp->msg_rtime = lmp->l_msg_rtime;
309 bmp->msg_ctime = lmp->l_msg_ctime;
310 }
311
312 void
313 bsd_to_linux_msqid_ds(bmp, lmp)
314 struct msqid_ds *bmp;
315 struct linux_msqid_ds *lmp;
316 {
317
318 bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
319 lmp->l_msg_first = bmp->_msg_first;
320 lmp->l_msg_last = bmp->_msg_last;
321 lmp->l_msg_cbytes = bmp->_msg_cbytes;
322 lmp->l_msg_qnum = bmp->msg_qnum;
323 lmp->l_msg_qbytes = bmp->msg_qbytes;
324 lmp->l_msg_lspid = bmp->msg_lspid;
325 lmp->l_msg_lrpid = bmp->msg_lrpid;
326 lmp->l_msg_stime = bmp->msg_stime;
327 lmp->l_msg_rtime = bmp->msg_rtime;
328 lmp->l_msg_ctime = bmp->msg_ctime;
329 }
330
331 int
332 linux_sys_msgctl(l, v, retval)
333 struct lwp *l;
334 void *v;
335 register_t *retval;
336 {
337 struct linux_sys_msgctl_args /* {
338 syscallarg(int) msqid;
339 syscallarg(int) cmd;
340 syscallarg(struct linux_msqid_ds *) buf;
341 } */ *uap = v;
342 struct msqid_ds bm;
343 struct linux_msqid_ds lm;
344 int error;
345
346 switch (SCARG(uap, cmd)) {
347 case LINUX_IPC_STAT:
348 error = msgctl1(l, SCARG(uap, msqid), IPC_STAT, &bm);
349 if (error == 0) {
350 bsd_to_linux_msqid_ds(&bm, &lm);
351 error = copyout(&lm, SCARG(uap, buf), sizeof lm);
352 }
353 return error;
354 case LINUX_IPC_SET:
355 if ((error = copyin(SCARG(uap, buf), &lm, sizeof lm)))
356 return error;
357 linux_to_bsd_msqid_ds(&lm, &bm);
358 return msgctl1(l, SCARG(uap, msqid), IPC_SET, &bm);
359 case LINUX_IPC_RMID:
360 return msgctl1(l, SCARG(uap, msqid), IPC_RMID, NULL);
361 break;
362 default:
363 return EINVAL;
364 }
365 }
366 #endif /* SYSVMSG */
367
368 #ifdef SYSVSHM
369 /*
370 * shmget(2). Just make sure the Linux-compatible shmat() semantics
371 * is enabled for the segment, so that shmat() succeeds even when
372 * the segment would be removed.
373 */
374 int
375 linux_sys_shmget(l, v, retval)
376 struct lwp *l;
377 void *v;
378 register_t *retval;
379 {
380 struct sys_shmget_args /* {
381 syscallarg(key_t) key;
382 syscallarg(size_t) size;
383 syscallarg(int) shmflg;
384 } */ *uap = v;
385
386 SCARG(uap, shmflg) |= _SHM_RMLINGER;
387 return sys_shmget(l, uap, retval);
388 }
389
390 /*
391 * shmat(2). Very straightforward, except that Linux passes a pointer
392 * in which the return value is to be passed. This is subsequently
393 * handled by libc, apparently.
394 */
395 #ifndef __amd64__
396 int
397 linux_sys_shmat(l, v, retval)
398 struct lwp *l;
399 void *v;
400 register_t *retval;
401 {
402 struct linux_sys_shmat_args /* {
403 syscallarg(int) shmid;
404 syscallarg(void *) shmaddr;
405 syscallarg(int) shmflg;
406 syscallarg(u_long *) raddr;
407 } */ *uap = v;
408 int error;
409
410 if ((error = sys_shmat(l, uap, retval)))
411 return error;
412
413 #ifndef __amd64__
414 if ((error = copyout(&retval[0], (void *) SCARG(uap, raddr),
415 sizeof retval[0])))
416 return error;
417
418 retval[0] = 0;
419 #endif
420 return 0;
421 }
422 #endif /* __amd64__ */
423
424 /*
425 * Convert between Linux and NetBSD shmid_ds structures.
426 * The order of the fields is once again the difference, and
427 * we also need a place to store the internal data pointer
428 * in, which is unfortunately stored in this structure.
429 *
430 * We abuse a Linux internal field for that.
431 */
432 void
433 linux_to_bsd_shmid_ds(lsp, bsp)
434 struct linux_shmid_ds *lsp;
435 struct shmid_ds *bsp;
436 {
437
438 linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
439 bsp->shm_segsz = lsp->l_shm_segsz;
440 bsp->shm_lpid = lsp->l_shm_lpid;
441 bsp->shm_cpid = lsp->l_shm_cpid;
442 bsp->shm_nattch = lsp->l_shm_nattch;
443 bsp->shm_atime = lsp->l_shm_atime;
444 bsp->shm_dtime = lsp->l_shm_dtime;
445 bsp->shm_ctime = lsp->l_shm_ctime;
446 bsp->_shm_internal = lsp->l_private2; /* XXX Oh well. */
447 }
448
449 void
450 linux_to_bsd_shmid64_ds(lsp, bsp)
451 struct linux_shmid64_ds *lsp;
452 struct shmid_ds *bsp;
453 {
454
455 linux_to_bsd_ipc64_perm(&lsp->l_shm_perm, &bsp->shm_perm);
456 bsp->shm_segsz = lsp->l_shm_segsz;
457 bsp->shm_lpid = lsp->l_shm_lpid;
458 bsp->shm_cpid = lsp->l_shm_cpid;
459 bsp->shm_nattch = lsp->l_shm_nattch;
460 bsp->shm_atime = lsp->l_shm_atime;
461 bsp->shm_dtime = lsp->l_shm_dtime;
462 bsp->shm_ctime = lsp->l_shm_ctime;
463 bsp->_shm_internal = (void*)lsp->l___unused5; /* XXX Oh well. */
464 }
465
466 void
467 bsd_to_linux_shmid_ds(bsp, lsp)
468 struct shmid_ds *bsp;
469 struct linux_shmid_ds *lsp;
470 {
471
472 bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
473 lsp->l_shm_segsz = bsp->shm_segsz;
474 lsp->l_shm_lpid = bsp->shm_lpid;
475 lsp->l_shm_cpid = bsp->shm_cpid;
476 lsp->l_shm_nattch = bsp->shm_nattch;
477 lsp->l_shm_atime = bsp->shm_atime;
478 lsp->l_shm_dtime = bsp->shm_dtime;
479 lsp->l_shm_ctime = bsp->shm_ctime;
480 lsp->l_private2 = bsp->_shm_internal; /* XXX */
481 }
482
483 void
484 bsd_to_linux_shmid64_ds(bsp, lsp)
485 struct shmid_ds *bsp;
486 struct linux_shmid64_ds *lsp;
487 {
488 bsd_to_linux_ipc64_perm(&bsp->shm_perm, &lsp->l_shm_perm);
489 lsp->l_shm_segsz = bsp->shm_segsz;
490 lsp->l_shm_lpid = bsp->shm_lpid;
491 lsp->l_shm_cpid = bsp->shm_cpid;
492 lsp->l_shm_nattch = bsp->shm_nattch;
493 lsp->l_shm_atime = bsp->shm_atime;
494 lsp->l_shm_dtime = bsp->shm_dtime;
495 lsp->l_shm_ctime = bsp->shm_ctime;
496 lsp->l___unused5 = (u_long)bsp->_shm_internal; /* XXX */
497 }
498
499 /*
500 * shmctl.SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
501 * by NetBSD itself.
502 *
503 * The usual structure conversion and massaging is done.
504 */
505 int
506 linux_sys_shmctl(l, v, retval)
507 struct lwp *l;
508 void *v;
509 register_t *retval;
510 {
511 struct linux_sys_shmctl_args /* {
512 syscallarg(int) shmid;
513 syscallarg(int) cmd;
514 syscallarg(struct linux_shmid_ds *) buf;
515 } */ *uap = v;
516 struct shmid_ds bs;
517 struct linux_shmid_ds ls;
518 struct linux_shmid64_ds ls64;
519 struct linux_shminfo64 lsi64;
520 struct linux_shm_info lsi;
521 int error, i, cmd, shmid;
522
523 shmid = SCARG(uap, shmid);
524 cmd = SCARG(uap, cmd);
525 #ifdef LINUX_SHMCTL_FORCEIPC64
526 if (cmd == LINUX_IPC_STAT || cmd == LINUX_SHM_STAT)
527 cmd |= LINUX_IPC_64;
528 #endif
529
530 switch (cmd) {
531 case LINUX_IPC_STAT:
532 case LINUX_SHM_STAT:
533 if (cmd == LINUX_SHM_STAT) {
534 shmid = IXSEQ_TO_IPCID(shmid, shmsegs[shmid].shm_perm);
535 retval[0] = shmid;
536 }
537 error = shmctl1(l, shmid, IPC_STAT, &bs);
538 if (error != 0)
539 return error;
540 bsd_to_linux_shmid_ds(&bs, &ls);
541 return copyout(&ls, SCARG(uap, buf), sizeof ls);
542
543 case LINUX_IPC_STAT | LINUX_IPC_64:
544 case LINUX_SHM_STAT | LINUX_IPC_64:
545 if (cmd == (LINUX_SHM_STAT | LINUX_IPC_64)) {
546 shmid = IXSEQ_TO_IPCID(shmid, shmsegs[shmid].shm_perm);
547 retval[0] = shmid;
548 }
549 error = shmctl1(l, shmid, IPC_STAT, &bs);
550 if (error != 0)
551 return error;
552 bsd_to_linux_shmid64_ds(&bs, &ls64);
553 return copyout(&ls64, SCARG(uap, buf), sizeof ls64);
554
555 case LINUX_IPC_SET:
556 if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
557 return error;
558 linux_to_bsd_shmid_ds(&ls, &bs);
559 return shmctl1(l, shmid, IPC_SET, &bs);
560
561 case LINUX_IPC_RMID:
562 return shmctl1(l, shmid, IPC_RMID, NULL);
563
564 case LINUX_SHM_LOCK:
565 return shmctl1(l, shmid, SHM_LOCK, NULL);
566
567 case LINUX_SHM_UNLOCK:
568 return shmctl1(l, shmid, SHM_UNLOCK, NULL);
569
570 case LINUX_IPC_INFO:
571 memset(&lsi64, 0, sizeof lsi64);
572 lsi64.l_shmmax = shminfo.shmmax;
573 lsi64.l_shmmin = shminfo.shmmin;
574 lsi64.l_shmmni = shminfo.shmmni;
575 lsi64.l_shmseg = shminfo.shmseg;
576 lsi64.l_shmall = shminfo.shmall;
577 for (i = shminfo.shmmni - 1; i > 0; i--)
578 if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
579 break;
580 retval[0] = i;
581 return copyout(&lsi64, SCARG(uap, buf), sizeof lsi64);
582
583 case LINUX_SHM_INFO:
584 (void)memset(&lsi, 0, sizeof lsi);
585 lsi.l_used_ids = shm_nused;
586 for (i = 0; i < shminfo.shmmni; i++)
587 if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
588 lsi.l_shm_tot +=
589 round_page(shmsegs[i].shm_segsz) /
590 uvmexp.pagesize;
591 lsi.l_shm_rss = 0;
592 lsi.l_shm_swp = 0;
593 lsi.l_swap_attempts = 0;
594 lsi.l_swap_successes = 0;
595 for (i = shminfo.shmmni - 1; i > 0; i--)
596 if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
597 break;
598 retval[0] = i;
599 return copyout(&lsi, SCARG(uap, buf), sizeof lsi);
600
601 default:
602 #ifdef DEBUG
603 printf("linux_sys_shmctl cmd %d\n", SCARG(uap, cmd));
604 #endif
605 return EINVAL;
606 }
607 }
608 #endif /* SYSVSHM */
609