Subversion Repositories wimsdev

Rev

Rev 3836 | Go to most recent revision | 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.         /* This file contains a routine to do housekeeping:
  19.          * it erases obsolete session directories.
  20.          * Regular checkup every 10 minutes or so. */
  21.  
  22.         /* internal */
  23. void _cleaning(char *di,int hardcheck)
  24. {
  25.     DIR *sdir_base;
  26.     struct dirent *ses;
  27.     struct stat session_stat;
  28.     char session_name[MAX_LINELEN+1];
  29.  
  30.     sdir_base=opendir(di);
  31.     if(sdir_base==NULL) return;
  32.     while((ses=readdir(sdir_base))!=NULL) {
  33.         if(ses->d_name[0]=='.') continue;
  34.         snprintf(session_name,sizeof(session_name),"%s/%s",
  35.                  di,ses->d_name);
  36.         if(lstat(session_name,&session_stat)) {
  37.             error("wimslog cleaning(): session stat failure.");
  38.             return;
  39.         }
  40.         if(!S_ISDIR(session_stat.st_mode)) { /* not a directory: remove it. */
  41.             if(remove(session_name)<0) {
  42.                 error("wimslogd cleaning(): unable to chase squatter file.");
  43.                 return;
  44.             }
  45.         }
  46.             /* remove idle session. */
  47.         else {
  48.             struct stat fst;
  49.             char fbuf[4096],cbuf[MAX_LINELEN+1];
  50.             char *pp;
  51.             if(session_stat.st_mtime<nowtime-idle_time ||
  52.                session_stat.st_mtime>nowtime+anti_time) {
  53.                 remove:
  54.                 if(remove_tree(session_name)!=0) {
  55.                     if(strstr(session_name,"chroot")!=NULL) {
  56.                         char tbuf[4096];
  57.                         snprintf(tbuf,sizeof(tbuf),"/%s",session_name);
  58.                         setenv("tmp_dir",tbuf,1);
  59.                         chmod(session_name,
  60.                               S_IRUSR|S_IWUSR|S_IXUSR|
  61.                               S_IRGRP|S_IWGRP|S_IXGRP|
  62.                               S_IROTH|S_IWOTH|S_IXOTH);
  63.                         chdir("public_html");
  64.                         call_ssh(1,"bin/ch..root cleantmpdir");
  65.                         chdir(cwd);
  66.                         chmod(session_name,S_IRUSR|S_IWUSR|S_IXUSR);
  67.                         fprintf(stderr,"%s\n",tbuf);
  68.                     }
  69.                     if(remove_tree(session_name)!=0) {
  70.                         fprintf(stderr,"Unable to remove session %s: %s.\n",
  71.                                 session_name,strerror(errno));
  72.                     }
  73.                 }
  74.                 continue;
  75.             }
  76.             if(hardcheck==2) {
  77.                 char dbuf[MAX_FNAME+1];
  78.                 struct dirent *s2d;
  79.                 struct stat fst;
  80.                 int t;
  81.                 DIR *s2D;
  82.                 snprintf(dbuf,sizeof(dbuf),"%s/%s",sesd,ses->d_name);
  83.                 if(ftest(dbuf)!=is_dir) goto remove;
  84.                 s2D=opendir(session_name);
  85.                 if(sdir_base==NULL) goto remove;
  86.                 while((s2d=readdir(s2D))!=NULL) { /* remove individual files */
  87.                     snprintf(dbuf,sizeof(dbuf),"%s/%s",session_name,s2d->d_name);
  88.                     t=stat(dbuf,&fst);
  89.                     if(t==0 && fst.st_mtime<nowtime-INS_DELAY &&
  90.                        fst.st_mtime>=nowtime+anti_time) remove(dbuf);
  91.                 }
  92.                 closedir(s2D);
  93.                 continue;
  94.             }
  95.             if(!hardcheck || strchr(session_name,'_')!=NULL) continue;
  96.             if(session_stat.st_mtime>=nowtime-idle_time3 &&
  97.                session_stat.st_mtime<nowtime+anti_time) continue;
  98.             snprintf(fbuf,sizeof(fbuf),"%s/var.stat",session_name);
  99.             if(stat(fbuf,&fst)==0) continue;
  100.             accessfile(cbuf,"r","%s/var",session_name);
  101.             if(cbuf[0]==0) goto remove; /* no var file */
  102.             pp=strstr(cbuf,"\nw_wims_ismanager=");
  103.             if(pp!=NULL) {
  104.                 pp+=strlen("\nw_wims_ismanager=");
  105.                 if(*pp>'0' && *pp<='9') continue;
  106.             }
  107.             if(session_stat.st_mtime<nowtime-idle_time2 ||
  108.                session_stat.st_mtime>nowtime+anti_time) goto remove;
  109.             if(session_stat.st_mtime<nowtime-idle_time3 &&
  110.                strstr(cbuf,"\nwims_new_session=yes\n")!=NULL) goto remove;
  111.                 /* popup session: 50 sec only. */
  112.             if(session_stat.st_mtime<nowtime-50 &&
  113.                strstr(cbuf,"\nw_wims_mode=popup\n")!=NULL) goto remove;
  114.         }
  115.     }
  116.     closedir(sdir_base);
  117. }
  118.  
  119.         /* Clean obsolete session directories. */
  120. void cleaning(int withmain)
  121. {
  122.     struct stat lastclean_stat;
  123.     pid_t pid;
  124.     char lastclean_name[MAX_FNAME+1];
  125.     FILE *lastclean;
  126.         /* Active only if idle_time>0 */
  127.     if(idle_time<=0) return;
  128.         /* when is last clean? */
  129.     if(lastcleantime>nowtime-300) return;
  130.     mystrncpy(lastclean_name,"tmp/log/lastclean",sizeof(lastclean_name));
  131.     if(stat(lastclean_name,&lastclean_stat)==0 &&
  132.        lastclean_stat.st_mtime>nowtime-300 &&
  133.        lastclean_stat.st_mtime<nowtime+100) return;
  134.     fflush(NULL);
  135.     pid=fork(); if(pid>0) {addfork(pid,0); return;}
  136.     close(commsock);
  137.     if(withmain) _cleaning(sesd,1);
  138.     _cleaning("s2",2);
  139.     _cleaning("tmp/sessions",0);
  140.     _cleaning("chroot/tmp/sessions",0);
  141.         /* touch lastclean file */
  142.     lastclean=fopen(lastclean_name,"w"); fclose(lastclean);
  143.     lastcleantime=nowtime;
  144.     snprintf(lastclean_name,sizeof(lastclean_name),"%s/trap.check",tmpd);
  145.     if(stat(lastclean_name,&lastclean_stat)==0 &&
  146.        (lastclean_stat.st_mtime<nowtime-3600 ||
  147.         lastclean_stat.st_mtime>nowtime+anti_time)) unlink(lastclean_name);
  148.     exit(0);
  149. }
  150.  
  151.