Commit fcfc338d authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Instead of our own home-rolled collision check, reuse VPF and always

put a pid-file in the workdir.

Improve the collision error message for -P by including the other pid.
parent 995c8b2c
...@@ -499,6 +499,7 @@ main(int argc, char * const *argv) ...@@ -499,6 +499,7 @@ main(int argc, char * const *argv)
struct f_arg *fa; struct f_arg *fa;
struct vsb *vsb; struct vsb *vsb;
VTAILQ_HEAD(,f_arg) f_args = VTAILQ_HEAD_INITIALIZER(f_args); VTAILQ_HEAD(,f_arg) f_args = VTAILQ_HEAD_INITIALIZER(f_args);
pid_t pid;
setbuf(stdout, NULL); setbuf(stdout, NULL);
setbuf(stderr, NULL); setbuf(stderr, NULL);
...@@ -787,9 +788,15 @@ main(int argc, char * const *argv) ...@@ -787,9 +788,15 @@ main(int argc, char * const *argv)
} }
VJ_master(JAIL_MASTER_FILE); VJ_master(JAIL_MASTER_FILE);
if (P_arg && (pfh = VPF_Open(P_arg, 0644, NULL)) == NULL) if (P_arg) {
ARGV_ERR("Could not open pid/lock (-P) file (%s): %s\n", pfh = VPF_Open(P_arg, 0644, &pid);
if (pfh == NULL && errno == EEXIST)
ARGV_ERR("Varnishd is already running (pid=%jd)\n",
(intmax_t)pid);
if (pfh == NULL)
ARGV_ERR("Could not open pid-file (%s): %s\n",
P_arg, strerror(errno)); P_arg, strerror(errno));
}
VJ_master(JAIL_MASTER_LOW); VJ_master(JAIL_MASTER_LOW);
/* If no -s argument specified, process default -s argument */ /* If no -s argument specified, process default -s argument */
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "common/heritage.h" #include "common/heritage.h"
#include "vfl.h" #include "vfl.h"
#include "vpf.h"
#include "vsm_priv.h" #include "vsm_priv.h"
#include "vfil.h" #include "vfil.h"
...@@ -59,6 +60,8 @@ ...@@ -59,6 +60,8 @@
static void *mgt_vsm_p; static void *mgt_vsm_p;
static ssize_t mgt_vsm_l; static ssize_t mgt_vsm_l;
static struct vpf_fh *priv_vpf;
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
* Use a bogo-VSM to hold master-copies of the VSM chunks the master * Use a bogo-VSM to hold master-copies of the VSM chunks the master
* publishes, such as -S & -T arguments. * publishes, such as -S & -T arguments.
...@@ -83,60 +86,6 @@ mgt_SHM_static_alloc(const void *ptr, ssize_t size, ...@@ -83,60 +86,6 @@ mgt_SHM_static_alloc(const void *ptr, ssize_t size,
} }
} }
/*--------------------------------------------------------------------
* Check that we are not started with the same -n argument as an already
* running varnishd.
*
* Non-zero return means we should exit and not trample the file.
*
*/
static int
vsm_n_check(void)
{
int fd, i;
struct stat st;
pid_t pid;
struct VSM_head vsmh;
int retval = 1;
fd = open(VSM_FILENAME, O_RDWR);
if (fd < 0)
return (0);
AZ(fstat(fd, &st));
if (!S_ISREG(st.st_mode)) {
fprintf(stderr,
"VSM (%s) not a regular file.\n", VSM_FILENAME);
} else {
i = VFL_Test(fd, &pid);
if (i < 0) {
fprintf(stderr,
"Cannot determine locking status of VSM (%s)\n.",
VSM_FILENAME);
} else if (i == 0) {
/*
* File is unlocked, mark it as dead, to help any
* consumers still stuck on it.
*/
if (pread(fd, &vsmh, sizeof vsmh, 0) == sizeof vsmh) {
vsmh.alloc_seq = 0;
assert(sizeof vsmh ==
pwrite(fd, &vsmh, sizeof vsmh, 0));
}
retval = 0;
} else {
/* The VSM is locked, we won't touch it. */
fprintf(stderr,
"VSM locked by running varnishd master (pid=%jd)\n"
"(Use unique -n arguments if you want"
" multiple instances)\n", (intmax_t)pid);
}
}
(void)close(fd);
return (retval);
}
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
* Build a zeroed file * Build a zeroed file
*/ */
...@@ -327,12 +276,15 @@ mgt_shm_atexit(void) ...@@ -327,12 +276,15 @@ mgt_shm_atexit(void)
void void
mgt_SHM_Init(void) mgt_SHM_Init(void)
{ {
int i; pid_t pid = 0;
/* Collision check with already running varnishd */ priv_vpf = VPF_Open("_.pid", 0644, &pid);
i = vsm_n_check(); if (priv_vpf == NULL && errno == EEXIST)
if (i) ARGV_ERR("Varnishd is already running (pid=%jd)\n",
exit(2); (intmax_t)pid);
if (priv_vpf == NULL)
ARGV_ERR("Failure on _.pid: %s\n", strerror(errno));
AZ(VPF_Write(priv_vpf));
/* Create our static VSM instance */ /* Create our static VSM instance */
static_vsm = VSM_common_new(static_vsm_buf, sizeof static_vsm_buf); static_vsm = VSM_common_new(static_vsm_buf, sizeof static_vsm_buf);
......
...@@ -14,5 +14,14 @@ client c1 { ...@@ -14,5 +14,14 @@ client c1 {
delay .2 delay .2
shell -err -expect {Could not open pid/lock} \ shell -err -expect {Error: Could not open pid-file} {
"varnishd -P ${v1_name}/varnishd.pid -b 127.0.0.1:80 -a :0 -n ${tmpdir}" varnishd -P /dev/tty -b 127.0.0.1:80 -a :0 -n ${tmpdir}
}
shell -err -expect {Error: Varnishd is already running} {
varnishd -P ${v1_name}/varnishd.pid -b 127.0.0.1:80 -a :0 -n ${tmpdir}
}
shell -err -expect {Error: Varnishd is already running} {
varnishd -b 127.0.0.1:80 -a:0 -n ${tmpdir}/v1 -F
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment