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