Rev 12474 | Rev 15487 | 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 | { |
||
12474 | bpr | 28 | char **p1,**p2; |
29 | p1=(char **) c1; p2=(char **) c2; |
||
30 | return strcmp(*p1,*p2); |
||
10 | reyssat | 31 | } |
32 | |||
33 | void onelogfile(char *fname) |
||
34 | { |
||
12474 | bpr | 35 | char namebuf[1024], *pp; |
36 | FILE *f; |
||
37 | size_t l, l2; |
||
38 | struct stat st; |
||
39 | int lim; |
||
10 | reyssat | 40 | |
12474 | bpr | 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 | char b3[1024], b4[1024]; |
||
64 | int i; |
||
65 | for(i=OLD_LOG_FILES-1;i>0;i--) { |
||
66 | snprintf(b3,sizeof(b3),"%s.old%d",namebuf,i); |
||
67 | snprintf(b4,sizeof(b4),"%s.old%d",namebuf,i+1); |
||
68 | if(stat(b3,&st)==0) rename(b3,b4); |
||
10 | reyssat | 69 | } |
12474 | bpr | 70 | if(OLD_LOG_FILES>0) { |
71 | snprintf(b3,sizeof(b3),"%s.old1",namebuf); |
||
72 | rename(namebuf,b3); |
||
10 | reyssat | 73 | } |
12474 | bpr | 74 | else unlink(namebuf); |
75 | } |
||
10 | reyssat | 76 | } |
77 | |||
78 | void dispatch_log(void) |
||
79 | { |
||
12474 | bpr | 80 | size_t l, l2; |
81 | pid_t pid; |
||
82 | FILE *f; |
||
83 | char *p1,*p2; |
||
84 | char namebuf[1024]; |
||
85 | int i; |
||
86 | #define TEMP_LOG_2 "log/temp-2.log" |
||
10 | reyssat | 87 | |
12474 | bpr | 88 | fflush(NULL); |
89 | pid=fork(); if(pid>0) {addfork(pid,1); return;} |
||
90 | close(commsock); |
||
91 | call_sh(1,"ls %s* 2>/dev/null >/dev/null || exit;\n\ |
||
92 | for f in %s*; do mv $f $f.bb; done;\n\ |
||
10 | reyssat | 93 | sleep 1;\n\ |
11173 | bpr | 94 | cat %s*.bb >%s;\n\ |
10 | reyssat | 95 | rm -f %s*.bb", |
15349 | bpr | 96 | &TEMP_LOG_FILE[3],&TEMP_LOG_FILE[3],&TEMP_LOG_FILE[3],TEMP_LOG_2,&TEMP_LOG_FILE[3]); |
12474 | bpr | 97 | f=fopen(TEMP_LOG_2,"r"); if(f==NULL) exit(0); |
98 | fseek(f,0,SEEK_END); l=ftell(f); |
||
99 | if(l<=0) |
||
100 | {fclose(f); unlink(TEMP_LOG_2); exit(0);} |
||
101 | logbuf=xmalloc(l+16); fseek(f,0,SEEK_SET); |
||
102 | l2=fread(logbuf,1,l,f);fclose(f); unlink(TEMP_LOG_2); |
||
103 | if(l2!=l) {free(logbuf); exit(0);} |
||
104 | logbuf[l2]=0; |
||
105 | for(loglinecnt=0,p1=logbuf;loglinecnt<MAX_LOGLINES && *p1;p1=p2) { |
||
106 | p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1); |
||
107 | p1=find_word_start(p1); if(*p1==0) continue; |
||
108 | linetab[loglinecnt++]=p1; |
||
109 | } |
||
110 | qsort(linetab,loglinecnt,sizeof(linetab[0]),llcmp); |
||
111 | namebuf[0]=0; llptr=llbuf; |
||
112 | for(i=0;i<loglinecnt;i++) { |
||
113 | p1=linetab[i]; p2=find_word_end(p1); *p2++=0; |
||
114 | if(strcmp(namebuf,p1)!=0) { |
||
115 | onelogfile(namebuf); snprintf(namebuf,sizeof(namebuf),"%s",p1); |
||
10 | reyssat | 116 | } |
12474 | bpr | 117 | if(strlen(p2)>=sizeof(llbuf)-(llptr-llbuf)-2) onelogfile(namebuf); |
118 | snprintf(llptr,sizeof(llbuf)-(llptr-llbuf),"%s\n",p2); |
||
119 | llptr+=strlen(llptr); |
||
120 | } |
||
121 | if(namebuf[0]) onelogfile(namebuf); |
||
122 | free(logbuf); |
||
123 | exit(0); |
||
10 | reyssat | 124 | } |