X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Faccesslog.c;h=ac324d5fcfe7d4d8578235c23881e5ab319a8f18;hb=cbc5f085ef03ebab38b9501acc3ec7aeca10e7d3;hp=49614831d4aa724bba3667688881228f562a4e4e;hpb=048ac11565aca7457873f5ec299101ae0e794e16;p=ashd.git diff --git a/src/accesslog.c b/src/accesslog.c index 4961483..ac324d5 100644 --- a/src/accesslog.c +++ b/src/accesslog.c @@ -24,6 +24,7 @@ #include #include #include +#include #ifdef HAVE_CONFIG_H #include @@ -36,10 +37,12 @@ #define DEFFORMAT "%{%Y-%m-%d %H:%M:%S}t %m %u %A \"%G\"" static int ch; +static char *outname = NULL; static FILE *out; static int flush = 1; static char *format; static time_t now; +static volatile int reopen = 0; static void qputs(char *s, FILE *o) { @@ -169,9 +172,31 @@ static void serve(struct hthead *req, int fd) logreq(req); } +static void sighandler(int sig) +{ + if(sig == SIGHUP) + reopen = 1; +} + +static void reopenlog(void) +{ + FILE *new; + + if(outname == NULL) { + flog(LOG_WARNING, "accesslog: received SIGHUP but logging to stdout, so ignoring"); + return; + } + if((new = fopen(outname, "a")) == NULL) { + flog(LOG_WARNING, "accesslog: could not reopen log file `%s' on SIGHUP: %s", outname, strerror(errno)); + return; + } + fclose(out); + out = new; +} + static void usage(FILE *out) { - fprintf(out, "usage: accesslog [-hFa] [-f FORMAT] [-o OUTFILE] CHILD [ARGS...]\n"); + fprintf(out, "usage: accesslog [-hFa] [-f FORMAT] OUTFILE CHILD [ARGS...]\n"); } int main(int argc, char **argv) @@ -180,17 +205,13 @@ int main(int argc, char **argv) struct hthead *req; int fd; struct pollfd pfd[2]; - char *outfile; optarg = NULL; - while((c = getopt(argc, argv, "+hFaf:o:")) >= 0) { + while((c = getopt(argc, argv, "+hFaf:")) >= 0) { switch(c) { case 'h': usage(stdout); exit(0); - case 'o': - outfile = optarg; - break; case 'F': flush = 0; break; @@ -205,25 +226,34 @@ int main(int argc, char **argv) exit(1); } } - if(optind >= argc) { + if(argc - optind < 2) { usage(stderr); exit(1); } if(format == NULL) format = DEFFORMAT; - if(outfile != NULL) { - if((out = fopen(outfile, "a")) == NULL) { - flog(LOG_ERR, "accesslog: could not open %s for logging: %s", outfile, strerror(errno)); + if(!strcmp(argv[optind], "-")) + outname = NULL; + else + outname = argv[optind]; + if(outname == NULL) { + out = stdout; + } else { + if((out = fopen(argv[optind], "a")) == NULL) { + flog(LOG_ERR, "accesslog: could not open %s for logging: %s", argv[optind], strerror(errno)); exit(1); } - } else { - out = stdout; } - if((ch = stdmkchild(argv + optind, NULL, NULL)) < 0) { + if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) { flog(LOG_ERR, "accesslog: could fork child: %s", strerror(errno)); exit(1); } + signal(SIGHUP, sighandler); while(1) { + if(reopen) { + reopenlog(); + reopen = 0; + } memset(pfd, 0, sizeof(pfd)); pfd[0].fd = 0; pfd[0].events = POLLIN;