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_compat.h"
75 #include "opt_sysvipc.h"
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/limits.h>
82 #include <sys/sysctl.h>
85 #include <sys/malloc.h>
87 #include <sys/module.h>
88 #include <sys/mutex.h>
89 #include <sys/racct.h>
90 #include <sys/resourcevar.h>
91 #include <sys/rwlock.h>
93 #include <sys/syscall.h>
94 #include <sys/syscallsubr.h>
95 #include <sys/sysent.h>
96 #include <sys/sysproto.h>
99 #include <security/audit/audit.h>
100 #include <security/mac/mac_framework.h>
103 #include <vm/vm_param.h>
105 #include <vm/vm_object.h>
106 #include <vm/vm_map.h>
107 #include <vm/vm_page.h>
108 #include <vm/vm_pager.h>
110 FEATURE(sysv_shm, "System V shared memory segments support");
112 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
114 static int shmget_allocate_segment(struct thread *td,
115 struct shmget_args *uap, int mode);
116 static int shmget_existing(struct thread *td, struct shmget_args *uap,
117 int mode, int segnum);
119 #define SHMSEG_FREE 0x0200
120 #define SHMSEG_REMOVED 0x0400
121 #define SHMSEG_ALLOCATED 0x0800
123 static int shm_last_free, shm_nused, shmalloced;
124 vm_size_t shm_committed;
125 static struct shmid_kernel *shmsegs;
126 static unsigned shm_prison_slot;
128 struct shmmap_state {
133 static void shm_deallocate_segment(struct shmid_kernel *);
134 static int shm_find_segment_by_key(struct prison *, key_t);
135 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
136 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
137 static void shmrealloc(void);
138 static int shminit(void);
139 static int sysvshm_modload(struct module *, int, void *);
140 static int shmunload(void);
141 static void shmexit_myhook(struct vmspace *vm);
142 static void shmfork_myhook(struct proc *p1, struct proc *p2);
143 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
144 static void shm_remove(struct shmid_kernel *, int);
145 static struct prison *shm_find_prison(struct ucred *);
146 static int shm_prison_cansee(struct prison *, struct shmid_kernel *);
147 static int shm_prison_check(void *, void *);
148 static int shm_prison_set(void *, void *);
149 static int shm_prison_get(void *, void *);
150 static int shm_prison_remove(void *, void *);
151 static void shm_prison_cleanup(struct prison *);
157 #define SHMMAXPGS 131072 /* Note: sysv shared memory is swap backed. */
160 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
172 #define SHMALL (SHMMAXPGS)
175 struct shminfo shminfo = {
183 static int shm_use_phys;
184 static int shm_allow_removed = 1;
186 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RWTUN, &shminfo.shmmax, 0,
187 "Maximum shared memory segment size");
188 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RWTUN, &shminfo.shmmin, 0,
189 "Minimum shared memory segment size");
190 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
191 "Number of shared memory identifiers");
192 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
193 "Number of segments per process");
194 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RWTUN, &shminfo.shmall, 0,
195 "Maximum number of pages available for shared memory");
196 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RWTUN,
197 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
198 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RWTUN,
199 &shm_allow_removed, 0,
200 "Enable/Disable attachment to attached segments marked for removal");
201 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
202 CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
203 "Array of struct shmid_kernel for each potential shared memory segment");
205 static struct sx sysvshmsx;
206 #define SYSVSHM_LOCK() sx_xlock(&sysvshmsx)
207 #define SYSVSHM_UNLOCK() sx_xunlock(&sysvshmsx)
208 #define SYSVSHM_ASSERT_LOCKED() sx_assert(&sysvshmsx, SA_XLOCKED)
211 shm_find_segment_by_key(struct prison *pr, key_t key)
215 for (i = 0; i < shmalloced; i++)
216 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
217 shmsegs[i].cred != NULL &&
218 shmsegs[i].cred->cr_prison == pr &&
219 shmsegs[i].u.shm_perm.key == key)
225 * Finds segment either by shmid if is_shmid is true, or by segnum if
228 static struct shmid_kernel *
229 shm_find_segment(struct prison *rpr, int arg, bool is_shmid)
231 struct shmid_kernel *shmseg;
234 segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
235 if (segnum < 0 || segnum >= shmalloced)
237 shmseg = &shmsegs[segnum];
238 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
239 (!shm_allow_removed &&
240 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
241 (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)) ||
242 shm_prison_cansee(rpr, shmseg) != 0)
248 shm_deallocate_segment(struct shmid_kernel *shmseg)
252 SYSVSHM_ASSERT_LOCKED();
254 vm_object_deallocate(shmseg->object);
255 shmseg->object = NULL;
256 size = round_page(shmseg->u.shm_segsz);
257 shm_committed -= btoc(size);
259 shmseg->u.shm_perm.mode = SHMSEG_FREE;
261 mac_sysvshm_cleanup(shmseg);
263 racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
264 racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
265 crfree(shmseg->cred);
270 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
272 struct shmid_kernel *shmseg;
276 SYSVSHM_ASSERT_LOCKED();
277 segnum = IPCID_TO_IX(shmmap_s->shmid);
278 KASSERT(segnum >= 0 && segnum < shmalloced,
279 ("segnum %d shmalloced %d", segnum, shmalloced));
281 shmseg = &shmsegs[segnum];
282 size = round_page(shmseg->u.shm_segsz);
283 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
284 if (result != KERN_SUCCESS)
286 shmmap_s->shmid = -1;
287 shmseg->u.shm_dtime = time_second;
288 if (--shmseg->u.shm_nattch == 0 &&
289 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
290 shm_deallocate_segment(shmseg);
291 shm_last_free = segnum;
297 shm_remove(struct shmid_kernel *shmseg, int segnum)
300 shmseg->u.shm_perm.key = IPC_PRIVATE;
301 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
302 if (shmseg->u.shm_nattch == 0) {
303 shm_deallocate_segment(shmseg);
304 shm_last_free = segnum;
308 static struct prison *
309 shm_find_prison(struct ucred *cred)
311 struct prison *pr, *rpr;
313 pr = cred->cr_prison;
315 rpr = osd_jail_get(pr, shm_prison_slot);
321 shm_prison_cansee(struct prison *rpr, struct shmid_kernel *shmseg)
324 if (shmseg->cred == NULL ||
325 !(rpr == shmseg->cred->cr_prison ||
326 prison_ischild(rpr, shmseg->cred->cr_prison)))
332 kern_shmdt_locked(struct thread *td, const void *shmaddr)
334 struct proc *p = td->td_proc;
335 struct shmmap_state *shmmap_s;
336 #if defined(AUDIT) || defined(MAC)
337 struct shmid_kernel *shmsegptr;
344 SYSVSHM_ASSERT_LOCKED();
345 if (shm_find_prison(td->td_ucred) == NULL)
347 shmmap_s = p->p_vmspace->vm_shm;
348 if (shmmap_s == NULL)
350 AUDIT_ARG_SVIPC_ID(shmmap_s->shmid);
351 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
352 if (shmmap_s->shmid != -1 &&
353 shmmap_s->va == (vm_offset_t)shmaddr) {
357 if (i == shminfo.shmseg)
359 #if (defined(AUDIT) && defined(KDTRACE_HOOKS)) || defined(MAC)
360 shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
363 error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
367 return (shm_delete_mapping(p->p_vmspace, shmmap_s));
370 #ifndef _SYS_SYSPROTO_H_
376 sys_shmdt(struct thread *td, struct shmdt_args *uap)
381 error = kern_shmdt_locked(td, uap->shmaddr);
387 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
391 struct proc *p = td->td_proc;
392 struct shmid_kernel *shmseg;
393 struct shmmap_state *shmmap_s;
394 vm_offset_t attach_va;
399 AUDIT_ARG_SVIPC_ID(shmid);
400 AUDIT_ARG_VALUE(shmflg);
402 SYSVSHM_ASSERT_LOCKED();
403 rpr = shm_find_prison(td->td_ucred);
406 shmmap_s = p->p_vmspace->vm_shm;
407 if (shmmap_s == NULL) {
408 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
410 for (i = 0; i < shminfo.shmseg; i++)
411 shmmap_s[i].shmid = -1;
412 KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
413 p->p_vmspace->vm_shm = shmmap_s;
415 shmseg = shm_find_segment(rpr, shmid, true);
418 error = ipcperm(td, &shmseg->u.shm_perm,
419 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
423 error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
427 for (i = 0; i < shminfo.shmseg; i++) {
428 if (shmmap_s->shmid == -1)
432 if (i >= shminfo.shmseg)
434 size = round_page(shmseg->u.shm_segsz);
436 if ((shmflg & SHM_RDONLY) == 0)
437 prot |= VM_PROT_WRITE;
438 if (shmaddr != NULL) {
439 if ((shmflg & SHM_RND) != 0)
440 attach_va = rounddown2((vm_offset_t)shmaddr, SHMLBA);
441 else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0)
442 attach_va = (vm_offset_t)shmaddr;
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));
454 vm_object_reference(shmseg->object);
455 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object, 0, &attach_va,
456 size, 0, shmaddr != NULL ? VMFS_NO_SPACE : VMFS_OPTIMAL_SPACE,
457 prot, prot, MAP_INHERIT_SHARE | MAP_PREFAULT_PARTIAL);
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);
816 shmfork_myhook(struct proc *p1, struct proc *p2)
818 struct shmmap_state *shmmap_s;
823 size = shminfo.shmseg * sizeof(struct shmmap_state);
824 shmmap_s = malloc(size, M_SHM, M_WAITOK);
825 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
826 p2->p_vmspace->vm_shm = shmmap_s;
827 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
828 if (shmmap_s->shmid != -1) {
829 KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
830 IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
831 ("segnum %d shmalloced %d",
832 IPCID_TO_IX(shmmap_s->shmid), shmalloced));
833 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
840 shmexit_myhook(struct vmspace *vm)
842 struct shmmap_state *base, *shm;
849 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
850 if (shm->shmid != -1)
851 shm_delete_mapping(vm, shm);
861 struct shmid_kernel *newsegs;
864 SYSVSHM_ASSERT_LOCKED();
866 if (shmalloced >= shminfo.shmmni)
869 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM,
871 for (i = 0; i < shmalloced; i++)
872 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
873 for (; i < shminfo.shmmni; i++) {
874 newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
875 newsegs[i].u.shm_perm.seq = 0;
877 mac_sysvshm_init(&newsegs[i]);
880 free(shmsegs, M_SHM);
882 shmalloced = shminfo.shmmni;
885 static struct syscall_helper_data shm_syscalls[] = {
886 SYSCALL_INIT_HELPER(shmat),
887 SYSCALL_INIT_HELPER(shmctl),
888 SYSCALL_INIT_HELPER(shmdt),
889 SYSCALL_INIT_HELPER(shmget),
890 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
891 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
892 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
894 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
895 SYSCALL_INIT_HELPER(shmsys),
900 #ifdef COMPAT_FREEBSD32
901 #include <compat/freebsd32/freebsd32.h>
902 #include <compat/freebsd32/freebsd32_ipc.h>
903 #include <compat/freebsd32/freebsd32_proto.h>
904 #include <compat/freebsd32/freebsd32_signal.h>
905 #include <compat/freebsd32/freebsd32_syscall.h>
906 #include <compat/freebsd32/freebsd32_util.h>
908 static struct syscall_helper_data shm32_syscalls[] = {
909 SYSCALL32_INIT_HELPER_COMPAT(shmat),
910 SYSCALL32_INIT_HELPER_COMPAT(shmdt),
911 SYSCALL32_INIT_HELPER_COMPAT(shmget),
912 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
913 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
914 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
915 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
916 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
928 osd_method_t methods[PR_MAXMETHOD] = {
929 [PR_METHOD_CHECK] = shm_prison_check,
930 [PR_METHOD_SET] = shm_prison_set,
931 [PR_METHOD_GET] = shm_prison_get,
932 [PR_METHOD_REMOVE] = shm_prison_remove,
936 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
937 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
939 if (shminfo.shmmax == SHMMAX) {
940 /* Initialize shmmax dealing with possible overflow. */
941 for (i = PAGE_SIZE; i != 0; i--) {
942 shminfo.shmmax = shminfo.shmall * i;
943 if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
947 shmalloced = shminfo.shmmni;
948 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM,
950 for (i = 0; i < shmalloced; i++) {
951 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
952 shmsegs[i].u.shm_perm.seq = 0;
954 mac_sysvshm_init(&shmsegs[i]);
960 sx_init(&sysvshmsx, "sysvshmsx");
961 shmexit_hook = &shmexit_myhook;
962 shmfork_hook = &shmfork_myhook;
964 /* Set current prisons according to their allow.sysvipc. */
965 shm_prison_slot = osd_jail_register(NULL, methods);
966 rsv = osd_reserve(shm_prison_slot);
967 prison_lock(&prison0);
968 (void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
969 prison_unlock(&prison0);
971 sx_slock(&allprison_lock);
972 TAILQ_FOREACH(pr, &allprison, pr_list) {
974 rsv = osd_reserve(shm_prison_slot);
976 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
977 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
984 osd_free_reserved(rsv);
985 sx_sunlock(&allprison_lock);
987 error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
990 #ifdef COMPAT_FREEBSD32
991 error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
1006 #ifdef COMPAT_FREEBSD32
1007 syscall32_helper_unregister(shm32_syscalls);
1009 syscall_helper_unregister(shm_syscalls);
1010 if (shm_prison_slot != 0)
1011 osd_jail_deregister(shm_prison_slot);
1013 for (i = 0; i < shmalloced; i++) {
1015 mac_sysvshm_destroy(&shmsegs[i]);
1018 * Objects might be still mapped into the processes
1019 * address spaces. Actual free would happen on the
1020 * last mapping destruction.
1022 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1023 vm_object_deallocate(shmsegs[i].object);
1025 free(shmsegs, M_SHM);
1026 shmexit_hook = NULL;
1027 shmfork_hook = NULL;
1028 sx_destroy(&sysvshmsx);
1033 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1035 struct shmid_kernel tshmseg;
1036 #ifdef COMPAT_FREEBSD32
1037 struct shmid_kernel32 tshmseg32;
1039 struct prison *pr, *rpr;
1045 pr = req->td->td_ucred->cr_prison;
1046 rpr = shm_find_prison(req->td->td_ucred);
1048 for (i = 0; i < shmalloced; i++) {
1049 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1050 rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1051 bzero(&tshmseg, sizeof(tshmseg));
1052 tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1054 tshmseg = shmsegs[i];
1055 if (tshmseg.cred->cr_prison != pr)
1056 tshmseg.u.shm_perm.key = IPC_PRIVATE;
1058 #ifdef COMPAT_FREEBSD32
1059 if (SV_CURPROC_FLAG(SV_ILP32)) {
1060 bzero(&tshmseg32, sizeof(tshmseg32));
1061 freebsd32_ipcperm_out(&tshmseg.u.shm_perm,
1062 &tshmseg32.u.shm_perm);
1063 CP(tshmseg, tshmseg32, u.shm_segsz);
1064 CP(tshmseg, tshmseg32, u.shm_lpid);
1065 CP(tshmseg, tshmseg32, u.shm_cpid);
1066 CP(tshmseg, tshmseg32, u.shm_nattch);
1067 CP(tshmseg, tshmseg32, u.shm_atime);
1068 CP(tshmseg, tshmseg32, u.shm_dtime);
1069 CP(tshmseg, tshmseg32, u.shm_ctime);
1070 /* Don't copy object, label, or cred */
1071 outaddr = &tshmseg32;
1072 outsize = sizeof(tshmseg32);
1076 tshmseg.object = NULL;
1077 tshmseg.label = NULL;
1078 tshmseg.cred = NULL;
1080 outsize = sizeof(tshmseg);
1082 error = SYSCTL_OUT(req, outaddr, outsize);
1091 shm_prison_check(void *obj, void *data)
1093 struct prison *pr = obj;
1094 struct prison *prpr;
1095 struct vfsoptlist *opts = data;
1099 * sysvshm is a jailsys integer.
1100 * It must be "disable" if the parent jail is disabled.
1102 error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1103 if (error != ENOENT) {
1107 case JAIL_SYS_DISABLE:
1110 case JAIL_SYS_INHERIT:
1111 prison_lock(pr->pr_parent);
1112 prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1113 prison_unlock(pr->pr_parent);
1126 shm_prison_set(void *obj, void *data)
1128 struct prison *pr = obj;
1129 struct prison *tpr, *orpr, *nrpr, *trpr;
1130 struct vfsoptlist *opts = data;
1135 * sysvshm controls which jail is the root of the associated segments
1136 * (this jail or same as the parent), or if the feature is available
1139 if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1140 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1142 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1145 if (jsys == JAIL_SYS_DISABLE) {
1147 orpr = osd_jail_get(pr, shm_prison_slot);
1149 osd_jail_del(pr, shm_prison_slot);
1153 shm_prison_cleanup(pr);
1154 /* Disable all child jails as well. */
1155 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1157 trpr = osd_jail_get(tpr, shm_prison_slot);
1159 osd_jail_del(tpr, shm_prison_slot);
1162 shm_prison_cleanup(tpr);
1169 } else if (jsys != -1) {
1170 if (jsys == JAIL_SYS_NEW)
1173 prison_lock(pr->pr_parent);
1174 nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1175 prison_unlock(pr->pr_parent);
1177 rsv = osd_reserve(shm_prison_slot);
1179 orpr = osd_jail_get(pr, shm_prison_slot);
1181 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1184 osd_free_reserved(rsv);
1188 shm_prison_cleanup(pr);
1190 /* Change child jails matching the old root, */
1191 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1193 trpr = osd_jail_get(tpr,
1196 (void)osd_jail_set(tpr,
1197 shm_prison_slot, nrpr);
1200 shm_prison_cleanup(tpr);
1214 shm_prison_get(void *obj, void *data)
1216 struct prison *pr = obj;
1218 struct vfsoptlist *opts = data;
1221 /* Set sysvshm based on the jail's root prison. */
1223 rpr = osd_jail_get(pr, shm_prison_slot);
1225 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1226 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1227 error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1228 if (error == ENOENT)
1234 shm_prison_remove(void *obj, void *data __unused)
1236 struct prison *pr = obj;
1241 rpr = osd_jail_get(pr, shm_prison_slot);
1244 shm_prison_cleanup(pr);
1250 shm_prison_cleanup(struct prison *pr)
1252 struct shmid_kernel *shmseg;
1255 /* Remove any segments that belong to this jail. */
1256 for (i = 0; i < shmalloced; i++) {
1257 shmseg = &shmsegs[i];
1258 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1259 shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1260 shm_remove(shmseg, i);
1265 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1267 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1269 struct ipc_perm_old shm_perm; /* operation perms */
1270 int shm_segsz; /* size of segment (bytes) */
1271 u_short shm_cpid; /* pid, creator */
1272 u_short shm_lpid; /* pid, last operation */
1273 short shm_nattch; /* no. of current attaches */
1274 time_t shm_atime; /* last attach time */
1275 time_t shm_dtime; /* last detach time */
1276 time_t shm_ctime; /* last change time */
1277 void *shm_handle; /* internal handle for shm segment */
1280 struct oshmctl_args {
1283 struct oshmid_ds *ubuf;
1287 oshmctl(struct thread *td, struct oshmctl_args *uap)
1292 struct shmid_kernel *shmseg;
1293 struct oshmid_ds outbuf;
1295 rpr = shm_find_prison(td->td_ucred);
1298 if (uap->cmd != IPC_STAT) {
1299 return (freebsd7_shmctl(td,
1300 (struct freebsd7_shmctl_args *)uap));
1303 shmseg = shm_find_segment(rpr, uap->shmid, true);
1304 if (shmseg == NULL) {
1308 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1314 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1320 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1321 outbuf.shm_segsz = shmseg->u.shm_segsz;
1322 outbuf.shm_cpid = shmseg->u.shm_cpid;
1323 outbuf.shm_lpid = shmseg->u.shm_lpid;
1324 outbuf.shm_nattch = shmseg->u.shm_nattch;
1325 outbuf.shm_atime = shmseg->u.shm_atime;
1326 outbuf.shm_dtime = shmseg->u.shm_dtime;
1327 outbuf.shm_ctime = shmseg->u.shm_ctime;
1328 outbuf.shm_handle = shmseg->object;
1330 return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1336 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1337 static sy_call_t *shmcalls[] = {
1338 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1339 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1340 (sy_call_t *)freebsd7_shmctl
1343 #ifndef _SYS_SYSPROTO_H_
1344 /* XXX actually varargs. */
1345 struct shmsys_args {
1353 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1356 AUDIT_ARG_SVIPC_WHICH(uap->which);
1357 if (uap->which < 0 || uap->which >= nitems(shmcalls))
1359 return ((*shmcalls[uap->which])(td, &uap->a2));
1362 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1364 #ifdef COMPAT_FREEBSD32
1367 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1370 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1371 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1372 AUDIT_ARG_SVIPC_WHICH(uap->which);
1373 switch (uap->which) {
1374 case 0: { /* shmat */
1375 struct shmat_args ap;
1378 ap.shmaddr = PTRIN(uap->a3);
1379 ap.shmflg = uap->a4;
1380 return (sysent[SYS_shmat].sy_call(td, &ap));
1382 case 2: { /* shmdt */
1383 struct shmdt_args ap;
1385 ap.shmaddr = PTRIN(uap->a2);
1386 return (sysent[SYS_shmdt].sy_call(td, &ap));
1388 case 3: { /* shmget */
1389 struct shmget_args ap;
1393 ap.shmflg = uap->a4;
1394 return (sysent[SYS_shmget].sy_call(td, &ap));
1396 case 4: { /* shmctl */
1397 struct freebsd7_freebsd32_shmctl_args ap;
1401 ap.buf = PTRIN(uap->a4);
1402 return (freebsd7_freebsd32_shmctl(td, &ap));
1404 case 1: /* oshmctl */
1409 return (nosys(td, NULL));
1413 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1414 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1416 freebsd7_freebsd32_shmctl(struct thread *td,
1417 struct freebsd7_freebsd32_shmctl_args *uap)
1421 struct shmid_ds shmid_ds;
1422 struct shm_info shm_info;
1423 struct shminfo shminfo;
1426 struct shmid_ds32_old shmid_ds32;
1427 struct shm_info32 shm_info32;
1428 struct shminfo32 shminfo32;
1432 if (uap->cmd == IPC_SET) {
1433 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1434 sizeof(u32.shmid_ds32))))
1436 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1437 &u.shmid_ds.shm_perm);
1438 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1439 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1440 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1441 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1442 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1443 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1444 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1447 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1451 /* Cases in which we need to copyout */
1454 CP(u.shminfo, u32.shminfo32, shmmax);
1455 CP(u.shminfo, u32.shminfo32, shmmin);
1456 CP(u.shminfo, u32.shminfo32, shmmni);
1457 CP(u.shminfo, u32.shminfo32, shmseg);
1458 CP(u.shminfo, u32.shminfo32, shmall);
1459 error = copyout(&u32.shminfo32, uap->buf,
1460 sizeof(u32.shminfo32));
1463 CP(u.shm_info, u32.shm_info32, used_ids);
1464 CP(u.shm_info, u32.shm_info32, shm_rss);
1465 CP(u.shm_info, u32.shm_info32, shm_tot);
1466 CP(u.shm_info, u32.shm_info32, shm_swp);
1467 CP(u.shm_info, u32.shm_info32, swap_attempts);
1468 CP(u.shm_info, u32.shm_info32, swap_successes);
1469 error = copyout(&u32.shm_info32, uap->buf,
1470 sizeof(u32.shm_info32));
1474 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1475 &u32.shmid_ds32.shm_perm);
1476 if (u.shmid_ds.shm_segsz > INT32_MAX)
1477 u32.shmid_ds32.shm_segsz = INT32_MAX;
1479 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1480 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1481 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1482 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1483 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1484 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1485 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1486 u32.shmid_ds32.shm_internal = 0;
1487 error = copyout(&u32.shmid_ds32, uap->buf,
1488 sizeof(u32.shmid_ds32));
1494 /* Invalidate the return value */
1495 td->td_retval[0] = -1;
1502 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1506 struct shmid_ds shmid_ds;
1507 struct shm_info shm_info;
1508 struct shminfo shminfo;
1511 struct shmid_ds32 shmid_ds32;
1512 struct shm_info32 shm_info32;
1513 struct shminfo32 shminfo32;
1517 if (uap->cmd == IPC_SET) {
1518 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1519 sizeof(u32.shmid_ds32))))
1521 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1522 &u.shmid_ds.shm_perm);
1523 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1524 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1525 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1526 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1527 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1528 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1529 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1532 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1536 /* Cases in which we need to copyout */
1539 CP(u.shminfo, u32.shminfo32, shmmax);
1540 CP(u.shminfo, u32.shminfo32, shmmin);
1541 CP(u.shminfo, u32.shminfo32, shmmni);
1542 CP(u.shminfo, u32.shminfo32, shmseg);
1543 CP(u.shminfo, u32.shminfo32, shmall);
1544 error = copyout(&u32.shminfo32, uap->buf,
1545 sizeof(u32.shminfo32));
1548 CP(u.shm_info, u32.shm_info32, used_ids);
1549 CP(u.shm_info, u32.shm_info32, shm_rss);
1550 CP(u.shm_info, u32.shm_info32, shm_tot);
1551 CP(u.shm_info, u32.shm_info32, shm_swp);
1552 CP(u.shm_info, u32.shm_info32, swap_attempts);
1553 CP(u.shm_info, u32.shm_info32, swap_successes);
1554 error = copyout(&u32.shm_info32, uap->buf,
1555 sizeof(u32.shm_info32));
1559 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1560 &u32.shmid_ds32.shm_perm);
1561 if (u.shmid_ds.shm_segsz > INT32_MAX)
1562 u32.shmid_ds32.shm_segsz = INT32_MAX;
1564 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1565 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1566 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1567 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1568 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1569 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1570 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1571 error = copyout(&u32.shmid_ds32, uap->buf,
1572 sizeof(u32.shmid_ds32));
1578 /* Invalidate the return value */
1579 td->td_retval[0] = -1;
1585 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1586 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1589 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1592 #ifndef _SYS_SYSPROTO_H_
1593 struct freebsd7_shmctl_args {
1596 struct shmid_ds_old *buf;
1600 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1603 struct shmid_ds_old old;
1604 struct shmid_ds buf;
1608 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1609 * Linux binaries. If we see the call come through the FreeBSD ABI,
1610 * return an error back to the user since we do not to support this.
1612 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1613 uap->cmd == SHM_STAT)
1616 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1617 if (uap->cmd == IPC_SET) {
1618 if ((error = copyin(uap->buf, &old, sizeof(old))))
1620 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1621 CP(old, buf, shm_segsz);
1622 CP(old, buf, shm_lpid);
1623 CP(old, buf, shm_cpid);
1624 CP(old, buf, shm_nattch);
1625 CP(old, buf, shm_atime);
1626 CP(old, buf, shm_dtime);
1627 CP(old, buf, shm_ctime);
1630 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1634 /* Cases in which we need to copyout */
1637 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1638 if (buf.shm_segsz > INT_MAX)
1639 old.shm_segsz = INT_MAX;
1641 CP(buf, old, shm_segsz);
1642 CP(buf, old, shm_lpid);
1643 CP(buf, old, shm_cpid);
1644 if (buf.shm_nattch > SHRT_MAX)
1645 old.shm_nattch = SHRT_MAX;
1647 CP(buf, old, shm_nattch);
1648 CP(buf, old, shm_atime);
1649 CP(buf, old, shm_dtime);
1650 CP(buf, old, shm_ctime);
1651 old.shm_internal = NULL;
1652 error = copyout(&old, uap->buf, sizeof(old));
1658 /* Invalidate the return value */
1659 td->td_retval[0] = -1;
1664 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1668 sysvshm_modload(struct module *module, int cmd, void *arg)
1679 error = shmunload();
1690 static moduledata_t sysvshm_mod = {
1696 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1697 MODULE_VERSION(sysvshm, 1);