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);
140 static void shmexit_myhook(struct vmspace *vm);
141 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 131072 /* 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);
611 #ifndef _SYS_SYSPROTO_H_
615 struct shmid_ds *buf;
619 sys_shmctl(struct thread *td, struct shmctl_args *uap)
626 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
627 * Linux binaries. If we see the call come through the FreeBSD ABI,
628 * return an error back to the user since we do not to support this.
630 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
631 uap->cmd == SHM_STAT)
634 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
635 if (uap->cmd == IPC_SET) {
636 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
640 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
644 /* Cases in which we need to copyout */
647 error = copyout(&buf, uap->buf, bufsz);
653 /* Invalidate the return value */
654 td->td_retval[0] = -1;
661 shmget_existing(struct thread *td, struct shmget_args *uap, int mode,
664 struct shmid_kernel *shmseg;
669 SYSVSHM_ASSERT_LOCKED();
670 KASSERT(segnum >= 0 && segnum < shmalloced,
671 ("segnum %d shmalloced %d", segnum, shmalloced));
672 shmseg = &shmsegs[segnum];
673 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
676 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
680 if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
682 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
687 shmget_allocate_segment(struct thread *td, struct shmget_args *uap, int mode)
689 struct ucred *cred = td->td_ucred;
690 struct shmid_kernel *shmseg;
691 vm_object_t shm_object;
695 SYSVSHM_ASSERT_LOCKED();
697 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
699 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
701 size = round_page(uap->size);
702 if (shm_committed + btoc(size) > shminfo.shmall)
704 if (shm_last_free < 0) {
705 shmrealloc(); /* Maybe expand the shmsegs[] array. */
706 for (i = 0; i < shmalloced; i++)
707 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
713 segnum = shm_last_free;
716 KASSERT(segnum >= 0 && segnum < shmalloced,
717 ("segnum %d shmalloced %d", segnum, shmalloced));
718 shmseg = &shmsegs[segnum];
721 PROC_LOCK(td->td_proc);
722 if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
723 PROC_UNLOCK(td->td_proc);
726 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
727 racct_sub(td->td_proc, RACCT_NSHM, 1);
728 PROC_UNLOCK(td->td_proc);
731 PROC_UNLOCK(td->td_proc);
736 * We make sure that we have allocated a pager before we need
739 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
740 0, size, VM_PROT_DEFAULT, 0, cred);
741 if (shm_object == NULL) {
744 PROC_LOCK(td->td_proc);
745 racct_sub(td->td_proc, RACCT_NSHM, 1);
746 racct_sub(td->td_proc, RACCT_SHMSIZE, size);
747 PROC_UNLOCK(td->td_proc);
752 shm_object->pg_color = 0;
753 VM_OBJECT_WLOCK(shm_object);
754 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
755 vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT);
756 VM_OBJECT_WUNLOCK(shm_object);
758 shmseg->object = shm_object;
759 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
760 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
761 shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
762 shmseg->u.shm_perm.key = uap->key;
763 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
764 shmseg->cred = crhold(cred);
765 shmseg->u.shm_segsz = uap->size;
766 shmseg->u.shm_cpid = td->td_proc->p_pid;
767 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
768 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
770 mac_sysvshm_create(cred, shmseg);
772 shmseg->u.shm_ctime = time_second;
773 shm_committed += btoc(size);
775 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
780 #ifndef _SYS_SYSPROTO_H_
788 sys_shmget(struct thread *td, struct shmget_args *uap)
793 if (shm_find_prison(td->td_ucred) == NULL)
795 mode = uap->shmflg & ACCESSPERMS;
797 if (uap->key == IPC_PRIVATE) {
798 error = shmget_allocate_segment(td, uap, mode);
800 segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
803 error = shmget_existing(td, uap, mode, segnum);
804 else if ((uap->shmflg & IPC_CREAT) == 0)
807 error = shmget_allocate_segment(td, uap, mode);
814 shmfork_myhook(struct proc *p1, struct proc *p2)
816 struct shmmap_state *shmmap_s;
821 size = shminfo.shmseg * sizeof(struct shmmap_state);
822 shmmap_s = malloc(size, M_SHM, M_WAITOK);
823 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
824 p2->p_vmspace->vm_shm = shmmap_s;
825 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
826 if (shmmap_s->shmid != -1) {
827 KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
828 IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
829 ("segnum %d shmalloced %d",
830 IPCID_TO_IX(shmmap_s->shmid), shmalloced));
831 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
838 shmexit_myhook(struct vmspace *vm)
840 struct shmmap_state *base, *shm;
847 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
848 if (shm->shmid != -1)
849 shm_delete_mapping(vm, shm);
859 struct shmid_kernel *newsegs;
862 SYSVSHM_ASSERT_LOCKED();
864 if (shmalloced >= shminfo.shmmni)
867 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM,
869 for (i = 0; i < shmalloced; i++)
870 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
871 for (; i < shminfo.shmmni; i++) {
872 newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
873 newsegs[i].u.shm_perm.seq = 0;
875 mac_sysvshm_init(&newsegs[i]);
878 free(shmsegs, M_SHM);
880 shmalloced = shminfo.shmmni;
883 static struct syscall_helper_data shm_syscalls[] = {
884 SYSCALL_INIT_HELPER(shmat),
885 SYSCALL_INIT_HELPER(shmctl),
886 SYSCALL_INIT_HELPER(shmdt),
887 SYSCALL_INIT_HELPER(shmget),
888 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
889 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
890 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
892 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
893 SYSCALL_INIT_HELPER(shmsys),
898 #ifdef COMPAT_FREEBSD32
899 #include <compat/freebsd32/freebsd32.h>
900 #include <compat/freebsd32/freebsd32_ipc.h>
901 #include <compat/freebsd32/freebsd32_proto.h>
902 #include <compat/freebsd32/freebsd32_signal.h>
903 #include <compat/freebsd32/freebsd32_syscall.h>
904 #include <compat/freebsd32/freebsd32_util.h>
906 static struct syscall_helper_data shm32_syscalls[] = {
907 SYSCALL32_INIT_HELPER_COMPAT(shmat),
908 SYSCALL32_INIT_HELPER_COMPAT(shmdt),
909 SYSCALL32_INIT_HELPER_COMPAT(shmget),
910 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
911 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
912 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
913 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
914 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
926 osd_method_t methods[PR_MAXMETHOD] = {
927 [PR_METHOD_CHECK] = shm_prison_check,
928 [PR_METHOD_SET] = shm_prison_set,
929 [PR_METHOD_GET] = shm_prison_get,
930 [PR_METHOD_REMOVE] = shm_prison_remove,
934 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
935 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
937 if (shminfo.shmmax == SHMMAX) {
938 /* Initialize shmmax dealing with possible overflow. */
939 for (i = PAGE_SIZE; i != 0; i--) {
940 shminfo.shmmax = shminfo.shmall * i;
941 if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
945 shmalloced = shminfo.shmmni;
946 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM,
948 for (i = 0; i < shmalloced; i++) {
949 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
950 shmsegs[i].u.shm_perm.seq = 0;
952 mac_sysvshm_init(&shmsegs[i]);
958 sx_init(&sysvshmsx, "sysvshmsx");
959 shmexit_hook = &shmexit_myhook;
960 shmfork_hook = &shmfork_myhook;
962 /* Set current prisons according to their allow.sysvipc. */
963 shm_prison_slot = osd_jail_register(NULL, methods);
964 rsv = osd_reserve(shm_prison_slot);
965 prison_lock(&prison0);
966 (void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
967 prison_unlock(&prison0);
969 sx_slock(&allprison_lock);
970 TAILQ_FOREACH(pr, &allprison, pr_list) {
972 rsv = osd_reserve(shm_prison_slot);
974 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
975 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
982 osd_free_reserved(rsv);
983 sx_sunlock(&allprison_lock);
985 error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
988 #ifdef COMPAT_FREEBSD32
989 error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
1004 #ifdef COMPAT_FREEBSD32
1005 syscall32_helper_unregister(shm32_syscalls);
1007 syscall_helper_unregister(shm_syscalls);
1008 if (shm_prison_slot != 0)
1009 osd_jail_deregister(shm_prison_slot);
1011 for (i = 0; i < shmalloced; i++) {
1013 mac_sysvshm_destroy(&shmsegs[i]);
1016 * Objects might be still mapped into the processes
1017 * address spaces. Actual free would happen on the
1018 * last mapping destruction.
1020 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1021 vm_object_deallocate(shmsegs[i].object);
1023 free(shmsegs, M_SHM);
1024 shmexit_hook = NULL;
1025 shmfork_hook = NULL;
1026 sx_destroy(&sysvshmsx);
1031 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1033 struct shmid_kernel tshmseg;
1034 #ifdef COMPAT_FREEBSD32
1035 struct shmid_kernel32 tshmseg32;
1037 struct prison *pr, *rpr;
1043 pr = req->td->td_ucred->cr_prison;
1044 rpr = shm_find_prison(req->td->td_ucred);
1046 for (i = 0; i < shmalloced; i++) {
1047 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1048 rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1049 bzero(&tshmseg, sizeof(tshmseg));
1050 tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1052 tshmseg = shmsegs[i];
1053 if (tshmseg.cred->cr_prison != pr)
1054 tshmseg.u.shm_perm.key = IPC_PRIVATE;
1056 #ifdef COMPAT_FREEBSD32
1057 if (SV_CURPROC_FLAG(SV_ILP32)) {
1058 bzero(&tshmseg32, sizeof(tshmseg32));
1059 freebsd32_ipcperm_out(&tshmseg.u.shm_perm,
1060 &tshmseg32.u.shm_perm);
1061 CP(tshmseg, tshmseg32, u.shm_segsz);
1062 CP(tshmseg, tshmseg32, u.shm_lpid);
1063 CP(tshmseg, tshmseg32, u.shm_cpid);
1064 CP(tshmseg, tshmseg32, u.shm_nattch);
1065 CP(tshmseg, tshmseg32, u.shm_atime);
1066 CP(tshmseg, tshmseg32, u.shm_dtime);
1067 CP(tshmseg, tshmseg32, u.shm_ctime);
1068 /* Don't copy object, label, or cred */
1069 outaddr = &tshmseg32;
1070 outsize = sizeof(tshmseg32);
1074 tshmseg.object = NULL;
1075 tshmseg.label = NULL;
1076 tshmseg.cred = NULL;
1078 outsize = sizeof(tshmseg);
1080 error = SYSCTL_OUT(req, outaddr, outsize);
1089 shm_prison_check(void *obj, void *data)
1091 struct prison *pr = obj;
1092 struct prison *prpr;
1093 struct vfsoptlist *opts = data;
1097 * sysvshm is a jailsys integer.
1098 * It must be "disable" if the parent jail is disabled.
1100 error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1101 if (error != ENOENT) {
1105 case JAIL_SYS_DISABLE:
1108 case JAIL_SYS_INHERIT:
1109 prison_lock(pr->pr_parent);
1110 prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1111 prison_unlock(pr->pr_parent);
1124 shm_prison_set(void *obj, void *data)
1126 struct prison *pr = obj;
1127 struct prison *tpr, *orpr, *nrpr, *trpr;
1128 struct vfsoptlist *opts = data;
1133 * sysvshm controls which jail is the root of the associated segments
1134 * (this jail or same as the parent), or if the feature is available
1137 if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1138 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1140 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1143 if (jsys == JAIL_SYS_DISABLE) {
1145 orpr = osd_jail_get(pr, shm_prison_slot);
1147 osd_jail_del(pr, shm_prison_slot);
1151 shm_prison_cleanup(pr);
1152 /* Disable all child jails as well. */
1153 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1155 trpr = osd_jail_get(tpr, shm_prison_slot);
1157 osd_jail_del(tpr, shm_prison_slot);
1160 shm_prison_cleanup(tpr);
1167 } else if (jsys != -1) {
1168 if (jsys == JAIL_SYS_NEW)
1171 prison_lock(pr->pr_parent);
1172 nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1173 prison_unlock(pr->pr_parent);
1175 rsv = osd_reserve(shm_prison_slot);
1177 orpr = osd_jail_get(pr, shm_prison_slot);
1179 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1182 osd_free_reserved(rsv);
1186 shm_prison_cleanup(pr);
1188 /* Change child jails matching the old root, */
1189 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1191 trpr = osd_jail_get(tpr,
1194 (void)osd_jail_set(tpr,
1195 shm_prison_slot, nrpr);
1198 shm_prison_cleanup(tpr);
1212 shm_prison_get(void *obj, void *data)
1214 struct prison *pr = obj;
1216 struct vfsoptlist *opts = data;
1219 /* Set sysvshm based on the jail's root prison. */
1221 rpr = osd_jail_get(pr, shm_prison_slot);
1223 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1224 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1225 error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1226 if (error == ENOENT)
1232 shm_prison_remove(void *obj, void *data __unused)
1234 struct prison *pr = obj;
1239 rpr = osd_jail_get(pr, shm_prison_slot);
1242 shm_prison_cleanup(pr);
1248 shm_prison_cleanup(struct prison *pr)
1250 struct shmid_kernel *shmseg;
1253 /* Remove any segments that belong to this jail. */
1254 for (i = 0; i < shmalloced; i++) {
1255 shmseg = &shmsegs[i];
1256 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1257 shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1258 shm_remove(shmseg, i);
1263 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1265 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1267 struct ipc_perm_old shm_perm; /* operation perms */
1268 int shm_segsz; /* size of segment (bytes) */
1269 u_short shm_cpid; /* pid, creator */
1270 u_short shm_lpid; /* pid, last operation */
1271 short shm_nattch; /* no. of current attaches */
1272 time_t shm_atime; /* last attach time */
1273 time_t shm_dtime; /* last detach time */
1274 time_t shm_ctime; /* last change time */
1275 void *shm_handle; /* internal handle for shm segment */
1278 struct oshmctl_args {
1281 struct oshmid_ds *ubuf;
1285 oshmctl(struct thread *td, struct oshmctl_args *uap)
1290 struct shmid_kernel *shmseg;
1291 struct oshmid_ds outbuf;
1293 rpr = shm_find_prison(td->td_ucred);
1296 if (uap->cmd != IPC_STAT) {
1297 return (freebsd7_shmctl(td,
1298 (struct freebsd7_shmctl_args *)uap));
1301 shmseg = shm_find_segment(rpr, uap->shmid, true);
1302 if (shmseg == NULL) {
1306 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1312 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1318 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1319 outbuf.shm_segsz = shmseg->u.shm_segsz;
1320 outbuf.shm_cpid = shmseg->u.shm_cpid;
1321 outbuf.shm_lpid = shmseg->u.shm_lpid;
1322 outbuf.shm_nattch = shmseg->u.shm_nattch;
1323 outbuf.shm_atime = shmseg->u.shm_atime;
1324 outbuf.shm_dtime = shmseg->u.shm_dtime;
1325 outbuf.shm_ctime = shmseg->u.shm_ctime;
1326 outbuf.shm_handle = shmseg->object;
1328 return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1334 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1335 static sy_call_t *shmcalls[] = {
1336 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1337 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1338 (sy_call_t *)freebsd7_shmctl
1341 #ifndef _SYS_SYSPROTO_H_
1342 /* XXX actually varargs. */
1343 struct shmsys_args {
1351 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1354 AUDIT_ARG_SVIPC_WHICH(uap->which);
1355 if (uap->which < 0 || uap->which >= nitems(shmcalls))
1357 return ((*shmcalls[uap->which])(td, &uap->a2));
1360 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1362 #ifdef COMPAT_FREEBSD32
1365 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1368 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1369 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1370 AUDIT_ARG_SVIPC_WHICH(uap->which);
1371 switch (uap->which) {
1372 case 0: { /* shmat */
1373 struct shmat_args ap;
1376 ap.shmaddr = PTRIN(uap->a3);
1377 ap.shmflg = uap->a4;
1378 return (sysent[SYS_shmat].sy_call(td, &ap));
1380 case 2: { /* shmdt */
1381 struct shmdt_args ap;
1383 ap.shmaddr = PTRIN(uap->a2);
1384 return (sysent[SYS_shmdt].sy_call(td, &ap));
1386 case 3: { /* shmget */
1387 struct shmget_args ap;
1391 ap.shmflg = uap->a4;
1392 return (sysent[SYS_shmget].sy_call(td, &ap));
1394 case 4: { /* shmctl */
1395 struct freebsd7_freebsd32_shmctl_args ap;
1399 ap.buf = PTRIN(uap->a4);
1400 return (freebsd7_freebsd32_shmctl(td, &ap));
1402 case 1: /* oshmctl */
1407 return (nosys(td, NULL));
1411 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1412 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1414 freebsd7_freebsd32_shmctl(struct thread *td,
1415 struct freebsd7_freebsd32_shmctl_args *uap)
1419 struct shmid_ds shmid_ds;
1420 struct shm_info shm_info;
1421 struct shminfo shminfo;
1424 struct shmid_ds32_old shmid_ds32;
1425 struct shm_info32 shm_info32;
1426 struct shminfo32 shminfo32;
1430 if (uap->cmd == IPC_SET) {
1431 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1432 sizeof(u32.shmid_ds32))))
1434 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1435 &u.shmid_ds.shm_perm);
1436 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1437 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1438 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1439 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1440 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1441 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1442 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1445 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1449 /* Cases in which we need to copyout */
1452 CP(u.shminfo, u32.shminfo32, shmmax);
1453 CP(u.shminfo, u32.shminfo32, shmmin);
1454 CP(u.shminfo, u32.shminfo32, shmmni);
1455 CP(u.shminfo, u32.shminfo32, shmseg);
1456 CP(u.shminfo, u32.shminfo32, shmall);
1457 error = copyout(&u32.shminfo32, uap->buf,
1458 sizeof(u32.shminfo32));
1461 CP(u.shm_info, u32.shm_info32, used_ids);
1462 CP(u.shm_info, u32.shm_info32, shm_rss);
1463 CP(u.shm_info, u32.shm_info32, shm_tot);
1464 CP(u.shm_info, u32.shm_info32, shm_swp);
1465 CP(u.shm_info, u32.shm_info32, swap_attempts);
1466 CP(u.shm_info, u32.shm_info32, swap_successes);
1467 error = copyout(&u32.shm_info32, uap->buf,
1468 sizeof(u32.shm_info32));
1472 memset(&u32.shmid_ds32, 0, sizeof(u32.shmid_ds32));
1473 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1474 &u32.shmid_ds32.shm_perm);
1475 if (u.shmid_ds.shm_segsz > INT32_MAX)
1476 u32.shmid_ds32.shm_segsz = INT32_MAX;
1478 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1479 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1480 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1481 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1482 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1483 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1484 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1485 u32.shmid_ds32.shm_internal = 0;
1486 error = copyout(&u32.shmid_ds32, uap->buf,
1487 sizeof(u32.shmid_ds32));
1493 /* Invalidate the return value */
1494 td->td_retval[0] = -1;
1501 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1505 struct shmid_ds shmid_ds;
1506 struct shm_info shm_info;
1507 struct shminfo shminfo;
1510 struct shmid_ds32 shmid_ds32;
1511 struct shm_info32 shm_info32;
1512 struct shminfo32 shminfo32;
1516 if (uap->cmd == IPC_SET) {
1517 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1518 sizeof(u32.shmid_ds32))))
1520 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1521 &u.shmid_ds.shm_perm);
1522 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1523 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1524 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1525 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1526 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1527 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1528 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1531 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1535 /* Cases in which we need to copyout */
1538 CP(u.shminfo, u32.shminfo32, shmmax);
1539 CP(u.shminfo, u32.shminfo32, shmmin);
1540 CP(u.shminfo, u32.shminfo32, shmmni);
1541 CP(u.shminfo, u32.shminfo32, shmseg);
1542 CP(u.shminfo, u32.shminfo32, shmall);
1543 error = copyout(&u32.shminfo32, uap->buf,
1544 sizeof(u32.shminfo32));
1547 CP(u.shm_info, u32.shm_info32, used_ids);
1548 CP(u.shm_info, u32.shm_info32, shm_rss);
1549 CP(u.shm_info, u32.shm_info32, shm_tot);
1550 CP(u.shm_info, u32.shm_info32, shm_swp);
1551 CP(u.shm_info, u32.shm_info32, swap_attempts);
1552 CP(u.shm_info, u32.shm_info32, swap_successes);
1553 error = copyout(&u32.shm_info32, uap->buf,
1554 sizeof(u32.shm_info32));
1558 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1559 &u32.shmid_ds32.shm_perm);
1560 if (u.shmid_ds.shm_segsz > INT32_MAX)
1561 u32.shmid_ds32.shm_segsz = INT32_MAX;
1563 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1564 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1565 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1566 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1567 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1568 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1569 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1570 error = copyout(&u32.shmid_ds32, uap->buf,
1571 sizeof(u32.shmid_ds32));
1577 /* Invalidate the return value */
1578 td->td_retval[0] = -1;
1584 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1585 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1588 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1591 #ifndef _SYS_SYSPROTO_H_
1592 struct freebsd7_shmctl_args {
1595 struct shmid_ds_old *buf;
1599 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1602 struct shmid_ds_old old;
1603 struct shmid_ds buf;
1607 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1608 * Linux binaries. If we see the call come through the FreeBSD ABI,
1609 * return an error back to the user since we do not to support this.
1611 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1612 uap->cmd == SHM_STAT)
1615 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1616 if (uap->cmd == IPC_SET) {
1617 if ((error = copyin(uap->buf, &old, sizeof(old))))
1619 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1620 CP(old, buf, shm_segsz);
1621 CP(old, buf, shm_lpid);
1622 CP(old, buf, shm_cpid);
1623 CP(old, buf, shm_nattch);
1624 CP(old, buf, shm_atime);
1625 CP(old, buf, shm_dtime);
1626 CP(old, buf, shm_ctime);
1629 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1633 /* Cases in which we need to copyout */
1636 memset(&old, 0, sizeof(old));
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);