Back to index

wims  3.65+svn20090927
log.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017        /* Routines to write log files. */
00018 
00019 char llbuf[4*MAX_LINELEN+4], *llptr;
00020 char *linetab[MAX_LOGLINES];
00021 int loglinecnt;
00022 char *logbuf, *logfname;
00023 
00024 int llcmp(const void *c1, const void *c2)
00025 {
00026     char **p1,**p2;
00027     p1=(char **) c1; p2=(char **) c2;
00028     return strcmp(*p1,*p2);
00029 }
00030 
00031 void onelogfile(char *fname)
00032 {
00033     char namebuf[1024], *pp;
00034     FILE *f;
00035     size_t l, l2;
00036     struct stat st;
00037     int lim;
00038 
00039     if(fname[0] == 0) {
00040 abort:
00041        llptr=llbuf; return;
00042     }
00043     if(llptr<=llbuf) goto abort;
00044     if(strstr(fname,"..")!=NULL) goto abort;
00045     for(pp=fname; *pp; pp++) if(*pp<'-' || *pp>'z' || strchr(":;=<>?\\^[]",*pp)!=NULL) goto abort;
00046     snprintf(namebuf,sizeof(namebuf),"%s/%s",logd,fname);
00047     pp=strrchr(namebuf,'/'); if(pp>namebuf+strlen(logd)) {
00048        *pp=0; mkdirs(namebuf); *pp='/';
00049     }
00050     f=fopen(namebuf,"a"); if(f==NULL) goto abort;
00051     l2=ftell(f);
00052     fwrite(llbuf,1,llptr-llbuf,f); l=ftell(f); fclose(f); llptr=llbuf;
00053     if(l2==0) chmod(namebuf,S_IRUSR|S_IWUSR);
00054     if(strcmp(fname,"access.log")==0 || strcmp(fname,"referer.log")==0 ||
00055        strcmp(fname,"session.log")==0 || strcmp(fname,"post.log")==0 ||
00056        strcmp(fname,"user_error.log")==0 ||
00057        strncmp(fname,"classes/",8)==0) lim=GEN_LOG_LIMIT;
00058     else lim=MODULE_LOG_LIMIT;
00059     if(l>=lim) {
00060        char b3[1024], b4[1024];
00061        int i;
00062        for(i=OLD_LOG_FILES-1;i>0;i--) {
00063            snprintf(b3,sizeof(b3),"%s.old%d",namebuf,i);
00064            snprintf(b4,sizeof(b4),"%s.old%d",namebuf,i+1);
00065            if(stat(b3,&st)==0) rename(b3,b4);
00066        }
00067        if(OLD_LOG_FILES>0) {
00068            snprintf(b3,sizeof(b3),"%s.old1",namebuf);
00069            rename(namebuf,b3);
00070        }
00071        else unlink(namebuf);
00072     }
00073 }
00074 
00075 void dispatch_log(void)
00076 {
00077     size_t l, l2;
00078     pid_t pid;
00079     FILE *f;
00080     char *p1,*p2;
00081     char namebuf[1024];
00082     int i;
00083     #define TEMP_LOG_2 "log/temp-2.log"
00084 
00085     fflush(NULL);
00086     pid=fork(); if(pid>0) {addfork(pid,1); return;}
00087     close(commsock);
00088     call_sh(1,"for f in %s*; do mv $f $f.bb 2>/dev/null; done;\n\
00089 sleep 1;\n\
00090 cat %s*.bb >%s 2>/dev/null;\n\
00091 rm -f %s*.bb",
00092            TEMP_LOG_FILE+3,TEMP_LOG_FILE+3,TEMP_LOG_2,TEMP_LOG_FILE+3);
00093     f=fopen(TEMP_LOG_2,"r"); if(f==NULL) exit(0);
00094     fseek(f,0,SEEK_END); l=ftell(f); if(l<=0)
00095        {fclose(f); unlink(TEMP_LOG_2); exit(0);}
00096     logbuf=xmalloc(l+16); fseek(f,0,SEEK_SET);
00097     l2=fread(logbuf,1,l,f);fclose(f); unlink(TEMP_LOG_2);
00098     if(l2!=l) {free(logbuf); exit(0);}
00099     logbuf[l2]=0;
00100     for(loglinecnt=0,p1=logbuf;loglinecnt<MAX_LOGLINES && *p1;p1=p2) {
00101        p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
00102        p1=find_word_start(p1); if(*p1==0) continue;
00103        linetab[loglinecnt++]=p1;
00104     }
00105     qsort(linetab,loglinecnt,sizeof(linetab[0]),llcmp);
00106     namebuf[0]=0; llptr=llbuf;
00107     for(i=0;i<loglinecnt;i++) {
00108        p1=linetab[i]; p2=find_word_end(p1); *p2++=0;
00109        if(strcmp(namebuf,p1)!=0) {
00110            onelogfile(namebuf); snprintf(namebuf,sizeof(namebuf),"%s",p1);
00111        }
00112        if(strlen(p2)>=sizeof(llbuf)-(llptr-llbuf)-2) onelogfile(namebuf);
00113        snprintf(llptr,sizeof(llbuf)-(llptr-llbuf),"%s\n",p2);
00114        llptr+=strlen(llptr);
00115     }
00116     if(namebuf[0]) onelogfile(namebuf);
00117     free(logbuf); 
00118     exit(0);
00119 }
00120