[master] 4ce9f90 Turn the optional vmod init function into an event function instead, and call it with LOAD and DISCARD events.

Poul-Henning Kamp phk at FreeBSD.org
Tue Jun 16 12:57:05 CEST 2015


commit 4ce9f900a1ce725735a48002e6dda437fe9a4fde
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Jun 16 10:56:34 2015 +0000

    Turn the optional vmod init function into an event function instead,
    and call it with LOAD and DISCARD events.

diff --git a/include/vrt.h b/include/vrt.h
index 36b0165..cbed87b 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -265,7 +265,9 @@ struct vmod_priv {
 	vmod_priv_free_f	*free;
 };
 
-typedef int vmod_init_f(VRT_CTX, struct vmod_priv *);
+#ifdef VCL_RET_MAX
+typedef int vmod_event_f(VRT_CTX, struct vmod_priv *, enum vcl_event_e);
+#endif
 
 void VRT_priv_fini(const struct vmod_priv *p);
 struct vmod_priv *VRT_priv_task(VRT_CTX, void *vmod_id);
diff --git a/lib/libvcc/vcc_vmod.c b/lib/libvcc/vcc_vmod.c
index cbae127..ffb70b7 100644
--- a/lib/libvcc/vcc_vmod.c
+++ b/lib/libvcc/vcc_vmod.c
@@ -189,19 +189,22 @@ vcc_ParseImport(struct vcc *tl)
 	spec = vmd->spec;
 	for (; *spec != NULL; spec++) {
 		p = *spec;
-		if (!strcmp(p, "OBJ")) {
+		if (!strcmp(p, "$OBJ")) {
 			p += strlen(p) + 1;
 			sym = VCC_AddSymbolStr(tl, p, SYM_OBJECT);
 			XXXAN(sym);
 			sym->args = p;
-		} else if (!strcmp(p, "INIT")) {
+		} else if (!strcmp(p, "$EVENT")) {
 			p += strlen(p) + 1;
 			if (ifp == NULL)
 				ifp = New_IniFin(tl);
 			VSB_printf(ifp->ini,
-			    "\tif (%s(ctx, &vmod_priv_%.*s))\n"
+			    "\tif (%s(ctx, &vmod_priv_%.*s, VCL_EVENT_LOAD))\n"
 			    "\t\treturn(1);",
 			    p, PF(mod));
+			VSB_printf(ifp->fin,
+			    "\t\t(void)%s(ctx, &vmod_priv_%.*s,\n"
+			    "\t\t    VCL_EVENT_DISCARD);\n", p, PF(mod));
 		} else {
 			sym = VCC_AddSymbolStr(tl, p, SYM_FUNC);
 			ERRCHK(tl);
diff --git a/lib/libvcc/vmodtool.py b/lib/libvcc/vmodtool.py
index c2c16c5..a5eb8bb 100755
--- a/lib/libvcc/vmodtool.py
+++ b/lib/libvcc/vmodtool.py
@@ -153,18 +153,19 @@ class Vmod(object):
 		self.nam = nam
 		self.dnam = dnam
 		self.sec = sec
-		self.init = None
+		self.event = None
 		self.funcs = list()
 		self.objs = list()
 		self.doc_str = []
 		self.doc_order = []
 
-	def set_init(self, nam):
-		if self.init != None:
-			raise ParseError("Module %s already has Init", self.nam)
+	def set_event(self, nam):
+		if self.event != None:
+			raise ParseError("Module %s already has $Event",
+			    self.nam)
 		if not is_c_name(nam):
-			raise ParseError("Init name '%s' is illegal", nam)
-		self.init = nam
+			raise ParseError("$Event name '%s' is illegal", nam)
+		self.event = nam
 
 	def add_func(self, fn):
 		self.funcs.append(fn)
@@ -185,11 +186,11 @@ class Vmod(object):
 		for f in self.funcs:
 			for i in lwrap(f.c_proto()):
 				fo.write(i + "\n")
-		if self.init != None:
+		if self.event != None:
 			fo.write("\n")
-			fo.write("int " + self.init)
-			fo.write(
-			    "(VRT_CTX, struct vmod_priv *);\n")
+			fo.write("#ifdef VCL_MET_MAX\n")
+			fo.write("vmod_event_f " + self.event + ";\n")
+			fo.write("#endif\n")
 
 	def c_typedefs_(self):
 		l = list()
@@ -265,8 +266,8 @@ class Vmod(object):
 			s += f.c_initializer()
 
 		s += "\n\t/* Init/Fini */\n"
-		if self.init != None:
-			s += "\t" + self.init + ",\n"
+		if self.event != None:
+			s += "\t" + self.event + ",\n"
 		s += "};"
 
 		return s
@@ -281,8 +282,8 @@ class Vmod(object):
 			s += f.c_struct(self.nam)
 
 		s += "\n\t/* Init/Fini */\n"
-		if self.init != None:
-			s += "\tvmod_init_f\t*_init;\n"
+		if self.event != None:
+			s += "\tvmod_event_f\t*_event;\n"
 		s += '}'
 		return s
 
@@ -297,9 +298,9 @@ class Vmod(object):
 		for f in self.funcs:
 			s += f.c_strspec(self.nam) + ',\n\n'
 
-		if self.init != None:
+		if self.event != None:
 			s += "\t/* Init/Fini */\n"
-			s += '\t"INIT\\0Vmod_' + self.nam + '_Func._init",\n'
+			s += '\t"$EVENT\\0Vmod_' + self.nam + '_Func._event",\n'
 
 		s += "\t0\n"
 		s += "};\n"
@@ -525,7 +526,7 @@ class Obj(object):
 
 	def c_strspec(self, modnam):
 		s = "\t/* Object " + self.nam + " */\n"
-		s += '\t"OBJ\\0"\n'
+		s += '\t"$OBJ\\0"\n'
 		s += self.init.c_strspec(modnam, pfx="\t\t") + '\n'
 		s += '\t\t"' + self.st + '\\0"\n'
 		s += self.fini.c_strspec(modnam, pfx="\t\t") + '\n'
@@ -749,9 +750,9 @@ class FileSection(object):
 		if t.str == "$Module":
 			o = parse_module(self)
 			vx.append(o)
-		elif t.str == "$Init":
+		elif t.str == "$Event":
 			x = self.get_token()
-			vx[0].set_init(x.str)
+			vx[0].set_event(x.str)
 			o = None
 		elif t.str == "$Function":
 			if len(vx) == 2:
@@ -854,7 +855,7 @@ def runmain(inputvcc, outputname="vcc_if"):
 		"$Function":	True,
 		"$Object":	True,
 		"$Method":	True,
-		"$Init":	True,
+		"$Event":	True,
 	}
 
 	sl = []
@@ -903,6 +904,7 @@ def runmain(inputvcc, outputname="vcc_if"):
 	vx[0].c_proto(fh)
 
 	fc.write('#include "config.h"\n')
+	fc.write('#include "vcl.h"\n')
 	fc.write('#include "vrt.h"\n')
 	fc.write('#include "vcc_if.h"\n')
 	fc.write('#include "vmod_abi.h"\n')
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 2e08154..67edd62 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -34,7 +34,7 @@ This vmod is used to develop, test and debug the various aspects
 of VMOD handling in Varnish.
 
 
-$Init init_function
+$Event event_function
 $Function VOID panic(STRING_LIST)
 
 Don't.
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index a91e845..f9f3361 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -248,12 +248,14 @@ priv_vcl_free(void *priv)
 }
 
 int __match_proto__(vmod_init_f)
-init_function(VRT_CTX, struct vmod_priv *priv)
+event_function(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
 {
 	struct priv_vcl *priv_vcl;
 
-	AN(ctx->msg);
+	if (e != VCL_EVENT_LOAD)
+		return (0);
 
+	AN(ctx->msg);
 	if (cache_param->nuke_limit == 42) {
 		VSB_printf(ctx->msg, "nuke_limit is not the answer.");
 		return (-1);



More information about the varnish-commit mailing list