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;
44 static struct timeval now;
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)
76 if((h = getheader(req, d)) == NULL) {
86 strcpy(buf, req->url);
87 if((p = strchr(buf, '?')) != NULL)
92 qputs(req->method, out);
95 qputs(req->rest, out);
102 d = "%a, %d %b %Y %H:%M:%S %z";
103 strftime(buf, sizeof(buf), d, localtime(&now.tv_sec));
108 d = "%a, %d %b %Y %H:%M:%S %z";
109 strftime(buf, sizeof(buf), d, gmtime(&now.tv_sec));
113 fprintf(out, "%06i", (int)now.tv_usec);
116 logitem(req, 'h', "X-Ash-Address");
119 logitem(req, 'h', "Host");
122 logitem(req, 'h', "Referer");
125 logitem(req, 'h', "User-Agent");
130 static void logreq(struct hthead *req)
133 char d[strlen(format)];
142 if((p2 = strchr(p, '}')) == NULL)
144 memcpy(d, p, p2 - p);
163 static void serve(struct hthead *req, int fd)
165 gettimeofday(&now, NULL);
166 if(sendreq(ch, req, fd)) {
167 flog(LOG_ERR, "accesslog: could not pass request to child: %s", strerror(errno));
173 static void sighandler(int sig)
179 static void reopenlog(void)
183 if(outname == NULL) {
184 flog(LOG_WARNING, "accesslog: received SIGHUP but logging to stdout, so ignoring");
187 if((new = fopen(outname, "a")) == NULL) {
188 flog(LOG_WARNING, "accesslog: could not reopen log file `%s' on SIGHUP: %s", outname, strerror(errno));
195 static void usage(FILE *out)
197 fprintf(out, "usage: accesslog [-hFa] [-f FORMAT] OUTFILE CHILD [ARGS...]\n");
200 int main(int argc, char **argv)
205 struct pollfd pfd[2];
207 while((c = getopt(argc, argv, "+hFaf:")) >= 0) {
219 format = "%A - - [%{%d/%b/%Y:%H:%M:%S %z}t] \"%m %u %v\" - - \"%R\" \"%G\"";
226 if(argc - optind < 2) {
232 if(!strcmp(argv[optind], "-"))
235 outname = argv[optind];
236 if(outname == NULL) {
239 if((out = fopen(argv[optind], "a")) == NULL) {
240 flog(LOG_ERR, "accesslog: could not open %s for logging: %s", argv[optind], strerror(errno));
244 if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
245 flog(LOG_ERR, "accesslog: could not fork child: %s", strerror(errno));
248 signal(SIGHUP, sighandler);
254 memset(pfd, 0, sizeof(pfd));
256 pfd[0].events = POLLIN;
258 pfd[1].events = POLLHUP;
259 if((ret = poll(pfd, 2, -1)) < 0) {
261 flog(LOG_ERR, "accesslog: error in poll: %s", strerror(errno));
266 if((fd = recvreq(0, &req)) < 0) {
269 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
276 if(pfd[1].revents & POLLHUP)