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

Simplify how we manage the -f argument:

The VCL file specified to -f must be read relative to the directory
from which varnishd is started, before we chdir to the workdir.

We used to deal with this by opening the file and passing the file
handle down.  It's simpler to just read the file and pass the actual
VCL code down.



git-svn-id: http://www.varnish-cache.org/svn/trunk@3415 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 8b6ea91b
...@@ -58,7 +58,7 @@ void MCF_ParamSet(struct cli *, const char *param, const char *val); ...@@ -58,7 +58,7 @@ void MCF_ParamSet(struct cli *, const char *param, const char *val);
/* mgt_vcc.c */ /* mgt_vcc.c */
void mgt_vcc_init(void); void mgt_vcc_init(void);
int mgt_vcc_default(const char *bflag, const char *fflag, int f_fd, int Cflag); int mgt_vcc_default(const char *bflag, char *vcl, int Cflag);
int mgt_push_vcls_and_start(unsigned *status, char **p); int mgt_push_vcls_and_start(unsigned *status, char **p);
int mgt_has_vcl(void); int mgt_has_vcl(void);
extern char *mgt_cc_cmd; extern char *mgt_cc_cmd;
......
...@@ -201,40 +201,27 @@ mgt_run_cc(const char *source, struct vsb *sb) ...@@ -201,40 +201,27 @@ mgt_run_cc(const char *source, struct vsb *sb)
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
static char * static char *
mgt_VccCompile(struct vsb *sb, const char *b, const char *e, int C_flag) mgt_VccCompile(struct vsb **sb, const char *b, int C_flag)
{ {
char *csrc, *vf = NULL; char *csrc, *vf = NULL;
csrc = VCC_Compile(sb, b, e); *sb = vsb_newauto();
if (csrc != NULL) { XXXAN(*sb);
if (C_flag) csrc = VCC_Compile(*sb, b, NULL);
(void)fputs(csrc, stdout);
vf = mgt_run_cc(csrc, sb);
if (C_flag && vf != NULL)
AZ(unlink(vf));
free(csrc);
}
return (vf);
}
static char *
mgt_VccCompileFile(struct vsb *sb, const char *fn, int C_flag, int fd)
{
char *csrc, *vf = NULL;
csrc = VCC_CompileFile(sb, fn, fd);
if (csrc != NULL) { if (csrc != NULL) {
if (C_flag) if (C_flag)
(void)fputs(csrc, stdout); (void)fputs(csrc, stdout);
vf = mgt_run_cc(csrc, sb); vf = mgt_run_cc(csrc, *sb);
if (C_flag && vf != NULL) if (C_flag && vf != NULL)
AZ(unlink(vf)); AZ(unlink(vf));
free(csrc); free(csrc);
} }
vsb_finish(*sb);
AZ(vsb_overflowed(*sb));
return (vf); return (vf);
} }
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
static struct vclprog * static struct vclprog *
...@@ -290,16 +277,15 @@ mgt_vcc_delbyname(const char *name) ...@@ -290,16 +277,15 @@ mgt_vcc_delbyname(const char *name)
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
int int
mgt_vcc_default(const char *b_arg, const char *f_arg, int f_fd, int C_flag) mgt_vcc_default(const char *b_arg, char *vcl, int C_flag)
{ {
char *addr, *port; char *addr, *port;
char *buf, *vf; char *vf;
struct vsb *sb; struct vsb *sb;
struct vclprog *vp; struct vclprog *vp;
sb = vsb_newauto();
XXXAN(sb);
if (b_arg != NULL) { if (b_arg != NULL) {
AZ(vcl);
/* /*
* XXX: should do a "HEAD /" on the -b argument to see that * XXX: should do a "HEAD /" on the -b argument to see that
* XXX: it even works. On the other hand, we should do that * XXX: it even works. On the other hand, we should do that
...@@ -318,26 +304,21 @@ mgt_vcc_default(const char *b_arg, const char *f_arg, int f_fd, int C_flag) ...@@ -318,26 +304,21 @@ mgt_vcc_default(const char *b_arg, const char *f_arg, int f_fd, int C_flag)
*/ */
free(port); free(port);
fprintf(stderr, "invalid backend address\n"); fprintf(stderr, "invalid backend address\n");
vsb_delete(sb);
return (1); return (1);
} }
buf = NULL; asprintf(&vcl,
asprintf(&buf,
"backend default {\n" "backend default {\n"
" .host = \"%s\";\n" " .host = \"%s\";\n"
" .port = \"%s\";\n" " .port = \"%s\";\n"
"}\n", addr, port ? port : "http"); "}\n", addr, port ? port : "http");
free(addr); free(addr);
free(port); free(port);
AN(buf); AN(vcl);
vf = mgt_VccCompile(sb, buf, NULL, C_flag);
free(buf);
} else {
vf = mgt_VccCompileFile(sb, f_arg, C_flag, f_fd);
} }
vsb_finish(sb);
AZ(vsb_overflowed(sb)); vf = mgt_VccCompile(&sb, vcl, C_flag);
free(vcl);
if (vsb_len(sb) > 0) if (vsb_len(sb) > 0)
fprintf(stderr, "%s", vsb_data(sb)); fprintf(stderr, "%s", vsb_data(sb));
vsb_delete(sb); vsb_delete(sb);
...@@ -432,11 +413,7 @@ mcf_config_inline(struct cli *cli, const char * const *av, void *priv) ...@@ -432,11 +413,7 @@ mcf_config_inline(struct cli *cli, const char * const *av, void *priv)
return; return;
} }
sb = vsb_newauto(); vf = mgt_VccCompile(&sb, av[3], 0);
XXXAN(sb);
vf = mgt_VccCompile(sb, av[3], NULL, 0);
vsb_finish(sb);
AZ(vsb_overflowed(sb));
if (vsb_len(sb) > 0) if (vsb_len(sb) > 0)
cli_out(cli, "%s", vsb_data(sb)); cli_out(cli, "%s", vsb_data(sb));
vsb_delete(sb); vsb_delete(sb);
...@@ -459,7 +436,7 @@ mcf_config_inline(struct cli *cli, const char * const *av, void *priv) ...@@ -459,7 +436,7 @@ mcf_config_inline(struct cli *cli, const char * const *av, void *priv)
void void
mcf_config_load(struct cli *cli, const char * const *av, void *priv) mcf_config_load(struct cli *cli, const char * const *av, void *priv)
{ {
char *vf; char *vf, *vcl;
struct vsb *sb; struct vsb *sb;
unsigned status; unsigned status;
char *p = NULL; char *p = NULL;
...@@ -473,11 +450,16 @@ mcf_config_load(struct cli *cli, const char * const *av, void *priv) ...@@ -473,11 +450,16 @@ mcf_config_load(struct cli *cli, const char * const *av, void *priv)
return; return;
} }
sb = vsb_newauto(); vcl = vreadfile(av[3]);
XXXAN(sb); if (vcl == NULL) {
vf = mgt_VccCompileFile(sb, av[3], 0, -1); cli_out(cli, "Cannot open '%s'", av[3]);
vsb_finish(sb); cli_result(cli, CLIS_PARAM);
AZ(vsb_overflowed(sb)); return;
}
vf = mgt_VccCompile(&sb, vcl, 0);
free(vcl);
if (vsb_len(sb) > 0) if (vsb_len(sb) > 0)
cli_out(cli, "%s", vsb_data(sb)); cli_out(cli, "%s", vsb_data(sb));
vsb_delete(sb); vsb_delete(sb);
......
...@@ -424,14 +424,13 @@ main(int argc, char * const *argv) ...@@ -424,14 +424,13 @@ main(int argc, char * const *argv)
const char *l_arg = "80m"; const char *l_arg = "80m";
uintmax_t l_size; uintmax_t l_size;
const char *q; const char *q;
int f_fd = -1;
const char *h_arg = "classic"; const char *h_arg = "classic";
const char *n_arg = NULL; const char *n_arg = NULL;
const char *P_arg = NULL; const char *P_arg = NULL;
const char *s_arg = "file"; const char *s_arg = "file";
int s_arg_given = 0; int s_arg_given = 0;
const char *T_arg = NULL; const char *T_arg = NULL;
char *p; char *p, *vcl = NULL;
struct cli cli[1]; struct cli cli[1];
struct pidfh *pfh = NULL; struct pidfh *pfh = NULL;
char dirname[1024]; char dirname[1024];
...@@ -567,9 +566,9 @@ main(int argc, char * const *argv) ...@@ -567,9 +566,9 @@ main(int argc, char * const *argv)
} }
if (f_arg != NULL) { if (f_arg != NULL) {
f_fd = open(f_arg, O_RDONLY); vcl = vreadfile(f_arg);
if (f_fd < 0) { if (vcl == NULL) {
fprintf(stderr, "Cannot open '%s': %s\n", fprintf(stderr, "Cannot read '%s': %s\n",
f_arg, strerror(errno)); f_arg, strerror(errno));
exit(1); exit(1);
} }
...@@ -606,7 +605,7 @@ main(int argc, char * const *argv) ...@@ -606,7 +605,7 @@ main(int argc, char * const *argv)
} }
if (b_arg != NULL || f_arg != NULL) if (b_arg != NULL || f_arg != NULL)
if (mgt_vcc_default(b_arg, f_arg, f_fd, C_flag)) if (mgt_vcc_default(b_arg, vcl, C_flag))
exit (2); exit (2);
if (C_flag) if (C_flag)
......
...@@ -86,7 +86,7 @@ void varnish_version(const char *); ...@@ -86,7 +86,7 @@ void varnish_version(const char *);
/* from libvarnish/vtmpfile.c */ /* from libvarnish/vtmpfile.c */
int vtmpfile(char *); int vtmpfile(char *);
char *vreadfile(int fd); char *vreadfile(const char *fn);
/* /*
* assert(), AN() and AZ() are static checks that should not happen. * assert(), AN() and AZ() are static checks that should not happen.
......
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
*/ */
char *VCC_Compile(struct vsb *sb, const char *b, const char *e); char *VCC_Compile(struct vsb *sb, const char *b, const char *e);
char *VCC_CompileFile(struct vsb *sb, const char *fn, int fd);
void VCC_InitCompile(const char *default_vcl); void VCC_InitCompile(const char *default_vcl);
...@@ -78,8 +78,8 @@ vtmpfile(char *template) ...@@ -78,8 +78,8 @@ vtmpfile(char *template)
/* not reached */ /* not reached */
} }
char * static char *
vreadfile(int fd) vreadfd(int fd)
{ {
struct stat st; struct stat st;
char *f; char *f;
...@@ -95,3 +95,19 @@ vreadfile(int fd) ...@@ -95,3 +95,19 @@ vreadfile(int fd)
f[i] = '\0'; f[i] = '\0';
return (f); return (f);
} }
char *
vreadfile(const char *fn)
{
int fd, err;
char *r;
fd = open(fn, O_RDONLY);
if (fd < 0)
return (NULL);
r = vreadfd(fd);
err = errno;
AZ(close(fd));
errno = err;
return (r);
}
...@@ -399,22 +399,17 @@ vcc_destroy_source(struct source *sp) ...@@ -399,22 +399,17 @@ vcc_destroy_source(struct source *sp)
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
static struct source * static struct source *
vcc_file_source(struct vsb *sb, const char *fn, int fd) vcc_file_source(struct vsb *sb, const char *fn)
{ {
char *f; char *f;
struct source *sp; struct source *sp;
if (fd < 0) { f = vreadfile(fn);
fd = open(fn, O_RDONLY); if (f == NULL) {
if (fd < 0) { vsb_printf(sb, "Cannot read file '%s': %s\n",
vsb_printf(sb, "Cannot open file '%s': %s\n", fn, strerror(errno));
fn, strerror(errno)); return (NULL);
return (NULL);
}
} }
f = vreadfile(fd);
AN(f);
AZ(close(fd));
sp = vcc_new_source(f, NULL, fn); sp = vcc_new_source(f, NULL, fn);
sp->freeit = f; sp->freeit = f;
return (sp); return (sp);
...@@ -450,7 +445,7 @@ vcc_resolve_includes(struct tokenlist *tl) ...@@ -450,7 +445,7 @@ vcc_resolve_includes(struct tokenlist *tl)
} }
assert(t2 != NULL); assert(t2 != NULL);
sp = vcc_file_source(tl->sb, t1->dec, -1); sp = vcc_file_source(tl->sb, t1->dec);
if (sp == NULL) { if (sp == NULL) {
vcc_ErrWhere(tl, t1); vcc_ErrWhere(tl, t1);
return; return;
...@@ -667,24 +662,6 @@ VCC_Compile(struct vsb *sb, const char *b, const char *e) ...@@ -667,24 +662,6 @@ VCC_Compile(struct vsb *sb, const char *b, const char *e)
return (r); return (r);
} }
/*--------------------------------------------------------------------
* Compile the VCL code from the file named. Error messages, if any
* are formatted into the vsb.
*/
char *
VCC_CompileFile(struct vsb *sb, const char *fn, int fd)
{
struct source *sp;
char *r;
sp = vcc_file_source(sb, fn, fd);
if (sp == NULL)
return (NULL);
r = vcc_CompileSource(sb, sp);
return (r);
}
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
* Initialize the compiler and register the default VCL code for later * Initialize the compiler and register the default VCL code for later
* compilation runs. * compilation runs.
......
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