r5184 - in trunk/varnish-cache: bin/varnishd bin/varnishtest/tests include

phk at varnish-cache.org phk at varnish-cache.org
Wed Sep 8 11:52:41 CEST 2010


Author: phk
Date: 2010-09-08 11:52:40 +0200 (Wed, 08 Sep 2010)
New Revision: 5184

Added:
   trunk/varnish-cache/bin/varnishtest/tests/m00001.vtc
Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_main.c
   trunk/varnish-cache/bin/varnishd/cache_vrt_vmod.c
   trunk/varnish-cache/include/vsc_fields.h
Log:
Add a list of refcounted VMOD's we have loaded.

Add stats counter to keep track of how many vmods are on the list
mostly so the testcase can see that it works.



Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2010-09-08 09:41:00 UTC (rev 5183)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2010-09-08 09:52:40 UTC (rev 5184)
@@ -699,6 +699,9 @@
 void ESI_Destroy(struct object *);
 void ESI_Parse(struct sess *);
 
+/* cache_vrt_vmod.c */
+void VMOD_Init(void);
+
 /* cache_ws.c */
 
 void WS_Init(struct ws *ws, const char *id, void *space, unsigned len);

Modified: trunk/varnish-cache/bin/varnishd/cache_main.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_main.c	2010-09-08 09:41:00 UTC (rev 5183)
+++ trunk/varnish-cache/bin/varnishd/cache_main.c	2010-09-08 09:52:40 UTC (rev 5184)
@@ -129,6 +129,8 @@
 	SMS_Init();
 	STV_open();
 
+	VMOD_Init();
+
 	BAN_Compile();
 
 	/* Wait for persistent storage to load if asked to */

Modified: trunk/varnish-cache/bin/varnishd/cache_vrt_vmod.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_vrt_vmod.c	2010-09-08 09:41:00 UTC (rev 5183)
+++ trunk/varnish-cache/bin/varnishd/cache_vrt_vmod.c	2010-09-08 09:52:40 UTC (rev 5184)
@@ -38,6 +38,7 @@
 #include <stdlib.h>
 #include <dlfcn.h>
 
+#include "cli_priv.h"
 #include "vrt.h"
 #include "cache.h"
 
@@ -46,6 +47,13 @@
  */
 
 struct vmod {
+	unsigned		magic;
+#define VMOD_MAGIC		0xb750219c
+
+	VTAILQ_ENTRY(vmod)	list;
+
+	int			ref;
+
 	char 			*nm;
 	char 			*path;
 	void 			*hdl;
@@ -53,6 +61,8 @@
 	int			funclen;
 };
 
+static VTAILQ_HEAD(,vmod)	vmods = VTAILQ_HEAD_INITIALIZER(vmods);
+
 void
 VRT_Vmod_Init(void **hdl, void *ptr, int len, const char *nm, const char *path)
 {
@@ -61,29 +71,42 @@
 	const int *i;
 	const char *p;
 
-	v = calloc(sizeof *v, 1);
-	AN(v);
-	REPLACE(v->nm, nm);
-	REPLACE(v->path, path);
-	v->hdl = dlopen(v->path, RTLD_NOW | RTLD_LOCAL);
-	AN(v->hdl);
+	ASSERT_CLI();
 
-	x = dlsym(v->hdl, "Vmod_Name");
-	AN(x);
-	p = x;
+	VTAILQ_FOREACH(v, &vmods, list)
+		if (!strcmp(v->nm, nm))
+			break;
+	if (v == NULL) {
+		ALLOC_OBJ(v, VMOD_MAGIC);
+		AN(v);
 
-	x = dlsym(v->hdl, "Vmod_Len");
-	AN(x);
-	i = x;
-	assert(len == *i);
+		VTAILQ_INSERT_TAIL(&vmods, v, list);
+		VSC_main->vmods++;
 
-	x = dlsym(v->hdl, "Vmod_Func");
-	AN(x);
-	memcpy(ptr, x, len);
+		REPLACE(v->nm, nm);
+		REPLACE(v->path, path);
 
-	v->funcs = x;
-	v->funclen = *i;
+		v->hdl = dlopen(v->path, RTLD_NOW | RTLD_LOCAL);
+		AN(v->hdl);
 
+		x = dlsym(v->hdl, "Vmod_Name");
+		AN(x);
+		p = x;
+
+		x = dlsym(v->hdl, "Vmod_Len");
+		AN(x);
+		i = x;
+		assert(len == *i);
+
+		x = dlsym(v->hdl, "Vmod_Func");
+		AN(x);
+		memcpy(ptr, x, len);
+
+		v->funcs = x;
+		v->funclen = *i;
+	}
+	v->ref++;
+
 	*hdl = v;
 }
 
