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