Mailing List Archive

r1411 - trunk/varnish-cache/bin/varnishlog
Author: des
Date: 2007-05-11 13:34:42 +0200 (Fri, 11 May 2007)
New Revision: 1411

Modified:
trunk/varnish-cache/bin/varnishlog/varnishlog.1
trunk/varnish-cache/bin/varnishlog/varnishlog.c
Log:
Add -D (daemonize) and -P (pid file) options.


Modified: trunk/varnish-cache/bin/varnishlog/varnishlog.1
===================================================================
--- trunk/varnish-cache/bin/varnishlog/varnishlog.1 2007-05-11 11:17:09 UTC (rev 1410)
+++ trunk/varnish-cache/bin/varnishlog/varnishlog.1 2007-05-11 11:34:42 UTC (rev 1411)
@@ -28,7 +28,7 @@
.\"
.\" $Id$
.\"
-.Dd April 21, 2007
+.Dd May 11, 2007
.Dt VARNISHLOG 1
.Os
.Sh NAME
@@ -40,10 +40,12 @@
.Op Fl b
.Op Fl C
.Op Fl c
+.Op Fl D
.Op Fl d
.Op Fl I Ar regex
.Op Fl i Ar tag
.Op Fl o
+.Op Fl P Ar file
.Op Fl r Ar file
.Op Fl V
.Op Fl w Ar file
@@ -82,6 +84,8 @@
is specified,
.Nm
acts as if they both were.
+.It Fl D
+Daemonize.
.It Fl d
Process old log entries on startup.
Normally,
@@ -107,6 +111,9 @@
This has no effect when writing to a file using the
.Fl w
option.
+.It Fl P Ar file
+Write the process's PID to the specified
+.Ar file .
.It Fl r Ar file
Read log entries from
.Ar file

Modified: trunk/varnish-cache/bin/varnishlog/varnishlog.c
===================================================================
--- trunk/varnish-cache/bin/varnishlog/varnishlog.c 2007-05-11 11:17:09 UTC (rev 1410)
+++ trunk/varnish-cache/bin/varnishlog/varnishlog.c 2007-05-11 11:34:42 UTC (rev 1411)
@@ -40,15 +40,24 @@
#include <string.h>
#include <unistd.h>

+#ifndef HAVE_DAEMON
+#include "compat/daemon.h"
+#endif
+
+#ifdef HAVE_VIS_H
+#include <vis.h>
+#else
#include "compat/vis.h"
+#endif

#include "vsb.h"
+#include "vpf.h"

#include "libvarnish.h"
#include "shmlog.h"
#include "varnishapi.h"

-static int bflag, cflag;
+static int b_flag, c_flag;

/* -------------------------------------------------------------------*/

@@ -100,7 +109,7 @@
(void)priv;

if (!(spec & (VSL_S_CLIENT|VSL_S_BACKEND))) {
- if (!bflag && !cflag)
+ if (!b_flag && !c_flag)
VSL_H_Print(stdout, tag, fd, len, spec, ptr);
return (0);
}
@@ -179,12 +188,12 @@
exit (2);
}
}
- if (!bflag) {
+ if (!b_flag) {
VSL_Select(vd, SLT_SessionOpen);
VSL_Select(vd, SLT_SessionClose);
VSL_Select(vd, SLT_ReqEnd);
}
- if (!cflag) {
+ if (!c_flag) {
VSL_Select(vd, SLT_BackendOpen);
VSL_Select(vd, SLT_BackendClose);
VSL_Select(vd, SLT_BackendReuse);
@@ -264,7 +273,7 @@
usage(void)
{
fprintf(stderr,
- "usage: varnishlog %s [-aoV] [-w file]\n", VSL_USAGE);
+ "usage: varnishlog %s [-aDoV] [-P file] [-w file]\n", VSL_USAGE);
exit(1);
}

@@ -272,20 +281,28 @@
main(int argc, char **argv)
{
int i, c;
- int a_flag = 0, o_flag = 0;
- char *w_opt = NULL;
+ int a_flag = 0, D_flag = 0, o_flag = 0;
+ const char *P_opt = NULL;
+ const char *w_opt = NULL;
+ struct pidfh *pfh = NULL;
struct VSL_data *vd;

vd = VSL_New();

- while ((c = getopt(argc, argv, VSL_ARGS "aoVw:")) != -1) {
+ while ((c = getopt(argc, argv, VSL_ARGS "aDoP:Vw:")) != -1) {
switch (c) {
case 'a':
a_flag = 1;
break;
+ case 'D':
+ D_flag = 1;
+ break;
case 'o':
o_flag = 1;
break;
+ case 'P':
+ P_opt = optarg;
+ break;
case 'V':
varnish_version("varnishlog");
exit(0);
@@ -293,12 +310,12 @@
w_opt = optarg;
break;
case 'c':
- cflag = 1;
+ c_flag = 1;
if (VSL_Arg(vd, c, optarg) > 0)
break;
usage();
case 'b':
- bflag = 1;
+ b_flag = 1;
if (VSL_Arg(vd, c, optarg) > 0)
break;
usage();
@@ -313,8 +330,23 @@
usage();

if (VSL_OpenLog(vd))
- exit (1);
+ exit(1);

+ if (P_opt && (pfh = vpf_open(P_opt, 0600, NULL)) == NULL) {
+ perror(P_opt);
+ exit(1);
+ }
+
+ if (D_flag && daemon(0, 0) == -1) {
+ perror("daemon()");
+ if (pfh != NULL)
+ vpf_remove(pfh);
+ exit(1);
+ }
+
+ if (pfh != NULL)
+ vpf_write(pfh);
+
if (w_opt != NULL)
do_write(vd, w_opt, a_flag);

@@ -329,5 +361,7 @@
break;
}

+ if (pfh != NULL)
+ vpf_remove(pfh);
return (0);
}