2 * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause
4 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Adam Glass and Charles
18 * 4. The names of the authors may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * $NetBSD: sysv_shm.c,v 1.39 1997/10/07 10:02:03 drochner Exp $
35 * Copyright (c) 2003-2005 McAfee, Inc.
36 * Copyright (c) 2016-2017 Robert N. M. Watson
37 * All rights reserved.
39 * This software was developed for the FreeBSD Project in part by McAfee
40 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
41 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
44 * Portions of this software were developed by BAE Systems, the University of
45 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
46 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
47 * Computing (TC) research program.
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 #include <sys/cdefs.h>
72 #include "opt_sysvipc.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/abi_compat.h>
77 #include <sys/kernel.h>
78 #include <sys/limits.h>
80 #include <sys/sysctl.h>
83 #include <sys/malloc.h>
85 #include <sys/module.h>
86 #include <sys/mutex.h>
87 #include <sys/racct.h>
88 #include <sys/resourcevar.h>
89 #include <sys/rwlock.h>
91 #include <sys/syscall.h>
92 #include <sys/syscallsubr.h>
93 #include <sys/sysent.h>
94 #include <sys/sysproto.h>
97 #include <security/audit/audit.h>
98 #include <security/mac/mac_framework.h>
101 #include <vm/vm_param.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_map.h>
105 #include <vm/vm_page.h>
106 #include <vm/vm_pager.h>
108 FEATURE(sysv_shm, "System V shared memory segments support");
110 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
112 #define SHMSEG_FREE 0x0200
113 #define SHMSEG_REMOVED 0x0400
114 #define SHMSEG_ALLOCATED 0x0800
116 static int shm_last_free, shm_nused, shmalloced;
117 vm_size_t shm_committed;
118 static struct shmid_kernel *shmsegs;
119 static unsigned shm_prison_slot;
121 struct shmmap_state {
126 static void shm_deallocate_segment(struct shmid_kernel *);
127 static int shm_find_segment_by_key(struct prison *, key_t);
128 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
129 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
130 static int shmget_allocate_segment(struct thread *td, key_t key, size_t size,
132 static int shmget_existing(struct thread *td, size_t size, int shmflg,
133 int mode, int segnum);
134 static void shmrealloc(void);
135 static int shminit(void);
136 static int sysvshm_modload(struct module *, int, void *);
137 static int shmunload(void);
139 static void shmexit_myhook(struct vmspace *vm);
140 static void shmfork_myhook(struct proc *p1, struct proc *p2);
142 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
143 static void shm_remove(struct shmid_kernel *, int);
144 static struct prison *shm_find_prison(struct ucred *);
145 static int shm_prison_cansee(struct prison *, struct shmid_kernel *);
146 static int shm_prison_check(void *, void *);
147 static int shm_prison_set(void *, void *);
148 static int shm_prison_get(void *, void *);
149 static int shm_prison_remove(void *, void *);
150 static void shm_prison_cleanup(struct prison *);
156 #define SHMMAXPGS 131072ul /* Note: sysv shared memory is swap backed. */
159 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
171 #define SHMALL (SHMMAXPGS)
174 struct shminfo shminfo = {
182 static int shm_use_phys;
183 static int shm_allow_removed = 1;
185 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RWTUN, &shminfo.shmmax, 0,
186 "Maximum shared memory segment size");
187 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RWTUN, &shminfo.shmmin, 0,
188 "Minimum shared memory segment size");
189 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
190 "Number of shared memory identifiers");
191 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
192 "Number of segments per process");
193 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RWTUN, &shminfo.shmall, 0,
194 "Maximum number of pages available for shared memory");
195 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RWTUN,
196 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
197 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RWTUN,
198 &shm_allow_removed, 0,
199 "Enable/Disable attachment to attached segments marked for removal");
200 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
201 CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
202 "Array of struct shmid_kernel for each potential shared memory segment");
204 static struct sx sysvshmsx;
205 #define SYSVSHM_LOCK() sx_xlock(&sysvshmsx)
206 #define SYSVSHM_UNLOCK() sx_xunlock(&sysvshmsx)
207 #define SYSVSHM_ASSERT_LOCKED() sx_assert(&sysvshmsx, SA_XLOCKED)
210 shm_find_segment_by_key(struct prison *pr, key_t key)
214 for (i = 0; i < shmalloced; i++)
215 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
216 shmsegs[i].cred != NULL &&
217 shmsegs[i].cred->cr_prison == pr &&
218 shmsegs[i].u.shm_perm.key == key)
224 * Finds segment either by shmid if is_shmid is true, or by segnum if
227 static struct shmid_kernel *
228 shm_find_segment(struct prison *rpr, int arg, bool is_shmid)
230 struct shmid_kernel *shmseg;
233 segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
234 if (segnum < 0 || segnum >= shmalloced)
236 shmseg = &shmsegs[segnum];
237 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
238 (!shm_allow_removed &&
239 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
240 (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)) ||
241 shm_prison_cansee(rpr, shmseg) != 0)
247 shm_deallocate_segment(struct shmid_kernel *shmseg)
251 SYSVSHM_ASSERT_LOCKED();
253 vm_object_deallocate(shmseg->object);
254 shmseg->object = NULL;
255 size = round_page(shmseg->u.shm_segsz);
256 shm_committed -= btoc(size);
258 shmseg->u.shm_perm.mode = SHMSEG_FREE;
260 mac_sysvshm_cleanup(shmseg);
262 racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
263 racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
264 crfree(shmseg->cred);
269 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
271 struct shmid_kernel *shmseg;
275 SYSVSHM_ASSERT_LOCKED();
276 segnum = IPCID_TO_IX(shmmap_s->shmid);
277 KASSERT(segnum >= 0 && segnum < shmalloced,
278 ("segnum %d shmalloced %d", segnum, shmalloced));
280 shmseg = &shmsegs[segnum];
281 size = round_page(shmseg->u.shm_segsz);
282 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
283 if (result != KERN_SUCCESS)
285 shmmap_s->shmid = -1;
286 shmseg->u.shm_dtime = time_second;
287 if (--shmseg->u.shm_nattch == 0 &&
288 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
289 shm_deallocate_segment(shmseg);
290 shm_last_free = segnum;
296 shm_remove(struct shmid_kernel *shmseg, int segnum)
299 shmseg->u.shm_perm.key = IPC_PRIVATE;
300 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
301 if (shmseg->u.shm_nattch == 0) {
302 shm_deallocate_segment(shmseg);
303 shm_last_free = segnum;
307 static struct prison *
308 shm_find_prison(struct ucred *cred)
310 struct prison *pr, *rpr;
312 pr = cred->cr_prison;
314 rpr = osd_jail_get(pr, shm_prison_slot);
320 shm_prison_cansee(struct prison *rpr, struct shmid_kernel *shmseg)
323 if (shmseg->cred == NULL ||
324 !(rpr == shmseg->cred->cr_prison ||
325 prison_ischild(rpr, shmseg->cred->cr_prison)))
331 kern_shmdt_locked(struct thread *td, const void *shmaddr)
333 struct proc *p = td->td_proc;
334 struct shmmap_state *shmmap_s;
340 SYSVSHM_ASSERT_LOCKED();
341 if (shm_find_prison(td->td_ucred) == NULL)
343 shmmap_s = p->p_vmspace->vm_shm;
344 if (shmmap_s == NULL)
346 AUDIT_ARG_SVIPC_ID(shmmap_s->shmid);
347 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
348 if (shmmap_s->shmid != -1 &&
349 shmmap_s->va == (vm_offset_t)shmaddr) {
353 if (i == shminfo.shmseg)
356 error = mac_sysvshm_check_shmdt(td->td_ucred,
357 &shmsegs[IPCID_TO_IX(shmmap_s->shmid)]);
361 return (shm_delete_mapping(p->p_vmspace, shmmap_s));
364 #ifndef _SYS_SYSPROTO_H_
370 sys_shmdt(struct thread *td, struct shmdt_args *uap)
375 error = kern_shmdt_locked(td, uap->shmaddr);
381 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
385 struct proc *p = td->td_proc;
386 struct shmid_kernel *shmseg;
387 struct shmmap_state *shmmap_s;
388 vm_offset_t attach_va;
391 int cow, error, find_space, i, rv;
393 AUDIT_ARG_SVIPC_ID(shmid);
394 AUDIT_ARG_VALUE(shmflg);
396 SYSVSHM_ASSERT_LOCKED();
397 rpr = shm_find_prison(td->td_ucred);
400 shmmap_s = p->p_vmspace->vm_shm;
401 if (shmmap_s == NULL) {
402 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
404 for (i = 0; i < shminfo.shmseg; i++)
405 shmmap_s[i].shmid = -1;
406 KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
407 p->p_vmspace->vm_shm = shmmap_s;
409 shmseg = shm_find_segment(rpr, shmid, true);
412 error = ipcperm(td, &shmseg->u.shm_perm,
413 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
417 error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
421 for (i = 0; i < shminfo.shmseg; i++) {
422 if (shmmap_s->shmid == -1)
426 if (i >= shminfo.shmseg)
428 size = round_page(shmseg->u.shm_segsz);
430 cow = MAP_INHERIT_SHARE | MAP_PREFAULT_PARTIAL;
431 if ((shmflg & SHM_RDONLY) == 0)
432 prot |= VM_PROT_WRITE;
433 if (shmaddr != NULL) {
434 if ((shmflg & SHM_RND) != 0)
435 attach_va = rounddown2((vm_offset_t)shmaddr, SHMLBA);
436 else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0)
437 attach_va = (vm_offset_t)shmaddr;
440 if ((shmflg & SHM_REMAP) != 0)
442 find_space = VMFS_NO_SPACE;
445 * This is just a hint to vm_map_find() about where to
448 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
449 lim_max(td, RLIMIT_DATA));
450 find_space = VMFS_OPTIMAL_SPACE;
453 vm_object_reference(shmseg->object);
454 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object, 0, &attach_va,
455 size, 0, find_space, prot, prot, cow);
456 if (rv != KERN_SUCCESS) {
457 vm_object_deallocate(shmseg->object);
461 shmmap_s->va = attach_va;
462 shmmap_s->shmid = shmid;
463 shmseg->u.shm_lpid = p->p_pid;
464 shmseg->u.shm_atime = time_second;
465 shmseg->u.shm_nattch++;
466 td->td_retval[0] = attach_va;
471 kern_shmat(struct thread *td, int shmid, const void *shmaddr, int shmflg)
476 error = kern_shmat_locked(td, shmid, shmaddr, shmflg);
481 #ifndef _SYS_SYSPROTO_H_
489 sys_shmat(struct thread *td, struct shmat_args *uap)
492 return (kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg));
496 kern_shmctl_locked(struct thread *td, int shmid, int cmd, void *buf,
500 struct shmid_kernel *shmseg;
501 struct shmid_ds *shmidp;
502 struct shm_info shm_info;
505 SYSVSHM_ASSERT_LOCKED();
507 rpr = shm_find_prison(td->td_ucred);
511 AUDIT_ARG_SVIPC_ID(shmid);
512 AUDIT_ARG_SVIPC_CMD(cmd);
516 * It is possible that kern_shmctl is being called from the Linux ABI
517 * layer, in which case, we will need to implement IPC_INFO. It should
518 * be noted that other shmctl calls will be funneled through here for
519 * Linix binaries as well.
521 * NB: The Linux ABI layer will convert this data to structure(s) more
522 * consistent with the Linux ABI.
525 memcpy(buf, &shminfo, sizeof(shminfo));
527 *bufsz = sizeof(shminfo);
528 td->td_retval[0] = shmalloced;
531 shm_info.used_ids = shm_nused;
532 shm_info.shm_rss = 0; /*XXX where to get from ? */
533 shm_info.shm_tot = 0; /*XXX where to get from ? */
534 shm_info.shm_swp = 0; /*XXX where to get from ? */
535 shm_info.swap_attempts = 0; /*XXX where to get from ? */
536 shm_info.swap_successes = 0; /*XXX where to get from ? */
537 memcpy(buf, &shm_info, sizeof(shm_info));
539 *bufsz = sizeof(shm_info);
540 td->td_retval[0] = shmalloced;
544 shmseg = shm_find_segment(rpr, shmid, cmd != SHM_STAT);
548 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
555 shmidp = (struct shmid_ds *)buf;
556 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
559 memcpy(shmidp, &shmseg->u, sizeof(struct shmid_ds));
560 if (td->td_ucred->cr_prison != shmseg->cred->cr_prison)
561 shmidp->shm_perm.key = IPC_PRIVATE;
563 *bufsz = sizeof(struct shmid_ds);
564 if (cmd == SHM_STAT) {
565 td->td_retval[0] = IXSEQ_TO_IPCID(shmid,
570 shmidp = (struct shmid_ds *)buf;
571 AUDIT_ARG_SVIPC_PERM(&shmidp->shm_perm);
572 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
575 shmseg->u.shm_perm.uid = shmidp->shm_perm.uid;
576 shmseg->u.shm_perm.gid = shmidp->shm_perm.gid;
577 shmseg->u.shm_perm.mode =
578 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
579 (shmidp->shm_perm.mode & ACCESSPERMS);
580 shmseg->u.shm_ctime = time_second;
583 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
586 shm_remove(shmseg, IPCID_TO_IX(shmid));
600 kern_shmctl(struct thread *td, int shmid, int cmd, void *buf, size_t *bufsz)
605 error = kern_shmctl_locked(td, shmid, cmd, buf, bufsz);
610 #ifndef _SYS_SYSPROTO_H_
614 struct shmid_ds *buf;
618 sys_shmctl(struct thread *td, struct shmctl_args *uap)
625 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
626 * Linux binaries. If we see the call come through the FreeBSD ABI,
627 * return an error back to the user since we do not to support this.
629 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
630 uap->cmd == SHM_STAT)
633 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
634 if (uap->cmd == IPC_SET) {
635 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
639 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
643 /* Cases in which we need to copyout */
646 error = copyout(&buf, uap->buf, bufsz);
652 /* Invalidate the return value */
653 td->td_retval[0] = -1;
659 shmget_existing(struct thread *td, size_t size, int shmflg, int mode,
662 struct shmid_kernel *shmseg;
667 SYSVSHM_ASSERT_LOCKED();
668 KASSERT(segnum >= 0 && segnum < shmalloced,
669 ("segnum %d shmalloced %d", segnum, shmalloced));
670 shmseg = &shmsegs[segnum];
671 if ((shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
674 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, shmflg);
678 if (size != 0 && size > shmseg->u.shm_segsz)
680 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
685 shmget_allocate_segment(struct thread *td, key_t key, size_t size, int mode)
687 struct ucred *cred = td->td_ucred;
688 struct shmid_kernel *shmseg;
689 vm_object_t shm_object;
692 SYSVSHM_ASSERT_LOCKED();
694 if (size < shminfo.shmmin || size > shminfo.shmmax)
696 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
698 size = round_page(size);
699 if (shm_committed + btoc(size) > shminfo.shmall)
701 if (shm_last_free < 0) {
702 shmrealloc(); /* Maybe expand the shmsegs[] array. */
703 for (i = 0; i < shmalloced; i++)
704 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
710 segnum = shm_last_free;
713 KASSERT(segnum >= 0 && segnum < shmalloced,
714 ("segnum %d shmalloced %d", segnum, shmalloced));
715 shmseg = &shmsegs[segnum];
718 PROC_LOCK(td->td_proc);
719 if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
720 PROC_UNLOCK(td->td_proc);
723 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
724 racct_sub(td->td_proc, RACCT_NSHM, 1);
725 PROC_UNLOCK(td->td_proc);
728 PROC_UNLOCK(td->td_proc);
733 * We make sure that we have allocated a pager before we need
736 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
737 0, size, VM_PROT_DEFAULT, 0, cred);
738 if (shm_object == NULL) {
741 PROC_LOCK(td->td_proc);
742 racct_sub(td->td_proc, RACCT_NSHM, 1);
743 racct_sub(td->td_proc, RACCT_SHMSIZE, size);
744 PROC_UNLOCK(td->td_proc);
750 shmseg->object = shm_object;
751 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
752 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
753 shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
754 shmseg->u.shm_perm.key = key;
755 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
756 shmseg->cred = crhold(cred);
757 shmseg->u.shm_segsz = size;
758 shmseg->u.shm_cpid = td->td_proc->p_pid;
759 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
760 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
762 mac_sysvshm_create(cred, shmseg);
764 shmseg->u.shm_ctime = time_second;
765 shm_committed += btoc(size);
767 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
772 #ifndef _SYS_SYSPROTO_H_
780 sys_shmget(struct thread *td, struct shmget_args *uap)
785 if (shm_find_prison(td->td_ucred) == NULL)
787 mode = uap->shmflg & ACCESSPERMS;
789 if (uap->key == IPC_PRIVATE) {
790 error = shmget_allocate_segment(td, uap->key, uap->size, mode);
792 segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
795 error = shmget_existing(td, uap->size, uap->shmflg,
797 else if ((uap->shmflg & IPC_CREAT) == 0)
800 error = shmget_allocate_segment(td, uap->key,
809 shmfork(struct proc *p1, struct proc *p2)
812 shmfork_myhook(struct proc *p1, struct proc *p2)
815 struct shmmap_state *shmmap_s;
820 size = shminfo.shmseg * sizeof(struct shmmap_state);
821 shmmap_s = malloc(size, M_SHM, M_WAITOK);
822 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
823 p2->p_vmspace->vm_shm = shmmap_s;
824 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
825 if (shmmap_s->shmid != -1) {
826 KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
827 IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
828 ("segnum %d shmalloced %d",
829 IPCID_TO_IX(shmmap_s->shmid), shmalloced));
830 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
838 shmexit(struct vmspace *vm)
841 shmexit_myhook(struct vmspace *vm)
844 struct shmmap_state *base, *shm;
851 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
852 if (shm->shmid != -1)
853 shm_delete_mapping(vm, shm);
863 struct shmid_kernel *newsegs;
866 SYSVSHM_ASSERT_LOCKED();
868 if (shmalloced >= shminfo.shmmni)
871 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM,
873 for (i = 0; i < shmalloced; i++)
874 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
875 for (; i < shminfo.shmmni; i++) {
876 newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
877 newsegs[i].u.shm_perm.seq = 0;
879 mac_sysvshm_init(&newsegs[i]);
882 free(shmsegs, M_SHM);
884 shmalloced = shminfo.shmmni;
887 static struct syscall_helper_data shm_syscalls[] = {
888 SYSCALL_INIT_HELPER(shmat),
889 SYSCALL_INIT_HELPER(shmctl),
890 SYSCALL_INIT_HELPER(shmdt),
891 SYSCALL_INIT_HELPER(shmget),
892 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
893 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
894 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
896 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
897 SYSCALL_INIT_HELPER(shmsys),
902 #ifdef COMPAT_FREEBSD32
903 #include <compat/freebsd32/freebsd32.h>
904 #include <compat/freebsd32/freebsd32_ipc.h>
905 #include <compat/freebsd32/freebsd32_proto.h>
906 #include <compat/freebsd32/freebsd32_signal.h>
907 #include <compat/freebsd32/freebsd32_syscall.h>
908 #include <compat/freebsd32/freebsd32_util.h>
910 static struct syscall_helper_data shm32_syscalls[] = {
911 SYSCALL32_INIT_HELPER_COMPAT(shmat),
912 SYSCALL32_INIT_HELPER_COMPAT(shmdt),
913 SYSCALL32_INIT_HELPER_COMPAT(shmget),
914 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
915 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
916 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
917 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
918 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
930 osd_method_t methods[PR_MAXMETHOD] = {
931 [PR_METHOD_CHECK] = shm_prison_check,
932 [PR_METHOD_SET] = shm_prison_set,
933 [PR_METHOD_GET] = shm_prison_get,
934 [PR_METHOD_REMOVE] = shm_prison_remove,
938 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
939 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
941 if (shminfo.shmmax == SHMMAX) {
942 /* Initialize shmmax dealing with possible overflow. */
943 for (i = PAGE_SIZE; i != 0; i--) {
944 shminfo.shmmax = shminfo.shmall * i;
945 if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
949 shmalloced = shminfo.shmmni;
950 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM,
952 for (i = 0; i < shmalloced; i++) {
953 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
954 shmsegs[i].u.shm_perm.seq = 0;
956 mac_sysvshm_init(&shmsegs[i]);
962 sx_init(&sysvshmsx, "sysvshmsx");
964 shmexit_hook = &shmexit_myhook;
965 shmfork_hook = &shmfork_myhook;
968 /* Set current prisons according to their allow.sysvipc. */
969 shm_prison_slot = osd_jail_register(NULL, methods);
970 rsv = osd_reserve(shm_prison_slot);
971 prison_lock(&prison0);
972 (void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
973 prison_unlock(&prison0);
975 sx_slock(&allprison_lock);
976 TAILQ_FOREACH(pr, &allprison, pr_list) {
978 rsv = osd_reserve(shm_prison_slot);
980 if (pr->pr_allow & PR_ALLOW_SYSVIPC) {
981 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
988 osd_free_reserved(rsv);
989 sx_sunlock(&allprison_lock);
991 error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
994 #ifdef COMPAT_FREEBSD32
995 error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
1010 #ifdef COMPAT_FREEBSD32
1011 syscall32_helper_unregister(shm32_syscalls);
1013 syscall_helper_unregister(shm_syscalls);
1014 if (shm_prison_slot != 0)
1015 osd_jail_deregister(shm_prison_slot);
1017 for (i = 0; i < shmalloced; i++) {
1019 mac_sysvshm_destroy(&shmsegs[i]);
1022 * Objects might be still mapped into the processes
1023 * address spaces. Actual free would happen on the
1024 * last mapping destruction.
1026 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1027 vm_object_deallocate(shmsegs[i].object);
1029 free(shmsegs, M_SHM);
1031 shmexit_hook = NULL;
1032 shmfork_hook = NULL;
1034 sx_destroy(&sysvshmsx);
1039 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1041 struct shmid_kernel tshmseg;
1042 #ifdef COMPAT_FREEBSD32
1043 struct shmid_kernel32 tshmseg32;
1045 struct prison *pr, *rpr;
1051 pr = req->td->td_ucred->cr_prison;
1052 rpr = shm_find_prison(req->td->td_ucred);
1054 for (i = 0; i < shmalloced; i++) {
1055 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1056 rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1057 bzero(&tshmseg, sizeof(tshmseg));
1058 tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1060 tshmseg = shmsegs[i];
1061 if (tshmseg.cred->cr_prison != pr)
1062 tshmseg.u.shm_perm.key = IPC_PRIVATE;
1064 #ifdef COMPAT_FREEBSD32
1065 if (SV_CURPROC_FLAG(SV_ILP32)) {
1066 bzero(&tshmseg32, sizeof(tshmseg32));
1067 freebsd32_ipcperm_out(&tshmseg.u.shm_perm,
1068 &tshmseg32.u.shm_perm);
1069 CP(tshmseg, tshmseg32, u.shm_segsz);
1070 CP(tshmseg, tshmseg32, u.shm_lpid);
1071 CP(tshmseg, tshmseg32, u.shm_cpid);
1072 CP(tshmseg, tshmseg32, u.shm_nattch);
1073 CP(tshmseg, tshmseg32, u.shm_atime);
1074 CP(tshmseg, tshmseg32, u.shm_dtime);
1075 CP(tshmseg, tshmseg32, u.shm_ctime);
1076 /* Don't copy object, label, or cred */
1077 outaddr = &tshmseg32;
1078 outsize = sizeof(tshmseg32);
1082 tshmseg.object = NULL;
1083 tshmseg.label = NULL;
1084 tshmseg.cred = NULL;
1086 outsize = sizeof(tshmseg);
1088 error = SYSCTL_OUT(req, outaddr, outsize);
1097 shm_prison_check(void *obj, void *data)
1099 struct prison *pr = obj;
1100 struct prison *prpr;
1101 struct vfsoptlist *opts = data;
1105 * sysvshm is a jailsys integer.
1106 * It must be "disable" if the parent jail is disabled.
1108 error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1109 if (error != ENOENT) {
1113 case JAIL_SYS_DISABLE:
1116 case JAIL_SYS_INHERIT:
1117 prison_lock(pr->pr_parent);
1118 prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1119 prison_unlock(pr->pr_parent);
1132 shm_prison_set(void *obj, void *data)
1134 struct prison *pr = obj;
1135 struct prison *tpr, *orpr, *nrpr, *trpr;
1136 struct vfsoptlist *opts = data;
1141 * sysvshm controls which jail is the root of the associated segments
1142 * (this jail or same as the parent), or if the feature is available
1145 if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1146 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1148 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1151 if (jsys == JAIL_SYS_DISABLE) {
1153 orpr = osd_jail_get(pr, shm_prison_slot);
1155 osd_jail_del(pr, shm_prison_slot);
1159 shm_prison_cleanup(pr);
1160 /* Disable all child jails as well. */
1161 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1163 trpr = osd_jail_get(tpr, shm_prison_slot);
1165 osd_jail_del(tpr, shm_prison_slot);
1168 shm_prison_cleanup(tpr);
1175 } else if (jsys != -1) {
1176 if (jsys == JAIL_SYS_NEW)
1179 prison_lock(pr->pr_parent);
1180 nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1181 prison_unlock(pr->pr_parent);
1183 rsv = osd_reserve(shm_prison_slot);
1185 orpr = osd_jail_get(pr, shm_prison_slot);
1187 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1190 osd_free_reserved(rsv);
1194 shm_prison_cleanup(pr);
1196 /* Change child jails matching the old root, */
1197 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1199 trpr = osd_jail_get(tpr,
1202 (void)osd_jail_set(tpr,
1203 shm_prison_slot, nrpr);
1206 shm_prison_cleanup(tpr);
1220 shm_prison_get(void *obj, void *data)
1222 struct prison *pr = obj;
1224 struct vfsoptlist *opts = data;
1227 /* Set sysvshm based on the jail's root prison. */
1229 rpr = osd_jail_get(pr, shm_prison_slot);
1231 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1232 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1233 error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1234 if (error == ENOENT)
1240 shm_prison_remove(void *obj, void *data __unused)
1242 struct prison *pr = obj;
1247 rpr = osd_jail_get(pr, shm_prison_slot);
1250 shm_prison_cleanup(pr);
1256 shm_prison_cleanup(struct prison *pr)
1258 struct shmid_kernel *shmseg;
1261 /* Remove any segments that belong to this jail. */
1262 for (i = 0; i < shmalloced; i++) {
1263 shmseg = &shmsegs[i];
1264 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1265 shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1266 shm_remove(shmseg, i);
1271 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1273 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1275 struct ipc_perm_old shm_perm; /* operation perms */
1276 int shm_segsz; /* size of segment (bytes) */
1277 u_short shm_cpid; /* pid, creator */
1278 u_short shm_lpid; /* pid, last operation */
1279 short shm_nattch; /* no. of current attaches */
1280 time_t shm_atime; /* last attach time */
1281 time_t shm_dtime; /* last detach time */
1282 time_t shm_ctime; /* last change time */
1283 void *shm_handle; /* internal handle for shm segment */
1286 struct oshmctl_args {
1289 struct oshmid_ds *ubuf;
1293 oshmctl(struct thread *td, struct oshmctl_args *uap)
1298 struct shmid_kernel *shmseg;
1299 struct oshmid_ds outbuf;
1301 rpr = shm_find_prison(td->td_ucred);
1304 if (uap->cmd != IPC_STAT) {
1305 return (freebsd7_shmctl(td,
1306 (struct freebsd7_shmctl_args *)uap));
1309 shmseg = shm_find_segment(rpr, uap->shmid, true);
1310 if (shmseg == NULL) {
1314 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1320 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1326 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1327 outbuf.shm_segsz = shmseg->u.shm_segsz;
1328 outbuf.shm_cpid = shmseg->u.shm_cpid;
1329 outbuf.shm_lpid = shmseg->u.shm_lpid;
1330 outbuf.shm_nattch = shmseg->u.shm_nattch;
1331 outbuf.shm_atime = shmseg->u.shm_atime;
1332 outbuf.shm_dtime = shmseg->u.shm_dtime;
1333 outbuf.shm_ctime = shmseg->u.shm_ctime;
1334 outbuf.shm_handle = shmseg->object;
1336 return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1342 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1343 static sy_call_t *shmcalls[] = {
1344 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1345 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1346 (sy_call_t *)freebsd7_shmctl
1349 #ifndef _SYS_SYSPROTO_H_
1350 /* XXX actually varargs. */
1351 struct shmsys_args {
1359 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1362 AUDIT_ARG_SVIPC_WHICH(uap->which);
1363 if (uap->which < 0 || uap->which >= nitems(shmcalls))
1365 return ((*shmcalls[uap->which])(td, &uap->a2));
1368 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1370 #ifdef COMPAT_FREEBSD32
1373 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1376 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1377 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1378 AUDIT_ARG_SVIPC_WHICH(uap->which);
1379 switch (uap->which) {
1380 case 0: { /* shmat */
1381 struct shmat_args ap;
1384 ap.shmaddr = PTRIN(uap->a3);
1385 ap.shmflg = uap->a4;
1386 return (sysent[SYS_shmat].sy_call(td, &ap));
1388 case 2: { /* shmdt */
1389 struct shmdt_args ap;
1391 ap.shmaddr = PTRIN(uap->a2);
1392 return (sysent[SYS_shmdt].sy_call(td, &ap));
1394 case 3: { /* shmget */
1395 struct shmget_args ap;
1399 ap.shmflg = uap->a4;
1400 return (sysent[SYS_shmget].sy_call(td, &ap));
1402 case 4: { /* shmctl */
1403 struct freebsd7_freebsd32_shmctl_args ap;
1407 ap.buf = PTRIN(uap->a4);
1408 return (freebsd7_freebsd32_shmctl(td, &ap));
1410 case 1: /* oshmctl */
1415 return (nosys(td, NULL));
1419 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1420 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1422 freebsd7_freebsd32_shmctl(struct thread *td,
1423 struct freebsd7_freebsd32_shmctl_args *uap)
1427 struct shmid_ds shmid_ds;
1428 struct shm_info shm_info;
1429 struct shminfo shminfo;
1432 struct shmid_ds32_old shmid_ds32;
1433 struct shm_info32 shm_info32;
1434 struct shminfo32 shminfo32;
1438 if (uap->cmd == IPC_SET) {
1439 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1440 sizeof(u32.shmid_ds32))))
1442 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1443 &u.shmid_ds.shm_perm);
1444 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1445 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1446 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1447 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1448 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1449 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1450 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1453 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1457 /* Cases in which we need to copyout */
1460 CP(u.shminfo, u32.shminfo32, shmmax);
1461 CP(u.shminfo, u32.shminfo32, shmmin);
1462 CP(u.shminfo, u32.shminfo32, shmmni);
1463 CP(u.shminfo, u32.shminfo32, shmseg);
1464 CP(u.shminfo, u32.shminfo32, shmall);
1465 error = copyout(&u32.shminfo32, uap->buf,
1466 sizeof(u32.shminfo32));
1469 CP(u.shm_info, u32.shm_info32, used_ids);
1470 CP(u.shm_info, u32.shm_info32, shm_rss);
1471 CP(u.shm_info, u32.shm_info32, shm_tot);
1472 CP(u.shm_info, u32.shm_info32, shm_swp);
1473 CP(u.shm_info, u32.shm_info32, swap_attempts);
1474 CP(u.shm_info, u32.shm_info32, swap_successes);
1475 error = copyout(&u32.shm_info32, uap->buf,
1476 sizeof(u32.shm_info32));
1480 memset(&u32.shmid_ds32, 0, sizeof(u32.shmid_ds32));
1481 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1482 &u32.shmid_ds32.shm_perm);
1483 if (u.shmid_ds.shm_segsz > INT32_MAX)
1484 u32.shmid_ds32.shm_segsz = INT32_MAX;
1486 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1487 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1488 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1489 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1490 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1491 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1492 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1493 u32.shmid_ds32.shm_internal = 0;
1494 error = copyout(&u32.shmid_ds32, uap->buf,
1495 sizeof(u32.shmid_ds32));
1501 /* Invalidate the return value */
1502 td->td_retval[0] = -1;
1509 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1513 struct shmid_ds shmid_ds;
1514 struct shm_info shm_info;
1515 struct shminfo shminfo;
1518 struct shmid_ds32 shmid_ds32;
1519 struct shm_info32 shm_info32;
1520 struct shminfo32 shminfo32;
1524 if (uap->cmd == IPC_SET) {
1525 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1526 sizeof(u32.shmid_ds32))))
1528 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1529 &u.shmid_ds.shm_perm);
1530 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1531 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1532 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1533 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1534 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1535 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1536 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1539 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1543 /* Cases in which we need to copyout */
1546 CP(u.shminfo, u32.shminfo32, shmmax);
1547 CP(u.shminfo, u32.shminfo32, shmmin);
1548 CP(u.shminfo, u32.shminfo32, shmmni);
1549 CP(u.shminfo, u32.shminfo32, shmseg);
1550 CP(u.shminfo, u32.shminfo32, shmall);
1551 error = copyout(&u32.shminfo32, uap->buf,
1552 sizeof(u32.shminfo32));
1555 CP(u.shm_info, u32.shm_info32, used_ids);
1556 CP(u.shm_info, u32.shm_info32, shm_rss);
1557 CP(u.shm_info, u32.shm_info32, shm_tot);
1558 CP(u.shm_info, u32.shm_info32, shm_swp);
1559 CP(u.shm_info, u32.shm_info32, swap_attempts);
1560 CP(u.shm_info, u32.shm_info32, swap_successes);
1561 error = copyout(&u32.shm_info32, uap->buf,
1562 sizeof(u32.shm_info32));
1566 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1567 &u32.shmid_ds32.shm_perm);
1568 if (u.shmid_ds.shm_segsz > INT32_MAX)
1569 u32.shmid_ds32.shm_segsz = INT32_MAX;
1571 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1572 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1573 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1574 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1575 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1576 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1577 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1578 error = copyout(&u32.shmid_ds32, uap->buf,
1579 sizeof(u32.shmid_ds32));
1585 /* Invalidate the return value */
1586 td->td_retval[0] = -1;
1592 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1593 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1595 #ifndef _SYS_SYSPROTO_H_
1596 struct freebsd7_shmctl_args {
1599 struct shmid_ds_old *buf;
1603 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1606 struct shmid_ds_old old;
1607 struct shmid_ds buf;
1611 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1612 * Linux binaries. If we see the call come through the FreeBSD ABI,
1613 * return an error back to the user since we do not to support this.
1615 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1616 uap->cmd == SHM_STAT)
1619 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1620 if (uap->cmd == IPC_SET) {
1621 if ((error = copyin(uap->buf, &old, sizeof(old))))
1623 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1624 CP(old, buf, shm_segsz);
1625 CP(old, buf, shm_lpid);
1626 CP(old, buf, shm_cpid);
1627 CP(old, buf, shm_nattch);
1628 CP(old, buf, shm_atime);
1629 CP(old, buf, shm_dtime);
1630 CP(old, buf, shm_ctime);
1633 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1637 /* Cases in which we need to copyout */
1640 memset(&old, 0, sizeof(old));
1641 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1642 if (buf.shm_segsz > INT_MAX)
1643 old.shm_segsz = INT_MAX;
1645 CP(buf, old, shm_segsz);
1646 CP(buf, old, shm_lpid);
1647 CP(buf, old, shm_cpid);
1648 if (buf.shm_nattch > SHRT_MAX)
1649 old.shm_nattch = SHRT_MAX;
1651 CP(buf, old, shm_nattch);
1652 CP(buf, old, shm_atime);
1653 CP(buf, old, shm_dtime);
1654 CP(buf, old, shm_ctime);
1655 old.shm_internal = NULL;
1656 error = copyout(&old, uap->buf, sizeof(old));
1662 /* Invalidate the return value */
1663 td->td_retval[0] = -1;
1668 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1672 sysvshm_modload(struct module *module, int cmd, void *arg)
1683 error = shmunload();
1694 static moduledata_t sysvshm_mod = {
1700 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1701 MODULE_VERSION(sysvshm, 1);