From 62fa1d73b24c65e19248ab190cc0d9899d22a7f6 Mon Sep 17 00:00:00 2001 From: Francesco Chemolli Date: Tue, 24 Feb 2015 11:32:15 +0100 Subject: [PATCH] Candidate for fixing MacOS SHM --- src/ipc/mem/Segment.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ipc/mem/Segment.cc b/src/ipc/mem/Segment.cc index c2cc40dd73..33a6ff9c9b 100644 --- a/src/ipc/mem/Segment.cc +++ b/src/ipc/mem/Segment.cc @@ -88,7 +88,8 @@ Ipc::Mem::Segment::create(const off_t aSize) assert(aSize > 0); assert(theFD < 0); - theFD = shm_open(theName.termedBuf(), O_CREAT | O_RDWR | O_TRUNC, + // OS X does not allow using O_TRUNC here. + theFD = shm_open(theName.termedBuf(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); if (theFD < 0) { debugs(54, 5, HERE << "shm_open " << theName << ": " << xstrerror()); @@ -97,14 +98,19 @@ Ipc::Mem::Segment::create(const off_t aSize) } if (ftruncate(theFD, aSize)) { - debugs(54, 5, HERE << "ftruncate " << theName << ": " << xstrerror()); + const int savedError = errno; + unlink(); + debugs(54, 5, HERE << "ftruncate " << theName << ": " << xstrerr(savedError)); fatalf("Ipc::Mem::Segment::create failed to ftruncate(%s): %s\n", - theName.termedBuf(), xstrerror()); + theName.termedBuf(), xstrerr(savedError)); } + // We assume that the shm_open(O_CREAT)+ftruncate() combo zeros the segment. - assert(statSize("Ipc::Mem::Segment::create") == aSize); // paranoid + theSize = statSize("Ipc::Mem::Segment::create"); + + // OS X will round up to a full page, so not checking for exact size match. + assert(theSize >= aSize); - theSize = aSize; theReserved = 0; doUnlink = true; -- 2.47.2