#include <time.h>
#include <sys/time.h>
#include <signal.h>
+#include <fcntl.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
static FILE *out;
static int flush = 1;
static char *format;
-static time_t now;
+static struct timeval now;
static volatile int reopen = 0;
static void qputs(char *s, FILE *o)
{
char *h, *p;
char buf[1024];
- struct timeval tv;
switch(o) {
case '%':
case 't':
if(!*d)
d = "%a, %d %b %Y %H:%M:%S %z";
- strftime(buf, sizeof(buf), d, localtime(&now));
+ strftime(buf, sizeof(buf), d, localtime(&now.tv_sec));
qputs(buf, out);
break;
case 'T':
if(!*d)
d = "%a, %d %b %Y %H:%M:%S %z";
- strftime(buf, sizeof(buf), d, gmtime(&now));
+ strftime(buf, sizeof(buf), d, gmtime(&now.tv_sec));
qputs(buf, out);
break;
case 's':
- gettimeofday(&tv, NULL);
- fprintf(out, "%06i", (int)tv.tv_usec);
+ fprintf(out, "%06i", (int)now.tv_usec);
break;
case 'A':
logitem(req, 'h', "X-Ash-Address");
static void serve(struct hthead *req, int fd)
{
- now = time(NULL);
+ gettimeofday(&now, NULL);
if(sendreq(ch, req, fd)) {
flog(LOG_ERR, "accesslog: could not pass request to child: %s", strerror(errno));
exit(1);
static void usage(FILE *out)
{
- fprintf(out, "usage: accesslog [-hFa] [-f FORMAT] OUTFILE CHILD [ARGS...]\n");
+ fprintf(out, "usage: accesslog [-hFa] [-f FORMAT] [-p PIDFILE] OUTFILE CHILD [ARGS...]\n");
}
int main(int argc, char **argv)
struct hthead *req;
int fd;
struct pollfd pfd[2];
+ char *pidfile;
+ FILE *pidout;
- optarg = NULL;
- while((c = getopt(argc, argv, "+hFaf:")) >= 0) {
+ pidfile = NULL;
+ while((c = getopt(argc, argv, "+hFaf:p:")) >= 0) {
switch(c) {
case 'h':
usage(stdout);
case 'f':
format = optarg;
break;
+ case 'p':
+ pidfile = optarg;
+ break;
case 'a':
format = "%A - - [%{%d/%b/%Y:%H:%M:%S %z}t] \"%m %u %v\" - - \"%R\" \"%G\"";
break;
}
}
if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
- flog(LOG_ERR, "accesslog: could fork child: %s", strerror(errno));
+ flog(LOG_ERR, "accesslog: could not fork child: %s", strerror(errno));
exit(1);
}
signal(SIGHUP, sighandler);
+ if(pidfile) {
+ if(!strcmp(pidfile, "-")) {
+ if(!outname) {
+ flog(LOG_ERR, "accesslog: cannot derive PID file name without an output file");
+ exit(1);
+ }
+ pidfile = sprintf2("%s.pid", outname);
+ }
+ if((pidout = fopen(pidfile, "w")) == NULL) {
+ flog(LOG_ERR, "accesslog: could not open PID file %s for writing: %s", pidfile);
+ exit(1);
+ }
+ fprintf(pidout, "%i\n", (int)getpid());
+ fclose(pidout);
+ }
while(1) {
if(reopen) {
reopenlog();
if(pfd[1].revents & POLLHUP)
break;
}
+ if(pidfile != NULL)
+ unlink(pidfile);
return(0);
}