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>
44 #define DEFFORMAT "%{%Y-%m-%d %H:%M:%S}t %m %u %A \"%G\""
46 static struct logdata {
47 struct hthead *req, *resp;
48 struct timeval start, end;
49 off_t bytesin, bytesout;
55 static int ch, filter;
56 static char *outname = NULL;
58 static int flush = 1, locklog = 1;
60 static volatile int reopen = 0;
62 static void qputs(char *sp, FILE *o)
64 unsigned char *s = (unsigned char *)sp;
69 } else if(*s == '\\') {
71 } else if(*s == '\n') {
73 } else if(*s == '\t') {
75 } else if((*s < 32) || (*s >= 128)) {
76 fprintf(o, "\\x%02x", (int)*s);
83 static void logitem(struct logdata *data, char o, char *d)
93 if((h = getheader(data->req, d)) == NULL) {
100 qputs(data->req->url, out);
103 strncpy(buf, data->req->url, sizeof(buf));
104 buf[sizeof(buf) - 1] = 0;
105 if((p = strchr(buf, '?')) != NULL)
110 qputs(data->req->method, out);
113 qputs(data->req->rest, out);
116 qputs(data->req->ver, out);
120 d = "%a, %d %b %Y %H:%M:%S %z";
121 strftime(buf, sizeof(buf), d, localtime(&data->start.tv_sec));
126 d = "%a, %d %b %Y %H:%M:%S %z";
127 strftime(buf, sizeof(buf), d, gmtime(&data->start.tv_sec));
131 fprintf(out, "%06i", (int)data->start.tv_usec);
137 fprintf(out, "%i", data->resp->code);
140 if(data->bytesin < 0)
143 fprintf(out, "%ji", (intmax_t)data->bytesin);
146 if(data->bytesout < 0)
149 fprintf(out, "%ji", (intmax_t)data->bytesout);
152 if((data->end.tv_sec == 0) && (data->end.tv_usec == 0))
155 fprintf(out, "%.6f", (data->end.tv_sec - data->start.tv_sec) + ((data->end.tv_usec - data->start.tv_usec) / 1000000.0));
158 logitem(data, 'h', "X-Ash-Address");
161 logitem(data, 'h', "Host");
164 logitem(data, 'h', "Referer");
167 logitem(data, 'h', "User-Agent");
172 static void logreq(struct logdata *data)
175 char d[strlen(format)];
184 if((p2 = strchr(p, '}')) == NULL)
186 memcpy(d, p, p2 - p);
205 static void serve(struct hthead *req, int fd)
211 gettimeofday(&data.start, NULL);
212 if(sendreq(ch, req, fd)) {
213 flog(LOG_ERR, "accesslog: could not pass request to child: %s", strerror(errno));
219 static int passdata(struct bufio *in, struct bufio *out, off_t *passed)
226 if((read = biocopysome(out, in)) < 0)
229 if(biorspace(in) && (biofillsome(in) < 0))
237 static void filterreq(struct muth *mt, va_list args)
239 vavar(struct hthead *, req);
243 struct bufio *cl, *hd;
250 gettimeofday(&data.start, NULL);
251 cl = mtbioopen(fd, 1, 600, "r+", NULL);
252 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
254 hd = mtbioopen(pfds[1], 1, 600, "r+", NULL);
255 if(sendreq(ch, req, pfds[0])) {
261 if(passdata(cl, hd, &data.bytesin))
265 shutdown(pfds[1], SHUT_WR);
266 if((resp = parseresponseb(hd)) == NULL)
269 writerespb(cl, resp);
270 bioprintf(cl, "\r\n");
271 if(passdata(hd, cl, &data.bytesout))
273 gettimeofday(&data.end, NULL);
286 static void sighandler(int sig)
296 static int lockfile(FILE *file)
300 memset(&ld, 0, sizeof(ld));
302 ld.l_whence = SEEK_SET;
305 return(fcntl(fileno(file), F_SETLK, &ld));
308 static void fetchpid(char *filename)
313 if((fd = open(filename, O_WRONLY)) < 0) {
314 fprintf(stderr, "accesslog: %s: %s\n", filename, strerror(errno));
317 memset(&ld, 0, sizeof(ld));
319 ld.l_whence = SEEK_SET;
322 ret = fcntl(fd, F_GETLK, &ld);
325 fprintf(stderr, "accesslog: %s: %s\n", filename, strerror(errno));
328 if(ld.l_type == F_UNLCK) {
329 fprintf(stderr, "accesslog: %s: not locked\n", filename);
332 printf("%i\n", (int)ld.l_pid);
335 static void reopenlog(void)
338 struct stat olds, news;
340 if(outname == NULL) {
341 flog(LOG_WARNING, "accesslog: received SIGHUP but logging to stdout, so ignoring");
345 if(fstat(fileno(out), &olds)) {
346 flog(LOG_ERR, "accesslog: could not stat current logfile(?!): %s", strerror(errno));
349 if(!stat(outname, &news)) {
350 if((olds.st_dev == news.st_dev) && (olds.st_ino == news.st_ino)) {
352 * This needs to be ignored, because if the same logfile
353 * is opened and then closed, the lock is lost. To quote
354 * the Linux fcntl(2) manpage: "This is bad." No kidding.
356 * Technically, there is a race condition here when the
357 * file has been stat'ed but not yet opened, where the old
358 * log file, having been previously renamed, changes name
359 * back to the name accesslog knows and is thus reopened
360 * regardlessly, but I think that might fit under the
361 * idiom "pathological case". It should, at least, not be
362 * a security problem.
364 flog(LOG_INFO, "accesslog: received SIGHUP, but logfile has not changed, so ignoring");
369 if((new = fopen(outname, "a")) == NULL) {
370 flog(LOG_WARNING, "accesslog: could not reopen log file `%s' on SIGHUP: %s", outname, strerror(errno));
373 fcntl(fileno(new), F_SETFD, FD_CLOEXEC);
376 if((errno == EAGAIN) || (errno == EACCES)) {
377 flog(LOG_ERR, "accesslog: logfile is already locked; reverting to current log", strerror(errno));
381 flog(LOG_WARNING, "accesslog: could not lock logfile, so no lock will be held: %s", strerror(errno));
389 static void listenloop(struct muth *mt, va_list args)
396 block(lfd, EV_READ, 0);
397 if((fd = recvreq(lfd, &req)) < 0) {
399 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
402 mustart(filterreq, req, fd);
406 static void chwatch(struct muth *mt, va_list args)
410 block(cfd, EV_READ, 0);
414 static void floop(void)
416 mustart(listenloop, 0);
417 mustart(chwatch, ch);
430 static void sloop(void)
434 struct pollfd pfd[2];
441 memset(pfd, 0, sizeof(pfd));
443 pfd[0].events = POLLIN;
445 pfd[1].events = POLLHUP;
446 if((ret = poll(pfd, 2, -1)) < 0) {
448 flog(LOG_ERR, "accesslog: error in poll: %s", strerror(errno));
453 if((fd = recvreq(0, &req)) < 0) {
456 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
463 if(pfd[1].revents & POLLHUP)
468 static void usage(FILE *out)
470 fprintf(out, "usage: accesslog [-hFaL] [-f FORMAT] [-p PIDFILE] OUTFILE CHILD [ARGS...]\n");
471 fprintf(out, " accesslog -P LOGFILE\n");
474 int main(int argc, char **argv)
481 while((c = getopt(argc, argv, "+hFaeLf:p:P:")) >= 0) {
505 format = "%A - - [%{%d/%b/%Y:%H:%M:%S %z}t] \"%m %u %v\" %c %o \"%R\" \"%G\"";
512 if(argc - optind < 2) {
518 if(!strcmp(argv[optind], "-"))
521 outname = argv[optind];
522 if(outname == NULL) {
526 if((out = fopen(argv[optind], "a")) == NULL) {
527 flog(LOG_ERR, "accesslog: could not open %s for logging: %s", argv[optind], strerror(errno));
530 fcntl(fileno(out), F_SETFD, FD_CLOEXEC);
534 if((errno == EAGAIN) || (errno == EACCES)) {
535 flog(LOG_ERR, "accesslog: logfile is already locked", strerror(errno));
538 flog(LOG_WARNING, "accesslog: could not lock logfile: %s", strerror(errno));
542 if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
543 flog(LOG_ERR, "accesslog: could not fork child: %s", strerror(errno));
546 signal(SIGHUP, sighandler);
548 if(!strcmp(pidfile, "-")) {
550 flog(LOG_ERR, "accesslog: cannot derive PID file name without an output file");
553 pidfile = sprintf2("%s.pid", outname);
555 if((pidout = fopen(pidfile, "w")) == NULL) {
556 flog(LOG_ERR, "accesslog: could not open PID file %s for writing: %s", pidfile);
559 fprintf(pidout, "%i\n", (int)getpid());