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 * All rights reserved.
35 * This software was developed for the FreeBSD Project in part by McAfee
36 * Research, the Security Research Division of McAfee, Inc under DARPA/SPAWAR
37 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS research
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include "opt_compat.h"
66 #include "opt_sysvipc.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
76 #include <sys/malloc.h>
78 #include <sys/module.h>
79 #include <sys/mutex.h>
80 #include <sys/resourcevar.h>
82 #include <sys/syscall.h>
83 #include <sys/syscallsubr.h>
84 #include <sys/sysent.h>
85 #include <sys/sysproto.h>
88 #include <security/mac/mac_framework.h>
91 #include <vm/vm_param.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_pager.h>
98 static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
100 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
102 static int oshmctl(struct thread *td, struct oshmctl_args *uap);
105 static int shmget_allocate_segment(struct thread *td,
106 struct shmget_args *uap, int mode);
107 static int shmget_existing(struct thread *td, struct shmget_args *uap,
108 int mode, int segnum);
110 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
111 /* XXX casting to (sy_call_t *) is bogus, as usual. */
112 static sy_call_t *shmcalls[] = {
113 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
114 (sy_call_t *)shmdt, (sy_call_t *)shmget,
119 #define SHMSEG_FREE 0x0200
120 #define SHMSEG_REMOVED 0x0400
121 #define SHMSEG_ALLOCATED 0x0800
122 #define SHMSEG_WANTED 0x1000
124 static int shm_last_free, shm_nused, shmalloced;
125 vm_size_t shm_committed;
126 static struct shmid_kernel *shmsegs;
128 struct shmmap_state {
133 static void shm_deallocate_segment(struct shmid_kernel *);
134 static int shm_find_segment_by_key(key_t);
135 static struct shmid_kernel *shm_find_segment_by_shmid(int);
136 static struct shmid_kernel *shm_find_segment_by_shmidx(int);
137 static int shm_delete_mapping(struct vmspace *vm, struct shmmap_state *);
138 static void shmrealloc(void);
139 static void shminit(void);
140 static int sysvshm_modload(struct module *, int, void *);
141 static int shmunload(void);
142 static void shmexit_myhook(struct vmspace *vm);
143 static void shmfork_myhook(struct proc *p1, struct proc *p2);
144 static int sysctl_shmsegs(SYSCTL_HANDLER_ARGS);
150 #define SHMMAXPGS 8192 /* Note: sysv shared memory is swap backed. */
153 #define SHMMAX (SHMMAXPGS*PAGE_SIZE)
165 #define SHMALL (SHMMAXPGS)
168 struct shminfo shminfo = {
176 static int shm_use_phys;
177 static int shm_allow_removed;
179 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmax, CTLFLAG_RW, &shminfo.shmmax, 0,
180 "Maximum shared memory segment size");
181 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmin, CTLFLAG_RW, &shminfo.shmmin, 0,
182 "Minimum shared memory segment size");
183 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmmni, CTLFLAG_RDTUN, &shminfo.shmmni, 0,
184 "Number of shared memory identifiers");
185 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmseg, CTLFLAG_RDTUN, &shminfo.shmseg, 0,
186 "Number of segments per process");
187 SYSCTL_ULONG(_kern_ipc, OID_AUTO, shmall, CTLFLAG_RW, &shminfo.shmall, 0,
188 "Maximum number of pages available for shared memory");
189 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_use_phys, CTLFLAG_RW,
190 &shm_use_phys, 0, "Enable/Disable locking of shared memory pages in core");
191 SYSCTL_INT(_kern_ipc, OID_AUTO, shm_allow_removed, CTLFLAG_RW,
192 &shm_allow_removed, 0,
193 "Enable/Disable attachment to attached segments marked for removal");
194 SYSCTL_PROC(_kern_ipc, OID_AUTO, shmsegs, CTLFLAG_RD,
195 NULL, 0, sysctl_shmsegs, "",
196 "Current number of shared memory segments allocated");
199 shm_find_segment_by_key(key)
204 for (i = 0; i < shmalloced; i++)
205 if ((shmsegs[i].u.shm_perm.mode & SHMSEG_ALLOCATED) &&
206 shmsegs[i].u.shm_perm.key == key)
211 static struct shmid_kernel *
212 shm_find_segment_by_shmid(int shmid)
215 struct shmid_kernel *shmseg;
217 segnum = IPCID_TO_IX(shmid);
218 if (segnum < 0 || segnum >= shmalloced)
220 shmseg = &shmsegs[segnum];
221 if ((shmseg->u.shm_perm.mode & SHMSEG_ALLOCATED) == 0 ||
222 (!shm_allow_removed &&
223 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) != 0) ||
224 shmseg->u.shm_perm.seq != IPCID_TO_SEQ(shmid))
229 static struct shmid_kernel *
230 shm_find_segment_by_shmidx(int segnum)
232 struct shmid_kernel *shmseg;
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))
245 shm_deallocate_segment(shmseg)
246 struct shmid_kernel *shmseg;
252 vm_object_deallocate(shmseg->u.shm_internal);
253 shmseg->u.shm_internal = NULL;
254 size = round_page(shmseg->shm_bsegsz);
255 shm_committed -= btoc(size);
257 shmseg->u.shm_perm.mode = SHMSEG_FREE;
259 mac_cleanup_sysv_shm(shmseg);
264 shm_delete_mapping(struct vmspace *vm, struct shmmap_state *shmmap_s)
266 struct shmid_kernel *shmseg;
272 segnum = IPCID_TO_IX(shmmap_s->shmid);
273 shmseg = &shmsegs[segnum];
274 size = round_page(shmseg->shm_bsegsz);
275 result = vm_map_remove(&vm->vm_map, shmmap_s->va, shmmap_s->va + size);
276 if (result != KERN_SUCCESS)
278 shmmap_s->shmid = -1;
279 shmseg->u.shm_dtime = time_second;
280 if ((--shmseg->u.shm_nattch <= 0) &&
281 (shmseg->u.shm_perm.mode & SHMSEG_REMOVED)) {
282 shm_deallocate_segment(shmseg);
283 shm_last_free = segnum;
288 #ifndef _SYS_SYSPROTO_H_
296 struct shmdt_args *uap;
298 struct proc *p = td->td_proc;
299 struct shmmap_state *shmmap_s;
301 struct shmid_kernel *shmsegptr;
306 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
309 shmmap_s = p->p_vmspace->vm_shm;
310 if (shmmap_s == NULL) {
314 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++) {
315 if (shmmap_s->shmid != -1 &&
316 shmmap_s->va == (vm_offset_t)uap->shmaddr) {
320 if (i == shminfo.shmseg) {
325 shmsegptr = &shmsegs[IPCID_TO_IX(shmmap_s->shmid)];
326 error = mac_check_sysv_shmdt(td->td_ucred, shmsegptr);
330 error = shm_delete_mapping(p->p_vmspace, shmmap_s);
336 #ifndef _SYS_SYSPROTO_H_
344 kern_shmat(td, shmid, shmaddr, shmflg)
350 struct proc *p = td->td_proc;
352 struct shmid_kernel *shmseg;
353 struct shmmap_state *shmmap_s = NULL;
354 vm_offset_t attach_va;
360 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
363 shmmap_s = p->p_vmspace->vm_shm;
364 if (shmmap_s == NULL) {
365 shmmap_s = malloc(shminfo.shmseg * sizeof(struct shmmap_state),
367 for (i = 0; i < shminfo.shmseg; i++)
368 shmmap_s[i].shmid = -1;
369 p->p_vmspace->vm_shm = shmmap_s;
371 shmseg = shm_find_segment_by_shmid(shmid);
372 if (shmseg == NULL) {
376 error = ipcperm(td, &shmseg->u.shm_perm,
377 (shmflg & SHM_RDONLY) ? IPC_R : IPC_R|IPC_W);
381 error = mac_check_sysv_shmat(td->td_ucred, shmseg, shmflg);
385 for (i = 0; i < shminfo.shmseg; i++) {
386 if (shmmap_s->shmid == -1)
390 if (i >= shminfo.shmseg) {
394 size = round_page(shmseg->shm_bsegsz);
395 #ifdef VM_PROT_READ_IS_EXEC
396 prot = VM_PROT_READ | VM_PROT_EXECUTE;
400 if ((shmflg & SHM_RDONLY) == 0)
401 prot |= VM_PROT_WRITE;
402 flags = MAP_ANON | MAP_SHARED;
405 if (shmflg & SHM_RND) {
406 attach_va = (vm_offset_t)shmaddr & ~(SHMLBA-1);
407 } else if (((vm_offset_t)shmaddr & (SHMLBA-1)) == 0) {
408 attach_va = (vm_offset_t)shmaddr;
415 * This is just a hint to vm_map_find() about where to
419 attach_va = round_page((vm_offset_t)p->p_vmspace->vm_daddr +
420 lim_max(p, RLIMIT_DATA));
424 vm_object_reference(shmseg->u.shm_internal);
425 rv = vm_map_find(&p->p_vmspace->vm_map, shmseg->u.shm_internal,
426 0, &attach_va, size, (flags & MAP_FIXED) ? VMFS_NO_SPACE :
427 VMFS_ANY_SPACE, prot, prot, 0);
428 if (rv != KERN_SUCCESS) {
429 vm_object_deallocate(shmseg->u.shm_internal);
433 vm_map_inherit(&p->p_vmspace->vm_map,
434 attach_va, attach_va + size, VM_INHERIT_SHARE);
436 shmmap_s->va = attach_va;
437 shmmap_s->shmid = shmid;
438 shmseg->u.shm_lpid = p->p_pid;
439 shmseg->u.shm_atime = time_second;
440 shmseg->u.shm_nattch++;
441 td->td_retval[0] = attach_va;
450 struct shmat_args *uap;
452 return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
455 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
457 struct ipc_perm shm_perm; /* operation perms */
458 int shm_segsz; /* size of segment (bytes) */
459 u_short shm_cpid; /* pid, creator */
460 u_short shm_lpid; /* pid, last operation */
461 short shm_nattch; /* no. of current attaches */
462 time_t shm_atime; /* last attach time */
463 time_t shm_dtime; /* last detach time */
464 time_t shm_ctime; /* last change time */
465 void *shm_handle; /* internal handle for shm segment */
468 struct oshmctl_args {
471 struct oshmid_ds *ubuf;
476 struct oshmctl_args *uap;
480 struct shmid_kernel *shmseg;
481 struct oshmid_ds outbuf;
483 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
486 shmseg = shm_find_segment_by_shmid(uap->shmid);
487 if (shmseg == NULL) {
493 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
497 error = mac_check_sysv_shmctl(td->td_ucred, shmseg, uap->cmd);
501 outbuf.shm_perm = shmseg->u.shm_perm;
502 outbuf.shm_segsz = shmseg->u.shm_segsz;
503 outbuf.shm_cpid = shmseg->u.shm_cpid;
504 outbuf.shm_lpid = shmseg->u.shm_lpid;
505 outbuf.shm_nattch = shmseg->u.shm_nattch;
506 outbuf.shm_atime = shmseg->u.shm_atime;
507 outbuf.shm_dtime = shmseg->u.shm_dtime;
508 outbuf.shm_ctime = shmseg->u.shm_ctime;
509 outbuf.shm_handle = shmseg->u.shm_internal;
510 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
515 error = shmctl(td, (struct shmctl_args *)uap);
527 #ifndef _SYS_SYSPROTO_H_
531 struct shmid_ds *buf;
535 kern_shmctl(td, shmid, cmd, buf, bufsz)
543 struct shmid_kernel *shmseg;
545 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
551 memcpy(buf, &shminfo, sizeof(shminfo));
553 *bufsz = sizeof(shminfo);
554 td->td_retval[0] = shmalloced;
557 struct shm_info shm_info;
558 shm_info.used_ids = shm_nused;
559 shm_info.shm_rss = 0; /*XXX where to get from ? */
560 shm_info.shm_tot = 0; /*XXX where to get from ? */
561 shm_info.shm_swp = 0; /*XXX where to get from ? */
562 shm_info.swap_attempts = 0; /*XXX where to get from ? */
563 shm_info.swap_successes = 0; /*XXX where to get from ? */
564 memcpy(buf, &shm_info, sizeof(shm_info));
566 *bufsz = sizeof(shm_info);
567 td->td_retval[0] = shmalloced;
572 shmseg = shm_find_segment_by_shmidx(shmid);
574 shmseg = shm_find_segment_by_shmid(shmid);
575 if (shmseg == NULL) {
580 error = mac_check_sysv_shmctl(td->td_ucred, shmseg, cmd);
587 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
590 memcpy(buf, &shmseg->u, sizeof(struct shmid_ds));
592 *bufsz = sizeof(struct shmid_ds);
594 td->td_retval[0] = IXSEQ_TO_IPCID(shmid, shmseg->u.shm_perm);
597 struct shmid_ds *shmid;
599 shmid = (struct shmid_ds *)buf;
600 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
603 shmseg->u.shm_perm.uid = shmid->shm_perm.uid;
604 shmseg->u.shm_perm.gid = shmid->shm_perm.gid;
605 shmseg->u.shm_perm.mode =
606 (shmseg->u.shm_perm.mode & ~ACCESSPERMS) |
607 (shmid->shm_perm.mode & ACCESSPERMS);
608 shmseg->u.shm_ctime = time_second;
612 error = ipcperm(td, &shmseg->u.shm_perm, IPC_M);
615 shmseg->u.shm_perm.key = IPC_PRIVATE;
616 shmseg->u.shm_perm.mode |= SHMSEG_REMOVED;
617 if (shmseg->u.shm_nattch <= 0) {
618 shm_deallocate_segment(shmseg);
619 shm_last_free = IPCID_TO_IX(shmid);
638 struct shmctl_args *uap;
644 /* IPC_SET needs to copyin the buffer before calling kern_shmctl */
645 if (uap->cmd == IPC_SET) {
646 if ((error = copyin(uap->buf, &buf, sizeof(struct shmid_ds))))
650 error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&buf, &bufsz);
654 /* Cases in which we need to copyout */
660 error = copyout(&buf, uap->buf, bufsz);
666 /* Invalidate the return value */
667 td->td_retval[0] = -1;
673 #ifndef _SYS_SYSPROTO_H_
681 shmget_existing(td, uap, mode, segnum)
683 struct shmget_args *uap;
687 struct shmid_kernel *shmseg;
690 shmseg = &shmsegs[segnum];
691 if (shmseg->u.shm_perm.mode & SHMSEG_REMOVED) {
693 * This segment is in the process of being allocated. Wait
694 * until it's done, and look the key up again (in case the
695 * allocation failed or it was freed).
697 shmseg->u.shm_perm.mode |= SHMSEG_WANTED;
698 error = tsleep(shmseg, PLOCK | PCATCH, "shmget", 0);
703 if ((uap->shmflg & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL))
706 error = mac_check_sysv_shmget(td->td_ucred, shmseg, uap->shmflg);
710 if (uap->size != 0 && uap->size > shmseg->shm_bsegsz)
712 td->td_retval[0] = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
717 shmget_allocate_segment(td, uap, mode)
719 struct shmget_args *uap;
722 int i, segnum, shmid;
724 struct ucred *cred = td->td_ucred;
725 struct shmid_kernel *shmseg;
726 vm_object_t shm_object;
730 if (uap->size < shminfo.shmmin || uap->size > shminfo.shmmax)
732 if (shm_nused >= shminfo.shmmni) /* Any shmids left? */
734 size = round_page(uap->size);
735 if (shm_committed + btoc(size) > shminfo.shmall)
737 if (shm_last_free < 0) {
738 shmrealloc(); /* Maybe expand the shmsegs[] array. */
739 for (i = 0; i < shmalloced; i++)
740 if (shmsegs[i].u.shm_perm.mode & SHMSEG_FREE)
746 segnum = shm_last_free;
749 shmseg = &shmsegs[segnum];
751 * In case we sleep in malloc(), mark the segment present but deleted
752 * so that noone else tries to create the same key.
754 shmseg->u.shm_perm.mode = SHMSEG_ALLOCATED | SHMSEG_REMOVED;
755 shmseg->u.shm_perm.key = uap->key;
756 shmseg->u.shm_perm.seq = (shmseg->u.shm_perm.seq + 1) & 0x7fff;
757 shmid = IXSEQ_TO_IPCID(segnum, shmseg->u.shm_perm);
760 * We make sure that we have allocated a pager before we need
765 vm_pager_allocate(OBJT_PHYS, 0, size, VM_PROT_DEFAULT, 0);
768 vm_pager_allocate(OBJT_SWAP, 0, size, VM_PROT_DEFAULT, 0);
770 VM_OBJECT_LOCK(shm_object);
771 vm_object_clear_flag(shm_object, OBJ_ONEMAPPING);
772 vm_object_set_flag(shm_object, OBJ_NOSPLIT);
773 VM_OBJECT_UNLOCK(shm_object);
775 shmseg->u.shm_internal = shm_object;
776 shmseg->u.shm_perm.cuid = shmseg->u.shm_perm.uid = cred->cr_uid;
777 shmseg->u.shm_perm.cgid = shmseg->u.shm_perm.gid = cred->cr_gid;
778 shmseg->u.shm_perm.mode = (shmseg->u.shm_perm.mode & SHMSEG_WANTED) |
779 (mode & ACCESSPERMS) | SHMSEG_ALLOCATED;
780 shmseg->u.shm_segsz = uap->size;
781 shmseg->shm_bsegsz = uap->size;
782 shmseg->u.shm_cpid = td->td_proc->p_pid;
783 shmseg->u.shm_lpid = shmseg->u.shm_nattch = 0;
784 shmseg->u.shm_atime = shmseg->u.shm_dtime = 0;
786 mac_create_sysv_shm(cred, shmseg);
788 shmseg->u.shm_ctime = time_second;
789 shm_committed += btoc(size);
791 if (shmseg->u.shm_perm.mode & SHMSEG_WANTED) {
793 * Somebody else wanted this key while we were asleep. Wake
796 shmseg->u.shm_perm.mode &= ~SHMSEG_WANTED;
799 td->td_retval[0] = shmid;
806 struct shmget_args *uap;
811 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
814 mode = uap->shmflg & ACCESSPERMS;
815 if (uap->key != IPC_PRIVATE) {
817 segnum = shm_find_segment_by_key(uap->key);
819 error = shmget_existing(td, uap, mode, segnum);
824 if ((uap->shmflg & IPC_CREAT) == 0) {
829 error = shmget_allocate_segment(td, uap, mode);
838 /* XXX actually varargs. */
839 struct shmsys_args /* {
846 #if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
849 if (!jail_sysvipc_allowed && jailed(td->td_ucred))
851 if (uap->which < 0 ||
852 uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
855 error = (*shmcalls[uap->which])(td, &uap->a2);
859 return (nosys(td, NULL));
864 shmfork_myhook(p1, p2)
865 struct proc *p1, *p2;
867 struct shmmap_state *shmmap_s;
872 size = shminfo.shmseg * sizeof(struct shmmap_state);
873 shmmap_s = malloc(size, M_SHM, M_WAITOK);
874 bcopy(p1->p_vmspace->vm_shm, shmmap_s, size);
875 p2->p_vmspace->vm_shm = shmmap_s;
876 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
877 if (shmmap_s->shmid != -1)
878 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].u.shm_nattch++;
883 shmexit_myhook(struct vmspace *vm)
885 struct shmmap_state *base, *shm;
888 if ((base = vm->vm_shm) != NULL) {
891 for (i = 0, shm = base; i < shminfo.shmseg; i++, shm++) {
892 if (shm->shmid != -1)
893 shm_delete_mapping(vm, shm);
904 struct shmid_kernel *newsegs;
906 if (shmalloced >= shminfo.shmmni)
909 newsegs = malloc(shminfo.shmmni * sizeof(*newsegs), M_SHM, M_WAITOK);
912 for (i = 0; i < shmalloced; i++)
913 bcopy(&shmsegs[i], &newsegs[i], sizeof(newsegs[0]));
914 for (; i < shminfo.shmmni; i++) {
915 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
916 shmsegs[i].u.shm_perm.seq = 0;
918 mac_init_sysv_shm(&shmsegs[i]);
921 free(shmsegs, M_SHM);
923 shmalloced = shminfo.shmmni;
931 TUNABLE_ULONG_FETCH("kern.ipc.shmmaxpgs", &shminfo.shmall);
932 for (i = PAGE_SIZE; i > 0; i--) {
933 shminfo.shmmax = shminfo.shmall * i;
934 if (shminfo.shmmax >= shminfo.shmall)
937 TUNABLE_ULONG_FETCH("kern.ipc.shmmin", &shminfo.shmmin);
938 TUNABLE_ULONG_FETCH("kern.ipc.shmmni", &shminfo.shmmni);
939 TUNABLE_ULONG_FETCH("kern.ipc.shmseg", &shminfo.shmseg);
940 TUNABLE_INT_FETCH("kern.ipc.shm_use_phys", &shm_use_phys);
942 shmalloced = shminfo.shmmni;
943 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
945 panic("cannot allocate initial memory for sysvshm");
946 for (i = 0; i < shmalloced; i++) {
947 shmsegs[i].u.shm_perm.mode = SHMSEG_FREE;
948 shmsegs[i].u.shm_perm.seq = 0;
950 mac_init_sysv_shm(&shmsegs[i]);
956 shmexit_hook = &shmexit_myhook;
957 shmfork_hook = &shmfork_myhook;
971 for (i = 0; i < shmalloced; i++)
972 mac_destroy_sysv_shm(&shmsegs[i]);
974 free(shmsegs, M_SHM);
981 sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
984 return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
988 sysvshm_modload(struct module *module, int cmd, void *arg)
1008 static moduledata_t sysvshm_mod = {
1014 SYSCALL_MODULE_HELPER(shmsys);
1015 SYSCALL_MODULE_HELPER(shmat);
1016 SYSCALL_MODULE_HELPER(shmctl);
1017 SYSCALL_MODULE_HELPER(shmdt);
1018 SYSCALL_MODULE_HELPER(shmget);
1020 DECLARE_MODULE(sysvshm, sysvshm_mod,
1021 SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1022 MODULE_VERSION(sysvshm, 1);