[master] 36e8ac2 Move VUT into libvarnishapi
Poul-Henning Kamp
phk at FreeBSD.org
Tue May 10 12:52:06 CEST 2016
commit 36e8ac2fc77e8f8b63ea0a41b14d6555ea50433c
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Tue May 10 10:51:20 2016 +0000
Move VUT into libvarnishapi
diff --git a/Makefile.inc.phk b/Makefile.inc.phk
index 680bab1..ba9202f 100644
--- a/Makefile.inc.phk
+++ b/Makefile.inc.phk
@@ -28,7 +28,6 @@ INSTALL_BASE ?= $(TOPDIR)/_install
LIB_VARNISH = -L $(TOPDIR)/lib/libvarnish -lvarnish
LIB_VARNISHAPI = $(TOPDIR)/lib/libvarnishapi/libvarnishapi.a
-LIB_VARNISHTOOLS = -L $(TOPDIR)/lib/libvarnishtools -lvarnishtools
LIB_VCC = -L $(TOPDIR)/lib/libvcc -lvcc
LIB_VGZ = -L $(TOPDIR)/lib/libvgz -lvgz
LIB_PCRE = -L /usr/local/lib -lpcre
diff --git a/bin/varnishd/Makefile.phk b/bin/varnishd/Makefile.phk
index fb9df6c..54f058a 100644
--- a/bin/varnishd/Makefile.phk
+++ b/bin/varnishd/Makefile.phk
@@ -78,15 +78,17 @@ PROG_SRC += mgt/mgt_vcc.c
PROG_SRC += mgt/mgt_vcl.c
PROG_SRC += mgt/mgt_acceptor.c
-PROG_SRC += storage/stevedore.c
PROG_SRC += storage/mgt_stevedore.c
+PROG_SRC += storage/mgt_storage_persistent.c
+PROG_SRC += storage/stevedore.c
PROG_SRC += storage/stevedore_utils.c
PROG_SRC += storage/storage_file.c
+PROG_SRC += storage/storage_lru.c
PROG_SRC += storage/storage_malloc.c
PROG_SRC += storage/storage_persistent.c
-PROG_SRC += storage/mgt_storage_persistent.c
PROG_SRC += storage/storage_persistent_silo.c
PROG_SRC += storage/storage_persistent_subr.c
+PROG_SRC += storage/storage_simple.c
PROG_SRC += storage/storage_umem.c
PROG_SRC += waiter/cache_waiter.c
diff --git a/bin/varnishhist/Makefile.am b/bin/varnishhist/Makefile.am
index c8d2820..f814bf2 100644
--- a/bin/varnishhist/Makefile.am
+++ b/bin/varnishhist/Makefile.am
@@ -11,10 +11,7 @@ varnishhist_SOURCES = varnishhist.c \
varnishhist_options.c \
$(top_srcdir)/lib/libvarnish/vas.c \
$(top_srcdir)/lib/libvarnish/version.c \
- $(top_srcdir)/lib/libvarnish/vpf.c \
- $(top_srcdir)/lib/libvarnish/vtim.c \
- $(top_srcdir)/lib/libvarnish/vfl.c \
- $(top_srcdir)/lib/libvarnishtools/vut.c
+ $(top_srcdir)/lib/libvarnish/vtim.c
varnishhist_CFLAGS = \
@SAN_CFLAGS@
diff --git a/bin/varnishhist/Makefile.phk b/bin/varnishhist/Makefile.phk
index 77e8c31..c2df17f 100644
--- a/bin/varnishhist/Makefile.phk
+++ b/bin/varnishhist/Makefile.phk
@@ -1,7 +1,6 @@
PROG_SRC = varnishhist.c
PROG_SRC += varnishhist_options.c
-LD_ADD += ${LIB_VARNISHTOOLS}
LD_ADD += ${LIB_VARNISHAPI}
LD_ADD += ${LIB_VARNISH}
LD_ADD += -lpthread -lncurses -lm ${LIB_PCRE}
diff --git a/bin/varnishlog/Makefile.am b/bin/varnishlog/Makefile.am
index 80c7ed2..92b93d3 100644
--- a/bin/varnishlog/Makefile.am
+++ b/bin/varnishlog/Makefile.am
@@ -10,12 +10,9 @@ varnishlog_SOURCES = \
varnishlog.c \
varnishlog_options.h \
varnishlog_options.c \
- $(top_srcdir)/lib/libvarnishtools/vut.c \
$(top_srcdir)/lib/libvarnish/vas.c \
$(top_srcdir)/lib/libvarnish/version.c \
- $(top_srcdir)/lib/libvarnish/vfl.c \
$(top_srcdir)/lib/libvarnish/vsb.c \
- $(top_srcdir)/lib/libvarnish/vpf.c \
$(top_srcdir)/lib/libvarnish/vtim.c
varnishlog_CFLAGS = \
diff --git a/bin/varnishlog/Makefile.phk b/bin/varnishlog/Makefile.phk
index 5f0de52..c024046 100644
--- a/bin/varnishlog/Makefile.phk
+++ b/bin/varnishlog/Makefile.phk
@@ -1,7 +1,6 @@
PROG_SRC = varnishlog.c
PROG_SRC = varnishlog_options.c
-LD_ADD += ${LIB_VARNISHTOOLS}
LD_ADD += ${LIB_VARNISHAPI}
LD_ADD += ${LIB_VARNISH}
LD_ADD += ${LIB_PCRE}
diff --git a/bin/varnishncsa/Makefile.am b/bin/varnishncsa/Makefile.am
index dcdd7b2..bb470f7 100644
--- a/bin/varnishncsa/Makefile.am
+++ b/bin/varnishncsa/Makefile.am
@@ -12,11 +12,8 @@ varnishncsa_SOURCES = \
varnishncsa_options.c \
base64.h \
base64.c \
- $(top_srcdir)/lib/libvarnishtools/vut.c \
$(top_srcdir)/lib/libvarnish/vas.c \
$(top_srcdir)/lib/libvarnish/version.c \
- $(top_srcdir)/lib/libvarnish/vfl.c \
- $(top_srcdir)/lib/libvarnish/vpf.c \
$(top_srcdir)/lib/libvarnish/vtim.c \
$(top_srcdir)/lib/libvarnish/vsb.c
diff --git a/bin/varnishncsa/Makefile.phk b/bin/varnishncsa/Makefile.phk
index 10fd638..fd73e25 100644
--- a/bin/varnishncsa/Makefile.phk
+++ b/bin/varnishncsa/Makefile.phk
@@ -2,7 +2,6 @@ PROG_SRC += base64.c
PROG_SRC += varnishncsa.c
PROG_SRC += varnishncsa_options.c
-LD_ADD += ${LIB_VARNISHTOOLS}
LD_ADD += ${LIB_VARNISHAPI}
LD_ADD += ${LIB_VARNISH}
LD_ADD += ${LIB_PCRE}
diff --git a/bin/varnishtop/Makefile.am b/bin/varnishtop/Makefile.am
index 4757839..9406d62 100644
--- a/bin/varnishtop/Makefile.am
+++ b/bin/varnishtop/Makefile.am
@@ -9,11 +9,8 @@ bin_PROGRAMS = varnishtop
varnishtop_SOURCES = varnishtop.c \
varnishtop_options.h \
varnishtop_options.c \
- $(top_srcdir)/lib/libvarnishtools/vut.c \
$(top_srcdir)/lib/libvarnish/vas.c \
$(top_srcdir)/lib/libvarnish/version.c \
- $(top_srcdir)/lib/libvarnish/vfl.c \
- $(top_srcdir)/lib/libvarnish/vpf.c \
$(top_srcdir)/lib/libvarnish/vtim.c \
$(top_srcdir)/lib/libvarnish/vsb.c
diff --git a/bin/varnishtop/Makefile.phk b/bin/varnishtop/Makefile.phk
index 7f8d760..12d4de3 100644
--- a/bin/varnishtop/Makefile.phk
+++ b/bin/varnishtop/Makefile.phk
@@ -1,7 +1,6 @@
PROG_SRC += varnishtop.c
PROG_SRC += varnishtop_options.c
-LD_ADD += ${LIB_VARNISHTOOLS}
LD_ADD += ${LIB_VARNISHAPI}
LD_ADD += ${LIB_VARNISH}
LD_ADD += ${LIB_PCRE}
diff --git a/lib/Makefile.am b/lib/Makefile.am
index c0c0e60..9359575 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,7 +4,6 @@ SUBDIRS = \
libvarnishcompat \
libvarnish \
libvarnishapi \
- libvarnishtools \
libvcc \
libvgz \
libvmod_debug \
diff --git a/lib/Makefile.phk b/lib/Makefile.phk
index 83d0674..86ea172 100644
--- a/lib/Makefile.phk
+++ b/lib/Makefile.phk
@@ -1,4 +1,4 @@
-SUBDIRS = libvcc libvarnish libvarnishapi libvarnishtools libvcc
+SUBDIRS = libvcc libvarnish libvarnishapi libvcc
SUBDIRS += libvgz
SUBDIRS += libvmod_debug libvmod_directors libvmod_std
diff --git a/lib/libvarnish/Makefile.phk b/lib/libvarnish/Makefile.phk
index 2a0c37a..501a350 100644
--- a/lib/libvarnish/Makefile.phk
+++ b/lib/libvarnish/Makefile.phk
@@ -3,13 +3,13 @@ LIB_SRC += binary_heap.c
LIB_SRC += cli_auth.c
LIB_SRC += cli_common.c
LIB_SRC += cli_serve.c
-LIB_SRC += flopen.c
LIB_SRC += vas.c
LIB_SRC += vav.c
LIB_SRC += vct.c
LIB_SRC += version.c
LIB_SRC += vev.c
LIB_SRC += vfil.c
+LIB_SRC += vfl.c
LIB_SRC += vin.c
LIB_SRC += vlu.c
LIB_SRC += vmb.c
diff --git a/lib/libvarnishapi/Makefile.am b/lib/libvarnishapi/Makefile.am
index 32986c4..03fefe4 100644
--- a/lib/libvarnishapi/Makefile.am
+++ b/lib/libvarnishapi/Makefile.am
@@ -22,8 +22,10 @@ libvarnishapi_la_SOURCES = \
../libvarnish/version.c \
../libvarnish/cli_common.c \
../libvarnish/cli_auth.c \
+ ../libvarnish/vfl.c \
../libvarnish/vin.c \
../libvarnish/vmb.c \
+ ../libvarnish/vpf.c \
../libvarnish/vre.c \
../libvarnish/vsb.c \
../libvarnish/vtim.c \
@@ -36,6 +38,7 @@ libvarnishapi_la_SOURCES = \
vsl_query.c \
vsl.c \
vsc.c \
+ vut.c \
vxp.c \
vxp_parse.c \
vxp_lexer.c \
diff --git a/lib/libvarnishapi/Makefile.phk b/lib/libvarnishapi/Makefile.phk
index 2c3e907..3c4eca9 100644
--- a/lib/libvarnishapi/Makefile.phk
+++ b/lib/libvarnishapi/Makefile.phk
@@ -7,6 +7,7 @@ LIB_SRC += vsl_cursor.c
LIB_SRC += vsl_dispatch.c
LIB_SRC += vsl_query.c
LIB_SRC += vsm.c
+LIB_SRC += vut.c
LIB_SRC += vxp.c
LIB_SRC += vxp_fixed_token.c
LIB_SRC += vxp_lexer.c
@@ -16,11 +17,13 @@ LIB_SRC += vxp_parse.c
# We add more stuff to the SHLIB version to make it self-contained
SHLIB_SRC = $(LIB_SRC)
VPATH += ../libvarnish
-SHLIB_SRC += vre.c
-SHLIB_SRC += vsb.c
SHLIB_SRC += vas.c
SHLIB_SRC += vav.c
+SHLIB_SRC += vfl.c
SHLIB_SRC += vin.c
+SHLIB_SRC += vpf.c
+SHLIB_SRC += vre.c
+SHLIB_SRC += vsb.c
SHLIB_SRC += vtim.c
#LIB_SRC += vsl_glob_test.c
diff --git a/lib/libvarnishapi/libvarnishapi.map b/lib/libvarnishapi/libvarnishapi.map
index cd3940f..3401363 100644
--- a/lib/libvarnishapi/libvarnishapi.map
+++ b/lib/libvarnishapi/libvarnishapi.map
@@ -131,3 +131,15 @@ LIBVARNISHAPI_1.4 {
VSLQ_SetCursor;
VSM_IsOpen;
} LIBVARNISHAPI_1.0;
+
+LIBVARNISHAPI_1.5 {
+ global:
+ VUT_Error;
+ VUT_g_Arg;
+ VUT_Arg;
+ VUT_Setup;
+ VUT_Init;
+ VUT_Fini;
+ VUT_Main;
+ VUT;
+} LIBVARNISHAPI_1.0;
diff --git a/lib/libvarnishapi/vut.c b/lib/libvarnishapi/vut.c
new file mode 100644
index 0000000..39c27fb
--- /dev/null
+++ b/lib/libvarnishapi/vut.c
@@ -0,0 +1,500 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2015 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Martin Blix Grydeland <martin at varnish-software.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Common functions for the utilities
+ */
+
+#include "config.h"
+
+#include <ctype.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <signal.h>
+#include <math.h>
+
+#include "compat/daemon.h"
+#include "vdef.h"
+#include "vpf.h"
+#include "vapi/vsm.h"
+#include "vapi/vsl.h"
+#include "vtim.h"
+#include "vas.h"
+#include "miniobj.h"
+#include "vcs.h"
+#include "vnum.h"
+
+#include "vut.h"
+
+#include "vapi/voptget.h"
+
+struct VUT VUT;
+
+static int vut_synopsis(const struct vopt_spec *);
+static int vut_options(const struct vopt_spec *);
+
+static void
+vut_vpf_remove(void)
+{
+ if (VUT.pfh) {
+ VPF_Remove(VUT.pfh);
+ VUT.pfh = NULL;
+ }
+}
+
+static void
+vut_sighup(int sig)
+{
+ (void)sig;
+ VUT.sighup = 1;
+}
+
+static void
+vut_sigint(int sig)
+{
+ (void)sig;
+ VUT.sigint = 1;
+}
+
+static void
+vut_sigusr1(int sig)
+{
+ (void)sig;
+ VUT.sigusr1 = 1;
+}
+
+static int __match_proto__(VSLQ_dispatch_f)
+vut_dispatch(struct VSL_data *vsl, struct VSL_transaction * const trans[],
+ void *priv)
+{
+ int i;
+
+ (void)priv;
+ if (VUT.k_arg == 0)
+ return (-1); /* End of file */
+ AN(VUT.dispatch_f);
+ i = VUT.dispatch_f(vsl, trans, VUT.dispatch_priv);
+ if (VUT.k_arg > 0)
+ VUT.k_arg--;
+ if (i >= 0 && VUT.k_arg == 0)
+ return (-1); /* End of file */
+ return (i);
+}
+
+void
+VUT_Error(int status, const char *fmt, ...)
+{
+ va_list ap;
+
+ AN(fmt);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
+
+ if (status)
+ exit(status);
+}
+
+int
+VUT_g_Arg(const char *arg)
+{
+
+ VUT.g_arg = VSLQ_Name2Grouping(arg, -1);
+ if (VUT.g_arg == -2)
+ VUT_Error(1, "Ambiguous grouping type: %s", arg);
+ else if (VUT.g_arg < 0)
+ VUT_Error(1, "Unknown grouping type: %s", arg);
+ return (1);
+}
+
+int
+VUT_Arg(int opt, const char *arg)
+{
+ int i;
+ char *p;
+
+ switch (opt) {
+ case 'd':
+ /* Head */
+ VUT.d_opt = 1;
+ return (1);
+ case 'D':
+ /* Daemon mode */
+ VUT.D_opt = 1;
+ return (1);
+ case 'g':
+ /* Grouping */
+ return (VUT_g_Arg(arg));
+ case 'k':
+ /* Log transaction limit */
+ VUT.k_arg = (int)strtol(arg, &p, 10);
+ if (*p != '\0' || VUT.k_arg <= 0)
+ VUT_Error(1, "-k: Invalid number '%s'", arg);
+ return (1);
+ case 'n':
+ /* Varnish instance name */
+ REPLACE(VUT.n_arg, arg);
+ return (1);
+ case 'N':
+ /* Varnish stale VSM file */
+ REPLACE(VUT.N_arg, arg);
+ return (1);
+ case 'P':
+ /* PID file */
+ REPLACE(VUT.P_arg, arg);
+ return (1);
+ case 'q':
+ /* Query to use */
+ REPLACE(VUT.q_arg, arg);
+ return (1);
+ case 'r':
+ /* Binary file input */
+ REPLACE(VUT.r_arg, arg);
+ return (1);
+ case 't':
+ /* VSM connect timeout */
+ if (!strcasecmp("off", arg))
+ VUT.t_arg = -1.;
+ else {
+ VUT.t_arg = VNUM(arg);
+ if (isnan(VUT.t_arg))
+ VUT_Error(1, "-t: Syntax error");
+ if (VUT.t_arg < 0.)
+ VUT_Error(1, "-t: Range error");
+ }
+ return (1);
+ case 'V':
+ /* Print version number and exit */
+ VCS_Message(VUT.progname);
+ exit(0);
+ default:
+ AN(VUT.vsl);
+ i = VSL_Arg(VUT.vsl, opt, arg);
+ if (i < 0)
+ VUT_Error(1, "%s", VSL_Error(VUT.vsl));
+ return (i);
+ }
+}
+
+void
+VUT_Init(const char *progname, int argc, char * const *argv,
+ const struct vopt_spec *voc)
+{
+
+ if (argc == 2 && !strcmp(argv[1], "--synopsis"))
+ exit(vut_synopsis(voc));
+ if (argc == 2 && !strcmp(argv[1], "--options"))
+ exit(vut_options(voc));
+
+ VUT.progname = progname;
+ REPLACE(VUT.name, "");
+ VUT.g_arg = VSL_g_vxid;
+ AZ(VUT.vsl);
+ VUT.vsl = VSL_New();
+ AN(VUT.vsl);
+ VUT.k_arg = -1;
+ VUT.t_arg = 5.;
+}
+
+void
+VUT_Setup(void)
+{
+ struct VSL_cursor *c;
+ double t_start;
+ int i;
+
+ AN(VUT.vsl);
+ AZ(VUT.vsm);
+ AZ(VUT.vslq);
+
+ /* Check input arguments */
+ if ((VUT.n_arg == NULL ? 0 : 1) +
+ (VUT.N_arg == NULL ? 0 : 1) +
+ (VUT.r_arg == NULL ? 0 : 1) > 1)
+ VUT_Error(1, "Only one of -n, -N and -r options may be used");
+
+ /* Create and validate the query expression */
+ VUT.vslq = VSLQ_New(VUT.vsl, NULL, VUT.g_arg, VUT.q_arg);
+ if (VUT.vslq == NULL)
+ VUT_Error(1, "Query expression error:\n%s", VSL_Error(VUT.vsl));
+
+ /* Setup input */
+ if (VUT.r_arg) {
+ REPLACE(VUT.name, VUT.r_arg);
+ c = VSL_CursorFile(VUT.vsl, VUT.r_arg, 0);
+ if (c == NULL)
+ VUT_Error(1, "Can't open log file (%s)",
+ VSL_Error(VUT.vsl));
+ } else {
+ VUT.vsm = VSM_New();
+ AN(VUT.vsm);
+ if (VUT.n_arg && VSM_n_Arg(VUT.vsm, VUT.n_arg) <= 0)
+ VUT_Error(1, "%s", VSM_Error(VUT.vsm));
+ if (VUT.N_arg && VSM_N_Arg(VUT.vsm, VUT.N_arg) <= 0)
+ VUT_Error(1, "%s", VSM_Error(VUT.vsm));
+ REPLACE(VUT.name, VSM_Name(VUT.vsm));
+ t_start = NAN;
+ c = NULL;
+ while (1) {
+ i = VSM_Open(VUT.vsm);
+ if (!i)
+ c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
+ (VUT.d_opt ? VSL_COPT_TAILSTOP :
+ VSL_COPT_TAIL)
+ | VSL_COPT_BATCH);
+ if (c)
+ break;
+
+ if (isnan(t_start) && VUT.t_arg > 0.) {
+ VUT_Error(0, "Can't open log -"
+ " retrying for %.0f seconds", VUT.t_arg);
+ t_start = VTIM_real();
+ }
+ VSM_Close(VUT.vsm);
+ if (VUT.t_arg <= 0.)
+ break;
+ if (VTIM_real() - t_start > VUT.t_arg)
+ break;
+
+ VSM_ResetError(VUT.vsm);
+ VSL_ResetError(VUT.vsl);
+ VTIM_sleep(0.5);
+ }
+
+ if (VUT.t_arg >= 0. && (i || !c)) {
+ if (i)
+ VUT_Error(1, "Can't open VSM file (%s)",
+ VSM_Error(VUT.vsm));
+ else
+ VUT_Error(1, "Can't open log (%s)",
+ VSL_Error(VUT.vsl));
+ } else if (!isnan(t_start))
+ VUT_Error(0, "Log opened");
+ }
+
+ if (c)
+ VSLQ_SetCursor(VUT.vslq, &c);
+ AZ(c);
+
+ /* Signal handlers */
+ (void)signal(SIGHUP, vut_sighup);
+ (void)signal(SIGINT, vut_sigint);
+ (void)signal(SIGTERM, vut_sigint);
+ (void)signal(SIGUSR1, vut_sigusr1);
+
+ /* Open PID file */
+ if (VUT.P_arg) {
+ AZ(VUT.pfh);
+ VUT.pfh = VPF_Open(VUT.P_arg, 0644, NULL);
+ if (VUT.pfh == NULL)
+ VUT_Error(1, "%s: %s", VUT.P_arg, strerror(errno));
+ }
+
+ /* Daemon mode */
+ if (VUT.D_opt && varnish_daemon(0, 0) == -1)
+ VUT_Error(1, "Daemon mode: %s", strerror(errno));
+
+ /* Write PID and setup exit handler */
+ if (VUT.pfh != NULL) {
+ VPF_Write(VUT.pfh);
+ AZ(atexit(vut_vpf_remove));
+ }
+}
+
+void
+VUT_Fini(void)
+{
+ free(VUT.n_arg);
+ free(VUT.N_arg);
+ free(VUT.r_arg);
+ free(VUT.P_arg);
+ free(VUT.name);
+
+ vut_vpf_remove();
+ AZ(VUT.pfh);
+
+ if (VUT.vslq)
+ VSLQ_Delete(&VUT.vslq);
+ if (VUT.vsl)
+ VSL_Delete(VUT.vsl);
+ if (VUT.vsm)
+ VSM_Delete(VUT.vsm);
+
+ memset(&VUT, 0, sizeof VUT);
+}
+
+int
+VUT_Main(void)
+{
+ struct VSL_cursor *c;
+ int i = -1;
+
+ AN(VUT.vslq);
+
+ while (!VUT.sigint) {
+ if (VUT.sighup && VUT.sighup_f) {
+ /* sighup callback */
+ VUT.sighup = 0;
+ i = (VUT.sighup_f)();
+ if (i)
+ break;
+ }
+
+ if (VUT.sigusr1) {
+ /* Flush and report any incomplete records */
+ VUT.sigusr1 = 0;
+ VSLQ_Flush(VUT.vslq, vut_dispatch, NULL);
+ }
+
+ if (VUT.vsm != NULL && !VSM_IsOpen(VUT.vsm)) {
+ /* Reconnect VSM */
+ AZ(VUT.r_arg);
+ VTIM_sleep(0.1);
+ if (VSM_Open(VUT.vsm)) {
+ VSM_ResetError(VUT.vsm);
+ continue;
+ }
+ c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
+ (VUT.d_opt ? VSL_COPT_TAILSTOP : VSL_COPT_TAIL)
+ | VSL_COPT_BATCH);
+ if (c == NULL) {
+ VSL_ResetError(VUT.vsl);
+ VSM_Close(VUT.vsm);
+ continue;
+ }
+ VSLQ_SetCursor(VUT.vslq, &c);
+ AZ(c);
+ VUT_Error(0, "Log reacquired");
+ }
+
+ i = VSLQ_Dispatch(VUT.vslq, vut_dispatch, NULL);
+ if (i == 1)
+ /* Call again */
+ continue;
+ else if (i == 0) {
+ /* Nothing to do but wait */
+ if (VUT.idle_f) {
+ i = (VUT.idle_f)();
+ if (i)
+ break;
+ }
+ VTIM_sleep(0.01);
+ continue;
+ } else if (i == -1) {
+ /* EOF */
+ break;
+ }
+
+ if (VUT.vsm == NULL)
+ break;
+
+ /* XXX: Make continuation optional */
+
+ VSLQ_Flush(VUT.vslq, vut_dispatch, NULL);
+
+ if (i == -2)
+ /* Abandoned */
+ VUT_Error(0, "Log abandoned");
+ else if (i < -2)
+ /* Overrun */
+ VUT_Error(0, "Log overrun");
+
+ VSM_Close(VUT.vsm);
+ }
+
+ return (i);
+}
+
+/**********************************************************************/
+
+
+static void
+print_nobrackets(const char *s)
+{
+ const char *e;
+
+ /* Remove whitespace */
+ while (isspace(*s))
+ s++;
+ e = s + strlen(s);
+ while (e > s && isspace(e[-1]))
+ e--;
+
+ /* Remove outer layer brackets if present */
+ if (e > s && *s == '[' && e[-1] == ']') {
+ s++;
+ e--;
+ }
+
+ printf("%.*s", (int)(e - s), s);
+}
+
+static void
+print_tabbed(const char *string, int tabs)
+{
+ int i;
+ const char *c;
+
+ for (c = string; *c; c++) {
+ if (c == string || *(c - 1) == '\n')
+ for (i = 0; i < tabs; i++)
+ printf("\t");
+ printf("%c", *c);
+ }
+}
+
+static void
+print_opt(const struct vopt_list *opt)
+{
+ print_nobrackets(opt->synopsis);
+ printf("\n\n");
+ print_tabbed(opt->ldesc, 1);
+ printf("\n\n");
+}
+
+static int
+vut_synopsis(const struct vopt_spec *voc)
+{
+ printf(".. |synopsis| replace:: %s\n", voc->vopt_synopsis);
+ return (0);
+}
+
+static int
+vut_options(const struct vopt_spec *voc)
+{
+ int i;
+
+ for (i = 0; i < voc->vopt_list_n; i++)
+ print_opt(&voc->vopt_list[i]);
+ return (0);
+}
diff --git a/lib/libvarnishtools/Makefile.am b/lib/libvarnishtools/Makefile.am
deleted file mode 100644
index 5fffe27..0000000
--- a/lib/libvarnishtools/Makefile.am
+++ /dev/null
@@ -1,4 +0,0 @@
-#
-
-EXTRA_DIST = \
- vut.c
diff --git a/lib/libvarnishtools/Makefile.phk b/lib/libvarnishtools/Makefile.phk
deleted file mode 100644
index 0781a45..0000000
--- a/lib/libvarnishtools/Makefile.phk
+++ /dev/null
@@ -1,6 +0,0 @@
-
-LIB_SRC += opt2rst.c
-LIB_SRC += vut.c
-
-TOPDIR= $(CURDIR)/../..
-include $(TOPDIR)/Makefile.inc.phk
diff --git a/lib/libvarnishtools/vut.c b/lib/libvarnishtools/vut.c
deleted file mode 100644
index 39c27fb..0000000
--- a/lib/libvarnishtools/vut.c
+++ /dev/null
@@ -1,500 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2015 Varnish Software AS
- * All rights reserved.
- *
- * Author: Martin Blix Grydeland <martin at varnish-software.com>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * Common functions for the utilities
- */
-
-#include "config.h"
-
-#include <ctype.h>
-#include <stdint.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <math.h>
-
-#include "compat/daemon.h"
-#include "vdef.h"
-#include "vpf.h"
-#include "vapi/vsm.h"
-#include "vapi/vsl.h"
-#include "vtim.h"
-#include "vas.h"
-#include "miniobj.h"
-#include "vcs.h"
-#include "vnum.h"
-
-#include "vut.h"
-
-#include "vapi/voptget.h"
-
-struct VUT VUT;
-
-static int vut_synopsis(const struct vopt_spec *);
-static int vut_options(const struct vopt_spec *);
-
-static void
-vut_vpf_remove(void)
-{
- if (VUT.pfh) {
- VPF_Remove(VUT.pfh);
- VUT.pfh = NULL;
- }
-}
-
-static void
-vut_sighup(int sig)
-{
- (void)sig;
- VUT.sighup = 1;
-}
-
-static void
-vut_sigint(int sig)
-{
- (void)sig;
- VUT.sigint = 1;
-}
-
-static void
-vut_sigusr1(int sig)
-{
- (void)sig;
- VUT.sigusr1 = 1;
-}
-
-static int __match_proto__(VSLQ_dispatch_f)
-vut_dispatch(struct VSL_data *vsl, struct VSL_transaction * const trans[],
- void *priv)
-{
- int i;
-
- (void)priv;
- if (VUT.k_arg == 0)
- return (-1); /* End of file */
- AN(VUT.dispatch_f);
- i = VUT.dispatch_f(vsl, trans, VUT.dispatch_priv);
- if (VUT.k_arg > 0)
- VUT.k_arg--;
- if (i >= 0 && VUT.k_arg == 0)
- return (-1); /* End of file */
- return (i);
-}
-
-void
-VUT_Error(int status, const char *fmt, ...)
-{
- va_list ap;
-
- AN(fmt);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- fprintf(stderr, "\n");
-
- if (status)
- exit(status);
-}
-
-int
-VUT_g_Arg(const char *arg)
-{
-
- VUT.g_arg = VSLQ_Name2Grouping(arg, -1);
- if (VUT.g_arg == -2)
- VUT_Error(1, "Ambiguous grouping type: %s", arg);
- else if (VUT.g_arg < 0)
- VUT_Error(1, "Unknown grouping type: %s", arg);
- return (1);
-}
-
-int
-VUT_Arg(int opt, const char *arg)
-{
- int i;
- char *p;
-
- switch (opt) {
- case 'd':
- /* Head */
- VUT.d_opt = 1;
- return (1);
- case 'D':
- /* Daemon mode */
- VUT.D_opt = 1;
- return (1);
- case 'g':
- /* Grouping */
- return (VUT_g_Arg(arg));
- case 'k':
- /* Log transaction limit */
- VUT.k_arg = (int)strtol(arg, &p, 10);
- if (*p != '\0' || VUT.k_arg <= 0)
- VUT_Error(1, "-k: Invalid number '%s'", arg);
- return (1);
- case 'n':
- /* Varnish instance name */
- REPLACE(VUT.n_arg, arg);
- return (1);
- case 'N':
- /* Varnish stale VSM file */
- REPLACE(VUT.N_arg, arg);
- return (1);
- case 'P':
- /* PID file */
- REPLACE(VUT.P_arg, arg);
- return (1);
- case 'q':
- /* Query to use */
- REPLACE(VUT.q_arg, arg);
- return (1);
- case 'r':
- /* Binary file input */
- REPLACE(VUT.r_arg, arg);
- return (1);
- case 't':
- /* VSM connect timeout */
- if (!strcasecmp("off", arg))
- VUT.t_arg = -1.;
- else {
- VUT.t_arg = VNUM(arg);
- if (isnan(VUT.t_arg))
- VUT_Error(1, "-t: Syntax error");
- if (VUT.t_arg < 0.)
- VUT_Error(1, "-t: Range error");
- }
- return (1);
- case 'V':
- /* Print version number and exit */
- VCS_Message(VUT.progname);
- exit(0);
- default:
- AN(VUT.vsl);
- i = VSL_Arg(VUT.vsl, opt, arg);
- if (i < 0)
- VUT_Error(1, "%s", VSL_Error(VUT.vsl));
- return (i);
- }
-}
-
-void
-VUT_Init(const char *progname, int argc, char * const *argv,
- const struct vopt_spec *voc)
-{
-
- if (argc == 2 && !strcmp(argv[1], "--synopsis"))
- exit(vut_synopsis(voc));
- if (argc == 2 && !strcmp(argv[1], "--options"))
- exit(vut_options(voc));
-
- VUT.progname = progname;
- REPLACE(VUT.name, "");
- VUT.g_arg = VSL_g_vxid;
- AZ(VUT.vsl);
- VUT.vsl = VSL_New();
- AN(VUT.vsl);
- VUT.k_arg = -1;
- VUT.t_arg = 5.;
-}
-
-void
-VUT_Setup(void)
-{
- struct VSL_cursor *c;
- double t_start;
- int i;
-
- AN(VUT.vsl);
- AZ(VUT.vsm);
- AZ(VUT.vslq);
-
- /* Check input arguments */
- if ((VUT.n_arg == NULL ? 0 : 1) +
- (VUT.N_arg == NULL ? 0 : 1) +
- (VUT.r_arg == NULL ? 0 : 1) > 1)
- VUT_Error(1, "Only one of -n, -N and -r options may be used");
-
- /* Create and validate the query expression */
- VUT.vslq = VSLQ_New(VUT.vsl, NULL, VUT.g_arg, VUT.q_arg);
- if (VUT.vslq == NULL)
- VUT_Error(1, "Query expression error:\n%s", VSL_Error(VUT.vsl));
-
- /* Setup input */
- if (VUT.r_arg) {
- REPLACE(VUT.name, VUT.r_arg);
- c = VSL_CursorFile(VUT.vsl, VUT.r_arg, 0);
- if (c == NULL)
- VUT_Error(1, "Can't open log file (%s)",
- VSL_Error(VUT.vsl));
- } else {
- VUT.vsm = VSM_New();
- AN(VUT.vsm);
- if (VUT.n_arg && VSM_n_Arg(VUT.vsm, VUT.n_arg) <= 0)
- VUT_Error(1, "%s", VSM_Error(VUT.vsm));
- if (VUT.N_arg && VSM_N_Arg(VUT.vsm, VUT.N_arg) <= 0)
- VUT_Error(1, "%s", VSM_Error(VUT.vsm));
- REPLACE(VUT.name, VSM_Name(VUT.vsm));
- t_start = NAN;
- c = NULL;
- while (1) {
- i = VSM_Open(VUT.vsm);
- if (!i)
- c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
- (VUT.d_opt ? VSL_COPT_TAILSTOP :
- VSL_COPT_TAIL)
- | VSL_COPT_BATCH);
- if (c)
- break;
-
- if (isnan(t_start) && VUT.t_arg > 0.) {
- VUT_Error(0, "Can't open log -"
- " retrying for %.0f seconds", VUT.t_arg);
- t_start = VTIM_real();
- }
- VSM_Close(VUT.vsm);
- if (VUT.t_arg <= 0.)
- break;
- if (VTIM_real() - t_start > VUT.t_arg)
- break;
-
- VSM_ResetError(VUT.vsm);
- VSL_ResetError(VUT.vsl);
- VTIM_sleep(0.5);
- }
-
- if (VUT.t_arg >= 0. && (i || !c)) {
- if (i)
- VUT_Error(1, "Can't open VSM file (%s)",
- VSM_Error(VUT.vsm));
- else
- VUT_Error(1, "Can't open log (%s)",
- VSL_Error(VUT.vsl));
- } else if (!isnan(t_start))
- VUT_Error(0, "Log opened");
- }
-
- if (c)
- VSLQ_SetCursor(VUT.vslq, &c);
- AZ(c);
-
- /* Signal handlers */
- (void)signal(SIGHUP, vut_sighup);
- (void)signal(SIGINT, vut_sigint);
- (void)signal(SIGTERM, vut_sigint);
- (void)signal(SIGUSR1, vut_sigusr1);
-
- /* Open PID file */
- if (VUT.P_arg) {
- AZ(VUT.pfh);
- VUT.pfh = VPF_Open(VUT.P_arg, 0644, NULL);
- if (VUT.pfh == NULL)
- VUT_Error(1, "%s: %s", VUT.P_arg, strerror(errno));
- }
-
- /* Daemon mode */
- if (VUT.D_opt && varnish_daemon(0, 0) == -1)
- VUT_Error(1, "Daemon mode: %s", strerror(errno));
-
- /* Write PID and setup exit handler */
- if (VUT.pfh != NULL) {
- VPF_Write(VUT.pfh);
- AZ(atexit(vut_vpf_remove));
- }
-}
-
-void
-VUT_Fini(void)
-{
- free(VUT.n_arg);
- free(VUT.N_arg);
- free(VUT.r_arg);
- free(VUT.P_arg);
- free(VUT.name);
-
- vut_vpf_remove();
- AZ(VUT.pfh);
-
- if (VUT.vslq)
- VSLQ_Delete(&VUT.vslq);
- if (VUT.vsl)
- VSL_Delete(VUT.vsl);
- if (VUT.vsm)
- VSM_Delete(VUT.vsm);
-
- memset(&VUT, 0, sizeof VUT);
-}
-
-int
-VUT_Main(void)
-{
- struct VSL_cursor *c;
- int i = -1;
-
- AN(VUT.vslq);
-
- while (!VUT.sigint) {
- if (VUT.sighup && VUT.sighup_f) {
- /* sighup callback */
- VUT.sighup = 0;
- i = (VUT.sighup_f)();
- if (i)
- break;
- }
-
- if (VUT.sigusr1) {
- /* Flush and report any incomplete records */
- VUT.sigusr1 = 0;
- VSLQ_Flush(VUT.vslq, vut_dispatch, NULL);
- }
-
- if (VUT.vsm != NULL && !VSM_IsOpen(VUT.vsm)) {
- /* Reconnect VSM */
- AZ(VUT.r_arg);
- VTIM_sleep(0.1);
- if (VSM_Open(VUT.vsm)) {
- VSM_ResetError(VUT.vsm);
- continue;
- }
- c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
- (VUT.d_opt ? VSL_COPT_TAILSTOP : VSL_COPT_TAIL)
- | VSL_COPT_BATCH);
- if (c == NULL) {
- VSL_ResetError(VUT.vsl);
- VSM_Close(VUT.vsm);
- continue;
- }
- VSLQ_SetCursor(VUT.vslq, &c);
- AZ(c);
- VUT_Error(0, "Log reacquired");
- }
-
- i = VSLQ_Dispatch(VUT.vslq, vut_dispatch, NULL);
- if (i == 1)
- /* Call again */
- continue;
- else if (i == 0) {
- /* Nothing to do but wait */
- if (VUT.idle_f) {
- i = (VUT.idle_f)();
- if (i)
- break;
- }
- VTIM_sleep(0.01);
- continue;
- } else if (i == -1) {
- /* EOF */
- break;
- }
-
- if (VUT.vsm == NULL)
- break;
-
- /* XXX: Make continuation optional */
-
- VSLQ_Flush(VUT.vslq, vut_dispatch, NULL);
-
- if (i == -2)
- /* Abandoned */
- VUT_Error(0, "Log abandoned");
- else if (i < -2)
- /* Overrun */
- VUT_Error(0, "Log overrun");
-
- VSM_Close(VUT.vsm);
- }
-
- return (i);
-}
-
-/**********************************************************************/
-
-
-static void
-print_nobrackets(const char *s)
-{
- const char *e;
-
- /* Remove whitespace */
- while (isspace(*s))
- s++;
- e = s + strlen(s);
- while (e > s && isspace(e[-1]))
- e--;
-
- /* Remove outer layer brackets if present */
- if (e > s && *s == '[' && e[-1] == ']') {
- s++;
- e--;
- }
-
- printf("%.*s", (int)(e - s), s);
-}
-
-static void
-print_tabbed(const char *string, int tabs)
-{
- int i;
- const char *c;
-
- for (c = string; *c; c++) {
- if (c == string || *(c - 1) == '\n')
- for (i = 0; i < tabs; i++)
- printf("\t");
- printf("%c", *c);
- }
-}
-
-static void
-print_opt(const struct vopt_list *opt)
-{
- print_nobrackets(opt->synopsis);
- printf("\n\n");
- print_tabbed(opt->ldesc, 1);
- printf("\n\n");
-}
-
-static int
-vut_synopsis(const struct vopt_spec *voc)
-{
- printf(".. |synopsis| replace:: %s\n", voc->vopt_synopsis);
- return (0);
-}
-
-static int
-vut_options(const struct vopt_spec *voc)
-{
- int i;
-
- for (i = 0; i < voc->vopt_list_n; i++)
- print_opt(&voc->vopt_list[i]);
- return (0);
-}
More information about the varnish-commit
mailing list