Deleted Added
full compact
sysv_shm.c (194832) sysv_shm.c (194894)
1/* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
2/*-
3 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

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
59 * SUCH DAMAGE.
60 */
61
62#include <sys/cdefs.h>
1/* $NetBSD: sysv_shm.c,v 1.23 1994/07/04 23:25:12 glass Exp $ */
2/*-
3 * Copyright (c) 1994 Adam Glass and Charles Hannum. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

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
59 * SUCH DAMAGE.
60 */
61
62#include <sys/cdefs.h>
63__FBSDID("$FreeBSD: head/sys/kern/sysv_shm.c 194832 2009-06-24 13:35:38Z jhb $");
63__FBSDID("$FreeBSD: head/sys/kern/sysv_shm.c 194894 2009-06-24 20:01:13Z jhb $");
64
65#include "opt_compat.h"
66#include "opt_sysvipc.h"
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/kernel.h>
71#include <sys/lock.h>

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

91#include <vm/pmap.h>
92#include <vm/vm_object.h>
93#include <vm/vm_map.h>
94#include <vm/vm_page.h>
95#include <vm/vm_pager.h>
96
97static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
98
64
65#include "opt_compat.h"
66#include "opt_sysvipc.h"
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/kernel.h>
71#include <sys/lock.h>

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

91#include <vm/pmap.h>
92#include <vm/vm_object.h>
93#include <vm/vm_map.h>
94#include <vm/vm_page.h>
95#include <vm/vm_pager.h>
96
97static MALLOC_DEFINE(M_SHM, "shm", "SVID compatible shared memory segments");
98
99#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
100struct oshmctl_args;
101static int oshmctl(struct thread *td, struct oshmctl_args *uap);
102#endif
103
104static int shmget_allocate_segment(struct thread *td,
105 struct shmget_args *uap, int mode);
106static int shmget_existing(struct thread *td, struct shmget_args *uap,
107 int mode, int segnum);
108
99static int shmget_allocate_segment(struct thread *td,
100 struct shmget_args *uap, int mode);
101static int shmget_existing(struct thread *td, struct shmget_args *uap,
102 int mode, int segnum);
103
109#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
110/* XXX casting to (sy_call_t *) is bogus, as usual. */
111static sy_call_t *shmcalls[] = {
112 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
113 (sy_call_t *)shmdt, (sy_call_t *)shmget,
114 (sy_call_t *)shmctl
115};
116#endif
117
118#define SHMSEG_FREE 0x0200
119#define SHMSEG_REMOVED 0x0400
120#define SHMSEG_ALLOCATED 0x0800
121#define SHMSEG_WANTED 0x1000
122
123static int shm_last_free, shm_nused, shmalloced;
124vm_size_t shm_committed;
125static struct shmid_kernel *shmsegs;

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

442int
443shmat(td, uap)
444 struct thread *td;
445 struct shmat_args *uap;
446{
447 return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
448}
449
104#define SHMSEG_FREE 0x0200
105#define SHMSEG_REMOVED 0x0400
106#define SHMSEG_ALLOCATED 0x0800
107#define SHMSEG_WANTED 0x1000
108
109static int shm_last_free, shm_nused, shmalloced;
110vm_size_t shm_committed;
111static struct shmid_kernel *shmsegs;

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

428int
429shmat(td, uap)
430 struct thread *td;
431 struct shmat_args *uap;
432{
433 return kern_shmat(td, uap->shmid, uap->shmaddr, uap->shmflg);
434}
435
450#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
451struct oshmid_ds {
452 struct ipc_perm shm_perm; /* operation perms */
453 int shm_segsz; /* size of segment (bytes) */
454 u_short shm_cpid; /* pid, creator */
455 u_short shm_lpid; /* pid, last operation */
456 short shm_nattch; /* no. of current attaches */
457 time_t shm_atime; /* last attach time */
458 time_t shm_dtime; /* last detach time */
459 time_t shm_ctime; /* last change time */
460 void *shm_handle; /* internal handle for shm segment */
461};
462
463struct oshmctl_args {
464 int shmid;
465 int cmd;
466 struct oshmid_ds *ubuf;
467};
468static int
469oshmctl(td, uap)
470 struct thread *td;
471 struct oshmctl_args *uap;
472{
473#ifdef COMPAT_43
474 int error = 0;
475 struct shmid_kernel *shmseg;
476 struct oshmid_ds outbuf;
477
478 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
479 return (ENOSYS);
480 mtx_lock(&Giant);
481 shmseg = shm_find_segment_by_shmid(uap->shmid);
482 if (shmseg == NULL) {
483 error = EINVAL;
484 goto done2;
485 }
486 switch (uap->cmd) {
487 case IPC_STAT:
488 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
489 if (error)
490 goto done2;
491#ifdef MAC
492 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
493 if (error != 0)
494 goto done2;
495#endif
496 outbuf.shm_perm = shmseg->u.shm_perm;
497 outbuf.shm_segsz = shmseg->u.shm_segsz;
498 outbuf.shm_cpid = shmseg->u.shm_cpid;
499 outbuf.shm_lpid = shmseg->u.shm_lpid;
500 outbuf.shm_nattch = shmseg->u.shm_nattch;
501 outbuf.shm_atime = shmseg->u.shm_atime;
502 outbuf.shm_dtime = shmseg->u.shm_dtime;
503 outbuf.shm_ctime = shmseg->u.shm_ctime;
504 outbuf.shm_handle = shmseg->u.shm_internal;
505 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
506 if (error)
507 goto done2;
508 break;
509 default:
510 error = shmctl(td, (struct shmctl_args *)uap);
511 break;
512 }
513done2:
514 mtx_unlock(&Giant);
515 return (error);
516#else
517 return (EINVAL);
518#endif
519}
520#endif
521
522int
523kern_shmctl(td, shmid, cmd, buf, bufsz)
524 struct thread *td;
525 int shmid;
526 int cmd;
527 void *buf;
528 size_t *bufsz;
529{

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

834 }
835 }
836 error = shmget_allocate_segment(td, uap, mode);
837done2:
838 mtx_unlock(&Giant);
839 return (error);
840}
841
436int
437kern_shmctl(td, shmid, cmd, buf, bufsz)
438 struct thread *td;
439 int shmid;
440 int cmd;
441 void *buf;
442 size_t *bufsz;
443{

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

748 }
749 }
750 error = shmget_allocate_segment(td, uap, mode);
751done2:
752 mtx_unlock(&Giant);
753 return (error);
754}
755
842int
843shmsys(td, uap)
844 struct thread *td;
845 /* XXX actually varargs. */
846 struct shmsys_args /* {
847 int which;
848 int a2;
849 int a3;
850 int a4;
851 } */ *uap;
852{
853#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
854 int error;
855
856 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
857 return (ENOSYS);
858 if (uap->which < 0 ||
859 uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
860 return (EINVAL);
861 mtx_lock(&Giant);
862 error = (*shmcalls[uap->which])(td, &uap->a2);
863 mtx_unlock(&Giant);
864 return (error);
865#else
866 return (nosys(td, NULL));
867#endif
868}
869
870static void
871shmfork_myhook(p1, p2)
872 struct proc *p1, *p2;
873{
874 struct shmmap_state *shmmap_s;
875 size_t size;
876 int i;
877

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

986
987static int
988sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
989{
990
991 return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
992}
993
756static void
757shmfork_myhook(p1, p2)
758 struct proc *p1, *p2;
759{
760 struct shmmap_state *shmmap_s;
761 size_t size;
762 int i;
763

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

872
873static int
874sysctl_shmsegs(SYSCTL_HANDLER_ARGS)
875{
876
877 return (SYSCTL_OUT(req, shmsegs, shmalloced * sizeof(shmsegs[0])));
878}
879
880#if defined(__i386__) && (defined(COMPAT_FREEBSD4) || defined(COMPAT_43))
881struct oshmid_ds {
882 struct ipc_perm_old shm_perm; /* operation perms */
883 int shm_segsz; /* size of segment (bytes) */
884 u_short shm_cpid; /* pid, creator */
885 u_short shm_lpid; /* pid, last operation */
886 short shm_nattch; /* no. of current attaches */
887 time_t shm_atime; /* last attach time */
888 time_t shm_dtime; /* last detach time */
889 time_t shm_ctime; /* last change time */
890 void *shm_handle; /* internal handle for shm segment */
891};
892
893struct oshmctl_args {
894 int shmid;
895 int cmd;
896 struct oshmid_ds *ubuf;
897};
898
994static int
899static int
900oshmctl(td, uap)
901 struct thread *td;
902 struct oshmctl_args *uap;
903{
904#ifdef COMPAT_43
905 int error = 0;
906 struct shmid_kernel *shmseg;
907 struct oshmid_ds outbuf;
908
909 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
910 return (ENOSYS);
911 mtx_lock(&Giant);
912 shmseg = shm_find_segment_by_shmid(uap->shmid);
913 if (shmseg == NULL) {
914 error = EINVAL;
915 goto done2;
916 }
917 switch (uap->cmd) {
918 case IPC_STAT:
919 error = ipcperm(td, &shmseg->u.shm_perm, IPC_R);
920 if (error)
921 goto done2;
922#ifdef MAC
923 error = mac_sysvshm_check_shmctl(td->td_ucred, shmseg, uap->cmd);
924 if (error != 0)
925 goto done2;
926#endif
927 ipcperm_new2old(&shmseg->u.shm_perm, &outbuf.shm_perm);
928 outbuf.shm_segsz = shmseg->u.shm_segsz;
929 outbuf.shm_cpid = shmseg->u.shm_cpid;
930 outbuf.shm_lpid = shmseg->u.shm_lpid;
931 outbuf.shm_nattch = shmseg->u.shm_nattch;
932 outbuf.shm_atime = shmseg->u.shm_atime;
933 outbuf.shm_dtime = shmseg->u.shm_dtime;
934 outbuf.shm_ctime = shmseg->u.shm_ctime;
935 outbuf.shm_handle = shmseg->object;
936 error = copyout(&outbuf, uap->ubuf, sizeof(outbuf));
937 if (error)
938 goto done2;
939 break;
940 default:
941 error = freebsd7_shmctl(td, (struct shmctl_args *)uap);
942 break;
943 }
944done2:
945 mtx_unlock(&Giant);
946 return (error);
947#else
948 return (EINVAL);
949#endif
950}
951
952/* XXX casting to (sy_call_t *) is bogus, as usual. */
953static sy_call_t *shmcalls[] = {
954 (sy_call_t *)shmat, (sy_call_t *)oshmctl,
955 (sy_call_t *)shmdt, (sy_call_t *)shmget,
956 (sy_call_t *)shmctl
957};
958
959int
960shmsys(td, uap)
961 struct thread *td;
962 /* XXX actually varargs. */
963 struct shmsys_args /* {
964 int which;
965 int a2;
966 int a3;
967 int a4;
968 } */ *uap;
969{
970 int error;
971
972 if (!prison_allow(td->td_ucred, PR_ALLOW_SYSVIPC))
973 return (ENOSYS);
974 if (uap->which < 0 ||
975 uap->which >= sizeof(shmcalls)/sizeof(shmcalls[0]))
976 return (EINVAL);
977 mtx_lock(&Giant);
978 error = (*shmcalls[uap->which])(td, &uap->a2);
979 mtx_unlock(&Giant);
980 return (error);
981}
982
983SYSCALL_MODULE_HELPER(shmsys);
984#endif /* i386 && (COMPAT_FREEBSD4 || COMPAT_43) */
985
986static int
995sysvshm_modload(struct module *module, int cmd, void *arg)
996{
997 int error = 0;
998
999 switch (cmd) {
1000 case MOD_LOAD:
1001 shminit();
1002 break;

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

1013}
1014
1015static moduledata_t sysvshm_mod = {
1016 "sysvshm",
1017 &sysvshm_modload,
1018 NULL
1019};
1020
987sysvshm_modload(struct module *module, int cmd, void *arg)
988{
989 int error = 0;
990
991 switch (cmd) {
992 case MOD_LOAD:
993 shminit();
994 break;

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

1005}
1006
1007static moduledata_t sysvshm_mod = {
1008 "sysvshm",
1009 &sysvshm_modload,
1010 NULL
1011};
1012
1021SYSCALL_MODULE_HELPER(shmsys);
1022SYSCALL_MODULE_HELPER(shmat);
1023SYSCALL_MODULE_HELPER(shmctl);
1024SYSCALL_MODULE_HELPER(shmdt);
1025SYSCALL_MODULE_HELPER(shmget);
1026
1027DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1028MODULE_VERSION(sysvshm, 1);
1013SYSCALL_MODULE_HELPER(shmat);
1014SYSCALL_MODULE_HELPER(shmctl);
1015SYSCALL_MODULE_HELPER(shmdt);
1016SYSCALL_MODULE_HELPER(shmget);
1017
1018DECLARE_MODULE(sysvshm, sysvshm_mod, SI_SUB_SYSV_SHM, SI_ORDER_FIRST);
1019MODULE_VERSION(sysvshm, 1);