Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: linux_ipc.c,v 1.61 2026/09/20 13:43:51 riastradh 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.61 2026/09/20 13:43:51 riastradh 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/syscallargs.h>
     49 
     50 #include <compat/linux/common/linux_types.h>
     51 #include <compat/linux/common/linux_sched.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 architectures 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  *	linux_semop		: linux_ipccall.c
     79  *	linux_semtimedop	: linux_ipccall.c
     80  *	linux_semget		: linux_ipccall.c
     81  *	linux_msgsnd		: linux_ipccall.c
     82  *	linux_msgrcv		: linux_ipccall.c
     83  *	linux_msgget		: linux_ipccall.c
     84  *	linux_shmdt		: linux_ipccall.c
     85  *	linux_shmget		: linux_ipccall.c
     86  */
     87 
     88 #if defined (SYSVSEM) || defined(SYSVSHM) || defined(SYSVMSG)
     89 /*
     90  * Convert between Linux and NetBSD ipc_perm structures. Only the
     91  * order of the fields is different.
     92  */
     93 void
     94 linux_to_bsd_ipc_perm(struct linux_ipc_perm *lpp, struct ipc_perm *bpp)
     95 {
     96 
     97 	bpp->_key = lpp->l_key;
     98 	bpp->uid = lpp->l_uid;
     99 	bpp->gid = lpp->l_gid;
    100 	bpp->cuid = lpp->l_cuid;
    101 	bpp->cgid = lpp->l_cgid;
    102 	bpp->mode = lpp->l_mode;
    103 	bpp->_seq = lpp->l_seq;
    104 }
    105 
    106 void
    107 linux_to_bsd_ipc64_perm(struct linux_ipc64_perm *lpp, struct ipc_perm *bpp)
    108 {
    109 	bpp->_key = lpp->l_key;
    110 	bpp->uid = lpp->l_uid;
    111 	bpp->gid = lpp->l_gid;
    112 	bpp->cuid = lpp->l_cuid;
    113 	bpp->cgid = lpp->l_cgid;
    114 	bpp->mode = lpp->l_mode;
    115 	bpp->_seq = lpp->l_seq;
    116 }
    117 
    118 void
    119 bsd_to_linux_ipc_perm(struct ipc_perm *bpp, struct linux_ipc_perm *lpp)
    120 {
    121 
    122 	memset(lpp, 0, sizeof *lpp);
    123 	lpp->l_key = bpp->_key;
    124 	lpp->l_uid = bpp->uid;
    125 	lpp->l_gid = bpp->gid;
    126 	lpp->l_cuid = bpp->cuid;
    127 	lpp->l_cgid = bpp->cgid;
    128 	lpp->l_mode = bpp->mode;
    129 	lpp->l_seq = bpp->_seq;
    130 }
    131 
    132 void
    133 bsd_to_linux_ipc64_perm(struct ipc_perm *bpp, struct linux_ipc64_perm *lpp)
    134 {
    135 
    136 	memset(lpp, 0, sizeof *lpp);
    137 	lpp->l_key = bpp->_key;
    138 	lpp->l_uid = bpp->uid;
    139 	lpp->l_gid = bpp->gid;
    140 	lpp->l_cuid = bpp->cuid;
    141 	lpp->l_cgid = bpp->cgid;
    142 	lpp->l_mode = bpp->mode;
    143 	lpp->l_seq = bpp->_seq;
    144 }
    145 
    146 #endif
    147 
    148 #ifdef SYSVSEM
    149 /*
    150  * Semaphore operations. Most constants and structures are the same on
    151  * both systems. Only semctl() needs some extra work.
    152  */
    153 
    154 /*
    155  * Convert between Linux and NetBSD semid_ds structures.
    156  */
    157 void
    158 bsd_to_linux_semid_ds(struct semid_ds *bs, struct linux_semid_ds *ls)
    159 {
    160 
    161 	memset(ls, 0, sizeof *ls);
    162 	bsd_to_linux_ipc_perm(&bs->sem_perm, &ls->l_sem_perm);
    163 	ls->l_sem_otime = bs->sem_otime;
    164 	ls->l_sem_ctime = bs->sem_ctime;
    165 	ls->l_sem_nsems = bs->sem_nsems;
    166 }
    167 
    168 void
    169 bsd_to_linux_semid64_ds(struct semid_ds *bs, struct linux_semid64_ds *ls)
    170 {
    171 
    172 	memset(ls, 0, sizeof *ls);
    173 	bsd_to_linux_ipc64_perm(&bs->sem_perm, &ls->l_sem_perm);
    174 	ls->l_sem_otime = bs->sem_otime;
    175 	ls->l_sem_ctime = bs->sem_ctime;
    176 	ls->l_sem_nsems = bs->sem_nsems;
    177 }
    178 
    179 void
    180 linux_to_bsd_semid_ds(struct linux_semid_ds *ls, struct semid_ds *bs)
    181 {
    182 
    183 	linux_to_bsd_ipc_perm(&ls->l_sem_perm, &bs->sem_perm);
    184 	bs->sem_otime = ls->l_sem_otime;
    185 	bs->sem_ctime = ls->l_sem_ctime;
    186 	bs->sem_nsems = ls->l_sem_nsems;
    187 }
    188 
    189 void
    190 linux_to_bsd_semid64_ds(struct linux_semid64_ds *ls, struct semid_ds *bs)
    191 {
    192 
    193 	linux_to_bsd_ipc64_perm(&ls->l_sem_perm, &bs->sem_perm);
    194 	bs->sem_otime = ls->l_sem_otime;
    195 	bs->sem_ctime = ls->l_sem_ctime;
    196 	bs->sem_nsems = ls->l_sem_nsems;
    197 }
    198 
    199 /*
    200  * Most of this can be handled by directly passing the arguments on; we
    201  * just need to frob the `cmd' and convert the semid_ds and semun.
    202  */
    203 int
    204 linux_sys_semctl(struct lwp *l, const struct linux_sys_semctl_args *uap, register_t *retval)
    205 {
    206 	/* {
    207 		syscallarg(int) semid;
    208 		syscallarg(int) semnum;
    209 		syscallarg(int) cmd;
    210 		syscallarg(union linux_semun) arg;
    211 	} */
    212 	struct semid_ds sembuf;
    213 	struct linux_semid_ds lsembuf;
    214 	struct linux_semid64_ds lsembuf64;
    215 	union __semun semun;
    216 	int cmd, lcmd, error;
    217 	void *pass_arg = NULL;
    218 
    219 	lcmd = SCARG(uap, cmd);
    220 #ifdef LINUX_IPC_FORCE64
    221 	lcmd |= LINUX_IPC_64;
    222 #endif
    223 
    224 	switch (lcmd & ~LINUX_IPC_64) {
    225 	case LINUX_IPC_SET:
    226 		if (lcmd & LINUX_IPC_64) {
    227 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf64,
    228 		    	    sizeof(lsembuf64));
    229 			linux_to_bsd_semid64_ds(&lsembuf64, &sembuf);
    230 		} else {
    231 			error = copyin(SCARG(uap, arg).l_buf, &lsembuf,
    232 		    	   sizeof(lsembuf));
    233 			linux_to_bsd_semid_ds(&lsembuf, &sembuf);
    234 		}
    235 		if (error)
    236 			return (error);
    237 		pass_arg = &sembuf;
    238 		cmd = IPC_SET;
    239 		break;
    240 
    241 	case LINUX_IPC_STAT:
    242 		pass_arg = &sembuf;
    243 		cmd = IPC_STAT;
    244 		break;
    245 
    246 	case LINUX_IPC_RMID:
    247 		cmd = IPC_RMID;
    248 		break;
    249 
    250 	case LINUX_GETVAL:
    251 		cmd = GETVAL;
    252 		break;
    253 
    254 	case LINUX_GETPID:
    255 		cmd = GETPID;
    256 		break;
    257 
    258 	case LINUX_GETNCNT:
    259 		cmd = GETNCNT;
    260 		break;
    261 
    262 	case LINUX_GETZCNT:
    263 		cmd = GETZCNT;
    264 		break;
    265 
    266 	case LINUX_GETALL:
    267 		pass_arg = &semun;
    268 		semun.array = SCARG(uap, arg).l_array;
    269 		cmd = GETALL;
    270 		break;
    271 
    272 	case LINUX_SETVAL:
    273 		pass_arg = &semun;
    274 		semun.val = SCARG(uap, arg).l_val;
    275 		cmd = SETVAL;
    276 		break;
    277 
    278 	case LINUX_SETALL:
    279 		pass_arg = &semun;
    280 		semun.array = SCARG(uap, arg).l_array;
    281 		cmd = SETALL;
    282 		break;
    283 
    284 	default:
    285 		return (EINVAL);
    286 	}
    287 
    288 	error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    289 	    pass_arg, retval);
    290 	if (error)
    291 		return error;
    292 
    293 	switch (lcmd) {
    294 	case LINUX_IPC_STAT:
    295 		bsd_to_linux_semid_ds(&sembuf, &lsembuf);
    296 		error = copyout(&lsembuf, SCARG(uap, arg).l_buf,
    297 		    sizeof(lsembuf));
    298 		break;
    299 	case LINUX_IPC_STAT | LINUX_IPC_64:
    300 		bsd_to_linux_semid64_ds(&sembuf, &lsembuf64);
    301 		error = copyout(&lsembuf64, SCARG(uap, arg).l_buf,
    302 		    sizeof(lsembuf64));
    303 		break;
    304 	default:
    305 		break;
    306 	}
    307 
    308 	return (error);
    309 }
    310 
    311 
    312 #ifdef LINUX_SYS_semtimedop
    313 /* Adapted from do_semop() to accept pointer to linux_timespec */
    314 static int
    315 linux_timespec_copyin(const void *uptr, void *kptr, size_t len __unused)
    316 {
    317 	struct linux_timespec lts;
    318 	int error = copyin(uptr, &lts, sizeof(lts));
    319 	if (error)
    320 		return error;
    321 	linux_to_native_timespec(kptr, &lts);
    322 	return 0;
    323 }
    324 
    325 int
    326 linux_sys_semtimedop(struct lwp *l, const struct linux_sys_semtimedop_args *uap,
    327     register_t *retval)
    328 {
    329 	/* {
    330 		syscallarg(int) semid;
    331 		syscallarg(struct sembuf *) sops;
    332 		syscallarg(size_t) nsops;
    333 		syscallarg(struct linux_timespec) timeout;
    334 	} */
    335 	return do_semop(l, SCARG(uap, semid), SCARG(uap, sops),
    336 	    SCARG(uap, nsops), SCARG(uap, timeout), retval,
    337 	    linux_timespec_copyin);
    338 }
    339 #endif
    340 #endif /* SYSVSEM */
    341 
    342 #ifdef SYSVMSG
    343 
    344 void
    345 linux_to_bsd_msqid_ds(struct linux_msqid_ds *lmp, struct msqid_ds *bmp)
    346 {
    347 
    348 	memset(bmp, 0, sizeof(*bmp));
    349 	linux_to_bsd_ipc_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    350 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    351 	bmp->msg_qnum = lmp->l_msg_qnum;
    352 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    353 	bmp->msg_lspid = lmp->l_msg_lspid;
    354 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    355 	bmp->msg_stime = lmp->l_msg_stime;
    356 	bmp->msg_rtime = lmp->l_msg_rtime;
    357 	bmp->msg_ctime = lmp->l_msg_ctime;
    358 }
    359 
    360 void
    361 linux_to_bsd_msqid64_ds(struct linux_msqid64_ds *lmp, struct msqid_ds *bmp)
    362 {
    363 
    364 	memset(bmp, 0, sizeof(*bmp));
    365 	linux_to_bsd_ipc64_perm(&lmp->l_msg_perm, &bmp->msg_perm);
    366 	bmp->_msg_cbytes = lmp->l_msg_cbytes;
    367 	bmp->msg_stime = lmp->l_msg_stime;
    368 	bmp->msg_rtime = lmp->l_msg_rtime;
    369 	bmp->msg_ctime = lmp->l_msg_ctime;
    370 	bmp->msg_qnum = lmp->l_msg_qnum;
    371 	bmp->msg_qbytes = lmp->l_msg_qbytes;
    372 	bmp->msg_lspid = lmp->l_msg_lspid;
    373 	bmp->msg_lrpid = lmp->l_msg_lrpid;
    374 }
    375 
    376 void
    377 bsd_to_linux_msqid_ds(struct msqid_ds *bmp, struct linux_msqid_ds *lmp)
    378 {
    379 
    380 	memset(lmp, 0, sizeof(*lmp));
    381 	bsd_to_linux_ipc_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    382 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    383 	lmp->l_msg_qnum = bmp->msg_qnum;
    384 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    385 	lmp->l_msg_lspid = bmp->msg_lspid;
    386 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    387 	lmp->l_msg_stime = bmp->msg_stime;
    388 	lmp->l_msg_rtime = bmp->msg_rtime;
    389 	lmp->l_msg_ctime = bmp->msg_ctime;
    390 }
    391 
    392 void
    393 bsd_to_linux_msqid64_ds(struct msqid_ds *bmp, struct linux_msqid64_ds *lmp)
    394 {
    395 
    396 	memset(lmp, 0, sizeof(*lmp));
    397 	bsd_to_linux_ipc64_perm(&bmp->msg_perm, &lmp->l_msg_perm);
    398 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    399 	lmp->l_msg_stime = bmp->msg_stime;
    400 	lmp->l_msg_rtime = bmp->msg_rtime;
    401 	lmp->l_msg_ctime = bmp->msg_ctime;
    402 	lmp->l_msg_cbytes = bmp->_msg_cbytes;
    403 	lmp->l_msg_qnum = bmp->msg_qnum;
    404 	lmp->l_msg_qbytes = bmp->msg_qbytes;
    405 	lmp->l_msg_lspid = bmp->msg_lspid;
    406 	lmp->l_msg_lrpid = bmp->msg_lrpid;
    407 }
    408 
    409 int
    410 linux_sys_msgctl(struct lwp *l, const struct linux_sys_msgctl_args *uap, register_t *retval)
    411 {
    412 	/* {
    413 		syscallarg(int) msqid;
    414 		syscallarg(int) cmd;
    415 		syscallarg(struct linux_msqid_ds *) buf;
    416 	} */
    417 	struct msqid_ds bm, *bmp = NULL;
    418 	struct linux_msqid_ds lm;
    419 	struct linux_msqid64_ds lm64;
    420 	int cmd, lcmd, error;
    421 
    422 	lcmd = SCARG(uap, cmd);
    423 #ifdef LINUX_IPC_FORCE64
    424 	lcmd |= LINUX_IPC_64;
    425 #endif
    426 
    427 	switch (lcmd & ~LINUX_IPC_64) {
    428 	case LINUX_IPC_STAT:
    429 		cmd = IPC_STAT;
    430 		bmp = &bm;
    431 		break;
    432 	case LINUX_IPC_SET:
    433 		if (lcmd & LINUX_IPC_64) {
    434 			error = copyin(SCARG(uap, buf), &lm64, sizeof lm64);
    435 			linux_to_bsd_msqid64_ds(&lm64, &bm);
    436 		} else {
    437 			error = copyin(SCARG(uap, buf), &lm, sizeof lm);
    438 			linux_to_bsd_msqid_ds(&lm, &bm);
    439 		}
    440 		if (error)
    441 			return error;
    442 		cmd = IPC_SET;
    443 		bmp = &bm;
    444 		break;
    445 	case LINUX_IPC_RMID:
    446 		cmd = IPC_RMID;
    447 		break;
    448 	default:
    449 		return EINVAL;
    450 	}
    451 
    452 	if ((error = msgctl1(l, SCARG(uap, msqid), cmd, bmp)))
    453 		return error;
    454 
    455 	switch (lcmd) {
    456 	case LINUX_IPC_STAT:
    457 		bsd_to_linux_msqid_ds(&bm, &lm);
    458 		error = copyout(&lm, SCARG(uap, buf), sizeof lm);
    459 		break;
    460 	case LINUX_IPC_STAT|LINUX_IPC_64:
    461 		bsd_to_linux_msqid64_ds(&bm, &lm64);
    462 		error = copyout(&lm64, SCARG(uap, buf), sizeof lm64);
    463 		break;
    464 	default:
    465 		break;
    466 	}
    467 
    468 	return error;
    469 }
    470 #endif /* SYSVMSG */
    471 
    472 #ifdef SYSVSHM
    473 /*
    474  * shmget(2). Just make sure the Linux-compatible shmat() semantics
    475  * is enabled for the segment, so that shmat() succeeds even when
    476  * the segment would be removed.
    477  */
    478 int
    479 linux_sys_shmget(struct lwp *l, const struct linux_sys_shmget_args *uap, register_t *retval)
    480 {
    481 	/* {
    482 		syscallarg(key_t) key;
    483 		syscallarg(size_t) size;
    484 		syscallarg(int) shmflg;
    485 	} */
    486 	struct sys_shmget_args bsd_ua;
    487 
    488 	memset(&bsd_ua, 0, sizeof(bsd_ua));
    489 	SCARG(&bsd_ua, key) = SCARG(uap, key);
    490 	SCARG(&bsd_ua, size) = SCARG(uap, size);
    491 	SCARG(&bsd_ua, shmflg) = SCARG(uap, shmflg) | _SHM_RMLINGER;
    492 
    493 	return sys_shmget(l, &bsd_ua, retval);
    494 }
    495 
    496 /*
    497  * shmat(2). Very straightforward, except that Linux passes a pointer
    498  * in which the return value is to be passed. This is subsequently
    499  * handled by libc, apparently.
    500  */
    501 #ifndef __amd64__
    502 int
    503 linux_sys_shmat(struct lwp *l, const struct linux_sys_shmat_args *uap, register_t *retval)
    504 {
    505 	/* {
    506 		syscallarg(int) shmid;
    507 		syscallarg(void *) shmaddr;
    508 		syscallarg(int) shmflg;
    509 		syscallarg(u_long *) raddr;
    510 	} */
    511 	int error;
    512 
    513 	if ((error = sys_shmat(l, (const void *)uap, retval)))
    514 		return error;
    515 
    516 	if ((error = copyout(&retval[0], SCARG(uap, raddr), sizeof retval[0])))
    517 		return error;
    518 
    519 	retval[0] = 0;
    520 	return 0;
    521 }
    522 #endif /* __amd64__ */
    523 
    524 /*
    525  * Convert between Linux and NetBSD shmid_ds structures.
    526  * The order of the fields is once again the difference, and
    527  * we also need a place to store the internal data pointer
    528  * in, which is unfortunately stored in this structure.
    529  *
    530  * We abuse a Linux internal field for that.
    531  */
    532 void
    533 linux_to_bsd_shmid_ds(struct linux_shmid_ds *lsp, struct shmid_ds *bsp)
    534 {
    535 
    536 	linux_to_bsd_ipc_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    537 	bsp->shm_segsz = lsp->l_shm_segsz;
    538 	bsp->shm_lpid = lsp->l_shm_lpid;
    539 	bsp->shm_cpid = lsp->l_shm_cpid;
    540 	bsp->shm_nattch = lsp->l_shm_nattch;
    541 	bsp->shm_atime = lsp->l_shm_atime;
    542 	bsp->shm_dtime = lsp->l_shm_dtime;
    543 	bsp->shm_ctime = lsp->l_shm_ctime;
    544 }
    545 
    546 void
    547 linux_to_bsd_shmid64_ds(struct linux_shmid64_ds *lsp, struct shmid_ds *bsp)
    548 {
    549 
    550 	linux_to_bsd_ipc64_perm(&lsp->l_shm_perm, &bsp->shm_perm);
    551 	bsp->shm_segsz = lsp->l_shm_segsz;
    552 	bsp->shm_lpid = lsp->l_shm_lpid;
    553 	bsp->shm_cpid = lsp->l_shm_cpid;
    554 	bsp->shm_nattch = lsp->l_shm_nattch;
    555 	bsp->shm_atime = lsp->l_shm_atime;
    556 	bsp->shm_dtime = lsp->l_shm_dtime;
    557 	bsp->shm_ctime = lsp->l_shm_ctime;
    558 }
    559 
    560 void
    561 bsd_to_linux_shmid_ds(struct shmid_ds *bsp, struct linux_shmid_ds *lsp)
    562 {
    563 
    564 	memset(lsp, 0, sizeof *lsp);
    565 	bsd_to_linux_ipc_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    566 	lsp->l_shm_segsz = bsp->shm_segsz;
    567 	lsp->l_shm_lpid = bsp->shm_lpid;
    568 	lsp->l_shm_cpid = bsp->shm_cpid;
    569 	lsp->l_shm_nattch = bsp->shm_nattch;
    570 	lsp->l_shm_atime = bsp->shm_atime;
    571 	lsp->l_shm_dtime = bsp->shm_dtime;
    572 	lsp->l_shm_ctime = bsp->shm_ctime;
    573 }
    574 
    575 void
    576 bsd_to_linux_shmid64_ds(struct shmid_ds *bsp, struct linux_shmid64_ds *lsp)
    577 {
    578 
    579 	memset(lsp, 0, sizeof *lsp);
    580 	bsd_to_linux_ipc64_perm(&bsp->shm_perm, &lsp->l_shm_perm);
    581 	lsp->l_shm_segsz = bsp->shm_segsz;
    582 	lsp->l_shm_lpid = bsp->shm_lpid;
    583 	lsp->l_shm_cpid = bsp->shm_cpid;
    584 	lsp->l_shm_nattch = bsp->shm_nattch;
    585 	lsp->l_shm_atime = bsp->shm_atime;
    586 	lsp->l_shm_dtime = bsp->shm_dtime;
    587 	lsp->l_shm_ctime = bsp->shm_ctime;
    588 }
    589 
    590 /*
    591  * shmctl.
    592  *
    593  * The usual structure conversion and massaging is done.
    594  */
    595 int
    596 linux_sys_shmctl(struct lwp *l, const struct linux_sys_shmctl_args *uap, register_t *retval)
    597 {
    598 	/* {
    599 		syscallarg(int) shmid;
    600 		syscallarg(int) cmd;
    601 		syscallarg(struct linux_shmid_ds *) buf;
    602 	} */
    603 	struct shmid_ds bs;
    604 	struct ipc_perm perm;
    605 	struct linux_shmid_ds ls;
    606 	struct linux_shmid64_ds ls64;
    607 	struct linux_shminfo64 lsi64;
    608 	struct linux_shm_info lsi;
    609 	int error, i, cmd, shmid;
    610 
    611 	shmid = SCARG(uap, shmid);
    612 	cmd = SCARG(uap, cmd);
    613 #ifdef LINUX_IPC_FORCE64
    614 	cmd |= LINUX_IPC_64;
    615 #endif
    616 
    617 	switch (cmd & ~LINUX_IPC_64) {
    618 	case LINUX_SHM_STAT:
    619 		error = shm_find_segment_perm_by_index(shmid, &perm);
    620 		if (error)
    621 			return error;
    622 		shmid = IXSEQ_TO_IPCID(shmid, perm);
    623 		retval[0] = shmid;
    624 		/*FALLTHROUGH*/
    625 
    626 	case LINUX_IPC_STAT:
    627 		error = shmctl1(l, shmid, IPC_STAT, &bs);
    628 		if (error != 0)
    629 			return error;
    630 		if (cmd & LINUX_IPC_64) {
    631 			bsd_to_linux_shmid64_ds(&bs, &ls64);
    632 			error = copyout(&ls64, SCARG(uap, buf), sizeof ls64);
    633 		} else {
    634 			bsd_to_linux_shmid_ds(&bs, &ls);
    635 			error = copyout(&ls, SCARG(uap, buf), sizeof ls);
    636 		}
    637 		return error;
    638 
    639 	case LINUX_IPC_SET:
    640 		if (cmd & LINUX_IPC_64) {
    641 			error = copyin(SCARG(uap, buf), &ls64, sizeof ls64);
    642 			linux_to_bsd_shmid64_ds(&ls64, &bs);
    643 		} else {
    644 			error = copyin(SCARG(uap, buf), &ls, sizeof ls);
    645 			linux_to_bsd_shmid_ds(&ls, &bs);
    646 		}
    647 		if (error != 0)
    648 			return error;
    649 		return shmctl1(l, shmid, IPC_SET, &bs);
    650 
    651 	case LINUX_IPC_RMID:
    652 		return shmctl1(l, shmid, IPC_RMID, NULL);
    653 
    654 	case LINUX_SHM_LOCK:
    655 		return shmctl1(l, shmid, SHM_LOCK, NULL);
    656 
    657 	case LINUX_SHM_UNLOCK:
    658 		return shmctl1(l, shmid, SHM_UNLOCK, NULL);
    659 
    660 	case LINUX_IPC_INFO:
    661 		memset(&lsi64, 0, sizeof lsi64);
    662 		lsi64.l_shmmax = shminfo.shmmax;
    663 		lsi64.l_shmmin = shminfo.shmmin;
    664 		lsi64.l_shmmni = shminfo.shmmni;
    665 		lsi64.l_shmseg = shminfo.shmseg;
    666 		lsi64.l_shmall = shminfo.shmall;
    667 		for (i = shminfo.shmmni - 1; i > 0; i--)
    668 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    669 				break;
    670 		retval[0] = i;
    671 		return copyout(&lsi64, SCARG(uap, buf), sizeof lsi64);
    672 
    673 	case LINUX_SHM_INFO:
    674 		(void)memset(&lsi, 0, sizeof lsi);
    675 		lsi.l_used_ids = shm_nused;
    676 		for (i = 0; i < shminfo.shmmni; i++)
    677 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    678 				lsi.l_shm_tot +=
    679 				    round_page(shmsegs[i].shm_segsz) /
    680 				    uvmexp.pagesize;
    681 		lsi.l_shm_rss = 0;
    682 		lsi.l_shm_swp = 0;
    683 		lsi.l_swap_attempts = 0;
    684 		lsi.l_swap_successes = 0;
    685 		for (i = shminfo.shmmni - 1; i > 0; i--)
    686 			if (shmsegs[i].shm_perm.mode & SHMSEG_ALLOCATED)
    687 				break;
    688 		retval[0] = i;
    689 		return copyout(&lsi, SCARG(uap, buf), sizeof lsi);
    690 
    691 	default:
    692 #ifdef DEBUG
    693 		printf("linux_sys_shmctl cmd %d\n", SCARG(uap, cmd));
    694 #endif
    695 		return EINVAL;
    696 	}
    697 }
    698 #endif /* SYSVSHM */
    699