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/>.
26 #include <sys/socket.h>
47 static int parsefile(struct cfstate *s, FILE *in);
48 static void stdmerge(struct child *old, struct child *new);
49 static int stdhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata);
50 static void stddestroy(struct child *ch);
52 static int doinclude(struct cfstate *s, char *spec)
57 char *fbk, *dir, *fspec;
65 fspec = sprintf3("%s/%s", dirname(dir), spec);
68 if(glob(fspec, 0, NULL, &globm))
70 for(i = 0; i < globm.gl_pathc; i++) {
71 if((inc = fopen(globm.gl_pathv[i], "r")) != NULL) {
72 s->file = globm.gl_pathv[i];
73 if(parsefile(s, inc)) {
89 static int parsefile(struct cfstate *s, FILE *in)
94 int ind, indst[80], indl;
101 if(fgets(line, sizeof(line), in) == NULL) {
107 for(p = line + strlen(line) - 1; p >= line; p--) {
114 for(ind = 0, p = line; *p; p++) {
117 } else if(*p == '\t') {
118 ind = ind - (ind % 8) + 8;
123 if(!eof && (!*p || (*p == '#')))
127 if(ind > indst[indl]) {
130 s->res = tokenize("start");
138 s->res = tokenize("end");
143 while(ind < indst[indl]) {
145 s->res = tokenize("end");
149 if(ind > indst[indl]) {
150 flog(LOG_WARNING, "%s:%i: unexpected indentation level", s->file, s->lno);
158 argc = calen(w = tokenize(line));
160 /* Shouldn't happen, but... */
166 if(!strcmp(w[0], "include")) {
167 for(i = 1; i < argc; i++) {
168 if(doinclude(s, w[i])) {
178 if(!strcmp(w[0], "start") ||
179 !strcmp(w[0], "end") ||
180 !strcmp(w[0], "eof")) {
181 flog(LOG_WARNING, "%s:%i: illegal directive: %s", s->file, s->lno, w[0]);
190 static void parsefn(struct muth *mt, va_list args)
192 vavar(struct cfstate *, s);
196 s->file = sstrdup(file);
200 s->res = tokenize("eof");
207 char **getcfline(struct cfstate *s)
212 s->argc = calen(s->argv = s->res);
217 struct cfstate *mkcfparser(FILE *in, char *name)
222 s->pf = mustart(parsefn, s, in, name);
226 void freecfparser(struct cfstate *s)
234 char *findstdconf(char *name)
236 char *path, *p, *p2, *t;
238 if((path = getenv("PATH")) == NULL)
240 path = sstrdup(path);
241 for(p = strtok(path, ":"); p != NULL; p = strtok(NULL, ":")) {
242 if((p2 = strrchr(p, '/')) == NULL)
245 if(!access(t = sprintf2("%s/etc/%s", p, name), R_OK)) {
255 struct child *newchild(char *name, struct chandler *iface, void *pdata)
260 ch->name = sstrdup(name);
266 void freechild(struct child *ch)
268 if(ch->iface->destroy != NULL)
269 ch->iface->destroy(ch);
275 void mergechildren(struct child *dst, struct child *src)
277 struct child *ch1, *ch2;
279 for(ch1 = dst; ch1 != NULL; ch1 = ch1->next) {
280 for(ch2 = src; ch2 != NULL; ch2 = ch2->next) {
281 if(ch1->iface->merge && !strcmp(ch1->name, ch2->name)) {
282 ch1->iface->merge(ch1, ch2);
289 void skipcfblock(struct cfstate *s)
295 if(!strcmp(w[0], "end") || !strcmp(w[0], "eof"))
300 static struct chandler stdhandler = {
303 .destroy = stddestroy,
306 static int stdhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
308 struct stdchild *i = ch->pdata;
311 if(i->type == CH_SOCKET) {
313 i->fd = stdmkchild(i->argv, chinit, idata);
314 if(sendreq2(i->fd, req, fd, MSG_NOSIGNAL | MSG_DONTWAIT)) {
316 if((serr == EPIPE) || (serr == ECONNRESET)) {
317 /* Assume that the child has crashed and restart it. */
319 i->fd = stdmkchild(i->argv, chinit, idata);
320 if(!sendreq2(i->fd, req, fd, MSG_NOSIGNAL | MSG_DONTWAIT))
323 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(serr));
330 } else if(i->type == CH_FORK) {
331 if(stdforkserve(i->argv, req, fd, chinit, idata) < 0)
337 static void stdmerge(struct child *dst, struct child *src)
339 struct stdchild *od, *nd;
341 if(src->iface == &stdhandler) {
349 static void stddestroy(struct child *ch)
351 struct stdchild *d = ch->pdata;
360 struct child *parsechild(struct cfstate *s)
368 if(!strcmp(s->argv[0], "child")) {
371 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
375 ch = newchild(s->argv[1], &stdhandler, omalloc(d));
377 } else if(!strcmp(s->argv[0], "fchild")) {
380 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
384 ch = newchild(s->argv[1], &stdhandler, omalloc(d));
393 if(!strcmp(s->argv[0], "exec")) {
395 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", s->file, s->lno);
398 d->argv = szmalloc(sizeof(*d->argv) * s->argc);
399 for(i = 0; i < s->argc - 1; i++)
400 d->argv[i] = sstrdup(s->argv[i + 1]);
401 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
404 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in child declaration", s->file, s->lno, s->argv[0]);
407 if(d->argv == NULL) {
408 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", s->file, sl, ch->name);
415 int childhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
417 return(ch->iface->handle(ch, req, fd, chinit, idata));