Subversion Repositories wimsdev

Rev

Rev 8849 | Rev 18509 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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