From b6df6b58a4e7e6c211bdec8086bea053bacc05c0 Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 27 Mar 2006 12:27:16 +0000 Subject: [PATCH] Build default VCL from "-b backend_IP" option and pass it to client via heritage. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@78 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/Makefile.am | 1 + varnish-cache/bin/varnishd/cache.h | 3 +- varnish-cache/bin/varnishd/cache_main.c | 4 +- varnish-cache/bin/varnishd/cache_vcl.c | 33 +++++++ varnish-cache/bin/varnishd/heritage.h | 3 + varnish-cache/bin/varnishd/varnishd.c | 117 ++++++++++++++++++------ 6 files changed, 131 insertions(+), 30 deletions(-) create mode 100644 varnish-cache/bin/varnishd/cache_vcl.c diff --git a/varnish-cache/bin/varnishd/Makefile.am b/varnish-cache/bin/varnishd/Makefile.am index 34828a61..72654aed 100644 --- a/varnish-cache/bin/varnishd/Makefile.am +++ b/varnish-cache/bin/varnishd/Makefile.am @@ -9,6 +9,7 @@ varnishd_SOURCES = \ cache_httpd.c \ cache_main.c \ cache_shmlog.c \ + cache_vcl.c \ cli_event.c \ mgt_child.c \ tcp.c \ diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index d95b0adc..04acdaf9 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -49,4 +49,5 @@ void VSLR(enum shmlogtag tag, unsigned id, const char *b, const char *e); void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...); #endif - +/* cache_vcl.c */ +int CVCL_Load(const char *fn, const char *name); diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 59da0e1f..940cd6a9 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -22,6 +22,7 @@ #include "cli_event.h" static struct event ev_keepalive; +static pthread_t vca_thread; /*--------------------------------------------------------------------*/ @@ -81,7 +82,7 @@ static struct cli_proto cli_proto[] = { { NULL } }; -static pthread_t vca_thread; +/*--------------------------------------------------------------------*/ void child_main(void) @@ -101,6 +102,7 @@ child_main(void) eb = event_init(); assert(eb != NULL); + CVCL_Load(heritage.vcl_file, "boot"); cli = cli_setup(heritage.fds[2], heritage.fds[1], 0, cli_proto); evtimer_set(&ev_keepalive, timer_keepalive, NULL); diff --git a/varnish-cache/bin/varnishd/cache_vcl.c b/varnish-cache/bin/varnishd/cache_vcl.c new file mode 100644 index 00000000..227c27e8 --- /dev/null +++ b/varnish-cache/bin/varnishd/cache_vcl.c @@ -0,0 +1,33 @@ +/* + * $Id$ + */ + +#include +#include + +#include "vcl_lang.h" +#include "cache.h" + +int +CVCL_Load(const char *fn, const char *name) +{ + void *dlh; + struct VCL_conf *vc; + + dlh = dlopen(fn, RTLD_NOW | RTLD_LOCAL); + if (dlh == NULL) { + fprintf(stderr, "dlopen(%s): %s\n", fn, dlerror()); + return (1); + } + vc = dlsym(dlh, "VCL_conf"); + if (vc == NULL) { + fprintf(stderr, "No VCL_conf symbol\n"); + return (1); + } + if (vc->magic != VCL_CONF_MAGIC) { + fprintf(stderr, "Wrong VCL_CONF_MAGIC\n"); + return (1); + } + fprintf(stderr, "Loaded \"%s\" as \"%s\"\n", fn , name); + return (0); +} diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index fa6a9be2..018cb054 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -24,6 +24,9 @@ struct heritage { /* Share memory log fd and size (incl header) */ int vsl_fd; unsigned vsl_size; + + /* Initial VCL file */ + char *vcl_file; }; extern struct heritage heritage; diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index 7e7a0734..89e6632d 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -25,6 +25,8 @@ #include #include +#include "vcl_lang.h" + #include "mgt.h" #include "heritage.h" #include "cli_event.h" @@ -153,15 +155,15 @@ static void usage(void) { fprintf(stderr, "usage: varnishd [options]\n"); + fprintf(stderr, " %-20s # %s\n", "-b", "backend_IP_number"); fprintf(stderr, " %-20s # %s\n", "-d", "debug"); + fprintf(stderr, " %-20s # %s\n", "-f", "VCL_file"); fprintf(stderr, " %-20s # %s\n", "-p number", "TCP listen port"); #if 0 -c clusterid@cluster_controller - -f config_file -m memory_limit -s kind[,storage-options] -l logfile,logsize - -b backend ip... -u uid -a CLI_port #endif @@ -203,12 +205,67 @@ init_vsl(const char *fn, unsigned size) /*--------------------------------------------------------------------*/ +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 */ #include #include -#include - void VCL_count(unsigned u) { @@ -220,38 +277,24 @@ main(int argc, char *argv[]) int o; const char *portnumber = "8080"; unsigned dflag = 1; /* XXX: debug=on for now */ + const char *bflag = NULL; + const char *fflag = NULL; register_printf_render_std((const unsigned char *)"HVQ"); - { - struct sbuf *sb; - VCL_InitCompile(); - sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); - assert(sb != NULL); - VCL_Compile(sb, - "backend default { set backend.ip = 10.0.0.1; } ", - NULL); - sbuf_finish(sb); - fprintf(stderr, "Result: %s\n", sbuf_data(sb)); - - { - void *dlhandle; - - dlhandle = dlopen("/tmp/_.so.1", - RTLD_NOW | RTLD_LOCAL ); - if (dlhandle == NULL) - err(1, "dlopen %s", dlerror()); - - } - exit (0); - } - while ((o = getopt(argc, argv, "dp:")) != -1) + while ((o = getopt(argc, argv, "b:df:p:")) != -1) switch (o) { + case 'b': + bflag = optarg; + break; case 'd': dflag++; break; + case 'f': + fflag = optarg; + break; case 'p': portnumber = optarg; break; @@ -262,8 +305,26 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - if (argc != 0) + if (argc != 0) { + fprintf(stderr, "Too many arguments\n"); + usage(); + } + + if (bflag != NULL && fflag != NULL) { + fprintf(stderr, "Only one of -b or -f can be specified\n"); usage(); + } + if (bflag == NULL && fflag == NULL) { + fprintf(stderr, "One of -b or -f must be specified\n"); + usage(); + } + + if (bflag != NULL) + heritage.vcl_file = vcl_default(bflag); + else + heritage.vcl_file = vcl_file(fflag); + if (heritage.vcl_file == NULL) + exit (1); /* * XXX: Lacking the suspend/resume facility (due to the socket API -- 2.39.5