Subversion Repositories wimsdev

Rev

Rev 18509 | Rev 18514 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 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
 
8155 bpr 18
/* This file contains a routine to do housekeeping:
19
 * it erases obsolete session directories.
20
 * Regular checkup every 10 minutes or so.
21
 */
8185 bpr 22
#include "wimslogd.h"
10 reyssat 23
 
8155 bpr 24
/* internal */
10 reyssat 25
void _cleaning(char *di,int hardcheck)
26
{
12474 bpr 27
  DIR *sdir_base;
28
  struct dirent *ses;
29
  struct stat session_stat;
30
  char session_name[MAX_LINELEN+1];
10 reyssat 31
 
12474 bpr 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;
8849 bpr 45
      }
12474 bpr 46
    }
47
    /* remove idle session. */
48
  else {
49
    struct stat fst;
50
    char fbuf[4096],cbuf[MAX_LINELEN+1];
51
    char *pp;
18509 czzmrn 52
    /* remove session inactive for more than idle_time seconds */
12474 bpr 53
    if(session_stat.st_mtime<nowtime-idle_time ||
54
        session_stat.st_mtime>nowtime+anti_time) {
55
      remove:
56
      if(remove_tree(session_name)!=0) {
57
        if(strstr(session_name,"chroot")!=NULL) {
58
          char tbuf[4096];
59
          snprintf(tbuf,sizeof(tbuf),"/%s",session_name);
60
          setenv("tmp_dir",tbuf,1);
61
          chmod(session_name,
62
                S_IRUSR|S_IWUSR|S_IXUSR|
63
                S_IRGRP|S_IWGRP|S_IXGRP|
64
                S_IROTH|S_IWOTH|S_IXOTH);
65
          (void)chdir("public_html");
66
          call_ssh(1,"bin/ch..root cleantmpdir");
67
          (void)chdir(cwd);
68
          chmod(session_name,S_IRUSR|S_IWUSR|S_IXUSR);
69
          fprintf(stderr,"%s\n",tbuf);
70
        }
71
        if(remove_tree(session_name)!=0) {
72
          fprintf(stderr,"Unable to remove session %s: %s.\n",
73
                 session_name,strerror(errno));
74
        }
8849 bpr 75
      }
12474 bpr 76
      continue;
77
    }
78
    if(hardcheck==2) {
79
      char dbuf[MAX_FNAME+1];
80
      struct dirent *s2d;
81
      struct stat fst;
82
      int t;
83
      DIR *s2D;
84
      snprintf(dbuf,sizeof(dbuf),"%s/%s",sesd,ses->d_name);
85
      if(ftest(dbuf)!=is_dir) goto remove;
86
      s2D=opendir(session_name);
87
      if(sdir_base==NULL) goto remove;
88
      while((s2d=readdir(s2D))!=NULL) { /* remove individual files */
89
        snprintf(dbuf,sizeof(dbuf),"%s/%s",session_name,s2d->d_name);
90
        t=stat(dbuf,&fst);
91
        if(t==0 && fst.st_mtime<nowtime-INS_DELAY &&
92
               fst.st_mtime>=nowtime+anti_time) remove(dbuf);
8849 bpr 93
      }
12474 bpr 94
      closedir(s2D);
95
      continue;
10 reyssat 96
    }
18509 czzmrn 97
    /* keep sessions with _ in name unless hardcheck */
12474 bpr 98
    if(!hardcheck || strchr(session_name,'_')!=NULL) continue;
18509 czzmrn 99
    /* keep very new sessions (inactive for less than idle_time3 seconds) */
12474 bpr 100
    if(session_stat.st_mtime>=nowtime-idle_time3 &&
101
       session_stat.st_mtime<nowtime+anti_time) continue;
18509 czzmrn 102
    /* for sessions inactive for more than idle_time3 seconds,
18510 czzmrn 103
       keep session if var.stat file exists and wims_class variable is defined
104
    */
12474 bpr 105
    snprintf(fbuf,sizeof(fbuf),"%s/var.stat",session_name);
18510 czzmrn 106
    wlogdaccessfile(fbuf,"r","%s/var.stat",session_name);
107
    if(fbuf[0]!= 0 && strstr(fbuf,"\nwims_class=")!=NULL) continue;
18509 czzmrn 108
    /* sessions with no var.stat file, chech var file */
12474 bpr 109
    wlogdaccessfile(cbuf,"r","%s/var",session_name);
18509 czzmrn 110
    if(cbuf[0]==0) goto remove;      /* no var file, remove */
111
    /* keep manager sessions */
12474 bpr 112
    pp=strstr(cbuf,"\nw_wims_ismanager=");
113
    if(pp!=NULL) {
114
      pp+=strlen("\nw_wims_ismanager=");
115
      if(*pp>'0' && *pp<='9') continue;
116
    }
18509 czzmrn 117
    /* remove session inactive for more than idle_time2 seconds */
12474 bpr 118
    if(session_stat.st_mtime<nowtime-idle_time2 ||
119
        session_stat.st_mtime>nowtime+anti_time) goto remove;
18509 czzmrn 120
    /* remove unused sessions */
12474 bpr 121
    if(session_stat.st_mtime<nowtime-idle_time3 &&
122
        strstr(cbuf,"\nwims_new_session=yes\n")!=NULL) goto remove;
18509 czzmrn 123
         /* remove popup session: 50 sec only. */
12474 bpr 124
    if(session_stat.st_mtime<nowtime-50 &&
125
        strstr(cbuf,"\nw_wims_mode=popup\n")!=NULL) goto remove;
126
    }
127
  }
128
  closedir(sdir_base);
10 reyssat 129
}
130
 
8155 bpr 131
/* Clean obsolete session directories. */
10 reyssat 132
void cleaning(int withmain)
133
{
12474 bpr 134
  struct stat lastclean_stat;
135
  pid_t pid;
136
  char lastclean_name[MAX_FNAME+1];
137
  FILE *lastclean;
138
  /* Active only if idle_time>0 */
139
  if(idle_time<=0) return;
140
  /* when is last clean? */
141
  if(lastcleantime>nowtime-300) return;
142
  mystrncpy(lastclean_name,"tmp/log/lastclean",sizeof(lastclean_name));
143
  if(stat(lastclean_name,&lastclean_stat)==0 &&
144
      lastclean_stat.st_mtime>nowtime-300 &&
145
      lastclean_stat.st_mtime<nowtime+100) return;
146
  fflush(NULL);
147
  pid=fork(); if(pid>0) {addfork(pid,0); return;}
148
  close(commsock);
149
  if(withmain) _cleaning(sesd,1);
150
  _cleaning("s2",2);
151
  _cleaning("tmp/sessions",0);
152
  _cleaning("chroot/tmp/sessions",0);
153
  /* touch lastclean file */
154
  lastclean=fopen(lastclean_name,"w"); fclose(lastclean);
155
  lastcleantime=nowtime;
156
  snprintf(lastclean_name,sizeof(lastclean_name),"%s/trap.check",tmpd);
157
  if(stat(lastclean_name,&lastclean_stat)==0 &&
158
      (lastclean_stat.st_mtime<nowtime-3600 ||
8849 bpr 159
      lastclean_stat.st_mtime>nowtime+anti_time)) unlink(lastclean_name);
12474 bpr 160
  exit(0);
10 reyssat 161
}