Subversion Repositories wimsdev

Rev

Rev 8342 | Rev 11125 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8342 Rev 8849
Line 25... Line 25...
25
char pidstr[32];
25
char pidstr[32];
26
char keepdate[32]="0";
26
char keepdate[32]="0";
27
char mupdate[32]="0";
27
char mupdate[32]="0";
28
char backdate[32]="0";
28
char backdate[32]="0";
29
char loadavg[MAX_LINELEN+1];
29
char loadavg[MAX_LINELEN+1];
30
char qbuf[MAX_LINELEN+1];       /* quota buffer */
30
char qbuf[MAX_LINELEN+1];       /* quota buffer */
31
time_t nowtime, starttime, lastcleantime=0;
31
time_t nowtime, starttime, lastcleantime=0;
32
time_t thismin, lastmin, startmin;
32
time_t thismin, lastmin, startmin;
33
struct tm *now;
33
struct tm *now;
34
int nowsec, nowmin, nowhr, nowday, nowwday,nowmon,nowyear;
34
int nowsec, nowmin, nowhr, nowday, nowwday,nowmon,nowyear;
35
int startdate;
35
int startdate;
36
char nowstr[64];
36
char nowstr[64];
37
pid_t mypid;
37
pid_t mypid;
38
int idle_time=5000;
38
int idle_time=5000;
39
int idle_time2=5000;
39
int idle_time2=5000;
40
int idle_time3=5000;
40
int idle_time3=5000;
41
int anti_time=3600*24;  /* antidate tolerance */
41
int anti_time=3600*24;      /* antidate tolerance */
42
int OLD_LOG_FILES=2;
42
int OLD_LOG_FILES=2;
43
int GEN_LOG_LIMIT=1024000;
43
int GEN_LOG_LIMIT=1024000;
44
int MODULE_LOG_LIMIT=102400;
44
int MODULE_LOG_LIMIT=102400;
45
int backup_hour=-1;
45
int backup_hour=-1;
46
int site_accounting=0;
46
int site_accounting=0;
47
int modupdatetime=0;
47
int modupdatetime=0;
48
int rshift;     /* shift minute start */
48
int rshift;      /* shift minute start */
49
int commsock;
49
int commsock;
50
int answerlen;
50
int answerlen;
51
int debugging;
51
int debugging;
52
char ipbuf[64];
52
char ipbuf[64];
53
char nodeipbuf[MAX_LINELEN+1];
53
char nodeipbuf[MAX_LINELEN+1];
Line 85... Line 85...
85
    nowsec=now->tm_sec;
85
    nowsec=now->tm_sec;
86
    nowmin=now->tm_min; nowhr=now->tm_hour;
86
    nowmin=now->tm_min; nowhr=now->tm_hour;
87
    nowday=now->tm_mday; nowwday=now->tm_wday;
87
    nowday=now->tm_mday; nowwday=now->tm_wday;
88
    nowmon=now->tm_mon+1; nowyear=now->tm_year+1900;
88
    nowmon=now->tm_mon+1; nowyear=now->tm_year+1900;
89
    snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d",
89
    snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d",
90
             nowyear,nowmon,nowday,nowhr,nowmin,nowsec);
90
           nowyear,nowmon,nowday,nowhr,nowmin,nowsec);
91
}
91
}
92
 
92
 
