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>
48 static int parsefile(struct cfstate *s, FILE *in);
49 static void stdmerge(struct child *old, struct child *new);
50 static int stdhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata);
51 static void stddestroy(struct child *ch);
53 static int doinclude(struct cfstate *s, char *spec)
58 char *fbk, *dir, *fspec;
66 fspec = sprintf3("%s/%s", dirname(dir), spec);
69 if(glob(fspec, 0, NULL, &globm))
71 for(i = 0; i < globm.gl_pathc; i++) {
72 if((inc = fopen(globm.gl_pathv[i], "r")) != NULL) {
73 s->file = globm.gl_pathv[i];
74 if(parsefile(s, inc)) {
90 static int parsefile(struct cfstate *s, FILE *in)
95 int ind, indst[80], indl;
102 if(fgets(line, sizeof(line), in) == NULL) {
108 for(p = line + strlen(line) - 1; p >= line; p--) {
115 for(ind = 0, p = line; *p; p++) {
118 } else if(*p == '\t') {
119 ind = ind - (ind % 8) + 8;
124 if(!eof && (!*p || (*p == '#')))
128 if(ind > indst[indl]) {
131 s->res = tokenize("start");
139 s->res = tokenize("end");
144 while(ind < indst[indl]) {
146 s->res = tokenize("end");
150 if(ind > indst[indl]) {
151 flog(LOG_WARNING, "%s:%i: unexpected indentation level", s->file, s->lno);
159 argc = calen(w = tokenize(line));
161 /* Shouldn't happen, but... */
167 if(!strcmp(w[0], "include")) {
168 for(i = 1; i < argc; i++) {
169 if(doinclude(s, w[i])) {
179 if(!strcmp(w[0], "start") ||
180 !strcmp(w[0], "end") ||
181 !strcmp(w[0], "eof")) {
182 flog(LOG_WARNING, "%s:%i: illegal directive: %s", s->file, s->lno, w[0]);
191 static void parsefn(struct muth *mt, va_list args)
193 vavar(struct cfstate *, s);
197 s->file = sstrdup(file);
201 s->res = tokenize("eof");
208 char **getcfline(struct cfstate *s)
213 s->argc = calen(s->argv = s->res);
218 struct cfstate *mkcfparser(FILE *in, char *name)
223 s->pf = mustart(parsefn, s, in, name);
227 void freecfparser(struct cfstate *s)
235 char *findstdconf(char *name)
237 char *home, *path, *p, *p2, *t;
239 if((home = getenv("HOME")) != NULL) {
240 if(!access(t = sprintf2("%s/.ashd/etc/%s", home, name), R_OK))
244 if((path = getenv("PATH")) != NULL) {
245 path = sstrdup(path);
246 for(p = strtok(path, ":"); p != NULL; p = strtok(NULL, ":")) {
247 if((p2 = strrchr(p, '/')) == NULL)
250 if(!access(t = sprintf2("%s/etc/%s", p, name), R_OK)) {
261 struct child *newchild(char *name, struct chandler *iface, void *pdata)
266 ch->name = sstrdup(name);
272 void freechild(struct child *ch)
274 if(ch->iface->destroy != NULL)
275 ch->iface->destroy(ch);
281 void mergechildren(struct child *dst, struct child *src)
283 struct child *ch1, *ch2;
285 for(ch1 = dst; ch1 != NULL; ch1 = ch1->next) {
286 for(ch2 = src; ch2 != NULL; ch2 = ch2->next) {
287 if(ch1->iface->merge && !strcmp(ch1->name, ch2->name)) {
288 ch1->iface->merge(ch1, ch2);
295 void skipcfblock(struct cfstate *s)
301 if(!strcmp(w[0], "end") || !strcmp(w[0], "eof"))
306 static struct chandler stdhandler = {
309 .destroy = stddestroy,
312 static int stdhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
314 struct stdchild *sd = ch->pdata;
317 void stdinit(void *data)
321 for(i = 0; sd->envp[i]; i += 2)
322 putenv(sprintf2("%s=%s", sd->envp[i], sd->envp[i + 1]));
327 if(sd->type == CH_SOCKET) {
329 sd->fd = stdmkchild(sd->argv, stdinit, idata);
330 if(sendreq2(sd->fd, req, fd, MSG_NOSIGNAL | MSG_DONTWAIT)) {
332 if((serr == EPIPE) || (serr == ECONNRESET)) {
333 /* Assume that the child has crashed and restart it. */
335 sd->fd = stdmkchild(sd->argv, stdinit, idata);
336 if(!sendreq2(sd->fd, req, fd, MSG_NOSIGNAL | MSG_DONTWAIT))
339 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(serr));
346 } else if(sd->type == CH_FORK) {
347 if(stdforkserve(sd->argv, req, fd, chinit, idata) < 0)
353 static void stdmerge(struct child *dst, struct child *src)
355 struct stdchild *od, *nd;
357 if(src->iface == &stdhandler) {
365 static void stddestroy(struct child *ch)
367 struct stdchild *d = ch->pdata;
378 struct child *parsechild(struct cfstate *s)
382 struct charvbuf envbuf;
387 if(!strcmp(s->argv[0], "child")) {
390 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
394 ch = newchild(s->argv[1], &stdhandler, omalloc(d));
396 } else if(!strcmp(s->argv[0], "fchild")) {
399 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
403 ch = newchild(s->argv[1], &stdhandler, omalloc(d));
413 if(!strcmp(s->argv[0], "exec")) {
415 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", s->file, s->lno);
418 d->argv = szmalloc(sizeof(*d->argv) * s->argc);
419 for(i = 0; i < s->argc - 1; i++)
420 d->argv[i] = sstrdup(s->argv[i + 1]);
421 } else if(!strcmp(s->argv[0], "env")) {
423 flog(LOG_WARNING, "%s:%i: too few parameters to `env'", s->file, s->lno);
426 bufadd(envbuf, sstrdup(s->argv[1]));
427 bufadd(envbuf, sstrdup(s->argv[2]));
428 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
431 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in child declaration", s->file, s->lno, s->argv[0]);
434 bufadd(envbuf, NULL);
436 if(d->argv == NULL) {
437 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", s->file, sl, ch->name);
444 int childhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
446 return(ch->iface->handle(ch, req, fd, chinit, idata));