]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
4 * DEBUG: section 54 Interprocess Communication
9 #include "base/TextException.h"
10 #include "compat/shm.h"
11 #include "ipc/mem/Segment.h"
20 // test cases change this
21 const char *Ipc::Mem::Segment::BasePath
= DEFAULT_STATEDIR
;
24 Ipc::Mem::Segment::reserve(size_t chunkSize
)
27 // check for overflows
28 // chunkSize >= 0 may result in warnings on systems where off_t is unsigned
29 assert(!chunkSize
|| static_cast<off_t
>(chunkSize
) > 0);
30 assert(static_cast<off_t
>(chunkSize
) <= theSize
);
31 assert(theReserved
<= theSize
- static_cast<off_t
>(chunkSize
));
32 void *result
= reinterpret_cast<char*>(theMem
) + theReserved
;
33 theReserved
+= chunkSize
;
39 Ipc::Mem::Segment::Segment(const char *const id
):
40 theFD(-1), theName(GenerateName(id
)), theMem(NULL
),
41 theSize(0), theReserved(0), doUnlink(false)
45 Ipc::Mem::Segment::~Segment()
49 if (close(theFD
) != 0)
50 debugs(54, 5, HERE
<< "close " << theName
<< ": " << xstrerror());
56 // fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective
58 Ipc::Mem::Segment::Enabled()
64 Ipc::Mem::Segment::create(const off_t aSize
)
69 theFD
= shm_open(theName
.termedBuf(), O_CREAT
| O_RDWR
| O_TRUNC
,
72 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
73 fatalf("Ipc::Mem::Segment::create failed to shm_open(%s): %s\n",
74 theName
.termedBuf(), xstrerror());
77 if (ftruncate(theFD
, aSize
)) {
78 debugs(54, 5, HERE
<< "ftruncate " << theName
<< ": " << xstrerror());
79 fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n",
80 theName
.termedBuf(), xstrerror());
83 assert(statSize("Ipc::Mem::Segment::create") == aSize
); // paranoid
89 debugs(54, 3, HERE
<< "created " << theName
<< " segment: " << theSize
);
95 Ipc::Mem::Segment::open()
99 theFD
= shm_open(theName
.termedBuf(), O_RDWR
, 0);
101 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
102 fatalf("Ipc::Mem::Segment::open failed to shm_open(%s): %s\n",
103 theName
.termedBuf(), xstrerror());
106 theSize
= statSize("Ipc::Mem::Segment::open");
108 debugs(54, 3, HERE
<< "opened " << theName
<< " segment: " << theSize
);
113 /// Map the shared memory segment to the process memory space.
115 Ipc::Mem::Segment::attach()
120 // mmap() accepts size_t for the size; we give it off_t which might
121 // be bigger; assert overflows until we support multiple mmap()s?
122 assert(theSize
== static_cast<off_t
>(static_cast<size_t>(theSize
)));
125 mmap(NULL
, theSize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, theFD
, 0);
126 if (p
== MAP_FAILED
) {
127 debugs(54, 5, HERE
<< "mmap " << theName
<< ": " << xstrerror());
128 fatalf("Ipc::Mem::Segment::attach failed to mmap(%s): %s\n",
129 theName
.termedBuf(), xstrerror());
134 /// Unmap the shared memory segment from the process memory space.
136 Ipc::Mem::Segment::detach()
141 if (munmap(theMem
, theSize
)) {
142 debugs(54, 5, HERE
<< "munmap " << theName
<< ": " << xstrerror());
143 fatalf("Ipc::Mem::Segment::detach failed to munmap(%s): %s\n",
144 theName
.termedBuf(), xstrerror());
150 Ipc::Mem::Segment::unlink()
152 if (shm_unlink(theName
.termedBuf()) != 0)
153 debugs(54, 5, HERE
<< "shm_unlink(" << theName
<< "): " << xstrerror());
155 debugs(54, 3, HERE
<< "unlinked " << theName
<< " segment");
158 /// determines the size of the underlying "file"
160 Ipc::Mem::Segment::statSize(const char *context
) const
165 memset(&s
, 0, sizeof(s
));
167 if (fstat(theFD
, &s
) != 0) {
168 debugs(54, 5, HERE
<< context
<< " fstat " << theName
<< ": " << xstrerror());
169 fatalf("Ipc::Mem::Segment::statSize: %s failed to fstat(%s): %s\n",
170 context
, theName
.termedBuf(), xstrerror());
176 /// Generate name for shared memory segment. Starts with a prefix required
177 /// for cross-platform portability and replaces all slashes in ID with dots.
179 Ipc::Mem::Segment::GenerateName(const char *id
)
182 static const bool nameIsPath
= shm_portable_segment_name_is_path();
183 String
name(nameIsPath
? BasePath
: "/squid-");
185 // append id, replacing slashes with dots
186 for (const char *slash
= strchr(id
, '/'); slash
; slash
= strchr(id
, '/')) {
188 name
.append(id
, slash
- id
);
195 name
.append(".shm"); // to distinguish from non-segments when nameIsPath
203 typedef std::map
<String
, Ipc::Mem::Segment
*> SegmentMap
;
204 static SegmentMap Segments
;
206 Ipc::Mem::Segment::Segment(const char *const id
):
207 theName(id
), theMem(NULL
), theSize(0), theReserved(0), doUnlink(false)
211 Ipc::Mem::Segment::~Segment()
214 delete [] static_cast<char *>(theMem
);
216 Segments
.erase(theName
);
217 debugs(54, 3, HERE
<< "unlinked " << theName
<< " fake segment");
222 Ipc::Mem::Segment::Enabled()
224 return !UsingSmp() && IamWorkerProcess();
228 Ipc::Mem::Segment::create(const off_t aSize
)
232 checkSupport("Fake segment creation");
234 const bool inserted
= Segments
.insert(std::make_pair(theName
, this)).second
;
236 fatalf("Duplicate fake segment creation: %s", theName
.termedBuf());
238 theMem
= new char[aSize
];
242 debugs(54, 3, HERE
<< "created " << theName
<< " fake segment: " << theSize
);
246 Ipc::Mem::Segment::open()
249 checkSupport("Fake segment open");
251 const SegmentMap::const_iterator i
= Segments
.find(theName
);
252 if (i
== Segments
.end())
253 fatalf("Fake segment not found: %s", theName
.termedBuf());
255 const Segment
&segment
= *i
->second
;
256 theMem
= segment
.theMem
;
257 theSize
= segment
.theSize
;
259 debugs(54, 3, HERE
<< "opened " << theName
<< " fake segment: " << theSize
);
263 Ipc::Mem::Segment::checkSupport(const char *const context
)
266 debugs(54, 5, HERE
<< context
<<
267 ": True shared memory segments are not supported. "
268 "Cannot fake shared segments in SMP config.");
269 fatalf("Ipc::Mem::Segment: Cannot fake shared segments in SMP config (%s)\n",
277 Ipc::Mem::RegisteredRunner::run(const RunnerRegistry
&r
)
279 // If Squid is built with real segments, we create() real segments
280 // in the master process only. Otherwise, we create() fake
281 // segments in each worker process. We assume that only workers
282 // need and can work with fake segments.
284 if (IamMasterProcess())
286 if (IamWorkerProcess())
290 // we assume that master process does not need shared segments
291 // unless it is also a worker
292 if (!InDaemonMode() || !IamMasterProcess())