Subversion Repositories wimsdev

Rev

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
    struct stat st;
63
  struct stat st;
64
    pid_t pid;
64
  pid_t pid;
65
    if(stat("log/wimslogd.exec",&st)) return;
65
  if(stat("log/wimslogd.exec",&st)) return;
66
    fflush(NULL);
66
  fflush(NULL);
67
    pid=fork(); if(pid>0) {addfork(pid,1); return;}
67
  pid=fork(); if(pid>0) {addfork(pid,1); return;}
68
    close(commsock); msleep(100);
68
  close(commsock); msleep(100);
69
    call_ssh(1,"sh log/wimslogd.exec >tmp/log/wimslogdexec.out 2>tmp/log/wimslogdexec.err");
69
  call_ssh(1,"sh log/wimslogd.exec >tmp/log/wimslogdexec.out 2>tmp/log/wimslogdexec.err");
70
    unlink("log/wimslogd.exec"); exit(0);
70
  unlink("log/wimslogd.exec"); exit(0);
71
}
71
}
72
 
72
 
73
void local(void)
73
void local(void)
74
{
74
{
75
    struct stat st;
75
  struct stat st;
76
    if(stat("log/wimslogd.local",&st)) return;
76
  if(stat("log/wimslogd.local",&st)) return;
77
    if(!(S_IXUSR&st.st_mode)) return;
77
  if(!(S_IXUSR&st.st_mode)) return;
78
    call_ssh(0,"sh log/wimslogd.local");
78
  call_ssh(0,"sh log/wimslogd.local");
79
}
79
}
80
 
80
 
81
void getnow(void)
81
void getnow(void)
82
{
82
{
83
    nowtime=time(NULL); thismin=(nowtime-rshift)/MINLENGTH;
83
  nowtime=time(NULL); thismin=(nowtime-rshift)/MINLENGTH;
84
    now=localtime(&nowtime);
84
  now=localtime(&nowtime);
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;
108
    if(idle_time3>idle_time2) idle_time3=idle_time2;
108
  if(idle_time3>idle_time2) idle_time3=idle_time2;
109
    OLD_LOG_FILES=atoi(parm[3]);
109
  OLD_LOG_FILES=atoi(parm[3]);
110
    if(OLD_LOG_FILES>100) OLD_LOG_FILES=100;
110
  if(OLD_LOG_FILES>100) OLD_LOG_FILES=100;
111
    if(parm[4]) GEN_LOG_LIMIT=atoi(parm[4]);
111
  if(parm[4]) GEN_LOG_LIMIT=atoi(parm[4]);
112
    if(parm[5]) MODULE_LOG_LIMIT=atoi(parm[5]);
112
  if(parm[5]) MODULE_LOG_LIMIT=atoi(parm[5]);
113
    if(parm[6]) backup_hour=atoi(parm[6]);
113
  if(parm[6]) backup_hour=atoi(parm[6]);
114
    if(parm[7]) site_accounting=atoi(parm[7]);
114
  if(parm[7]) site_accounting=atoi(parm[7]);
115
    if(parm[8]) r=atoi(parm[8])+1; else r=8;
115
  if(parm[8]) r=atoi(parm[8])+1; else r=8;
116
    if(r<2) r=2;
116
  if(r<2) r=2;
117
    if(r>100) r=100;
117
  if(r>100) r=100;
118
    snprintf(buf,sizeof(buf),"%d",r); setenv("examlog_lim2",buf,1);
118
  snprintf(buf,sizeof(buf),"%d",r); setenv("examlog_lim2",buf,1);
119
    if(site_accounting>0) setenv("site_accounting","yes",1);
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
    if(verify_order(cmdlist,cmdcnt,sizeof(cmdlist[0]))) return -1;
127
  if(verify_order(cmdlist,cmdcnt,sizeof(cmdlist[0]))) return -1;
128
    return 0;
128
  return 0;
129
}
129
}
130
 
130
 
131
int main(int argc, char *argv[])
131
int main(int argc, char *argv[])
132
{
132
{
133
    char *p;
133
  char *p;
134
    struct stat st;
134
  struct stat st;
135
    uid_t myid;
135
  uid_t myid;
136
    int /*mfd,*/rsock,mincnt;
136
  int /*mfd,*/rsock,mincnt;
137
    char buf[MAX_LINELEN+1];
137
  char buf[MAX_LINELEN+1];
138
    forkcnt=0; exec_wait=1; mincnt=0;
138
  forkcnt=0; exec_wait=1; mincnt=0;
139
    classcaches=sheetcaches=0;
139
  classcaches=sheetcaches=0;
140
    (void)freopen("/dev/null","r",stdin);
140
  (void)freopen("/dev/null","r",stdin);
141
    (void)freopen("../tmp/log/wimslogd.out","w",stdout);
141
  (void)freopen("../tmp/log/wimslogd.out","w",stdout);
142
    (void)freopen("../tmp/log/wimslogd.err","w",stderr);
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
    verify_tables();
152
  verify_tables();
153
    init_random();
153
  init_random();
154
    modupdatetime=(double) random()*350/RAND_MAX;
154
  modupdatetime=(double) random()*350/RAND_MAX;
155
    rshift=(double) random()*MINLENGTH/RAND_MAX;
155
  rshift=(double) random()*MINLENGTH/RAND_MAX;
156
    parms();
156
  parms();
157
    if(getcwd(cwd,sizeof(cwd))==NULL) {      /* directory missing */
157
  if(getcwd(cwd,sizeof(cwd))==NULL) {      /* directory missing */
158
      fprintf(stderr,"wimslogd: getcwd() failure. %s\n",
158
    fprintf(stderr,"wimslogd: getcwd() failure. %s\n",
159
            strerror(errno));
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
    p=strstr(cwd,"/public_html");
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
    if(p!=NULL && *(p+strlen("/public_html"))==0) {
197
    for(selectcnt=0; selectcnt<100; selectcnt++) {
164
      *p=0; if(chdir(cwd)<0) {      /* strong error */
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
          fprintf(stderr,"wimslogd: Unable to change directory. %s\n",
202
        if(t<0) {wimslogd_error("select() error."); continue;}
166
                strerror(errno));
203
        rsock=accept(commsock,NULL,NULL);
-
 
204
        if(rsock==-1) {wimslogd_error("accept() error."); continue;}
167
          return 1;
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
    myid=getegid(); setregid(myid,myid);
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 it is a cluster child */
224
    wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then no housekeeping */
183
    if(strstr(buf,"yes")!=NULL) {      /* register my real IP */
225
    if(strstr(buf,"yes")==NULL) {
-
 
226
      dispatch_log();
184
      wlogdaccessfile(nodeipbuf,"r","/etc/myip");
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
      wlogdaccessfile(nodeipbuf,"w","tmp/log/myip");
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
      else {      /* cluster child */
237
    else {      /* cluster child */
237
          if((thismin%9)==0) cleaning(0);       /* clean up session directories */
238
      if((thismin%9)==0) cleaning(0);       /* clean up session directories */
238
      }
-
 
239
    }
239
    }
-
 
240
  }
240
    while(1==1);
241
  while(1==1);
241
    return 0;
242
  return 0;
242
}
243
}
243
 
244