Commit 02edb664 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add config.load, config.inline and config.use commands.


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@83 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 870b7dd1
......@@ -51,3 +51,9 @@ void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...);
/* cache_vcl.c */
int CVCL_Load(const char *fn, const char *name);
#ifdef CLI_PRIV_H
cli_func_t cli_func_config_list;
cli_func_t cli_func_config_load;
cli_func_t cli_func_config_unload;
cli_func_t cli_func_config_use;
#endif
......@@ -78,6 +78,10 @@ cli_func_ping(struct cli *cli, char **av, void *priv __unused)
static struct cli_proto cli_proto[] = {
{ CLI_URL_QUERY, cli_func_url_query },
{ CLI_CONFIG_LOAD, cli_func_config_load },
{ CLI_CONFIG_LIST, cli_func_config_list },
{ CLI_CONFIG_UNLOAD, cli_func_config_unload },
{ CLI_CONFIG_USE, cli_func_config_use },
{ CLI_PING, cli_func_ping },
{ NULL }
};
......
......@@ -3,31 +3,142 @@
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/queue.h>
#include "cli.h"
#include "cli_priv.h"
#include "vcl_lang.h"
#include "cache.h"
struct vcls {
TAILQ_ENTRY(vcls) list;
const char *name;
void *dlh;
struct VCL_conf *conf;
};
static TAILQ_HEAD(, vcls) vcl_head =
TAILQ_HEAD_INITIALIZER(vcl_head);
static struct vcls *active_vcl;
int
CVCL_Load(const char *fn, const char *name)
{
void *dlh;
struct VCL_conf *vc;
struct vcls *vcl;
vcl = calloc(sizeof *vcl, 1);
assert(vcl != NULL);
dlh = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
if (dlh == NULL) {
vcl->dlh = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
if (vcl->dlh == NULL) {
fprintf(stderr, "dlopen(%s): %s\n", fn, dlerror());
free(vcl);
return (1);
}
vc = dlsym(dlh, "VCL_conf");
if (vc == NULL) {
vcl->conf = dlsym(vcl->dlh, "VCL_conf");
if (vcl->conf == NULL) {
fprintf(stderr, "No VCL_conf symbol\n");
dlclose(vcl->dlh);
free(vcl);
return (1);
}
if (vc->magic != VCL_CONF_MAGIC) {
if (vcl->conf->magic != VCL_CONF_MAGIC) {
fprintf(stderr, "Wrong VCL_CONF_MAGIC\n");
dlclose(vcl->dlh);
free(vcl);
return (1);
}
vcl->name = strdup(name);
assert(vcl->name != NULL);
TAILQ_INSERT_TAIL(&vcl_head, vcl, list);
if (active_vcl == NULL)
active_vcl = vcl;
fprintf(stderr, "Loaded \"%s\" as \"%s\"\n", fn , name);
return (0);
}
void
cli_func_config_list(struct cli *cli, char **av, void *priv)
{
struct vcls *vcl;
TAILQ_FOREACH(vcl, &vcl_head, list) {
cli_out(cli, "%s%s\n",
vcl == active_vcl ? "* " : " ",
vcl->name);
}
}
void
cli_func_config_load(struct cli *cli, char **av, void *priv)
{
struct vcls *vcl;
TAILQ_FOREACH(vcl, &vcl_head, list) {
if (!strcmp(vcl->name, av[2])) {
cli_out(cli, "Config '%s' already loaded", av[2]);
cli_result(cli, CLIS_PARAM);
return;
}
}
vcl = calloc(sizeof *vcl, 1);
assert(vcl != NULL);
vcl->dlh = dlopen(av[3], RTLD_NOW | RTLD_LOCAL);
if (vcl->dlh == NULL) {
cli_out(cli, "dlopen(%s): %s\n", av[3], dlerror());
cli_result(cli, CLIS_PARAM);
free(vcl);
return;
}
vcl->conf = dlsym(vcl->dlh, "VCL_conf");
if (vcl->conf == NULL) {
cli_out(cli, "No VCL_conf symbol\n");
cli_result(cli, CLIS_PARAM);
dlclose(vcl->dlh);
free(vcl);
return;
}
if (vcl->conf->magic != VCL_CONF_MAGIC) {
cli_out(cli, "Wrong VCL_CONF_MAGIC\n");
cli_result(cli, CLIS_PARAM);
dlclose(vcl->dlh);
free(vcl);
return;
}
vcl->name = strdup(av[2]);
assert(vcl->name != NULL);
TAILQ_INSERT_TAIL(&vcl_head, vcl, list);
if (active_vcl == NULL)
active_vcl = vcl;
cli_out(cli, "Loaded \"%s\" from \"%s\"\n", vcl->name , av[3]);
}
void
cli_func_config_unload(struct cli *cli, char **av, void *priv)
{
cli_result(cli, CLIS_UNIMPL);
}
void
cli_func_config_use(struct cli *cli, char **av, void *priv)
{
struct vcls *vcl;
TAILQ_FOREACH(vcl, &vcl_head, list) {
if (!strcmp(vcl->name, av[2]))
break;
}
if (vcl == NULL) {
cli_out(cli, "No config named '%s' loaded", av[2]);
cli_result(cli, CLIS_PARAM);
return;
}
active_vcl = vcl;
}
......@@ -58,6 +58,96 @@ cli_func_passthrough(struct cli *cli, char **av __unused, void *priv)
mgt_child_request(cli_passthrough_cb, cli, &av[2], av[1]);
}
/*--------------------------------------------------------------------*/
static char *
vcl_default(const char *bflag)
{
char *buf, *vf;
struct sbuf *sb;
buf = NULL;
asprintf(&buf,
"backend default { set backend.ip = %s; }",
bflag);
assert(buf != NULL);
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
assert(sb != NULL);
vf = VCL_Compile(sb, buf, NULL);
sbuf_finish(sb);
if (sbuf_len(sb) > 0) {
fprintf(stderr, "%s", sbuf_data(sb));
free(buf);
sbuf_delete(sb);
return (NULL);
}
sbuf_delete(sb);
free(buf);
return (vf);
}
static void
cli_func_config_inline(struct cli *cli, char **av, void *priv __unused)
{
char *vf;
struct sbuf *sb;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
assert(sb != NULL);
vf = VCL_Compile(sb, av[3], NULL);
sbuf_finish(sb);
if (sbuf_len(sb) > 0) {
cli_out(cli, "%s", sbuf_data(sb));
sbuf_delete(sb);
return;
}
sbuf_delete(sb);
cli_suspend(cli);
mgt_child_request(cli_passthrough_cb, cli, NULL,
"config.load %s %s", av[2], vf);
}
static void
cli_func_config_load(struct cli *cli, char **av, void *priv __unused)
{
char *vf;
struct sbuf *sb;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
assert(sb != NULL);
vf = VCL_CompileFile(sb, av[3]);
sbuf_finish(sb);
if (sbuf_len(sb) > 0) {
cli_out(cli, "%s", sbuf_data(sb));
sbuf_delete(sb);
return;
}
sbuf_delete(sb);
cli_suspend(cli);
mgt_child_request(cli_passthrough_cb, cli, NULL,
"config.load %s %s", av[2], vf);
}
static char *
vcl_file(const char *fflag)
{
char *vf;
struct sbuf *sb;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
assert(sb != NULL);
vf = VCL_CompileFile(sb, fflag);
sbuf_finish(sb);
if (sbuf_len(sb) > 0) {
fprintf(stderr, "%s", sbuf_data(sb));
sbuf_delete(sb);
return (NULL);
}
sbuf_delete(sb);
return (vf);
}
/*--------------------------------------------------------------------*/
static void
......@@ -105,11 +195,11 @@ static struct cli_proto cli_proto[] = {
{ CLI_URL_QUERY, cli_func_passthrough, NULL },
{ CLI_URL_PURGE, cli_func_passthrough, NULL },
{ CLI_URL_STATUS, cli_func_passthrough, NULL },
{ CLI_CONFIG_LOAD },
{ CLI_CONFIG_INLINE },
{ CLI_CONFIG_UNLOAD },
{ CLI_CONFIG_LIST },
{ CLI_CONFIG_USE },
{ CLI_CONFIG_LOAD, cli_func_config_load, NULL },
{ CLI_CONFIG_INLINE, cli_func_config_inline, NULL },
{ CLI_CONFIG_UNLOAD, cli_func_passthrough, NULL },
{ CLI_CONFIG_LIST, cli_func_passthrough, NULL },
{ CLI_CONFIG_USE, cli_func_passthrough, NULL },
{ CLI_SERVER_FREEZE, cli_func_passthrough, NULL },
{ CLI_SERVER_THAW, cli_func_passthrough, NULL },
{ CLI_SERVER_SUSPEND, cli_func_passthrough, NULL },
......@@ -202,64 +292,6 @@ init_vsl(const char *fn, unsigned size)
AZ(ftruncate(heritage.vsl_fd, sizeof slh + size));
heritage.vsl_size = slh.size + slh.start;
}
/*--------------------------------------------------------------------*/
static char *
vcl_default(const char *bflag)
{
char *buf, *vf;
struct sbuf *sb;
buf = NULL;
asprintf(&buf,
"backend default { set backend.ip = %s; }",
bflag);
assert(buf != NULL);
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
assert(sb != NULL);
vf = VCL_Compile(sb, buf, NULL);
sbuf_finish(sb);
if (sbuf_len(sb) > 0) {
fprintf(stderr, "%s", sbuf_data(sb));
free(buf);
sbuf_delete(sb);
return (NULL);
}
sbuf_delete(sb);
free(buf);
return (vf);
}
/*--------------------------------------------------------------------*/
static char *
vcl_file(const char *bflag)
{
char *buf, *vf;
struct sbuf *sb;
return (NULL);
buf = NULL;
asprintf(&buf,
"backend default { set backend.ip = %s; }",
bflag);
assert(buf != NULL);
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
assert(sb != NULL);
vf = VCL_Compile(sb, buf, NULL);
sbuf_finish(sb);
if (sbuf_len(sb) > 0) {
fprintf(stderr, "%s", sbuf_data(sb));
free(buf);
sbuf_delete(sb);
return (NULL);
}
sbuf_delete(sb);
free(buf);
return (vf);
}
/*--------------------------------------------------------------------*/
/* for development purposes */
......
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