Home | History | Annotate | Line # | Download | only in common
linux_ipc.c revision 1.16
      1 /*	$NetBSD: linux_ipc.c,v 1.16 1998/10/04 00:02:34 fvdl 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/types.h>
     40 #include <sys/param.h>
     41 #include <sys/shm.h>
     42 #include <sys/sem.h>
     43 #include <sys/msg.h>
     44 #include <sys/proc.h>
     45 #include <sys/systm.h>
     46 
     47 #include <sys/mount.h>
     48 #include <sys/syscallargs.h>
     49 
     50 #include <compat/linux/common/linux_types.h>
     51 #include <compat/linux/common/linux_signal.h>
     52 #include <compat/linux/common/linux_util.h>
     53 
     54 #include <compat/linux/linux_syscallargs.h>
     55 #include <compat/linux/linux_syscall.h>
     56 
     57 #include <compat/linux/common/linux_ipc.h>
     58 #include <compat/linux/common/linux_msg.h>
     59 #include <compat/linux/common/linux_shm.h>
     60 #include <compat/linux/common/linux_sem.h>
     61 #include <compat/linux/common/linux_ipccall.h>
     62 
     63 /*
     64  * Note: Not all linux architechtures have explicit versions
     65  *	of the SYSV* syscalls.  On the ones that don't
     66  *	we pretend that they are defined anyway.  *_args and
     67  *	prototypes are defined in individual headers;
     68  *	syscalls.master lists those syscalls as NOARGS.
     69  *
     70  *	The functions in multiarch are the ones that just need
     71  *	the arguments shuffled around and then use the
     72  *	normal NetBSD syscall.
     73  *
     74  * Function in multiarch:
     75  *	linux_sys_ipc		: linux_ipccall.c
     76  *	liunx_semop		: linux_ipccall.c
     77  *	linux_semget		: linux_ipccall.c
     78  *	linux_msgsnd		: linux_ipccall.c
     79  *	linux_msgrcv		: linux_ipccall.c
     80  *	linux_msgget		: linux_ipccall.c
     81  *	linux_shmdt		: linux_ipccall.c
     82  *	linux_shmget		: linux_ipccall.c
     83  */
     84 
     85 #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
     86 /*
     87  * Convert between Linux and NetBSD ipc_perm structures. Only the
     88  * order of the fields is different.
     89  */
     90 void
     91 linux_to_bsd_ipc_perm(lpp, bpp)
     92 	struct linux_ipc_perm *lpp;
     93 	struct ipc_perm *bpp;
     94 {
     95 
     96 	bpp->key = lpp->l_key;
     97 	bpp->uid = lpp->l_uid;
     98 	bpp->gid = lpp->l_gid;
     99 	bpp->cuid = lpp->l_cuid;
    100 	bpp->cgid = lpp->l_cgid;
    101 	bpp->mode = lpp->l_mode;
    102 	bpp->seq = lpp->l_seq;
    103 }
    104 
    105 void
    106 bsd_to_linux_ipc_perm(bpp, lpp)
    107 	struct ipc_perm *bpp;
    108 	struct linux_ipc_perm *lpp;
    109 {
    110 
    111 	lpp->l_key = bpp->key;
    112 	lpp->l_uid = bpp->uid;
    113 	lpp->l_gid = bpp->gid;
    114 	lpp->l_cuid = bpp->cuid;
    115 	lpp->l_cgid = bpp->cgid;
    116 	lpp->l_mode = bpp->mode;
    117 	lpp->l_seq = bpp->seq;
    118 }
    119 #endif
    120 
    121 #ifdef SYSVSEM
    122 /*
    123  * Semaphore operations. Most constants and structures are the same on
    124  * both systems. Only semctl() needs some extra work.
    125  */
    126 
    127 /*
    128  * Convert between Linux and NetBSD semid_ds structures.
    129  */
    130 void
    131 bsd_to_linux_semid_ds(bs, ls)
    132 	struct semid_ds *bs;
    133 	struct linux_semid_ds *ls;
    134 {
    135 
    136 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    137 	ls->l_sem_otime = bs->sem_otime;
    138 	ls->l_sem_ctime = bs->sem_ctime;
    139 	ls->l_sem_nsems = bs->sem_nsems;
    140 	ls->l_sem_base = bs->sem_base;
    141 }
    142 
    143 void
    144 linux_to_bsd_semid_ds(ls, bs)
    145 	struct linux_semid_ds *ls;
    146 	struct semid_ds *bs;
    147 {
    148 
    149 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    150 	bs->sem_otime = ls->l_sem_otime;
    151 	bs->sem_ctime = ls->l_sem_ctime;
    152 	bs->sem_nsems = ls->l_sem_nsems;
    153 	bs->sem_base = ls->l_sem_base;
    154 }
    155 
    156 /*
    157  * Most of this can be handled by directly passing the arguments on,
    158  * but IPC_* require a lot of copy{in,out} because of the extra indirection
    159  * (we need to pass a pointer to a union cointaining a pointer to a semid_ds
    160  * structure.  Linux actually handles this better than we do.)
    161  */
    162 int
    163 linux_sys_semctl(p, v, retval)
    164 	struct proc *p;
    165 	void *v;
    166 	register_t *retval;
    167 {
    168 	struct linux_sys_semctl_args /* {
    169 		syscallarg(int) semid;
    170 		syscallarg(int) semnum;
    171 		syscallarg(int) cmd;
    172 		syscallarg(union linux_semun) arg;
    173 	} */ *uap = v;
    174 	caddr_t sg;
    175 	struct sys___semctl_args nua;
    176 	struct semid_ds *bmp, bm;
    177 	struct linux_semid_ds lm;
    178 	union semun *bup;
    179 	int error;
    180 
    181 	SCARG(&nua, semid) = SCARG(uap, semid);
    182 	SCARG(&nua, semnum) = SCARG(uap, semnum);
    183 	switch (SCARG(uap, cmd)) {
    184 	case LINUX_IPC_STAT:
    185 		sg = stackgap_init(p->p_emul);
    186 		bup = stackgap_alloc(&sg, sizeof (union semun));
    187 		bmp = stackgap_alloc(&sg, sizeof (struct semid_ds));
    188 		if ((error = copyout(&bmp, bup, sizeof bmp)))
    189 			return error;
    190 		SCARG(&nua, cmd) = IPC_STAT;
    191 		SCARG(&nua, arg) = bup;
    192 		if ((error = sys___semctl(p, &nua, retval)))
    193 			return error;
    194 		if ((error = copyin(bmp, &bm, sizeof bm)))
    195 			return error;
    196 		bsd_to_linux_semid_ds(&bm, &lm);
    197 		return copyout(&lm, SCARG(uap, arg).l_buf, sizeof lm);
    198 	case LINUX_IPC_SET:
    199 		if ((error = copyin(SCARG(uap, arg).l_buf, &lm, sizeof lm)))
    200 			return error;
    201 		linux_to_bsd_semid_ds(&lm, &bm);
    202 		sg = stackgap_init(p->p_emul);
    203 		bup = stackgap_alloc(&sg, sizeof (union semun));
    204 		bmp = stackgap_alloc(&sg, sizeof (struct semid_ds));
    205 		if ((error = copyout(&bm, bmp, sizeof bm)))
    206 			return error;
    207 		if ((error = copyout(&bmp, bup, sizeof bmp)))
    208 			return error;
    209 		SCARG(&nua, cmd) = IPC_SET;
    210 		SCARG(&nua, arg) = bup;
    211 		break;
    212 	case LINUX_IPC_RMID:
    213 		SCARG(&nua, cmd) = IPC_RMID;
    214 		break;
    215 	case LINUX_GETVAL:
    216 		SCARG(&nua, cmd) = GETVAL;
    217 		break;
    218 	case LINUX_GETPID:
    219 		SCARG(&nua, cmd) = GETPID;
    220 		break;
    221 	case LINUX_GETNCNT:
    222 		SCARG(&nua, cmd) = GETNCNT;
    223 		break;
    224 	case LINUX_GETZCNT:
    225 		SCARG(&nua, cmd) = GETZCNT;
    226 		break;
    227 	case LINUX_SETVAL:
    228 		SCARG(&nua, cmd) = SETVAL;
    229 		break;
    230 	default:
    231 		return EINVAL;
    232 	}
    233 	return sys___semctl(p, &nua, retval);
    234 }
    235 #endif /* SYSVSEM */
    236 
    237 #ifdef SYSVMSG
    238 
    239 void
    240 linux_to_bsd_msqid_ds(lmp, bmp)
    241 	struct linux_msqid_ds *lmp;
    242 	struct msqid_ds *bmp;
    243 {
    244 
    245 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    246 	bmp->msg_first = lmp->l_msg_first;
    247 	bmp->msg_last = lmp->l_msg_last;
    248 	bmp->msg_cbytes = lmp->l_msg_cbytes;
    249 	bmp->msg_qnum = lmp->l_msg_qnum;
    250 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    251 	bmp->msg_lspid = lmp->l_msg_lspid;
    252 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    253 	bmp->msg_stime = lmp->l_msg_stime;
    254 	bmp->msg_rtime = lmp->l_msg_rtime;
    255 	bmp->msg_ctime = lmp->l_msg_ctime;
    256 }
    257 
    258 void
    259 bsd_to_linux_msqid_ds(bmp, lmp)
    260 	struct msqid_ds *bmp;
    261 	struct linux_msqid_ds *lmp;
    262 {
    263 
    264 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    265 	lmp->l_msg_first = bmp->msg_first;
    266 	lmp->l_msg_last = bmp->msg_last;
    267 	lmp->l_msg_cbytes = bmp->msg_cbytes;
    268 	lmp->l_msg_qnum = bmp->msg_qnum;
    269 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    270 	lmp->l_msg_lspid = bmp->msg_lspid;
    271 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    272 	lmp->l_msg_stime = bmp->msg_stime;
    273 	lmp->l_msg_rtime = bmp->msg_rtime;
    274 	lmp->l_msg_ctime = bmp->msg_ctime;
    275 }
    276 
    277 int
    278 linux_sys_msgctl(p, v, retval)
    279 	struct proc *p;
    280 	void *v;
    281 	register_t *retval;
    282 {
    283 	struct linux_sys_msgctl_args /* {
    284 		syscallarg(int) msqid;
    285 		syscallarg(int) cmd;
    286 		syscallarg(struct linux_msqid_ds *) buf;
    287 	} */ *uap = v;
    288 	caddr_t sg;
    289 	struct sys_msgctl_args nua;
    290 	struct msqid_ds *bmp, bm;
    291 	struct linux_msqid_ds lm;
    292 	int error;
    293 
    294 	SCARG(&nua, msqid) = SCARG(uap, msqid);
    295 	switch (SCARG(uap, cmd)) {
    296 	case LINUX_IPC_STAT:
    297 		sg = stackgap_init(p->p_emul);
    298 		bmp = stackgap_alloc(&sg, sizeof (struct msqid_ds));
    299 		SCARG(&nua, cmd) = IPC_STAT;
    300 		SCARG(&nua, buf) = bmp;
    301 		if ((error = sys_msgctl(p, &nua, retval)))
    302 			return error;
    303 		if ((error = copyin(bmp, &bm, sizeof bm)))
    304 			return error;
    305 		bsd_to_linux_msqid_ds(&bm, &lm);
    306 		return copyout(&lm, SCARG(uap, buf), sizeof lm);
    307 	case LINUX_IPC_SET:
    308 		if ((error = copyin(SCARG(uap, buf), &lm, sizeof lm)))
    309 			return error;
    310 		linux_to_bsd_msqid_ds(&lm, &bm);
    311 		sg = stackgap_init(p->p_emul);
    312 		bmp = stackgap_alloc(&sg, sizeof bm);
    313 		if ((error = copyout(&bm, bmp, sizeof bm)))
    314 			return error;
    315 		SCARG(&nua, cmd) = IPC_SET;
    316 		SCARG(&nua, buf) = bmp;
    317 		break;
    318 	case LINUX_IPC_RMID:
    319 		SCARG(&nua, cmd) = IPC_RMID;
    320 		SCARG(&nua, buf) = NULL;
    321 		break;
    322 	default:
    323 		return EINVAL;
    324 	}
    325 	return sys_msgctl(p, &nua, retval);
    326 }
    327 #endif /* SYSVMSG */
    328 
    329 #ifdef SYSVSHM
    330 /*
    331  * shmat(2). Very straightforward, except that Linux passes a pointer
    332  * in which the return value is to be passed. This is subsequently
    333  * handled by libc, apparently.
    334  */
    335 int
    336 linux_sys_shmat(p, v, retval)
    337 	struct proc *p;
    338 	void *v;
    339 	register_t *retval;
    340 {
    341 	struct linux_sys_shmat_args /* {
    342 		syscallarg(int) shmid;
    343 		syscallarg(void *) shmaddr;
    344 		syscallarg(int) shmflg;
    345 		syscallarg(u_long *) raddr;
    346 	} */ *uap = v;
    347 	int error;
    348 
    349 	if ((error = sys_shmat(p, uap, retval)))
    350 		return error;
    351 
    352 	if ((error = copyout(&retval[0], (caddr_t) SCARG(uap, raddr),
    353 	     sizeof retval[0])))
    354 		return error;
    355 
    356 	retval[0] = 0;
    357 	return 0;
    358 }
    359 
    360 /*
    361  * Convert between Linux and NetBSD shmid_ds structures.
    362  * The order of the fields is once again the difference, and
    363  * we also need a place to store the internal data pointer
    364  * in, which is unfortunately stored in this structure.
    365  *
    366  * We abuse a Linux internal field for that.
    367  */
    368 void
    369 linux_to_bsd_shmid_ds(lsp, bsp)
    370 	struct linux_shmid_ds *lsp;
    371 	struct shmid_ds *bsp;
    372 {
    373 
    374 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    375 	bsp->shm_segsz = lsp->l_shm_segsz;
    376 	bsp->shm_lpid = lsp->l_shm_lpid;
    377 	bsp->shm_cpid = lsp->l_shm_cpid;
    378 	bsp->shm_nattch = lsp->l_shm_nattch;
    379 	bsp->shm_atime = lsp->l_shm_atime;
    380 	bsp->shm_dtime = lsp->l_shm_dtime;
    381 	bsp->shm_ctime = lsp->l_shm_ctime;
    382 	bsp->shm_internal = lsp->l_private2;	/* XXX Oh well. */
    383 }
    384 
    385 void
    386 bsd_to_linux_shmid_ds(bsp, lsp)
    387 	struct shmid_ds *bsp;
    388 	struct linux_shmid_ds *lsp;
    389 {
    390 
    391 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    392 	lsp->l_shm_segsz = bsp->shm_segsz;
    393 	lsp->l_shm_lpid = bsp->shm_lpid;
    394 	lsp->l_shm_cpid = bsp->shm_cpid;
    395 	lsp->l_shm_nattch = bsp->shm_nattch;
    396 	lsp->l_shm_atime = bsp->shm_atime;
    397 	lsp->l_shm_dtime = bsp->shm_dtime;
    398 	lsp->l_shm_ctime = bsp->shm_ctime;
    399 	lsp->l_private2 = bsp->shm_internal;	/* XXX */
    400 }
    401 
    402 /*
    403  * shmctl. Not implemented (for now): IPC_INFO, SHM_INFO, SHM_STAT
    404  * SHM_LOCK and SHM_UNLOCK are passed on, but currently not implemented
    405  * by NetBSD itself.
    406  *
    407  * The usual structure conversion and massaging is done.
    408  */
    409 int
    410 linux_sys_shmctl(p, v, retval)
    411 	struct proc *p;
    412 	void *v;
    413 	register_t *retval;
    414 {
    415 	struct linux_sys_shmctl_args /* {
    416 		syscallarg(int) shmid;
    417 		syscallarg(int) cmd;
    418 		syscallarg(struct linux_shmid_ds *) buf;
    419 	} */ *uap = v;
    420 	caddr_t sg;
    421 	struct sys_shmctl_args nua;
    422 	struct shmid_ds *bsp, bs;
    423 	struct linux_shmid_ds ls;
    424 	int error;
    425 
    426 	SCARG(&nua, shmid) = SCARG(uap, shmid);
    427 	switch (SCARG(uap, cmd)) {
    428 	case LINUX_IPC_STAT:
    429 		sg = stackgap_init(p->p_emul);
    430 		bsp = stackgap_alloc(&sg, sizeof(struct shmid_ds));
    431 		SCARG(&nua, cmd) = IPC_STAT;
    432 		SCARG(&nua, buf) = bsp;
    433 		if ((error = sys_shmctl(p, &nua, retval)))
    434 			return error;
    435 		if ((error = copyin(SCARG(&nua, buf), &bs, sizeof bs)))
    436 			return error;
    437 		bsd_to_linux_shmid_ds(&bs, &ls);
    438 		return copyout(&ls, SCARG(uap, buf), sizeof ls);
    439 	case LINUX_IPC_SET:
    440 		if ((error = copyin(SCARG(uap, buf), &ls, sizeof ls)))
    441 			return error;
    442 		linux_to_bsd_shmid_ds(&ls, &bs);
    443 		sg = stackgap_init(p->p_emul);
    444 		bsp = stackgap_alloc(&sg, sizeof bs);
    445 		if ((error = copyout(&bs, bsp, sizeof bs)))
    446 			return error;
    447 		SCARG(&nua, cmd) = IPC_SET;
    448 		SCARG(&nua, buf) = bsp;
    449 		break;
    450 	case LINUX_IPC_RMID:
    451 		SCARG(&nua, cmd) = IPC_RMID;
    452 		SCARG(&nua, buf) = NULL;
    453 		break;
    454 	case LINUX_SHM_LOCK:
    455 		SCARG(&nua, cmd) = SHM_LOCK;
    456 		SCARG(&nua, buf) = NULL;
    457 		break;
    458 	case LINUX_SHM_UNLOCK:
    459 		SCARG(&nua, cmd) = SHM_UNLOCK;
    460 		SCARG(&nua, buf) = NULL;
    461 		break;
    462 	case LINUX_IPC_INFO:
    463 	case LINUX_SHM_STAT:
    464 	case LINUX_SHM_INFO:
    465 	default:
    466 		return EINVAL;
    467 	}
    468 	return sys_shmctl(p, &nua, retval);
    469 }
    470 #endif /* SYSVSHM */
    471