Subversion Repositories wimsdev

Rev

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