X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fhtparser.c;h=a20b32906ab25d0a98926c1515660999a47959b2;hb=43066106084a7cbf5e3f4c08b0f84e1022c02d99;hp=e6f545a6f87567a5bb7ae4f23368b7bd6c6f6ad6;hpb=328d1dbcd801cd602fb245642bede234a7726c0c;p=ashd.git diff --git a/src/htparser.c b/src/htparser.c index e6f545a..a20b329 100644 --- a/src/htparser.c +++ b/src/htparser.c @@ -41,6 +41,7 @@ static int plex; static char *pidfile = NULL; static int daemonize, usesyslog; +struct mtbuf listeners; static void trimx(struct hthead *req) { @@ -279,27 +280,48 @@ static int http10keep(struct hthead *req, struct hthead *resp) } } +static char *connid(void) +{ + static struct charbuf cur; + int i; + char *ret; + + for(i = 0; i < cur.d; i++) { + if((++cur.b[i]) > 'Z') + cur.b[i] = 'A'; + else + goto done; + } + bufadd(cur, 'A'); +done: + ret = memcpy(smalloc(cur.d + 1), cur.b, cur.d); + ret[cur.d] = 0; + return(ret); +} + void serve(FILE *in, struct conn *conn) { int pfds[2]; FILE *out; struct hthead *req, *resp; - char *hd; + char *hd, *id; off_t dlen; int keep; + id = connid(); out = NULL; req = resp = NULL; - while(1) { + while(plex >= 0) { if((req = parsereq(in)) == NULL) break; if(!canonreq(req)) break; + headappheader(req, "X-Ash-Connection-ID", id); if((conn->initreq != NULL) && conn->initreq(conn, req)) break; - if(block(plex, EV_WRITE, 60) <= 0) + if((plex < 0) || block(plex, EV_WRITE, 60) <= 0) break; if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds)) break; @@ -398,28 +420,43 @@ void serve(FILE *in, struct conn *conn) if(resp != NULL) freehthead(resp); fclose(in); + free(id); } static void plexwatch(struct muth *muth, va_list args) { vavar(int, fd); char *buf; - int ret; + int i, s, ret; + s = 0; while(1) { - block(fd, EV_READ, 0); + if(block(fd, EV_READ, 0) == 0) + break; buf = smalloc(65536); ret = recv(fd, buf, 65536, 0); if(ret < 0) { flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno)); exit(1); } else if(ret == 0) { - exit(0); + s = 1; + free(buf); + break; } /* Maybe I'd like to implement some protocol in this direction * some day... */ free(buf); } + shutdown(plex, SHUT_RDWR); + for(i = 0; i < listeners.d; i++) { + if(listeners.b[i] == muth) + bufdel(listeners, i); + } + if(s) { + flog(LOG_INFO, "root handler exited, so shutting down listening..."); + while(listeners.d > 0) + resume(listeners.b[0], 0); + } } static void initroot(void *uu) @@ -494,9 +531,14 @@ static void addport(char *spec) buffree(vals); } +static void sighandler(int sig) +{ + exitioloop(1); +} + int main(int argc, char **argv) { - int c; + int c, d; int i, s1; char *root; FILE *pidout; @@ -548,7 +590,7 @@ int main(int argc, char **argv) flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno)); return(1); } - mustart(plexwatch, plex); + bufadd(listeners, mustart(plexwatch, plex)); pidout = NULL; if(pidfile != NULL) { if((pidout = fopen(pidfile, "w")) == NULL) { @@ -575,6 +617,9 @@ int main(int argc, char **argv) } } signal(SIGPIPE, SIG_IGN); + signal(SIGCHLD, SIG_IGN); + signal(SIGINT, sighandler); + signal(SIGTERM, sighandler); if(daemonize) { daemon(0, 0); } @@ -582,6 +627,22 @@ int main(int argc, char **argv) fprintf(pidout, "%i\n", getpid()); fclose(pidout); } - ioloop(); + d = 0; + while(!d) { + switch(ioloop()) { + case 0: + d = 1; + break; + case 1: + if(listeners.d > 0) { + while(listeners.d > 0) + resume(listeners.b[0], 0); + flog(LOG_INFO, "no longer listening"); + } else { + d = 1; + } + break; + } + } return(0); }