Home | History | Annotate | Line # | Download | only in common
linux_ipc.c revision 1.22.2.1
      1 /*	$NetBSD: linux_ipc.c,v 1.22.2.1 2001/03/05 22:49:25 nathanw 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 #if defined(_KERNEL) && !defined(_LKM)
     40 #include "opt_sysv.h"
     41 #endif
     42 
     43 #include <sys/types.h>
     44 #include <sys/param.h>
     45 #include <sys/shm.h>
     46 #include <sys/sem.h>
     47 #include <sys/msg.h>
     48 #include <sys/lwp.h>
     49 #include <sys/proc.h>
     50 #include <sys/systm.h>
     51 
     52 #include <sys/mount.h>
     53 #include <sys/syscallargs.h>
     54 
     55 #include <compat/linux/common/linux_types.h>
     56 #include <compat/linux/common/linux_signal.h>
     57 #include <compat/linux/common/linux_util.h>
     58 
     59 #include <compat/linux/linux_syscallargs.h>
     60 #include <compat/linux/linux_syscall.h>
     61 
     62 #include <compat/linux/common/linux_ipc.h>
     63 #include <compat/linux/common/linux_msg.h>
     64 #include <compat/linux/common/linux_shm.h>
     65 #include <compat/linux/common/linux_sem.h>
     66 #include <compat/linux/common/linux_ipccall.h>
     67 
     68 /*
     69  * Note: Not all linux architechtures have explicit versions
     70  *	of the SYSV* syscalls.  On the ones that don't
     71  *	we pretend that they are defined anyway.  *_args and
     72  *	prototypes are defined in individual headers;
     73  *	syscalls.master lists those syscalls as NOARGS.
     74  *
     75  *	The functions in multiarch are the ones that just need
     76  *	the arguments shuffled around and then use the
     77  *	normal NetBSD syscall.
     78  *
     79  * Function in multiarch:
     80  *	linux_sys_ipc		: linux_ipccall.c
     81  *	liunx_semop		: linux_ipccall.c
     82  *	linux_semget		: linux_ipccall.c
     83  *	linux_msgsnd		: linux_ipccall.c
     84  *	linux_msgrcv		: linux_ipccall.c
     85  *	linux_msgget		: linux_ipccall.c
     86  *	linux_shmdt		: linux_ipccall.c
     87  *	linux_shmget		: linux_ipccall.c
     88  */
     89 
     90 #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
     91 /*
     92  * Convert between Linux and NetBSD ipc_perm structures. Only the
     93  * order of the fields is different.
     94  */
     95 void
     96 linux_to_bsd_ipc_perm(lpp, bpp)
     97 	struct linux_ipc_perm *lpp;
     98 	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 bsd_to_linux_ipc_perm(bpp, lpp)
    112 	struct ipc_perm *bpp;
    113 	struct linux_ipc_perm *lpp;
    114 {
    115 
    116 	lpp->l_key = bpp->_key;
    117 	lpp->l_uid = bpp->uid;
    118 	lpp->l_gid = bpp->gid;
    119 	lpp->l_cuid = bpp->cuid;
    120 	lpp->l_cgid = bpp->cgid;
    121 	lpp->l_mode = bpp->mode;
    122 	lpp->l_seq = bpp->_seq;
    123 }
    124 #endif
    125 
    126 #ifdef SYSVSEM
    127 /*
    128  * Semaphore operations. Most constants and structures are the same on
    129  * both systems. Only semctl() needs some extra work.
    130  */
    131 
    132 /*
    133  * Convert between Linux and NetBSD semid_ds structures.
    134  */
    135 void
    136 bsd_to_linux_semid_ds(bs, ls)
    137 	struct semid_ds *bs;
    138 	struct linux_semid_ds *ls;
    139 {
    140 
    141 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    142 	ls->l_sem_otime = bs->sem_otime;
    143 	ls->l_sem_ctime = bs->sem_ctime;
    144 	ls->l_sem_nsems = bs->sem_nsems;
    145 	ls->l_sem_base = bs->_sem_base;
    146 }
    147 
    148 void
    149 linux_to_bsd_semid_ds(ls, bs)
    150 	struct linux_semid_ds *ls;
    151 	struct semid_ds *bs;
    152 {
    153 
    154 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    155 	bs->sem_otime = ls->l_sem_otime;
    156 	bs->sem_ctime = ls->l_sem_ctime;
    157 	bs->sem_nsems = ls->l_sem_nsems;
    158 	bs->_sem_base = ls->l_sem_base;
    159 }
    160 
    161 /*
    162  * Most of this can be handled by directly passing the arguments on; we
    163  * just need to frob the `cmd' and convert the semid_ds and semun.
    164  */
    165 int
    166 linux_sys_semctl(l, v, retval)
    167 	struct lwp *l;
    168 	void *v;
    169 	register_t *retval;
    170 {
    171 	struct linux_sys_semctl_args /* {
    172 		syscallarg(int) semid;
    173 		syscallarg(int) semnum;
    174 		syscallarg(int) cmd;
    175 		syscallarg(union linux_semun) arg;
    176 	} */ *uap = v;
    177 	struct proc *p = l->l_proc;
    178 	struct semid_ds sembuf;
    179 	struct linux_semid_ds lsembuf;
    180 	union __semun semun;
    181 	int cmd, error;
    182 	void *pass_arg = NULL;
    183 
    184 	cmd = SCARG(uap, cmd);
    185 
    186 	switch (cmd) {
    187 	case LINUX_IPC_SET:
    188 		pass_arg = &sembuf;
    189 		cmd = IPC_SET;
    190 		break;
    191 
    192 	case LINUX_IPC_STAT:
    193 		pass_arg = &sembuf;
    194 		cmd = IPC_STAT;
    195 		break;
    196 
    197 	case LINUX_IPC_RMID:
    198 		cmd = IPC_RMID;
    199 		break;
    200 
    201 	case LINUX_GETVAL:
    202 		cmd = GETVAL;
    203 		break;
    204 
    205 	case LINUX_GETPID:
    206 		cmd = GETPID;
    207 		break;
    208 
    209 	case LINUX_GETNCNT:
    210 		cmd = GETNCNT;
    211 		break;
    212 
    213 	case LINUX_GETZCNT:
    214 		cmd = GETZCNT;
    215 		break;
    216 
    217 	case LINUX_GETALL:
    218 		pass_arg = &semun;
    219 		semun.array = SCARG(uap, arg).l_array;
    220 		cmd = GETALL;
    221 		break;
    222 
    223 	case LINUX_SETVAL:
    224 		pass_arg = &semun;
    225 		semun.val = SCARG(uap, arg).l_val;
    226 		cmd = SETVAL;
    227 		break;
    228 
    229 	case LINUX_SETALL:
    230 		pass_arg = &semun;
    231 		semun.array = SCARG(uap, arg).l_array;
    232 		cmd = SETALL;
    233 		break;
    234 
    235 	default:
    236 		return (EINVAL);
    237 	}
    238 
    239 	if (cmd == IPC_SET) {
    240 		error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
    241 		    sizeof(lsembuf));
    242 		if (error)
    243 			return (error);
    244 		linux_to_bsd_semid_ds(&lsembuf, &sembuf);
    245 	}
    246 
    247 	error = semctl1(p, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    248 	    pass_arg, retval);
    249 
    250 	if (error == 0 && cmd == IPC_STAT) {
    251 		bsd_to_linux_semid_ds(&sembuf, &lsembuf);
    252 		error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
    253 		    sizeof(lsembuf));
    254 	}
    255 
    256 	return (error);
    257 }
    258 #endif /* SYSVSEM */
    259 
    260 #ifdef SYSVMSG
    261 
    262 void
    263 linux_to_bsd_msqid_ds(lmp, bmp)
    264 	struct linux_msqid_ds *lmp;
    265 	struct msqid_ds *bmp;
    266 {
    267 
    268 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    269 	bmp->_msg_first = lmp->l_msg_first;
    270 	bmp->_msg_last = lmp->l_msg_last;
    271 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    272 	bmp->msg_qnum = lmp->l_msg_qnum;
    273 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    274 	bmp->msg_lspid = lmp->l_msg_lspid;
    275 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    276 	bmp->msg_stime = lmp->l_msg_stime;
    277 	bmp->msg_rtime = lmp->l_msg_rtime;
    278 	bmp->msg_ctime = lmp->l_msg_ctime;
    279 }
    280 
    281 void
    282 bsd_to_linux_msqid_ds(bmp, lmp)
    283 	struct msqid_ds *bmp;
    284 	struct linux_msqid_ds *lmp;
    285 {
    286 
    287 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    288 	lmp->l_msg_first = bmp->_msg_first;
    289 	lmp->l_msg_last = bmp->_msg_last;
    290 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    291 	lmp->l_msg_qnum = bmp->msg_qnum;
    292 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    293 	lmp->l_msg_lspid = bmp->msg_lspid;
    294 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    295 	lmp->l_msg_stime = bmp->msg_stime;
    296 	lmp->l_msg_rtime = bmp->msg_rtime;
    297 	lmp->l_msg_ctime = bmp->msg_ctime;
    298 }
    299 
    300 int
    301 linux_sys_msgctl(l, v, retval)
    302 	struct lwp *l;
    303 	void *v;
    304 	register_t *retval;
    305 {
    306 	struct linux_sys_msgctl_args /* {
    307 		syscallarg(int) msqid;
    308 		syscallarg(int) cmd;
    309 		syscallarg(struct linux_msqid_ds *) buf;
    310 	} */ *uap = v;
    311 	struct proc *p = l->l_proc;
    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(l, &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(l, &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(l, v, retval)
    361 	struct lwp *l;
    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(l, 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(l, v, retval)
    435 	struct lwp *l;
    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 	struct proc *p = l->l_proc;
    445 	caddr_t sg;
    446 	struct sys___shmctl13_args nua;
    447 	struct shmid_ds *bsp, bs;
    448 	struct linux_shmid_ds ls;
    449 	int error;
    450 
    451 	SCARG(&nua, shmid) = SCARG(uap, shmid);
    452 	switch (SCARG(uap, cmd)) {
    453 	case LINUX_IPC_STAT:
    454 		sg = stackgap_init(p->p_emul);
    455 		bsp = stackgap_alloc(&sg, sizeof(struct shmid_ds));
    456 		SCARG(&nua, cmd) = IPC_STAT;
    457 		SCARG(&nua, buf) = bsp;
    458 		if ((error = sys___shmctl13(l, &nua, retval)))
    459 			return error;
    460 		if ((error = copyin(SCARG(&nua, buf), &bs, sizeof bs)))
    461 			return error;
    462 		bsd_to_linux_shmid_ds(&bs, &ls);
    463 		return copyout(&ls, SCARG(uap, buf), sizeof ls);
    464 	case LINUX_IPC_SET:
    465 		if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
    466 			return error;
    467 		linux_to_bsd_shmid_ds(&ls, &bs);
    468 		sg = stackgap_init(p->p_emul);
    469 		bsp = stackgap_alloc(&sg, sizeof bs);
    470 		if ((error = copyout(&bs, bsp, sizeof bs)))
    471 			return error;
    472 		SCARG(&nua, cmd) = IPC_SET;
    473 		SCARG(&nua, buf) = bsp;
    474 		break;
    475 	case LINUX_IPC_RMID:
    476 		SCARG(&nua, cmd) = IPC_RMID;
    477 		SCARG(&nua, buf) = NULL;
    478 		break;
    479 	case LINUX_SHM_LOCK:
    480 		SCARG(&nua, cmd) = SHM_LOCK;
    481 		SCARG(&nua, buf) = NULL;
    482 		break;
    483 	case LINUX_SHM_UNLOCK:
    484 		SCARG(&nua, cmd) = SHM_UNLOCK;
    485 		SCARG(&nua, buf) = NULL;
    486 		break;
    487 	case LINUX_IPC_INFO:
    488 	case LINUX_SHM_STAT:
    489 	case LINUX_SHM_INFO:
    490 	default:
    491 		return EINVAL;
    492 	}
    493 	return sys___shmctl13(l, &nua, retval);
    494 }
    495 #endif /* SYSVSHM */
    496