r78 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Mon Mar 27 14:27:16 CEST 2006


Author: phk
Date: 2006-03-27 14:27:16 +0200 (Mon, 27 Mar 2006)
New Revision: 78

Added:
   trunk/varnish-cache/bin/varnishd/cache_vcl.c
Modified:
   trunk/varnish-cache/bin/varnishd/Makefile.am
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_main.c
   trunk/varnish-cache/bin/varnishd/heritage.h
   trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
Build default VCL from "-b backend_IP" option and pass it to client
via heritage.



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am	2006-03-27 11:51:31 UTC (rev 77)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am	2006-03-27 12:27:16 UTC (rev 78)
@@ -9,6 +9,7 @@
 	cache_httpd.c \
 	cache_main.c \
 	cache_shmlog.c \
+	cache_vcl.c \
 	cli_event.c \
 	mgt_child.c \
 	tcp.c \

Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2006-03-27 11:51:31 UTC (rev 77)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2006-03-27 12:27:16 UTC (rev 78)
@@ -49,4 +49,5 @@
 void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...);
 #endif
 
-
+/* cache_vcl.c */
+int CVCL_Load(const char *fn, const char *name);

Modified: trunk/varnish-cache/bin/varnishd/cache_main.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_main.c	2006-03-27 11:51:31 UTC (rev 77)
+++ trunk/varnish-cache/bin/varnishd/cache_main.c	2006-03-27 12:27:16 UTC (rev 78)
@@ -22,6 +22,7 @@
 #include "cli_event.h"
 
 static struct event ev_keepalive;
+static pthread_t vca_thread;
 
 /*--------------------------------------------------------------------*/
 
@@ -81,7 +82,7 @@
 	{ NULL }
 };
 
-static pthread_t vca_thread;
+/*--------------------------------------------------------------------*/
 
 void
 child_main(void)
@@ -101,6 +102,7 @@
 	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);

Added: trunk/varnish-cache/bin/varnishd/cache_vcl.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_vcl.c	2006-03-27 11:51:31 UTC (rev 77)
+++ trunk/varnish-cache/bin/varnishd/cache_vcl.c	2006-03-27 12:27:16 UTC (rev 78)
@@ -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);
+}

Modified: trunk/varnish-cache/bin/varnishd/heritage.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/heritage.h	2006-03-27 11:51:31 UTC (rev 77)
+++ trunk/varnish-cache/bin/varnishd/heritage.h	2006-03-27 12:27:16 UTC (rev 78)
@@ -24,6 +24,9 @@
 	/* Share memory log fd and size (incl header) */
 	int		vsl_fd;
 	unsigned	vsl_size;
+
+	/* Initial VCL file */
+	char		*vcl_file;
 };
 
 extern struct heritage heritage;

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c	2006-03-27 11:51:31 UTC (rev 77)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c	2006-03-27 12:27:16 UTC (rev 78)
@@ -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 @@
 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 at 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 @@
 
 /*--------------------------------------------------------------------*/
 
+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 @@
 	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,9 +305,27 @@
 	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
 	 * missing an unlisten(2) facility) we may want to push this into




More information about the varnish-commit mailing list