Commit d760dde4 authored by Dag Erling Smørgrav's avatar Dag Erling Smørgrav

Copy&paste -D (daemonize) from varnishlog.


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@2230 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 0ad1b190
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
.\" .\"
.\" $Id$ .\" $Id$
.\" .\"
.Dd June 15, 2007 .Dd November 8, 2007
.Dt VARNISHNCSA 1 .Dt VARNISHNCSA 1
.Os .Os
.Sh NAME .Sh NAME
...@@ -40,10 +40,12 @@ ...@@ -40,10 +40,12 @@
.Op Fl b .Op Fl b
.Op Fl C .Op Fl C
.Op Fl c .Op Fl c
.Op Fl D
.Op Fl d .Op Fl d
.Op Fl I Ar regex .Op Fl I Ar regex
.Op Fl i Ar tag .Op Fl i Ar tag
.Op Fl n Ar varnish_name .Op Fl n Ar varnish_name
.Op Fl P Ar file
.Op Fl r Ar file .Op Fl r Ar file
.Op Fl V .Op Fl V
.Op Fl w Ar file .Op Fl w Ar file
...@@ -82,6 +84,8 @@ nor ...@@ -82,6 +84,8 @@ nor
is specified, is specified,
.Nm .Nm
acts as if they both were. acts as if they both were.
.It Fl D
Daemonize.
.It Fl d .It Fl d
Process old log entries on startup. Process old log entries on startup.
Normally, Normally,
...@@ -109,6 +113,9 @@ instance to get logs from. ...@@ -109,6 +113,9 @@ instance to get logs from.
If If
.Fl n .Fl n
is not specified, the host name is used. is not specified, the host name is used.
.It Fl P Ar file
Write the process's PID to the specified
.Ar file .
.It Fl r Ar file .It Fl r Ar file
Read log entries from Read log entries from
.Ar file .Ar file
......
...@@ -69,10 +69,16 @@ ...@@ -69,10 +69,16 @@
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
#ifndef HAVE_DAEMON
#include "compat/daemon.h"
#endif
#include "vsb.h"
#include "vpf.h"
#include "libvarnish.h" #include "libvarnish.h"
#include "shmlog.h" #include "shmlog.h"
#include "varnishapi.h" #include "varnishapi.h"
#include "vsb.h"
static struct logline { static struct logline {
char *df_H; /* %H, Protocol version */ char *df_H; /* %H, Protocol version */
...@@ -434,7 +440,7 @@ static void ...@@ -434,7 +440,7 @@ static void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: varnishncsa %s [-aV] [-n varnish_name] [-w file]\n", VSL_ARGS); fprintf(stderr, "usage: varnishncsa %s [-aDV] [-n varnish_name] [-P file] [-w file]\n", VSL_ARGS);
exit(1); exit(1);
} }
...@@ -442,15 +448,17 @@ int ...@@ -442,15 +448,17 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, c; int i, c;
struct VSL_data *vd; int a_flag = 0, D_flag = 0;
const char *ofn = NULL;
const char *n_arg = NULL; const char *n_arg = NULL;
int append = 0; const char *P_arg = NULL;
const char *w_arg = NULL;
struct pidfh *pfh = NULL;
struct VSL_data *vd;
FILE *of; FILE *of;
vd = VSL_New(); vd = VSL_New();
while ((c = getopt(argc, argv, VSL_ARGS "an:Vw:")) != -1) { while ((c = getopt(argc, argv, VSL_ARGS "aDn:P:Vw:")) != -1) {
i = VSL_Arg(vd, c, optarg); i = VSL_Arg(vd, c, optarg);
if (i < 0) if (i < 0)
exit (1); exit (1);
...@@ -458,16 +466,22 @@ main(int argc, char *argv[]) ...@@ -458,16 +466,22 @@ main(int argc, char *argv[])
continue; continue;
switch (c) { switch (c) {
case 'a': case 'a':
append = 1; a_flag = 1;
break;
case 'D':
D_flag = 1;
break; break;
case 'n': case 'n':
n_arg = optarg; n_arg = optarg;
break; break;
case 'P':
P_arg = optarg;
break;
case 'V': case 'V':
varnish_version("varnishncsa"); varnish_version("varnishncsa");
exit(0); exit(0);
case 'w': case 'w':
ofn = optarg; w_arg = optarg;
break; break;
default: default:
if (VSL_Arg(vd, c, optarg) > 0) if (VSL_Arg(vd, c, optarg) > 0)
...@@ -479,22 +493,37 @@ main(int argc, char *argv[]) ...@@ -479,22 +493,37 @@ main(int argc, char *argv[])
if (VSL_OpenLog(vd, n_arg)) if (VSL_OpenLog(vd, n_arg))
exit(1); exit(1);
if (ofn) { if (P_arg && (pfh = vpf_open(P_arg, 0600, NULL)) == NULL) {
of = open_log(ofn, append); perror(P_arg);
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_arg) {
of = open_log(w_arg, a_flag);
signal(SIGHUP, sighup); signal(SIGHUP, sighup);
} else { } else {
ofn = "stdout"; w_arg = "stdout";
of = stdout; of = stdout;
} }
while (VSL_Dispatch(vd, h_ncsa, of) == 0) { while (VSL_Dispatch(vd, h_ncsa, of) == 0) {
if (fflush(of) != 0) { if (fflush(of) != 0) {
perror(ofn); perror(w_arg);
exit(1); exit(1);
} }
if (reopen && of != stdout) { if (reopen && of != stdout) {
fclose(of); fclose(of);
of = open_log(ofn, append); of = open_log(w_arg, a_flag);
reopen = 0; reopen = 0;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment