Back to index

wims  3.65+svn20090927
Defines | Enumerations | Functions | Variables
wimslogd.c File Reference
#include "wimslogd.h"
#include "fork.c"
#include "lines.c"
#include "cache.c"
#include "files.c"
#include "socket.c"
#include "log.c"
#include "cleaning.c"
#include "housekeep.c"
#include "homedir.c"

Go to the source code of this file.

Defines

#define textbuf   (commbuf+sizeof(int))

Enumerations

enum  { dir_home, dir_class, dir_session, dir_module }

Functions

void logexec (void)
void local (void)
void getnow (void)
void parms (void)
int verify_tables (void)
int main (int argc, char *argv[])

Variables

char ** environ
char cwd [MAX_FNAME+1]
char pidstr [32]
char keepdate [32] = "0"
char mupdate [32] = "0"
char backdate [32] = "0"
char loadavg [MAX_LINELEN+1]
char qbuf [MAX_LINELEN+1]
time_t nowtime
time_t starttime
time_t lastcleantime = 0
time_t thismin
time_t lastmin
time_t startmin
struct tm * now
int nowsec
int nowmin
int nowhr
int nowday
int nowwday
int nowmon
int nowyear
int startdate
char nowstr [64]
pid_t mypid
int idle_time = 5000
int idle_time2 = 5000
int idle_time3 = 5000
int anti_time = 3600*24
int OLD_LOG_FILES = 2
int GEN_LOG_LIMIT = 1024000
int MODULE_LOG_LIMIT = 102400
int backup_hour = -1
int site_accounting = 0
int modupdatetime = 0
int rshift
int commsock
int answerlen
int debugging
char ipbuf [64]
char nodeipbuf [MAX_LINELEN+1]
char commbuf [BUFFERLEN+1]
char * textptr
int cwdtype

Define Documentation

#define textbuf   (commbuf+sizeof(int))

Definition at line 55 of file wimslogd.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
dir_home 
dir_class 
dir_session 
dir_module 

Definition at line 59 of file wimslogd.c.


Function Documentation

void getnow ( void  )

Definition at line 92 of file wimslogd.c.

{
    nowtime=time(NULL); thismin=(nowtime-rshift)/MINLENGTH;
    now=localtime(&nowtime);
    nowsec=now->tm_sec;
    nowmin=now->tm_min; nowhr=now->tm_hour;
    nowday=now->tm_mday; nowwday=now->tm_wday;
    nowmon=now->tm_mon+1; nowyear=now->tm_year+1900;
    snprintf(nowstr,sizeof(nowstr),"%04d%02d%02d.%02d:%02d:%02d",
            nowyear,nowmon,nowday,nowhr,nowmin,nowsec);
}

Here is the caller graph for this function:

void local ( void  )

Definition at line 84 of file wimslogd.c.

{
    struct stat st;
    if(stat("log/wimslogd.local",&st)) return;
    if(!(S_IXUSR&st.st_mode)) return;
    call_ssh(0,"sh log/wimslogd.local");
}

Here is the call graph for this function:

Here is the caller graph for this function:

void logexec ( void  )

Definition at line 72 of file wimslogd.c.

{
    struct stat st;
    pid_t pid;
    if(stat("log/wimslogd.exec",&st)) return;
    fflush(NULL);
    pid=fork(); if(pid>0) {addfork(pid,1); return;}
    close(commsock); msleep(100);
    call_ssh(1,"sh log/wimslogd.exec >tmp/log/wimslogdexec.out 2>tmp/log/wimslogdexec.err");
    unlink("log/wimslogd.exec"); exit(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 140 of file wimslogd.c.

{
    char *p;
    struct stat st;
    uid_t myid;
    int /*mfd,*/rsock,mincnt;
    char buf[MAX_LINELEN+1];

    error1=error; error2=error; error3=error;
    forkcnt=0; exec_wait=1; mincnt=0;
    classcaches=sheetcaches=0;
    freopen("/dev/null","r",stdin);
    freopen("../tmp/log/wimslogd.out","w",stdout);
    freopen("../tmp/log/wimslogd.err","w",stderr);
/*    mfd=shm_open(SHM_NAME,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
    write(mfd,buf,SHM_SIZE);
    shmptr=mmap(0,SHM_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,mfd,0);
    if(shmptr==MAP_FAILED) {
       fprintf(stderr,"wimslogd: mmap() failure. %s\n",
              strerror(errno));
       exit(1);
    }
*/    
    verify_tables();
    init_random();
    modupdatetime=(double) random()*350/RAND_MAX;
    rshift=(double) random()*MINLENGTH/RAND_MAX;
    parms();
    if(getcwd(cwd,sizeof(cwd))==NULL) {   /* directory missing */
       fprintf(stderr,"wimslogd: getcwd() failure. %s\n",
              strerror(errno));
       return 1;
    }
    p=strstr(cwd,"/public_html");
    if(p!=NULL && *(p+strlen("/public_html"))==0) {
       *p=0; if(chdir(cwd)<0) {    /* strong error */
           fprintf(stderr,"wimslogd: Unable to change directory. %s\n",
                  strerror(errno));
           return 1;
       }
    }
    opensock();
    mypid=getpid();
    myid=geteuid(); setreuid(myid,myid);
    myid=getegid(); setregid(myid,myid);
    stat("/sysmask/notice/init-end",&st);
    snprintf(pidstr,sizeof(pidstr),"%u",mypid);
    getnow(); printf("wimslogd %s started at %s.\n",pidstr,nowstr);
    startdate=nowday;
    fflush(NULL);
    starttime=nowtime; startmin=lastmin=thismin;
    accessfile(qbuf,"r","log/cquota/lim.host");
    accessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf));
    accessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then it is a cluster child */
    if(strstr(buf,"yes")!=NULL) {  /* register my real IP */
       accessfile(nodeipbuf,"r","/etc/myip"); 
       accessfile(nodeipbuf,"w","tmp/log/myip");
    }
    do {
       fd_set rset;
       struct timeval tv;
       int t, selectcnt;

       if(getpid()!=mypid) return 0;      /* leaked child */
       if(stat(debugfile,&st)==0 && st.st_size<MAX_DEBUGLENGTH) debugging=1;
       else debugging=0;
       accessfile(loadavg,"r","/proc/loadavg");
       for(selectcnt=0; selectcnt<100; selectcnt++) {
           tv.tv_sec=0; tv.tv_usec=50000; /* a pause every 50 ms. */
           FD_ZERO(&rset); FD_SET(commsock,&rset);
           t=select(commsock+1,&rset,NULL,NULL,&tv);
           if(t==0) {forkman(0); continue;}
           if(t<0) {error("select() error."); continue;}
           rsock=accept(commsock,NULL,NULL);
           if(rsock==-1) {error("accept() error."); continue;}
           answer(rsock);
       }
       forkman(1);
       getnow();
       if(thismin==lastmin) continue;
       mincnt++; /* if(mincnt>MAX_MIN) return 0; Refreshment. */
       if(nowday!=startdate) return 0; /* Daily refreshment. */
       lastmin=thismin;
        accessfile(buf,"r",pidfile); strip_trailing_spaces(buf);
       if(strcmp(buf,pidstr)!=0) { /* wrong pid: abandon. */
           wait_children();
           return 0;
       }

       if(getpid()!=mypid) return 0;      /* leaked child */
       accessfile(qbuf,"r","log/cquota/lim.host");
       accessfile(buf,"r","log/myip"); mystrncpy(ipbuf,find_word_start(buf),sizeof(ipbuf));
       cleancache();
       if((thismin%127)==6) homedir();    /* update home directory setup */
       accessfile(buf,"r","tmp/log/wimslogd.relax"); /* if yes then no housekeeping */
       if(strstr(buf,"yes")==NULL) {
           dispatch_log();
           if((thismin%2)==1) local();
           /* if((thismin%9)==0) */ cleaning(1);        /* clean up session directories */
           if((thismin%5)==0 && nowmin>15) housekeep(); /* daily housekeeping */
           if(getpid()!=mypid) return 0;  /* leaked child */
           if(nowhr*60+nowmin>modupdatetime && (thismin%17)==11) modupdate();
           if(backup_hour>0 && backup_hour<23 && (thismin%17)==3 && nowhr>=backup_hour)
             backup();      /* daily backup */
           fflush(NULL);
           logexec();
       }
       else { /* cluster child */
           if((thismin%9)==0) cleaning(0);       /* clean up session directories */
       }
    }
    while(1==1);
    return 0;
}

Here is the call graph for this function:

void parms ( void  )

Definition at line 104 of file wimslogd.c.

{
    char *p, *p1, *p2, *parm[16];
    char buf[16];
    int t,r;
    p=getenv("wimslogd");
    if(p==NULL || *p==0) return;
    for(t=0, p1=find_word_start(p); *p1; p1=find_word_start(p2)) {
       p2=find_word_end(p1); if(*p2) *p2++=0;
       parm[t++]=p1;
    }
    idle_time=atoi(parm[0]); if(idle_time<=10) idle_time=5000;
    idle_time2=atoi(parm[1]); if(idle_time2<=10) idle_time2=idle_time;
    idle_time3=atoi(parm[2]); if(idle_time3<=10) idle_time3=idle_time2;
    if(idle_time2>idle_time) idle_time2=idle_time;
    if(idle_time3>idle_time2) idle_time3=idle_time2;
    OLD_LOG_FILES=atoi(parm[3]);
    if(OLD_LOG_FILES>100) OLD_LOG_FILES=100;
    if(parm[4]) GEN_LOG_LIMIT=atoi(parm[4]);
    if(parm[5]) MODULE_LOG_LIMIT=atoi(parm[5]);
    if(parm[6]) backup_hour=atoi(parm[6]);
    if(parm[7]) site_accounting=atoi(parm[7]);
    if(parm[8]) r=atoi(parm[8])+1; else r=8;
    if(r<2) r=2; if(r>100) r=100;
    snprintf(buf,sizeof(buf),"%d",r); setenv("examlog_lim2",buf,1);
    if(site_accounting>0) setenv("site_accounting","yes",1);
}

Here is the call graph for this function:

int verify_tables ( void  )

Definition at line 134 of file wimslogd.c.

{
    if(verify_order(cmdlist,cmdcnt,sizeof(cmdlist[0]))) return -1;
    return 0;
}

Here is the call graph for this function:


Variable Documentation

int answerlen

Definition at line 50 of file wimslogd.c.

int anti_time = 3600*24

Definition at line 41 of file wimslogd.c.

char backdate[32] = "0"

Definition at line 28 of file wimslogd.c.

int backup_hour = -1

Definition at line 45 of file wimslogd.c.

char commbuf[BUFFERLEN+1]

Definition at line 54 of file wimslogd.c.

int commsock

Definition at line 49 of file wimslogd.c.

char cwd[MAX_FNAME+1]

Definition at line 24 of file wimslogd.c.

int cwdtype

Definition at line 58 of file wimslogd.c.

int debugging

Definition at line 51 of file wimslogd.c.

char** environ
int GEN_LOG_LIMIT = 1024000

Definition at line 43 of file wimslogd.c.

int idle_time = 5000

Definition at line 38 of file wimslogd.c.

int idle_time2 = 5000

Definition at line 39 of file wimslogd.c.

int idle_time3 = 5000

Definition at line 40 of file wimslogd.c.

char ipbuf[64]

Definition at line 52 of file wimslogd.c.

char keepdate[32] = "0"

Definition at line 26 of file wimslogd.c.

time_t lastcleantime = 0

Definition at line 31 of file wimslogd.c.

time_t lastmin

Definition at line 32 of file wimslogd.c.

Definition at line 29 of file wimslogd.c.

int MODULE_LOG_LIMIT = 102400

Definition at line 44 of file wimslogd.c.

int modupdatetime = 0

Definition at line 47 of file wimslogd.c.

char mupdate[32] = "0"

Definition at line 27 of file wimslogd.c.

pid_t mypid

Definition at line 37 of file wimslogd.c.

Definition at line 53 of file wimslogd.c.

struct tm* now

Definition at line 33 of file wimslogd.c.

int nowday

Definition at line 34 of file wimslogd.c.

int nowhr

Definition at line 34 of file wimslogd.c.

int nowmin

Definition at line 34 of file wimslogd.c.

int nowmon

Definition at line 34 of file wimslogd.c.

int nowsec

Definition at line 34 of file wimslogd.c.

char nowstr[64]

Definition at line 36 of file wimslogd.c.

time_t nowtime

Definition at line 31 of file wimslogd.c.

int nowwday

Definition at line 34 of file wimslogd.c.

int nowyear

Definition at line 34 of file wimslogd.c.

int OLD_LOG_FILES = 2

Definition at line 42 of file wimslogd.c.

char pidstr[32]

Definition at line 25 of file wimslogd.c.

char qbuf[MAX_LINELEN+1]

Definition at line 30 of file wimslogd.c.

int rshift

Definition at line 48 of file wimslogd.c.

int site_accounting = 0

Definition at line 46 of file wimslogd.c.

int startdate

Definition at line 35 of file wimslogd.c.

time_t startmin

Definition at line 32 of file wimslogd.c.

time_t starttime

Definition at line 31 of file wimslogd.c.

char* textptr

Definition at line 56 of file wimslogd.c.

time_t thismin

Definition at line 32 of file wimslogd.c.