[squid-dev] ftruncate() failures on OS X (Darwin)

Alex Rousskov rousskov at measurement-factory.com
Thu Jan 21 15:00:14 UTC 2016


On 01/20/2016 10:34 PM, Markus Mayer wrote:
> There are several bug reports out there that squid is failing to start
> up on OS X with an error similar to this:
> 
> Ipc::Mem::Segment::create failed to ftruncate(/squid-cf__metadata.shm):
> (22) Invalid argument


> So, I took some time to dig into this, down to first looking at the
> Darwin kernel sources

Nice triage! OS X changes already broke shared memory management in
Squid[1]. I hope your thorough investigation will result in an improved
code for everyone.

[1] Item #1 at
http://lists.squid-cache.org/pipermail/squid-dev/2015-December/004112.html


> What this means is that OS X will only support ftruncate() *ONCE* on a
> shm segment

Which means Squid cannot really reuse old segments on OS X because
reusing an old segment requires double-truncate per [1]. I hope there
are no use cases requiring reuse of old segments. I do not know of any.


> - Call shm_open with O_EXCL, so that it'll fail if the shm segment
> already exists. If the call fails with EEXIST, unlink the shm segment
> and retry. This one may not even need to be Darwin-specific. Would be
> unnecessary on other platforms, but wouldn't really hurt.

Could you please simplify your code using the following loopless sketch
and post a tested patch as an attachment?

  if (!createExclusive() && errno == EEXIST) {
      unlink();
      createExclusive()
  }
  ... old error handling code here ...
  ... no need for double-truncate here ...


where createExclusive() is a new protected method:


bool
Ipc::Mem::Segment::createExclusive()
{
    theFD = shm_open(theName.termedBuf(),
                     O_EXCL | O_CREAT | O_RDWR | flags,
                     S_IRUSR | S_IWUSR);
    return theFD >= 0;
}

We can then commit that instead of the double-truncate solution in [1].
AFAICT, it will address both problems.


Thank you,

Alex.



More information about the squid-dev mailing list