Subversion Repositories wimsdev

Rev

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