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) {
70 fprintf(stderr, "multifscgi: child %i (%s) dumped core\n", ch, chspec[0]);
71 for(i = 0; i < nchildren; i++) {
79 sigprocmask(SIG_SETMASK, &ns, NULL);
82 static void killall(void)
90 signal(SIGINT, SIG_DFL);
91 signal(SIGTERM, SIG_DFL);
93 sigaddset(&ss, SIGCHLD);
94 sigprocmask(SIG_BLOCK, &ss, &ns);
95 for(try = 0; try < 2; try++) {
96 for(i = 0; i < nchildren; i++) {
98 kill(children[i], SIGTERM);
101 while(time(NULL) - b < 5) {
102 for(i = 0, left = 0; i < nchildren; i++) {
110 pselect(0, NULL, NULL, NULL, &to, &ns);
112 while((ch = waitpid(-1, &st, WNOHANG)) > 0) {
114 fprintf(stderr, "multifscgi: child %i (%s) dumped core\n", ch, chspec[0]);
115 for(i = 0; i < nchildren; i++) {
116 if(children[i] == ch)
124 for(i = 0; i < nchildren; i++) {
126 kill(children[i], SIGKILL);
130 static void chld(int sig)
135 static void term(int sig)
140 static void usage(FILE *out)
142 fprintf(out, "usage: multifscgi [-h] NUM PROGRAM [ARGS...]\n");
145 int main(int argc, char **argv)
149 while((c = getopt(argc, argv, "+h")) >= 0) {
159 if(argc - optind < 2) {
163 nchildren = atoi(argv[optind]);
168 children = szmalloc(sizeof(pid_t) * nchildren);
169 chspec = argv + optind + 1;
170 signal(SIGINT, term);
171 signal(SIGTERM, term);
172 signal(SIGCHLD, chld);