@@ -92,8 +115,44 @@
 {
 	struct vmod *v;
 
+	ASSERT_CLI();
+
 	AN(*hdl);
-	v = *hdl;
-	free(*hdl);
+	CAST_OBJ_NOTNULL(v, *hdl, VMOD_MAGIC);
 	*hdl = NULL;
+	if (--v->ref != 0)
+		return;
+	dlclose(v->hdl);
+	free(v->nm);
+	free(v->path);
+	VTAILQ_REMOVE(&vmods, v, list);
+	VSC_main->vmods--;
+	FREE_OBJ(v);
 }
+
+/*---------------------------------------------------------------------*/
+
+static void
+ccf_debug_vmod(struct cli *cli, const char * const *av, void *priv)
+{
+	struct vmod *v;
+
+	(void)av;
+	(void)priv;
+	ASSERT_CLI();
+	VTAILQ_FOREACH(v, &vmods, list)
+		cli_out(cli, "%5d %s (%s)\n", v->ref, v->nm, v->path);
+}
+
+static struct cli_proto vcl_cmds[] = {
+	{ "debug.vmod", "debug.vmod", "show loaded vmods", 0, 0,
+		"d", ccf_debug_vmod },
+	{ NULL }
+};
+
+void
+VMOD_Init(void)
+{
+
+	CLI_AddFuncs(vcl_cmds);
+}

Added: trunk/varnish-cache/bin/varnishtest/tests/m00001.vtc
===================================================================
--- trunk/varnish-cache/bin/varnishtest/tests/m00001.vtc	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishtest/tests/m00001.vtc	2010-09-08 09:52:40 UTC (rev 5184)
@@ -0,0 +1,50 @@
+# $Id$
+
+test "Test std vmod"
+
+server s1 {
+	rxreq 
+	txresp -hdr "foo: bAr" -hdr "bar: fOo" -bodylen 4
+} -start
+
+varnish v1 -vcl+backend {
+	import std from "${topsrc}/lib/libvmod_std/.libs/libvmod_std.so.1" ;
+
+	sub vcl_deliver {
+		set resp.http.foo = std.toupper(resp.http.foo);
+		set resp.http.bar = std.tolower(resp.http.bar);
+	}
+} -start
+
+client c1 {
+	txreq -url "/bar"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.content-length == "4"
+	expect resp.http.foo == "BAR"
+	expect resp.http.bar == "foo"
+} -run
+
+varnish v1 -vcl+backend {
+}
+
+client c1 {
+	txreq -url "/bar"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.content-length == "4"
+	expect resp.http.foo == "bAr"
+	expect resp.http.bar == "fOo"
+} -run
+
+client c1 -run
+
+varnish v1 -expect vmods == 1
+
+varnish v1 -cliok "debug.vmod"
+varnish v1 -cliok "vcl.list"
+varnish v1 -cliok "vcl.discard vcl1"
+varnish v1 -cliok "vcl.list"
+varnish v1 -cliok "debug.vmod"
+
+varnish v1 -expect vmods == 0

Modified: trunk/varnish-cache/include/vsc_fields.h
===================================================================
--- trunk/varnish-cache/include/vsc_fields.h	2010-09-08 09:41:00 UTC (rev 5183)
+++ trunk/varnish-cache/include/vsc_fields.h	2010-09-08 09:52:40 UTC (rev 5184)
@@ -163,6 +163,8 @@
 
 VSC_F_MAIN(critbit_cooler,	uint64_t, 0, 'i', "Objhdr's on cool list")
 
+VSC_F_MAIN(vmods,		uint64_t, 0, 'i', "Loaded VMODs")
+
 #ifdef __VSC_F_MAIN
 #undef VSC_F_MAIN
 #undef __VSC_F_MAIN




More information about the varnish-commit mailing list