From: Tollef Fog Heen Date: Sun, 16 Nov 2008 18:34:00 +0000 (+0100) Subject: Implement vcl.reload command X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd9fb3701b1ee28c09b1b5d566ace71fd10b3b7d;p=varnish Implement vcl.reload command --- diff --git a/varnish-cache/bin/varnishd/cache_vcl.c b/varnish-cache/bin/varnishd/cache_vcl.c index fb65a2ae..64c34680 100644 --- a/varnish-cache/bin/varnishd/cache_vcl.c +++ b/varnish-cache/bin/varnishd/cache_vcl.c @@ -239,6 +239,37 @@ ccf_config_list(struct cli *cli, const char * const *av, void *priv) } } +static void +ccf_config_reload(struct cli *cli, const char * const *av, void *priv) +{ + char *t; + struct vcls *vcl, *vcl_discard; + (void)av; + (void)priv; + ASSERT_CLI(); + + asprintf(&t, "vcltmp-%d", random()); + if (VCL_Load(av[3], t, cli)) + cli_result(cli, CLIS_PARAM); + + vcl = vcl_find(t); + vcl_discard = vcl_find(av[2]); + REPLACE(vcl->name, av[2]); + + Lck_Lock(&vcl_mtx); + vcl_active = vcl; + Lck_Unlock(&vcl_mtx); + + VSL_stats->n_vcl_discard++; + VSL_stats->n_vcl_avail--; + vcl_discard->conf->discard = 1; + + if (vcl_discard->conf->busy == 0) + VCL_Nuke(vcl_discard); + + return; +} + static void ccf_config_load(struct cli *cli, const char * const *av, void *priv) { @@ -322,6 +353,7 @@ VCL_##func##_method(struct sess *sp) \ static struct cli_proto vcl_cmds[] = { { CLI_VCL_LOAD, ccf_config_load }, + { CLI_VCL_RELOAD, ccf_config_reload }, { CLI_VCL_LIST, ccf_config_list }, { CLI_VCL_DISCARD, ccf_config_discard }, { CLI_VCL_USE, ccf_config_use }, diff --git a/varnish-cache/bin/varnishd/mgt_cli.c b/varnish-cache/bin/varnishd/mgt_cli.c index 07b5ce48..75223a20 100644 --- a/varnish-cache/bin/varnishd/mgt_cli.c +++ b/varnish-cache/bin/varnishd/mgt_cli.c @@ -135,6 +135,7 @@ static struct cli_proto cli_proto[] = { { CLI_SERVER_STOP, mcf_server_startstop, cli_proto }, { CLI_STATS, mcf_stats, NULL }, { CLI_VCL_LOAD, mcf_config_load, NULL }, + { CLI_VCL_RELOAD, mcf_config_reload, NULL }, { CLI_VCL_INLINE, mcf_config_inline, NULL }, { CLI_VCL_USE, mcf_config_use, NULL }, { CLI_VCL_DISCARD, mcf_config_discard, NULL }, diff --git a/varnish-cache/bin/varnishd/mgt_cli.h b/varnish-cache/bin/varnishd/mgt_cli.h index b26dafe3..e1e04bfe 100644 --- a/varnish-cache/bin/varnishd/mgt_cli.h +++ b/varnish-cache/bin/varnishd/mgt_cli.h @@ -39,6 +39,7 @@ cli_func_t mcf_param_set; /* mgt_vcc.c */ cli_func_t mcf_config_load; +cli_func_t mcf_config_reload; cli_func_t mcf_config_inline; cli_func_t mcf_config_use; cli_func_t mcf_config_discard; diff --git a/varnish-cache/bin/varnishd/mgt_vcc.c b/varnish-cache/bin/varnishd/mgt_vcc.c index c782e719..b713dea1 100644 --- a/varnish-cache/bin/varnishd/mgt_vcc.c +++ b/varnish-cache/bin/varnishd/mgt_vcc.c @@ -488,6 +488,51 @@ mcf_config_inline(struct cli *cli, const char * const *av, void *priv) free(p); } +void +mcf_config_reload(struct cli *cli, const char *const *av, void *priv) +{ + char *vf; + struct vsb *sb; + unsigned status; + char *p = NULL; + struct vclprog *vp; + int active; + + vp = mgt_vcc_byname(av[2]); + if (vp == NULL) { + cli_out(cli, "No VCL program named %s, use vcl.load", av[2]); + cli_result(cli, CLIS_PARAM); + return; + } + + sb = vsb_newauto(); + XXXAN(sb); + vf = mgt_VccCompileFile(sb, av[3], 0, -1); + vsb_finish(sb); + AZ(vsb_overflowed(sb)); + if (vsb_len(sb) > 0) + cli_out(cli, "%s", vsb_data(sb)); + vsb_delete(sb); + if (vf == NULL) { + cli_out(cli, "VCL compilation failed"); + cli_result(cli, CLIS_PARAM); + return; + } + + if (child_pid >= 0 && + mgt_cli_askchild(&status, &p, "vcl.reload %s %s\n", av[2], vf)) { + cli_result(cli, status); + cli_out(cli, "%s", p); + } else { + active = vp->active; + mgt_vcc_del(vp); + vp = mgt_vcc_add(av[2], vf); + vp->active = active; + } + cli_out(cli, "VCL %s reloaded.", av[2]); + free(p); +} + void mcf_config_load(struct cli *cli, const char * const *av, void *priv) { diff --git a/varnish-cache/include/cli.h b/varnish-cache/include/cli.h index 6c1602c0..8aa2fa80 100644 --- a/varnish-cache/include/cli.h +++ b/varnish-cache/include/cli.h @@ -99,6 +99,12 @@ "\tCompile and load the VCL file under the name provided.", \ 2, 2 +#define CLI_VCL_RELOAD \ + "vcl.reload", \ + "vcl.reload ", \ + "\tLoad the VCL file under the name provided, discarding the old VCL", \ + 2, 2 + #define CLI_VCL_INLINE \ "vcl.inline", \ "vcl.inline ", \