Subversion Repositories wimsdev

Rev

Rev 8849 | Rev 15509 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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"
  21.  
  22. extern char **environ;
  23.  
  24. char cwd[MAX_FNAME+1];
  25. char pidstr[32];
  26. char keepdate[32]="0";
  27. char mupdate[32]="0";
  28. char backdate[32]="0";
  29. char loadavg[MAX_LINELEN+1];
  30. char qbuf[MAX_LINELEN+1];       /* quota buffer */
  31. time_t nowtime, starttime, lastcleantime=0;
  32. time_t thismin, lastmin, startmin;
  33. struct tm *now;
  34. int nowsec, nowmin, nowhr, nowday, nowwday,nowmon,nowyear;
  35. int startdate;
  36. char nowstr[64];
  37. pid_t mypid;
  38. int idle_time=5000;
  39. int idle_time2=5000;
  40. int idle_time3=5000;
  41. int anti_time=3600*24;      /* antidate tolerance */
  42. int OLD_LOG_FILES=2;
  43. int GEN_LOG_LIMIT=1024000;
  44. int MODULE_LOG_LIMIT=102400;
  45. int backup_hour=-1;
  46. int site_accounting=0;
  47. int modupdatetime=0;
  48. int rshift;      /* shift minute start */
  49. int commsock;
  50. int answerlen;
  51. int debugging;
  52. char ipbuf[64];
  53. char nodeipbuf[MAX_LINELEN+1];
  54. char commbuf[BUFFERLEN+1];
  55. #define textbuf (commbuf+sizeof(int))
  56. char *textptr;
  57.  
  58. int cwdtype;
  59.  
  60. /* check whether there is anything to execute */
  61. void logexec(void)
  62. {
  63.     struct stat st;
  64.     pid_t pid;
  65.     if(stat("log/wimslogd.exec",&st)) return;
  66.     fflush(NULL);
  67.     pid=fork(); if(pid>0) {addfork(pid,1); return;}
  68.     close(commsock); msleep(100);
  69.     call_ssh(1,"sh log/wimslogd.exec >tmp/log/wimslogdexec.out 2>tmp/log/wimslogdexec.err");
  70.     unlink("log/wimslogd.exec"); exit(0);
  71. }
  72.  
  73. void local(void)
  74. {
  75.     struct stat st;
  76.     if(stat("log/wimslogd.local",&st)) return;
  77.     if(!(S_IXUSR&st.st_mode)) return;
  78.     call_ssh(0,"sh log/wimslogd.local");
  79. }
  80.  
  81. void getnow(void)
  82. {
  83.     nowtime=time(NULL); thismin=(nowtime-rshift)/MINLENGTH;
  84.     now=localtime(&nowtime);
  85.     nowsec=now->tm_sec;
  86.     nowmin=now->tm_min; nowhr=now->tm_hour;
  87.     nowday=now->tm_mday; nowwday=now->tm_wday;
  88.     nowmon=now->tm_mon+1; nowyear=now->tm_year+1900;
  89.     snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d",
  90.            nowyear,nowmon,nowday,nowhr,nowmin,nowsec);
  91. }
  92.  
  93. void parms(void)
  94. {
  95.     char *p, *p1, *p2, *parm[16];
  96.     char buf[16];
  97.     int t,r;
  98.     p=getenv("wimslogd");
  99.     if(p==NULL || *p==0) return;
  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;
  102.       parm[t++]=p1;
  103.     }
  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;
  106.     idle_time3=atoi(parm[2]); if(idle_time3<=10) idle_time3=idle_time2;
  107.     if(idle_time2>idle_time) idle_time2=idle_time;
  108.     if(idle_time3>idle_time2) idle_time3=idle_time2;
  109.     OLD_LOG_FILES=atoi(parm[3]);
  110.     if(OLD_LOG_FILES>100) OLD_LOG_FILES=100;
  111.     if(parm[4]) GEN_LOG_LIMIT=atoi(parm[4]);
  112.     if(parm[5]) MODULE_LOG_LIMIT=atoi(parm[5]);
  113.     if(parm[6]) backup_hour=atoi(parm[6]);
  114.     if(parm[7]) site_accounting=atoi(parm[7]);
  115.     if(parm[8]) r=atoi(parm[8])+1; else r=8;
  116.     if(r<2) r=2;
  117.     if(r>100) r=100;
  118.     snprintf(buf,sizeof(buf),"%d",r); setenv("examlog_lim2",buf,1);
  119.     if(site_accounting>0) setenv("site_accounting","yes",1);
  120. }
  121.  
  122. /* This is run only when manually invoking the program.
  123.  * Verifies the orderedness of various list tables.
  124.  */
  125. int verify_tables(void)
  126. {
  127.     if(verify_order(cmdlist,cmdcnt,sizeof(cmdlist[0]))) return -1;
  128.     return 0;
  129. }
  130.  
  131. int main(int argc, char *argv[])
  132. {
  133.     char *p;
  134.     struct stat st;
  135.     uid_t myid;
  136.     int /*mfd,*/rsock,mincnt;
  137.     char buf[MAX_LINELEN+1];
  138.     forkcnt=0; exec_wait=1; mincnt=0;
  139.     classcaches=sheetcaches=0;
  140.     (void)freopen("/dev/null","r",stdin);
  141.     (void)freopen("../tmp/log/wimslogd.out","w",stdout);
  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);
  144.     write(mfd,buf,SHM_SIZE);
  145.     shmptr=mmap(0,SHM_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,mfd,0);
  146.     if(shmptr==MAP_FAILED) {
  147.       fprintf(stderr,"wimslogd: mmap() failure. %s\n",
  148.             strerror(errno));
  149.       exit(1);
  150.     }
  151. */
  152.     verify_tables();
  153.     init_random();
  154.     modupdatetime=(double) random()*350/RAND_MAX;
  155.     rshift=(double) random()*MINLENGTH/RAND_MAX;
  156.     parms();
  157.     if(getcwd(cwd,sizeof(cwd))==NULL) {      /* directory missing */
  158.       fprintf(stderr,"wimslogd: getcwd() failure. %s\n",
  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; if(chdir(cwd)<0) {      /* strong error */
  165.           fprintf(stderr,"wimslogd: Unable to change directory. %s\n",
  166.                 strerror(errno));
  167.           return 1;
  168.       }
  169.     }
  170.     opensock();
  171.     mypid=getpid();
  172.     myid=geteuid(); setreuid(myid,myid);
  173.     myid=getegid(); setregid(myid,myid);
  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");
  181.     wlogdaccessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf));
  182.     wlogdaccessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then it is a cluster child */
  183.     if(strstr(buf,"yes")!=NULL) {      /* register my real IP */
  184.       wlogdaccessfile(nodeipbuf,"r","/etc/myip");
  185.       wlogdaccessfile(nodeipbuf,"w","tmp/log/myip");
  186.     }
  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.           if((thismin%9)==0) cleaning(0);       /* clean up session directories */
  238.       }
  239.     }
  240.     while(1==1);
  241.     return 0;
  242. }
  243.  
  244.