]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 54 Interprocess Communication */
12 #include "base/TextException.h"
13 #include "compat/shm.h"
16 #include "ipc/mem/Segment.h"
33 // test cases change this
34 const char *Ipc::Mem::Segment::BasePath
= DEFAULT_STATEDIR
;
37 Ipc::Mem::Segment::reserve(size_t chunkSize
)
40 // check for overflows
41 // chunkSize >= 0 may result in warnings on systems where off_t is unsigned
42 assert(!chunkSize
|| static_cast<off_t
>(chunkSize
) > 0);
43 assert(static_cast<off_t
>(chunkSize
) <= theSize
);
44 assert(theReserved
<= theSize
- static_cast<off_t
>(chunkSize
));
45 void *result
= reinterpret_cast<char*>(theMem
) + theReserved
;
46 theReserved
+= chunkSize
;
51 Ipc::Mem::Segment::Name(const SBuf
&prefix
, const char *suffix
)
55 result
.append(suffix
);
61 Ipc::Mem::Segment::Segment(const char *const id
):
62 theFD(-1), theName(GenerateName(id
)), theMem(NULL
),
63 theSize(0), theReserved(0), doUnlink(false)
67 Ipc::Mem::Segment::~Segment()
71 if (close(theFD
) != 0)
72 debugs(54, 5, HERE
<< "close " << theName
<< ": " << xstrerror());
78 // fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective
80 Ipc::Mem::Segment::Enabled()
86 Ipc::Mem::Segment::create(const off_t aSize
)
91 theFD
= shm_open(theName
.termedBuf(), O_CREAT
| O_RDWR
| O_TRUNC
,
94 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
95 fatalf("Ipc::Mem::Segment::create failed to shm_open(%s): %s\n",
96 theName
.termedBuf(), xstrerror());
99 if (ftruncate(theFD
, aSize
)) {
100 debugs(54, 5, HERE
<< "ftruncate " << theName
<< ": " << xstrerror());
101 fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n",
102 theName
.termedBuf(), xstrerror());
105 assert(statSize("Ipc::Mem::Segment::create") == aSize
); // paranoid
111 debugs(54, 3, HERE
<< "created " << theName
<< " segment: " << theSize
);
117 Ipc::Mem::Segment::open()
121 theFD
= shm_open(theName
.termedBuf(), O_RDWR
, 0);
123 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
124 fatalf("Ipc::Mem::Segment::open failed to shm_open(%s): %s\n",
125 theName
.termedBuf(), xstrerror());
128 theSize
= statSize("Ipc::Mem::Segment::open");
130 debugs(54, 3, HERE
<< "opened " << theName
<< " segment: " << theSize
);
135 /// Map the shared memory segment to the process memory space.
137 Ipc::Mem::Segment::attach()
142 // mmap() accepts size_t for the size; we give it off_t which might
143 // be bigger; assert overflows until we support multiple mmap()s?
144 assert(theSize
== static_cast<off_t
>(static_cast<size_t>(theSize
)));
147 mmap(NULL
, theSize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, theFD
, 0);
148 if (p
== MAP_FAILED
) {
149 debugs(54, 5, HERE
<< "mmap " << theName
<< ": " << xstrerror());
150 fatalf("Ipc::Mem::Segment::attach failed to mmap(%s): %s\n",
151 theName
.termedBuf(), xstrerror());
156 /// Unmap the shared memory segment from the process memory space.
158 Ipc::Mem::Segment::detach()
163 if (munmap(theMem
, theSize
)) {
164 debugs(54, 5, HERE
<< "munmap " << theName
<< ": " << xstrerror());
165 fatalf("Ipc::Mem::Segment::detach failed to munmap(%s): %s\n",
166 theName
.termedBuf(), xstrerror());
172 Ipc::Mem::Segment::unlink()
174 if (shm_unlink(theName
.termedBuf()) != 0)
175 debugs(54, 5, HERE
<< "shm_unlink(" << theName
<< "): " << xstrerror());
177 debugs(54, 3, HERE
<< "unlinked " << theName
<< " segment");
180 /// determines the size of the underlying "file"
182 Ipc::Mem::Segment::statSize(const char *context
) const
187 memset(&s
, 0, sizeof(s
));
189 if (fstat(theFD
, &s
) != 0) {
190 debugs(54, 5, HERE
<< context
<< " fstat " << theName
<< ": " << xstrerror());
191 fatalf("Ipc::Mem::Segment::statSize: %s failed to fstat(%s): %s\n",
192 context
, theName
.termedBuf(), xstrerror());
198 /// Generate name for shared memory segment. Starts with a prefix required
199 /// for cross-platform portability and replaces all slashes in ID with dots.
201 Ipc::Mem::Segment::GenerateName(const char *id
)
203 assert(BasePath
&& *BasePath
);
204 static const bool nameIsPath
= shm_portable_segment_name_is_path();
207 name
.append(BasePath
);
208 if (name
[name
.size()-1] != '/')
211 name
.append("/squid-");
213 // append id, replacing slashes with dots
214 for (const char *slash
= strchr(id
, '/'); slash
; slash
= strchr(id
, '/')) {
216 name
.append(id
, slash
- id
);
223 name
.append(".shm"); // to distinguish from non-segments when nameIsPath
231 typedef std::map
<String
, Ipc::Mem::Segment
*> SegmentMap
;
232 static SegmentMap Segments
;
234 Ipc::Mem::Segment::Segment(const char *const id
):
235 theName(id
), theMem(NULL
), theSize(0), theReserved(0), doUnlink(false)
239 Ipc::Mem::Segment::~Segment()
242 delete [] static_cast<char *>(theMem
);
244 Segments
.erase(theName
);
245 debugs(54, 3, HERE
<< "unlinked " << theName
<< " fake segment");
250 Ipc::Mem::Segment::Enabled()
252 return !UsingSmp() && IamWorkerProcess();
256 Ipc::Mem::Segment::create(const off_t aSize
)
260 checkSupport("Fake segment creation");
262 const bool inserted
= Segments
.insert(std::make_pair(theName
, this)).second
;
264 fatalf("Duplicate fake segment creation: %s", theName
.termedBuf());
266 theMem
= new char[aSize
];
270 debugs(54, 3, HERE
<< "created " << theName
<< " fake segment: " << theSize
);
274 Ipc::Mem::Segment::open()
277 checkSupport("Fake segment open");
279 const SegmentMap::const_iterator i
= Segments
.find(theName
);
280 if (i
== Segments
.end())
281 fatalf("Fake segment not found: %s", theName
.termedBuf());
283 const Segment
&segment
= *i
->second
;
284 theMem
= segment
.theMem
;
285 theSize
= segment
.theSize
;
287 debugs(54, 3, HERE
<< "opened " << theName
<< " fake segment: " << theSize
);
291 Ipc::Mem::Segment::checkSupport(const char *const context
)
294 debugs(54, 5, HERE
<< context
<<
295 ": True shared memory segments are not supported. "
296 "Cannot fake shared segments in SMP config.");
297 fatalf("Ipc::Mem::Segment: Cannot fake shared segments in SMP config (%s)\n",
305 Ipc::Mem::RegisteredRunner::useConfig()
307 // If Squid is built with real segments, we create() real segments
308 // in the master process only. Otherwise, we create() fake
309 // segments in each worker process. We assume that only workers
310 // need and can work with fake segments.
312 if (IamMasterProcess())
314 if (IamWorkerProcess())
318 // we assume that master process does not need shared segments
319 // unless it is also a worker
320 if (!InDaemonMode() || !IamMasterProcess())