From 6ad26022dd996b5cece6d18950fea9bab355f145 Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 21 Nov 2008 12:09:46 +0000 Subject: [PATCH] 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: svn+ssh://projects.linpro.no/svn/varnish/trunk@3415 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/mgt.h | 2 +- varnish-cache/bin/varnishd/mgt_vcc.c | 72 ++++++++++--------------- varnish-cache/bin/varnishd/varnishd.c | 11 ++-- varnish-cache/include/libvarnish.h | 2 +- varnish-cache/include/libvcl.h | 1 - varnish-cache/lib/libvarnish/vtmpfile.c | 20 ++++++- varnish-cache/lib/libvcl/vcc_compile.c | 37 +++---------- 7 files changed, 59 insertions(+), 86 deletions(-) diff --git a/varnish-cache/bin/varnishd/mgt.h b/varnish-cache/bin/varnishd/mgt.h index 088f87e5..a64eb26d 100644 --- a/varnish-cache/bin/varnishd/mgt.h +++ b/varnish-cache/bin/varnishd/mgt.h @@ -58,7 +58,7 @@ void MCF_ParamSet(struct cli *, const char *param, const char *val); /* mgt_vcc.c */ 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_has_vcl(void); extern char *mgt_cc_cmd; diff --git a/varnish-cache/bin/varnishd/mgt_vcc.c b/varnish-cache/bin/varnishd/mgt_vcc.c index 14f53d06..08abf037 100644 --- a/varnish-cache/bin/varnishd/mgt_vcc.c +++ b/varnish-cache/bin/varnishd/mgt_vcc.c @@ -201,40 +201,27 @@ mgt_run_cc(const char *source, struct vsb *sb) /*--------------------------------------------------------------------*/ 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; - csrc = VCC_Compile(sb, b, e); - if (csrc != NULL) { - if (C_flag) - (void)fputs(csrc, stdout); - vf = mgt_run_cc(csrc, sb); - if (C_flag && vf != NULL) - AZ(unlink(vf)); - free(csrc); - } - return (vf); -} + *sb = vsb_newauto(); + XXXAN(*sb); + csrc = VCC_Compile(*sb, b, NULL); -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 (C_flag) (void)fputs(csrc, stdout); - vf = mgt_run_cc(csrc, sb); + vf = mgt_run_cc(csrc, *sb); if (C_flag && vf != NULL) AZ(unlink(vf)); free(csrc); } + vsb_finish(*sb); + AZ(vsb_overflowed(*sb)); return (vf); } - /*--------------------------------------------------------------------*/ static struct vclprog * @@ -290,16 +277,15 @@ mgt_vcc_delbyname(const char *name) /*--------------------------------------------------------------------*/ 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 *buf, *vf; + char *vf; struct vsb *sb; struct vclprog *vp; - sb = vsb_newauto(); - XXXAN(sb); if (b_arg != NULL) { + AZ(vcl); /* * XXX: should do a "HEAD /" on the -b argument to see 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) */ free(port); fprintf(stderr, "invalid backend address\n"); - vsb_delete(sb); return (1); } - buf = NULL; - asprintf(&buf, + asprintf(&vcl, "backend default {\n" " .host = \"%s\";\n" " .port = \"%s\";\n" "}\n", addr, port ? port : "http"); free(addr); free(port); - AN(buf); - vf = mgt_VccCompile(sb, buf, NULL, C_flag); - free(buf); - } else { - vf = mgt_VccCompileFile(sb, f_arg, C_flag, f_fd); + AN(vcl); } - vsb_finish(sb); - AZ(vsb_overflowed(sb)); + + vf = mgt_VccCompile(&sb, vcl, C_flag); + free(vcl); if (vsb_len(sb) > 0) fprintf(stderr, "%s", vsb_data(sb)); vsb_delete(sb); @@ -432,11 +413,7 @@ mcf_config_inline(struct cli *cli, const char * const *av, void *priv) return; } - sb = vsb_newauto(); - XXXAN(sb); - vf = mgt_VccCompile(sb, av[3], NULL, 0); - vsb_finish(sb); - AZ(vsb_overflowed(sb)); + vf = mgt_VccCompile(&sb, av[3], 0); if (vsb_len(sb) > 0) cli_out(cli, "%s", vsb_data(sb)); vsb_delete(sb); @@ -459,7 +436,7 @@ mcf_config_inline(struct cli *cli, const char * const *av, void *priv) void mcf_config_load(struct cli *cli, const char * const *av, void *priv) { - char *vf; + char *vf, *vcl; struct vsb *sb; unsigned status; char *p = NULL; @@ -473,11 +450,16 @@ mcf_config_load(struct cli *cli, const char * const *av, void *priv) return; } - sb = vsb_newauto(); - XXXAN(sb); - vf = mgt_VccCompileFile(sb, av[3], 0, -1); - vsb_finish(sb); - AZ(vsb_overflowed(sb)); + vcl = vreadfile(av[3]); + if (vcl == NULL) { + cli_out(cli, "Cannot open '%s'", av[3]); + cli_result(cli, CLIS_PARAM); + return; + } + + vf = mgt_VccCompile(&sb, vcl, 0); + free(vcl); + if (vsb_len(sb) > 0) cli_out(cli, "%s", vsb_data(sb)); vsb_delete(sb); diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index 5759666d..b2ffd2b8 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -424,14 +424,13 @@ main(int argc, char * const *argv) const char *l_arg = "80m"; uintmax_t l_size; const char *q; - int f_fd = -1; const char *h_arg = "classic"; const char *n_arg = NULL; const char *P_arg = NULL; const char *s_arg = "file"; int s_arg_given = 0; const char *T_arg = NULL; - char *p; + char *p, *vcl = NULL; struct cli cli[1]; struct pidfh *pfh = NULL; char dirname[1024]; @@ -567,9 +566,9 @@ main(int argc, char * const *argv) } if (f_arg != NULL) { - f_fd = open(f_arg, O_RDONLY); - if (f_fd < 0) { - fprintf(stderr, "Cannot open '%s': %s\n", + vcl = vreadfile(f_arg); + if (vcl == NULL) { + fprintf(stderr, "Cannot read '%s': %s\n", f_arg, strerror(errno)); exit(1); } @@ -606,7 +605,7 @@ main(int argc, char * const *argv) } 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); if (C_flag) diff --git a/varnish-cache/include/libvarnish.h b/varnish-cache/include/libvarnish.h index 99ef27d8..04dac6b8 100644 --- a/varnish-cache/include/libvarnish.h +++ b/varnish-cache/include/libvarnish.h @@ -86,7 +86,7 @@ void varnish_version(const char *); /* from libvarnish/vtmpfile.c */ int vtmpfile(char *); -char *vreadfile(int fd); +char *vreadfile(const char *fn); /* * assert(), AN() and AZ() are static checks that should not happen. diff --git a/varnish-cache/include/libvcl.h b/varnish-cache/include/libvcl.h index b72e97e4..f8c7c3f8 100644 --- a/varnish-cache/include/libvcl.h +++ b/varnish-cache/include/libvcl.h @@ -30,7 +30,6 @@ */ 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); diff --git a/varnish-cache/lib/libvarnish/vtmpfile.c b/varnish-cache/lib/libvarnish/vtmpfile.c index 733efa26..a0d5452f 100644 --- a/varnish-cache/lib/libvarnish/vtmpfile.c +++ b/varnish-cache/lib/libvarnish/vtmpfile.c @@ -78,8 +78,8 @@ vtmpfile(char *template) /* not reached */ } -char * -vreadfile(int fd) +static char * +vreadfd(int fd) { struct stat st; char *f; @@ -95,3 +95,19 @@ vreadfile(int fd) f[i] = '\0'; 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); +} diff --git a/varnish-cache/lib/libvcl/vcc_compile.c b/varnish-cache/lib/libvcl/vcc_compile.c index a7674ce5..625ec44c 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.c +++ b/varnish-cache/lib/libvcl/vcc_compile.c @@ -399,22 +399,17 @@ vcc_destroy_source(struct source *sp) /*--------------------------------------------------------------------*/ 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; struct source *sp; - if (fd < 0) { - fd = open(fn, O_RDONLY); - if (fd < 0) { - vsb_printf(sb, "Cannot open file '%s': %s\n", - fn, strerror(errno)); - return (NULL); - } + f = vreadfile(fn); + if (f == NULL) { + vsb_printf(sb, "Cannot read file '%s': %s\n", + fn, strerror(errno)); + return (NULL); } - f = vreadfile(fd); - AN(f); - AZ(close(fd)); sp = vcc_new_source(f, NULL, fn); sp->freeit = f; return (sp); @@ -450,7 +445,7 @@ vcc_resolve_includes(struct tokenlist *tl) } assert(t2 != NULL); - sp = vcc_file_source(tl->sb, t1->dec, -1); + sp = vcc_file_source(tl->sb, t1->dec); if (sp == NULL) { vcc_ErrWhere(tl, t1); return; @@ -667,24 +662,6 @@ VCC_Compile(struct vsb *sb, const char *b, const char *e) 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 * compilation runs. -- 2.39.5