summaryrefslogtreecommitdiffabout
path: root/src
authorSergey Poznyakoff <gray@gnu.org.ua>2014-11-01 14:28:19 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2014-11-01 14:34:10 (GMT)
commitfffbbfb089847644e2b0281faba2fa892df4f8b9 (patch) (unidiff)
tree83899ca03d92bdb4100c413904c83385ca0c7d6c /src
parentc16de06862f9b2106dcb7810fc20856689ef2aa6 (diff)
downloadvmod-binlog-fffbbfb089847644e2b0281faba2fa892df4f8b9.tar.gz
vmod-binlog-fffbbfb089847644e2b0281faba2fa892df4f8b9.tar.bz2
Support for VCL 4.0
* configure.ac: Version 1.0.91 Detect varnish version and set variables and conditionals accordingly. * NEWS: Update. * src/.gitignore: Update. * src/Makefile.am: Always build vcc_if.c and vcc_if.h. Don't distribute them. * src/binlog.c: Use VCL data type. Define them for VCL 3.0. (vmod_init): Fix premature memory deallocation. * src/vmod.vcc: Rewrite in VCL 4.0 syntax. * tests/atlocal.in (ret_vcl_recv): New variable. * tests/test01.at (vcl_recv): Return $ret_vcl_recv. * tests/test02.at: Likewise.
Diffstat (limited to 'src') (more/less context) (show whitespace changes)
-rw-r--r--src/.gitignore2
-rw-r--r--src/Makefile.am37
-rw-r--r--src/binlog.c64
-rw-r--r--src/vmod-binlog.h3
-rw-r--r--src/vmod.vcc111
5 files changed, 172 insertions, 45 deletions
diff --git a/src/.gitignore b/src/.gitignore
index 8f5a6ab..bae35ac 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -3,3 +3,5 @@ binlogsel
3parse-datetime.c 3parse-datetime.c
4vcc_if.c 4vcc_if.c
5vcc_if.h 5vcc_if.h
6*.rst
7*.vcc3
diff --git a/src/Makefile.am b/src/Makefile.am
index a20f9cb..7917730 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,5 @@
1# This file is part of vmod-binlog 1# This file is part of vmod-binlog
2# Copyright (C) 2013 Sergey Poznyakoff 2# Copyright (C) 2013-2014 Sergey Poznyakoff
3# 3#
4# Vmod-binlog is free software; you can redistribute it and/or modify 4# Vmod-binlog is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by 5# it under the terms of the GNU General Public License as published by
@@ -14,7 +14,10 @@
14# You should have received a copy of the GNU General Public License 14# You should have received a copy of the GNU General Public License
15# along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. 15# along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>.
16 16
17AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) 17AM_CPPFLAGS =\
18 -I$(VARNISHSRC)/include\
19 -I$(VARNISHSRC)/bin/varnishd\
20 -I$(VARNISHSRC)
18 21
19noinst_LIBRARIES = libbinlog.a 22noinst_LIBRARIES = libbinlog.a
20libbinlog_a_SOURCES = pack.c err.c parse-datetime.y xalloc.c 23libbinlog_a_SOURCES = pack.c err.c parse-datetime.y xalloc.c
@@ -38,17 +41,33 @@ libvmod_binlog_la_LIBADD=
38libvmod_binlog_la_SOURCES = \ 41libvmod_binlog_la_SOURCES = \
39 binlog.c\ 42 binlog.c\
40 pack.c\ 43 pack.c\
41 vmod-binlog.h\ 44 vmod-binlog.h
42 vcc_if.c vcc_if.h 45nodist_libvmod_binlog_la_SOURCES = vcc_if.c vcc_if.h
43
44noinst_HEADERS = pack.h err.h parse-datetime.h xalloc.h 46noinst_HEADERS = pack.h err.h parse-datetime.h xalloc.h
45 47
46BUILT_SOURCES = vcc_if.c vcc_if.h 48binlog.lo: vcc_if.c vcc_if.h
49
50CLEANFILES = vcc_if.c vcc_if.h *.rst
51
52if VARNISH4
53 vmodtool = $(VARNISHSRC)/lib/libvcc/vmodtool.py
54 vmodtoolargs = --strict
55 vccfile = $(top_srcdir)/src/vmod.vcc
56else
57 vmodtool = $(VARNISHSRC)/lib/libvmod_std/vmod.py
58 vmodtoolargs =
59 vccfile = vmod.vcc3
60
61vmod.vcc3: $(top_srcdir)/src/vmod.vcc
62 CLEANFILES += vmod.vcc3
63endif
64
65.vcc.vcc3:
66 sed -n '/^\$$/{s///;s/\(Module *[^ ][^ ]*\).*/\1/;p}' $< > $@
47 67
48vcc_if.c vcc_if.h: $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc 68vcc_if.c vcc_if.h: $(vmodtool) $(vccfile)
49 @PYTHON@ $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc 69 @PYTHON@ $(vmodtool) $(vmodtoolargs) $(vccfile)
50 70
51EXTRA_DIST = vmod.vcc 71EXTRA_DIST = vmod.vcc
52 72
53CLEANFILES = $(builddir)/vcc_if.c $(builddir)/vcc_if.h
54 73
diff --git a/src/binlog.c b/src/binlog.c
index 45299f2..e9fb16e 100644
--- a/src/binlog.c
+++ b/src/binlog.c
@@ -29,9 +29,20 @@
29#include <time.h> 29#include <time.h>
30#include "vrt.h" 30#include "vrt.h"
31#include "vcc_if.h" 31#include "vcc_if.h"
32#if VARNISHVERSION == 3
32#include "bin/varnishd/cache.h" 33#include "bin/varnishd/cache.h"
34# define VCL_VOID void
35# define VCL_STRING const char *
36# define MOD_CTX struct sess *
37# define WSPTR(s) ((s)->wrk->ws)
38#else
39# include "bin/varnishd/cache/cache.h"
40# define MOD_CTX const struct vrt_ctx *
41# define WSPTR(s) ((s)->ws)
42#endif
33#include "vmod-binlog.h" 43#include "vmod-binlog.h"
34#include "pack.h" 44#include "pack.h"
45#include "pthread.h"
35 46
36#ifndef O_SEARCH 47#ifndef O_SEARCH
37# define O_SEARCH 0 48# define O_SEARCH 0
@@ -262,7 +273,7 @@ make_key()
262} 273}
263 274
264void 275void
265vmod_init(struct sess *sp, struct vmod_priv *priv, 276vmod_init(MOD_CTX ctx, struct vmod_priv *priv,
266 const char *dir, const char *format, const char *param) 277 const char *dir, const char *format, const char *param)
267{ 278{
268 struct binlog_config *conf = priv->priv; 279 struct binlog_config *conf = priv->priv;
@@ -352,7 +363,6 @@ vmod_init(struct sess *sp, struct vmod_priv *priv,
352 binlog_error("invalid size value"); 363 binlog_error("invalid size value");
353 abort(); 364 abort();
354 } 365 }
355 free(p);
356 switch (*q) { 366 switch (*q) {
357 case 'g': 367 case 'g':
358 case 'G': 368 case 'G':
@@ -369,6 +379,7 @@ vmod_init(struct sess *sp, struct vmod_priv *priv,
369 abort(); 379 abort();
370 } 380 }
371 conf->size = u; 381 conf->size = u;
382 free(p);
372 } else 383 } else
373 conf->size = BINLOG_SIZE; 384 conf->size = BINLOG_SIZE;
374 385
@@ -426,7 +437,7 @@ vmod_init(struct sess *sp, struct vmod_priv *priv,
426} 437}
427 438
428static char * 439static char *
429mkfilename(struct sess *sp, struct binlog_config *conf) 440mkfilename(MOD_CTX ctx, struct binlog_config *conf)
430{ 441{
431 time_t ts = time(NULL); 442 time_t ts = time(NULL);
432 size_t u, n; 443 size_t u, n;
@@ -434,16 +445,16 @@ mkfilename(struct sess *sp, struct binlog_config *conf)
434 445
435 if (conf->flags & BLF_ROUNDTS) 446 if (conf->flags & BLF_ROUNDTS)
436 ts -= ts % conf->interval; 447 ts -= ts % conf->interval;
437 u = WS_Reserve(sp->wrk->ws, 0); 448 u = WS_Reserve(WSPTR(ctx), 0);
438 p = sp->wrk->ws->f; 449 p = WSPTR(ctx)->f;
439 n = strftime(p, u, conf->pattern, gmtime(&ts)); 450 n = strftime(p, u, conf->pattern, gmtime(&ts));
440 if (n == 0) { 451 if (n == 0) {
441 WS_Release(sp->wrk->ws, 0); 452 WS_Release(WSPTR(ctx), 0);
442 return NULL; 453 return NULL;
443 } 454 }
444 q = strdup(p); 455 q = strdup(p);
445 AN(q); 456 AN(q);
446 WS_Release(sp->wrk->ws, 0); 457 WS_Release(WSPTR(ctx), 0);
447 return q; 458 return q;
448} 459}
449 460
@@ -481,7 +492,7 @@ mkdir_p(struct binlog_config *conf, char *dir)
481} 492}
482 493
483static int 494static int
484createfile(struct sess *sp, struct binlog_config *conf) 495createfile(MOD_CTX ctx, struct binlog_config *conf)
485{ 496{
486 char *fname; 497 char *fname;
487 int fd; 498 int fd;
@@ -489,7 +500,7 @@ createfile(struct sess *sp, struct binlog_config *conf)
489 conf->fname = NULL; 500 conf->fname = NULL;
490 conf->fd = -1; 501 conf->fd = -1;
491 502
492 fname = mkfilename(sp, conf); 503 fname = mkfilename(ctx, conf);
493 if (!fname) 504 if (!fname)
494 return -1; 505 return -1;
495 if (mkdir_p(conf, fname)) { 506 if (mkdir_p(conf, fname)) {
@@ -591,7 +602,7 @@ checkheader(struct binlog_config *conf, size_t hdrsize)
591} 602}
592 603
593static int 604static int
594newfile(struct sess *sp, struct binlog_config *conf) 605newfile(MOD_CTX ctx, struct binlog_config *conf)
595{ 606{
596 int c; 607 int c;
597 void *base; 608 void *base;
@@ -601,7 +612,7 @@ newfile(struct sess *sp, struct binlog_config *conf)
601 612
602 setstoptime(conf); 613 setstoptime(conf);
603 614
604 if (createfile(sp, conf)) 615 if (createfile(ctx, conf))
605 return -1; 616 return -1;
606 617
607 hdrsize = ((sizeof(struct binlog_file_header) + 618 hdrsize = ((sizeof(struct binlog_file_header) +
@@ -657,7 +668,8 @@ newfile(struct sess *sp, struct binlog_config *conf)
657 debug(conf,1,("reusing log file %s, recnum=%lu", 668 debug(conf,1,("reusing log file %s, recnum=%lu",
658 conf->fname, (unsigned long)conf->base->recnum)); 669 conf->fname, (unsigned long)conf->base->recnum));
659 } else { 670 } else {
660 debug(conf,1,("created new log file %s",conf->fname)); 671 debug(conf,1,("created new log file %s, size %lu",conf->fname,
672 (unsigned long)conf->size));
661 memcpy(conf->base->magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN); 673 memcpy(conf->base->magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN);
662 conf->base->version = BINLOG_VERSION; 674 conf->base->version = BINLOG_VERSION;
663 conf->base->recsize = conf->recsize; 675 conf->base->recsize = conf->recsize;
@@ -674,7 +686,7 @@ newfile(struct sess *sp, struct binlog_config *conf)
674} 686}
675 687
676static void 688static void
677closefile(struct sess *sp, struct binlog_config *conf) 689closefile(MOD_CTX ctx, struct binlog_config *conf)
678{ 690{
679 size_t size; 691 size_t size;
680 692
@@ -691,8 +703,8 @@ closefile(struct sess *sp, struct binlog_config *conf)
691 reset(conf); 703 reset(conf);
692} 704}
693 705
694void 706VCL_VOID
695vmod_start(struct sess *sp, struct vmod_priv *priv) 707vmod_start(MOD_CTX ctx, struct vmod_priv *priv)
696{ 708{
697 struct binlog_config *conf = priv->priv; 709 struct binlog_config *conf = priv->priv;
698 time_t ts; 710 time_t ts;
@@ -705,8 +717,8 @@ vmod_start(struct sess *sp, struct vmod_priv *priv)
705 717
706 if (ts >= conf->stoptime) { 718 if (ts >= conf->stoptime) {
707 AZ(pthread_mutex_lock(&conf->mutex)); 719 AZ(pthread_mutex_lock(&conf->mutex));
708 closefile(sp, conf); 720 closefile(ctx, conf);
709 newfile(sp, conf); 721 newfile(ctx, conf);
710 AZ(pthread_mutex_unlock(&conf->mutex)); 722 AZ(pthread_mutex_unlock(&conf->mutex));
711 } 723 }
712 724
@@ -714,8 +726,8 @@ vmod_start(struct sess *sp, struct vmod_priv *priv)
714 binlog_env_init(ep, conf, ts); 726 binlog_env_init(ep, conf, ts);
715} 727}
716 728
717void 729VCL_VOID
718vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) 730vmod_pack(MOD_CTX ctx, struct vmod_priv *priv, VCL_STRING str)
719{ 731{
720 struct binlog_config *conf = priv->priv; 732 struct binlog_config *conf = priv->priv;
721 struct binlog_env *ep = binlog_env_get(); 733 struct binlog_env *ep = binlog_env_get();
@@ -749,8 +761,8 @@ vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str)
749 ep->state = state_pack; 761 ep->state = state_pack;
750} 762}
751 763
752void 764VCL_VOID
753vmod_commit(struct sess *sp, struct vmod_priv *priv) 765vmod_commit(MOD_CTX ctx, struct vmod_priv *priv)
754{ 766{
755 struct binlog_config *conf = priv->priv; 767 struct binlog_config *conf = priv->priv;
756 struct binlog_env *ep = binlog_env_get(); 768 struct binlog_env *ep = binlog_env_get();
@@ -789,8 +801,8 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv)
789 ep->state = state_init; 801 ep->state = state_init;
790} 802}
791 803
792void 804VCL_VOID
793vmod_sync(struct sess *sp, struct vmod_priv *priv) 805vmod_sync(MOD_CTX ctx, struct vmod_priv *priv)
794{ 806{
795 struct binlog_config *conf = priv->priv; 807 struct binlog_config *conf = priv->priv;
796 808
@@ -803,8 +815,8 @@ vmod_sync(struct sess *sp, struct vmod_priv *priv)
803 AZ(pthread_mutex_unlock(&conf->mutex)); 815 AZ(pthread_mutex_unlock(&conf->mutex));
804 } 816 }
805 817
806void 818VCL_VOID
807vmod_close(struct sess *sp, struct vmod_priv *priv) 819vmod_close(MOD_CTX ctx, struct vmod_priv *priv)
808{ 820{
809 struct binlog_config *conf = priv->priv; 821 struct binlog_config *conf = priv->priv;
810 822
@@ -813,7 +825,7 @@ vmod_close(struct sess *sp, struct vmod_priv *priv)
813 825
814 mutex = conf->mutex; 826 mutex = conf->mutex;
815 AZ(pthread_mutex_lock(&mutex)); 827 AZ(pthread_mutex_lock(&mutex));
816 closefile(sp, conf); 828 closefile(ctx, conf);
817 close(conf->dd); 829 close(conf->dd);
818 free(conf->dir); 830 free(conf->dir);
819 free(conf->pattern); 831 free(conf->pattern);
diff --git a/src/vmod-binlog.h b/src/vmod-binlog.h
index d67f1cd..fec8d24 100644
--- a/src/vmod-binlog.h
+++ b/src/vmod-binlog.h
@@ -1,5 +1,5 @@
1/* This file is part of vmod-binlog 1/* This file is part of vmod-binlog
2 Copyright (C) 2013 Sergey Poznyakoff 2 Copyright (C) 2013-2014 Sergey Poznyakoff
3 3
4 Vmod-binlog is free software; you can redistribute it and/or modify 4 Vmod-binlog is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
14 You should have received a copy of the GNU General Public License 14 You should have received a copy of the GNU General Public License
15 along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. 15 along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17
18#include <stdint.h> 17#include <stdint.h>
19 18
20#define SIZE_T_MAX ((size_t)~0) 19#define SIZE_T_MAX ((size_t)~0)
diff --git a/src/vmod.vcc b/src/vmod.vcc
index 45da354..b090797 100644
--- a/src/vmod.vcc
+++ b/src/vmod.vcc
@@ -1,8 +1,103 @@
1Module binlog 1# This file is part of vmod-binlog
2Init module_init 2# Copyright (C) 2013-2014 Sergey Poznyakoff
3Function VOID init(PRIV_VCL, STRING, STRING, STRING) 3#
4Function VOID start(PRIV_VCL) 4# Vmod-binlog is free software; you can redistribute it and/or modify
5Function VOID commit(PRIV_VCL) 5# it under the terms of the GNU General Public License as published by
6Function VOID pack(PRIV_VCL, STRING) 6# the Free Software Foundation; either version 3, or (at your option)
7Function VOID sync(PRIV_VCL) 7# any later version.
8Function VOID close(PRIV_VCL) 8#
9# Vmod-binlog is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>.
16
17$Module binlog 3 Binary Log File Support
18
19COLOPHON
20========
21This document provides a short description of the **vmod-binlog** module.
22For a detailed documentation, please see the vmod-binlog(3) manual page.
23
24You will find documentation sources in subdirectory **doc** of the
25**vmod-binlog** source tree.
26
27DESCRIPTION
28===========
29This module adds binary log files support to Varnish Cache.
30
31Binary log file consists of an arbitrary number of equally sized
32records. Each record contains a UNIX timestamp in binary form and
33user-defined data. The file begins with a header containing auxiliary
34information.
35
36Binary log files are rotated at preconfigured time intervals. In order
37to speed up searches, their names follow the strftime(3) pattern:
38
39%Y%m%dT%H%M%S.log
40
41Binary logs can be viewed using the binlogcat(1) and binlogsel(1) utilities.
42
43$Init module_init
44$Function VOID init(PRIV_VCL, STRING, STRING, STRING)
45
46Description
47 Initializes binary log engine.
48Example
49 ::
50
51 binlog.init("/var/lib/varnish/binlog", "LZ[64]L",
52 "size=1G;index=1;umask=027;roundts=1;debug=2");
53
54
55$Function VOID start(PRIV_VCL)
56
57Description
58 Initialize new binary record. It should be followed by one or
59 more calls to **binlog.pack** and a final call to **binlog.commit**.
60Example
61 ::
62 binlog.start();
63
64$Function VOID pack(PRIV_VCL, STRING)
65
66Description
67 Pack next data item into the binary log record. The item is
68 converted according to the second argument of **binlog.init**.
69 The conversion specifier is selected using the ordinal number
70 of this **pack** call after the latest **binlog.start**.
71
72$Function VOID commit(PRIV_VCL)
73
74Description
75 Finalize the binary log record and commit it. Any uninitialized
76 fields will be set to 0.
77
78Example
79 ::
80
81 binlog.start();
82 binlog.pack(http.X-Id);
83 binlog.pack(http.req.url);
84 binlog.pack(http.X-Node);
85 binlog.commit();
86
87$Function VOID sync(PRIV_VCL)
88
89Description
90 Synchronize the binary log with the disk file.
91
92$Function VOID close(PRIV_VCL)
93
94Description
95 Close the log. This is supposed to be called from **vcl_fini**.
96
97SEE ALSO
98========
99
100* binlogcat(1)
101* binlogsel(1)
102* vcl(7)
103* varnishd(1)

Return to:

Send suggestions and report system problems to the System administrator.