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>
36 static void usage(FILE *out)
38 fprintf(out, "usage: httimed [-h] [-t TIMEOUT] CHILD [ARGS...]\n");
41 int main(int argc, char **argv)
50 while((c = getopt(argc, argv, "+ht:")) >= 0) {
56 timeout = atoi(optarg);
58 fprintf(stderr, "httimed: timeout must be positive\n");
64 if(argc - optind < 1) {
68 if((ch = stdmkchild(argv + optind, NULL, NULL)) < 0) {
69 flog(LOG_ERR, "httimed: could not fork child: %s", strerror(errno));
74 memset(pfd, 0, sizeof(pfd));
76 pfd[0].events = POLLIN;
78 pfd[1].events = POLLHUP;
79 if((ret = poll(pfd, 2, (timeout < 0)?-1:((timeout + 1 - (time(NULL) - lreq)) * 1000))) < 0) {
81 flog(LOG_ERR, "httimed: error in poll: %s", strerror(errno));
87 if((fd = recvreq(0, &req)) < 0) {
90 flog(LOG_ERR, "httimed: error in recvreq: %s", strerror(errno));
93 if(sendreq(ch, req, fd)) {
94 flog(LOG_ERR, "httimed: could not pass request to child: %s", strerror(errno));
100 if((pfd[1].revents & POLLHUP) || (now - lreq > timeout)) {
102 shutdown(0, SHUT_RDWR);