Subversion Repositories wimsdev

Rev

Rev 15509 | 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
/* Daily housekeeping jobs. */
8185 bpr 19
 
20
#include "wimslogd.h"
15509 bpr 21
int stringtodays(char * dat)
22
{
23
  static int len[13] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
24
  int y = 1000*(dat[0]-'0')+100*(dat[1]-'0')+10*(dat[2]-'0')+(dat[3]-'0');
25
  int m = 10*(dat[4]-'0')+(dat[5]-'0');
26
  int d = 10*(dat[6]-'0')+(dat[7]-'0');
27
  if (m < 3) {y--; d += 365;}
28
  return d + len[m] + 365*y + y/4 - y/100 + y/400;
29
}
30
void purgefile(char *fname)
31
{
32
  char buf[8];
33
  FILE *f = fopen(fname,"r");
34
  fread (buf,1,8,f);
35
  fclose(f);
36
  int d1 = stringtodays(buf);
37
  int d2 = stringtodays(nowstr);
15511 bpr 38
  if (d2-d1 > LOG_DELETE)
15509 bpr 39
    unlink(fname);
40
}
8185 bpr 41
 
10 reyssat 42
void housekeep(void)
43
{
15509 bpr 44
  struct stat st;
45
  char fname[1024], pathname[1024];
12474 bpr 46
  FILE *keeplog;
15509 bpr 47
  static char* lognames[]={
48
    "access.log","referer.log","session.log","post.log","mail.log",
49
    "user_error.log","internal_error.log","refuse.log"
50
  };
51
  int i,j,cntlogs=sizeof(lognames)/sizeof(char*);
12474 bpr 52
  if(strncmp(keepdate,nowstr,8)==0) return;
53
  snprintf(fname,sizeof(fname),"%s/keepdate",tmpd);
54
  keeplog=fopen(fname,"r");
55
  if(keeplog==NULL) goto dokeep;
56
  (void)fread(keepdate,8,1,keeplog); keepdate[8]=0; fclose(keeplog);
57
  if(strncmp(keepdate,nowstr,8)==0) return;
58
  dokeep:
59
  keeplog=fopen(fname,"w");
60
  if(keeplog!=NULL) {
61
    fwrite(nowstr,8,1,keeplog);fclose(keeplog);
62
  }
15509 bpr 63
/* delete files older than LOG_DELETE days */
64
  for (j=0; j < cntlogs; ++j)
65
  {
66
    snprintf(pathname,sizeof(pathname),"%s/%s",logd,lognames[j]);
67
    for(i=OLD_LOG_FILES-1;i>0;i--) {
68
      snprintf(fname,sizeof(fname),"%s.old%d",pathname,i);
69
     if(stat(fname,&st)==0) purgefile(fname);
70
    }
71
    if(stat(pathname,&st)==0) purgefile(pathname);
72
  }
73
 
12474 bpr 74
  call_ssh(0,"bin/housekeep.daily &>%s/housekeep.log",tmpd);
10 reyssat 75
}
76
 
8155 bpr 77
/* module update */
10 reyssat 78
void modupdate(void)
79
{
12474 bpr 80
  char fname[1024];
81
  FILE *muplog;
8155 bpr 82
 
12474 bpr 83
  if(strncmp(mupdate,nowstr,8)==0) return;
84
  snprintf(fname,sizeof(fname),"%s/mupdate",tmpd);
85
  muplog=fopen(fname,"r");
86
  if(muplog==NULL) goto domup;
87
  (void)fread(mupdate,8,1,muplog); mupdate[8]=0; fclose(muplog);
88
  if(strncmp(mupdate,nowstr,8)==0) return;
89
  domup:
90
  muplog=fopen(fname,"w");
91
  if(muplog!=NULL) {
92
    fwrite(nowstr,8,1,muplog);fclose(muplog);
93
  }
94
  call_ssh(0,"bin/modupdate.auto &>%s/modupdate.log",tmpd);
10 reyssat 95
}
96
 
8155 bpr 97
/* Daily backup. */
10 reyssat 98
void backup(void)
99
{
12474 bpr 100
  FILE *backlog;
8155 bpr 101
 
12474 bpr 102
  if(strncmp(backdate,nowstr,8)==0) return;
103
  backlog=fopen("backup/backdate","r");
104
  if(backlog==NULL) goto dobackup;
105
  (void)fread(backdate,8,1,backlog); backdate[8]=0; fclose(backlog);
106
  if(strncmp(backdate,nowstr,8)==0) return;
107
  dobackup:
108
  call_ssh(0,"bin/backup &>%s/backup.log",tmpd);
10 reyssat 109
}