+ setsid();
+ if(daemonize) {
+ chdir("/");
+ if((fd = open("/dev/null", O_RDWR)) >= 0) {
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ close(fd);
+ }
+ }
+ if(usesyslog)
+ putenv("ASHD_USESYSLOG=1");
+ else
+ unsetenv("ASHD_USESYSLOG");
+}
+
+static void usage(FILE *out)
+{
+ fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
+ fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
+ fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
+}
+
+static void addport(char *spec)
+{
+ char *nm, *p, *p2, *n;
+ struct charvbuf pars, vals;
+
+ bufinit(pars);
+ bufinit(vals);
+ if((p = strchr(spec, ':')) == NULL) {
+ nm = spec;
+ } else {
+ nm = spec;
+ *(p++) = 0;
+ do {
+ if((n = strchr(p, ',')) != NULL)
+ *(n++) = 0;
+ if((p2 = strchr(p, '=')) != NULL)
+ *(p2++) = 0;
+ if(!*p) {
+ usage(stderr);
+ exit(1);
+ }
+ bufadd(pars, p);
+ if(p2)
+ bufadd(vals, p2);
+ else
+ bufadd(vals, "");
+ } while((p = n) != NULL);
+ }
+
+ /* XXX: It would be nice to decentralize this, but, meh... */
+ if(!strcmp(nm, "plain")) {
+ handleplain(pars.d, pars.b, vals.b);
+#ifdef HAVE_GNUTLS
+ } else if(!strcmp(nm, "ssl")) {
+ handlegnussl(pars.d, pars.b, vals.b);
+#endif
+ } else {
+ flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
+ exit(1);
+ }
+
+ buffree(pars);
+ buffree(vals);
+}
+
+static void sighandler(int sig)
+{
+ exitioloop(1);