2 * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
74 #include "opt_sysvipc.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/limits.h>
81 #include <sys/sysctl.h>
84 #include <sys/malloc.h>
86 #include <sys/module.h>
87 #include <sys/mutex.h>
88 #include <sys/racct.h>
89 #include <sys/resourcevar.h>
90 #include <sys/rwlock.h>
92 #include <sys/syscall.h>
93 #include <sys/syscallsubr.h>
94 #include <sys/sysent.h>
95 #include <sys/sysproto.h>
98 #include <security/audit/audit.h>
99 #include <security/mac/mac_framework.h>
102 #include <vm/vm_param.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_map.h>
106 #include <vm/vm_page.h>
107 #include <vm/vm_pager.h>
109 FEATURE(sysv_shm, "System V shared memory segments support");
111 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
113 static int shmget_allocate_segment(struct thread *td,
114 struct shmget_args *uap, int mode);
115 static int shmget_existing(struct thread *td, struct shmget_args *uap,
116 int mode, int segnum);
118 #define SHMSEG_FREE 0x0200
119 #define SHMSEG_REMOVED 0x0400
120 #define SHMSEG_ALLOCATED 0x0800
122 static int shm_last_free, shm_nused, shmalloced;
123 vm_size_t shm_committed;
124 static struct shmid_kernel *shmsegs;
125 static unsigned shm_prison_slot;
127 struct shmmap_state {
132 static void shm_deallocate_segment(struct shmid_kernel *);
133 static int shm_find_segment_by_key(struct prison *, key_t);
134 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
135 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
136 static void shmrealloc(void);
137 static int shminit(void);
138 static int sysvshm_modload(struct module *, int, void *);
139 static int shmunload(void);
141 static void shmexit_myhook(struct vmspace *vm);
142 static void shmfork_myhook(struct proc *p1, struct proc *p2);
144 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
145 static void shm_remove(struct shmid_kernel *, int);
146 static struct prison *shm_find_prison(struct ucred *);
147 static int shm_prison_cansee(struct prison *, struct shmid_kernel *);
148 static int shm_prison_check(void *, void *);
149 static int shm_prison_set(void *, void *);
150 static int shm_prison_get(void *, void *);
151 static int shm_prison_remove(void *, void *);
152 static void shm_prison_cleanup(struct prison *);
158 #define SHMMAXPGS 131072 /* Note: sysv shared memory is swap backed. */
161 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
173 #define SHMALL (SHMMAXPGS)
176 struct shminfo shminfo = {
184 static int shm_use_phys;
185 static int shm_allow_removed = 1;
187 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RWTUN, &shminfo.shmmax, 0,
188 "Maximum shared memory segment size");
189 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RWTUN, &shminfo.shmmin, 0,
190 "Minimum shared memory segment size");
191 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
192 "Number of shared memory identifiers");
193 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
194 "Number of segments per process");
195 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RWTUN, &shminfo.shmall, 0,
196 "Maximum number of pages available for shared memory");
197 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RWTUN,
198 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
199 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RWTUN,
200 &shm_allow_removed, 0,
201 "Enable/Disable attachment to attached segments marked for removal");
202 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
203 CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
204 "Array of struct shmid_kernel for each potential shared memory segment");
206 static struct sx sysvshmsx;
207 #define SYSVSHM_LOCK() sx_xlock(&sysvshmsx)
208 #define SYSVSHM_UNLOCK() sx_xunlock(&sysvshmsx)
209 #define SYSVSHM_ASSERT_LOCKED() sx_assert(&sysvshmsx, SA_XLOCKED)
212 shm_find_segment_by_key(struct prison *pr, key_t key)
216 for (i = 0; i < shmalloced; i++)
217 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
218 shmsegs[i].cred != NULL &&
219 shmsegs[i].cred->cr_prison == pr &&
220 shmsegs[i].u.shm_perm.key == key)
226 * Finds segment either by shmid if is_shmid is true, or by segnum if
229 static struct shmid_kernel *
230 shm_find_segment(struct prison *rpr, int arg, bool is_shmid)
232 struct shmid_kernel *shmseg;
235 segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
236 if (segnum < 0 || segnum >= shmalloced)
238 shmseg = &shmsegs[segnum];
239 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
240 (!shm_allow_removed &&
241 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
242 (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)) ||
243 shm_prison_cansee(rpr, shmseg) != 0)
249 shm_deallocate_segment(struct shmid_kernel *shmseg)
253 SYSVSHM_ASSERT_LOCKED();
255 vm_object_deallocate(shmseg->object);
256 shmseg->object = NULL;
257 size = round_page(shmseg->u.shm_segsz);
258 shm_committed -= btoc(size);
260 shmseg->u.shm_perm.mode = SHMSEG_FREE;
262 mac_sysvshm_cleanup(shmseg);
264 racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
265 racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
266 crfree(shmseg->cred);
271 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
273 struct shmid_kernel *shmseg;
277 SYSVSHM_ASSERT_LOCKED();
278 segnum = IPCID_TO_IX(shmmap_s->shmid);
279 KASSERT(segnum >= 0 && segnum < shmalloced,
280 ("segnum %d shmalloced %d", segnum, shmalloced));
282 shmseg = &shmsegs[segnum];
283 size = round_page(shmseg->u.shm_segsz);
284 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
285 if (result != KERN_SUCCESS)
287 shmmap_s->shmid = -1;
288 shmseg->u.shm_dtime = time_second;
289 if (--shmseg->u.shm_nattch == 0 &&
290 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
291 shm_deallocate_segment(shmseg);
292 shm_last_free = segnum;
298 shm_remove(struct shmid_kernel *shmseg, int segnum)
301 shmseg->u.shm_perm.key = IPC_PRIVATE;
302 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
303 if (shmseg->u.shm_nattch == 0) {
304 shm_deallocate_segment(shmseg);
305 shm_last_free = segnum;
309 static struct prison *
310 shm_find_prison(struct ucred *cred)
312 struct prison *pr, *rpr;
314 pr = cred->cr_prison;
316 rpr = osd_jail_get(pr, shm_prison_slot);
322 shm_prison_cansee(struct prison *rpr, struct shmid_kernel *shmseg)
325 if (shmseg->cred == NULL ||
326 !(rpr == shmseg->cred->cr_prison ||
327 prison_ischild(rpr, shmseg->cred->cr_prison)))
333 kern_shmdt_locked(struct thread *td, const void *shmaddr)
335 struct proc *p = td->td_proc;
336 struct shmmap_state *shmmap_s;
342 SYSVSHM_ASSERT_LOCKED();
343 if (shm_find_prison(td->td_ucred) == NULL)
345 shmmap_s = p->p_vmspace->vm_shm;
346 if (shmmap_s == NULL)
348 AUDIT_ARG_SVIPC_ID(shmmap_s->shmid);
349 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
350 if (shmmap_s->shmid != -1 &&
351 shmmap_s->va == (vm_offset_t)shmaddr) {
355 if (i == shminfo.shmseg)
358 error = mac_sysvshm_check_shmdt(td->td_ucred,
359 &shmsegs[IPCID_TO_IX(shmmap_s->shmid)]);
363 return (shm_delete_mapping(p->p_vmspace, shmmap_s));
366 #ifndef _SYS_SYSPROTO_H_
372 sys_shmdt(struct thread *td, struct shmdt_args *uap)
377 error = kern_shmdt_locked(td, uap->shmaddr);
383 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
387 struct proc *p = td->td_proc;
388 struct shmid_kernel *shmseg;
389 struct shmmap_state *shmmap_s;
390 vm_offset_t attach_va;
393 int cow, error, find_space, i, rv;
395 AUDIT_ARG_SVIPC_ID(shmid);
396 AUDIT_ARG_VALUE(shmflg);
398 SYSVSHM_ASSERT_LOCKED();
399 rpr = shm_find_prison(td->td_ucred);
402 shmmap_s = p->p_vmspace->vm_shm;
403 if (shmmap_s == NULL) {
404 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
406 for (i = 0; i < shminfo.shmseg; i++)
407 shmmap_s[i].shmid = -1;
408 KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
409 p->p_vmspace->vm_shm = shmmap_s;
411 shmseg = shm_find_segment(rpr, shmid, true);
414 error = ipcperm(td, &shmseg->u.shm_perm,
415 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
419 error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
423 for (i = 0; i < shminfo.shmseg; i++) {
424 if (shmmap_s->shmid == -1)
428 if (i >= shminfo.shmseg)
430 size = round_page(shmseg->u.shm_segsz);
432 cow = MAP_INHERIT_SHARE | MAP_PREFAULT_PARTIAL;
433 if ((shmflg & SHM_RDONLY) == 0)
434 prot |= VM_PROT_WRITE;
435 if (shmaddr != NULL) {
436 if ((shmflg & SHM_RND) != 0)
437 attach_va = rounddown2((vm_offset_t)shmaddr, SHMLBA);
438 else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0)
439 attach_va = (vm_offset_t)shmaddr;
442 if ((shmflg & SHM_REMAP) != 0)
444 find_space = VMFS_NO_SPACE;
447 * This is just a hint to vm_map_find() about where to
450 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
451 lim_max(td, RLIMIT_DATA));
452 find_space = VMFS_OPTIMAL_SPACE;
455 vm_object_reference(shmseg->object);
456 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object, 0, &attach_va,
457 size, 0, find_space, prot, prot, cow);
458 if (rv != KERN_SUCCESS) {
459 vm_object_deallocate(shmseg->object);
463 shmmap_s->va = attach_va;
464 shmmap_s->shmid = shmid;
465 shmseg->u.shm_lpid = p->p_pid;
466 shmseg->u.shm_atime = time_second;
467 shmseg->u.shm_nattch++;
468 td->td_retval[0] = attach_va;
473 kern_shmat(struct thread *td, int shmid, const void *shmaddr, int shmflg)
478 error = kern_shmat_locked(td, shmid, shmaddr, shmflg);
483 #ifndef _SYS_SYSPROTO_H_
491 sys_shmat(struct thread *td, struct shmat_args *uap)
494 return (kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg));
498 kern_shmctl_locked(struct thread *td, int shmid, int cmd, void *buf,
502 struct shmid_kernel *shmseg;
503 struct shmid_ds *shmidp;
504 struct shm_info shm_info;
507 SYSVSHM_ASSERT_LOCKED();
509 rpr = shm_find_prison(td->td_ucred);
513 AUDIT_ARG_SVIPC_ID(shmid);
514 AUDIT_ARG_SVIPC_CMD(cmd);
518 * It is possible that kern_shmctl is being called from the Linux ABI
519 * layer, in which case, we will need to implement IPC_INFO. It should
520 * be noted that other shmctl calls will be funneled through here for
521 * Linix binaries as well.
523 * NB: The Linux ABI layer will convert this data to structure(s) more
524 * consistent with the Linux ABI.
527 memcpy(buf, &shminfo, sizeof(shminfo));
529 *bufsz = sizeof(shminfo);
530 td->td_retval[0] = shmalloced;
533 shm_info.used_ids = shm_nused;
534 shm_info.shm_rss = 0; /*XXX where to get from ? */
535 shm_info.shm_tot = 0; /*XXX where to get from ? */
536 shm_info.shm_swp = 0; /*XXX where to get from ? */
537 shm_info.swap_attempts = 0; /*XXX where to get from ? */
538 shm_info.swap_successes = 0; /*XXX where to get from ? */
539 memcpy(buf, &shm_info, sizeof(shm_info));
541 *bufsz = sizeof(shm_info);
542 td->td_retval[0] = shmalloced;
546 shmseg = shm_find_segment(rpr, shmid, cmd != SHM_STAT);
550 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
557 shmidp = (struct shmid_ds *)buf;
558 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
561 memcpy(shmidp, &shmseg->u, sizeof(struct shmid_ds));
562 if (td->td_ucred->cr_prison != shmseg->cred->cr_prison)
563 shmidp->shm_perm.key = IPC_PRIVATE;
565 *bufsz = sizeof(struct shmid_ds);
566 if (cmd == SHM_STAT) {
567 td->td_retval[0] = IXSEQ_TO_IPCID(shmid,
572 shmidp = (struct shmid_ds *)buf;
573 AUDIT_ARG_SVIPC_PERM(&shmidp->shm_perm);
574 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
577 shmseg->u.shm_perm.uid = shmidp->shm_perm.uid;
578 shmseg->u.shm_perm.gid = shmidp->shm_perm.gid;
579 shmseg->u.shm_perm.mode =
580 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
581 (shmidp->shm_perm.mode & ACCESSPERMS);
582 shmseg->u.shm_ctime = time_second;
585 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
588 shm_remove(shmseg, IPCID_TO_IX(shmid));
602 kern_shmctl(struct thread *td, int shmid, int cmd, void *buf, size_t *bufsz)
607 error = kern_shmctl_locked(td, shmid, cmd, buf, bufsz);
613 #ifndef _SYS_SYSPROTO_H_
617 struct shmid_ds *buf;
621 sys_shmctl(struct thread *td, struct shmctl_args *uap)
628 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
629 * Linux binaries. If we see the call come through the FreeBSD ABI,
630 * return an error back to the user since we do not to support this.
632 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
633 uap->cmd == SHM_STAT)
636 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
637 if (uap->cmd == IPC_SET) {
638 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
642 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
646 /* Cases in which we need to copyout */
649 error = copyout(&buf, uap->buf, bufsz);
655 /* Invalidate the return value */
656 td->td_retval[0] = -1;
663 shmget_existing(struct thread *td, struct shmget_args *uap, int mode,
666 struct shmid_kernel *shmseg;
671 SYSVSHM_ASSERT_LOCKED();
672 KASSERT(segnum >= 0 && segnum < shmalloced,
673 ("segnum %d shmalloced %d", segnum, shmalloced));
674 shmseg = &shmsegs[segnum];
675 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
678 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
682 if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
684 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
689 shmget_allocate_segment(struct thread *td, struct shmget_args *uap, int mode)
691 struct ucred *cred = td->td_ucred;
692 struct shmid_kernel *shmseg;
693 vm_object_t shm_object;
697 SYSVSHM_ASSERT_LOCKED();
699 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
701 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
703 size = round_page(uap->size);
704 if (shm_committed + btoc(size) > shminfo.shmall)
706 if (shm_last_free < 0) {
707 shmrealloc(); /* Maybe expand the shmsegs[] array. */
708 for (i = 0; i < shmalloced; i++)
709 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
715 segnum = shm_last_free;
718 KASSERT(segnum >= 0 && segnum < shmalloced,
719 ("segnum %d shmalloced %d", segnum, shmalloced));
720 shmseg = &shmsegs[segnum];
723 PROC_LOCK(td->td_proc);
724 if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
725 PROC_UNLOCK(td->td_proc);
728 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
729 racct_sub(td->td_proc, RACCT_NSHM, 1);
730 PROC_UNLOCK(td->td_proc);
733 PROC_UNLOCK(td->td_proc);
738 * We make sure that we have allocated a pager before we need
741 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
742 0, size, VM_PROT_DEFAULT, 0, cred);
743 if (shm_object == NULL) {
746 PROC_LOCK(td->td_proc);
747 racct_sub(td->td_proc, RACCT_NSHM, 1);
748 racct_sub(td->td_proc, RACCT_SHMSIZE, size);
749 PROC_UNLOCK(td->td_proc);
754 shm_object->pg_color = 0;
755 VM_OBJECT_WLOCK(shm_object);
756 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
757 vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT);
758 VM_OBJECT_WUNLOCK(shm_object);
760 shmseg->object = shm_object;
761 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
762 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
763 shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
764 shmseg->u.shm_perm.key = uap->key;
765 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
766 shmseg->cred = crhold(cred);
767 shmseg->u.shm_segsz = uap->size;
768 shmseg->u.shm_cpid = td->td_proc->p_pid;
769 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
770 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
772 mac_sysvshm_create(cred, shmseg);
774 shmseg->u.shm_ctime = time_second;
775 shm_committed += btoc(size);
777 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
782 #ifndef _SYS_SYSPROTO_H_
790 sys_shmget(struct thread *td, struct shmget_args *uap)
795 if (shm_find_prison(td->td_ucred) == NULL)
797 mode = uap->shmflg & ACCESSPERMS;
799 if (uap->key == IPC_PRIVATE) {
800 error = shmget_allocate_segment(td, uap, mode);
802 segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
805 error = shmget_existing(td, uap, mode, segnum);
806 else if ((uap->shmflg & IPC_CREAT) == 0)
809 error = shmget_allocate_segment(td, uap, mode);
817 shmfork(struct proc *p1, struct proc *p2)
820 shmfork_myhook(struct proc *p1, struct proc *p2)
823 struct shmmap_state *shmmap_s;
828 size = shminfo.shmseg * sizeof(struct shmmap_state);
829 shmmap_s = malloc(size, M_SHM, M_WAITOK);
830 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
831 p2->p_vmspace->vm_shm = shmmap_s;
832 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
833 if (shmmap_s->shmid != -1) {
834 KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
835 IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
836 ("segnum %d shmalloced %d",
837 IPCID_TO_IX(shmmap_s->shmid), shmalloced));
838 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
846 shmexit(struct vmspace *vm)
849 shmexit_myhook(struct vmspace *vm)
852 struct shmmap_state *base, *shm;
859 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
860 if (shm->shmid != -1)
861 shm_delete_mapping(vm, shm);
871 struct shmid_kernel *newsegs;
874 SYSVSHM_ASSERT_LOCKED();
876 if (shmalloced >= shminfo.shmmni)
879 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM,
881 for (i = 0; i < shmalloced; i++)
882 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
883 for (; i < shminfo.shmmni; i++) {
884 newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
885 newsegs[i].u.shm_perm.seq = 0;
887 mac_sysvshm_init(&newsegs[i]);
890 free(shmsegs, M_SHM);
892 shmalloced = shminfo.shmmni;
895 static struct syscall_helper_data shm_syscalls[] = {
896 SYSCALL_INIT_HELPER(shmat),
897 SYSCALL_INIT_HELPER(shmctl),
898 SYSCALL_INIT_HELPER(shmdt),
899 SYSCALL_INIT_HELPER(shmget),
900 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
901 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
902 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
904 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
905 SYSCALL_INIT_HELPER(shmsys),
910 #ifdef COMPAT_FREEBSD32
911 #include <compat/freebsd32/freebsd32.h>
912 #include <compat/freebsd32/freebsd32_ipc.h>
913 #include <compat/freebsd32/freebsd32_proto.h>
914 #include <compat/freebsd32/freebsd32_signal.h>
915 #include <compat/freebsd32/freebsd32_syscall.h>
916 #include <compat/freebsd32/freebsd32_util.h>
918 static struct syscall_helper_data shm32_syscalls[] = {
919 SYSCALL32_INIT_HELPER_COMPAT(shmat),
920 SYSCALL32_INIT_HELPER_COMPAT(shmdt),
921 SYSCALL32_INIT_HELPER_COMPAT(shmget),
922 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
923 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
924 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
925 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
926 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
938 osd_method_t methods[PR_MAXMETHOD] = {
939 [PR_METHOD_CHECK] = shm_prison_check,
940 [PR_METHOD_SET] = shm_prison_set,
941 [PR_METHOD_GET] = shm_prison_get,
942 [PR_METHOD_REMOVE] = shm_prison_remove,
946 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
947 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
949 if (shminfo.shmmax == SHMMAX) {
950 /* Initialize shmmax dealing with possible overflow. */
951 for (i = PAGE_SIZE; i != 0; i--) {
952 shminfo.shmmax = shminfo.shmall * i;
953 if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
957 shmalloced = shminfo.shmmni;
958 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM,
960 for (i = 0; i < shmalloced; i++) {
961 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
962 shmsegs[i].u.shm_perm.seq = 0;
964 mac_sysvshm_init(&shmsegs[i]);
970 sx_init(&sysvshmsx, "sysvshmsx");
972 shmexit_hook = &shmexit_myhook;
973 shmfork_hook = &shmfork_myhook;
976 /* Set current prisons according to their allow.sysvipc. */
977 shm_prison_slot = osd_jail_register(NULL, methods);
978 rsv = osd_reserve(shm_prison_slot);
979 prison_lock(&prison0);
980 (void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
981 prison_unlock(&prison0);
983 sx_slock(&allprison_lock);
984 TAILQ_FOREACH(pr, &allprison, pr_list) {
986 rsv = osd_reserve(shm_prison_slot);
988 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
989 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
996 osd_free_reserved(rsv);
997 sx_sunlock(&allprison_lock);
999 error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
1002 #ifdef COMPAT_FREEBSD32
1003 error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
1018 #ifdef COMPAT_FREEBSD32
1019 syscall32_helper_unregister(shm32_syscalls);
1021 syscall_helper_unregister(shm_syscalls);
1022 if (shm_prison_slot != 0)
1023 osd_jail_deregister(shm_prison_slot);
1025 for (i = 0; i < shmalloced; i++) {
1027 mac_sysvshm_destroy(&shmsegs[i]);
1030 * Objects might be still mapped into the processes
1031 * address spaces. Actual free would happen on the
1032 * last mapping destruction.
1034 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1035 vm_object_deallocate(shmsegs[i].object);
1037 free(shmsegs, M_SHM);
1039 shmexit_hook = NULL;
1040 shmfork_hook = NULL;
1042 sx_destroy(&sysvshmsx);
1047 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1049 struct shmid_kernel tshmseg;
1050 #ifdef COMPAT_FREEBSD32
1051 struct shmid_kernel32 tshmseg32;
1053 struct prison *pr, *rpr;
1059 pr = req->td->td_ucred->cr_prison;
1060 rpr = shm_find_prison(req->td->td_ucred);
1062 for (i = 0; i < shmalloced; i++) {
1063 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1064 rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1065 bzero(&tshmseg, sizeof(tshmseg));
1066 tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1068 tshmseg = shmsegs[i];
1069 if (tshmseg.cred->cr_prison != pr)
1070 tshmseg.u.shm_perm.key = IPC_PRIVATE;
1072 #ifdef COMPAT_FREEBSD32
1073 if (SV_CURPROC_FLAG(SV_ILP32)) {
1074 bzero(&tshmseg32, sizeof(tshmseg32));
1075 freebsd32_ipcperm_out(&tshmseg.u.shm_perm,
1076 &tshmseg32.u.shm_perm);
1077 CP(tshmseg, tshmseg32, u.shm_segsz);
1078 CP(tshmseg, tshmseg32, u.shm_lpid);
1079 CP(tshmseg, tshmseg32, u.shm_cpid);
1080 CP(tshmseg, tshmseg32, u.shm_nattch);
1081 CP(tshmseg, tshmseg32, u.shm_atime);
1082 CP(tshmseg, tshmseg32, u.shm_dtime);
1083 CP(tshmseg, tshmseg32, u.shm_ctime);
1084 /* Don't copy object, label, or cred */
1085 outaddr = &tshmseg32;
1086 outsize = sizeof(tshmseg32);
1090 tshmseg.object = NULL;
1091 tshmseg.label = NULL;
1092 tshmseg.cred = NULL;
1094 outsize = sizeof(tshmseg);
1096 error = SYSCTL_OUT(req, outaddr, outsize);
1105 shm_prison_check(void *obj, void *data)
1107 struct prison *pr = obj;
1108 struct prison *prpr;
1109 struct vfsoptlist *opts = data;
1113 * sysvshm is a jailsys integer.
1114 * It must be "disable" if the parent jail is disabled.
1116 error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1117 if (error != ENOENT) {
1121 case JAIL_SYS_DISABLE:
1124 case JAIL_SYS_INHERIT:
1125 prison_lock(pr->pr_parent);
1126 prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1127 prison_unlock(pr->pr_parent);
1140 shm_prison_set(void *obj, void *data)
1142 struct prison *pr = obj;
1143 struct prison *tpr, *orpr, *nrpr, *trpr;
1144 struct vfsoptlist *opts = data;
1149 * sysvshm controls which jail is the root of the associated segments
1150 * (this jail or same as the parent), or if the feature is available
1153 if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1154 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1156 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1159 if (jsys == JAIL_SYS_DISABLE) {
1161 orpr = osd_jail_get(pr, shm_prison_slot);
1163 osd_jail_del(pr, shm_prison_slot);
1167 shm_prison_cleanup(pr);
1168 /* Disable all child jails as well. */
1169 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1171 trpr = osd_jail_get(tpr, shm_prison_slot);
1173 osd_jail_del(tpr, shm_prison_slot);
1176 shm_prison_cleanup(tpr);
1183 } else if (jsys != -1) {
1184 if (jsys == JAIL_SYS_NEW)
1187 prison_lock(pr->pr_parent);
1188 nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1189 prison_unlock(pr->pr_parent);
1191 rsv = osd_reserve(shm_prison_slot);
1193 orpr = osd_jail_get(pr, shm_prison_slot);
1195 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1198 osd_free_reserved(rsv);
1202 shm_prison_cleanup(pr);
1204 /* Change child jails matching the old root, */
1205 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1207 trpr = osd_jail_get(tpr,
1210 (void)osd_jail_set(tpr,
1211 shm_prison_slot, nrpr);
1214 shm_prison_cleanup(tpr);
1228 shm_prison_get(void *obj, void *data)
1230 struct prison *pr = obj;
1232 struct vfsoptlist *opts = data;
1235 /* Set sysvshm based on the jail's root prison. */
1237 rpr = osd_jail_get(pr, shm_prison_slot);
1239 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1240 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1241 error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1242 if (error == ENOENT)
1248 shm_prison_remove(void *obj, void *data __unused)
1250 struct prison *pr = obj;
1255 rpr = osd_jail_get(pr, shm_prison_slot);
1258 shm_prison_cleanup(pr);
1264 shm_prison_cleanup(struct prison *pr)
1266 struct shmid_kernel *shmseg;
1269 /* Remove any segments that belong to this jail. */
1270 for (i = 0; i < shmalloced; i++) {
1271 shmseg = &shmsegs[i];
1272 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1273 shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1274 shm_remove(shmseg, i);
1279 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1281 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1283 struct ipc_perm_old shm_perm; /* operation perms */
1284 int shm_segsz; /* size of segment (bytes) */
1285 u_short shm_cpid; /* pid, creator */
1286 u_short shm_lpid; /* pid, last operation */
1287 short shm_nattch; /* no. of current attaches */
1288 time_t shm_atime; /* last attach time */
1289 time_t shm_dtime; /* last detach time */
1290 time_t shm_ctime; /* last change time */
1291 void *shm_handle; /* internal handle for shm segment */
1294 struct oshmctl_args {
1297 struct oshmid_ds *ubuf;
1301 oshmctl(struct thread *td, struct oshmctl_args *uap)
1306 struct shmid_kernel *shmseg;
1307 struct oshmid_ds outbuf;
1309 rpr = shm_find_prison(td->td_ucred);
1312 if (uap->cmd != IPC_STAT) {
1313 return (freebsd7_shmctl(td,
1314 (struct freebsd7_shmctl_args *)uap));
1317 shmseg = shm_find_segment(rpr, uap->shmid, true);
1318 if (shmseg == NULL) {
1322 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1328 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1334 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1335 outbuf.shm_segsz = shmseg->u.shm_segsz;
1336 outbuf.shm_cpid = shmseg->u.shm_cpid;
1337 outbuf.shm_lpid = shmseg->u.shm_lpid;
1338 outbuf.shm_nattch = shmseg->u.shm_nattch;
1339 outbuf.shm_atime = shmseg->u.shm_atime;
1340 outbuf.shm_dtime = shmseg->u.shm_dtime;
1341 outbuf.shm_ctime = shmseg->u.shm_ctime;
1342 outbuf.shm_handle = shmseg->object;
1344 return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1350 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1351 static sy_call_t *shmcalls[] = {
1352 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1353 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1354 (sy_call_t *)freebsd7_shmctl
1357 #ifndef _SYS_SYSPROTO_H_
1358 /* XXX actually varargs. */
1359 struct shmsys_args {
1367 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1370 AUDIT_ARG_SVIPC_WHICH(uap->which);
1371 if (uap->which < 0 || uap->which >= nitems(shmcalls))
1373 return ((*shmcalls[uap->which])(td, &uap->a2));
1376 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1378 #ifdef COMPAT_FREEBSD32
1381 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1384 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1385 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1386 AUDIT_ARG_SVIPC_WHICH(uap->which);
1387 switch (uap->which) {
1388 case 0: { /* shmat */
1389 struct shmat_args ap;
1392 ap.shmaddr = PTRIN(uap->a3);
1393 ap.shmflg = uap->a4;
1394 return (sysent[SYS_shmat].sy_call(td, &ap));
1396 case 2: { /* shmdt */
1397 struct shmdt_args ap;
1399 ap.shmaddr = PTRIN(uap->a2);
1400 return (sysent[SYS_shmdt].sy_call(td, &ap));
1402 case 3: { /* shmget */
1403 struct shmget_args ap;
1407 ap.shmflg = uap->a4;
1408 return (sysent[SYS_shmget].sy_call(td, &ap));
1410 case 4: { /* shmctl */
1411 struct freebsd7_freebsd32_shmctl_args ap;
1415 ap.buf = PTRIN(uap->a4);
1416 return (freebsd7_freebsd32_shmctl(td, &ap));
1418 case 1: /* oshmctl */
1423 return (nosys(td, NULL));
1427 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1428 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1430 freebsd7_freebsd32_shmctl(struct thread *td,
1431 struct freebsd7_freebsd32_shmctl_args *uap)
1435 struct shmid_ds shmid_ds;
1436 struct shm_info shm_info;
1437 struct shminfo shminfo;
1440 struct shmid_ds32_old shmid_ds32;
1441 struct shm_info32 shm_info32;
1442 struct shminfo32 shminfo32;
1446 if (uap->cmd == IPC_SET) {
1447 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1448 sizeof(u32.shmid_ds32))))
1450 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1451 &u.shmid_ds.shm_perm);
1452 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1453 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1454 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1455 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1456 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1457 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1458 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1461 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1465 /* Cases in which we need to copyout */
1468 CP(u.shminfo, u32.shminfo32, shmmax);
1469 CP(u.shminfo, u32.shminfo32, shmmin);
1470 CP(u.shminfo, u32.shminfo32, shmmni);
1471 CP(u.shminfo, u32.shminfo32, shmseg);
1472 CP(u.shminfo, u32.shminfo32, shmall);
1473 error = copyout(&u32.shminfo32, uap->buf,
1474 sizeof(u32.shminfo32));
1477 CP(u.shm_info, u32.shm_info32, used_ids);
1478 CP(u.shm_info, u32.shm_info32, shm_rss);
1479 CP(u.shm_info, u32.shm_info32, shm_tot);
1480 CP(u.shm_info, u32.shm_info32, shm_swp);
1481 CP(u.shm_info, u32.shm_info32, swap_attempts);
1482 CP(u.shm_info, u32.shm_info32, swap_successes);
1483 error = copyout(&u32.shm_info32, uap->buf,
1484 sizeof(u32.shm_info32));
1488 memset(&u32.shmid_ds32, 0, sizeof(u32.shmid_ds32));
1489 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1490 &u32.shmid_ds32.shm_perm);
1491 if (u.shmid_ds.shm_segsz > INT32_MAX)
1492 u32.shmid_ds32.shm_segsz = INT32_MAX;
1494 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1495 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1496 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1497 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1498 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1499 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1500 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1501 u32.shmid_ds32.shm_internal = 0;
1502 error = copyout(&u32.shmid_ds32, uap->buf,
1503 sizeof(u32.shmid_ds32));
1509 /* Invalidate the return value */
1510 td->td_retval[0] = -1;
1517 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1521 struct shmid_ds shmid_ds;
1522 struct shm_info shm_info;
1523 struct shminfo shminfo;
1526 struct shmid_ds32 shmid_ds32;
1527 struct shm_info32 shm_info32;
1528 struct shminfo32 shminfo32;
1532 if (uap->cmd == IPC_SET) {
1533 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1534 sizeof(u32.shmid_ds32))))
1536 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1537 &u.shmid_ds.shm_perm);
1538 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1539 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1540 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1541 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1542 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1543 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1544 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1547 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1551 /* Cases in which we need to copyout */
1554 CP(u.shminfo, u32.shminfo32, shmmax);
1555 CP(u.shminfo, u32.shminfo32, shmmin);
1556 CP(u.shminfo, u32.shminfo32, shmmni);
1557 CP(u.shminfo, u32.shminfo32, shmseg);
1558 CP(u.shminfo, u32.shminfo32, shmall);
1559 error = copyout(&u32.shminfo32, uap->buf,
1560 sizeof(u32.shminfo32));
1563 CP(u.shm_info, u32.shm_info32, used_ids);
1564 CP(u.shm_info, u32.shm_info32, shm_rss);
1565 CP(u.shm_info, u32.shm_info32, shm_tot);
1566 CP(u.shm_info, u32.shm_info32, shm_swp);
1567 CP(u.shm_info, u32.shm_info32, swap_attempts);
1568 CP(u.shm_info, u32.shm_info32, swap_successes);
1569 error = copyout(&u32.shm_info32, uap->buf,
1570 sizeof(u32.shm_info32));
1574 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1575 &u32.shmid_ds32.shm_perm);
1576 if (u.shmid_ds.shm_segsz > INT32_MAX)
1577 u32.shmid_ds32.shm_segsz = INT32_MAX;
1579 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1580 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1581 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1582 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1583 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1584 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1585 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1586 error = copyout(&u32.shmid_ds32, uap->buf,
1587 sizeof(u32.shmid_ds32));
1593 /* Invalidate the return value */
1594 td->td_retval[0] = -1;
1600 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1601 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1604 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1607 #ifndef _SYS_SYSPROTO_H_
1608 struct freebsd7_shmctl_args {
1611 struct shmid_ds_old *buf;
1615 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1618 struct shmid_ds_old old;
1619 struct shmid_ds buf;
1623 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1624 * Linux binaries. If we see the call come through the FreeBSD ABI,
1625 * return an error back to the user since we do not to support this.
1627 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1628 uap->cmd == SHM_STAT)
1631 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1632 if (uap->cmd == IPC_SET) {
1633 if ((error = copyin(uap->buf, &old, sizeof(old))))
1635 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1636 CP(old, buf, shm_segsz);
1637 CP(old, buf, shm_lpid);
1638 CP(old, buf, shm_cpid);
1639 CP(old, buf, shm_nattch);
1640 CP(old, buf, shm_atime);
1641 CP(old, buf, shm_dtime);
1642 CP(old, buf, shm_ctime);
1645 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1649 /* Cases in which we need to copyout */
1652 memset(&old, 0, sizeof(old));
1653 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1654 if (buf.shm_segsz > INT_MAX)
1655 old.shm_segsz = INT_MAX;
1657 CP(buf, old, shm_segsz);
1658 CP(buf, old, shm_lpid);
1659 CP(buf, old, shm_cpid);
1660 if (buf.shm_nattch > SHRT_MAX)
1661 old.shm_nattch = SHRT_MAX;
1663 CP(buf, old, shm_nattch);
1664 CP(buf, old, shm_atime);
1665 CP(buf, old, shm_dtime);
1666 CP(buf, old, shm_ctime);
1667 old.shm_internal = NULL;
1668 error = copyout(&old, uap->buf, sizeof(old));
1674 /* Invalidate the return value */
1675 td->td_retval[0] = -1;
1680 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1684 sysvshm_modload(struct module *module, int cmd, void *arg)
1695 error = shmunload();
1706 static moduledata_t sysvshm_mod = {
1712 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1713 MODULE_VERSION(sysvshm, 1);