Subversion Repositories wimsdev

Rev

Rev 8849 | Rev 12474 | Go to most recent revision | 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
 */
8155 bpr 17
/* Routines to write log files. */
10 reyssat 18
 
8185 bpr 19
#include "wimslogd.h"
20
 
10 reyssat 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], *pp;
36
    FILE *f;
37
    size_t l, l2;
38
    struct stat st;
39
    int lim;
40
 
41
    if(fname[0] == 0) {
42
abort:
8849 bpr 43
      llptr=llbuf; return;
10 reyssat 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)) {
8849 bpr 50
      *pp=0; mkdirs(namebuf); *pp='/';
10 reyssat 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) lim=GEN_LOG_LIMIT;
60
    else lim=MODULE_LOG_LIMIT;
61
    if(l>=lim) {
8849 bpr 62
      char b3[1024], b4[1024];
63
      int i;
64
      for(i=OLD_LOG_FILES-1;i>0;i--) {
65
          snprintf(b3,sizeof(b3),"%s.old%d",namebuf,i);
66
          snprintf(b4,sizeof(b4),"%s.old%d",namebuf,i+1);
67
          if(stat(b3,&st)==0) rename(b3,b4);
68
      }
69
      if(OLD_LOG_FILES>0) {
70
          snprintf(b3,sizeof(b3),"%s.old1",namebuf);
71
          rename(namebuf,b3);
72
      }
73
      else unlink(namebuf);
10 reyssat 74
    }
75
}
76
 
77
void dispatch_log(void)
78
{
79
    size_t l, l2;
80
    pid_t pid;
81
    FILE *f;
82
    char *p1,*p2;
83
    char namebuf[1024];
84
    int i;
85
    #define TEMP_LOG_2 "log/temp-2.log"
86
 
87
    fflush(NULL);
88
    pid=fork(); if(pid>0) {addfork(pid,1); return;}
89
    close(commsock);
11173 bpr 90
    call_sh(1,"ls %s* 2>/dev/null >/dev/null || exit;\n\
91
    for f in %s*; do mv $f $f.bb; done;\n\
10 reyssat 92
sleep 1;\n\
11173 bpr 93
cat %s*.bb >%s;\n\
10 reyssat 94
rm -f %s*.bb",
11173 bpr 95
          TEMP_LOG_FILE+3,TEMP_LOG_FILE+3,TEMP_LOG_FILE+3,TEMP_LOG_2,TEMP_LOG_FILE+3);
10 reyssat 96
    f=fopen(TEMP_LOG_2,"r"); if(f==NULL) exit(0);
97
    fseek(f,0,SEEK_END); l=ftell(f); if(l<=0)
8849 bpr 98
      {fclose(f); unlink(TEMP_LOG_2); exit(0);}
10 reyssat 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) {
8849 bpr 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;
10 reyssat 107
    }
108
    qsort(linetab,loglinecnt,sizeof(linetab[0]),llcmp);
109
    namebuf[0]=0; llptr=llbuf;
110
    for(i=0;i<loglinecnt;i++) {
8849 bpr 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);
10 reyssat 118
    }
119
    if(namebuf[0]) onelogfile(namebuf);
8155 bpr 120
    free(logbuf);
10 reyssat 121
    exit(0);
122
}
123