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/>.
33 static pid_t *children;
35 static volatile int done, chdone;
37 static void runchild(void)
39 execvp(chspec[0], chspec);
43 static void manage(void)
50 sigaddset(&ss, SIGCHLD);
51 sigaddset(&ss, SIGTERM);
52 sigaddset(&ss, SIGINT);
53 sigprocmask(SIG_BLOCK, &ss, &ns);
55 for(i = 0; i < nchildren; i++) {
56 if(children[i] == 0) {
64 pselect(0, NULL, NULL, NULL, NULL, &ns);
66 while((ch = waitpid(-1, &st, WNOHANG)) > 0) {
67 for(i = 0; i < nchildren; i++) {
75 sigprocmask(SIG_SETMASK, &ns, NULL);
78 static void killall(void)
86 signal(SIGINT, SIG_DFL);
87 signal(SIGTERM, SIG_DFL);
89 sigaddset(&ss, SIGCHLD);
90 sigprocmask(SIG_BLOCK, &ss, &ns);
91 for(try = 0; try < 2; try++) {
92 for(i = 0; i < nchildren; i++) {
94 kill(children[i], SIGTERM);
97 while(time(NULL) - b < 5) {
98 for(i = 0, left = 0; i < nchildren; i++) {
106 pselect(0, NULL, NULL, NULL, &to, &ns);
108 while((ch = waitpid(-1, &st, WNOHANG)) > 0) {
109 for(i = 0; i < nchildren; i++) {
110 if(children[i] == ch)
118 for(i = 0; i < nchildren; i++) {
120 kill(children[i], SIGKILL);
124 static void chld(int sig)
129 static void term(int sig)
134 static void usage(FILE *out)
136 fprintf(out, "usage: multifscgi NUM PROGRAM [ARGS...]\n");
139 int main(int argc, char **argv)
143 while((c = getopt(argc, argv, "h")) >= 0) {
153 if(argc - optind < 2) {
157 nchildren = atoi(argv[optind]);
162 children = szmalloc(sizeof(pid_t) * nchildren);
163 chspec = argv + optind + 1;
164 signal(SIGINT, term);
165 signal(SIGTERM, term);
166 signal(SIGCHLD, chld);