Rev 15519 | Rev 15847 | 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 | */ |
||
17 | |||
18 | /* WIMS log daemon */ |
||
19 | |||
20 | #include "wimslogd.h" |
||
15519 | bpr | 21 | /* variables shared with wims.c */ |
22 | #include "../commun.h" |
||
10 | reyssat | 23 | extern char **environ; |
24 | |||
25 | char cwd[MAX_FNAME+1]; |
||
26 | char pidstr[32]; |
||
27 | char keepdate[32]="0"; |
||
28 | char mupdate[32]="0"; |
||
29 | char backdate[32]="0"; |
||
30 | char loadavg[MAX_LINELEN+1]; |
||
8849 | bpr | 31 | char qbuf[MAX_LINELEN+1]; /* quota buffer */ |
10 | reyssat | 32 | time_t nowtime, starttime, lastcleantime=0; |
33 | time_t thismin, lastmin, startmin; |
||
34 | struct tm *now; |
||
35 | int nowsec, nowmin, nowhr, nowday, nowwday,nowmon,nowyear; |
||
36 | int startdate; |
||
37 | char nowstr[64]; |
||
38 | pid_t mypid; |
||
8849 | bpr | 39 | int anti_time=3600*24; /* antidate tolerance */ |
10 | reyssat | 40 | int backup_hour=-1; |
41 | int site_accounting=0; |
||
42 | int modupdatetime=0; |
||
8849 | bpr | 43 | int rshift; /* shift minute start */ |
10 | reyssat | 44 | int commsock; |
45 | int answerlen; |
||
46 | int debugging; |
||
47 | char ipbuf[64]; |
||
48 | char nodeipbuf[MAX_LINELEN+1]; |
||
49 | char commbuf[BUFFERLEN+1]; |
||
50 | #define textbuf (commbuf+sizeof(int)) |
||
51 | char *textptr; |
||
52 | |||
53 | int cwdtype; |
||
54 | |||
8155 | bpr | 55 | /* check whether there is anything to execute */ |
10 | reyssat | 56 | void logexec(void) |
57 | { |
||
12472 | bpr | 58 | struct stat st; |
59 | pid_t pid; |
||
60 | if(stat("log/wimslogd.exec",&st)) return; |
||
61 | fflush(NULL); |
||
62 | pid=fork(); if(pid>0) {addfork(pid,1); return;} |
||
63 | close(commsock); msleep(100); |
||
64 | call_ssh(1,"sh log/wimslogd.exec >tmp/log/wimslogdexec.out 2>tmp/log/wimslogdexec.err"); |
||
65 | unlink("log/wimslogd.exec"); exit(0); |
||
10 | reyssat | 66 | } |
67 | |||
68 | void local(void) |
||
69 | { |
||
12472 | bpr | 70 | struct stat st; |
71 | if(stat("log/wimslogd.local",&st)) return; |
||
72 | if(!(S_IXUSR&st.st_mode)) return; |
||
73 | call_ssh(0,"sh log/wimslogd.local"); |
||
10 | reyssat | 74 | } |
75 | |||
76 | void getnow(void) |
||
77 | { |
||
12472 | bpr | 78 | nowtime=time(NULL); thismin=(nowtime-rshift)/MINLENGTH; |
79 | now=localtime(&nowtime); |
||
80 | nowsec=now->tm_sec; |
||
81 | nowmin=now->tm_min; nowhr=now->tm_hour; |
||
82 | nowday=now->tm_mday; nowwday=now->tm_wday; |
||
83 | nowmon=now->tm_mon+1; nowyear=now->tm_year+1900; |
||
84 | snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d", |
||
85 | nowyear,nowmon,nowday,nowhr,nowmin,nowsec); |
||
10 | reyssat | 86 | } |
87 | |||
88 | void parms(void) |
||
89 | { |
||
12472 | bpr | 90 | char *p, *p1, *p2, *parm[16]; |
91 | char buf[16]; |
||
92 | int t,r; |
||
93 | p=getenv("wimslogd"); |
||
94 | if(p==NULL || *p==0) return; |
||
95 | for(t=0, p1=find_word_start(p); *p1; p1=find_word_start(p2)) { |
||
96 | p2=find_word_end(p1); if(*p2) *p2++=0; |
||
97 | parm[t++]=p1; |
||
98 | } |
||
99 | idle_time=atoi(parm[0]); if(idle_time<=10) idle_time=5000; |
||
100 | idle_time2=atoi(parm[1]); if(idle_time2<=10) idle_time2=idle_time; |
||
101 | idle_time3=atoi(parm[2]); if(idle_time3<=10) idle_time3=idle_time2; |
||
102 | if(idle_time2>idle_time) idle_time2=idle_time; |
||
103 | if(idle_time3>idle_time2) idle_time3=idle_time2; |
||
104 | OLD_LOG_FILES=atoi(parm[3]); |
||
105 | if(OLD_LOG_FILES>100) OLD_LOG_FILES=100; |
||
15509 | bpr | 106 | LOG_DELETE=atoi(parm[9]); |
15551 | bpr | 107 | if(LOG_DELETE>1000) LOG_DELETE=1000; |
12472 | bpr | 108 | if(parm[4]) GEN_LOG_LIMIT=atoi(parm[4]); |
109 | if(parm[5]) MODULE_LOG_LIMIT=atoi(parm[5]); |
||
110 | if(parm[6]) backup_hour=atoi(parm[6]); |
||
111 | if(parm[7]) site_accounting=atoi(parm[7]); |
||
112 | if(parm[8]) r=atoi(parm[8])+1; else r=8; |
||
113 | if(r<2) r=2; |
||
114 | if(r>100) r=100; |
||
115 | snprintf(buf,sizeof(buf),"%d",r); setenv("examlog_lim2",buf,1); |
||
116 | if(site_accounting>0) setenv("site_accounting","yes",1); |
||
10 | reyssat | 117 | } |
118 | |||
8155 | bpr | 119 | /* This is run only when manually invoking the program. |
120 | * Verifies the orderedness of various list tables. |
||
121 | */ |
||
10 | reyssat | 122 | int verify_tables(void) |
123 | { |
||
12472 | bpr | 124 | if(verify_order(cmdlist,cmdcnt,sizeof(cmdlist[0]))) return -1; |
125 | return 0; |
||
10 | reyssat | 126 | } |
127 | |||
128 | int main(int argc, char *argv[]) |
||
129 | { |
||
12472 | bpr | 130 | char *p; |
131 | struct stat st; |
||
132 | uid_t myid; |
||
133 | int /*mfd,*/rsock,mincnt; |
||
134 | char buf[MAX_LINELEN+1]; |
||
135 | forkcnt=0; exec_wait=1; mincnt=0; |
||
136 | classcaches=sheetcaches=0; |
||
137 | (void)freopen("/dev/null","r",stdin); |
||
138 | (void)freopen("../tmp/log/wimslogd.out","w",stdout); |
||
139 | (void)freopen("../tmp/log/wimslogd.err","w",stderr); |
||
140 | /* mfd=shm_open(SHM_NAME,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR); |
||
10 | reyssat | 141 | write(mfd,buf,SHM_SIZE); |
142 | shmptr=mmap(0,SHM_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,mfd,0); |
||
143 | if(shmptr==MAP_FAILED) { |
||
8849 | bpr | 144 | fprintf(stderr,"wimslogd: mmap() failure. %s\n", |
145 | strerror(errno)); |
||
146 | exit(1); |
||
10 | reyssat | 147 | } |
12472 | bpr | 148 | */ |
149 | verify_tables(); |
||
150 | init_random(); |
||
151 | modupdatetime=(double) random()*350/RAND_MAX; |
||
152 | rshift=(double) random()*MINLENGTH/RAND_MAX; |
||
153 | parms(); |
||
154 | if(getcwd(cwd,sizeof(cwd))==NULL) { /* directory missing */ |
||
155 | fprintf(stderr,"wimslogd: getcwd() failure. %s\n", |
||
156 | strerror(errno)); |
||
157 | return 1; |
||
158 | } |
||
159 | p=strstr(cwd,"/public_html"); |
||
160 | if(p!=NULL && *(p+strlen("/public_html"))==0) { |
||
161 | *p=0; |
||
162 | if(chdir(cwd)<0) { /* strong error */ |
||
163 | fprintf(stderr,"wimslogd: Unable to change directory. %s\n", |
||
164 | strerror(errno)); |
||
8849 | bpr | 165 | return 1; |
10 | reyssat | 166 | } |
12472 | bpr | 167 | } |
168 | opensock(); |
||
169 | mypid=getpid(); |
||
170 | myid=geteuid(); setreuid(myid,myid); |
||
171 | myid=getegid(); setregid(myid,myid); |
||
172 | stat("/sysmask/notice/init-end",&st); |
||
173 | snprintf(pidstr,sizeof(pidstr),"%u",mypid); |
||
174 | getnow(); printf("wimslogd %s started at %s.\n",pidstr,nowstr); |
||
175 | startdate=nowday; |
||
176 | fflush(NULL); |
||
177 | starttime=nowtime; startmin=lastmin=thismin; |
||
178 | wlogdaccessfile(qbuf,"r","log/cquota/lim.host"); |
||
179 | wlogdaccessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf)); |
||
180 | wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then it is a cluster child */ |
||
181 | if(strstr(buf,"yes")!=NULL) { /* register my real IP */ |
||
182 | wlogdaccessfile(nodeipbuf,"r","/etc/myip"); |
||
183 | wlogdaccessfile(nodeipbuf,"w","tmp/log/myip"); |
||
184 | } |
||
185 | do { |
||
186 | fd_set rset; |
||
187 | struct timeval tv; |
||
188 | int t, selectcnt; |
||
189 | |||
190 | if(getpid()!=mypid) return 0; /* leaked child */ |
||
191 | if(stat(debugfile,&st)==0 && st.st_size<MAX_DEBUGLENGTH) debugging=1; |
||
192 | else debugging=0; |
||
193 | wlogdaccessfile(loadavg,"r","/proc/loadavg"); |
||
194 | for(selectcnt=0; selectcnt<100; selectcnt++) { |
||
195 | tv.tv_sec=0; tv.tv_usec=50000; /* a pause every 50 ms. */ |
||
196 | FD_ZERO(&rset); FD_SET(commsock,&rset); |
||
197 | t=select(commsock+1,&rset,NULL,NULL,&tv); |
||
198 | if(t==0) {forkman(0); continue;} |
||
199 | if(t<0) {wimslogd_error("select() error."); continue;} |
||
200 | rsock=accept(commsock,NULL,NULL); |
||
201 | if(rsock==-1) {wimslogd_error("accept() error."); continue;} |
||
202 | answer(rsock); |
||
10 | reyssat | 203 | } |
12472 | bpr | 204 | forkman(1); |
205 | getnow(); |
||
206 | if(thismin==lastmin) continue; |
||
207 | mincnt++; /* if(mincnt>MAX_MIN) return 0; Refreshment. */ |
||
208 | if(nowday!=startdate) return 0; /* Daily refreshment. */ |
||
209 | lastmin=thismin; |
||
210 | wlogdaccessfile(buf,"r",pidfile); strip_trailing_spaces(buf); |
||
211 | if(strcmp(buf,pidstr)!=0) { /* wrong pid: abandon. */ |
||
212 | wait_children(); |
||
213 | return 0; |
||
214 | } |
||
215 | |||
216 | if(getpid()!=mypid) return 0; /* leaked child */ |
||
8342 | bpr | 217 | wlogdaccessfile(qbuf,"r","log/cquota/lim.host"); |
218 | wlogdaccessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf)); |
||
12472 | bpr | 219 | cleancache(); |
220 | if((thismin%127)==6) homedir(); /* update home directory setup */ |
||
221 | wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then no housekeeping */ |
||
222 | if(strstr(buf,"yes")==NULL) { |
||
223 | dispatch_log(); |
||
224 | if((thismin%2)==1) local(); |
||
225 | /* if((thismin%9)==0) */ cleaning(1); /* clean up session directories */ |
||
226 | if((thismin%5)==0 && nowmin>15) housekeep(); /* daily housekeeping */ |
||
8849 | bpr | 227 | if(getpid()!=mypid) return 0; /* leaked child */ |
12472 | bpr | 228 | if(nowhr*60+nowmin>modupdatetime && (thismin%17)==11) modupdate(); |
229 | if(backup_hour>0 && backup_hour<23 && (thismin%17)==3 && nowhr>=backup_hour) |
||
230 | backup(); /* daily backup */ |
||
231 | fflush(NULL); |
||
232 | logexec(); |
||
10 | reyssat | 233 | } |
12472 | bpr | 234 | else { /* cluster child */ |
235 | if((thismin%9)==0) cleaning(0); /* clean up session directories */ |
||
236 | } |
||
237 | } |
||
238 | while(1==1); |
||
239 | return 0; |
||
10 | reyssat | 240 | } |
241 |