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