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/>.
37 #define DEFFORMAT "%{%Y-%m-%d %H:%M:%S}t %m %u %A \"%G\""
40 static char *outname = NULL;
45 static volatile int reopen = 0;
47 static void qputs(char *s, FILE *o)
52 } else if(*s == '\\') {
54 } else if(*s == '\n') {
56 } else if(*s == '\t') {
58 } else if((*s < 32) || (*s >= 128)) {
59 fprintf(o, "\\x%02x", *s);
66 static void logitem(struct hthead *req, char o, char *d)
77 if((h = getheader(req, d)) == NULL) {
87 strcpy(buf, req->url);
88 if((p = strchr(buf, '?')) != NULL)
93 qputs(req->method, out);
96 qputs(req->rest, out);
103 d = "%a, %d %b %Y %H:%M:%S %z";
104 strftime(buf, sizeof(buf), d, localtime(&now));
109 d = "%a, %d %b %Y %H:%M:%S %z";
110 strftime(buf, sizeof(buf), d, gmtime(&now));
114 gettimeofday(&tv, NULL);
115 fprintf(out, "%06i", (int)tv.tv_usec);
118 logitem(req, 'h', "X-Ash-Address");
121 logitem(req, 'h', "Host");
124 logitem(req, 'h', "Referer");
127 logitem(req, 'h', "User-Agent");
132 static void logreq(struct hthead *req)
135 char d[strlen(format)];
144 if((p2 = strchr(p, '}')) == NULL)
146 memcpy(d, p, p2 - p);
165 static void serve(struct hthead *req, int fd)
168 if(sendreq(ch, req, fd)) {
169 flog(LOG_ERR, "accesslog: could not pass request to child: %s", strerror(errno));
175 static void sighandler(int sig)
181 static void reopenlog(void)
185 if(outname == NULL) {
186 flog(LOG_WARNING, "accesslog: received SIGHUP but logging to stdout, so ignoring");
189 if((new = fopen(outname, "a")) == NULL) {
190 flog(LOG_WARNING, "accesslog: could not reopen log file `%s' on SIGHUP: %s", outname, strerror(errno));
197 static void usage(FILE *out)
199 fprintf(out, "usage: accesslog [-hFa] [-f FORMAT] OUTFILE CHILD [ARGS...]\n");
202 int main(int argc, char **argv)
207 struct pollfd pfd[2];
210 while((c = getopt(argc, argv, "+hFaf:")) >= 0) {
222 format = "%A - - [%{%d/%b/%Y:%H:%M:%S %z}t] \"%m %u %v\" - - \"%R\" \"%G\"";
229 if(argc - optind < 2) {
235 if(!strcmp(argv[optind], "-"))
238 outname = argv[optind];
239 if(outname == NULL) {
242 if((out = fopen(argv[optind], "a")) == NULL) {
243 flog(LOG_ERR, "accesslog: could not open %s for logging: %s", argv[optind], strerror(errno));
247 if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
248 flog(LOG_ERR, "accesslog: could fork child: %s", strerror(errno));
251 signal(SIGHUP, sighandler);
257 memset(pfd, 0, sizeof(pfd));
259 pfd[0].events = POLLIN;
261 pfd[1].events = POLLHUP;
262 if((ret = poll(pfd, 2, -1)) < 0) {
264 flog(LOG_ERR, "accesslog: error in poll: %s", strerror(errno));
269 if((fd = recvreq(0, &req)) < 0) {
272 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
279 if(pfd[1].revents & POLLHUP)