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

Refactor pid-file creation into a separate function.

parent 899ca1bd
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <ctype.h> #include <ctype.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -431,6 +432,32 @@ mgt_f_read(const char *fn) ...@@ -431,6 +432,32 @@ mgt_f_read(const char *fn)
VTAILQ_INSERT_TAIL(&f_args, fa, list); VTAILQ_INSERT_TAIL(&f_args, fa, list);
} }
static struct vpf_fh *
create_pid_file(pid_t *ppid, const char *fmt, ...)
{
struct vsb *vsb;
va_list ap;
struct vpf_fh *pfh;
va_start(ap, fmt);
vsb = VSB_new_auto();
AN(vsb);
VSB_vprintf(vsb, fmt, ap);
AZ(VSB_finish(vsb));
VJ_master(JAIL_MASTER_FILE);
pfh = VPF_Open(VSB_data(vsb), 0644, ppid);
if (pfh == NULL && errno == EEXIST)
ARGV_ERR(
"Varnishd is already running (pid=%jd) (pidfile=%s)\n",
(intmax_t)*ppid, VSB_data(vsb));
if (pfh == NULL)
ARGV_ERR("Could not open pid-file (%s): %s\n",
VSB_data(vsb), vstrerror(errno));
VJ_master(JAIL_MASTER_LOW);
VSB_destroy(&vsb);
return (pfh);
}
int int
main(int argc, char * const *argv) main(int argc, char * const *argv)
{ {
...@@ -764,29 +791,10 @@ main(int argc, char * const *argv) ...@@ -764,29 +791,10 @@ main(int argc, char * const *argv)
dirname, vstrerror(errno)); dirname, vstrerror(errno));
} }
vsb = VSB_new_auto(); pfh1 = create_pid_file(&pid, "%s/_.pid", dirname);
AN(vsb);
VSB_printf(vsb, "%s/_.pid", dirname); if (P_arg)
AZ(VSB_finish(vsb)); pfh2 = create_pid_file(&pid, "%s", P_arg);
VJ_master(JAIL_MASTER_FILE);
pfh1 = VPF_Open(VSB_data(vsb), 0644, &pid);
if (pfh1 == NULL && errno == EEXIST)
ARGV_ERR("Varnishd is already running (pid=%jd)\n",
(intmax_t)pid);
if (pfh1 == NULL)
ARGV_ERR("Could not open pid-file (%s): %s\n",
VSB_data(vsb), vstrerror(errno));
VSB_destroy(&vsb);
if (P_arg) {
pfh2 = VPF_Open(P_arg, 0644, &pid);
if (pfh2 == NULL && errno == EEXIST)
ARGV_ERR("Varnishd is already running (pid=%jd)\n",
(intmax_t)pid);
if (pfh2 == NULL)
ARGV_ERR("Could not open pid-file (%s): %s\n",
P_arg, vstrerror(errno));
}
VJ_master(JAIL_MASTER_LOW);
/* If no -s argument specified, process default -s argument */ /* If no -s argument specified, process default -s argument */
if (!s_arg_given) if (!s_arg_given)
......
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