]>
Commit | Line | Data |
---|---|---|
f5eef98c | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
f5eef98c | 3 | * |
bbc27441 AJ |
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. | |
f5eef98c DK |
7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 54 Interprocess Communication */ |
10 | ||
f7f3304a | 11 | #include "squid.h" |
e08b0f77 | 12 | #include "base/TextException.h" |
c975f532 | 13 | #include "compat/shm.h" |
675b8408 | 14 | #include "debug/Stream.h" |
af69c635 | 15 | #include "fatal.h" |
d8a82533 | 16 | #include "ipc/mem/Segment.h" |
65e41a45 | 17 | #include "sbuf/SBuf.h" |
c756d517 | 18 | #include "SquidConfig.h" |
5bed43d6 | 19 | #include "tools.h" |
f5eef98c | 20 | |
7e851a39 | 21 | #if HAVE_FCNTL_H |
f5eef98c | 22 | #include <fcntl.h> |
7e851a39 AJ |
23 | #endif |
24 | #if HAVE_SYS_MMAN_H | |
f5eef98c | 25 | #include <sys/mman.h> |
7e851a39 AJ |
26 | #endif |
27 | #if HAVE_SYS_STAT_H | |
f5eef98c | 28 | #include <sys/stat.h> |
7e851a39 AJ |
29 | #endif |
30 | #if HAVE_UNISTD_H | |
f5eef98c | 31 | #include <unistd.h> |
7e851a39 | 32 | #endif |
f5eef98c | 33 | |
ef8de464 AR |
34 | // test cases change this |
35 | const char *Ipc::Mem::Segment::BasePath = DEFAULT_STATEDIR; | |
36 | ||
be9cb908 DK |
37 | void * |
38 | Ipc::Mem::Segment::reserve(size_t chunkSize) | |
39 | { | |
40 | Must(theMem); | |
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; | |
48 | return result; | |
49 | } | |
50 | ||
1860fbac AR |
51 | SBuf |
52 | Ipc::Mem::Segment::Name(const SBuf &prefix, const char *suffix) | |
53 | { | |
54 | SBuf result = prefix; | |
55 | result.append("_"); | |
56 | result.append(suffix); | |
57 | return result; | |
58 | } | |
59 | ||
be9cb908 DK |
60 | #if HAVE_SHM |
61 | ||
d8a82533 | 62 | Ipc::Mem::Segment::Segment(const char *const id): |
aee3523a | 63 | theFD(-1), theName(GenerateName(id)), theMem(nullptr), |
f53969cc | 64 | theSize(0), theReserved(0), doUnlink(false) |
f5eef98c DK |
65 | { |
66 | } | |
67 | ||
9199139f AR |
68 | Ipc::Mem::Segment::~Segment() |
69 | { | |
f5eef98c DK |
70 | if (theFD >= 0) { |
71 | detach(); | |
b69e9ffa AJ |
72 | if (close(theFD) != 0) { |
73 | int xerrno = errno; | |
74 | debugs(54, 5, "close " << theName << ": " << xstrerr(xerrno)); | |
75 | } | |
f5eef98c | 76 | } |
68353d5a DK |
77 | if (doUnlink) |
78 | unlink(); | |
f5eef98c DK |
79 | } |
80 | ||
be9cb908 | 81 | // fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective |
c975f532 | 82 | bool |
9199139f AR |
83 | Ipc::Mem::Segment::Enabled() |
84 | { | |
c975f532 | 85 | return true; |
c975f532 DK |
86 | } |
87 | ||
f5eef98c | 88 | void |
e08b0f77 | 89 | Ipc::Mem::Segment::create(const off_t aSize) |
f5eef98c DK |
90 | { |
91 | assert(aSize > 0); | |
92 | assert(theFD < 0); | |
93 | ||
45a6b2c8 | 94 | int xerrno = 0; |
b69e9ffa | 95 | |
f58b1eb4 MM |
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. | |
45a6b2c8 | 100 | if (!createFresh(xerrno) && xerrno == EEXIST) { |
f58b1eb4 | 101 | unlink(); |
45a6b2c8 | 102 | createFresh(xerrno); |
f58b1eb4 MM |
103 | } |
104 | ||
f5eef98c | 105 | if (theFD < 0) { |
b69e9ffa | 106 | debugs(54, 5, "shm_open " << theName << ": " << xstrerr(xerrno)); |
77915f06 | 107 | fatalf("Ipc::Mem::Segment::create failed to shm_open(%s): %s\n", |
b69e9ffa | 108 | theName.termedBuf(), xstrerr(xerrno)); |
f5eef98c DK |
109 | } |
110 | ||
111 | if (ftruncate(theFD, aSize)) { | |
45a6b2c8 | 112 | xerrno = errno; |
62fa1d73 | 113 | unlink(); |
45a6b2c8 | 114 | debugs(54, 5, "ftruncate " << theName << ": " << xstrerr(xerrno)); |
b46ae525 | 115 | fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n", |
45a6b2c8 | 116 | theName.termedBuf(), xstrerr(xerrno)); |
f5eef98c | 117 | } |
62fa1d73 FC |
118 | // We assume that the shm_open(O_CREAT)+ftruncate() combo zeros the segment. |
119 | ||
120 | theSize = statSize("Ipc::Mem::Segment::create"); | |
f5eef98c | 121 | |
62fa1d73 FC |
122 | // OS X will round up to a full page, so not checking for exact size match. |
123 | assert(theSize >= aSize); | |
e08b0f77 | 124 | |
937d0d3f | 125 | theReserved = 0; |
68353d5a | 126 | doUnlink = true; |
f5eef98c | 127 | |
45a6b2c8 | 128 | debugs(54, 3, "created " << theName << " segment: " << theSize); |
f5eef98c DK |
129 | attach(); |
130 | } | |
131 | ||
132 | void | |
8ecbe78d | 133 | Ipc::Mem::Segment::open(const bool unlinkWhenDone) |
f5eef98c DK |
134 | { |
135 | assert(theFD < 0); | |
136 | ||
137 | theFD = shm_open(theName.termedBuf(), O_RDWR, 0); | |
138 | if (theFD < 0) { | |
b69e9ffa AJ |
139 | int xerrno = errno; |
140 | debugs(54, 5, "shm_open " << theName << ": " << xstrerr(xerrno)); | |
77915f06 | 141 | fatalf("Ipc::Mem::Segment::open failed to shm_open(%s): %s\n", |
b69e9ffa | 142 | theName.termedBuf(), xstrerr(xerrno)); |
f5eef98c DK |
143 | } |
144 | ||
e08b0f77 | 145 | theSize = statSize("Ipc::Mem::Segment::open"); |
8ecbe78d | 146 | doUnlink = unlinkWhenDone; |
f5eef98c | 147 | |
bf95c10a | 148 | debugs(54, 3, "opened " << theName << " segment: " << theSize); |
446485d5 | 149 | |
f5eef98c DK |
150 | attach(); |
151 | } | |
152 | ||
f58b1eb4 MM |
153 | /// Creates a brand new shared memory segment and returns true. |
154 | /// Fails and returns false if there exist an old segment with the same name. | |
155 | bool | |
45a6b2c8 | 156 | Ipc::Mem::Segment::createFresh(int &xerrno) |
f58b1eb4 MM |
157 | { |
158 | theFD = shm_open(theName.termedBuf(), | |
159 | O_EXCL | O_CREAT | O_RDWR, | |
160 | S_IRUSR | S_IWUSR); | |
45a6b2c8 | 161 | xerrno = errno; |
f58b1eb4 MM |
162 | return theFD >= 0; |
163 | } | |
164 | ||
f5eef98c DK |
165 | /// Map the shared memory segment to the process memory space. |
166 | void | |
d8a82533 | 167 | Ipc::Mem::Segment::attach() |
f5eef98c DK |
168 | { |
169 | assert(theFD >= 0); | |
f5eef98c DK |
170 | assert(!theMem); |
171 | ||
3a2ede14 AR |
172 | // mmap() accepts size_t for the size; we give it off_t which might |
173 | // be bigger; assert overflows until we support multiple mmap()s? | |
174 | assert(theSize == static_cast<off_t>(static_cast<size_t>(theSize))); | |
175 | ||
f5eef98c | 176 | void *const p = |
aee3523a | 177 | mmap(nullptr, theSize, PROT_READ | PROT_WRITE, MAP_SHARED, theFD, 0); |
f5eef98c | 178 | if (p == MAP_FAILED) { |
b69e9ffa AJ |
179 | int xerrno = errno; |
180 | debugs(54, 5, "mmap " << theName << ": " << xstrerr(xerrno)); | |
b46ae525 | 181 | fatalf("Ipc::Mem::Segment::attach failed to mmap(%s): %s\n", |
b69e9ffa | 182 | theName.termedBuf(), xstrerr(xerrno)); |
f5eef98c DK |
183 | } |
184 | theMem = p; | |
c756d517 AR |
185 | |
186 | lock(); | |
f5eef98c DK |
187 | } |
188 | ||
189 | /// Unmap the shared memory segment from the process memory space. | |
190 | void | |
d8a82533 | 191 | Ipc::Mem::Segment::detach() |
f5eef98c DK |
192 | { |
193 | if (!theMem) | |
194 | return; | |
195 | ||
196 | if (munmap(theMem, theSize)) { | |
b69e9ffa AJ |
197 | int xerrno = errno; |
198 | debugs(54, 5, "munmap " << theName << ": " << xstrerr(xerrno)); | |
b46ae525 | 199 | fatalf("Ipc::Mem::Segment::detach failed to munmap(%s): %s\n", |
b69e9ffa | 200 | theName.termedBuf(), xstrerr(xerrno)); |
f5eef98c | 201 | } |
aee3523a | 202 | theMem = nullptr; |
f5eef98c DK |
203 | } |
204 | ||
c756d517 AR |
205 | /// Lock the segment into RAM, ensuring that the OS has enough RAM for it [now] |
206 | /// and preventing segment bytes from being swapped out to disk later by the OS. | |
207 | void | |
208 | Ipc::Mem::Segment::lock() | |
209 | { | |
210 | if (!Config.shmLocking) { | |
211 | debugs(54, 5, "mlock(2)-ing disabled"); | |
212 | return; | |
213 | } | |
214 | ||
215 | #if defined(_POSIX_MEMLOCK_RANGE) | |
216 | debugs(54, 7, "mlock(" << theName << ',' << theSize << ") starts"); | |
217 | if (mlock(theMem, theSize) != 0) { | |
218 | const int savedError = errno; | |
219 | fatalf("shared_memory_locking on but failed to mlock(%s, %" PRId64 "): %s\n", | |
90ab8f20 | 220 | theName.termedBuf(),static_cast<int64_t>(theSize), xstrerr(savedError)); |
c756d517 AR |
221 | } |
222 | // TODO: Warn if it took too long. | |
223 | debugs(54, 7, "mlock(" << theName << ',' << theSize << ") OK"); | |
224 | #else | |
225 | debugs(54, 5, "insufficient mlock(2) support"); | |
226 | if (Config.shmLocking.configured()) { // set explicitly | |
227 | static bool warnedOnce = false; | |
228 | if (!warnedOnce) { | |
229 | debugs(54, DBG_IMPORTANT, "ERROR: insufficient mlock(2) support prevents " << | |
230 | "honoring `shared_memory_locking on`. " << | |
231 | "If you lack RAM, kernel will kill Squid later."); | |
232 | warnedOnce = true; | |
233 | } | |
234 | } | |
235 | #endif | |
236 | } | |
237 | ||
68353d5a DK |
238 | void |
239 | Ipc::Mem::Segment::unlink() | |
240 | { | |
b69e9ffa AJ |
241 | if (shm_unlink(theName.termedBuf()) != 0) { |
242 | int xerrno = errno; | |
243 | debugs(54, 5, "shm_unlink(" << theName << "): " << xstrerr(xerrno)); | |
244 | } else | |
245 | debugs(54, 3, "unlinked " << theName << " segment"); | |
68353d5a DK |
246 | } |
247 | ||
e08b0f77 AR |
248 | /// determines the size of the underlying "file" |
249 | off_t | |
250 | Ipc::Mem::Segment::statSize(const char *context) const | |
251 | { | |
252 | Must(theFD >= 0); | |
253 | ||
254 | struct stat s; | |
255 | memset(&s, 0, sizeof(s)); | |
256 | ||
257 | if (fstat(theFD, &s) != 0) { | |
b69e9ffa AJ |
258 | int xerrno = errno; |
259 | debugs(54, 5, context << " fstat " << theName << ": " << xstrerr(xerrno)); | |
b46ae525 | 260 | fatalf("Ipc::Mem::Segment::statSize: %s failed to fstat(%s): %s\n", |
b69e9ffa | 261 | context, theName.termedBuf(), xstrerr(xerrno)); |
e08b0f77 AR |
262 | } |
263 | ||
264 | return s.st_size; | |
265 | } | |
266 | ||
ef8de464 AR |
267 | /// Generate name for shared memory segment. Starts with a prefix required |
268 | /// for cross-platform portability and replaces all slashes in ID with dots. | |
f5eef98c | 269 | String |
d8a82533 | 270 | Ipc::Mem::Segment::GenerateName(const char *id) |
f5eef98c | 271 | { |
38189e84 | 272 | assert(BasePath && *BasePath); |
ef8de464 | 273 | static const bool nameIsPath = shm_portable_segment_name_is_path(); |
38189e84 AJ |
274 | String name; |
275 | if (nameIsPath) { | |
276 | name.append(BasePath); | |
277 | if (name[name.size()-1] != '/') | |
278 | name.append('/'); | |
51d4bfbb JLG |
279 | } else { |
280 | name.append('/'); | |
281 | name.append(service_name.c_str()); | |
282 | name.append('-'); | |
283 | } | |
ef8de464 AR |
284 | |
285 | // append id, replacing slashes with dots | |
9a51593d DK |
286 | for (const char *slash = strchr(id, '/'); slash; slash = strchr(id, '/')) { |
287 | if (id != slash) { | |
288 | name.append(id, slash - id); | |
289 | name.append('.'); | |
290 | } | |
291 | id = slash + 1; | |
292 | } | |
f5eef98c | 293 | name.append(id); |
ef8de464 AR |
294 | |
295 | name.append(".shm"); // to distinguish from non-segments when nameIsPath | |
f5eef98c DK |
296 | return name; |
297 | } | |
be9cb908 DK |
298 | |
299 | #else // HAVE_SHM | |
300 | ||
301 | #include <map> | |
302 | ||
303 | typedef std::map<String, Ipc::Mem::Segment *> SegmentMap; | |
304 | static SegmentMap Segments; | |
305 | ||
306 | Ipc::Mem::Segment::Segment(const char *const id): | |
f53969cc | 307 | theName(id), theMem(NULL), theSize(0), theReserved(0), doUnlink(false) |
be9cb908 DK |
308 | { |
309 | } | |
310 | ||
311 | Ipc::Mem::Segment::~Segment() | |
312 | { | |
313 | if (doUnlink) { | |
314 | delete [] static_cast<char *>(theMem); | |
44c64af0 AR |
315 | theMem = NULL; |
316 | Segments.erase(theName); | |
bf95c10a | 317 | debugs(54, 3, "unlinked " << theName << " fake segment"); |
be9cb908 DK |
318 | } |
319 | } | |
320 | ||
321 | bool | |
322 | Ipc::Mem::Segment::Enabled() | |
323 | { | |
38a129ef | 324 | return !UsingSmp() && IamWorkerProcess(); |
be9cb908 DK |
325 | } |
326 | ||
327 | void | |
328 | Ipc::Mem::Segment::create(const off_t aSize) | |
329 | { | |
330 | assert(aSize > 0); | |
331 | assert(!theMem); | |
332 | checkSupport("Fake segment creation"); | |
333 | ||
334 | const bool inserted = Segments.insert(std::make_pair(theName, this)).second; | |
335 | if (!inserted) | |
336 | fatalf("Duplicate fake segment creation: %s", theName.termedBuf()); | |
337 | ||
338 | theMem = new char[aSize]; | |
339 | theSize = aSize; | |
340 | doUnlink = true; | |
341 | ||
bf95c10a | 342 | debugs(54, 3, "created " << theName << " fake segment: " << theSize); |
be9cb908 DK |
343 | } |
344 | ||
345 | void | |
346 | Ipc::Mem::Segment::open() | |
347 | { | |
348 | assert(!theMem); | |
349 | checkSupport("Fake segment open"); | |
350 | ||
351 | const SegmentMap::const_iterator i = Segments.find(theName); | |
352 | if (i == Segments.end()) | |
353 | fatalf("Fake segment not found: %s", theName.termedBuf()); | |
354 | ||
355 | const Segment &segment = *i->second; | |
356 | theMem = segment.theMem; | |
357 | theSize = segment.theSize; | |
358 | ||
bf95c10a | 359 | debugs(54, 3, "opened " << theName << " fake segment: " << theSize); |
be9cb908 DK |
360 | } |
361 | ||
362 | void | |
363 | Ipc::Mem::Segment::checkSupport(const char *const context) | |
364 | { | |
365 | if (!Enabled()) { | |
bf95c10a | 366 | debugs(54, 5, context << |
b46ae525 | 367 | ": True shared memory segments are not supported. " |
be9cb908 | 368 | "Cannot fake shared segments in SMP config."); |
b46ae525 AR |
369 | fatalf("Ipc::Mem::Segment: Cannot fake shared segments in SMP config (%s)\n", |
370 | context); | |
be9cb908 DK |
371 | } |
372 | } | |
373 | ||
374 | #endif // HAVE_SHM | |
f5480a9e DK |
375 | |
376 | void | |
21b7990f | 377 | Ipc::Mem::RegisteredRunner::useConfig() |
f5480a9e DK |
378 | { |
379 | // If Squid is built with real segments, we create() real segments | |
380 | // in the master process only. Otherwise, we create() fake | |
381 | // segments in each worker process. We assume that only workers | |
382 | // need and can work with fake segments. | |
383 | #if HAVE_SHM | |
384 | if (IamMasterProcess()) | |
385 | #else | |
9e3f96a4 | 386 | if (IamWorkerProcess()) |
f5480a9e | 387 | #endif |
21b7990f | 388 | create(); |
f5480a9e DK |
389 | |
390 | // we assume that master process does not need shared segments | |
391 | // unless it is also a worker | |
392 | if (!InDaemonMode() || !IamMasterProcess()) | |
21b7990f | 393 | open(); |
f5480a9e | 394 | } |
f53969cc | 395 |