Subversion Repositories wimsdev

Rev

Rev 8849 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  
  18. /* fork management */
  19. #include "wimslogd.h"
  20.  
  21. #define MAX_FORK 1024
  22. #define MAX_DELAY 1500      /* At most these seconds of execution */
  23.  
  24. struct {
  25.   pid_t pid;
  26.   time_t t;
  27.   int type;
  28.   int pad;
  29. } forklist[MAX_FORK];
  30. int forkcnt;
  31.  
  32. void addfork(pid_t pid, int type)
  33. {
  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++;
  41. }
  42.  
  43. /* forklist management */
  44. void forkman(int kz)
  45. {
  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 */
  60. }
  61.  
  62. void wait_children(void)
  63. {
  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;
  70.     }
  71.     if(i>=forkcnt) return;
  72.     sleep(1);
  73.   }
  74.   while(1);
  75. }
  76.  
  77.