Subversion Repositories wimsdev

Rev

Rev 15487 | 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.  
  33. void onelogfile(char *fname)
  34. {
  35.   char namebuf[1024], b3[1024], b4[1024], *pp;
  36.   FILE *f;
  37.   size_t l, l2;
  38.   struct stat st;
  39.   int i, lim;
  40.  
  41.   if(fname[0] == 0) {
  42.     abort:
  43.     llptr=llbuf; return;
  44.   }
  45.   if(llptr<=llbuf) goto abort;
  46.   if(strstr(fname,"..")!=NULL) goto abort;
  47.   for(pp=fname; *pp; pp++) if(*pp<'-' || *pp>'z' || strchr(":;=<>?\\^[]",*pp)!=NULL) goto abort;
  48.   snprintf(namebuf,sizeof(namebuf),"%s/%s",logd,fname);
  49.   pp=strrchr(namebuf,'/'); if(pp>namebuf+strlen(logd)) {
  50.     *pp=0; mkdirs(namebuf); *pp='/';
  51.   }
  52.   f=fopen(namebuf,"a"); if(f==NULL) goto abort;
  53.   l2=ftell(f);
  54.   fwrite(llbuf,1,llptr-llbuf,f); l=ftell(f); fclose(f); llptr=llbuf;
  55.   if(l2==0) chmod(namebuf,S_IRUSR|S_IWUSR);
  56.   if(strcmp(fname,"access.log")==0 || strcmp(fname,"referer.log")==0 ||
  57.       strcmp(fname,"session.log")==0 || strcmp(fname,"post.log")==0 ||
  58.       strcmp(fname,"user_error.log")==0 ||
  59.       strncmp(fname,"classes/",8)==0)
  60.     lim=GEN_LOG_LIMIT;
  61.   else lim=MODULE_LOG_LIMIT;
  62.   if(l>=lim) {
  63.     for(i=OLD_LOG_FILES-1;i>0;i--) {
  64.       snprintf(b3,sizeof(b3),"%s.old%d",namebuf,i);
  65.       snprintf(b4,sizeof(b4),"%s.old%d",namebuf,i+1);
  66.       if(stat(b3,&st)==0) rename(b3,b4);
  67.     }
  68.     if(OLD_LOG_FILES>0) {
  69.       snprintf(b3,sizeof(b3),"%s.old1",namebuf);
  70.       rename(namebuf,b3);
  71.     }
  72.     else unlink(namebuf);
  73.   }
  74. }
  75.  
  76. void dispatch_log(void)
  77. {
  78.   size_t l, l2;
  79.   pid_t pid;
  80.   FILE *f;
  81.   char *p1,*p2;
  82.   char namebuf[1024];
  83.   int i;
  84.   #define TEMP_LOG_2 "log/temp-2.log"
  85.  
  86.   fflush(NULL);
  87.   pid=fork(); if(pid>0) {addfork(pid,1); return;}
  88.   close(commsock);
  89.   call_sh(1,"ls %s* 2>/dev/null >/dev/null || exit;\n\
  90.  for f in %s*; do mv $f $f.bb; done;\n\
  91. sleep 1;\n\
  92. cat %s*.bb >%s;\n\
  93. rm -f %s*.bb",
  94.       &TEMP_LOG_FILE[3],&TEMP_LOG_FILE[3],&TEMP_LOG_FILE[3],TEMP_LOG_2,&TEMP_LOG_FILE[3]);
  95.   f=fopen(TEMP_LOG_2,"r"); if(f==NULL) exit(0);
  96.   fseek(f,0,SEEK_END); l=ftell(f);
  97.   if(l<=0)
  98.     {fclose(f); unlink(TEMP_LOG_2); exit(0);}
  99.   logbuf=xmalloc(l+16); fseek(f,0,SEEK_SET);
  100.   l2=fread(logbuf,1,l,f);fclose(f); unlink(TEMP_LOG_2);
  101.   if(l2!=l) {free(logbuf); exit(0);}
  102.   logbuf[l2]=0;
  103.   for(loglinecnt=0,p1=logbuf;loglinecnt<MAX_LOGLINES && *p1;p1=p2) {
  104.     p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
  105.     p1=find_word_start(p1); if(*p1==0) continue;
  106.     linetab[loglinecnt++]=p1;
  107.   }
  108.   qsort(linetab,loglinecnt,sizeof(linetab[0]),llcmp);
  109.   namebuf[0]=0; llptr=llbuf;
  110.   for(i=0;i<loglinecnt;i++) {
  111.     p1=linetab[i]; p2=find_word_end(p1); *p2++=0;
  112.     if(strcmp(namebuf,p1)!=0) {
  113.       onelogfile(namebuf); snprintf(namebuf,sizeof(namebuf),"%s",p1);
  114.     }
  115.     if(strlen(p2)>=sizeof(llbuf)-(llptr-llbuf)-2) onelogfile(namebuf);
  116.     snprintf(llptr,sizeof(llbuf)-(llptr-llbuf),"%s\n",p2);
  117.     llptr+=strlen(llptr);
  118.   }
  119.   if(namebuf[0]) onelogfile(namebuf);
  120.   free(logbuf);
  121.   exit(0);
  122. }
  123.