r4896 - in trunk/varnish-cache: include lib/libvarnishapi
phk at varnish-cache.org
phk at varnish-cache.org
Sat Jun 5 13:04:17 CEST 2010
Author: phk
Date: 2010-06-05 13:04:17 +0200 (Sat, 05 Jun 2010)
New Revision: 4896
Modified:
trunk/varnish-cache/include/varnishapi.h
trunk/varnish-cache/lib/libvarnishapi/vsl.c
Log:
Add VSL_ReOpen() to detect shmfile changes (ie: master restarts)
Modified: trunk/varnish-cache/include/varnishapi.h
===================================================================
--- trunk/varnish-cache/include/varnishapi.h 2010-06-05 10:16:01 UTC (rev 4895)
+++ trunk/varnish-cache/include/varnishapi.h 2010-06-05 11:04:17 UTC (rev 4896)
@@ -65,6 +65,8 @@
void *VSL_Find_Alloc(struct VSL_data *vd, const char *class, const char *type,
const char *ident, unsigned *lenp);
+int VSL_ReOpen(struct VSL_data *vd);
+
struct shmalloc *vsl_iter0(const struct VSL_data *vd);
void vsl_itern(const struct VSL_data *vd, struct shmalloc **pp);
Modified: trunk/varnish-cache/lib/libvarnishapi/vsl.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/vsl.c 2010-06-05 10:16:01 UTC (rev 4895)
+++ trunk/varnish-cache/lib/libvarnishapi/vsl.c 2010-06-05 11:04:17 UTC (rev 4896)
@@ -103,8 +103,8 @@
/*--------------------------------------------------------------------*/
-int
-VSL_Open(struct VSL_data *vd)
+static int
+vsl_open(struct VSL_data *vd, int rep)
{
int i;
struct shmloghead slh;
@@ -114,42 +114,57 @@
vd->vsl_fd = open(vd->fname, O_RDONLY);
if (vd->vsl_fd < 0) {
- fprintf(stderr, "Cannot open %s: %s\n",
- vd->fname, strerror(errno));
+ if (rep)
+ fprintf(stderr, "Cannot open %s: %s\n",
+ vd->fname, strerror(errno));
return (1);
}
assert(fstat(vd->vsl_fd, &vd->fstat) == 0);
if (!S_ISREG(vd->fstat.st_mode)) {
- fprintf(stderr, "%s is not a regular file\n", vd->fname);
+ if (rep)
+ fprintf(stderr, "%s is not a regular file\n",
+ vd->fname);
return (1);
}
i = read(vd->vsl_fd, &slh, sizeof slh);
if (i != sizeof slh) {
- fprintf(stderr, "Cannot read %s: %s\n",
- vd->fname, strerror(errno));
+ if (rep)
+ fprintf(stderr, "Cannot read %s: %s\n",
+ vd->fname, strerror(errno));
return (1);
}
if (slh.magic != SHMLOGHEAD_MAGIC) {
- fprintf(stderr, "Wrong magic number in file %s\n",
- vd->fname);
+ if (rep)
+ fprintf(stderr, "Wrong magic number in file %s\n",
+ vd->fname);
return (1);
}
vd->vsl_lh = (void *)mmap(NULL, slh.shm_size,
PROT_READ, MAP_SHARED|MAP_HASSEMAPHORE, vd->vsl_fd, 0);
if (vd->vsl_lh == MAP_FAILED) {
- fprintf(stderr, "Cannot mmap %s: %s\n",
- vd->fname, strerror(errno));
+ if (rep)
+ fprintf(stderr, "Cannot mmap %s: %s\n",
+ vd->fname, strerror(errno));
return (1);
}
vd->vsl_end = (uint8_t *)vd->vsl_lh + slh.shm_size;
+ while(slh.alloc_seq == 0)
+ usleep(50000);
vd->alloc_seq = slh.alloc_seq;
return (0);
}
+int
+VSL_Open(struct VSL_data *vd)
+{
+
+ return (vsl_open(vd, 1));
+}
+
/*--------------------------------------------------------------------*/
void
@@ -166,6 +181,33 @@
/*--------------------------------------------------------------------*/
+int
+VSL_ReOpen(struct VSL_data *vd)
+{
+ struct stat st;
+ int i;
+
+ if (vd->vsl_lh == NULL)
+ return (-1);
+
+ if (stat(vd->fname, &st))
+ return (0);
+
+ if (st.st_dev == vd->fstat.st_dev && st.st_ino == vd->fstat.st_ino)
+ return (0);
+
+ VSL_Close(vd);
+ for (i = 0; i < 5; i++) {
+ if (!vsl_open(vd, 0))
+ return (1);
+ }
+ if (vsl_open(vd, 1))
+ return (-1);
+ return (1);
+}
+
+/*--------------------------------------------------------------------*/
+
struct shmalloc *
vsl_iter0(const struct VSL_data *vd)
{
More information about the varnish-commit
mailing list