Segment.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "SquidConfig.h"
19 #include "tools.h"
20 
21 #if HAVE_FCNTL_H
22 #include <fcntl.h>
23 #endif
24 #if HAVE_SYS_MMAN_H
25 #include <sys/mman.h>
26 #endif
27 #if HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 
34 // test cases change this
35 const char *Ipc::Mem::Segment::BasePath = DEFAULT_STATEDIR;
36 
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 
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 
60 #if HAVE_SHM
61 
62 Ipc::Mem::Segment::Segment(const char *const id):
63  theFD(-1), theName(GenerateName(id)), theMem(NULL),
64  theSize(0), theReserved(0), doUnlink(false)
65 {
66 }
67 
69 {
70  if (theFD >= 0) {
71  detach();
72  if (close(theFD) != 0) {
73  int xerrno = errno;
74  debugs(54, 5, "close " << theName << ": " << xstrerr(xerrno));
75  }
76  }
77  if (doUnlink)
78  unlink();
79 }
80 
81 // fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective
82 bool
84 {
85  return true;
86 }
87 
88 void
89 Ipc::Mem::Segment::create(const off_t aSize)
90 {
91  assert(aSize > 0);
92  assert(theFD < 0);
93 
94  int xerrno = 0;
95 
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) {
101  unlink();
102  createFresh(xerrno);
103  }
104 
105  if (theFD < 0) {
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));
109  }
110 
111  if (ftruncate(theFD, aSize)) {
112  xerrno = errno;
113  unlink();
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));
117  }
118  // We assume that the shm_open(O_CREAT)+ftruncate() combo zeros the segment.
119 
120  theSize = statSize("Ipc::Mem::Segment::create");
121 
122  // OS X will round up to a full page, so not checking for exact size match.
123  assert(theSize >= aSize);
124 
125  theReserved = 0;
126  doUnlink = true;
127 
128  debugs(54, 3, "created " << theName << " segment: " << theSize);
129  attach();
130 }
131 
132 void
134 {
135  assert(theFD < 0);
136 
137  theFD = shm_open(theName.termedBuf(), O_RDWR, 0);
138  if (theFD < 0) {
139  int xerrno = errno;
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));
143  }
144 
145  theSize = statSize("Ipc::Mem::Segment::open");
146 
147  debugs(54, 3, HERE << "opened " << theName << " segment: " << theSize);
148 
149  attach();
150 }
151 
154 bool
155 Ipc::Mem::Segment::createFresh(int &xerrno)
156 {
157  theFD = shm_open(theName.termedBuf(),
158  O_EXCL | O_CREAT | O_RDWR,
159  S_IRUSR | S_IWUSR);
160  xerrno = errno;
161  return theFD >= 0;
162 }
163 
165 void
166 Ipc::Mem::Segment::attach()
167 {
168  assert(theFD >= 0);
169  assert(!theMem);
170 
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)));
174 
175  void *const p =
176  mmap(NULL, theSize, PROT_READ | PROT_WRITE, MAP_SHARED, theFD, 0);
177  if (p == MAP_FAILED) {
178  int xerrno = errno;
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));
182  }
183  theMem = p;
184 
185  lock();
186 }
187 
189 void
190 Ipc::Mem::Segment::detach()
191 {
192  if (!theMem)
193  return;
194 
195  if (munmap(theMem, theSize)) {
196  int xerrno = errno;
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));
200  }
201  theMem = 0;
202 }
203 
206 void
207 Ipc::Mem::Segment::lock()
208 {
209  if (!Config.shmLocking) {
210  debugs(54, 5, "mlock(2)-ing disabled");
211  return;
212  }
213 
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));
220  }
221  // TODO: Warn if it took too long.
222  debugs(54, 7, "mlock(" << theName << ',' << theSize << ") OK");
223 #else
224  debugs(54, 5, "insufficient mlock(2) support");
225  if (Config.shmLocking.configured()) { // set explicitly
226  static bool warnedOnce = false;
227  if (!warnedOnce) {
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.");
231  warnedOnce = true;
232  }
233  }
234 #endif
235 }
236 
237 void
238 Ipc::Mem::Segment::unlink()
239 {
240  if (shm_unlink(theName.termedBuf()) != 0) {
241  int xerrno = errno;
242  debugs(54, 5, "shm_unlink(" << theName << "): " << xstrerr(xerrno));
243  } else
244  debugs(54, 3, "unlinked " << theName << " segment");
245 }
246 
248 off_t
249 Ipc::Mem::Segment::statSize(const char *context) const
250 {
251  Must(theFD >= 0);
252 
253  struct stat s;
254  memset(&s, 0, sizeof(s));
255 
256  if (fstat(theFD, &s) != 0) {
257  int xerrno = errno;
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));
261  }
262 
263  return s.st_size;
264 }
265 
268 String
269 Ipc::Mem::Segment::GenerateName(const char *id)
270 {
271  assert(BasePath && *BasePath);
272  static const bool nameIsPath = shm_portable_segment_name_is_path();
273  String name;
274  if (nameIsPath) {
275  name.append(BasePath);
276  if (name[name.size()-1] != '/')
277  name.append('/');
278  } else {
279  name.append('/');
280  name.append(service_name.c_str());
281  name.append('-');
282  }
283 
284  // append id, replacing slashes with dots
285  for (const char *slash = strchr(id, '/'); slash; slash = strchr(id, '/')) {
286  if (id != slash) {
287  name.append(id, slash - id);
288  name.append('.');
289  }
290  id = slash + 1;
291  }
292  name.append(id);
293 
294  name.append(".shm"); // to distinguish from non-segments when nameIsPath
295  return name;
296 }
297 
298 #else // HAVE_SHM
299 
300 #include <map>
301 
302 typedef std::map<String, Ipc::Mem::Segment *> SegmentMap;
303 static SegmentMap Segments;
304 
305 Ipc::Mem::Segment::Segment(const char *const id):
306  theName(id), theMem(NULL), theSize(0), theReserved(0), doUnlink(false)
307 {
308 }
309 
311 {
312  if (doUnlink) {
313  delete [] static_cast<char *>(theMem);
314  theMem = NULL;
315  Segments.erase(theName);
316  debugs(54, 3, HERE << "unlinked " << theName << " fake segment");
317  }
318 }
319 
320 bool
322 {
323  return !UsingSmp() && IamWorkerProcess();
324 }
325 
326 void
327 Ipc::Mem::Segment::create(const off_t aSize)
328 {
329  assert(aSize > 0);
330  assert(!theMem);
331  checkSupport("Fake segment creation");
332 
333  const bool inserted = Segments.insert(std::make_pair(theName, this)).second;
334  if (!inserted)
335  fatalf("Duplicate fake segment creation: %s", theName.termedBuf());
336 
337  theMem = new char[aSize];
338  theSize = aSize;
339  doUnlink = true;
340 
341  debugs(54, 3, HERE << "created " << theName << " fake segment: " << theSize);
342 }
343 
344 void
346 {
347  assert(!theMem);
348  checkSupport("Fake segment open");
349 
350  const SegmentMap::const_iterator i = Segments.find(theName);
351  if (i == Segments.end())
352  fatalf("Fake segment not found: %s", theName.termedBuf());
353 
354  const Segment &segment = *i->second;
355  theMem = segment.theMem;
356  theSize = segment.theSize;
357 
358  debugs(54, 3, HERE << "opened " << theName << " fake segment: " << theSize);
359 }
360 
361 void
362 Ipc::Mem::Segment::checkSupport(const char *const context)
363 {
364  if (!Enabled()) {
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",
369  context);
370  }
371 }
372 
373 #endif // HAVE_SHM
374 
375 void
377 {
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.
382 #if HAVE_SHM
383  if (IamMasterProcess())
384 #else
385  if (IamWorkerProcess())
386 #endif
387  create();
388 
389  // we assume that master process does not need shared segments
390  // unless it is also a worker
391  if (!InDaemonMode() || !IamMasterProcess())
392  open();
393 }
394 
static SegmentMap Segments
Definition: Segment.cc:303
Segment(const char *const id)
Create a shared memory segment.
Definition: Segment.cc:305
bool shm_portable_segment_name_is_path()
Determines whether segment names are iterpreted as full file paths.
Definition: shm.cc:23
#define assert(EX)
Definition: assert.h:17
YesNoNone shmLocking
shared_memory_locking
Definition: SquidConfig.h:80
Definition: SBuf.h:87
int shm_unlink(const char *)
Definition: shm.h:41
static SBuf Name(const SBuf &prefix, const char *suffix)
concatenates parts of a name to form a complete name (or its prefix)
Definition: Segment.cc:52
int i
Definition: membanger.c:49
#define PRId64
Definition: types.h:110
SBuf & append(const SBuf &S)
Definition: SBuf.cc:207
std::map< String, Ipc::Mem::Segment * > SegmentMap
Definition: Segment.cc:302
POSIX shared memory segment.
Definition: Segment.h:23
void open()
Open an existing shared memory segment.
Definition: Segment.cc:345
char * p
Definition: membanger.c:43
bool IamMasterProcess()
whether the current process is the parent of all other Squid processes
Definition: tools.cc:630
size_type size() const
Definition: SquidString.h:71
void append(char const *buf, int len)
Definition: String.cc:161
bool IamWorkerProcess()
whether the current process handles HTTP transactions and such
Definition: stub_tools.cc:49
void fatalf(const char *fmt,...)
Definition: fatal.cc:79
static bool Enabled()
Whether shared memory support is available.
Definition: Segment.cc:321
const char * xstrerr(int error)
Definition: xstrerror.cc:83
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define DBG_IMPORTANT
Definition: Debug.h:45
bool configured() const
Definition: YesNoNone.h:66
static const char * BasePath
common path of all segment names in path-based environments
Definition: Segment.h:43
int shm_open(const char *, int, mode_t)
Definition: shm.h:35
const char * c_str()
Definition: SBuf.cc:546
void create(const off_t aSize)
Create a new shared memory segment. Unlinks the segment on destruction.
Definition: Segment.cc:327
bool UsingSmp()
Whether there should be more than one worker process running.
Definition: tools.cc:658
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
#define Must(cond)
Definition: TextException.h:89
void * reserve(size_t chunkSize)
reserve and return the next chunk
Definition: Segment.cc:38
bool InDaemonMode()
Whether we are running in daemon mode.
Definition: tools.cc:652
virtual void useConfig()
Definition: Segment.cc:376
void * theMem
pointer to mmapped shared memory segment
Definition: Segment.h:74
SBuf service_name(APP_SHORTNAME)
void checkSupport(const char *const context)
Definition: Segment.cc:362
off_t theReserved
the total number of reserve()d bytes
Definition: Segment.h:76
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
#define false
Definition: GnuRegex.c:233
off_t theSize
shared memory segment size
Definition: Segment.h:75

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors