sysv_shm.c (cb1f0db9dbf89ed25f16308c33a721e7ed6ba531) sysv_shm.c (78525ce3185cb7196d629f4b1ec2fa2a5039129b)
1/* $FreeBSD$ */
2/* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
3
4/*
5 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 31 unchanged lines hidden (view full) ---

40#include <sys/sysproto.h>
41#include <sys/kernel.h>
42#include <sys/sysctl.h>
43#include <sys/shm.h>
44#include <sys/proc.h>
45#include <sys/malloc.h>
46#include <sys/mman.h>
47#include <sys/stat.h>
1/* $FreeBSD$ */
2/* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
3
4/*
5 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 31 unchanged lines hidden (view full) ---

40#include <sys/sysproto.h>
41#include <sys/kernel.h>
42#include <sys/sysctl.h>
43#include <sys/shm.h>
44#include <sys/proc.h>
45#include <sys/malloc.h>
46#include <sys/mman.h>
47#include <sys/stat.h>
48#include <sys/syscall.h>
48#include <sys/sysent.h>
49#include <sys/jail.h>
50
51#include <vm/vm.h>
52#include <vm/vm_param.h>
53#include <sys/lock.h>
54#include <vm/pmap.h>
55#include <vm/vm_object.h>

--- 34 unchanged lines hidden (view full) ---

90 int shmid;
91};
92
93static void shm_deallocate_segment __P((struct shmid_ds *));
94static int shm_find_segment_by_key __P((key_t));
95static struct shmid_ds *shm_find_segment_by_shmid __P((int));
96static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
97static void shmrealloc __P((void));
49#include <sys/sysent.h>
50#include <sys/jail.h>
51
52#include <vm/vm.h>
53#include <vm/vm_param.h>
54#include <sys/lock.h>
55#include <vm/pmap.h>
56#include <vm/vm_object.h>

--- 34 unchanged lines hidden (view full) ---

91 int shmid;
92};
93
94static void shm_deallocate_segment __P((struct shmid_ds *));
95static int shm_find_segment_by_key __P((key_t));
96static struct shmid_ds *shm_find_segment_by_shmid __P((int));
97static int shm_delete_mapping __P((struct proc *, struct shmmap_state *));
98static void shmrealloc __P((void));
98static void shminit __P((void *));
99static void shminit __P((void));
100static int sysvshm_modload __P((struct module *, int, void *));
101static int shmunload __P((void));
102static void shmexit_myhook __P((struct proc *p));
103static void shmfork_myhook __P((struct proc *p1, struct proc *p2));
99
100/*
101 * Tuneable values
102 */
103#ifndef SHMMAXPGS
104#define SHMMAXPGS 1024 /* XXX increase this, it's not in kva! */
105#endif
106#ifndef SHMMAX

--- 512 unchanged lines hidden (view full) ---

619 if (!jail_sysvipc_allowed && p->p_prison != NULL)
620 return (ENOSYS);
621
622 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
623 return EINVAL;
624 return ((*shmcalls[uap->which])(p, &uap->a2));
625}
626
104
105/*
106 * Tuneable values
107 */
108#ifndef SHMMAXPGS
109#define SHMMAXPGS 1024 /* XXX increase this, it's not in kva! */
110#endif
111#ifndef SHMMAX

--- 512 unchanged lines hidden (view full) ---

