1 /* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
3 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Adam Glass and Charles
17 * 4. The names of the authors may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 2003-2005 McAfee, Inc.
33 * Copyright (c) 2016-2017 Robert N. M. Watson
34 * All rights reserved.
36 * This software was developed for the FreeBSD Project in part by McAfee
37 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
38 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
41 * Portions of this software were developed by BAE Systems, the University of
42 * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
43 * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
44 * Computing (TC) research program.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
71 #include "opt_compat.h"
72 #include "opt_sysvipc.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/limits.h>
79 #include <sys/sysctl.h>
82 #include <sys/malloc.h>
84 #include <sys/module.h>
85 #include <sys/mutex.h>
86 #include <sys/racct.h>
87 #include <sys/resourcevar.h>
88 #include <sys/rwlock.h>
90 #include <sys/syscall.h>
91 #include <sys/syscallsubr.h>
92 #include <sys/sysent.h>
93 #include <sys/sysproto.h>
96 #include <security/audit/audit.h>
97 #include <security/mac/mac_framework.h>
100 #include <vm/vm_param.h>
102 #include <vm/vm_object.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_page.h>
105 #include <vm/vm_pager.h>
107 FEATURE(sysv_shm, "System V shared memory segments support");
109 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
111 static int shmget_allocate_segment(struct thread *td,
112 struct shmget_args *uap, int mode);
113 static int shmget_existing(struct thread *td, struct shmget_args *uap,
114 int mode, int segnum);
116 #define SHMSEG_FREE 0x0200
117 #define SHMSEG_REMOVED 0x0400
118 #define SHMSEG_ALLOCATED 0x0800
120 static int shm_last_free, shm_nused, shmalloced;
121 vm_size_t shm_committed;
122 static struct shmid_kernel *shmsegs;
123 static unsigned shm_prison_slot;
125 struct shmmap_state {
130 static void shm_deallocate_segment(struct shmid_kernel *);
131 static int shm_find_segment_by_key(struct prison *, key_t);
132 static struct shmid_kernel *shm_find_segment(struct prison *, int, bool);
133 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
134 static void shmrealloc(void);
135 static int shminit(void);
136 static int sysvshm_modload(struct module *, int, void *);
137 static int shmunload(void);
138 static void shmexit_myhook(struct vmspace *vm);
139 static void shmfork_myhook(struct proc *p1, struct proc *p2);
140 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
141 static void shm_remove(struct shmid_kernel *, int);
142 static struct prison *shm_find_prison(struct ucred *);
143 static int shm_prison_cansee(struct prison *, struct shmid_kernel *);
144 static int shm_prison_check(void *, void *);
145 static int shm_prison_set(void *, void *);
146 static int shm_prison_get(void *, void *);
147 static int shm_prison_remove(void *, void *);
148 static void shm_prison_cleanup(struct prison *);
154 #define SHMMAXPGS 131072 /* Note: sysv shared memory is swap backed. */
157 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
169 #define SHMALL (SHMMAXPGS)
172 struct shminfo shminfo = {
180 static int shm_use_phys;
181 static int shm_allow_removed = 1;
183 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RWTUN, &shminfo.shmmax, 0,
184 "Maximum shared memory segment size");
185 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RWTUN, &shminfo.shmmin, 0,
186 "Minimum shared memory segment size");
187 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
188 "Number of shared memory identifiers");
189 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
190 "Number of segments per process");
191 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RWTUN, &shminfo.shmall, 0,
192 "Maximum number of pages available for shared memory");
193 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RWTUN,
194 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
195 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RWTUN,
196 &shm_allow_removed, 0,
197 "Enable/Disable attachment to attached segments marked for removal");
198 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLTYPE_OPAQUE | CTLFLAG_RD |
199 CTLFLAG_MPSAFE, NULL, 0, sysctl_shmsegs, "",
200 "Current number of shared memory segments allocated");
202 static struct sx sysvshmsx;
203 #define SYSVSHM_LOCK() sx_xlock(&sysvshmsx)
204 #define SYSVSHM_UNLOCK() sx_xunlock(&sysvshmsx)
205 #define SYSVSHM_ASSERT_LOCKED() sx_assert(&sysvshmsx, SA_XLOCKED)
208 shm_find_segment_by_key(struct prison *pr, key_t key)
212 for (i = 0; i < shmalloced; i++)
213 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
214 shmsegs[i].cred != NULL &&
215 shmsegs[i].cred->cr_prison == pr &&
216 shmsegs[i].u.shm_perm.key == key)
222 * Finds segment either by shmid if is_shmid is true, or by segnum if
225 static struct shmid_kernel *
226 shm_find_segment(struct prison *rpr, int arg, bool is_shmid)
228 struct shmid_kernel *shmseg;
231 segnum = is_shmid ? IPCID_TO_IX(arg) : arg;
232 if (segnum < 0 || segnum >= shmalloced)
234 shmseg = &shmsegs[segnum];
235 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
236 (!shm_allow_removed &&
237 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
238 (is_shmid && shmseg->u.shm_perm.seq != IPCID_TO_SEQ(arg)) ||
239 shm_prison_cansee(rpr, shmseg) != 0)
245 shm_deallocate_segment(struct shmid_kernel *shmseg)
249 SYSVSHM_ASSERT_LOCKED();
251 vm_object_deallocate(shmseg->object);
252 shmseg->object = NULL;
253 size = round_page(shmseg->u.shm_segsz);
254 shm_committed -= btoc(size);
256 shmseg->u.shm_perm.mode = SHMSEG_FREE;
258 mac_sysvshm_cleanup(shmseg);
260 racct_sub_cred(shmseg->cred, RACCT_NSHM, 1);
261 racct_sub_cred(shmseg->cred, RACCT_SHMSIZE, size);
262 crfree(shmseg->cred);
267 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
269 struct shmid_kernel *shmseg;
273 SYSVSHM_ASSERT_LOCKED();
274 segnum = IPCID_TO_IX(shmmap_s->shmid);
275 KASSERT(segnum >= 0 && segnum < shmalloced,
276 ("segnum %d shmalloced %d", segnum, shmalloced));
278 shmseg = &shmsegs[segnum];
279 size = round_page(shmseg->u.shm_segsz);
280 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
281 if (result != KERN_SUCCESS)
283 shmmap_s->shmid = -1;
284 shmseg->u.shm_dtime = time_second;
285 if (--shmseg->u.shm_nattch == 0 &&
286 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
287 shm_deallocate_segment(shmseg);
288 shm_last_free = segnum;
294 shm_remove(struct shmid_kernel *shmseg, int segnum)
297 shmseg->u.shm_perm.key = IPC_PRIVATE;
298 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
299 if (shmseg->u.shm_nattch == 0) {
300 shm_deallocate_segment(shmseg);
301 shm_last_free = segnum;
305 static struct prison *
306 shm_find_prison(struct ucred *cred)
308 struct prison *pr, *rpr;
310 pr = cred->cr_prison;
312 rpr = osd_jail_get(pr, shm_prison_slot);
318 shm_prison_cansee(struct prison *rpr, struct shmid_kernel *shmseg)
321 if (shmseg->cred == NULL ||
322 !(rpr == shmseg->cred->cr_prison ||
323 prison_ischild(rpr, shmseg->cred->cr_prison)))
329 kern_shmdt_locked(struct thread *td, const void *shmaddr)
331 struct proc *p = td->td_proc;
332 struct shmmap_state *shmmap_s;
333 #if defined(AUDIT) || defined(MAC)
334 struct shmid_kernel *shmsegptr;
341 SYSVSHM_ASSERT_LOCKED();
342 if (shm_find_prison(td->td_ucred) == NULL)
344 shmmap_s = p->p_vmspace->vm_shm;
345 if (shmmap_s == NULL)
347 AUDIT_ARG_SVIPC_ID(shmmap_s->shmid);
348 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
349 if (shmmap_s->shmid != -1 &&
350 shmmap_s->va == (vm_offset_t)shmaddr) {
354 if (i == shminfo.shmseg)
356 #if (defined(AUDIT) && defined(KDTRACE_HOOKS)) || defined(MAC)
357 shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
360 error = mac_sysvshm_check_shmdt(td->td_ucred, shmsegptr);
364 return (shm_delete_mapping(p->p_vmspace, shmmap_s));
367 #ifndef _SYS_SYSPROTO_H_
373 sys_shmdt(struct thread *td, struct shmdt_args *uap)
378 error = kern_shmdt_locked(td, uap->shmaddr);
384 kern_shmat_locked(struct thread *td, int shmid, const void *shmaddr,
388 struct proc *p = td->td_proc;
389 struct shmid_kernel *shmseg;
390 struct shmmap_state *shmmap_s;
391 vm_offset_t attach_va;
396 AUDIT_ARG_SVIPC_ID(shmid);
397 AUDIT_ARG_VALUE(shmflg);
399 SYSVSHM_ASSERT_LOCKED();
400 rpr = shm_find_prison(td->td_ucred);
403 shmmap_s = p->p_vmspace->vm_shm;
404 if (shmmap_s == NULL) {
405 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
407 for (i = 0; i < shminfo.shmseg; i++)
408 shmmap_s[i].shmid = -1;
409 KASSERT(p->p_vmspace->vm_shm == NULL, ("raced"));
410 p->p_vmspace->vm_shm = shmmap_s;
412 shmseg = shm_find_segment(rpr, shmid, true);
415 error = ipcperm(td, &shmseg->u.shm_perm,
416 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
420 error = mac_sysvshm_check_shmat(td->td_ucred, shmseg, shmflg);
424 for (i = 0; i < shminfo.shmseg; i++) {
425 if (shmmap_s->shmid == -1)
429 if (i >= shminfo.shmseg)
431 size = round_page(shmseg->u.shm_segsz);
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;
444 * This is just a hint to vm_map_find() about where to
447 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
448 lim_max(td, RLIMIT_DATA));
451 vm_object_reference(shmseg->object);
452 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->object, 0, &attach_va,
453 size, 0, shmaddr != NULL ? VMFS_NO_SPACE : VMFS_OPTIMAL_SPACE,
454 prot, prot, MAP_INHERIT_SHARE | MAP_PREFAULT_PARTIAL);
455 if (rv != KERN_SUCCESS) {
456 vm_object_deallocate(shmseg->object);
460 shmmap_s->va = attach_va;
461 shmmap_s->shmid = shmid;
462 shmseg->u.shm_lpid = p->p_pid;
463 shmseg->u.shm_atime = time_second;
464 shmseg->u.shm_nattch++;
465 td->td_retval[0] = attach_va;
470 kern_shmat(struct thread *td, int shmid, const void *shmaddr, int shmflg)
475 error = kern_shmat_locked(td, shmid, shmaddr, shmflg);
480 #ifndef _SYS_SYSPROTO_H_
488 sys_shmat(struct thread *td, struct shmat_args *uap)
491 return (kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg));
495 kern_shmctl_locked(struct thread *td, int shmid, int cmd, void *buf,
499 struct shmid_kernel *shmseg;
500 struct shmid_ds *shmidp;
501 struct shm_info shm_info;
504 SYSVSHM_ASSERT_LOCKED();
506 rpr = shm_find_prison(td->td_ucred);
510 AUDIT_ARG_SVIPC_ID(shmid);
511 AUDIT_ARG_SVIPC_CMD(cmd);
515 * It is possible that kern_shmctl is being called from the Linux ABI
516 * layer, in which case, we will need to implement IPC_INFO. It should
517 * be noted that other shmctl calls will be funneled through here for
518 * Linix binaries as well.
520 * NB: The Linux ABI layer will convert this data to structure(s) more
521 * consistent with the Linux ABI.
524 memcpy(buf, &shminfo, sizeof(shminfo));
526 *bufsz = sizeof(shminfo);
527 td->td_retval[0] = shmalloced;
530 shm_info.used_ids = shm_nused;
531 shm_info.shm_rss = 0; /*XXX where to get from ? */
532 shm_info.shm_tot = 0; /*XXX where to get from ? */
533 shm_info.shm_swp = 0; /*XXX where to get from ? */
534 shm_info.swap_attempts = 0; /*XXX where to get from ? */
535 shm_info.swap_successes = 0; /*XXX where to get from ? */
536 memcpy(buf, &shm_info, sizeof(shm_info));
538 *bufsz = sizeof(shm_info);
539 td->td_retval[0] = shmalloced;
543 shmseg = shm_find_segment(rpr, shmid, cmd != SHM_STAT);
547 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, cmd);
554 shmidp = (struct shmid_ds *)buf;
555 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
558 memcpy(shmidp, &shmseg->u, sizeof(struct shmid_ds));
559 if (td->td_ucred->cr_prison != shmseg->cred->cr_prison)
560 shmidp->shm_perm.key = IPC_PRIVATE;
562 *bufsz = sizeof(struct shmid_ds);
563 if (cmd == SHM_STAT) {
564 td->td_retval[0] = IXSEQ_TO_IPCID(shmid,
569 shmidp = (struct shmid_ds *)buf;
570 AUDIT_ARG_SVIPC_PERM(&shmidp->shm_perm);
571 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
574 shmseg->u.shm_perm.uid = shmidp->shm_perm.uid;
575 shmseg->u.shm_perm.gid = shmidp->shm_perm.gid;
576 shmseg->u.shm_perm.mode =
577 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
578 (shmidp->shm_perm.mode & ACCESSPERMS);
579 shmseg->u.shm_ctime = time_second;
582 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
585 shm_remove(shmseg, IPCID_TO_IX(shmid));
599 kern_shmctl(struct thread *td, int shmid, int cmd, void *buf, size_t *bufsz)
604 error = kern_shmctl_locked(td, shmid, cmd, buf, bufsz);
610 #ifndef _SYS_SYSPROTO_H_
614 struct shmid_ds *buf;
618 sys_shmctl(struct thread *td, struct shmctl_args *uap)
625 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
626 * Linux binaries. If we see the call come through the FreeBSD ABI,
627 * return an error back to the user since we do not to support this.
629 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
630 uap->cmd == SHM_STAT)
633 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
634 if (uap->cmd == IPC_SET) {
635 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
639 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
643 /* Cases in which we need to copyout */
646 error = copyout(&buf, uap->buf, bufsz);
652 /* Invalidate the return value */
653 td->td_retval[0] = -1;
660 shmget_existing(struct thread *td, struct shmget_args *uap, int mode,
663 struct shmid_kernel *shmseg;
668 SYSVSHM_ASSERT_LOCKED();
669 KASSERT(segnum >= 0 && segnum < shmalloced,
670 ("segnum %d shmalloced %d", segnum, shmalloced));
671 shmseg = &shmsegs[segnum];
672 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
675 error = mac_sysvshm_check_shmget(td->td_ucred, shmseg, uap->shmflg);
679 if (uap->size != 0 && uap->size > shmseg->u.shm_segsz)
681 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
686 shmget_allocate_segment(struct thread *td, struct shmget_args *uap, int mode)
688 struct ucred *cred = td->td_ucred;
689 struct shmid_kernel *shmseg;
690 vm_object_t shm_object;
694 SYSVSHM_ASSERT_LOCKED();
696 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
698 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
700 size = round_page(uap->size);
701 if (shm_committed + btoc(size) > shminfo.shmall)
703 if (shm_last_free < 0) {
704 shmrealloc(); /* Maybe expand the shmsegs[] array. */
705 for (i = 0; i < shmalloced; i++)
706 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
712 segnum = shm_last_free;
715 KASSERT(segnum >= 0 && segnum < shmalloced,
716 ("segnum %d shmalloced %d", segnum, shmalloced));
717 shmseg = &shmsegs[segnum];
720 PROC_LOCK(td->td_proc);
721 if (racct_add(td->td_proc, RACCT_NSHM, 1)) {
722 PROC_UNLOCK(td->td_proc);
725 if (racct_add(td->td_proc, RACCT_SHMSIZE, size)) {
726 racct_sub(td->td_proc, RACCT_NSHM, 1);
727 PROC_UNLOCK(td->td_proc);
730 PROC_UNLOCK(td->td_proc);
735 * We make sure that we have allocated a pager before we need
738 shm_object = vm_pager_allocate(shm_use_phys ? OBJT_PHYS : OBJT_SWAP,
739 0, size, VM_PROT_DEFAULT, 0, cred);
740 if (shm_object == NULL) {
743 PROC_LOCK(td->td_proc);
744 racct_sub(td->td_proc, RACCT_NSHM, 1);
745 racct_sub(td->td_proc, RACCT_SHMSIZE, size);
746 PROC_UNLOCK(td->td_proc);
751 shm_object->pg_color = 0;
752 VM_OBJECT_WLOCK(shm_object);
753 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
754 vm_object_set_flag(shm_object, OBJ_COLORED | OBJ_NOSPLIT);
755 VM_OBJECT_WUNLOCK(shm_object);
757 shmseg->object = shm_object;
758 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
759 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
760 shmseg->u.shm_perm.mode = (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
761 shmseg->u.shm_perm.key = uap->key;
762 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
763 shmseg->cred = crhold(cred);
764 shmseg->u.shm_segsz = uap->size;
765 shmseg->u.shm_cpid = td->td_proc->p_pid;
766 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
767 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
769 mac_sysvshm_create(cred, shmseg);
771 shmseg->u.shm_ctime = time_second;
772 shm_committed += btoc(size);
774 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
779 #ifndef _SYS_SYSPROTO_H_
787 sys_shmget(struct thread *td, struct shmget_args *uap)
792 if (shm_find_prison(td->td_ucred) == NULL)
794 mode = uap->shmflg & ACCESSPERMS;
796 if (uap->key == IPC_PRIVATE) {
797 error = shmget_allocate_segment(td, uap, mode);
799 segnum = shm_find_segment_by_key(td->td_ucred->cr_prison,
802 error = shmget_existing(td, uap, mode, segnum);
803 else if ((uap->shmflg & IPC_CREAT) == 0)
806 error = shmget_allocate_segment(td, uap, mode);
813 shmfork_myhook(struct proc *p1, struct proc *p2)
815 struct shmmap_state *shmmap_s;
820 size = shminfo.shmseg * sizeof(struct shmmap_state);
821 shmmap_s = malloc(size, M_SHM, M_WAITOK);
822 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
823 p2->p_vmspace->vm_shm = shmmap_s;
824 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
825 if (shmmap_s->shmid != -1) {
826 KASSERT(IPCID_TO_IX(shmmap_s->shmid) >= 0 &&
827 IPCID_TO_IX(shmmap_s->shmid) < shmalloced,
828 ("segnum %d shmalloced %d",
829 IPCID_TO_IX(shmmap_s->shmid), shmalloced));
830 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
837 shmexit_myhook(struct vmspace *vm)
839 struct shmmap_state *base, *shm;
846 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
847 if (shm->shmid != -1)
848 shm_delete_mapping(vm, shm);
858 struct shmid_kernel *newsegs;
861 SYSVSHM_ASSERT_LOCKED();
863 if (shmalloced >= shminfo.shmmni)
866 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
867 for (i = 0; i < shmalloced; i++)
868 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
869 for (; i < shminfo.shmmni; i++) {
870 newsegs[i].u.shm_perm.mode = SHMSEG_FREE;
871 newsegs[i].u.shm_perm.seq = 0;
873 mac_sysvshm_init(&newsegs[i]);
876 free(shmsegs, M_SHM);
878 shmalloced = shminfo.shmmni;
881 static struct syscall_helper_data shm_syscalls[] = {
882 SYSCALL_INIT_HELPER(shmat),
883 SYSCALL_INIT_HELPER(shmctl),
884 SYSCALL_INIT_HELPER(shmdt),
885 SYSCALL_INIT_HELPER(shmget),
886 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
887 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
888 SYSCALL_INIT_HELPER_COMPAT(freebsd7_shmctl),
890 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
891 SYSCALL_INIT_HELPER(shmsys),
896 #ifdef COMPAT_FREEBSD32
897 #include <compat/freebsd32/freebsd32.h>
898 #include <compat/freebsd32/freebsd32_ipc.h>
899 #include <compat/freebsd32/freebsd32_proto.h>
900 #include <compat/freebsd32/freebsd32_signal.h>
901 #include <compat/freebsd32/freebsd32_syscall.h>
902 #include <compat/freebsd32/freebsd32_util.h>
904 static struct syscall_helper_data shm32_syscalls[] = {
905 SYSCALL32_INIT_HELPER_COMPAT(shmat),
906 SYSCALL32_INIT_HELPER_COMPAT(shmdt),
907 SYSCALL32_INIT_HELPER_COMPAT(shmget),
908 SYSCALL32_INIT_HELPER(freebsd32_shmsys),
909 SYSCALL32_INIT_HELPER(freebsd32_shmctl),
910 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
911 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
912 SYSCALL32_INIT_HELPER(freebsd7_freebsd32_shmctl),
924 osd_method_t methods[PR_MAXMETHOD] = {
925 [PR_METHOD_CHECK] = shm_prison_check,
926 [PR_METHOD_SET] = shm_prison_set,
927 [PR_METHOD_GET] = shm_prison_get,
928 [PR_METHOD_REMOVE] = shm_prison_remove,
932 if (TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall) != 0)
933 printf("kern.ipc.shmmaxpgs is now called kern.ipc.shmall!\n");
935 if (shminfo.shmmax == SHMMAX) {
936 /* Initialize shmmax dealing with possible overflow. */
937 for (i = PAGE_SIZE; i != 0; i--) {
938 shminfo.shmmax = shminfo.shmall * i;
939 if ((shminfo.shmmax / shminfo.shmall) == (u_long)i)
943 shmalloced = shminfo.shmmni;
944 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
945 for (i = 0; i < shmalloced; i++) {
946 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
947 shmsegs[i].u.shm_perm.seq = 0;
949 mac_sysvshm_init(&shmsegs[i]);
955 sx_init(&sysvshmsx, "sysvshmsx");
956 shmexit_hook = &shmexit_myhook;
957 shmfork_hook = &shmfork_myhook;
959 /* Set current prisons according to their allow.sysvipc. */
960 shm_prison_slot = osd_jail_register(NULL, methods);
961 rsv = osd_reserve(shm_prison_slot);
962 prison_lock(&prison0);
963 (void)osd_jail_set_reserved(&prison0, shm_prison_slot, rsv, &prison0);
964 prison_unlock(&prison0);
966 sx_slock(&allprison_lock);
967 TAILQ_FOREACH(pr, &allprison, pr_list) {
969 rsv = osd_reserve(shm_prison_slot);
971 if ((pr->pr_allow & PR_ALLOW_SYSVIPC) && pr->pr_ref > 0) {
972 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
979 osd_free_reserved(rsv);
980 sx_sunlock(&allprison_lock);
982 error = syscall_helper_register(shm_syscalls, SY_THR_STATIC_KLD);
985 #ifdef COMPAT_FREEBSD32
986 error = syscall32_helper_register(shm32_syscalls, SY_THR_STATIC_KLD);
1001 #ifdef COMPAT_FREEBSD32
1002 syscall32_helper_unregister(shm32_syscalls);
1004 syscall_helper_unregister(shm_syscalls);
1005 if (shm_prison_slot != 0)
1006 osd_jail_deregister(shm_prison_slot);
1008 for (i = 0; i < shmalloced; i++) {
1010 mac_sysvshm_destroy(&shmsegs[i]);
1013 * Objects might be still mapped into the processes
1014 * address spaces. Actual free would happen on the
1015 * last mapping destruction.
1017 if (shmsegs[i].u.shm_perm.mode != SHMSEG_FREE)
1018 vm_object_deallocate(shmsegs[i].object);
1020 free(shmsegs, M_SHM);
1021 shmexit_hook = NULL;
1022 shmfork_hook = NULL;
1023 sx_destroy(&sysvshmsx);
1028 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
1030 struct shmid_kernel tshmseg;
1031 struct prison *pr, *rpr;
1035 pr = req->td->td_ucred->cr_prison;
1036 rpr = shm_find_prison(req->td->td_ucred);
1038 for (i = 0; i < shmalloced; i++) {
1039 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
1040 rpr == NULL || shm_prison_cansee(rpr, &shmsegs[i]) != 0) {
1041 bzero(&tshmseg, sizeof(tshmseg));
1042 tshmseg.u.shm_perm.mode = SHMSEG_FREE;
1044 tshmseg = shmsegs[i];
1045 if (tshmseg.cred->cr_prison != pr)
1046 tshmseg.u.shm_perm.key = IPC_PRIVATE;
1048 error = SYSCTL_OUT(req, &tshmseg, sizeof(tshmseg));
1057 shm_prison_check(void *obj, void *data)
1059 struct prison *pr = obj;
1060 struct prison *prpr;
1061 struct vfsoptlist *opts = data;
1065 * sysvshm is a jailsys integer.
1066 * It must be "disable" if the parent jail is disabled.
1068 error = vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys));
1069 if (error != ENOENT) {
1073 case JAIL_SYS_DISABLE:
1076 case JAIL_SYS_INHERIT:
1077 prison_lock(pr->pr_parent);
1078 prpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1079 prison_unlock(pr->pr_parent);
1092 shm_prison_set(void *obj, void *data)
1094 struct prison *pr = obj;
1095 struct prison *tpr, *orpr, *nrpr, *trpr;
1096 struct vfsoptlist *opts = data;
1101 * sysvshm controls which jail is the root of the associated segments
1102 * (this jail or same as the parent), or if the feature is available
1105 if (vfs_copyopt(opts, "sysvshm", &jsys, sizeof(jsys)) == ENOENT)
1106 jsys = vfs_flagopt(opts, "allow.sysvipc", NULL, 0)
1108 : vfs_flagopt(opts, "allow.nosysvipc", NULL, 0)
1111 if (jsys == JAIL_SYS_DISABLE) {
1113 orpr = osd_jail_get(pr, shm_prison_slot);
1115 osd_jail_del(pr, shm_prison_slot);
1119 shm_prison_cleanup(pr);
1120 /* Disable all child jails as well. */
1121 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1123 trpr = osd_jail_get(tpr, shm_prison_slot);
1125 osd_jail_del(tpr, shm_prison_slot);
1128 shm_prison_cleanup(tpr);
1135 } else if (jsys != -1) {
1136 if (jsys == JAIL_SYS_NEW)
1139 prison_lock(pr->pr_parent);
1140 nrpr = osd_jail_get(pr->pr_parent, shm_prison_slot);
1141 prison_unlock(pr->pr_parent);
1143 rsv = osd_reserve(shm_prison_slot);
1145 orpr = osd_jail_get(pr, shm_prison_slot);
1147 (void)osd_jail_set_reserved(pr, shm_prison_slot, rsv,
1150 osd_free_reserved(rsv);
1154 shm_prison_cleanup(pr);
1156 /* Change child jails matching the old root, */
1157 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1159 trpr = osd_jail_get(tpr,
1162 (void)osd_jail_set(tpr,
1163 shm_prison_slot, nrpr);
1166 shm_prison_cleanup(tpr);
1180 shm_prison_get(void *obj, void *data)
1182 struct prison *pr = obj;
1184 struct vfsoptlist *opts = data;
1187 /* Set sysvshm based on the jail's root prison. */
1189 rpr = osd_jail_get(pr, shm_prison_slot);
1191 jsys = rpr == NULL ? JAIL_SYS_DISABLE
1192 : rpr == pr ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
1193 error = vfs_setopt(opts, "sysvshm", &jsys, sizeof(jsys));
1194 if (error == ENOENT)
1200 shm_prison_remove(void *obj, void *data __unused)
1202 struct prison *pr = obj;
1207 rpr = osd_jail_get(pr, shm_prison_slot);
1210 shm_prison_cleanup(pr);
1216 shm_prison_cleanup(struct prison *pr)
1218 struct shmid_kernel *shmseg;
1221 /* Remove any segments that belong to this jail. */
1222 for (i = 0; i < shmalloced; i++) {
1223 shmseg = &shmsegs[i];
1224 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) &&
1225 shmseg->cred != NULL && shmseg->cred->cr_prison == pr) {
1226 shm_remove(shmseg, i);
1231 SYSCTL_JAIL_PARAM_SYS_NODE(sysvshm, CTLFLAG_RW, "SYSV shared memory");
1233 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
1235 struct ipc_perm_old shm_perm; /* operation perms */
1236 int shm_segsz; /* size of segment (bytes) */
1237 u_short shm_cpid; /* pid, creator */
1238 u_short shm_lpid; /* pid, last operation */
1239 short shm_nattch; /* no. of current attaches */
1240 time_t shm_atime; /* last attach time */
1241 time_t shm_dtime; /* last detach time */
1242 time_t shm_ctime; /* last change time */
1243 void *shm_handle; /* internal handle for shm segment */
1246 struct oshmctl_args {
1249 struct oshmid_ds *ubuf;
1253 oshmctl(struct thread *td, struct oshmctl_args *uap)
1258 struct shmid_kernel *shmseg;
1259 struct oshmid_ds outbuf;
1261 rpr = shm_find_prison(td->td_ucred);
1264 if (uap->cmd != IPC_STAT) {
1265 return (freebsd7_shmctl(td,
1266 (struct freebsd7_shmctl_args *)uap));
1269 shmseg = shm_find_segment(rpr, uap->shmid, true);
1270 if (shmseg == NULL) {
1274 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
1280 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
1286 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
1287 outbuf.shm_segsz = shmseg->u.shm_segsz;
1288 outbuf.shm_cpid = shmseg->u.shm_cpid;
1289 outbuf.shm_lpid = shmseg->u.shm_lpid;
1290 outbuf.shm_nattch = shmseg->u.shm_nattch;
1291 outbuf.shm_atime = shmseg->u.shm_atime;
1292 outbuf.shm_dtime = shmseg->u.shm_dtime;
1293 outbuf.shm_ctime = shmseg->u.shm_ctime;
1294 outbuf.shm_handle = shmseg->object;
1296 return (copyout(&outbuf, uap->ubuf, sizeof(outbuf)));
1302 /* XXX casting to (sy_call_t *) is bogus, as usual. */
1303 static sy_call_t *shmcalls[] = {
1304 (sy_call_t *)sys_shmat, (sy_call_t *)oshmctl,
1305 (sy_call_t *)sys_shmdt, (sy_call_t *)sys_shmget,
1306 (sy_call_t *)freebsd7_shmctl
1309 #ifndef _SYS_SYSPROTO_H_
1310 /* XXX actually varargs. */
1311 struct shmsys_args {
1319 sys_shmsys(struct thread *td, struct shmsys_args *uap)
1322 AUDIT_ARG_SVIPC_WHICH(uap->which);
1323 if (uap->which < 0 || uap->which >= nitems(shmcalls))
1325 return ((*shmcalls[uap->which])(td, &uap->a2));
1328 #endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
1330 #ifdef COMPAT_FREEBSD32
1333 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1336 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1337 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1338 AUDIT_ARG_SVIPC_WHICH(uap->which);
1339 switch (uap->which) {
1340 case 0: { /* shmat */
1341 struct shmat_args ap;
1344 ap.shmaddr = PTRIN(uap->a3);
1345 ap.shmflg = uap->a4;
1346 return (sysent[SYS_shmat].sy_call(td, &ap));
1348 case 2: { /* shmdt */
1349 struct shmdt_args ap;
1351 ap.shmaddr = PTRIN(uap->a2);
1352 return (sysent[SYS_shmdt].sy_call(td, &ap));
1354 case 3: { /* shmget */
1355 struct shmget_args ap;
1359 ap.shmflg = uap->a4;
1360 return (sysent[SYS_shmget].sy_call(td, &ap));
1362 case 4: { /* shmctl */
1363 struct freebsd7_freebsd32_shmctl_args ap;
1367 ap.buf = PTRIN(uap->a4);
1368 return (freebsd7_freebsd32_shmctl(td, &ap));
1370 case 1: /* oshmctl */
1375 return (nosys(td, NULL));
1379 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1380 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1382 freebsd7_freebsd32_shmctl(struct thread *td,
1383 struct freebsd7_freebsd32_shmctl_args *uap)
1387 struct shmid_ds shmid_ds;
1388 struct shm_info shm_info;
1389 struct shminfo shminfo;
1392 struct shmid_ds32_old shmid_ds32;
1393 struct shm_info32 shm_info32;
1394 struct shminfo32 shminfo32;
1398 if (uap->cmd == IPC_SET) {
1399 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1400 sizeof(u32.shmid_ds32))))
1402 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
1403 &u.shmid_ds.shm_perm);
1404 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1405 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1406 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1407 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1408 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1409 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1410 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1413 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1417 /* Cases in which we need to copyout */
1420 CP(u.shminfo, u32.shminfo32, shmmax);
1421 CP(u.shminfo, u32.shminfo32, shmmin);
1422 CP(u.shminfo, u32.shminfo32, shmmni);
1423 CP(u.shminfo, u32.shminfo32, shmseg);
1424 CP(u.shminfo, u32.shminfo32, shmall);
1425 error = copyout(&u32.shminfo32, uap->buf,
1426 sizeof(u32.shminfo32));
1429 CP(u.shm_info, u32.shm_info32, used_ids);
1430 CP(u.shm_info, u32.shm_info32, shm_rss);
1431 CP(u.shm_info, u32.shm_info32, shm_tot);
1432 CP(u.shm_info, u32.shm_info32, shm_swp);
1433 CP(u.shm_info, u32.shm_info32, swap_attempts);
1434 CP(u.shm_info, u32.shm_info32, swap_successes);
1435 error = copyout(&u32.shm_info32, uap->buf,
1436 sizeof(u32.shm_info32));
1440 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
1441 &u32.shmid_ds32.shm_perm);
1442 if (u.shmid_ds.shm_segsz > INT32_MAX)
1443 u32.shmid_ds32.shm_segsz = INT32_MAX;
1445 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1446 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1447 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1448 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1449 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1450 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1451 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1452 u32.shmid_ds32.shm_internal = 0;
1453 error = copyout(&u32.shmid_ds32, uap->buf,
1454 sizeof(u32.shmid_ds32));
1460 /* Invalidate the return value */
1461 td->td_retval[0] = -1;
1468 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1472 struct shmid_ds shmid_ds;
1473 struct shm_info shm_info;
1474 struct shminfo shminfo;
1477 struct shmid_ds32 shmid_ds32;
1478 struct shm_info32 shm_info32;
1479 struct shminfo32 shminfo32;
1483 if (uap->cmd == IPC_SET) {
1484 if ((error = copyin(uap->buf, &u32.shmid_ds32,
1485 sizeof(u32.shmid_ds32))))
1487 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1488 &u.shmid_ds.shm_perm);
1489 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1490 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1491 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1492 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1493 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1494 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1495 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1498 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1502 /* Cases in which we need to copyout */
1505 CP(u.shminfo, u32.shminfo32, shmmax);
1506 CP(u.shminfo, u32.shminfo32, shmmin);
1507 CP(u.shminfo, u32.shminfo32, shmmni);
1508 CP(u.shminfo, u32.shminfo32, shmseg);
1509 CP(u.shminfo, u32.shminfo32, shmall);
1510 error = copyout(&u32.shminfo32, uap->buf,
1511 sizeof(u32.shminfo32));
1514 CP(u.shm_info, u32.shm_info32, used_ids);
1515 CP(u.shm_info, u32.shm_info32, shm_rss);
1516 CP(u.shm_info, u32.shm_info32, shm_tot);
1517 CP(u.shm_info, u32.shm_info32, shm_swp);
1518 CP(u.shm_info, u32.shm_info32, swap_attempts);
1519 CP(u.shm_info, u32.shm_info32, swap_successes);
1520 error = copyout(&u32.shm_info32, uap->buf,
1521 sizeof(u32.shm_info32));
1525 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1526 &u32.shmid_ds32.shm_perm);
1527 if (u.shmid_ds.shm_segsz > INT32_MAX)
1528 u32.shmid_ds32.shm_segsz = INT32_MAX;
1530 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1531 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1532 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1533 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1534 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1535 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1536 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1537 error = copyout(&u32.shmid_ds32, uap->buf,
1538 sizeof(u32.shmid_ds32));
1544 /* Invalidate the return value */
1545 td->td_retval[0] = -1;
1551 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
1552 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
1555 #define CP(src, dst, fld) do { (dst).fld = (src).fld; } while (0)
1558 #ifndef _SYS_SYSPROTO_H_
1559 struct freebsd7_shmctl_args {
1562 struct shmid_ds_old *buf;
1566 freebsd7_shmctl(struct thread *td, struct freebsd7_shmctl_args *uap)
1569 struct shmid_ds_old old;
1570 struct shmid_ds buf;
1574 * The only reason IPC_INFO, SHM_INFO, SHM_STAT exists is to support
1575 * Linux binaries. If we see the call come through the FreeBSD ABI,
1576 * return an error back to the user since we do not to support this.
1578 if (uap->cmd == IPC_INFO || uap->cmd == SHM_INFO ||
1579 uap->cmd == SHM_STAT)
1582 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
1583 if (uap->cmd == IPC_SET) {
1584 if ((error = copyin(uap->buf, &old, sizeof(old))))
1586 ipcperm_old2new(&old.shm_perm, &buf.shm_perm);
1587 CP(old, buf, shm_segsz);
1588 CP(old, buf, shm_lpid);
1589 CP(old, buf, shm_cpid);
1590 CP(old, buf, shm_nattch);
1591 CP(old, buf, shm_atime);
1592 CP(old, buf, shm_dtime);
1593 CP(old, buf, shm_ctime);
1596 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
1600 /* Cases in which we need to copyout */
1603 ipcperm_new2old(&buf.shm_perm, &old.shm_perm);
1604 if (buf.shm_segsz > INT_MAX)
1605 old.shm_segsz = INT_MAX;
1607 CP(buf, old, shm_segsz);
1608 CP(buf, old, shm_lpid);
1609 CP(buf, old, shm_cpid);
1610 if (buf.shm_nattch > SHRT_MAX)
1611 old.shm_nattch = SHRT_MAX;
1613 CP(buf, old, shm_nattch);
1614 CP(buf, old, shm_atime);
1615 CP(buf, old, shm_dtime);
1616 CP(buf, old, shm_ctime);
1617 old.shm_internal = NULL;
1618 error = copyout(&old, uap->buf, sizeof(old));
1624 /* Invalidate the return value */
1625 td->td_retval[0] = -1;
1630 #endif /* COMPAT_FREEBSD4 || COMPAT_FREEBSD5 || COMPAT_FREEBSD6 ||
1634 sysvshm_modload(struct module *module, int cmd, void *arg)
1645 error = shmunload();
1656 static moduledata_t sysvshm_mod = {
1662 DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1663 MODULE_VERSION(sysvshm, 1);