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