]> err.no Git - varnish/commitdiff
Build default VCL from "-b backend_IP" option and pass it to client
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 27 Mar 2006 12:27:16 +0000 (12:27 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 27 Mar 2006 12:27:16 +0000 (12:27 +0000)
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
varnish-cache/bin/varnishd/cache.h
varnish-cache/bin/varnishd/cache_main.c
varnish-cache/bin/varnishd/cache_vcl.c [new file with mode: 0644]
varnish-cache/bin/varnishd/heritage.h
varnish-cache/bin/varnishd/varnishd.c

index 34828a61c015de83b81a7a50033acf0c7987b741..72654aed843bf63d052dccde93e4d31be01fa65d 100644 (file)
@@ -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 \
index d95b0adce98e919600ef55bbe518532b15c14de2..04acdaf986aae9c878ddff2b2d90f99243913b2a 100644 (file)
@@ -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);
index 59da0e1fda676dd861e194f075bcfdda34d046bf..940cd6a9e91062b3fe50ba6522e7220ffab7df74 100644 (file)
@@ -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 (file)
index 0000000..227c27e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <dlfcn.h>
+
+#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);
+}
index fa6a9be2bdf798c3cca83db4916b6d4082ade45b..018cb0541a1e7702e9b2113d3e66910e776a5933 100644 (file)
@@ -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;
index 7e7a073474e0212e0de0cff49e6cd4d09771a7ff..89e6632ded0d6f59c9e8f254b1f1a35a805b3149 100644 (file)
@@ -25,6 +25,8 @@
 #include <libvarnish.h>
 #include <libvcl.h>
 
+#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 <printf.h>
 #include <err.h>
 
-#include <dlfcn.h>
-
 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