static int doall() { int ret = 0; do { CHK2(doconnect() == 0, "connect to NDB"); int loop = 0; while (++loop <= _loops) { g_info << "loop " << loop << " of " << _loops << endl; if (!_sys_any) { if (loop == 1) { CHK1(checkobjs() == 0); } CHK2(dostats() == 0, "at loop " << loop); } else { CHK2(dosys() == 0, "at loop " << loop); } } CHK1(ret == 0); } while (0); dodisconnect(); return ret; }
static int dostats() { int ret = 0; do { for (int i = 0; i < g_indcount; i++) { CHK1(dostats(i) == 0); } CHK1(ret == 0); } while (0); return ret; }
/* * parseline - pull out the scan/readback/name/value from a line * and manage the storage of this information... * * /BASENAME/READBACK is created if scan == 1 * name -> BASENAME/READBACK/name (only on scan 1) * values -> BASENAME/READBACK/values (via addaline() ) * values -> means/ssds/counts ( via dostats() ) */ parseline(char *theline,float themeans[],float thessds[], float thecounts[], char * basename) { int scan, readback; float thevalue,deltamean; char *hop, *hop2; char buf[256]; char thedir[256]; char thefile[256]; char outline[256]; hop = theline; /* _SCAN_23_3434_blah blah: 3.45 */ while(!isdigit(*hop)) hop++; /* 23_3434_blah blah: 3.45 */ sscanf(hop,"%d",&scan); /* scan <- 23 */ while(isdigit(*hop)) hop++; /* _3434_blah blah: 3.45 */ while(!isdigit(*hop)) hop++; /* 3434_blah blah: 3.45 */ sscanf(hop,"%d",&readback); /* readback <- 3434 */ while(isdigit(*hop)) hop++; /* _blah blah: 3.45 */ hop++; /* blah blah: 3.45 */ hop2 = hop; /* now split the remainder into */ while(*hop2 != ':') hop2++; /* two strings: name, val */ *hop2 = (char) 0; hop2++; /* hop="blah blah" hop2=" 3.45" */ sscanf(hop2,"%f",&thevalue); sprintf(thedir,"%s/%d",basename,readback); if(scan == 1) { mkdir(thedir,0755); sprintf(thefile,"%s/name",thedir); addaline(thefile,hop); } sprintf(thefile,"%s/values",thedir); sprintf(outline,"%d %f",scan,thevalue); /* addaline_persistant_values(thefile,readback,outline); */ addaline(thefile,outline); dostats(readback,thevalue,themeans,thessds,thecounts); }
int main(int argc, char *argv[]) { #ifndef DEBUG pid_t wpid; FILE *pid; FILE *quicklog; FILE *mainlog; struct sigaction act; if((wpid = fork()) > 0) { //we are the parent - so exit right away exit(0); } // now detached if(setsid() < 0) { syslog(LOG_ERR,"%s","setsid fails"); exit(1); /* failure! */ } if((quicklog = fopen(QUICK_LOGFILE_NAME, "w")) == NULL) { syslog(LOG_ERR,"opening %s fails",QUICK_LOGFILE_NAME); exit(1); /* failure! */ } if((mainlog = fopen(LIVE_LOGFILE_NAME, "r+")) == NULL) { syslog(LOG_ERR,"opening %s fails",LIVE_LOGFILE_NAME); exit(1); /* failure! */ } // now setup signal handling act.sa_handler = finish; sigemptyset(&act.sa_mask); // dont want child signals, want any interrupted syscalls to restart act.sa_flags = SA_NOCLDSTOP|SA_RESTART; sigaction(SIGINT, &act, 0); sigaction(SIGTERM, &act, 0); sigaction(SIGHUP, &act, 0); sigaction(SIGQUIT, &act, 0); sigaction(SIGUSR1, &act, 0); freopen("/dev/null", "a+", stdin); freopen("/dev/null", "a+", stdout); freopen("/dev/null", "a+", stderr); program_name = "trafficlogger"; program_version = "3"; // if get here we are the working slave // kill any existing logger - there can be only one if((pid = fopen(PIDFILE,"r")) != NULL) { if(flock(fileno(pid), LOCK_EX) == 0) { // we have a lock on this file now // kill the current incarnation, can only be one of us pid_t thatpid = 0; int killstat; int attempt = 0; fscanf(pid, "%d", &thatpid); if(thatpid) { // see if that pid does exist first killstat = kill(thatpid, 0); if(killstat < 0 && errno == ESRCH) { // process already gone } else {// process is there do{ killstat = kill(thatpid, SIGTERM); attempt++; sleep(1); } while(killstat && attempt < 10); if(killstat) { syslog(LOG_ERR,"%s %d","Cannot kill existing process ", thatpid); flock(fileno(pid), LOCK_UN); fclose(pid); exit(1); } } } // write our own pid here now freopen(PIDFILE,"w",pid); rewind(pid); } else { syslog(LOG_ERR,"%s","Cannot lock " PIDFILE); fclose(pid); exit(1); } } // else file not there yet else if((pid = fopen(PIDFILE,"w")) != NULL) { if(flock(fileno(pid), LOCK_EX) == 0) { // do nothing } else { syslog(LOG_ERR,"%s","Cannot lock " PIDFILE); fclose(pid); exit(1); } } // either way pid is now open fprintf(pid,"%d\n", getpid()); flock(fileno(pid), LOCK_UN); fclose(pid); #endif while(traffic_iptables_missing()) { sleep(5); } syslog(LOG_WARNING,"%s", "starting"); dostats(quicklog, mainlog); syslog(LOG_WARNING,"%s", "finishing"); fclose(quicklog); fclose(mainlog); // only time these are explicily closed }