Created
June 9, 2011 11:50
-
-
Save anonymous/1016579 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 6abdb8f49de62a0f5c087beb2a4320bc447f65aa | |
Author: Martin Blix Grydeland <martin@varnish-software.com> | |
Date: Thu Jun 9 13:49:37 2011 +0200 | |
Use file locking on SHMFILE to indicate this file is currently in use | |
diff --git a/bin/varnishd/mgt_shmem.c b/bin/varnishd/mgt_shmem.c | |
index f28bc9b..e628119 100644 | |
--- a/bin/varnishd/mgt_shmem.c | |
+++ b/bin/varnishd/mgt_shmem.c | |
@@ -101,6 +101,7 @@ | |
#include "heritage.h" | |
#include "vmb.h" | |
#include "vsm.h" | |
+#include "flopen.h" | |
#ifndef MAP_HASSEMAPHORE | |
#define MAP_HASSEMAPHORE 0 /* XXX Linux */ | |
@@ -125,11 +126,27 @@ vsl_n_check(int fd) | |
struct VSM_head slh; | |
int i; | |
struct stat st; | |
+ struct flock lock; | |
AZ(fstat(fd, &st)); | |
if (!S_ISREG(st.st_mode)) | |
ARGV_ERR("\tshmlog: Not a file\n"); | |
+ /* Test if the SHMFILE is locked by other Varnish */ | |
+ memset(&lock, 0, sizeof lock); | |
+ lock.l_type = F_WRLCK; | |
+ lock.l_whence = SEEK_SET; | |
+ if (fcntl(fd, F_GETLK, &lock) == 0 && lock.l_type != F_UNLCK) { | |
+ fprintf(stderr, | |
+ "SHMFILE locked by running varnishd master (pid=%jd)\n", | |
+ (intmax_t)lock.l_pid); | |
+ fprintf(stderr, | |
+ "(Use unique -n arguments if you want multiple " | |
+ "instances)\n"); | |
+ exit(2); | |
+ } | |
+ | |
+ /* Read owning pid from SHMFILE */ | |
memset(&slh, 0, sizeof slh); /* XXX: for flexelint */ | |
i = read(fd, &slh, sizeof slh); | |
if (i != sizeof slh) | |
@@ -138,15 +155,11 @@ vsl_n_check(int fd) | |
return; | |
if (slh.hdrsize != sizeof slh) | |
return; | |
- | |
if (slh.master_pid != 0 && !kill(slh.master_pid, 0)) { | |
- fprintf(stderr, | |
- "SHMFILE owned by running varnishd master (pid=%jd)\n", | |
- (intmax_t)slh.master_pid); | |
- fprintf(stderr, | |
- "(Use unique -n arguments if you want multiple " | |
- "instances.)\n"); | |
- exit(2); | |
+ fprintf(stderr, | |
+ "WARNING: Taking over SHMFILE marked as owned by " | |
+ "running process (pid=%jd)\n", | |
+ (intmax_t)slh.master_pid); | |
} | |
} | |
@@ -161,15 +174,20 @@ vsl_buildnew(const char *fn, unsigned size, int fill) | |
int i; | |
unsigned u; | |
char buf[64*1024]; | |
+ int flags; | |
(void)unlink(fn); | |
- vsl_fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0644); | |
+ vsl_fd = flopen(fn, O_RDWR | O_CREAT | O_EXCL | O_NONBLOCK, 0644); | |
if (vsl_fd < 0) { | |
fprintf(stderr, "Could not create %s: %s\n", | |
fn, strerror(errno)); | |
exit (1); | |
} | |
- | |
+ flags = fcntl(vsl_fd, F_GETFL); | |
+ assert(flags != -1); | |
+ flags &= ~O_NONBLOCK; | |
+ AZ(fcntl(vsl_fd, F_SETFL, flags)); | |
+ | |
memset(&slh, 0, sizeof slh); | |
slh.magic = VSM_HEAD_MAGIC; | |
slh.hdrsize = sizeof slh; | |
@@ -277,7 +295,6 @@ mgt_SHM_Init(const char *l_arg) | |
vsl_n_check(i); | |
(void)close(i); | |
} | |
- (void)close(i); | |
vsl_buildnew(VSM_FILENAME, size, fill); | |
VSM_head = (void *)mmap(NULL, size, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment