Subversion Repositories wimsdev

Rev

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