]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
2 * Copyright (C) 1996-2016 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"
17 #include "sbuf/SBuf.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 // Why a brand new segment? A Squid crash may leave a reusable segment, but
92 // our placement-new code requires an all-0s segment. We could truncate and
93 // resize the old segment, but OS X does not allow using O_TRUNC with
94 // shm_open() and does not support ftruncate() for old segments.
95 if (!createFresh() && errno
== EEXIST
) {
101 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
102 fatalf("Ipc::Mem::Segment::create failed to shm_open(%s): %s\n",
103 theName
.termedBuf(), xstrerror());
106 if (ftruncate(theFD
, aSize
)) {
107 const int savedError
= errno
;
109 debugs(54, 5, HERE
<< "ftruncate " << theName
<< ": " << xstrerr(savedError
));
110 fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n",
111 theName
.termedBuf(), xstrerr(savedError
));
113 // We assume that the shm_open(O_CREAT)+ftruncate() combo zeros the segment.
115 theSize
= statSize("Ipc::Mem::Segment::create");
117 // OS X will round up to a full page, so not checking for exact size match.
118 assert(theSize
>= aSize
);
123 debugs(54, 3, HERE
<< "created " << theName
<< " segment: " << theSize
);
129 Ipc::Mem::Segment::open()
133 theFD
= shm_open(theName
.termedBuf(), O_RDWR
, 0);
135 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
136 fatalf("Ipc::Mem::Segment::open failed to shm_open(%s): %s\n",
137 theName
.termedBuf(), xstrerror());
140 theSize
= statSize("Ipc::Mem::Segment::open");
142 debugs(54, 3, HERE
<< "opened " << theName
<< " segment: " << theSize
);
147 /// Creates a brand new shared memory segment and returns true.
148 /// Fails and returns false if there exist an old segment with the same name.
150 Ipc::Mem::Segment::createFresh()
152 theFD
= shm_open(theName
.termedBuf(),
153 O_EXCL
| O_CREAT
| O_RDWR
,
158 /// Map the shared memory segment to the process memory space.
160 Ipc::Mem::Segment::attach()
165 // mmap() accepts size_t for the size; we give it off_t which might
166 // be bigger; assert overflows until we support multiple mmap()s?
167 assert(theSize
== static_cast<off_t
>(static_cast<size_t>(theSize
)));
170 mmap(NULL
, theSize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, theFD
, 0);
171 if (p
== MAP_FAILED
) {
172 debugs(54, 5, HERE
<< "mmap " << theName
<< ": " << xstrerror());
173 fatalf("Ipc::Mem::Segment::attach failed to mmap(%s): %s\n",
174 theName
.termedBuf(), xstrerror());
179 /// Unmap the shared memory segment from the process memory space.
181 Ipc::Mem::Segment::detach()
186 if (munmap(theMem
, theSize
)) {
187 debugs(54, 5, HERE
<< "munmap " << theName
<< ": " << xstrerror());
188 fatalf("Ipc::Mem::Segment::detach failed to munmap(%s): %s\n",
189 theName
.termedBuf(), xstrerror());
195 Ipc::Mem::Segment::unlink()
197 if (shm_unlink(theName
.termedBuf()) != 0)
198 debugs(54, 5, HERE
<< "shm_unlink(" << theName
<< "): " << xstrerror());
200 debugs(54, 3, HERE
<< "unlinked " << theName
<< " segment");
203 /// determines the size of the underlying "file"
205 Ipc::Mem::Segment::statSize(const char *context
) const
210 memset(&s
, 0, sizeof(s
));
212 if (fstat(theFD
, &s
) != 0) {
213 debugs(54, 5, HERE
<< context
<< " fstat " << theName
<< ": " << xstrerror());
214 fatalf("Ipc::Mem::Segment::statSize: %s failed to fstat(%s): %s\n",
215 context
, theName
.termedBuf(), xstrerror());
221 /// Generate name for shared memory segment. Starts with a prefix required
222 /// for cross-platform portability and replaces all slashes in ID with dots.
224 Ipc::Mem::Segment::GenerateName(const char *id
)
226 assert(BasePath
&& *BasePath
);
227 static const bool nameIsPath
= shm_portable_segment_name_is_path();
230 name
.append(BasePath
);
231 if (name
[name
.size()-1] != '/')
235 name
.append(service_name
.c_str());
239 // append id, replacing slashes with dots
240 for (const char *slash
= strchr(id
, '/'); slash
; slash
= strchr(id
, '/')) {
242 name
.append(id
, slash
- id
);
249 name
.append(".shm"); // to distinguish from non-segments when nameIsPath
257 typedef std::map
<String
, Ipc::Mem::Segment
*> SegmentMap
;
258 static SegmentMap Segments
;
260 Ipc::Mem::Segment::Segment(const char *const id
):
261 theName(id
), theMem(NULL
), theSize(0), theReserved(0), doUnlink(false)
265 Ipc::Mem::Segment::~Segment()
268 delete [] static_cast<char *>(theMem
);
270 Segments
.erase(theName
);
271 debugs(54, 3, HERE
<< "unlinked " << theName
<< " fake segment");
276 Ipc::Mem::Segment::Enabled()
278 return !UsingSmp() && IamWorkerProcess();
282 Ipc::Mem::Segment::create(const off_t aSize
)
286 checkSupport("Fake segment creation");
288 const bool inserted
= Segments
.insert(std::make_pair(theName
, this)).second
;
290 fatalf("Duplicate fake segment creation: %s", theName
.termedBuf());
292 theMem
= new char[aSize
];
296 debugs(54, 3, HERE
<< "created " << theName
<< " fake segment: " << theSize
);
300 Ipc::Mem::Segment::open()
303 checkSupport("Fake segment open");
305 const SegmentMap::const_iterator i
= Segments
.find(theName
);
306 if (i
== Segments
.end())
307 fatalf("Fake segment not found: %s", theName
.termedBuf());
309 const Segment
&segment
= *i
->second
;
310 theMem
= segment
.theMem
;
311 theSize
= segment
.theSize
;
313 debugs(54, 3, HERE
<< "opened " << theName
<< " fake segment: " << theSize
);
317 Ipc::Mem::Segment::checkSupport(const char *const context
)
320 debugs(54, 5, HERE
<< context
<<
321 ": True shared memory segments are not supported. "
322 "Cannot fake shared segments in SMP config.");
323 fatalf("Ipc::Mem::Segment: Cannot fake shared segments in SMP config (%s)\n",
331 Ipc::Mem::RegisteredRunner::useConfig()
333 // If Squid is built with real segments, we create() real segments
334 // in the master process only. Otherwise, we create() fake
335 // segments in each worker process. We assume that only workers
336 // need and can work with fake segments.
338 if (IamMasterProcess())
340 if (IamWorkerProcess())
344 // we assume that master process does not need shared segments
345 // unless it is also a worker
346 if (!InDaemonMode() || !IamMasterProcess())