2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/socket.h>
43 #define DEFFORMAT "%{%Y-%m-%d %H:%M:%S}t %m %u %A \"%G\""
45 static struct logdata {
46 struct hthead *req, *resp;
47 struct timeval start, end;
48 off_t bytesin, bytesout;
54 static int ch, filter;
55 static char *outname = NULL;
57 static int flush = 1, locklog = 1;
59 static volatile int reopen = 0;
61 static void qputs(char *sp, FILE *o)
63 unsigned char *s = (unsigned char *)sp;
68 } else if(*s == '\\') {
70 } else if(*s == '\n') {
72 } else if(*s == '\t') {
74 } else if((*s < 32) || (*s >= 128)) {
75 fprintf(o, "\\x%02x", (int)*s);
82 static void logitem(struct logdata *data, char o, char *d)
92 if((h = getheader(data->req, d)) == NULL) {
99 qputs(data->req->url, out);
102 strncpy(buf, data->req->url, sizeof(buf));
103 buf[sizeof(buf) - 1] = 0;
104 if((p = strchr(buf, '?')) != NULL)
109 qputs(data->req->method, out);
112 qputs(data->req->rest, out);
115 qputs(data->req->ver, out);
119 d = "%a, %d %b %Y %H:%M:%S %z";
120 strftime(buf, sizeof(buf), d, localtime(&data->start.tv_sec));
125 d = "%a, %d %b %Y %H:%M:%S %z";
126 strftime(buf, sizeof(buf), d, gmtime(&data->start.tv_sec));
130 fprintf(out, "%06i", (int)data->start.tv_usec);
136 fprintf(out, "%i", data->resp->code);
139 if(data->bytesin < 0)
142 fprintf(out, "%ji", (intmax_t)data->bytesin);
145 if(data->bytesout < 0)
148 fprintf(out, "%ji", (intmax_t)data->bytesout);
151 if((data->end.tv_sec == 0) && (data->end.tv_usec == 0))
154 fprintf(out, "%.6f", (data->end.tv_sec - data->start.tv_sec) + ((data->end.tv_usec - data->start.tv_usec) / 1000000.0));
157 logitem(data, 'h', "X-Ash-Address");
160 logitem(data, 'h', "Host");
163 logitem(data, 'h', "Referer");
166 logitem(data, 'h', "User-Agent");
171 static void logreq(struct logdata *data)
174 char d[strlen(format)];
183 if((p2 = strchr(p, '}')) == NULL)
185 memcpy(d, p, p2 - p);
204 static void serve(struct hthead *req, int fd)
210 gettimeofday(&data.start, NULL);
211 if(sendreq(ch, req, fd)) {
212 flog(LOG_ERR, "accesslog: could not pass request to child: %s", strerror(errno));
218 static int passdata(FILE *in, FILE *out, off_t *passed)
226 read = fread(buf, 1, sizeof(buf), in);
229 if(fwrite(buf, 1, read, out) != read)
238 static void filterreq(struct muth *mt, va_list args)
240 vavar(struct hthead *, req);
251 gettimeofday(&data.start, NULL);
252 cl = mtstdopen(fd, 1, 600, "r+");
253 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
255 hd = mtstdopen(pfds[1], 1, 600, "r+");
256 if(sendreq(ch, req, pfds[0])) {
262 if(passdata(cl, hd, &data.bytesin))
264 shutdown(pfds[1], SHUT_WR);
265 if((resp = parseresponse(hd)) == NULL)
270 if(passdata(hd, cl, &data.bytesout))
272 gettimeofday(&data.end, NULL);
285 static void sighandler(int sig)
295 static int lockfile(FILE *file)
299 memset(&ld, 0, sizeof(ld));
301 ld.l_whence = SEEK_SET;
304 return(fcntl(fileno(file), F_SETLK, &ld));
307 static void fetchpid(char *filename)
312 if((fd = open(filename, O_WRONLY)) < 0) {
313 fprintf(stderr, "accesslog: %s: %s\n", filename, strerror(errno));
316 memset(&ld, 0, sizeof(ld));
318 ld.l_whence = SEEK_SET;
321 ret = fcntl(fd, F_GETLK, &ld);
324 fprintf(stderr, "accesslog: %s: %s\n", filename, strerror(errno));
327 if(ld.l_type == F_UNLCK) {
328 fprintf(stderr, "accesslog: %s: not locked\n", filename);
331 printf("%i\n", (int)ld.l_pid);
334 static void reopenlog(void)
337 struct stat olds, news;
339 if(outname == NULL) {
340 flog(LOG_WARNING, "accesslog: received SIGHUP but logging to stdout, so ignoring");
344 if(fstat(fileno(out), &olds)) {
345 flog(LOG_ERR, "accesslog: could not stat current logfile(?!): %s", strerror(errno));
348 if(!stat(outname, &news)) {
349 if((olds.st_dev == news.st_dev) && (olds.st_ino == news.st_ino)) {
351 * This needs to be ignored, because if the same logfile
352 * is opened and then closed, the lock is lost. To quote
353 * the Linux fcntl(2) manpage: "This is bad." No kidding.
355 * Technically, there is a race condition here when the
356 * file has been stat'ed but not yet opened, where the old
357 * log file, having been previously renamed, changes name
358 * back to the name accesslog knows and is thus reopened
359 * regardlessly, but I think that might fit under the
360 * idiom "pathological case". It should, at least, not be
361 * a security problem.
363 flog(LOG_INFO, "accesslog: received SIGHUP, but logfile has not changed, so ignoring");
368 if((new = fopen(outname, "a")) == NULL) {
369 flog(LOG_WARNING, "accesslog: could not reopen log file `%s' on SIGHUP: %s", outname, strerror(errno));
372 fcntl(fileno(new), F_SETFD, FD_CLOEXEC);
375 if((errno == EAGAIN) || (errno == EACCES)) {
376 flog(LOG_ERR, "accesslog: logfile is already locked; reverting to current log", strerror(errno));
380 flog(LOG_WARNING, "accesslog: could not lock logfile, so no lock will be held: %s", strerror(errno));
388 static void listenloop(struct muth *mt, va_list args)
395 block(lfd, EV_READ, 0);
396 if((fd = recvreq(lfd, &req)) < 0) {
398 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
401 mustart(filterreq, req, fd);
405 static void chwatch(struct muth *mt, va_list args)
409 block(cfd, EV_READ, 0);
413 static void floop(void)
415 mustart(listenloop, 0);
416 mustart(chwatch, ch);
429 static void sloop(void)
433 struct pollfd pfd[2];
440 memset(pfd, 0, sizeof(pfd));
442 pfd[0].events = POLLIN;
444 pfd[1].events = POLLHUP;
445 if((ret = poll(pfd, 2, -1)) < 0) {
447 flog(LOG_ERR, "accesslog: error in poll: %s", strerror(errno));
452 if((fd = recvreq(0, &req)) < 0) {
455 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
462 if(pfd[1].revents & POLLHUP)
467 static void usage(FILE *out)
469 fprintf(out, "usage: accesslog [-hFaL] [-f FORMAT] [-p PIDFILE] OUTFILE CHILD [ARGS...]\n");
470 fprintf(out, " accesslog -P LOGFILE\n");
473 int main(int argc, char **argv)
480 while((c = getopt(argc, argv, "+hFaeLf:p:P:")) >= 0) {
504 format = "%A - - [%{%d/%b/%Y:%H:%M:%S %z}t] \"%m %u %v\" %c %o \"%R\" \"%G\"";
511 if(argc - optind < 2) {
517 if(!strcmp(argv[optind], "-"))
520 outname = argv[optind];
521 if(outname == NULL) {
525 if((out = fopen(argv[optind], "a")) == NULL) {
526 flog(LOG_ERR, "accesslog: could not open %s for logging: %s", argv[optind], strerror(errno));
529 fcntl(fileno(out), F_SETFD, FD_CLOEXEC);
533 if((errno == EAGAIN) || (errno == EACCES)) {
534 flog(LOG_ERR, "accesslog: logfile is already locked", strerror(errno));
537 flog(LOG_WARNING, "accesslog: could not lock logfile: %s", strerror(errno));
541 if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
542 flog(LOG_ERR, "accesslog: could not fork child: %s", strerror(errno));
545 signal(SIGHUP, sighandler);
547 if(!strcmp(pidfile, "-")) {
549 flog(LOG_ERR, "accesslog: cannot derive PID file name without an output file");
552 pidfile = sprintf2("%s.pid", outname);
554 if((pidout = fopen(pidfile, "w")) == NULL) {
555 flog(LOG_ERR, "accesslog: could not open PID file %s for writing: %s", pidfile);
558 fprintf(pidout, "%i\n", (int)getpid());