Subversion Repositories wimsdev

Rev

Rev 18514 | 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. #include "wimslogd.h"
  23.  
  24. /* internal */
  25. void _cleaning(char *di,int hardcheck)
  26. {
  27.   DIR *sdir_base;
  28.   struct dirent *ses;
  29.   struct stat session_stat;
  30.   char session_name[MAX_LINELEN+1];
  31.  
  32.   sdir_base=opendir(di);
  33.   if(sdir_base==NULL) return;
  34.   while((ses=readdir(sdir_base))!=NULL) {
  35.     if(ses->d_name[0]=='.') continue;
  36.     snprintf(session_name,sizeof(session_name),"%s/%s",di,ses->d_name);
  37.     if(lstat(session_name,&session_stat)) {
  38.       wimslogd_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.         wimslogd_error("wimslogd cleaning(): unable to chase squatter file.");
  44.         return;
  45.       }
  46.     }
  47.     /* remove idle session. */
  48.   else {
  49.     char fbuf[4096],cbuf[MAX_LINELEN+1];
  50.     char *pp;
  51.     /* remove session inactive for more than idle_time seconds */
  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.     /* keep sessions with _ in name unless hardcheck */
  97.     if(!hardcheck || strchr(session_name,'_')!=NULL) continue;
  98.     /* keep very new sessions (inactive for less than idle_time3 seconds) */
  99.     if(session_stat.st_mtime>=nowtime-idle_time3 &&
  100.        session_stat.st_mtime<nowtime+anti_time) continue;
  101.     /* for sessions inactive for more than idle_time3 seconds,
  102.        keep session if var.stat file exists and wims_user or wims_developer variable is defined
  103.     */
  104.     snprintf(fbuf,sizeof(fbuf),"%s/var.stat",session_name);
  105.     wlogdaccessfile(fbuf,"r","%s/var.stat",session_name);
  106.     if(fbuf[0]!= 0 && (strstr(fbuf,"wims_user=")!=NULL || strstr(fbuf,"wims_developer=")!=NULL)) continue;
  107.     /* sessions with no var.stat file, chech var file */
  108.     wlogdaccessfile(cbuf,"r","%s/var",session_name);
  109.     if(cbuf[0]==0) goto remove;      /* no var file, remove */
  110.     /* keep manager sessions */
  111.     pp=strstr(cbuf,"\nw_wims_ismanager=");
  112.     if(pp!=NULL) {
  113.       pp+=strlen("\nw_wims_ismanager=");
  114.       if(*pp>'0' && *pp<='9') continue;
  115.     }
  116.     /* remove session inactive for more than idle_time2 seconds */
  117.     if(session_stat.st_mtime<nowtime-idle_time2 ||
  118.         session_stat.st_mtime>nowtime+anti_time) goto remove;
  119.     /* remove unused sessions */
  120.     if(session_stat.st_mtime<nowtime-idle_time3 &&
  121.         strstr(cbuf,"\nwims_new_session=yes\n")!=NULL) goto remove;
  122.          /* remove popup session: 50 sec only. */
  123.     if(session_stat.st_mtime<nowtime-50 &&
  124.         strstr(cbuf,"\nw_wims_mode=popup\n")!=NULL) goto remove;
  125.     }
  126.   }
  127.   closedir(sdir_base);
  128. }
  129.  
  130. /* Clean obsolete session directories. */
  131. void cleaning(int withmain)
  132. {
  133.   struct stat lastclean_stat;
  134.   pid_t pid;
  135.   char lastclean_name[MAX_FNAME+1];
  136.   FILE *lastclean;
  137.   /* Active only if idle_time>0 */
  138.   if(idle_time<=0) return;
  139.   /* when is last clean? */
  140.   if(lastcleantime>nowtime-300) return;
  141.   mystrncpy(lastclean_name,"tmp/log/lastclean",sizeof(lastclean_name));
  142.   if(stat(lastclean_name,&lastclean_stat)==0 &&
  143.       lastclean_stat.st_mtime>nowtime-300 &&
  144.       lastclean_stat.st_mtime<nowtime+100) return;
  145.   fflush(NULL);
  146.   pid=fork(); if(pid>0) {addfork(pid,0); return;}
  147.   close(commsock);
  148.   if(withmain) _cleaning(sesd,1);
  149.   _cleaning("s2",2);
  150.   _cleaning("tmp/sessions",0);
  151.   _cleaning("chroot/tmp/sessions",0);
  152.   /* touch lastclean file */
  153.   lastclean=fopen(lastclean_name,"w"); fclose(lastclean);
  154.   lastcleantime=nowtime;
  155.   snprintf(lastclean_name,sizeof(lastclean_name),"%s/trap.check",tmpd);
  156.   if(stat(lastclean_name,&lastclean_stat)==0 &&
  157.       (lastclean_stat.st_mtime<nowtime-3600 ||
  158.       lastclean_stat.st_mtime>nowtime+anti_time)) unlink(lastclean_name);
  159.   exit(0);
  160. }
  161.