]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
2 * Copyright (C) 1996-2020 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"
18 #include "SquidConfig.h"
34 // test cases change this
35 const char *Ipc::Mem::Segment::BasePath
= DEFAULT_STATEDIR
;
38 Ipc::Mem::Segment::reserve(size_t chunkSize
)
41 // check for overflows
42 // chunkSize >= 0 may result in warnings on systems where off_t is unsigned
43 assert(!chunkSize
|| static_cast<off_t
>(chunkSize
) > 0);
44 assert(static_cast<off_t
>(chunkSize
) <= theSize
);
45 assert(theReserved
<= theSize
- static_cast<off_t
>(chunkSize
));
46 void *result
= reinterpret_cast<char*>(theMem
) + theReserved
;
47 theReserved
+= chunkSize
;
52 Ipc::Mem::Segment::Name(const SBuf
&prefix
, const char *suffix
)
56 result
.append(suffix
);
62 Ipc::Mem::Segment::Segment(const char *const id
):
63 theFD(-1), theName(GenerateName(id
)), theMem(NULL
),
64 theSize(0), theReserved(0), doUnlink(false)
68 Ipc::Mem::Segment::~Segment()
72 if (close(theFD
) != 0) {
74 debugs(54, 5, "close " << theName
<< ": " << xstrerr(xerrno
));
81 // fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective
83 Ipc::Mem::Segment::Enabled()
89 Ipc::Mem::Segment::create(const off_t aSize
)
96 // Why a brand new segment? A Squid crash may leave a reusable segment, but
97 // our placement-new code requires an all-0s segment. We could truncate and
98 // resize the old segment, but OS X does not allow using O_TRUNC with
99 // shm_open() and does not support ftruncate() for old segments.
100 if (!createFresh(xerrno
) && xerrno
== EEXIST
) {
106 debugs(54, 5, "shm_open " << theName
<< ": " << xstrerr(xerrno
));
107 fatalf("Ipc::Mem::Segment::create failed to shm_open(%s): %s\n",
108 theName
.termedBuf(), xstrerr(xerrno
));
111 if (ftruncate(theFD
, aSize
)) {
114 debugs(54, 5, "ftruncate " << theName
<< ": " << xstrerr(xerrno
));
115 fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n",
116 theName
.termedBuf(), xstrerr(xerrno
));
118 // We assume that the shm_open(O_CREAT)+ftruncate() combo zeros the segment.
120 theSize
= statSize("Ipc::Mem::Segment::create");
122 // OS X will round up to a full page, so not checking for exact size match.
123 assert(theSize
>= aSize
);
128 debugs(54, 3, "created " << theName
<< " segment: " << theSize
);
133 Ipc::Mem::Segment::open()
137 theFD
= shm_open(theName
.termedBuf(), O_RDWR
, 0);
140 debugs(54, 5, "shm_open " << theName
<< ": " << xstrerr(xerrno
));
141 fatalf("Ipc::Mem::Segment::open failed to shm_open(%s): %s\n",
142 theName
.termedBuf(), xstrerr(xerrno
));
145 theSize
= statSize("Ipc::Mem::Segment::open");
147 debugs(54, 3, HERE
<< "opened " << theName
<< " segment: " << theSize
);
152 /// Creates a brand new shared memory segment and returns true.
153 /// Fails and returns false if there exist an old segment with the same name.
155 Ipc::Mem::Segment::createFresh(int &xerrno
)
157 theFD
= shm_open(theName
.termedBuf(),
158 O_EXCL
| O_CREAT
| O_RDWR
,
164 /// Map the shared memory segment to the process memory space.
166 Ipc::Mem::Segment::attach()
171 // mmap() accepts size_t for the size; we give it off_t which might
172 // be bigger; assert overflows until we support multiple mmap()s?
173 assert(theSize
== static_cast<off_t
>(static_cast<size_t>(theSize
)));
176 mmap(NULL
, theSize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, theFD
, 0);
177 if (p
== MAP_FAILED
) {
179 debugs(54, 5, "mmap " << theName
<< ": " << xstrerr(xerrno
));
180 fatalf("Ipc::Mem::Segment::attach failed to mmap(%s): %s\n",
181 theName
.termedBuf(), xstrerr(xerrno
));
188 /// Unmap the shared memory segment from the process memory space.
190 Ipc::Mem::Segment::detach()
195 if (munmap(theMem
, theSize
)) {
197 debugs(54, 5, "munmap " << theName
<< ": " << xstrerr(xerrno
));
198 fatalf("Ipc::Mem::Segment::detach failed to munmap(%s): %s\n",
199 theName
.termedBuf(), xstrerr(xerrno
));
204 /// Lock the segment into RAM, ensuring that the OS has enough RAM for it [now]
205 /// and preventing segment bytes from being swapped out to disk later by the OS.
207 Ipc::Mem::Segment::lock()
209 if (!Config
.shmLocking
) {
210 debugs(54, 5, "mlock(2)-ing disabled");
214 #if defined(_POSIX_MEMLOCK_RANGE)
215 debugs(54, 7, "mlock(" << theName
<< ',' << theSize
<< ") starts");
216 if (mlock(theMem
, theSize
) != 0) {
217 const int savedError
= errno
;
218 fatalf("shared_memory_locking on but failed to mlock(%s, %" PRId64
"): %s\n",
219 theName
.termedBuf(),static_cast<int64_t>(theSize
), xstrerr(savedError
));
221 // TODO: Warn if it took too long.
222 debugs(54, 7, "mlock(" << theName
<< ',' << theSize
<< ") OK");
224 debugs(54, 5, "insufficient mlock(2) support");
225 if (Config
.shmLocking
.configured()) { // set explicitly
226 static bool warnedOnce
= false;
228 debugs(54, DBG_IMPORTANT
, "ERROR: insufficient mlock(2) support prevents " <<
229 "honoring `shared_memory_locking on`. " <<
230 "If you lack RAM, kernel will kill Squid later.");
238 Ipc::Mem::Segment::unlink()
240 if (shm_unlink(theName
.termedBuf()) != 0) {
242 debugs(54, 5, "shm_unlink(" << theName
<< "): " << xstrerr(xerrno
));
244 debugs(54, 3, "unlinked " << theName
<< " segment");
247 /// determines the size of the underlying "file"
249 Ipc::Mem::Segment::statSize(const char *context
) const
254 memset(&s
, 0, sizeof(s
));
256 if (fstat(theFD
, &s
) != 0) {
258 debugs(54, 5, context
<< " fstat " << theName
<< ": " << xstrerr(xerrno
));
259 fatalf("Ipc::Mem::Segment::statSize: %s failed to fstat(%s): %s\n",
260 context
, theName
.termedBuf(), xstrerr(xerrno
));
266 /// Generate name for shared memory segment. Starts with a prefix required
267 /// for cross-platform portability and replaces all slashes in ID with dots.
269 Ipc::Mem::Segment::GenerateName(const char *id
)
271 assert(BasePath
&& *BasePath
);
272 static const bool nameIsPath
= shm_portable_segment_name_is_path();
275 name
.append(BasePath
);
276 if (name
[name
.size()-1] != '/')
280 name
.append(service_name
.c_str());
284 // append id, replacing slashes with dots
285 for (const char *slash
= strchr(id
, '/'); slash
; slash
= strchr(id
, '/')) {
287 name
.append(id
, slash
- id
);
294 name
.append(".shm"); // to distinguish from non-segments when nameIsPath
302 typedef std::map
<String
, Ipc::Mem::Segment
*> SegmentMap
;
303 static SegmentMap Segments
;
305 Ipc::Mem::Segment::Segment(const char *const id
):
306 theName(id
), theMem(NULL
), theSize(0), theReserved(0), doUnlink(false)
310 Ipc::Mem::Segment::~Segment()
313 delete [] static_cast<char *>(theMem
);
315 Segments
.erase(theName
);
316 debugs(54, 3, HERE
<< "unlinked " << theName
<< " fake segment");
321 Ipc::Mem::Segment::Enabled()
323 return !UsingSmp() && IamWorkerProcess();
327 Ipc::Mem::Segment::create(const off_t aSize
)
331 checkSupport("Fake segment creation");
333 const bool inserted
= Segments
.insert(std::make_pair(theName
, this)).second
;
335 fatalf("Duplicate fake segment creation: %s", theName
.termedBuf());
337 theMem
= new char[aSize
];
341 debugs(54, 3, HERE
<< "created " << theName
<< " fake segment: " << theSize
);
345 Ipc::Mem::Segment::open()
348 checkSupport("Fake segment open");
350 const SegmentMap::const_iterator i
= Segments
.find(theName
);
351 if (i
== Segments
.end())
352 fatalf("Fake segment not found: %s", theName
.termedBuf());
354 const Segment
&segment
= *i
->second
;
355 theMem
= segment
.theMem
;
356 theSize
= segment
.theSize
;
358 debugs(54, 3, HERE
<< "opened " << theName
<< " fake segment: " << theSize
);
362 Ipc::Mem::Segment::checkSupport(const char *const context
)
365 debugs(54, 5, HERE
<< context
<<
366 ": True shared memory segments are not supported. "
367 "Cannot fake shared segments in SMP config.");
368 fatalf("Ipc::Mem::Segment: Cannot fake shared segments in SMP config (%s)\n",
376 Ipc::Mem::RegisteredRunner::useConfig()
378 // If Squid is built with real segments, we create() real segments
379 // in the master process only. Otherwise, we create() fake
380 // segments in each worker process. We assume that only workers
381 // need and can work with fake segments.
383 if (IamMasterProcess())
385 if (IamWorkerProcess())
389 // we assume that master process does not need shared segments
390 // unless it is also a worker
391 if (!InDaemonMode() || !IamMasterProcess())