Hi all,

This patch allows varnishncsa to obtain the logformat string from a file
instead of a command line argument. The main usecase is for sysv init
scripts where shell expansion causes issues like
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=657449 .
Varnishncsa will read a single line from the specified af the "-f"
option, use that as the logging format and close the file.
I don't really like using -f as an option, as that has a different
meaning in varnishncsa 3.0.x (X-Forwarded-For vs client.ip), but I
couldn't come up with a better alternative.

Comments?

BR 
Kristian Sørensen
From b5e70150dda9424de58dd3e6d942e6c051aab972 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kristian=20Gr=C3=B8nfeldt=20S=C3=B8rensen?=
 <[email protected]>
Date: Mon, 15 Dec 2014 12:59:17 +0100
Subject: [PATCH] Allow varnishncsa logformat to be read from a file. Adds
 supports to varnishncsa for specifying the logging format by reading the log
 format from a file using the option "-f". Since -f means prefer
 X-Forwarded-For over client.ip in Varnish 3, maybe we should use another
 option letter.

---
 bin/varnishncsa/varnishncsa.c         | 25 +++++++++++++++++++++++++
 bin/varnishncsa/varnishncsa_options.h |  6 ++++++
 2 files changed, 31 insertions(+)

diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c
index 162829d..16450ec 100644
--- a/bin/varnishncsa/varnishncsa.c
+++ b/bin/varnishncsa/varnishncsa.c
@@ -880,6 +880,25 @@ dispatch_f(struct VSL_data *vsl, struct VSL_transaction * const pt[],
 	return (0);
 }
 
+static char *read_format(const char *formatfile){
+        FILE *fmtfile;
+        size_t len = 0;
+        char *fmt = 0;
+
+        fmtfile = fopen(formatfile,"r");
+        if (fmtfile == NULL)
+                VUT_Error(1, "Can't open format file (%s)", strerror(errno));
+        if (getline(&fmt, &len, fmtfile) == -1) {
+                free(fmt);
+                if (feof(fmtfile))
+                        VUT_Error(1, "Empty format file");
+                else
+                        VUT_Error(1, "Can't read format from file (%s)", strerror(errno));
+        }
+        fclose(fmtfile);
+        return fmt;
+}
+
 int
 main(int argc, char * const *argv)
 {
@@ -909,6 +928,12 @@ main(int argc, char * const *argv)
 			format = strdup(optarg);
 			AN(format);
 			break;
+                case 'f':
+                        if (format !=NULL)
+                                free(format);
+                        format = read_format(optarg);
+                        AN(format);
+                        break;
 		case 'h':
 			/* Usage help */
 			usage(0);
diff --git a/bin/varnishncsa/varnishncsa_options.h b/bin/varnishncsa/varnishncsa_options.h
index dc8ea30..dd56199 100644
--- a/bin/varnishncsa/varnishncsa_options.h
+++ b/bin/varnishncsa/varnishncsa_options.h
@@ -40,6 +40,11 @@
 	    "Set the output log format string."				\
 	)
 
+#define NCSA_OPT_f                                                      \
+        VOPT("f:", "[-f formatfile]", "Read output format from file",   \
+             "Read output format from a file. Will read a single line   \
+             from the specified file, and use that line as the format")
+
 #define NCSA_OPT_g							\
 	VOPT("g:", "[-g <request|vxid>]", "Grouping mode (default: vxid)",		\
 	    "The grouping of the log records. The default is to group"	\
@@ -59,6 +64,7 @@ VSL_OPT_C
 VUT_OPT_d
 VUT_OPT_D
 NCSA_OPT_F
+NCSA_OPT_f
 NCSA_OPT_g
 VUT_OPT_h
 VUT_OPT_n
-- 
2.1.3

_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to