93
void parms(void)
93
void parms(void)
94
{
94
{
95
    char *p, *p1, *p2, *parm[16];
95
    char *p, *p1, *p2, *parm[16];
96
    char buf[16];
96
    char buf[16];
97
    int t,r;
97
    int t,r;
98
    p=getenv("wimslogd");
98
    p=getenv("wimslogd");
99
    if(p==NULL || *p==0) return;
99
    if(p==NULL || *p==0) return;
100
    for(t=0, p1=find_word_start(p); *p1; p1=find_word_start(p2)) {
100
    for(t=0, p1=find_word_start(p); *p1; p1=find_word_start(p2)) {
101
        p2=find_word_end(p1); if(*p2) *p2++=0;
101
      p2=find_word_end(p1); if(*p2) *p2++=0;
102
        parm[t++]=p1;
102
      parm[t++]=p1;
103
    }
103
    }
104
    idle_time=atoi(parm[0]); if(idle_time<=10) idle_time=5000;
104
    idle_time=atoi(parm[0]); if(idle_time<=10) idle_time=5000;
105
    idle_time2=atoi(parm[1]); if(idle_time2<=10) idle_time2=idle_time;
105
    idle_time2=atoi(parm[1]); if(idle_time2<=10) idle_time2=idle_time;
106
    idle_time3=atoi(parm[2]); if(idle_time3<=10) idle_time3=idle_time2;
106
    idle_time3=atoi(parm[2]); if(idle_time3<=10) idle_time3=idle_time2;
107
    if(idle_time2>idle_time) idle_time2=idle_time;
107
    if(idle_time2>idle_time) idle_time2=idle_time;
Line 141... Line 141...
141
    (void)freopen("../tmp/log/wimslogd.err","w",stderr);
141
    (void)freopen("../tmp/log/wimslogd.err","w",stderr);
142
/*    mfd=shm_open(SHM_NAME,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
142
/*    mfd=shm_open(SHM_NAME,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
143
    write(mfd,buf,SHM_SIZE);
143
    write(mfd,buf,SHM_SIZE);
144
    shmptr=mmap(0,SHM_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,mfd,0);
144
    shmptr=mmap(0,SHM_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,mfd,0);
145
    if(shmptr==MAP_FAILED) {
145
    if(shmptr==MAP_FAILED) {
146
        fprintf(stderr,"wimslogd: mmap() failure. %s\n",
146
      fprintf(stderr,"wimslogd: mmap() failure. %s\n",
147
                strerror(errno));
147
            strerror(errno));
148
        exit(1);
148
      exit(1);
149
    }
149
    }
150
*/
150
*/
151
    verify_tables();
151
    verify_tables();
152
    init_random();
152
    init_random();
153
    modupdatetime=(double) random()*350/RAND_MAX;
153
    modupdatetime=(double) random()*350/RAND_MAX;
154
    rshift=(double) random()*MINLENGTH/RAND_MAX;
154
    rshift=(double) random()*MINLENGTH/RAND_MAX;
155
    parms();
155
    parms();
156
    if(getcwd(cwd,sizeof(cwd))==NULL) { /* directory missing */
156
    if(getcwd(cwd,sizeof(cwd))==NULL) {      /* directory missing */
157
        fprintf(stderr,"wimslogd: getcwd() failure. %s\n",
157
      fprintf(stderr,"wimslogd: getcwd() failure. %s\n",
158
                strerror(errno));
158
            strerror(errno));
159
        return 1;
159
      return 1;
160
    }
160
    }
161
    p=strstr(cwd,"/public_html");
161
    p=strstr(cwd,"/public_html");
162
    if(p!=NULL && *(p+strlen("/public_html"))==0) {
162
    if(p!=NULL && *(p+strlen("/public_html"))==0) {
163
        *p=0; if(chdir(cwd)<0) {        /* strong error */
163
      *p=0; if(chdir(cwd)<0) {      /* strong error */
164
            fprintf(stderr,"wimslogd: Unable to change directory. %s\n",
164
          fprintf(stderr,"wimslogd: Unable to change directory. %s\n",
165
                    strerror(errno));
165
                strerror(errno));
166
            return 1;
166
          return 1;
167
        }
167
      }
168
    }
168
    }
169
    opensock();
169
    opensock();
170
    mypid=getpid();
170
    mypid=getpid();
171
    myid=geteuid(); setreuid(myid,myid);
171
    myid=geteuid(); setreuid(myid,myid);
172
    myid=getegid(); setregid(myid,myid);
172
    myid=getegid(); setregid(myid,myid);
Line 177... Line 177...
177
    fflush(NULL);
177
    fflush(NULL);
178
    starttime=nowtime; startmin=lastmin=thismin;
178
    starttime=nowtime; startmin=lastmin=thismin;
179
    wlogdaccessfile(qbuf,"r","log/cquota/lim.host");
179
    wlogdaccessfile(qbuf,"r","log/cquota/lim.host");
180
    wlogdaccessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf));
180
    wlogdaccessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf));
181
    wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then it is a cluster child */
181
    wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then it is a cluster child */
182
    if(strstr(buf,"yes")!=NULL) {       /* register my real IP */
182
    if(strstr(buf,"yes")!=NULL) {      /* register my real IP */
183
        wlogdaccessfile(nodeipbuf,"r","/etc/myip");
183
      wlogdaccessfile(nodeipbuf,"r","/etc/myip");
184
        wlogdaccessfile(nodeipbuf,"w","tmp/log/myip");
184
      wlogdaccessfile(nodeipbuf,"w","tmp/log/myip");
185
    }
185
    }
