]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
Refactor SBuf into own library and simplify link time dependencies
[thirdparty/squid.git] / src / ipc / mem / Segment.cc
1 /*
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 54 Interprocess Communication */
10
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "compat/shm.h"
14 #include "Debug.h"
15 #include "fatal.h"
16 #include "ipc/mem/Segment.h"
17 #include "sbuf/SBuf.h"
18 #include "tools.h"
19
20 #if HAVE_FCNTL_H
21 #include <fcntl.h>
22 #endif
23 #if HAVE_SYS_MMAN_H
24 #include <sys/mman.h>
25 #endif
26 #if HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 // test cases change this
34 const char *Ipc::Mem::Segment::BasePath = DEFAULT_STATEDIR;
35
36 void *
37 Ipc::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
50 SBuf
51 Ipc::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
59 #if HAVE_SHM
60
61 Ipc::Mem::Segment::Segment(const char *const id):
62 theFD(-1), theName(GenerateName(id)), theMem(NULL),
63 theSize(0), theReserved(0), doUnlink(false)
64 {
65 }
66
67 Ipc::Mem::Segment::~Segment()
68 {
69 if (theFD >= 0) {
70 detach();
71 if (close(theFD) != 0)
72 debugs(54, 5, HERE << "close " << theName << ": " << xstrerror());
73 }
74 if (doUnlink)
75 unlink();
76 }
77
78 // fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective
79 bool
80 Ipc::Mem::Segment::Enabled()
81 {
82 return true;
83 }
84
85 void
86 Ipc::Mem::Segment::create(const off_t aSize)
87 {
88 assert(aSize > 0);
89 assert(theFD < 0);
90
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) {
96 unlink();
97 createFresh();
98 }
99
100 if (theFD < 0) {
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());
104 }
105
106 if (ftruncate(theFD, aSize)) {
107 const int savedError = errno;
108 unlink();
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));
112 }
113 // We assume that the shm_open(O_CREAT)+ftruncate() combo zeros the segment.
114
115 theSize = statSize("Ipc::Mem::Segment::create");
116
117 // OS X will round up to a full page, so not checking for exact size match.
118 assert(theSize >= aSize);
119
120 theReserved = 0;
121 doUnlink = true;
122
123 debugs(54, 3, HERE << "created " << theName << " segment: " << theSize);
124
125 attach();
126 }
127
128 void
129 Ipc::Mem::Segment::open()
130 {
131 assert(theFD < 0);
132
133 theFD = shm_open(theName.termedBuf(), O_RDWR, 0);
134 if (theFD < 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());
138 }
139
140 theSize = statSize("Ipc::Mem::Segment::open");
141
142 debugs(54, 3, HERE << "opened " << theName << " segment: " << theSize);
143
144 attach();
145 }
146
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.
149 bool
150 Ipc::Mem::Segment::createFresh()
151 {
152 theFD = shm_open(theName.termedBuf(),
153 O_EXCL | O_CREAT | O_RDWR,
154 S_IRUSR | S_IWUSR);
155 return theFD >= 0;
156 }
157
158 /// Map the shared memory segment to the process memory space.
159 void
160 Ipc::Mem::Segment::attach()
161 {
162 assert(theFD >= 0);
163 assert(!theMem);
164
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)));
168
169 void *const p =
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());
175 }
176 theMem = p;
177 }
178
179 /// Unmap the shared memory segment from the process memory space.
180 void
181 Ipc::Mem::Segment::detach()
182 {
183 if (!theMem)
184 return;
185
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());
190 }
191 theMem = 0;
192 }
193
194 void
195 Ipc::Mem::Segment::unlink()
196 {
197 if (shm_unlink(theName.termedBuf()) != 0)
198 debugs(54, 5, HERE << "shm_unlink(" << theName << "): " << xstrerror());
199 else
200 debugs(54, 3, HERE << "unlinked " << theName << " segment");
201 }
202
203 /// determines the size of the underlying "file"
204 off_t
205 Ipc::Mem::Segment::statSize(const char *context) const
206 {
207 Must(theFD >= 0);
208
209 struct stat s;
210 memset(&s, 0, sizeof(s));
211
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());
216 }
217
218 return s.st_size;
219 }
220
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.
223 String
224 Ipc::Mem::Segment::GenerateName(const char *id)
225 {
226 assert(BasePath && *BasePath);
227 static const bool nameIsPath = shm_portable_segment_name_is_path();
228 String name;
229 if (nameIsPath) {
230 name.append(BasePath);
231 if (name[name.size()-1] != '/')
232 name.append('/');
233 } else {
234 name.append('/');
235 name.append(service_name.c_str());
236 name.append('-');
237 }
238
239 // append id, replacing slashes with dots
240 for (const char *slash = strchr(id, '/'); slash; slash = strchr(id, '/')) {
241 if (id != slash) {
242 name.append(id, slash - id);
243 name.append('.');
244 }
245 id = slash + 1;
246 }
247 name.append(id);
248
249 name.append(".shm"); // to distinguish from non-segments when nameIsPath
250 return name;
251 }
252
253 #else // HAVE_SHM
254
255 #include <map>
256
257 typedef std::map<String, Ipc::Mem::Segment *> SegmentMap;
258 static SegmentMap Segments;
259
260 Ipc::Mem::Segment::Segment(const char *const id):
261 theName(id), theMem(NULL), theSize(0), theReserved(0), doUnlink(false)
262 {
263 }
264
265 Ipc::Mem::Segment::~Segment()
266 {
267 if (doUnlink) {
268 delete [] static_cast<char *>(theMem);
269 theMem = NULL;
270 Segments.erase(theName);
271 debugs(54, 3, HERE << "unlinked " << theName << " fake segment");
272 }
273 }
274
275 bool
276 Ipc::Mem::Segment::Enabled()
277 {
278 return !UsingSmp() && IamWorkerProcess();
279 }
280
281 void
282 Ipc::Mem::Segment::create(const off_t aSize)
283 {
284 assert(aSize > 0);
285 assert(!theMem);
286 checkSupport("Fake segment creation");
287
288 const bool inserted = Segments.insert(std::make_pair(theName, this)).second;
289 if (!inserted)
290 fatalf("Duplicate fake segment creation: %s", theName.termedBuf());
291
292 theMem = new char[aSize];
293 theSize = aSize;
294 doUnlink = true;
295
296 debugs(54, 3, HERE << "created " << theName << " fake segment: " << theSize);
297 }
298
299 void
300 Ipc::Mem::Segment::open()
301 {
302 assert(!theMem);
303 checkSupport("Fake segment open");
304
305 const SegmentMap::const_iterator i = Segments.find(theName);
306 if (i == Segments.end())
307 fatalf("Fake segment not found: %s", theName.termedBuf());
308
309 const Segment &segment = *i->second;
310 theMem = segment.theMem;
311 theSize = segment.theSize;
312
313 debugs(54, 3, HERE << "opened " << theName << " fake segment: " << theSize);
314 }
315
316 void
317 Ipc::Mem::Segment::checkSupport(const char *const context)
318 {
319 if (!Enabled()) {
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",
324 context);
325 }
326 }
327
328 #endif // HAVE_SHM
329
330 void
331 Ipc::Mem::RegisteredRunner::useConfig()
332 {
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.
337 #if HAVE_SHM
338 if (IamMasterProcess())
339 #else
340 if (IamWorkerProcess())
341 #endif
342 create();
343
344 // we assume that master process does not need shared segments
345 // unless it is also a worker
346 if (!InDaemonMode() || !IamMasterProcess())
347 open();
348 }
349