624 if (!jail_sysvipc_allowed && p->p_prison != NULL)
625 return (ENOSYS);
626
627 if (uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
628 return EINVAL;
629 return ((*shmcalls[uap->which])(p, &uap->a2));
630}
631
627void
628shmfork(p1, p2)
632static void
633shmfork_myhook(p1, p2)
629 struct proc *p1, *p2;
630{
631 struct shmmap_state *shmmap_s;
632 size_t size;
633 int i;
634
635 size = shminfo.shmseg * sizeof(struct shmmap_state);
636 shmmap_s = malloc(size, M_SHM, M_WAITOK);
637 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
638 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
639 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
640 if (shmmap_s->shmid != -1)
641 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
642}
643
634 struct proc *p1, *p2;
635{
636 struct shmmap_state *shmmap_s;
637 size_t size;
638 int i;
639
640 size = shminfo.shmseg * sizeof(struct shmmap_state);
641 shmmap_s = malloc(size, M_SHM, M_WAITOK);
642 bcopy((caddr_t)p1->p_vmspace->vm_shm, (caddr_t)shmmap_s, size);
643 p2->p_vmspace->vm_shm = (caddr_t)shmmap_s;
644 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
645 if (shmmap_s->shmid != -1)
646 shmsegs[IPCID_TO_IX(shmmap_s->shmid)].shm_nattch++;
647}
648
644void
645shmexit(p)
649static void
650shmexit_myhook(p)
646 struct proc *p;
647{
648 struct shmmap_state *shmmap_s;
649 int i;
650
651 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
652 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
653 if (shmmap_s->shmid != -1)

--- 21 unchanged lines hidden (view full) ---

675 shmsegs[i].shm_perm.seq = 0;
676 }
677 free(shmsegs, M_SHM);
678 shmsegs = newsegs;
679 shmalloced = shminfo.shmmni;
680}
681
682static void
651 struct proc *p;
652{
653 struct shmmap_state *shmmap_s;
654 int i;
655
656 shmmap_s = (struct shmmap_state *)p->p_vmspace->vm_shm;
657 for (i = 0; i < shminfo.shmseg; i++, shmmap_s++)
658 if (shmmap_s->shmid != -1)

--- 21 unchanged lines hidden (view full) ---

680 shmsegs[i].shm_perm.seq = 0;
681 }
682 free(shmsegs, M_SHM);
683 shmsegs = newsegs;
684 shmalloced = shminfo.shmmni;
685}
686
687static void
683shminit(dummy)
684 void *dummy;
688shminit()
685{
686 int i;
687
688 shmalloced = shminfo.shmmni;
689 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
690 if (shmsegs == NULL)
691 panic("cannot allocate initial memory for sysvshm");
692 for (i = 0; i < shmalloced; i++) {
693 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
694 shmsegs[i].shm_perm.seq = 0;
695 }
696 shm_last_free = 0;
697 shm_nused = 0;
698 shm_committed = 0;
689{
690 int i;
691
692 shmalloced = shminfo.shmmni;
693 shmsegs = malloc(shmalloced * sizeof(shmsegs[0]), M_SHM, M_WAITOK);
694 if (shmsegs == NULL)
695 panic("cannot allocate initial memory for sysvshm");
696 for (i = 0; i < shmalloced; i++) {
697 shmsegs[i].shm_perm.mode = SHMSEG_FREE;
698 shmsegs[i].shm_perm.seq = 0;
699 }
700 shm_last_free = 0;
701 shm_nused = 0;
702 shm_committed = 0;
703 shmexit_hook = &shmexit_myhook;
704 shmfork_hook = &shmfork_myhook;
699}
705}
700SYSINIT(sysv_shm, SI_SUB_SYSV_SHM, SI_ORDER_FIRST, shminit, NULL);
706
707static int
708shmunload()
709{
710
711 if (shm_nused > 0)
712 return (EBUSY);
713
714 free(shmsegs, M_SHM);
715 shmexit_hook = NULL;
716 shmfork_hook = NULL;
717 return (0);
718}
719
720static int
721sysvshm_modload(struct module *module, int cmd, void *arg)
722{
723 int error = 0;
724
725 switch (cmd) {
726 case MOD_LOAD:
727 shminit();
728 break;
729 case MOD_UNLOAD:
730 error = shmunload();
731 break;
732 case MOD_SHUTDOWN:
733 break;
734 default:
735 error = EINVAL;
736 break;
737 }
738 return (error);
739}
740
741static moduledata_t sysvshm_moduledata = {
742 "sysvshm_mod",
743 &sysvshm_modload,
744 NULL
745};
746
747SYSCALL_MODULE_HELPER(shmsys, 4);
748SYSCALL_MODULE_HELPER(shmat, 3);
749SYSCALL_MODULE_HELPER(shmctl, 3);
750SYSCALL_MODULE_HELPER(shmdt, 1);
751SYSCALL_MODULE_HELPER(shmget, 3);
752
753DECLARE_MODULE(sysvshm_mod, sysvshm_moduledata,
754 SI_SUB_SYSV_SHM, SI_ORDER_FIRST);