diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/.gitignore | 2 | ||||
-rw-r--r-- | src/Makefile.am | 37 | ||||
-rw-r--r-- | src/binlog.c | 64 | ||||
-rw-r--r-- | src/vmod-binlog.h | 3 | ||||
-rw-r--r-- | src/vmod.vcc | 111 |
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 parse-datetime.c vcc_if.c vcc_if.h +*.rst +*.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 @@ # This file is part of vmod-binlog -# Copyright (C) 2013 Sergey Poznyakoff +# Copyright (C) 2013-2014 Sergey Poznyakoff # # Vmod-binlog is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -14,7 +14,10 @@ # You should have received a copy of the GNU General Public License # along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. -AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) +AM_CPPFLAGS =\ + -I$(VARNISHSRC)/include\ + -I$(VARNISHSRC)/bin/varnishd\ + -I$(VARNISHSRC) noinst_LIBRARIES = libbinlog.a libbinlog_a_SOURCES = pack.c err.c parse-datetime.y xalloc.c @@ -38,17 +41,33 @@ libvmod_binlog_la_LIBADD= libvmod_binlog_la_SOURCES = \ binlog.c\ pack.c\ - vmod-binlog.h\ - vcc_if.c vcc_if.h - + vmod-binlog.h +nodist_libvmod_binlog_la_SOURCES = vcc_if.c vcc_if.h noinst_HEADERS = pack.h err.h parse-datetime.h xalloc.h -BUILT_SOURCES = vcc_if.c vcc_if.h +binlog.lo: vcc_if.c vcc_if.h + +CLEANFILES = vcc_if.c vcc_if.h *.rst + +if VARNISH4 + vmodtool = $(VARNISHSRC)/lib/libvcc/vmodtool.py + vmodtoolargs = --strict + vccfile = $(top_srcdir)/src/vmod.vcc +else + vmodtool = $(VARNISHSRC)/lib/libvmod_std/vmod.py + vmodtoolargs = + vccfile = vmod.vcc3 + +vmod.vcc3: $(top_srcdir)/src/vmod.vcc + CLEANFILES += vmod.vcc3 +endif + +.vcc.vcc3: + sed -n '/^\$$/{s///;s/\(Module *[^ ][^ ]*\).*/\1/;p}' $< > $@ -vcc_if.c vcc_if.h: $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc - @PYTHON@ $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc +vcc_if.c vcc_if.h: $(vmodtool) $(vccfile) + @PYTHON@ $(vmodtool) $(vmodtoolargs) $(vccfile) EXTRA_DIST = vmod.vcc -CLEANFILES = $(builddir)/vcc_if.c $(builddir)/vcc_if.h 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 @@ #include <time.h> #include "vrt.h" #include "vcc_if.h" +#if VARNISHVERSION == 3 # include "bin/varnishd/cache.h" +# define VCL_VOID void +# define VCL_STRING const char * +# define MOD_CTX struct sess * +# define WSPTR(s) ((s)->wrk->ws) +#else +# include "bin/varnishd/cache/cache.h" +# define MOD_CTX const struct vrt_ctx * +# define WSPTR(s) ((s)->ws) +#endif #include "vmod-binlog.h" #include "pack.h" +#include "pthread.h" #ifndef O_SEARCH # define O_SEARCH 0 @@ -262,7 +273,7 @@ make_key() } void -vmod_init(struct sess *sp, struct vmod_priv *priv, +vmod_init(MOD_CTX ctx, struct vmod_priv *priv, const char *dir, const char *format, const char *param) { struct binlog_config *conf = priv->priv; @@ -352,7 +363,6 @@ vmod_init(struct sess *sp, struct vmod_priv *priv, binlog_error("invalid size value"); abort(); } - free(p); switch (*q) { case 'g': case 'G': @@ -369,6 +379,7 @@ vmod_init(struct sess *sp, struct vmod_priv *priv, abort(); } conf->size = u; + free(p); } else conf->size = BINLOG_SIZE; @@ -426,7 +437,7 @@ vmod_init(struct sess *sp, struct vmod_priv *priv, } static char * -mkfilename(struct sess *sp, struct binlog_config *conf) +mkfilename(MOD_CTX ctx, struct binlog_config *conf) { time_t ts = time(NULL); size_t u, n; @@ -434,16 +445,16 @@ mkfilename(struct sess *sp, struct binlog_config *conf) if (conf->flags & BLF_ROUNDTS) ts -= ts % conf->interval; - u = WS_Reserve(sp->wrk->ws, 0); - p = sp->wrk->ws->f; + u = WS_Reserve(WSPTR(ctx), 0); + p = WSPTR(ctx)->f; n = strftime(p, u, conf->pattern, gmtime(&ts)); if (n == 0) { - WS_Release(sp->wrk->ws, 0); + WS_Release(WSPTR(ctx), 0); return NULL; } q = strdup(p); AN(q); - WS_Release(sp->wrk->ws, 0); + WS_Release(WSPTR(ctx), 0); return q; } @@ -481,7 +492,7 @@ mkdir_p(struct binlog_config *conf, char *dir) } static int -createfile(struct sess *sp, struct binlog_config *conf) +createfile(MOD_CTX ctx, struct binlog_config *conf) { char *fname; int fd; @@ -489,7 +500,7 @@ createfile(struct sess *sp, struct binlog_config *conf) conf->fname = NULL; conf->fd = -1; - fname = mkfilename(sp, conf); + fname = mkfilename(ctx, conf); if (!fname) return -1; if (mkdir_p(conf, fname)) { @@ -591,7 +602,7 @@ checkheader(struct binlog_config *conf, size_t hdrsize) } static int -newfile(struct sess *sp, struct binlog_config *conf) +newfile(MOD_CTX ctx, struct binlog_config *conf) { int c; void *base; @@ -601,7 +612,7 @@ newfile(struct sess *sp, struct binlog_config *conf) setstoptime(conf); - if (createfile(sp, conf)) + if (createfile(ctx, conf)) return -1; hdrsize = ((sizeof(struct binlog_file_header) + @@ -657,7 +668,8 @@ newfile(struct sess *sp, struct binlog_config *conf) debug(conf,1,("reusing log file %s, recnum=%lu", conf->fname, (unsigned long)conf->base->recnum)); } else { - debug(conf,1,("created new log file %s",conf->fname)); + debug(conf,1,("created new log file %s, size %lu",conf->fname, + (unsigned long)conf->size)); memcpy(conf->base->magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN); conf->base->version = BINLOG_VERSION; conf->base->recsize = conf->recsize; @@ -674,7 +686,7 @@ newfile(struct sess *sp, struct binlog_config *conf) } static void -closefile(struct sess *sp, struct binlog_config *conf) +closefile(MOD_CTX ctx, struct binlog_config *conf) { size_t size; @@ -691,8 +703,8 @@ closefile(struct sess *sp, struct binlog_config *conf) reset(conf); } -void -vmod_start(struct sess *sp, struct vmod_priv *priv) +VCL_VOID +vmod_start(MOD_CTX ctx, struct vmod_priv *priv) { struct binlog_config *conf = priv->priv; time_t ts; @@ -705,8 +717,8 @@ vmod_start(struct sess *sp, struct vmod_priv *priv) if (ts >= conf->stoptime) { AZ(pthread_mutex_lock(&conf->mutex)); - closefile(sp, conf); - newfile(sp, conf); + closefile(ctx, conf); + newfile(ctx, conf); AZ(pthread_mutex_unlock(&conf->mutex)); } @@ -714,8 +726,8 @@ vmod_start(struct sess *sp, struct vmod_priv *priv) binlog_env_init(ep, conf, ts); } -void -vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) +VCL_VOID +vmod_pack(MOD_CTX ctx, struct vmod_priv *priv, VCL_STRING str) { struct binlog_config *conf = priv->priv; struct binlog_env *ep = binlog_env_get(); @@ -749,8 +761,8 @@ vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) ep->state = state_pack; } -void -vmod_commit(struct sess *sp, struct vmod_priv *priv) +VCL_VOID +vmod_commit(MOD_CTX ctx, struct vmod_priv *priv) { struct binlog_config *conf = priv->priv; struct binlog_env *ep = binlog_env_get(); @@ -789,8 +801,8 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv) ep->state = state_init; } -void -vmod_sync(struct sess *sp, struct vmod_priv *priv) +VCL_VOID +vmod_sync(MOD_CTX ctx, struct vmod_priv *priv) { struct binlog_config *conf = priv->priv; @@ -803,8 +815,8 @@ vmod_sync(struct sess *sp, struct vmod_priv *priv) AZ(pthread_mutex_unlock(&conf->mutex)); } -void -vmod_close(struct sess *sp, struct vmod_priv *priv) +VCL_VOID +vmod_close(MOD_CTX ctx, struct vmod_priv *priv) { struct binlog_config *conf = priv->priv; @@ -813,7 +825,7 @@ vmod_close(struct sess *sp, struct vmod_priv *priv) mutex = conf->mutex; AZ(pthread_mutex_lock(&mutex)); - closefile(sp, conf); + closefile(ctx, conf); close(conf->dd); free(conf->dir); 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 @@ /* This file is part of vmod-binlog - Copyright (C) 2013 Sergey Poznyakoff + Copyright (C) 2013-2014 Sergey Poznyakoff Vmod-binlog is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,7 +14,6 @@ You should have received a copy of the GNU General Public License along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. */ - #include <stdint.h> #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 @@ -Module binlog -Init module_init -Function VOID init(PRIV_VCL, STRING, STRING, STRING) -Function VOID start(PRIV_VCL) -Function VOID commit(PRIV_VCL) -Function VOID pack(PRIV_VCL, STRING) -Function VOID sync(PRIV_VCL) -Function VOID close(PRIV_VCL) +# This file is part of vmod-binlog +# Copyright (C) 2013-2014 Sergey Poznyakoff +# +# Vmod-binlog is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Vmod-binlog is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. + +$Module binlog 3 Binary Log File Support + +COLOPHON +======== +This document provides a short description of the **vmod-binlog** module. +For a detailed documentation, please see the vmod-binlog(3) manual page. + +You will find documentation sources in subdirectory **doc** of the +**vmod-binlog** source tree. + +DESCRIPTION +=========== +This module adds binary log files support to Varnish Cache. + +Binary log file consists of an arbitrary number of equally sized +records. Each record contains a UNIX timestamp in binary form and +user-defined data. The file begins with a header containing auxiliary +information. + +Binary log files are rotated at preconfigured time intervals. In order +to speed up searches, their names follow the strftime(3) pattern: + +%Y%m%dT%H%M%S.log + +Binary logs can be viewed using the binlogcat(1) and binlogsel(1) utilities. + +$Init module_init +$Function VOID init(PRIV_VCL, STRING, STRING, STRING) + +Description + Initializes binary log engine. +Example + :: + + binlog.init("/var/lib/varnish/binlog", "LZ[64]L", + "size=1G;index=1;umask=027;roundts=1;debug=2"); + + +$Function VOID start(PRIV_VCL) + +Description + Initialize new binary record. It should be followed by one or + more calls to **binlog.pack** and a final call to **binlog.commit**. +Example + :: + binlog.start(); + +$Function VOID pack(PRIV_VCL, STRING) + +Description + Pack next data item into the binary log record. The item is + converted according to the second argument of **binlog.init**. + The conversion specifier is selected using the ordinal number + of this **pack** call after the latest **binlog.start**. + +$Function VOID commit(PRIV_VCL) + +Description + Finalize the binary log record and commit it. Any uninitialized + fields will be set to 0. + +Example + :: + + binlog.start(); + binlog.pack(http.X-Id); + binlog.pack(http.req.url); + binlog.pack(http.X-Node); + binlog.commit(); + +$Function VOID sync(PRIV_VCL) + +Description + Synchronize the binary log with the disk file. + +$Function VOID close(PRIV_VCL) + +Description + Close the log. This is supposed to be called from **vcl_fini**. + +SEE ALSO +======== + +* binlogcat(1) +* binlogsel(1) +* vcl(7) +* varnishd(1) |