{"id":1046,"date":"2012-05-26T03:49:17","date_gmt":"2012-05-25T18:49:17","guid":{"rendered":"http:\/\/blog.xcir.net\/?p=1046"},"modified":"2012-08-02T00:54:14","modified_gmt":"2012-08-01T15:54:14","slug":"how-to-hook-vcl-function-at-vmod","status":"publish","type":"post","link":"https:\/\/blog.xcir.net\/?p=1046","title":{"rendered":"How to hook VCL function at VMOD"},"content":{"rendered":"<p>VMOD processing want to before\/after VCL function processing.<br \/>\nBut, I do not want to write extra line.<br \/>\nHow to do it?<\/p>\n<p><a href=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook2.png\" alt=\"\" title=\"hook2\" width=\"650\" height=\"385\" class=\"aligncenter size-full wp-image-1024\" srcset=\"https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook2.png 650w, https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook2-300x177.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/a><br \/>\nCan be realized by the replace the pointer to sp->vcl-><strong>XXXX<\/strong>_func (VCL_<strong>XXXX<\/strong>)<\/p>\n<p><a href=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook3.png\" alt=\"\" title=\"hook3\" width=\"471\" height=\"293\" class=\"aligncenter size-full wp-image-1075\" srcset=\"https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook3.png 471w, https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook3-300x186.png 300w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/a><br \/>\nCan be added to the processing by replacing pointer<\/p>\n<pre class=\"brush: c; \">\n\r\n\r\nstatic vcl_func_f         *vmod_redirect_Hook_vcl_error = NULL;\r\nstatic pthread_mutex_t    vmod_redirect_mutex = PTHREAD_MUTEX_INITIALIZER;\r\nstatic unsigned           hook_done = 0;\r\n\r\nstatic int vmod_Hook_vcl_error(struct sess *sp){\r\n\r\n    ....    \/\/write the processing\r\n\r\n    \/\/call original vcl_error\r\n    return(vmod_redirect_Hook_vcl_error(sp));\r\n\r\n}\r\n\r\nint\r\nvmod_location(struct sess *sp, int status, const char*p,...)\r\n{\r\n        \/\/vcl reload detection.\r\n        if(hook_done == 1\r\n           &amp;&amp; sp-&gt;vcl-&gt;error_func != vmod_redirect_Hook_vcl_error ) hook_done = 0;\r\n\r\n        if(hook_done == 0){\r\n            \/\/lock to prevent another thread write\r\n            AZ(pthread_mutex_lock(&amp;vmod_redirect_mutex));\r\n\r\n            if(hook_done == 0)\r\n            {\r\n                \/\/store the original vcl_error pointer\r\n                vmod_redirect_Hook_vcl_error = sp-&gt;vcl-&gt;error_func;\r\n\r\n                \/\/hook\r\n                sp-&gt;vcl-&gt;error_func = vmod_Hook_vcl_error;\r\n\r\n                hook_done = 1;\r\n            }\r\n            \/\/unlock\r\n            AZ(pthread_mutex_unlock(&amp;vmod_redirect_mutex));\r\n        }\r\n\r\n    ....\r\n\r\n    return (status);\r\n}\r\n\n<\/pre>\n<p>(via:<a href=\"http:\/\/blog.xcir.net\/index.php\/2012\/02\/varnish-redirect-operation-to-easylibvmod-rewrite\/\" title=\"Varnish redirect operation to easy(libvmod-redirect)\">vmod_redirect<\/a>)<\/p>\n<p>Hook to vcl_error at vmod processing in first time.<br \/>\nbecause there is no way to write-access the pointer at vmod_Init.<\/p>\n<p><a href=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook4.png\" alt=\"\" title=\"hook4\" width=\"963\" height=\"242\" class=\"aligncenter size-full wp-image-1041\" srcset=\"https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook4.png 963w, https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook4-300x75.png 300w\" sizes=\"auto, (max-width: 963px) 100vw, 963px\" \/><\/a><br \/>\n<a href=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook5.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook5.png\" alt=\"\" title=\"hook5\" width=\"814\" height=\"382\" class=\"aligncenter size-full wp-image-1079\" srcset=\"https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook5.png 814w, https:\/\/blog.xcir.net\/wp-content\/uploads\/2012\/05\/hook5-300x140.png 300w\" sizes=\"auto, (max-width: 814px) 100vw, 814px\" \/><\/a><\/p>\n<p>also, has locked using the mutex, because to prevent the loop by thread concurrent access.<\/p>\n<p>I hope that this code is of help to you.<\/p>\n<p><strong>ATTENTION<\/strong><br \/>\ndon&#8217;t use varnishadm&#8217;s command &#8220;vcl.use&#8221; and &#8220;vcl.discard&#8221; . because to the segfault or call to other vcl function. <\/p>\n<p>&#8212;<br \/>\nmodify(2012-08-01)<br \/>\nwhen you vcl reloaded, hook method be off.<\/p>\n<div class='wp_social_bookmarking_light'>        <div class=\"wsbl_hatena\"><a href='\/\/b.hatena.ne.jp\/add?mode=confirm&url=https%3A%2F%2Fblog.xcir.net%2F%3Fp%3D1046&title=How%20to%20hook%20VCL%20function%20at%20VMOD' title='\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/blog.xcir.net\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/hatena.gif' alt='\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0' title='\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0' width='16' height='12' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_hatena_users\"><a href='\/\/b.hatena.ne.jp\/entry\/https:\/\/blog.xcir.net\/?p=1046' title='\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af - How to hook VCL function at VMOD' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='\/\/b.hatena.ne.jp\/entry\/image\/https:\/\/blog.xcir.net\/?p=1046' alt='\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af - How to hook VCL function at VMOD' title='\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af - How to hook VCL function at VMOD'   class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_facebook\"><a href='http:\/\/www.facebook.com\/share.php?u=https%3A%2F%2Fblog.xcir.net%2F%3Fp%3D1046&t=How%20to%20hook%20VCL%20function%20at%20VMOD' title='Facebook \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/blog.xcir.net\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/facebook.png' alt='Facebook \u306b\u30b7\u30a7\u30a2' title='Facebook \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_linkedin\"><a href='\/\/www.linkedin.com\/shareArticle?mini=true&url=https%3A%2F%2Fblog.xcir.net%2F%3Fp%3D1046&title=How%20to%20hook%20VCL%20function%20at%20VMOD' title='LinkedIn \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/blog.xcir.net\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/linkedin.png' alt='LinkedIn \u306b\u30b7\u30a7\u30a2' title='LinkedIn \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/blog.xcir.net\/?p=1046\" data-text=\"How to hook VCL function at VMOD\" data-lang=\"ja\">Tweet<\/a><\/div><\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>VMOD processing want to before\/after VCL function processing. But, I do not want to write extra line. How to d <a href='https:\/\/blog.xcir.net\/?p=1046' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,3],"tags":[33,34],"class_list":["post-1046","post","type-post","status-publish","format-standard","hentry","category-english","category-3","tag-varnish3-0-2","tag-vmod","category-24-id","category-3-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts\/1046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1046"}],"version-history":[{"count":32,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts\/1046\/revisions"}],"predecessor-version":[{"id":1234,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=\/wp\/v2\/posts\/1046\/revisions\/1234"}],"wp:attachment":[{"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xcir.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}