Subversion Repositories wimsdev

Rev

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