Home | History | Annotate | Line # | Download | only in common
linux_ipc.c revision 1.24
      1 /*	$NetBSD: linux_ipc.c,v 1.24 2001/11/13 02:08:53 lukem 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.24 2001/11/13 02:08:53 lukem Exp $");
     41 
     42 #if defined(_KERNEL_OPT)
     43 #include "opt_sysv.h"
     44 #endif
     45 
     46 #include <sys/types.h>
     47 #include <sys/param.h>
     48 #include <sys/shm.h>
     49 #include <sys/sem.h>
     50 #include <sys/msg.h>
     51 #include <sys/proc.h>
     52 #include <sys/systm.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 
     61 #include <compat/linux/linux_syscallargs.h>
     62 #include <compat/linux/linux_syscall.h>
     63 
     64 #include <compat/linux/common/linux_ipc.h>
     65 #include <compat/linux/common/linux_msg.h>
     66 #include <compat/linux/common/linux_shm.h>
     67 #include <compat/linux/common/linux_sem.h>
     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(lpp, bpp)
     99 	struct linux_ipc_perm *lpp;
    100 	struct ipc_perm *bpp;
    101 {
    102 
    103 	bpp->_key = lpp->l_key;
    104 	bpp->uid = lpp->l_uid;
    105 	bpp->gid = lpp->l_gid;
    106 	bpp->cuid = lpp->l_cuid;
    107 	bpp->cgid = lpp->l_cgid;
    108 	bpp->mode = lpp->l_mode;
    109 	bpp->_seq = lpp->l_seq;
    110 }
    111 
    112 void
    113 bsd_to_linux_ipc_perm(bpp, lpp)
    114 	struct ipc_perm *bpp;
    115 	struct linux_ipc_perm *lpp;
    116 {
    117 
    118 	lpp->l_key = bpp->_key;
    119 	lpp->l_uid = bpp->uid;
    120 	lpp->l_gid = bpp->gid;
    121 	lpp->l_cuid = bpp->cuid;
    122 	lpp->l_cgid = bpp->cgid;
    123 	lpp->l_mode = bpp->mode;
    124 	lpp->l_seq = bpp->_seq;
    125 }
    126 #endif
    127 
    128 #ifdef SYSVSEM
    129 /*
    130  * Semaphore operations. Most constants and structures are the same on
    131  * both systems. Only semctl() needs some extra work.
    132  */
    133 
    134 /*
    135  * Convert between Linux and NetBSD semid_ds structures.
    136  */
    137 void
    138 bsd_to_linux_semid_ds(bs, ls)
    139 	struct semid_ds *bs;
    140 	struct linux_semid_ds *ls;
    141 {
    142 
    143 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    144 	ls->l_sem_otime = bs->sem_otime;
    145 	ls->l_sem_ctime = bs->sem_ctime;
    146 	ls->l_sem_nsems = bs->sem_nsems;
    147 	ls->l_sem_base = bs->_sem_base;
    148 }
    149 
    150 void
    151 linux_to_bsd_semid_ds(ls, bs)
    152 	struct linux_semid_ds *ls;
    153 	struct semid_ds *bs;
    154 {
    155 
    156 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    157 	bs->sem_otime = ls->l_sem_otime;
    158 	bs->sem_ctime = ls->l_sem_ctime;
    159 	bs->sem_nsems = ls->l_sem_nsems;
    160 	bs->_sem_base = ls->l_sem_base;
    161 }
    162 
    163 /*
    164  * Most of this can be handled by directly passing the arguments on; we
    165  * just need to frob the `cmd' and convert the semid_ds and semun.
    166  */
    167 int
    168 linux_sys_semctl(p, v, retval)
    169 	struct proc *p;
    170 	void *v;
    171 	register_t *retval;
    172 {
    173 	struct linux_sys_semctl_args /* {
    174 		syscallarg(int) semid;
    175 		syscallarg(int) semnum;
    176 		syscallarg(int) cmd;
    177 		syscallarg(union linux_semun) arg;
    178 	} */ *uap = v;
    179 	struct semid_ds sembuf;
    180 	struct linux_semid_ds lsembuf;
    181 	union __semun semun;
    182 	int cmd, error;
    183 	void *pass_arg = NULL;
    184 
    185 	cmd = SCARG(uap, cmd);
    186 
    187 	switch (cmd) {
    188 	case LINUX_IPC_SET:
    189 		pass_arg = &sembuf;
    190 		cmd = IPC_SET;
    191 		break;
    192 
    193 	case LINUX_IPC_STAT:
    194 		pass_arg = &sembuf;
    195 		cmd = IPC_STAT;
    196 		break;
    197 
    198 	case LINUX_IPC_RMID:
    199 		cmd = IPC_RMID;
    200 		break;
    201 
    202 	case LINUX_GETVAL:
    203 		cmd = GETVAL;
    204 		break;
    205 
    206 	case LINUX_GETPID:
    207 		cmd = GETPID;
    208 		break;
    209 
    210 	case LINUX_GETNCNT:
    211 		cmd = GETNCNT;
    212 		break;
    213 
    214 	case LINUX_GETZCNT:
    215 		cmd = GETZCNT;
    216 		break;
    217 
    218 	case LINUX_GETALL:
    219 		pass_arg = &semun;
    220 		semun.array = SCARG(uap, arg).l_array;
    221 		cmd = GETALL;
    222 		break;
    223 
    224 	case LINUX_SETVAL:
    225 		pass_arg = &semun;
    226 		semun.val = SCARG(uap, arg).l_val;
    227 		cmd = SETVAL;
    228 		break;
    229 
    230 	case LINUX_SETALL:
    231 		pass_arg = &semun;
    232 		semun.array = SCARG(uap, arg).l_array;
    233 		cmd = SETALL;
    234 		break;
    235 
    236 	default:
    237 		return (EINVAL);
    238 	}
    239 
    240 	if (cmd == IPC_SET) {
    241 		error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
    242 		    sizeof(lsembuf));
    243 		if (error)
    244 			return (error);
    245 		linux_to_bsd_semid_ds(&lsembuf, &sembuf);
    246 	}
    247 
    248 	error = semctl1(p, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    249 	    pass_arg, retval);
    250 
    251 	if (error == 0 && cmd == IPC_STAT) {
    252 		bsd_to_linux_semid_ds(&sembuf, &lsembuf);
    253 		error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
    254 		    sizeof(lsembuf));
    255 	}
    256 
    257 	return (error);
    258 }
    259 #endif /* SYSVSEM */
    260 
    261 #ifdef SYSVMSG
    262 
    263 void
    264 linux_to_bsd_msqid_ds(lmp, bmp)
    265 	struct linux_msqid_ds *lmp;
    266 	struct msqid_ds *bmp;
    267 {
    268 
    269 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    270 	bmp->_msg_first = lmp->l_msg_first;
    271 	bmp->_msg_last = lmp->l_msg_last;
    272 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    273 	bmp->msg_qnum = lmp->l_msg_qnum;
    274 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    275 	bmp->msg_lspid = lmp->l_msg_lspid;
    276 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    277 	bmp->msg_stime = lmp->l_msg_stime;
    278 	bmp->msg_rtime = lmp->l_msg_rtime;
    279 	bmp->msg_ctime = lmp->l_msg_ctime;
    280 }
    281 
    282 void
    283 bsd_to_linux_msqid_ds(bmp, lmp)
    284 	struct msqid_ds *bmp;
    285 	struct linux_msqid_ds *lmp;
    286 {
    287 
    288 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    289 	lmp->l_msg_first = bmp->_msg_first;
    290 	lmp->l_msg_last = bmp->_msg_last;
    291 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    292 	lmp->l_msg_qnum = bmp->msg_qnum;
    293 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    294 	lmp->l_msg_lspid = bmp->msg_lspid;
    295 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    296 	lmp->l_msg_stime = bmp->msg_stime;
    297 	lmp->l_msg_rtime = bmp->msg_rtime;
    298 	lmp->l_msg_ctime = bmp->msg_ctime;
    299 }
    300 
    301 int
    302 linux_sys_msgctl(p, v, retval)
    303 	struct proc *p;
    304 	void *v;
    305 	register_t *retval;
    306 {
    307 	struct linux_sys_msgctl_args /* {
    308 		syscallarg(int) msqid;
    309 		syscallarg(int) cmd;
    310 		syscallarg(struct linux_msqid_ds *) buf;
    311 	} */ *uap = v;
    312 	caddr_t sg;
    313 	struct sys___msgctl13_args nua;
    314 	struct msqid_ds *bmp, bm;
    315 	struct linux_msqid_ds lm;
    316 	int error;
    317 
    318 	SCARG(&nua, msqid) = SCARG(uap, msqid);
    319 	switch (SCARG(uap, cmd)) {
    320 	case LINUX_IPC_STAT:
    321 		sg = stackgap_init(p->p_emul);
    322 		bmp = stackgap_alloc(&sg, sizeof (struct msqid_ds));
    323 		SCARG(&nua, cmd) = IPC_STAT;
    324 		SCARG(&nua, buf) = bmp;
    325 		if ((error = sys___msgctl13(p, &nua, retval)))
    326 			return error;
    327 		if ((error = copyin(bmp, &bm, sizeof bm)))
    328 			return error;
    329 		bsd_to_linux_msqid_ds(&bm, &lm);
    330 		return copyout(&lm, SCARG(uap, buf), sizeof lm);
    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 		sg = stackgap_init(p->p_emul);
    336 		bmp = stackgap_alloc(&sg, sizeof bm);
    337 		if ((error = copyout(&bm, bmp, sizeof bm)))
    338 			return error;
    339 		SCARG(&nua, cmd) = IPC_SET;
    340 		SCARG(&nua, buf) = bmp;
    341 		break;
    342 	case LINUX_IPC_RMID:
    343 		SCARG(&nua, cmd) = IPC_RMID;
    344 		SCARG(&nua, buf) = NULL;
    345 		break;
    346 	default:
    347 		return EINVAL;
    348 	}
    349 	return sys___msgctl13(p, &nua, retval);
    350 }
    351 #endif /* SYSVMSG */
    352 
    353 #ifdef SYSVSHM
    354 /*
    355  * shmat(2). Very straightforward, except that Linux passes a pointer
    356  * in which the return value is to be passed. This is subsequently
    357  * handled by libc, apparently.
    358  */
    359 int
    360 linux_sys_shmat(p, v, retval)
    361 	struct proc *p;
    362 	void *v;
    363 	register_t *retval;
    364 {
    365 	struct linux_sys_shmat_args /* {
    366 		syscallarg(int) shmid;
    367 		syscallarg(void *) shmaddr;
    368 		syscallarg(int) shmflg;
    369 		syscallarg(u_long *) raddr;
    370 	} */ *uap = v;
    371 	int error;
    372 
    373 	if ((error = sys_shmat(p, uap, retval)))
    374 		return error;
    375 
    376 	if ((error = copyout(&retval[0], (caddr_t) SCARG(uap, raddr),
    377 	     sizeof retval[0])))
    378 		return error;
    379 
    380 	retval[0] = 0;
    381 	return 0;
    382 }
    383 
    384 /*
    385  * Convert between Linux and NetBSD shmid_ds structures.
    386  * The order of the fields is once again the difference, and
    387  * we also need a place to store the internal data pointer
    388  * in, which is unfortunately stored in this structure.
    389  *
    390  * We abuse a Linux internal field for that.
    391  */
    392 void
    393 linux_to_bsd_shmid_ds(lsp, bsp)
    394 	struct linux_shmid_ds *lsp;
    395 	struct shmid_ds *bsp;
    396 {
    397 
    398 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    399 	bsp->shm_segsz = lsp->l_shm_segsz;
    400 	bsp->shm_lpid = lsp->l_shm_lpid;
    401 	bsp->shm_cpid = lsp->l_shm_cpid;
    402 	bsp->shm_nattch = lsp->l_shm_nattch;
    403 	bsp->shm_atime = lsp->l_shm_atime;
    404 	bsp->shm_dtime = lsp->l_shm_dtime;
    405 	bsp->shm_ctime = lsp->l_shm_ctime;
    406 	bsp->_shm_internal = lsp->l_private2;	/* XXX Oh well. */
    407 }
    408 
    409 void
    410 bsd_to_linux_shmid_ds(bsp, lsp)
    411 	struct shmid_ds *bsp;
    412 	struct linux_shmid_ds *lsp;
    413 {
    414 
    415 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    416 	lsp->l_shm_segsz = bsp->shm_segsz;
    417 	lsp->l_shm_lpid = bsp->shm_lpid;
    418 	lsp->l_shm_cpid = bsp->shm_cpid;
    419 	lsp->l_shm_nattch = bsp->shm_nattch;
    420 	lsp->l_shm_atime = bsp->shm_atime;
    421 	lsp->l_shm_dtime = bsp->shm_dtime;
    422 	lsp->l_shm_ctime = bsp->shm_ctime;
    423 	lsp->l_private2 = bsp->_shm_internal;	/* XXX */
    424 }
    425 
    426 /*
    427  * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT
    428  * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
    429  * by NetBSD itself.
    430  *
    431  * The usual structure conversion and massaging is done.
    432  */
    433 int
    434 linux_sys_shmctl(p, v, retval)
    435 	struct proc *p;
    436 	void *v;
    437 	register_t *retval;
    438 {
    439 	struct linux_sys_shmctl_args /* {
    440 		syscallarg(int) shmid;
    441 		syscallarg(int) cmd;
    442 		syscallarg(struct linux_shmid_ds *) buf;
    443 	} */ *uap = v;
    444 	caddr_t sg;
    445 	struct sys___shmctl13_args nua;
    446 	struct shmid_ds *bsp, bs;
    447 	struct linux_shmid_ds ls;
    448 	int error;
    449 
    450 	SCARG(&nua, shmid) = SCARG(uap, shmid);
    451 	switch (SCARG(uap, cmd)) {
    452 	case LINUX_IPC_STAT:
    453 		sg = stackgap_init(p->p_emul);
    454 		bsp = stackgap_alloc(&sg, sizeof(struct shmid_ds));
    455 		SCARG(&nua, cmd) = IPC_STAT;
    456 		SCARG(&nua, buf) = bsp;
    457 		if ((error = sys___shmctl13(p, &nua, retval)))
    458 			return error;
    459 		if ((error = copyin(SCARG(&nua, buf), &bs, sizeof bs)))
    460 			return error;
    461 		bsd_to_linux_shmid_ds(&bs, &ls);
    462 		return copyout(&ls, SCARG(uap, buf), sizeof ls);
    463 	case LINUX_IPC_SET:
    464 		if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
    465 			return error;
    466 		linux_to_bsd_shmid_ds(&ls, &bs);
    467 		sg = stackgap_init(p->p_emul);
    468 		bsp = stackgap_alloc(&sg, sizeof bs);
    469 		if ((error = copyout(&bs, bsp, sizeof bs)))
    470 			return error;
    471 		SCARG(&nua, cmd) = IPC_SET;
    472 		SCARG(&nua, buf) = bsp;
    473 		break;
    474 	case LINUX_IPC_RMID:
    475 		SCARG(&nua, cmd) = IPC_RMID;
    476 		SCARG(&nua, buf) = NULL;
    477 		break;
    478 	case LINUX_SHM_LOCK:
    479 		SCARG(&nua, cmd) = SHM_LOCK;
    480 		SCARG(&nua, buf) = NULL;
    481 		break;
    482 	case LINUX_SHM_UNLOCK:
    483 		SCARG(&nua, cmd) = SHM_UNLOCK;
    484 		SCARG(&nua, buf) = NULL;
    485 		break;
    486 	case LINUX_IPC_INFO:
    487 	case LINUX_SHM_STAT:
    488 	case LINUX_SHM_INFO:
    489 	default:
    490 		return EINVAL;
    491 	}
    492 	return sys___shmctl13(p, &nua, retval);
    493 }
    494 #endif /* SYSVSHM */
    495