]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Segment.cc
2 * Copyright (C) 1996-2014 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"
25 // test cases change this
26 const char *Ipc::Mem::Segment::BasePath
= DEFAULT_STATEDIR
;
29 Ipc::Mem::Segment::reserve(size_t chunkSize
)
32 // check for overflows
33 // chunkSize >= 0 may result in warnings on systems where off_t is unsigned
34 assert(!chunkSize
|| static_cast<off_t
>(chunkSize
) > 0);
35 assert(static_cast<off_t
>(chunkSize
) <= theSize
);
36 assert(theReserved
<= theSize
- static_cast<off_t
>(chunkSize
));
37 void *result
= reinterpret_cast<char*>(theMem
) + theReserved
;
38 theReserved
+= chunkSize
;
43 Ipc::Mem::Segment::Name(const SBuf
&prefix
, const char *suffix
)
47 result
.append(suffix
);
53 Ipc::Mem::Segment::Segment(const char *const id
):
54 theFD(-1), theName(GenerateName(id
)), theMem(NULL
),
55 theSize(0), theReserved(0), doUnlink(false)
59 Ipc::Mem::Segment::~Segment()
63 if (close(theFD
) != 0)
64 debugs(54, 5, HERE
<< "close " << theName
<< ": " << xstrerror());
70 // fake Ipc::Mem::Segment::Enabled (!HAVE_SHM) is more selective
72 Ipc::Mem::Segment::Enabled()
78 Ipc::Mem::Segment::create(const off_t aSize
)
83 theFD
= shm_open(theName
.termedBuf(), O_CREAT
| O_RDWR
| O_TRUNC
,
86 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
87 fatalf("Ipc::Mem::Segment::create failed to shm_open(%s): %s\n",
88 theName
.termedBuf(), xstrerror());
91 if (ftruncate(theFD
, aSize
)) {
92 debugs(54, 5, HERE
<< "ftruncate " << theName
<< ": " << xstrerror());
93 fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n",
94 theName
.termedBuf(), xstrerror());
97 assert(statSize("Ipc::Mem::Segment::create") == aSize
); // paranoid
103 debugs(54, 3, HERE
<< "created " << theName
<< " segment: " << theSize
);
109 Ipc::Mem::Segment::open()
113 theFD
= shm_open(theName
.termedBuf(), O_RDWR
, 0);
115 debugs(54, 5, HERE
<< "shm_open " << theName
<< ": " << xstrerror());
116 fatalf("Ipc::Mem::Segment::open failed to shm_open(%s): %s\n",
117 theName
.termedBuf(), xstrerror());
120 theSize
= statSize("Ipc::Mem::Segment::open");
122 debugs(54, 3, HERE
<< "opened " << theName
<< " segment: " << theSize
);
127 /// Map the shared memory segment to the process memory space.
129 Ipc::Mem::Segment::attach()
134 // mmap() accepts size_t for the size; we give it off_t which might
135 // be bigger; assert overflows until we support multiple mmap()s?
136 assert(theSize
== static_cast<off_t
>(static_cast<size_t>(theSize
)));
139 mmap(NULL
, theSize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, theFD
, 0);
140 if (p
== MAP_FAILED
) {
141 debugs(54, 5, HERE
<< "mmap " << theName
<< ": " << xstrerror());
142 fatalf("Ipc::Mem::Segment::attach failed to mmap(%s): %s\n",
143 theName
.termedBuf(), xstrerror());
148 /// Unmap the shared memory segment from the process memory space.
150 Ipc::Mem::Segment::detach()
155 if (munmap(theMem
, theSize
)) {
156 debugs(54, 5, HERE
<< "munmap " << theName
<< ": " << xstrerror());
157 fatalf("Ipc::Mem::Segment::detach failed to munmap(%s): %s\n",
158 theName
.termedBuf(), xstrerror());
164 Ipc::Mem::Segment::unlink()
166 if (shm_unlink(theName
.termedBuf()) != 0)
167 debugs(54, 5, HERE
<< "shm_unlink(" << theName
<< "): " << xstrerror());
169 debugs(54, 3, HERE
<< "unlinked " << theName
<< " segment");
172 /// determines the size of the underlying "file"
174 Ipc::Mem::Segment::statSize(const char *context
) const
179 memset(&s
, 0, sizeof(s
));
181 if (fstat(theFD
, &s
) != 0) {
182 debugs(54, 5, HERE
<< context
<< " fstat " << theName
<< ": " << xstrerror());
183 fatalf("Ipc::Mem::Segment::statSize: %s failed to fstat(%s): %s\n",
184 context
, theName
.termedBuf(), xstrerror());
190 /// Generate name for shared memory segment. Starts with a prefix required
191 /// for cross-platform portability and replaces all slashes in ID with dots.
193 Ipc::Mem::Segment::GenerateName(const char *id
)
195 assert(BasePath
&& *BasePath
);
196 static const bool nameIsPath
= shm_portable_segment_name_is_path();
199 name
.append(BasePath
);
200 if (name
[name
.size()-1] != '/')
203 name
.append("/squid-");
205 // append id, replacing slashes with dots
206 for (const char *slash
= strchr(id
, '/'); slash
; slash
= strchr(id
, '/')) {
208 name
.append(id
, slash
- id
);
215 name
.append(".shm"); // to distinguish from non-segments when nameIsPath
223 typedef std::map
<String
, Ipc::Mem::Segment
*> SegmentMap
;
224 static SegmentMap Segments
;
226 Ipc::Mem::Segment::Segment(const char *const id
):
227 theName(id
), theMem(NULL
), theSize(0), theReserved(0), doUnlink(false)
231 Ipc::Mem::Segment::~Segment()
234 delete [] static_cast<char *>(theMem
);
236 Segments
.erase(theName
);
237 debugs(54, 3, HERE
<< "unlinked " << theName
<< " fake segment");
242 Ipc::Mem::Segment::Enabled()
244 return !UsingSmp() && IamWorkerProcess();
248 Ipc::Mem::Segment::create(const off_t aSize
)
252 checkSupport("Fake segment creation");
254 const bool inserted
= Segments
.insert(std::make_pair(theName
, this)).second
;
256 fatalf("Duplicate fake segment creation: %s", theName
.termedBuf());
258 theMem
= new char[aSize
];
262 debugs(54, 3, HERE
<< "created " << theName
<< " fake segment: " << theSize
);
266 Ipc::Mem::Segment::open()
269 checkSupport("Fake segment open");
271 const SegmentMap::const_iterator i
= Segments
.find(theName
);
272 if (i
== Segments
.end())
273 fatalf("Fake segment not found: %s", theName
.termedBuf());
275 const Segment
&segment
= *i
->second
;
276 theMem
= segment
.theMem
;
277 theSize
= segment
.theSize
;
279 debugs(54, 3, HERE
<< "opened " << theName
<< " fake segment: " << theSize
);
283 Ipc::Mem::Segment::checkSupport(const char *const context
)
286 debugs(54, 5, HERE
<< context
<<
287 ": True shared memory segments are not supported. "
288 "Cannot fake shared segments in SMP config.");
289 fatalf("Ipc::Mem::Segment: Cannot fake shared segments in SMP config (%s)\n",
297 Ipc::Mem::RegisteredRunner::useConfig()
299 // If Squid is built with real segments, we create() real segments
300 // in the master process only. Otherwise, we create() fake
301 // segments in each worker process. We assume that only workers
302 // need and can work with fake segments.
304 if (IamMasterProcess())
306 if (IamWorkerProcess())
310 // we assume that master process does not need shared segments
311 // unless it is also a worker
312 if (!InDaemonMode() || !IamMasterProcess())