]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
4 * DEBUG: section 54 Interprocess Communication
10 #include "ipc/mem/Segment.h"
16 #include <sys/types.h>
19 Ipc::Mem::Segment::Segment(const char *const id
):
20 theName(GenerateName(id
)), theFD(-1), theSize(-1), theMem(NULL
)
24 Ipc::Mem::Segment::~Segment() {
27 if (close(theFD
) != 0)
28 debugs(54, 5, HERE
<< "close: " << xstrerror());
33 Ipc::Mem::Segment::create(const int aSize
)
38 theFD
= shm_open(theName
.termedBuf(), O_CREAT
| O_RDWR
| O_TRUNC
,
41 debugs(54, 5, HERE
<< "shm_open: " << xstrerror());
42 fatal("Ipc::Mem::Segment::create failed to shm_open");
45 if (ftruncate(theFD
, aSize
)) {
46 debugs(54, 5, HERE
<< "ftruncate: " << xstrerror());
47 fatal("Ipc::Mem::Segment::create failed to ftruncate");
56 Ipc::Mem::Segment::open()
60 theFD
= shm_open(theName
.termedBuf(), O_RDWR
, 0);
62 debugs(54, 5, HERE
<< "shm_open: " << xstrerror());
63 String s
= "Ipc::Mem::Segment::open failed to shm_open";
70 memset(&s
, 0, sizeof(s
));
71 if (fstat(theFD
, &s
)) {
72 debugs(54, 5, HERE
<< "fstat: " << xstrerror());
73 String s
= "Ipc::Mem::Segment::open failed to fstat";
84 /// Map the shared memory segment to the process memory space.
86 Ipc::Mem::Segment::attach()
93 mmap(NULL
, theSize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, theFD
, 0);
94 if (p
== MAP_FAILED
) {
95 debugs(54, 5, HERE
<< "mmap: " << xstrerror());
96 fatal("Ipc::Mem::Segment::attach failed to mmap");
101 /// Unmap the shared memory segment from the process memory space.
103 Ipc::Mem::Segment::detach()
108 if (munmap(theMem
, theSize
)) {
109 debugs(54, 5, HERE
<< "munmap: " << xstrerror());
110 fatal("Ipc::Mem::Segment::detach failed to munmap");
115 /// Generate name for shared memory segment. Replaces all slashes with dots.
117 Ipc::Mem::Segment::GenerateName(const char *id
)
119 String
name("/squid-");
120 for (const char *slash
= strchr(id
, '/'); slash
; slash
= strchr(id
, '/')) {
122 name
.append(id
, slash
- id
);