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))
266 shutdown(pfds[1], SHUT_WR);
267 if((resp = parseresponse(hd)) == NULL)
272 if(passdata(hd, cl, &data.bytesout))
274 gettimeofday(&data.end, NULL);
287 static void sighandler(int sig)
297 static int lockfile(FILE *file)
301 memset(&ld, 0, sizeof(ld));
303 ld.l_whence = SEEK_SET;
306 return(fcntl(fileno(file), F_SETLK, &ld));
309 static void fetchpid(char *filename)
314 if((fd = open(filename, O_WRONLY)) < 0) {
315 fprintf(stderr, "accesslog: %s: %s\n", filename, strerror(errno));
318 memset(&ld, 0, sizeof(ld));
320 ld.l_whence = SEEK_SET;
323 ret = fcntl(fd, F_GETLK, &ld);
326 fprintf(stderr, "accesslog: %s: %s\n", filename, strerror(errno));
329 if(ld.l_type == F_UNLCK) {
330 fprintf(stderr, "accesslog: %s: not locked\n", filename);
333 printf("%i\n", (int)ld.l_pid);
336 static void reopenlog(void)
339 struct stat olds, news;
341 if(outname == NULL) {
342 flog(LOG_WARNING, "accesslog: received SIGHUP but logging to stdout, so ignoring");
346 if(fstat(fileno(out), &olds)) {
347 flog(LOG_ERR, "accesslog: could not stat current logfile(?!): %s", strerror(errno));
350 if(!stat(outname, &news)) {
351 if((olds.st_dev == news.st_dev) && (olds.st_ino == news.st_ino)) {
353 * This needs to be ignored, because if the same logfile
354 * is opened and then closed, the lock is lost. To quote
355 * the Linux fcntl(2) manpage: "This is bad." No kidding.
357 * Technically, there is a race condition here when the
358 * file has been stat'ed but not yet opened, where the old
359 * log file, having been previously renamed, changes name
360 * back to the name accesslog knows and is thus reopened
361 * regardlessly, but I think that might fit under the
362 * idiom "pathological case". It should, at least, not be
363 * a security problem.
365 flog(LOG_INFO, "accesslog: received SIGHUP, but logfile has not changed, so ignoring");
370 if((new = fopen(outname, "a")) == NULL) {
371 flog(LOG_WARNING, "accesslog: could not reopen log file `%s' on SIGHUP: %s", outname, strerror(errno));
374 fcntl(fileno(new), F_SETFD, FD_CLOEXEC);
377 if((errno == EAGAIN) || (errno == EACCES)) {
378 flog(LOG_ERR, "accesslog: logfile is already locked; reverting to current log", strerror(errno));
382 flog(LOG_WARNING, "accesslog: could not lock logfile, so no lock will be held: %s", strerror(errno));
390 static void listenloop(struct muth *mt, va_list args)
397 block(lfd, EV_READ, 0);
398 if((fd = recvreq(lfd, &req)) < 0) {
400 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
403 mustart(filterreq, req, fd);
407 static void chwatch(struct muth *mt, va_list args)
411 block(cfd, EV_READ, 0);
415 static void floop(void)
417 mustart(listenloop, 0);
418 mustart(chwatch, ch);
431 static void sloop(void)
435 struct pollfd pfd[2];
442 memset(pfd, 0, sizeof(pfd));
444 pfd[0].events = POLLIN;
446 pfd[1].events = POLLHUP;
447 if((ret = poll(pfd, 2, -1)) < 0) {
449 flog(LOG_ERR, "accesslog: error in poll: %s", strerror(errno));
454 if((fd = recvreq(0, &req)) < 0) {
457 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
464 if(pfd[1].revents & POLLHUP)
469 static void usage(FILE *out)
471 fprintf(out, "usage: accesslog [-hFaL] [-f FORMAT] [-p PIDFILE] OUTFILE CHILD [ARGS...]\n");
472 fprintf(out, " accesslog -P LOGFILE\n");
475 int main(int argc, char **argv)
482 while((c = getopt(argc, argv, "+hFaeLf:p:P:")) >= 0) {
506 format = "%A - - [%{%d/%b/%Y:%H:%M:%S %z}t] \"%m %u %v\" %c %o \"%R\" \"%G\"";
513 if(argc - optind < 2) {
519 if(!strcmp(argv[optind], "-"))
522 outname = argv[optind];
523 if(outname == NULL) {
527 if((out = fopen(argv[optind], "a")) == NULL) {
528 flog(LOG_ERR, "accesslog: could not open %s for logging: %s", argv[optind], strerror(errno));
531 fcntl(fileno(out), F_SETFD, FD_CLOEXEC);
535 if((errno == EAGAIN) || (errno == EACCES)) {
536 flog(LOG_ERR, "accesslog: logfile is already locked", strerror(errno));
539 flog(LOG_WARNING, "accesslog: could not lock logfile: %s", strerror(errno));
543 if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
544 flog(LOG_ERR, "accesslog: could not fork child: %s", strerror(errno));
547 signal(SIGHUP, sighandler);
549 if(!strcmp(pidfile, "-")) {
551 flog(LOG_ERR, "accesslog: cannot derive PID file name without an output file");
554 pidfile = sprintf2("%s.pid", outname);
556 if((pidout = fopen(pidfile, "w")) == NULL) {
557 flog(LOG_ERR, "accesslog: could not open PID file %s for writing: %s", pidfile);
560 fprintf(pidout, "%i\n", (int)getpid());