--- /dev/null
+httimed(1)
+==========
+
+NAME
+----
+httimed - Ashd handler expiration pipe
+
+SYNOPSIS
+--------
+*httimed* [*-h*] [*-t* 'TIMEOUT'] 'CHILD' ['ARGS'...]
+
+DESCRIPTION
+-----------
+
+The *httimed* handler starts a single child handler which it passes
+all request it receives unmodified, and in addition keeps track of
+time and simply exits if no requests are received in a certain
+interval, which might be useful for handlers which are only rarely
+used and waste unproportionally much system resources if kept beyond
+their usefulness.
+
+*httimed* is a persistent handler, as defined in *ashd*(7), and the
+specified child handler must also be a persistent handler.
+
+By default, *httimed* exits if it receives no requests in five
+minutes, but the time interval can be specified using the *-t* option.
+
+If the child handler exits, *httimed* exits as well.
+
+OPTIONS
+-------
+
+*-h*::
+
+ Print a brief help message to standard output and exit.
+
+*-t* 'TIMEOUT'::
+
+ Exit if no requests are received after 'TIMEOUT' seconds since
+ the last one. If *-t* is not specified, 'TIMEOUT' is five
+ minutes by default.
+
+AUTHOR
+------
+Fredrik Tolf <fredrik@dolda2000.com>
+
+SEE ALSO
+--------
+*ashd*(7)
--- /dev/null
+/*
+ ashd - A Sane HTTP Daemon
+ Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <time.h>
+#include <sys/poll.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <utils.h>
+#include <log.h>
+#include <req.h>
+#include <proc.h>
+
+static void usage(FILE *out)
+{
+ fprintf(out, "usage: httimed [-h] [-t TIMEOUT] CHILD [ARGS...]\n");
+}
+
+int main(int argc, char **argv)
+{
+ int c, t, ret;
+ int ch, fd;
+ struct hthead *req;
+ struct pollfd pfd[2];
+ time_t lreq, now;
+
+ t = 300;
+ while((c = getopt(argc, argv, "+ht:")) >= 0) {
+ switch(c) {
+ case 'h':
+ usage(stdout);
+ exit(0);
+ case 't':
+ t = atoi(optarg);
+ if(t < 1) {
+ fprintf(stderr, "httimed: timeout must be positive\n");
+ exit(1);
+ }
+ break;
+ }
+ }
+ if(argc - optind < 1) {
+ usage(stderr);
+ exit(1);
+ }
+ if((ch = stdmkchild(argv + optind, NULL, NULL)) < 0) {
+ flog(LOG_ERR, "httimed: could not fork child: %s", strerror(errno));
+ exit(1);
+ }
+ lreq = time(NULL);
+ while(1) {
+ memset(pfd, 0, sizeof(pfd));
+ pfd[0].fd = 0;
+ pfd[0].events = POLLIN;
+ pfd[1].fd = ch;
+ pfd[1].events = POLLHUP;
+ if((ret = poll(pfd, 2, (t + 1 - (time(NULL) - lreq)) * 1000)) < 0) {
+ if(errno != EINTR) {
+ flog(LOG_ERR, "httimed: error in poll: %s", strerror(errno));
+ exit(1);
+ }
+ }
+ now = time(NULL);
+ if(pfd[0].revents) {
+ if((fd = recvreq(0, &req)) < 0) {
+ if(errno == 0)
+ break;
+ flog(LOG_ERR, "httimed: error in recvreq: %s", strerror(errno));
+ exit(1);
+ }
+ if(sendreq(ch, req, fd)) {
+ flog(LOG_ERR, "httimed: could not pass request to child: %s", strerror(errno));
+ exit(1);
+ }
+ freehthead(req);
+ close(fd);
+ }
+ if(pfd[1].revents & POLLHUP)
+ break;
+ if(now - lreq > t)
+ break;
+ lreq = now;
+ }
+ return(0);
+}