From: phk Date: Fri, 14 Jul 2006 11:44:28 +0000 (+0000) Subject: Better and more paranoid SHMEM creation logic X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8cdf64bcf0ee1aef0435ad1761eefbf5db8e93bd;p=varnish Better and more paranoid SHMEM creation logic git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@471 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/shmlog.c b/varnish-cache/bin/varnishd/shmlog.c index cfe84d62..6db63317 100644 --- a/varnish-cache/bin/varnishd/shmlog.c +++ b/varnish-cache/bin/varnishd/shmlog.c @@ -141,19 +141,29 @@ void VSL_MgtInit(const char *fn, unsigned size) { struct shmloghead slh; - int i; - - heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0644); - if (heritage.vsl_fd < 0) { - fprintf(stderr, "Could not open %s: %s\n", - fn, strerror(errno)); - exit (1); - } - i = read(heritage.vsl_fd, &slh, sizeof slh); - if (i != sizeof slh || slh.magic != SHMLOGHEAD_MAGIC) { + int i = 0; + + heritage.vsl_fd = open(fn, O_RDWR, 0644); + if (heritage.vsl_fd >= 0) + i = read(heritage.vsl_fd, &slh, sizeof slh); + if (heritage.vsl_fd < 0 || i != sizeof slh || + slh.magic != SHMLOGHEAD_MAGIC || + slh.hdrsize != sizeof slh) { /* XXX more checks */ + if (heritage.vsl_fd >= 0); + close(heritage.vsl_fd); + unlink(fn); + heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0644); + if (heritage.vsl_fd < 0) { + fprintf(stderr, "Could not open %s: %s\n", + fn, strerror(errno)); + exit (1); + } + memset(&slh, 0, sizeof slh); + slh.magic = SHMLOGHEAD_MAGIC; + slh.hdrsize = sizeof slh; slh.size = size; slh.ptr = 0; slh.start = sizeof slh;