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