186
    do {
186
    do {
187
        fd_set rset;
187
      fd_set rset;
188
        struct timeval tv;
188
      struct timeval tv;
189
        int t, selectcnt;
189
      int t, selectcnt;
190
 
190
 
191
        if(getpid()!=mypid) return 0;   /* leaked child */
191
      if(getpid()!=mypid) return 0;      /* leaked child */
192
        if(stat(debugfile,&st)==0 && st.st_size<MAX_DEBUGLENGTH) debugging=1;
192
      if(stat(debugfile,&st)==0 && st.st_size<MAX_DEBUGLENGTH) debugging=1;
193
        else debugging=0;
193
      else debugging=0;
194
        wlogdaccessfile(loadavg,"r","/proc/loadavg");
194
      wlogdaccessfile(loadavg,"r","/proc/loadavg");
195
        for(selectcnt=0; selectcnt<100; selectcnt++) {
195
      for(selectcnt=0; selectcnt<100; selectcnt++) {
196
            tv.tv_sec=0; tv.tv_usec=50000; /* a pause every 50 ms. */
196
          tv.tv_sec=0; tv.tv_usec=50000; /* a pause every 50 ms. */
197
            FD_ZERO(&rset); FD_SET(commsock,&rset);
197
          FD_ZERO(&rset); FD_SET(commsock,&rset);
198
            t=select(commsock+1,&rset,NULL,NULL,&tv);
198
          t=select(commsock+1,&rset,NULL,NULL,&tv);
199
            if(t==0) {forkman(0); continue;}
199
          if(t==0) {forkman(0); continue;}
200
            if(t<0) {wimslogd_error("select() error."); continue;}
200
          if(t<0) {wimslogd_error("select() error."); continue;}
201
            rsock=accept(commsock,NULL,NULL);
201
          rsock=accept(commsock,NULL,NULL);
202
            if(rsock==-1) {wimslogd_error("accept() error."); continue;}
202
          if(rsock==-1) {wimslogd_error("accept() error."); continue;}
203
            answer(rsock);
203
          answer(rsock);
204
        }
204
      }
205
        forkman(1);
205
      forkman(1);
206
        getnow();
206
      getnow();
207
        if(thismin==lastmin) continue;
207
      if(thismin==lastmin) continue;
208
        mincnt++; /* if(mincnt>MAX_MIN) return 0; Refreshment. */
208
      mincnt++; /* if(mincnt>MAX_MIN) return 0; Refreshment. */
209
        if(nowday!=startdate) return 0; /* Daily refreshment. */
209
      if(nowday!=startdate) return 0; /* Daily refreshment. */
210
        lastmin=thismin;
210
      lastmin=thismin;
211
        wlogdaccessfile(buf,"r",pidfile); strip_trailing_spaces(buf);
211
        wlogdaccessfile(buf,"r",pidfile); strip_trailing_spaces(buf);
212
        if(strcmp(buf,pidstr)!=0) {     /* wrong pid: abandon. */
212
      if(strcmp(buf,pidstr)!=0) {      /* wrong pid: abandon. */
213
            wait_children();
213
          wait_children();
214
            return 0;
214
          return 0;
215
        }
215
      }
216
 
216
 
217
        if(getpid()!=mypid) return 0;   /* leaked child */
217
      if(getpid()!=mypid) return 0;      /* leaked child */
218
        wlogdaccessfile(qbuf,"r","log/cquota/lim.host");
218
      wlogdaccessfile(qbuf,"r","log/cquota/lim.host");
219
        wlogdaccessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf));
219
      wlogdaccessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf));
220
        cleancache();
220
      cleancache();
221
        if((thismin%127)==6) homedir(); /* update home directory setup */
221
      if((thismin%127)==6) homedir();      /* update home directory setup */
222
        wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then no housekeeping */
222
      wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then no housekeeping */
223
        if(strstr(buf,"yes")==NULL) {
223
      if(strstr(buf,"yes")==NULL) {
224
            dispatch_log();
224
          dispatch_log();
225
            if((thismin%2)==1) local();
225
          if((thismin%2)==1) local();
226
            /* if((thismin%9)==0) */ cleaning(1);       /* clean up session directories */
226
          /* if((thismin%9)==0) */ cleaning(1);       /* clean up session directories */
227
            if((thismin%5)==0 && nowmin>15) housekeep();        /* daily housekeeping */
227
          if((thismin%5)==0 && nowmin>15) housekeep();      /* daily housekeeping */
228
            if(getpid()!=mypid) return 0;       /* leaked child */
228
          if(getpid()!=mypid) return 0;      /* leaked child */
229
            if(nowhr*60+nowmin>modupdatetime && (thismin%17)==11) modupdate();
229
          if(nowhr*60+nowmin>modupdatetime && (thismin%17)==11) modupdate();
230
            if(backup_hour>0 && backup_hour<23 && (thismin%17)==3 && nowhr>=backup_hour)
230
          if(backup_hour>0 && backup_hour<23 && (thismin%17)==3 && nowhr>=backup_hour)
231
              backup(); /* daily backup */
231
            backup();      /* daily backup */
232
            fflush(NULL);
232
          fflush(NULL);
233
            logexec();
233
          logexec();
234
        }
234
      }
235
        else {  /* cluster child */
235
      else {      /* cluster child */
236
            if((thismin%9)==0) cleaning(0);     /* clean up session directories */
236
          if((thismin%9)==0) cleaning(0);       /* clean up session directories */
237
        }
237
      }
238
    }
238
    }
239
    while(1==1);
239
    while(1==1);
240
    return 0;
240
    return 0;
241
}
241
}
242
 
242