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