Back to index

wims  3.65+svn20090927
cleaning.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* This file contains a routine to do housekeeping:
00019         * it erases obsolete session directories.
00020         * Regular checkup every 10 minutes or so. */
00021 
00022        /* internal */
00023 void _cleaning(char *di,int hardcheck)
00024 {
00025     DIR *sdir_base;
00026     struct dirent *ses;
00027     struct stat session_stat;
00028     char session_name[MAX_LINELEN+1];
00029 
00030     sdir_base=opendir(di);
00031     if(sdir_base==NULL) return;
00032     while((ses=readdir(sdir_base))!=NULL) {
00033        if(ses->d_name[0]=='.') continue;
00034        snprintf(session_name,sizeof(session_name),"%s/%s",
00035                di,ses->d_name);
00036        if(lstat(session_name,&session_stat)) {
00037            error("wimslog cleaning(): session stat failure.");
00038            return;
00039        }
00040        if(!S_ISDIR(session_stat.st_mode)) { /* not a directory: remove it. */
00041            if(remove(session_name)<0) {
00042               error("wimslogd cleaning(): unable to chase squatter file.");
00043               return;
00044            }
00045        }
00046            /* remove idle session. */
00047        else {
00048            struct stat fst;
00049            char fbuf[4096],cbuf[MAX_LINELEN+1];
00050            char *pp;
00051            if(session_stat.st_mtime<nowtime-idle_time ||
00052               session_stat.st_mtime>nowtime+anti_time) {
00053               remove:
00054               if(remove_tree(session_name)!=0) {
00055                   if(strstr(session_name,"chroot")!=NULL) {
00056                      char tbuf[4096];
00057                      snprintf(tbuf,sizeof(tbuf),"/%s",session_name);
00058                      setenv("tmp_dir",tbuf,1);
00059                      chmod(session_name,
00060                            S_IRUSR|S_IWUSR|S_IXUSR|
00061                            S_IRGRP|S_IWGRP|S_IXGRP|
00062                            S_IROTH|S_IWOTH|S_IXOTH);
00063                      chdir("public_html");
00064                      call_ssh(1,"bin/ch..root cleantmpdir");
00065                      chdir(cwd);
00066                      chmod(session_name,S_IRUSR|S_IWUSR|S_IXUSR);
00067                      fprintf(stderr,"%s\n",tbuf);
00068                   }
00069                   if(remove_tree(session_name)!=0) {
00070                      fprintf(stderr,"Unable to remove session %s: %s.\n",
00071                             session_name,strerror(errno));
00072                   }
00073               }
00074               continue;
00075            }
00076            if(hardcheck==2) {
00077               char dbuf[MAX_FNAME+1];
00078               struct dirent *s2d;
00079               struct stat fst;
00080               int t;
00081               DIR *s2D;
00082               snprintf(dbuf,sizeof(dbuf),"%s/%s",sesd,ses->d_name);
00083               if(ftest(dbuf)!=is_dir) goto remove;
00084               s2D=opendir(session_name);
00085               if(sdir_base==NULL) goto remove;
00086               while((s2d=readdir(s2D))!=NULL) { /* remove individual files */
00087                   snprintf(dbuf,sizeof(dbuf),"%s/%s",session_name,s2d->d_name);
00088                   t=stat(dbuf,&fst);
00089                   if(t==0 && fst.st_mtime<nowtime-INS_DELAY &&
00090                      fst.st_mtime>=nowtime+anti_time) remove(dbuf);
00091               }
00092               closedir(s2D);
00093               continue;
00094            }
00095            if(!hardcheck || strchr(session_name,'_')!=NULL) continue;
00096            if(session_stat.st_mtime>=nowtime-idle_time3 &&
00097               session_stat.st_mtime<nowtime+anti_time) continue;
00098            snprintf(fbuf,sizeof(fbuf),"%s/var.stat",session_name);
00099            if(stat(fbuf,&fst)==0) continue;
00100            accessfile(cbuf,"r","%s/var",session_name);
00101            if(cbuf[0]==0) goto remove;    /* no var file */
00102            pp=strstr(cbuf,"\nw_wims_ismanager=");
00103            if(pp!=NULL) {
00104               pp+=strlen("\nw_wims_ismanager=");
00105               if(*pp>'0' && *pp<='9') continue;
00106            }
00107            if(session_stat.st_mtime<nowtime-idle_time2 ||
00108               session_stat.st_mtime>nowtime+anti_time) goto remove;
00109            if(session_stat.st_mtime<nowtime-idle_time3 &&
00110               strstr(cbuf,"\nwims_new_session=yes\n")!=NULL) goto remove;
00111               /* popup session: 50 sec only. */
00112            if(session_stat.st_mtime<nowtime-50 &&
00113               strstr(cbuf,"\nw_wims_mode=popup\n")!=NULL) goto remove;
00114        }
00115     }
00116     closedir(sdir_base);
00117 }
00118 
00119        /* Clean obsolete session directories. */
00120 void cleaning(int withmain)
00121 {
00122     struct stat lastclean_stat;
00123     pid_t pid;
00124     char lastclean_name[MAX_FNAME+1];
00125     FILE *lastclean;
00126        /* Active only if idle_time>0 */
00127     if(idle_time<=0) return;
00128        /* when is last clean? */
00129     if(lastcleantime>nowtime-300) return;
00130     mystrncpy(lastclean_name,"tmp/log/lastclean",sizeof(lastclean_name));
00131     if(stat(lastclean_name,&lastclean_stat)==0 &&
00132        lastclean_stat.st_mtime>nowtime-300 &&
00133        lastclean_stat.st_mtime<nowtime+100) return;
00134     fflush(NULL);
00135     pid=fork(); if(pid>0) {addfork(pid,0); return;}
00136     close(commsock);
00137     if(withmain) _cleaning(sesd,1);
00138     _cleaning("s2",2);
00139     _cleaning("tmp/sessions",0);
00140     _cleaning("chroot/tmp/sessions",0);
00141        /* touch lastclean file */
00142     lastclean=fopen(lastclean_name,"w"); fclose(lastclean);
00143     lastcleantime=nowtime;
00144     snprintf(lastclean_name,sizeof(lastclean_name),"%s/trap.check",tmpd);
00145     if(stat(lastclean_name,&lastclean_stat)==0 &&
00146        (lastclean_stat.st_mtime<nowtime-3600 ||
00147        lastclean_stat.st_mtime>nowtime+anti_time)) unlink(lastclean_name);
00148     exit(0);
00149 }
00150