Rev 8849 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | |||
8155 | bpr | 18 | /* fork management */ |
8185 | bpr | 19 | #include "wimslogd.h" |
10 | reyssat | 20 | |
21 | #define MAX_FORK 1024 |
||
8849 | bpr | 22 | #define MAX_DELAY 1500 /* At most these seconds of execution */ |
10 | reyssat | 23 | |
24 | struct { |
||
12474 | bpr | 25 | pid_t pid; |
26 | time_t t; |
||
27 | int type; |
||
28 | int pad; |
||
10 | reyssat | 29 | } forklist[MAX_FORK]; |
30 | int forkcnt; |
||
31 | |||
32 | void addfork(pid_t pid, int type) |
||
33 | { |
||
12474 | bpr | 34 | if(forkcnt>=MAX_FORK) { |
35 | kill(pid,SIGKILL); return; |
||
36 | } |
||
37 | forklist[forkcnt].pid=pid; |
||
38 | forklist[forkcnt].t=time(NULL); |
||
39 | forklist[forkcnt].type=type; |
||
40 | forkcnt++; |
||
10 | reyssat | 41 | } |
42 | |||
8155 | bpr | 43 | /* forklist management */ |
44 | void forkman(int kz) |
||
10 | reyssat | 45 | { |
12474 | bpr | 46 | int delay, i, t, st; |
47 | time_t now; |
||
48 | delay=MAX_DELAY; |
||
49 | if(forkcnt>=MAX_FORK/2) delay=delay/5; |
||
50 | if(forkcnt*4>=MAX_FORK*3) delay=delay/4; |
||
51 | now=time(NULL); |
||
52 | for(i=forkcnt-1; i>=0; i--) { |
||
53 | if(now-forklist[i].t>delay) kill(forklist[i].pid,SIGKILL); |
||
54 | t=waitpid(forklist[i].pid,&st,WNOHANG); |
||
55 | if(t==0 || !WIFEXITED(st)) continue; |
||
56 | memmove(forklist+i,forklist+i+1,forkcnt-1-i); |
||
57 | forkcnt--; |
||
58 | } |
||
59 | if(kz) waitpid(-1,NULL,WNOHANG); /* kill zombies */ |
||
10 | reyssat | 60 | } |
61 | |||
62 | void wait_children(void) |
||
63 | { |
||
12474 | bpr | 64 | time_t now; |
65 | int i; |
||
66 | do { |
||
67 | now=time(NULL); |
||
68 | for(i=0; i<forkcnt; i++) { |
||
69 | if(forklist[i].type && forklist[i].t < now-MAX_DELAY) break; |
||
10 | reyssat | 70 | } |
12474 | bpr | 71 | if(i>=forkcnt) return; |
72 | sleep(1); |
||
73 | } |
||
74 | while(1); |
||
10 | reyssat | 75 | } |
76 |