Subversion Repositories wimsdev

Rev

Rev 15349 | Go to most recent revision | 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. /* Routines to write log files. */
  18.  
  19. #include "wimslogd.h"
  20.  
  21. char llbuf[4*MAX_LINELEN+4], *llptr;
  22. char *linetab[MAX_LOGLINES];
  23. int loglinecnt;
  24. char *logbuf, *logfname;
  25.  
  26. int llcmp(const void *c1, const void *c2)
  27. {
  28.   char **p1,**p2;
  29.   p1=(char **) c1; p2=(char **) c2;
  30.   return strcmp(*p1,*p2);
  31. }
  32. int stringtodays(char * dat)
  33. {
  34.   static int len[13] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  35.   int y = 1000*(dat[0]-'0')+100*(dat[1]-'0')+10*(dat[2]-'0')+(dat[3]-'0');
  36.   int m = 10*(dat[4]-'0')+(dat[5]-'0');
  37.   int d = 10*(dat[6]-'0')+(dat[7]-'0');
  38.   if (m < 3) {y--; d += 365;}
  39.   return d + len[m] + 365*y + y/4 - y/100 + y/400;
  40. }
  41. void purgefile(char *fname)
  42. {
  43.   char buf[8];
  44.   FILE *f = fopen(fname,"r");
  45.   fread (buf,1,8,f);
  46.   fclose(f);
  47.   int d1 = stringtodays(buf);
  48.   int d2 = stringtodays(nowstr);
  49.   if (d2-d1 > 100) /* to replace by FILEROT */
  50.     unlink(fname);
  51. }
  52.  
  53. void onelogfile(char *fname)
  54. {
  55.   char namebuf[1024], b3[1024], b4[1024], *pp;
  56.   FILE *f;
  57.   size_t l, l2;
  58.   struct stat st;
  59.   int i, lim;
  60.  
  61.   if(fname[0] == 0) {
  62.     abort:
  63.     llptr=llbuf; return;
  64.   }
  65.   if(llptr<=llbuf) goto abort;
  66.   if(strstr(fname,"..")!=NULL) goto abort;
  67.   for(pp=fname; *pp; pp++) if(*pp<'-' || *pp>'z' || strchr(":;=<>?\\^[]",*pp)!=NULL) goto abort;
  68.   snprintf(namebuf,sizeof(namebuf),"%s/%s",logd,fname);
  69.   pp=strrchr(namebuf,'/'); if(pp>namebuf+strlen(logd)) {
  70.     *pp=0; mkdirs(namebuf); *pp='/';
  71.   }
  72.   f=fopen(namebuf,"a"); if(f==NULL) goto abort;
  73.   l2=ftell(f);
  74.   fwrite(llbuf,1,llptr-llbuf,f); l=ftell(f); fclose(f); llptr=llbuf;
  75.   if(l2==0) chmod(namebuf,S_IRUSR|S_IWUSR);
  76.   if(strcmp(fname,"access.log")==0 || strcmp(fname,"referer.log")==0 ||
  77.       strcmp(fname,"session.log")==0 || strcmp(fname,"post.log")==0 ||
  78.       strcmp(fname,"user_error.log")==0 ||
  79.       strncmp(fname,"classes/",8)==0)
  80.     lim=GEN_LOG_LIMIT;
  81.   else lim=MODULE_LOG_LIMIT;
  82.   if(l>=lim) {
  83.     for(i=OLD_LOG_FILES-1;i>0;i--) {
  84.       snprintf(b3,sizeof(b3),"%s.old%d",namebuf,i);
  85.       snprintf(b4,sizeof(b4),"%s.old%d",namebuf,i+1);
  86.       if(stat(b3,&st)==0) rename(b3,b4);
  87.     }
  88.     if(OLD_LOG_FILES>0) {
  89.       snprintf(b3,sizeof(b3),"%s.old1",namebuf);
  90.       rename(namebuf,b3);
  91.     }
  92.     else unlink(namebuf);
  93.   }
  94. }
  95.  
  96. void dispatch_log(void)
  97. {
  98.   size_t l, l2;
  99.   pid_t pid;
  100.   FILE *f;
  101.   char *p1,*p2;
  102.   char namebuf[1024];
  103.   int i;
  104.   #define TEMP_LOG_2 "log/temp-2.log"
  105.  
  106.   fflush(NULL);
  107.   pid=fork(); if(pid>0) {addfork(pid,1); return;}
  108.   close(commsock);
  109.   call_sh(1,"ls %s* 2>/dev/null >/dev/null || exit;\n\
  110.  for f in %s*; do mv $f $f.bb; done;\n\
  111. sleep 1;\n\
  112. cat %s*.bb >%s;\n\
  113. rm -f %s*.bb",
  114.       &TEMP_LOG_FILE[3],&TEMP_LOG_FILE[3],&TEMP_LOG_FILE[3],TEMP_LOG_2,&TEMP_LOG_FILE[3]);
  115.   f=fopen(TEMP_LOG_2,"r"); if(f==NULL) exit(0);
  116.   fseek(f,0,SEEK_END); l=ftell(f);
  117.   if(l<=0)
  118.     {fclose(f); unlink(TEMP_LOG_2); exit(0);}
  119.   logbuf=xmalloc(l+16); fseek(f,0,SEEK_SET);
  120.   l2=fread(logbuf,1,l,f);fclose(f); unlink(TEMP_LOG_2);
  121.   if(l2!=l) {free(logbuf); exit(0);}
  122.   logbuf[l2]=0;
  123.   for(loglinecnt=0,p1=logbuf;loglinecnt<MAX_LOGLINES && *p1;p1=p2) {
  124.     p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
  125.     p1=find_word_start(p1); if(*p1==0) continue;
  126.     linetab[loglinecnt++]=p1;
  127.   }
  128.   qsort(linetab,loglinecnt,sizeof(linetab[0]),llcmp);
  129.   namebuf[0]=0; llptr=llbuf;
  130.   for(i=0;i<loglinecnt;i++) {
  131.     p1=linetab[i]; p2=find_word_end(p1); *p2++=0;
  132.     if(strcmp(namebuf,p1)!=0) {
  133.       onelogfile(namebuf); snprintf(namebuf,sizeof(namebuf),"%s",p1);
  134.     }
  135.     if(strlen(p2)>=sizeof(llbuf)-(llptr-llbuf)-2) onelogfile(namebuf);
  136.     snprintf(llptr,sizeof(llbuf)-(llptr-llbuf),"%s\n",p2);
  137.     llptr+=strlen(llptr);
  138.   }
  139.   if(namebuf[0]) onelogfile(namebuf);
  140.   free(logbuf);
  141.   exit(0);
  142. }
  143.