]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/mem/Segment.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ipc / mem / Segment.cc
CommitLineData
f5eef98c 1/*
bde978a6 2 * Copyright (C) 1996-2015 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"
af69c635
FC
14#include "Debug.h"
15#include "fatal.h"
d8a82533 16#include "ipc/mem/Segment.h"
1860fbac 17#include "SBuf.h"
5bed43d6 18#include "tools.h"
f5eef98c 19
7e851a39 20#if HAVE_FCNTL_H
f5eef98c 21#include <fcntl.h>
7e851a39
AJ
22#endif
23#if HAVE_SYS_MMAN_H
f5eef98c 24#include <sys/mman.h>
7e851a39
AJ
25#endif
26#if HAVE_SYS_STAT_H
f5eef98c 27#include <sys/stat.h>
7e851a39
AJ
28#endif
29#if HAVE_UNISTD_H
f5eef98c 30#include <unistd.h>
7e851a39 31#endif
f5eef98c 32
ef8de464
AR
33// test cases change this
34const char *Ipc::Mem::Segment::BasePath = DEFAULT_STATEDIR;
35
be9cb908
DK
36void *
37Ipc::Mem::Segment::reserve(size_t chunkSize)
38{
39 Must(theMem);
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;
47 return result;
48}
49
1860fbac
AR
50SBuf
51Ipc::Mem::Segment::Name(const SBuf &prefix, const char *suffix)
52{
53 SBuf result = prefix;
54 result.append("_");
55 result.append(suffix);
56 return result;
57}
58
be9cb908
DK
59#if HAVE_SHM
60
d8a82533 61Ipc::Mem::Segment::Segment(const char *const id):
f53969cc
SM
62 theFD(-1), theName(GenerateName(id)), theMem(NULL),
63 theSize(0), theReserved(0), doUnlink(false)
f5eef98c
DK
64{
65}
66
9199139f
AR
67Ipc::Mem::Segment::~Segment()
68{
f5eef98c
DK
69 if (theFD >= 0) {
70 detach();
d8a82533 71 if (close(theFD) != 0)
be1d3736 72 debugs(54, 5, HERE << "close " << theName << ": " << xstrerror());
f5eef98c 73 }
68353d5a
DK
74 if (doUnlink)
75 unlink();
f5eef98c
DK
76}
77
be9cb908 78// fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective
c975f532 79bool
9199139f
AR
80Ipc::Mem::Segment::Enabled()
81{
c975f532 82 return true;
c975f532
DK
83}
84
f5eef98c 85void
e08b0f77 86Ipc::Mem::Segment::create(const off_t aSize)
f5eef98c
DK
87{
88 assert(aSize > 0);
89 assert(theFD < 0);
90
e1825c5d 91 theFD = shm_open(theName.termedBuf(), O_CREAT | O_RDWR | O_TRUNC,
f5eef98c
DK
92 S_IRUSR | S_IWUSR);
93 if (theFD < 0) {
be1d3736 94 debugs(54, 5, HERE << "shm_open " << theName << ": " << xstrerror());
77915f06
AR
95 fatalf("Ipc::Mem::Segment::create failed to shm_open(%s): %s\n",
96 theName.termedBuf(), xstrerror());
f5eef98c
DK
97 }
98
99 if (ftruncate(theFD, aSize)) {
be1d3736 100 debugs(54, 5, HERE << "ftruncate " << theName << ": " << xstrerror());
b46ae525
AR
101 fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n",
102 theName.termedBuf(), xstrerror());
f5eef98c
DK
103 }
104
e08b0f77
AR
105 assert(statSize("Ipc::Mem::Segment::create") == aSize); // paranoid
106
f5eef98c 107 theSize = aSize;
937d0d3f 108 theReserved = 0;
68353d5a 109 doUnlink = true;
f5eef98c 110
446485d5
AR
111 debugs(54, 3, HERE << "created " << theName << " segment: " << theSize);
112
f5eef98c
DK
113 attach();
114}
115
116void
d8a82533 117Ipc::Mem::Segment::open()
f5eef98c
DK
118{
119 assert(theFD < 0);
120
121 theFD = shm_open(theName.termedBuf(), O_RDWR, 0);
122 if (theFD < 0) {
be1d3736 123 debugs(54, 5, HERE << "shm_open " << theName << ": " << xstrerror());
77915f06
AR
124 fatalf("Ipc::Mem::Segment::open failed to shm_open(%s): %s\n",
125 theName.termedBuf(), xstrerror());
f5eef98c
DK
126 }
127
e08b0f77 128 theSize = statSize("Ipc::Mem::Segment::open");
f5eef98c 129
446485d5
AR
130 debugs(54, 3, HERE << "opened " << theName << " segment: " << theSize);
131
f5eef98c
DK
132 attach();
133}
134
135/// Map the shared memory segment to the process memory space.
136void
d8a82533 137Ipc::Mem::Segment::attach()
f5eef98c
DK
138{
139 assert(theFD >= 0);
f5eef98c
DK
140 assert(!theMem);
141
3a2ede14
AR
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)));
145
f5eef98c
DK
146 void *const p =
147 mmap(NULL, theSize, PROT_READ | PROT_WRITE, MAP_SHARED, theFD, 0);
148 if (p == MAP_FAILED) {
be1d3736 149 debugs(54, 5, HERE << "mmap " << theName << ": " << xstrerror());
b46ae525
AR
150 fatalf("Ipc::Mem::Segment::attach failed to mmap(%s): %s\n",
151 theName.termedBuf(), xstrerror());
f5eef98c
DK
152 }
153 theMem = p;
154}
155
156/// Unmap the shared memory segment from the process memory space.
157void
d8a82533 158Ipc::Mem::Segment::detach()
f5eef98c
DK
159{
160 if (!theMem)
161 return;
162
163 if (munmap(theMem, theSize)) {
be1d3736 164 debugs(54, 5, HERE << "munmap " << theName << ": " << xstrerror());
b46ae525
AR
165 fatalf("Ipc::Mem::Segment::detach failed to munmap(%s): %s\n",
166 theName.termedBuf(), xstrerror());
f5eef98c
DK
167 }
168 theMem = 0;
169}
170
68353d5a
DK
171void
172Ipc::Mem::Segment::unlink()
173{
174 if (shm_unlink(theName.termedBuf()) != 0)
175 debugs(54, 5, HERE << "shm_unlink(" << theName << "): " << xstrerror());
176 else
177 debugs(54, 3, HERE << "unlinked " << theName << " segment");
178}
179
e08b0f77
AR
180/// determines the size of the underlying "file"
181off_t
182Ipc::Mem::Segment::statSize(const char *context) const
183{
184 Must(theFD >= 0);
185
186 struct stat s;
187 memset(&s, 0, sizeof(s));
188
189 if (fstat(theFD, &s) != 0) {
b46ae525
AR
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());
e08b0f77
AR
193 }
194
195 return s.st_size;
196}
197
ef8de464
AR
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.
f5eef98c 200String
d8a82533 201Ipc::Mem::Segment::GenerateName(const char *id)
f5eef98c 202{
38189e84 203 assert(BasePath && *BasePath);
ef8de464 204 static const bool nameIsPath = shm_portable_segment_name_is_path();
38189e84
AJ
205 String name;
206 if (nameIsPath) {
207 name.append(BasePath);
208 if (name[name.size()-1] != '/')
209 name.append('/');
210 } else
211 name.append("/squid-");
ef8de464
AR
212
213 // append id, replacing slashes with dots
9a51593d
DK
214 for (const char *slash = strchr(id, '/'); slash; slash = strchr(id, '/')) {
215 if (id != slash) {
216 name.append(id, slash - id);
217 name.append('.');
218 }
219 id = slash + 1;
220 }
f5eef98c 221 name.append(id);
ef8de464
AR
222
223 name.append(".shm"); // to distinguish from non-segments when nameIsPath
f5eef98c
DK
224 return name;
225}
be9cb908
DK
226
227#else // HAVE_SHM
228
229#include <map>
230
231typedef std::map<String, Ipc::Mem::Segment *> SegmentMap;
232static SegmentMap Segments;
233
234Ipc::Mem::Segment::Segment(const char *const id):
f53969cc 235 theName(id), theMem(NULL), theSize(0), theReserved(0), doUnlink(false)
be9cb908
DK
236{
237}
238
239Ipc::Mem::Segment::~Segment()
240{
241 if (doUnlink) {
242 delete [] static_cast<char *>(theMem);
44c64af0
AR
243 theMem = NULL;
244 Segments.erase(theName);
245 debugs(54, 3, HERE << "unlinked " << theName << " fake segment");
be9cb908
DK
246 }
247}
248
249bool
250Ipc::Mem::Segment::Enabled()
251{
38a129ef 252 return !UsingSmp() && IamWorkerProcess();
be9cb908
DK
253}
254
255void
256Ipc::Mem::Segment::create(const off_t aSize)
257{
258 assert(aSize > 0);
259 assert(!theMem);
260 checkSupport("Fake segment creation");
261
262 const bool inserted = Segments.insert(std::make_pair(theName, this)).second;
263 if (!inserted)
264 fatalf("Duplicate fake segment creation: %s", theName.termedBuf());
265
266 theMem = new char[aSize];
267 theSize = aSize;
268 doUnlink = true;
269
270 debugs(54, 3, HERE << "created " << theName << " fake segment: " << theSize);
271}
272
273void
274Ipc::Mem::Segment::open()
275{
276 assert(!theMem);
277 checkSupport("Fake segment open");
278
279 const SegmentMap::const_iterator i = Segments.find(theName);
280 if (i == Segments.end())
281 fatalf("Fake segment not found: %s", theName.termedBuf());
282
283 const Segment &segment = *i->second;
284 theMem = segment.theMem;
285 theSize = segment.theSize;
286
287 debugs(54, 3, HERE << "opened " << theName << " fake segment: " << theSize);
288}
289
290void
291Ipc::Mem::Segment::checkSupport(const char *const context)
292{
293 if (!Enabled()) {
b46ae525
AR
294 debugs(54, 5, HERE << context <<
295 ": True shared memory segments are not supported. "
be9cb908 296 "Cannot fake shared segments in SMP config.");
b46ae525
AR
297 fatalf("Ipc::Mem::Segment: Cannot fake shared segments in SMP config (%s)\n",
298 context);
be9cb908
DK
299 }
300}
301
302#endif // HAVE_SHM
f5480a9e
DK
303
304void
21b7990f 305Ipc::Mem::RegisteredRunner::useConfig()
f5480a9e
DK
306{
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.
311#if HAVE_SHM
312 if (IamMasterProcess())
313#else
9e3f96a4 314 if (IamWorkerProcess())
f5480a9e 315#endif
21b7990f 316 create();
f5480a9e
DK
317
318 // we assume that master process does not need shared segments
319 // unless it is also a worker
320 if (!InDaemonMode() || !IamMasterProcess())
21b7990f 321 open();
f5480a9e 322}
f53969cc 323