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/>.
34 static pid_t *children;
36 static volatile int done, chdone;
38 static void runchild(void)
40 execvp(chspec[0], chspec);
41 fprintf(stderr, "%s: %s", chspec[0], strerror(errno));
45 static void manage(void)
52 sigaddset(&ss, SIGCHLD);
53 sigaddset(&ss, SIGTERM);
54 sigaddset(&ss, SIGINT);
55 sigprocmask(SIG_BLOCK, &ss, &ns);
57 for(i = 0; i < nchildren; i++) {
58 if(children[i] == 0) {
66 pselect(0, NULL, NULL, NULL, NULL, &ns);
68 while((ch = waitpid(-1, &st, WNOHANG)) > 0) {
69 for(i = 0; i < nchildren; i++) {
77 sigprocmask(SIG_SETMASK, &ns, NULL);
80 static void killall(void)
88 signal(SIGINT, SIG_DFL);
89 signal(SIGTERM, SIG_DFL);
91 sigaddset(&ss, SIGCHLD);
92 sigprocmask(SIG_BLOCK, &ss, &ns);
93 for(try = 0; try < 2; try++) {
94 for(i = 0; i < nchildren; i++) {
96 kill(children[i], SIGTERM);
99 while(time(NULL) - b < 5) {
100 for(i = 0, left = 0; i < nchildren; i++) {
108 pselect(0, NULL, NULL, NULL, &to, &ns);
110 while((ch = waitpid(-1, &st, WNOHANG)) > 0) {
111 for(i = 0; i < nchildren; i++) {
112 if(children[i] == ch)
120 for(i = 0; i < nchildren; i++) {
122 kill(children[i], SIGKILL);
126 static void chld(int sig)
131 static void term(int sig)
136 static void usage(FILE *out)
138 fprintf(out, "usage: multifscgi [-h] NUM PROGRAM [ARGS...]\n");
141 int main(int argc, char **argv)
145 while((c = getopt(argc, argv, "+h")) >= 0) {
155 if(argc - optind < 2) {
159 nchildren = atoi(argv[optind]);
164 children = szmalloc(sizeof(pid_t) * nchildren);
165 chspec = argv + optind + 1;
166 signal(SIGINT, term);
167 signal(SIGTERM, term);
168 signal(SIGCHLD, chld);