Subversion Repositories wimsdev

Rev

Rev 8185 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8185 Rev 8849
Line 17... Line 17...
17
 
17
 
18
/* fork management */
18
/* fork management */
19
#include "wimslogd.h"
19
#include "wimslogd.h"
20
 
20
 
21
#define MAX_FORK 1024
21
#define MAX_FORK 1024
22
#define MAX_DELAY 1500  /* At most these seconds of execution */
22
#define MAX_DELAY 1500      /* At most these seconds of execution */
23
 
23
 
24
struct {
24
struct {
25
    pid_t pid;
25
    pid_t pid;
26
    time_t t;
26
    time_t t;
27
    int type;
27
    int type;
Line 30... Line 30...
30
int forkcnt;
30
int forkcnt;
31
 
31
 
32
void addfork(pid_t pid, int type)
32
void addfork(pid_t pid, int type)
33
{
33
{
34
    if(forkcnt>=MAX_FORK) {
34
    if(forkcnt>=MAX_FORK) {
35
        kill(pid,SIGKILL); return;
35
      kill(pid,SIGKILL); return;
36
    }
36
    }
37
    forklist[forkcnt].pid=pid;
37
    forklist[forkcnt].pid=pid;
38
    forklist[forkcnt].t=time(NULL);
38
    forklist[forkcnt].t=time(NULL);
39
    forklist[forkcnt].type=type;
39
    forklist[forkcnt].type=type;
40
    forkcnt++;
40
    forkcnt++;
Line 48... Line 48...
48
    delay=MAX_DELAY;
48
    delay=MAX_DELAY;
49
    if(forkcnt>=MAX_FORK/2) delay=delay/5;
49
    if(forkcnt>=MAX_FORK/2) delay=delay/5;
50
    if(forkcnt*4>=MAX_FORK*3) delay=delay/4;
50
    if(forkcnt*4>=MAX_FORK*3) delay=delay/4;
51
    now=time(NULL);
51
    now=time(NULL);
52
    for(i=forkcnt-1; i>=0; i--) {
52
    for(i=forkcnt-1; i>=0; i--) {
53
        if(now-forklist[i].t>delay) kill(forklist[i].pid,SIGKILL);
53
      if(now-forklist[i].t>delay) kill(forklist[i].pid,SIGKILL);
54
        t=waitpid(forklist[i].pid,&st,WNOHANG);
54
      t=waitpid(forklist[i].pid,&st,WNOHANG);
55
        if(t==0 || !WIFEXITED(st)) continue;
55
      if(t==0 || !WIFEXITED(st)) continue;
56
        memmove(forklist+i,forklist+i+1,forkcnt-1-i);
56
      memmove(forklist+i,forklist+i+1,forkcnt-1-i);
57
        forkcnt--;
57
      forkcnt--;
58
    }
58
    }
59
    if(kz) waitpid(-1,NULL,WNOHANG); /* kill zombies */
59
    if(kz) waitpid(-1,NULL,WNOHANG); /* kill zombies */
60
}
60
}
61
 
61
 
62
void wait_children(void)
62
void wait_children(void)
63
{
63
{
64
    time_t now;
64
    time_t now;
65
    int i;
65
    int i;
66
    do {
66
    do {
67
        now=time(NULL);
67
      now=time(NULL);
68
        for(i=0; i<forkcnt; i++) {
68
      for(i=0; i<forkcnt; i++) {
69
            if(forklist[i].type && forklist[i].t < now-MAX_DELAY) break;
69
          if(forklist[i].type && forklist[i].t < now-MAX_DELAY) break;
70
        }
70
      }
71
        if(i>=forkcnt) return;
71
      if(i>=forkcnt) return;
72
        sleep(1);
72
      sleep(1);
73
    }
73
    }
74
    while(1);
74
    while(1);
75
}
75
}
76
 
76