// we only test the size of the file const bool EvaCachedFile::isInfoFinished() { if(m_IsLoading) return false; unsigned int tmp = 0; map<unsigned int, unsigned int>::iterator iter; //Q_UINT32 qoffset, qlen; for(iter=m_FragInfo.begin(); iter!=m_FragInfo.end(); ++iter){ tmp += iter->second; } util_log(0,"EvaCachedFile::isInfoFinished -- tmp: %d, m_FileSize: %d\n", tmp, m_FileSize); if(tmp == m_FileSize) return true; return false; }
static void dump_cnf_chn(int level, struct edes_cnf_chn *chn) { sprintf(buf, "index = %d, ", chn->index); sprintf(buf+strlen(buf), "wrdsiz=%d, ", chn->wrdsiz); sprintf(buf+strlen(buf), "instype=`%s', ", chn->instype); sprintf(buf+strlen(buf), "order=0x%08x, ", chn->order); sprintf(buf+strlen(buf), "sint=%.3f, ", chn->sint); sprintf(buf+strlen(buf), "calib=%.3f, ", chn->calib); sprintf(buf+strlen(buf), "calper=%.3f, ", chn->calper); sprintf(buf+strlen(buf), "vang=%.3f, ", chn->vang); sprintf(buf+strlen(buf), "hang=%.3f, ", chn->hang); sprintf(buf+strlen(buf), "paklen=%.3f", chn->paklen); util_log(level, buf); }
const bool EvaCachedFile::changeFileInfo() { m_CachedFileName = m_FileName + CacheFileName_Ext; m_InfoFileName = m_FileName + InfoFileName_Ext; string filePath = m_DirPath + "\\" + m_CachedFileName; if (!_access(filePath.c_str(),0)) { util_log(0, "EvaCachedFile::constructor -- \"%s\" already exist!\n", filePath.c_str()); m_State = EExists; return false; } //m_InfoFileName = m_FileName + InfoFileName_Ext; filePath = m_DirPath + "/" + m_InfoFileName; if (!_access(filePath.c_str(),0)) { util_log(0, "EvaCachedFile::constructor -- \"%s\" already exists! delete it!\n", filePath.c_str()); if(!DeleteFileA(filePath.c_str())){ util_log(0, "EvaCachedFile::constructor -- cannot remove \"%s\"!\n", filePath.c_str()); m_State = EError; return false; } } return true; }
static BOOL IsVirgin(IDA10_TSHDR *dhead, struct nrts_sys *sys, int fdhdr, int chnndx) { off_t offset; struct nrts_chn *chn; char buf[MAXHDRLEN]; static char *fid = "nrts_ida10decodeinit:IsVirgin"; chn = &sys->sta[0].chn[chnndx]; if (chn->count == 0) return TRUE; if (chn->hdr.len > MAXHDRLEN) { util_log(1, "ABORT: MAXHDRLEN in %s is TOO SMALL!", fid); exit(1); } offset = chn->hdr.off + (chn->hdr.yngest * chn->hdr.len); if (lseek(fdhdr, offset, SEEK_SET) != offset) { util_log(1, "ABORT: lseek in %s FAILED!", fid); exit(1); } if (read(fdhdr, buf, chn->hdr.len) != chn->hdr.len) { util_log(1, "ABORT: read in %s FAILED!", fid); exit(1); } if (ida10Type((UINT8 *)buf) != IDA10_TYPE_TS) { util_log(1, "ABORT: %s: WRONG TYPE: expect %d, got %d, chn=%s", fid, IDA10_TYPE_TS, ida10Type((UINT8 *)buf), chn->name); exit(1); } if (ida10UnpackTSHdr((UINT8 *)buf, dhead) == 0) { util_log(1, "ABORT: ida10UnpackTSHdr in %s FAILED!", fid); exit(1); } return FALSE; }
void FlushIsoImage() { THREAD tid; static char *fid = "FlushIsoImage"; MUTEX_LOCK(&LocalMutex); if (Enabled && !ActiveFlag) { ActiveFlag = TRUE; if (!THREAD_CREATE(&tid, FlushIsoImageThread, NULL)) { util_log(1, "%s: failed to start FlushIsoImageThread", fid); ispd_die(MY_MOD_ID + 2); } } MUTEX_UNLOCK(&LocalMutex); }
int main(int argc, char **argv) { int port = ISP_BOOTMGR_PORT; struct sockaddr_in cli_addr; int client, len = sizeof(cli_addr); if (argc > 1) port = atoi(argv[1]); util_bground(0, 0); util_logopen("syslogd", 1, 9, 1, (char *) NULL, argv[0]); server_init(port); util_log(1, "ISP boot manager installed at port %d", port); while (1) { client = accept(sd, (struct sockaddr *) &cli_addr, &len); if (client < 0) { util_log(1, "accept: %s", syserrmsg(errno)); } else { serve(client); } } }
int isp_send( int sd, int type, char *buffer, int datalen, int to ) { UINT8 *ptr; int msglen; static char *fid = "isp_send"; if (to < 1) to = 10; ptr = (UINT8 *) buffer; ptr += utilPackINT16(ptr, (UINT16) datalen+2); ptr += utilPackINT16(ptr, (UINT16) type); msglen = datalen + 4; if (util_write(sd, buffer, msglen, to) != msglen) { util_log(1, "%s: util_write: %s", fid, syserrmsg(errno)); return ISP_ERROR; } return ISP_OK; }
const unsigned int EvaCachedFile::getFragment(const unsigned int offset, const unsigned int len, unsigned char *buf) { if(!m_IsLoading) return false; unsigned int bytesRead = 0; FILE* fp; if(!(fp=fopen(string(m_DirPath+"\\"+m_FileName).c_str(),"rb"))){ util_log(0,"EvaCachedFile::getFragment -- \"%s\" dosen't exist!\n", m_FileName.c_str()); return bytesRead; } if(!fseek(fp,offset,SEEK_SET)) bytesRead = fread((char*)buf,1,len,fp); fclose(fp); if( !bytesRead){ m_State = EReadError; return 0; } return bytesRead; }
void dump_par(int level, struct edes_params *par) { int isys; struct edes_cnf *cnf; util_log(level, "PARAMETER DUMP"); util_log(level, "nsys = %d", par->nsys); for (isys = 0; isys < par->nsys; isys++) { util_log(level, "----------"); util_log(level, "System %d of %d:", isys+1, par->nsys); dump_cnf(level, par->cnf + isys, par->sys+isys); util_log(level, ".........."); dump_inf(level, par->inf + isys, par->sys+isys); } util_log(level, "++++++++++"); }
static char *get_peer(int sd, char *buffer) { int addrlen, h_errno; struct sockaddr_in cli_addr, *cli_addrp; struct hostent *hp, result; static char *fid = "get_peer"; addrlen = sizeof(cli_addr); cli_addrp = &cli_addr; if (getpeername(sd, (struct sockaddr *)cli_addrp, &addrlen) != 0) { util_log(1, "%s: getpeername: %s", fid, syserrmsg(errno)); return NULL; } #ifdef SOLARIS hp = gethostbyaddr_r( (char *) &cli_addrp->sin_addr, sizeof(struct in_addr), cli_addrp->sin_family, &result, buffer, MAXPATHLEN, &h_errno ); #else hp = gethostbyaddr( (char *) &cli_addrp->sin_addr, sizeof(struct in_addr), cli_addrp->sin_family ); #endif if (hp != NULL) { strcpy(buffer, hp->h_name); } else { strcpy(buffer, inet_ntoa(cli_addrp->sin_addr)); } return buffer; }
static void dump_inf_chn(int level, struct edes_inf_chn *chn) { if ((int) chn->beg == (int) XFER_YNGEST) { sprintf(buf, "beg=XFER_YNGEST, "); } else if ((int) chn->beg == (int) XFER_OLDEST) { sprintf(buf, "beg=XFER_OLDEST, "); } else { sprintf(buf, "beg=%s, ", util_dttostr(chn->beg, 0)); } if ((int) chn->end == (int) XFER_YNGEST) { sprintf(buf+strlen(buf), "end=XFER_YNGEST, "); } else if ((int) chn->end == (int) XFER_OLDEST) { sprintf(buf+strlen(buf), "end=XFER_OLDEST, "); } else { sprintf(buf+strlen(buf), "end=%s, ", util_dttostr(chn->end, 0)); } sprintf(buf+strlen(buf), "begndx=%ld ", chn->begndx); sprintf(buf+strlen(buf), "endndx=%ld ", chn->endndx); sprintf(buf+strlen(buf), "nxtndx=%ld ", chn->nxtndx); sprintf(buf+strlen(buf), "count=%ld ", chn->count); sprintf(buf+strlen(buf), "status=%d ", chn->status); util_log(level, buf); }
const bool EvaCachedFile::isFileCorrect() { if(m_IsLoading) return true; // if we are loading file, this would be always true // check dest-file size FILE* fp=fopen(string(m_DirPath + "\\" + m_FileName).c_str(),"rb"); if (!fp) return false; fseek(fp,0,SEEK_END); if(ftell(fp) != m_FileSize) return false; // check dest-file md5 value char *md5 = new char[16]; if(!getSourceFileMd5(md5)){ m_State = EMd5Error; fclose(fp); return false; } if(memcmp(m_FileMd5, md5, 16)){ m_State = EMd5Error; fclose(fp); return false; } // check dest-file name md5 getSourceFileNameMd5(md5); if(memcmp(m_FileNameMd5, md5, 16)){ util_log(0, "EvaCachedFile::isFileCorrect -- \"%s\" file name saved might be wrong but file contents should be all right.\n", m_FileName.c_str()); } delete []md5; fclose(fp); return true; }
const bool EvaCachedFile::calculateFileMd5(const string& fullpath, char *md5Buf) { FILE* fp=fopen(fullpath.c_str(),"rb"); if(!fp){ util_log(0, "EvaCachedFile::calculateFileMd5 -- \"%s\" dosen't exist!\n", fullpath.c_str()); return false; } fseek(fp,0,SEEK_END); unsigned int len = ftell(fp); fseek(fp,0,SEEK_SET); if(len > MaxMd5CheckLength) len = MaxMd5CheckLength; char *buf = new char[len]; unsigned int numRead = fread(buf,1,len,fp); fclose(fp); if(numRead!=len){ delete []buf; return false; } memcpy(md5Buf, EvaUtil::doMd5(buf, len), 16); delete []buf; return true; }
const bool EvaCachedFile::loadInfoFile() { if(m_IsLoading) return false; FILE* fp; if(!(fp=fopen(string(m_DirPath+"\\"+m_InfoFileName).c_str(),"rb"))){ util_log(0, "EvaCachedFile::loadInfoFile -- no info file available.\n"); m_State = EInfoOpen; return false; } char* fileName; unsigned char fileNameLen; fread(&fileNameLen,1,1,fp); fileName=new char[fileNameLen+1]; fread(fileName,fileNameLen,1,fp); fileName[fileNameLen]=0; if (string(fileName)!=m_FileName) { delete[] fileName; fclose(fp); return false; } delete[] fileName; unsigned int tmp; fread(&tmp,4,1,fp); if(tmp != m_FileSize) { fclose(fp); return false; } char *fnmd5 = new char[16]; fread(fnmd5,16,1,fp); if(memcmp(fnmd5, m_FileNameMd5, 16)){ m_State = EMd5Error; fclose(fp); delete [] fnmd5; return false; } delete [] fnmd5; char *fmd5 = new char[16]; fread(fmd5,16,1,fp); if(memcmp(fmd5, m_FileMd5, 16)){ m_State = EMd5Error; fclose(fp); delete [] fmd5; return false; } delete [] fmd5; unsigned int qoffset, qlen; while(!feof(fp)){ fread(&qoffset,4,1,fp); fread(&qlen,4,1,fp); m_FragInfo[qoffset]=qlen; } fclose(fp); return true; }
void die(int status) { util_log(2, "exit %d", status); exit(status); }
int nrts_ida10decodeinit( char *home, char *syscode, struct nrts_sys *sys_ptr, int new_flags, IDA *unused1, struct nrts_ida *unused2, void *opt, int fdhdr ){ int i; static int initialized = FALSE; static char *fid = "nrts_ida10decodeinit"; /* We can modify the flag and tic tolerance many times */ flags = new_flags; if (opt != (void *) 0) { errtol = *((unsigned long *) opt); util_log(1, "time tears of %ld tics or less are tolerated", (long) errtol ); } else { errtol = 0; } /* But the rest of the stuff can be done only once */ if (initialized) return 0; sys = sys_ptr; if (sys->nsta != 1) { util_log(1, "%s: ERROR: nsta = %d", fid, sys->nsta); util_log(1, "%s: IDA systems can have only one station", fid); return -1; } first = (BOOL *) malloc(sys->sta[0].nchn * sizeof(BOOL)); if (first == (BOOL *) NULL) { util_log(1, "%s: malloc: %s", fid, syserrmsg(errno)); return -1; } prev_dhead = (IDA10_TSHDR *) malloc(sys->sta[0].nchn * sizeof(IDA10_TSHDR)); if (prev_dhead == (IDA10_TSHDR *) NULL) { util_log(1, "%s: malloc: %s", fid, syserrmsg(errno)); return -1; } for (i = 0; i < sys->sta[0].nchn; i++) first[i] = IsVirgin(&prev_dhead[i], sys, fdhdr, i); /* Fill out the static parts of the packet header */ hd.wfdisc = wfdisc_null; hd.standx = 0; sprintf(hd.wfdisc.dir, "%s/%s", home, syscode); sprintf(hd.wfdisc.dfile, "%s", NRTS_DATNAME); sprintf(hd.wfdisc.sta, "%s", sys->sta[0].name); sprintf(hd.wfdisc.datatype, "s4"); hd.hlen = IDA10_TSHEADLEN; hd.dlen = sys->reclen - hd.hlen; /* Initialization complete */ initialized = TRUE; return 0; }
void do_rate(struct timeval *begtm, struct timeval *endtm, int len, char *client) { char *type; double beg, end, elapsed, xferrate; double nbytes, min, ave, max; static char *fid = "do_rate"; if (begtm != NULL && endtm != NULL && len > 0) { if ( begtm->tv_sec == endtm->tv_sec && begtm->tv_usec == endtm->tv_usec ) { elapsed = 0.0001; } else { beg = (double) begtm->tv_sec + ((double) begtm->tv_usec / (double) 1000000); end = (double) endtm->tv_sec + ((double) endtm->tv_usec / (double) 1000000); elapsed = end - beg; } rate.nbytes += len; xferrate = (double) (len) / elapsed; if (++rate.nrec == 1) { rate.min = rate.max = rate.ave = xferrate; } else { if (xferrate < rate.min) rate.min = xferrate; if (xferrate > rate.max) rate.max = xferrate; rate.ave += xferrate; } } else if (rate.nrec > 0) { if (rate.nbytes < BYTES_PER_KBYTE) { nbytes = (double) rate.nbytes; type = "bytes"; } else if (rate.nbytes < BYTES_PER_MBYTE) { nbytes = (double) rate.nbytes / (double) BYTES_PER_KBYTE; type = "Kbytes"; } else if (rate.nbytes < BYTES_PER_GBYTE) { nbytes = (double) rate.nbytes / (double) BYTES_PER_MBYTE; type = "Mbytes"; } else { nbytes = (double) rate.nbytes / (double) BYTES_PER_GBYTE; type = "Gbytes"; } rate.ave /= (double) rate.nrec; min = rate.min / (double) BYTES_PER_KBYTE; ave = rate.ave / (double) BYTES_PER_KBYTE; max = rate.max / (double) BYTES_PER_KBYTE; util_log(2, "%ld records (%.2lf %s) sent to %s", rate.nrec, nbytes, type, client ); util_log(3, "rate = %.2lf/%.2lf/%.2lf Kbytes/s", min, ave, max); rate.nrec = rate.nbytes = 0; } else if (rate.nrec == 0) { util_log(1, "0 records transferred to %s", client); } }
struct nrts_header *nrts_ida10decode(char *buffer, int len, int *action) { struct nrts_chn *chn; REAL64 errsec, errsmp; int i, status, sign; UINT64 err; IDA10_TSHDR dhead, *prev, *crnt; static char tqual[2] = {'?', 'M'}; static short tcode[2] = {5, 1}; static char *fid = "nrts_ida10decode"; if (ida10Type((UINT8 *)buffer) != IDA10_TYPE_TS) return NULL; /* Decode the record header */ memset(&dhead, 0, sizeof(IDA10_TSHDR)); crnt = &dhead; if (ida10UnpackTSHdr((UINT8 *)buffer, crnt) == 0) { util_log(2, "corrupt data record dropped"); return (struct nrts_header *) NULL; } /* Determine if desired stream. */ for (hd.chnndx = -1, i = 0; hd.chnndx < 0 && i < sys->sta[0].nchn; i++) { if (strcasecmp(sys->sta[0].chn[i].name, crnt->cname) == 0) hd.chnndx = i; } if (hd.chnndx == -1) { util_log(2, "unconfigured channel %s, packet dropped", crnt->cname); return (struct nrts_header *) NULL; } chn = sys->sta[0].chn + hd.chnndx; /* Sanity checks on header contents */ sprintf(msgbuf, "corrupt data record dropped: "); if (crnt->unused > 0) { util_log(1, "short %s packet dropped (%d unused bytes)", chn->name, crnt->unused); return (struct nrts_header *) NULL; } else if (crnt->unused < 0) { sprintf(msgbuf+strlen(msgbuf), "%s claims %d extra bytes?", chn->name, crnt->unused); util_log(1, msgbuf); return (struct nrts_header *) NULL; } if (UnreliableTimeTag(&crnt->cmn.ttag.beg.status) || UnreliableTimeTag(&crnt->cmn.ttag.end.status)) { sprintf(msgbuf+strlen(msgbuf), "%s unreliable time stamp", chn->name); util_log(1, msgbuf); return (struct nrts_header *) NULL; } /* Look for and reject records with duplicate or overlapping headers or times */ prev = &prev_dhead[hd.chnndx]; if (write_enabled && !first[hd.chnndx]) { if (memcmp(prev, crnt, sizeof(IDA10_TSHDR)) == 0) { util_log(1, "duplicate %s data header, packet dropped", chn->name); return (struct nrts_header *) NULL; } if ( memcmp(&prev->cmn.ttag.beg, &crnt->cmn.ttag.beg, sizeof(IDA10_TTAG)) == 0 && memcmp(&prev->cmn.ttag.end, &crnt->cmn.ttag.end, sizeof(IDA10_TTAG)) == 0 ) { util_log(1, "duplicate %s start and end times, packet dropped", chn->name ); return (struct nrts_header *) NULL; } if (crnt->tofs <= prev->tols) { sprintf(msgbuf, "%s <= ", util_dttostr(crnt->tofs, 0)); sprintf(msgbuf+strlen(msgbuf), "%s", util_dttostr(prev->tols, 0)); util_log(1, "dropped overlapping %s packet (%s)", chn->name, msgbuf); return (struct nrts_header *) NULL; } } /* Fill in the output header for this packet */ sprintf(hd.wfdisc.chan, "%s", chn->name); hd.wrdsiz = 4; hd.wfdisc.nsamp = crnt->nsamp; hd.wfdisc.time = crnt->tofs; hd.wfdisc.endtime = crnt->tols; hd.wfdisc.smprate = (float) crnt->srate; hd.order = BIG_ENDIAN_ORDER; hd.beg.code = (char) tcode[crnt->cmn.ttag.beg.status.locked ? 1 : 0]; hd.beg.qual = tqual[crnt->cmn.ttag.beg.status.locked ? 1 : 0]; hd.end.code = (char) tcode[crnt->cmn.ttag.end.status.locked ? 1 : 0]; hd.end.qual = tqual[crnt->cmn.ttag.end.status.locked ? 1 : 0]; /* If filling disk loop, save the sample interval */ if (write_enabled && first[hd.chnndx]) chn->sint = (float) crnt->sint; /* Look for time tears */ if (!first[hd.chnndx]) { status = ida10TtagIncrErr(prev, crnt, &sign, &err); if (status != 0) { util_log(1,"%s: ida10TtagIncrErr: error %d", fid, status); return (struct nrts_header *) NULL; } else { errsec = (REAL64) err / (REAL64) NANOSEC_PER_SEC; errsmp = errsec / (REAL64) crnt->sint; } if ((unsigned long) errsmp > errtol) { if (write_enabled) { util_log(1, "%c%.5f sec %s time tear (%.2lf samp)", sign > 0 ? '+' : '-',errsec, chn->name, errsmp ); } if (sign < 0) errsec *= -1.0; hd.tear = errsec; *action = NRTS_CREATE; } else { /* No time tear detected */ hd.tear = 0.0; *action = NRTS_EXTEND; } } else { hd.tear = 0.0; *action = NRTS_TESTWD; } /* Save the decoded header for later comparisons */ *prev = *crnt; first[hd.chnndx] = FALSE; return &hd; }
void program_long_arg_invalid(struct program *program, char *arg) { ASSERT_PROGRAM(program); util_log(LOG_WARNING, "'%s' is not a valid argument", arg); }
struct nrts_header *nrts_asldecode(char *buffer, int len, int *action) { int i, j, status; long error; struct nrts_chn *chn; struct channel_info *channel; struct seed_minipacket dhead, *prev, *crnt; static struct seed_minipacket prev_dhead[NRTS_MAXCHN]; static char *fid = "nrts_asldecode"; /* Decode the record header */ if (buffer[6] != 'D') { util_log(2, "unexpected non-data record dropped"); return (struct nrts_header *) NULL; } /* Must be a data record at this point */ crnt = &dhead; /* Decode and determine if desired stream. */ sprintf(msgbuf, "corrupt data record dropped: "); if ((status = seed_minihdr(crnt, buffer)) < 0) { sprintf(msgbuf+strlen(msgbuf), "seed_minihdr status %d", status ); util_log(1, msgbuf); return (struct nrts_header *) NULL; } #ifdef DEBUG util_log(1, "%s/%s/%s %s %.3lf %4ld", crnt->sname, crnt->cname, crnt->nname, util_dttostr(crnt->beg, 0), crnt->sint, crnt->nsamp ); #endif /* Find its position in the local lookup table */ for (j = -1, i = 0; j == -1 && i < sys->sta[0].nchn; i++) { if (strcasecmp(crnt->cname, channel_table[i].name) == 0) j = i; } if (j == -1) { util_log(2, "unexpected %s record dropped", crnt->cname); return (struct nrts_header *) NULL; } channel = channel_table + j; /* Fill in the output header for this packet */ sprintf(hd.wfdisc.chan, "%s", crnt->cname); switch (crnt->b1000.format) { case SEED_INT_32: hd.order = LTL_ENDIAN_ORDER; hd.wrdsiz = 4; sprintf(hd.wfdisc.datatype, "s4"); break; case SEED_INT_16: hd.order = LTL_ENDIAN_ORDER; hd.wrdsiz = 2; sprintf(hd.wfdisc.datatype, "s2"); break; default: hd.order = 0; hd.wrdsiz = 0; hd.wfdisc.datatype[0] = 0; break; } hd.wfdisc.nsamp = crnt->nsamp; hd.wfdisc.time = crnt->beg; hd.wfdisc.endtime = crnt->beg + ((crnt->nsamp - 1) * crnt->sint); hd.wfdisc.smprate = (float) (1.0 / crnt->sint); hd.wfdisc.calib = channel->calib; hd.wfdisc.calper = channel->calper; strlcpy(hd.wfdisc.instype, channel->instype, NRTS_INAMLEN+1); hd.hang = channel->hang; hd.vang = channel->vang; hd.beg.code = hd.end.code = ' '; hd.beg.qual = hd.end.qual = 0; /* If no further action is specified, then we are done */ if (*action == NRTS_DECODE) { return &hd; } /* Locate this channel in the NRTS system */ if ((hd.chnndx = channel->index) < 0) { util_log(2, "unexpected %s record dropped", crnt->cname); return (struct nrts_header *) NULL; } chn = sys->sta[0].chn + hd.chnndx; #ifdef DEBUG util_log(1, "%d: %d %d %s %15.4f %15.4f %8.3f %8.3f %s", j, channel->index, hd.chnndx, chn->name, channel->calib, channel->calper, channel->vang, channel->hang, channel->instype ); #endif /* Look for and reject records with duplicate headers or times */ prev = prev_dhead + hd.chnndx; if (write_enabled && channel->count > 0) { if (memcmp(prev, crnt, sizeof(IDA_DHDR)) == 0) { util_log(1, "duplicate %s data header, packet dropped", chn->name); return (struct nrts_header *) NULL; } } /* If filling disk loop, save the sample interval */ if (write_enabled && channel->count == 0) chn->sint = (float) crnt->sint; /* Count time tears */ if (channel->count > 0) { if ((status = seed_timetear(prev, crnt, &error)) != 0) { util_log(1,"%s: seed_timetear failed with status %d: ignored", fid, status ); } else if (error > 1) { util_log(1, "%d sample %s time tear", error, chn->name); } else if (error < -1) { util_log(1, "%d sample %s time tear, packet dropped", error, chn->name ); return (struct nrts_header *) NULL; } } /* Save the decoded header for later comparisons */ *prev = *crnt; ++channel->count; /* For ASL we don't attempt wfdisc maintenance */ *action = NRTS_NOP; return &hd; }
int nrts_asldecodeinit( char *home, char *syscode, struct nrts_sys *sys_ptr, int new_flags, IDA *ida, struct nrts_asl *info, void *unused1, int unused2 ){ ISI_INST inst; ISI_STREAM_NAME name; int i, j, ok, nmatch, nchan; long now; static int initialized = 0; static char *fid = "nrts_asldecodeinit"; /* We can modify the flags many times */ flags = new_flags; /* But the rest of the stuff can be done only once */ if (initialized) return 0; sys = sys_ptr; if (sys->nsta != 1) { util_log(1, "%s: ERROR: nsta = %d", fid, sys->nsta); util_log(1, "%s: ASL systems can have only one station", fid); return -1; } blksiz = info->blksiz; /* Fill out the static parts of the packet header */ hd.wfdisc = wfdisc_null; hd.standx = 0; sprintf(hd.wfdisc.dir, "%s/%s", home, syscode); sprintf(hd.wfdisc.dfile, "%s", NRTS_DATNAME); sprintf(hd.wfdisc.sta, "%s", sys->sta[0].name); hd.hlen = 64; hd.dlen = blksiz - hd.hlen; /* Save all current calib params for this station */ nchan = sys->sta[0].nchn; channel_table = (struct channel_info *) malloc(nchan * sizeof(struct channel_info)); if (channel_table == (struct channel_info *) NULL) { util_log(1, "%s: malloc: %s", fid, syserrmsg(errno)); util_log(1, "%s: tried to allocate %d bytes", fid, (nchan * sizeof(struct channel_info)) ); return -3; } now = time(NULL); nmatch = 0; for (i = 0; i < nchan; i++) { channel_table[i].count = 0; channel_table[i].name = sys->sta[0].chn[i].name; channel_table[i].index = nrts_chnndx(sys->sta, channel_table[i].name); if (channel_table[i].index >= 0) { isiStaChnLocToStreamName(hd.wfdisc.sta, channel_table[i].name, NULL, &name); isiLookupInst(ida->db, &name, now, &inst); channel_table[i].calib = inst.calib; channel_table[i].calper = inst.calper; channel_table[i].vang = inst.vang; channel_table[i].hang = inst.hang; memcpy(channel_table[i].instype, inst.type, NRTS_INAMLEN); channel_table[i].instype[NRTS_INAMLEN] = 0; util_log(1, "ASL channel %s mapped to chan index %d", channel_table[i].name, channel_table[i].index); ++nmatch; } else { channel_table[i].name = (char *) NULL; channel_table[i].calib = 0.0; channel_table[i].calper = -1.0; channel_table[i].hang = -999.0; channel_table[i].vang = -999.0; strlcpy(channel_table[i].instype, "-", NRTS_INAMLEN+1); } } if (nmatch < 1) { util_log(1, "no recognized ASL channels match configuration"); return -5; } /* Note any NRTS configured channels that don't have map entries */ for (i = 0; i < sys->sta[0].nchn; i++) { for (ok = 0, j = 0; ok == 0 && j < nchan; j++) { if (channel_table[j].index == i) ok = 1; } if (ok == 0) { util_log(1, "warning: no ASL mapping for %s stream", sys->sta[0].chn[i].name ); } } /* Initialization complete */ return 0; }
static THREAD_FUNC TapeWriteThread(void *dummy) { int status; size_t blen, remain, want, put, nrec; char *ptr; OLD_MSGQ_MSG *obuf; static char *fid = "TapeWriteThread"; blen = Params->bfact * IDA_BUFSIZ; /* IDA10 OK */ MUTEX_LOCK(&mp); ioerr = 0; MUTEX_UNLOCK(&mp); while (1) { obuf = msgq_get(&Q->obuf, OLD_MSGQ_WAITFOREVER); if (!msgq_chkmsg2(fid, obuf)) { util_log(1, "%s: corrupt message received", fid); ispd_die(MY_MOD_ID + 1); } nrec = *((size_t *) obuf->data); util_log(1, "dumping %ld records to %s", nrec, Params->odev); ptr = obuf->data + sizeof(size_t); remain = nrec * IDA_BUFSIZ; /* IDA10 OK */ while (remain > 0) { want = remain > blen ? blen : remain; nrec = want / IDA_BUFSIZ; /* IDA10 OK */ do { lock_device(); put = mtio_write(tp, ptr, want); if (put == want) { MUTEX_LOCK(&Status->lock); Status->output.nrec += nrec; status = Status->output.state; MUTEX_UNLOCK(&Status->lock); ptr += put; if (ioerr) { clear_alarm(ISP_ALARM_IOERR); ioerr = 0; } } else { if (put != 0) { if (++ioerr == 1) { set_alarm(ISP_ALARM_IOERR); util_log(1, "%s: %s", Params->odev, syserrmsg(errno) ); } MUTEX_LOCK(&Status->lock); ++Status->output.err; MUTEX_UNLOCK(&Status->lock); eject_tape(0); } } release_device(); if (put != want) { if (shutting_down()) { complete_shutdown(); THREAD_EXIT(0); } else { sleep(5); } } } while (put != want); remain -= put; } util_log(1, "tape dump completed OK"); if (shutting_down()) { complete_shutdown(); THREAD_EXIT(0); } MUTEX_LOCK(&mp); if (eject_flag) eject_tape(0); eject_flag = 0; MUTEX_UNLOCK(&mp); msgq_put(&Heap->obuf, obuf); } }
long search(struct old_xfer_req *req, struct nrts_chn *chn, double target, long ldef, long rdef, char *buffer) { struct span *span, dl; long oldest, yngest; static char *fid = "search"; oldest = chn->hdr.oldest; yngest = chn->hdr.yngest; util_log(3, "`%s' search for time %s", chn->name, util_dttostr(target, 0)); util_log(3, "`%s' oldest = %ld, youngest = %ld", chn->name, oldest, yngest); /* First, insure the desired time falls inside the disk buffer */ span = get_span(req, oldest, chn, buffer); dl.beg = span->beg; span = get_span(req, yngest, chn, buffer); dl.end = span->end; sprintf(buffer, "`%s' spans %s ", chn->name, util_dttostr(dl.beg, 0)); util_log(3, "%s to %s", buffer, util_dttostr(dl.end, 0)); if (target < dl.beg) { util_log(3, "`%s' target too old, return %d", chn->name, ldef); return ldef; } if (target > dl.end) { util_log(3, "`%s' target too young, return %d", chn->name, rdef); return rdef; } /* It must be available, so search for it */ /* First case, no wraparound at end of disk buffer */ if (oldest < yngest) { util_log(3, "`%s' no disk loop wrap around"); util_log(3, "`%s' search from oldest (%ld) to youngest (%ld)", chn->name, oldest, yngest ); return do_search(req, target, chn, oldest, yngest, buffer); } /* Second case, data wrap around end of disk buffer */ span = get_span(req, (long) 0, chn, buffer); span->beg -= chn->sint; /* will allow for one sample slop */ if (target > span->beg && target <= dl.end) { util_log(3, "`%s' target is in later half of disk buffer", chn->name); util_log(3, "`%s' search from 0 to youngest (%ld)", chn->name, yngest ); return do_search(req, target, chn, 0, yngest, buffer); } else { span = get_span(req, (long) chn->hdr.nrec - 1, chn, buffer); if (target > dl.beg && target <= span->end) { util_log(3, "`%s' search from oldest (%ld) to eof (%ld)", chn->name, oldest, chn->hdr.nrec - 1 ); return do_search(req,target,chn,oldest,chn->hdr.nrec - 1,buffer); } else { util_log(1, "HELP! I don't know what to do!"); util_log(1, "target = %s", util_dttostr(target, 0)); util_log(1, "dl beg = %s", util_dttostr(dl.beg, 0)); util_log(1, "dl end = %s", util_dttostr(dl.end, 0)); util_log(1, "end of dl file time = %s", util_dttostr(span->end, 0)); ack(0); die(1); } } util_log(1, "%s: FATAL PROGRAM LOGIC ERROR!", fid); util_log(1, "%s: target = %s", fid, util_dttostr(target, 0)); util_log(1, "%s: dl beg = %s", fid, util_dttostr(dl.beg, 0)); util_log(1, "%s: dl end = %s", fid, util_dttostr(dl.end, 0)); }
int isp_savcnf(char *fname, struct isp_dascnf *dascnf) { int i; FILE *fp; struct isp_dascnf newcnf; static char *fid = "isp_savcnf"; if ((fp = fopen(fname, "w")) == (FILE *) NULL) { util_log(1, "%s: fopen: %s: %s", fid, fname, syserrmsg(errno)); return -1; } fprintf(fp, "# DAS configuration table\n"); fprintf(fp, "#\n"); fprintf(fp, "# The \"#\" char is the comment delimiter. All comments\n"); fprintf(fp, "# and blank lines are ignored.\n"); fprintf(fp, "#\n"); fprintf(fp, "# Stream definition is up to 25 lines of the form:\n"); fprintf(fp, "#\n"); fprintf(fp, "# Stream = Strm On Chan Filt Mode\n"); fprintf(fp, "#\n"); fprintf(fp, "# where\n"); fprintf(fp, "#\n"); fprintf(fp, "# Strm = Stream number (0 thru 24)\n"); fprintf(fp, "# On = y if stream is active, n if disabled\n"); fprintf(fp, "# Chan = DAS input channel number\n"); fprintf(fp, "# Filt = DAS filter code\n"); fprintf(fp, "# Mode = c for continuous, t for triggered\n"); fprintf(fp, "#\n"); fprintf(fp, "# Strm On Chan Filt Mode\n"); fprintf(fp, "\n"); for (i = 0; i < DAS_MAXSTREAM; i++) { if (dascnf->stream[i].active) { dascnf->stream[i].active = 1; dascnf->stream[i].mode = dascnf->stream[i].mode ? 1 : 0; fprintf(fp, "Stream = %4d y %4d %4d %c\n", i, dascnf->stream[i].channel, dascnf->stream[i].filter, dascnf->stream[i].mode ? 't' : 'c' ); } else { fprintf(fp, "Stream = %4d n -1 -1 x\n", i); } } fprintf(fp, "\n"); fprintf(fp, "# Event Detector\n"); fprintf(fp, "\n"); fprintf(fp, "# detector channels (blank disables detector)\n"); fprintf(fp, "Chans ="); for (i = 0; i < dascnf->detect.nchan; i++) { fprintf(fp, " %d", dascnf->detect.chan[i]); } fprintf(fp, "\n"); fprintf(fp, "\n"); fprintf(fp, "# detector channel whose amplitude gets reported\n"); fprintf(fp, "Key = %d\n", dascnf->detect.key); fprintf(fp, "\n"); fprintf(fp, "# short term average, SAMPLES\n"); fprintf(fp, "STA = %d\n", dascnf->detect.sta); fprintf(fp, "\n"); fprintf(fp, "# long term average, SAMPLES\n"); fprintf(fp, "LTA = %d\n", dascnf->detect.lta); fprintf(fp, "\n"); fprintf(fp, "# STA/LTA trigger ratio\n"); fprintf(fp, "Thresh = %.4f\n", 10000.0 / (float) dascnf->detect.thresh); fprintf(fp, "\n"); fprintf(fp, "# minimum number of voters to declare a trigger\n"); fprintf(fp, "Voters = %d\n", dascnf->detect.voters); fprintf(fp, "\n"); fprintf(fp, "# maximum trigger length, SAMPLES\n"); fprintf(fp, "MaxTrig = %d\n", dascnf->detect.maxtrig); fprintf(fp, "\n"); fprintf(fp, "# pre-event memory, in RECORDS\n"); fprintf(fp, "Pre-event = %d\n", dascnf->detect.preevent); fclose(fp); if (isp_readcnf(fname, &newcnf) != 0) return -2; for (i = 0; i < DAS_MAXSTREAM; i++) { if (dascnf->stream[i].active != newcnf.stream[i].active) return -3; if (dascnf->stream[i].active) { if (dascnf->stream[i].channel != newcnf.stream[i].channel) { return -4; } if (dascnf->stream[i].filter != newcnf.stream[i].filter) { return -5; } if (dascnf->stream[i].mode != newcnf.stream[i].mode) { return -6; } } } if (dascnf->detect.nchan != newcnf.detect.nchan) return -7; if (dascnf->detect.nchan == 0) return 0; /* detector disabled */ for (i = 0; i < dascnf->detect.nchan; i++) { if (dascnf->detect.chan[i] != newcnf.detect.chan[i]) return -8; } if (dascnf->detect.key != newcnf.detect.key) return -9; if (dascnf->detect.sta != newcnf.detect.sta) return -10; if (dascnf->detect.lta != newcnf.detect.lta) return -11; #ifdef STRICT_CHECKS if (dascnf->detect.thresh != newcnf.detect.thresh) return -12; #endif if (dascnf->detect.voters != newcnf.detect.voters) return -13; if (dascnf->detect.maxtrig != newcnf.detect.maxtrig) return -14; if (dascnf->detect.preevent != newcnf.detect.preevent) return -15; return 0; }
int send_data(void) { static time_t delay, now, last_hbeat = 0; int i, j, k, nsent, done, have_data; static char *fid = "send_data"; /* Loop over all channels looking for data to send */ nsent = 0; do { /* start off assuming we don't have, and won't have, anything */ done = 1; have_data = 0; /* now loop through all channels to see if this is true */ for (i = 0; i < edes->nsys; i++) { for (j = 0; j < SYS->nsta; j++) { for (k = 0; k < SSTA->nchn; k++) { /* if waiting for data, see if any are ready */ if (ICHN->status == EDES_WAITING) { set_indices(edes,i,j,k,buffer); } /* send anything we have to send */ if (ICHN->status == EDES_READY) { transfer(cnf,FORM,COMP,edes,i,j,k,buffer,peer); ++nsent; } /* if we have more to do, set appropriate flag(s) */ if (ICHN->status == EDES_READY) { have_data = 1; done = 0; } else if (ICHN->status == EDES_WAITING) { done = 0; } } } } } while (have_data); /* Quit if we have sent all requested data */ if (done) { util_log(2, "%s: waveform request processing complete", fid); return EDES_DONE; } /* If we didn't send anything, send a heartbeat */ if (nsent == 0) { now = time(NULL); delay = now - last_hbeat; if (delay > HeartbeatInterval) { util_log(2, "send HEARTBEAT (delay = %ld)", now - last_hbeat); Xfer_Ack(edes->osd, XFER_HEARTBEAT); last_hbeat = now; } } return EDES_WAITING; }
static THREAD_FUNC TapeWatchThread(void *dummy) { int boot_flag; static struct mtget info; static char *fid = "TapeWatchThread"; boot_flag = 1; util_log(2, "TAPE_WATCH thread started, tid = %d", THREAD_SELF()); set_alarm(ISP_ALARM_OFFLINE); while (1) { MUTEX_LOCK(&mp); /* if we were offline, see if a tape is now present */ if (prev_state == MTIO_OFFLINE) { if ((tp = mtio_open(Params->odev, "w")) != null) { if (mtio_status(tp, &info) != -1) { crnt_state = MTIO_ONLINE; clear_alarm(ISP_ALARM_OFFLINE); if (ioerr) { clear_alarm(ISP_ALARM_IOERR); ioerr = 0; } check_tape(boot_flag); } else { mtio_close(tp); tp = (TAPE *) NULL; } } if (tp == null && shutting_down()) { util_log(1, "shutting down w/o final flush (no tape)"); complete_shutdown(); } /* if we were on-line, make sure we still are that way */ /* THIS CODE DOES NOT WORK AND I DON'T KNOW HOW TO MAKE IT WORK! */ } else if (mtio_nop(tp) < 0 || mtio_status(tp, &info) < 0) { mtio_close(tp); tp = (TAPE *) tp; set_alarm(ISP_ALARM_OFFLINE); crnt_state = MTIO_OFFLINE; } if (crnt_state != prev_state) { MUTEX_LOCK(&Status->lock); if (crnt_state == MTIO_ONLINE) { Status->output.state = ISP_OUTPUT_ONLINE; Status->output.err = 0; Status->output.file = info.mt_fileno + 1; Status->output.nrec = 0; } else { Status->output.state = ISP_OUTPUT_OFFLINE; } MUTEX_UNLOCK(&Status->lock); } prev_state = crnt_state; boot_flag = 0; if (pending && (crnt_state == MTIO_ONLINE)) { pending = 0; SEM_POST(&sp); } MUTEX_UNLOCK(&mp); sleep(10); } }
int isp_setcnf(struct isp_dascnf *dascnf) { static char *cnf = ISP_CNF_FILE; static char *tmp = ISP_TMPCNF_FILE; static char *def = ISP_DEFCNF_FILE; static char *bak = ISP_BAKCNF_FILE; static char *fid = "isp_setcnf"; /* Restore backup configuration */ if (dascnf->flag == ISP_DASCNF_BAK) { util_log(1, "restoring backup DAS configuration"); if (isp_readcnf(bak, dascnf) != 0) { util_log(1, "%s: can't load backup DAS configuration", fid); return -1; } unlink(cnf); if (rename(bak, cnf) != 0) { util_log(1, "%s: rename(%s, %s) failed: %s", fid, bak, cnf, syserrmsg(errno) ); return -2; } /* Load default configuration */ } else if (dascnf->flag == ISP_DASCNF_DEF) { util_log(1, "restoring default DAS configuration"); if (isp_readcnf(def, dascnf) != 0) { util_log(1, "%s: can't load default DAS configuration", fid); return -3; } unlink(bak); if (rename(cnf, bak) != 0) { util_log(1, "%s: WARNING: unable to backup cnf: %s", fid, syserrmsg(errno) ); } unlink(cnf); if (util_cat(def, cnf, (char *) NULL) != 0) { util_log(1, "%s: can't copy default DAS configuration", fid); if (rename(cnf, bak) == 0 && isp_getcnf(dascnf) == 0) { util_log(1, "%s: backup configuration restored", fid); return -4; } else { util_log(1, "%s: can't restore backup configuration", fid); return -5; } } /* Load a new configuration */ } else if (dascnf->flag == ISP_DASCNF_SET) { util_log(1, "saving new DAS configuration"); if (isp_savcnf(tmp, dascnf) != 0) { util_log(1, "%s: unable to save new DAS configuration", fid); return -5; } unlink(bak); if (rename(cnf, bak) != 0) { util_log(1, "%s: WARNING: unable to backup cnf: %s", fid, syserrmsg(errno) ); } unlink(cnf); if (rename(tmp, cnf) != 0) { util_log(1, "%s: can't save new DAS configuration", fid); if (rename(cnf, bak) == 0 && isp_getcnf(dascnf) == 0) { util_log(1, "%s: backup configuration restored", fid); return -6; } else { util_log(1, "%s: can't restore backup configuration", fid); return -7; } } /* Ignore anything else */ } else { util_log(1, "%s: unrecognized flag `0x%x' ignored", fid, dascnf->flag ); return 0; } /* Now, read active configuration to make sure everything is OK */ return isp_getcnf(dascnf); }
int init(int argc, char **argv, struct param *param) { unsigned long tictol = 0; void *opt; int i, status, flags, rev; char *home = NULL; char *iname = NULL; #ifdef WIN32 char *log = NULL; #else char *log = "syslogd"; #endif char *syscode = NULL; char *SYSCODE = NULL; int debug = NRTS_DEFLOG; int background = 1; int stamp_year = 1; int ctsrts = 0; int leapfix = 0; int strict = 1; int addseqno = 1; int forcettst = 0; float minseg = 0.0; time_t now; struct tm *gmt; struct nrts_files *file; char *dbspec = (char *) NULL; DBIO *db = NULL; char *mapname = NULL; LOGIO *lp; static struct nrts_sys *sys; static struct nrts_ida *info; static struct nrts_mmap mmap, *map; static char *fid = "init"; XferLibInit(); param->sys = NULL; param->beg = NRTS_OLDEST; param->end = NRTS_YNGEST; param->keeparg = NULL; param->timeout = -1; param->tto = 0; param->retry = 1; param->port = -1; param->tapfile = (char *) NULL; param->ida = (IDA *) NULL; param->fwd = 60; param->dbgpath = NULL; param->IsiStatPath = NULL; param->blksiz = 512; /* ASL network block size */ param->tolerance = 1; /* largest tolerated time tear in samples */ param->req.protocol = 0x01; param->req.type = XFER_WAVREQ; param->req.timeout = NRTS_DEFTCPTO; param->req.sndbuf = 0; param->req.rcvbuf = 0; param->req.preamble.ver01.client_id = getpid(); param->req.preamble.ver01.format = XFER_CNFNRTS; param->wavreq = ¶m->req.request.wav.ver01; param->wavreq->keepup = 0; param->wavreq->comp = XFER_CMPIGPP; /* Get command line arguments */ for (i = 1; i < argc; i++) { if (strncmp(argv[i], "home=", strlen("home=")) == 0) { home = argv[i] + strlen("home="); } else if (strncmp(argv[i], "debug=", strlen("debug=")) == 0) { debug = atoi(argv[i]+strlen("debug=")); } else if (strncmp(argv[i], "fwd=", strlen("fwd=")) == 0) { param->fwd = atoi(argv[i]+strlen("fwd=")); } else if (strncmp(argv[i], "log=", strlen("log=")) == 0) { log = argv[i] + strlen("log="); } else if (strncmp(argv[i], "dbgpath=", strlen("dbgpath=")) == 0) { param->dbgpath = argv[i] + strlen("dbgpath="); } else if (strncmp(argv[i], "to=", strlen("to=")) == 0) { param->timeout = atoi(argv[i] + strlen("to=")); param->req.timeout = param->timeout; } else if (strncmp(argv[i], "tto=", strlen("tto=")) == 0) { param->tto = atoi(argv[i] + strlen("tto=")); } else if (strncmp(argv[i], "minseg=", strlen("minseg=")) == 0) { minseg = atof(argv[i] + strlen("minseg=")); } else if (strncmp(argv[i], "bg=", strlen("bg=")) == 0) { background = atoi(argv[i] + strlen("bg=")); } else if (strncmp(argv[i], "port=", strlen("port=")) == 0) { param->port = atoi(argv[i] + strlen("port=")); } else if (strncmp(argv[i], "tcpbuf=", strlen("tcpbuf=")) == 0){ param->req.sndbuf = atoi(argv[i] + strlen("tcpbuf=")); param->req.rcvbuf = param->req.sndbuf; } else if (strncmp(argv[i], "snd=", strlen("snd=")) == 0) { param->req.sndbuf = atoi(argv[i] + strlen("snd=")); } else if (strncmp(argv[i], "rcv=", strlen("rcv=")) == 0) { param->req.rcvbuf = atoi(argv[i] + strlen("rcv=")); } else if (strncmp(argv[i], "bs=", strlen("bs=")) == 0) { param->blksiz = atoi(argv[i] + strlen("bs=")); } else if (strncmp(argv[i], "comp=", strlen("comp=")) == 0) { param->wavreq->comp = atoi(argv[i] + strlen("comp=")); } else if (strncmp(argv[i], "tolerance=", strlen("tolerance=")) == 0) { param->tolerance = atoi(argv[i] + strlen("tolerance=")); } else if (strcmp(argv[i], "+retry") == 0) { param->retry = 1; } else if (strcmp(argv[i], "-retry") == 0) { param->retry = 0; } else if (strcmp(argv[i], "++retry") == 0) { param->retry = -1; } else if (strcmp(argv[i], "+keepup") == 0) { param->wavreq->keepup = 1; } else if (strcmp(argv[i], "-keepup") == 0) { param->wavreq->keepup = 0; } else if (strncmp(argv[i], "tap=", strlen("tap=")) == 0) { param->tapfile = strdup(argv[i] + strlen("tap=")); } else if (strncmp(argv[i], "chan=", strlen("chan=")) == 0) { param->keeparg = strdup(argv[i] + strlen("chan=")); } else if (strcmp(argv[i], "+ctsrts") == 0) { ctsrts = 1; } else if (strcmp(argv[i], "-ctsrts") == 0) { ctsrts = 0; } else if (strcmp(argv[i], "+strict") == 0) { strict = 1; } else if (strcmp(argv[i], "-strict") == 0) { strict = 0; } else if (strcmp(argv[i], "+forcettst") == 0) { forcettst = 1; } else if (strcmp(argv[i], "-forcettst") == 0) { forcettst = 0; } else if (strcmp(argv[i], "+seqno") == 0) { addseqno = 1; } else if (strcmp(argv[i], "-seqno") == 0) { addseqno = 0; } else if (strcmp(argv[i], "+leapfix") == 0) { leapfix = 1; } else if (strcmp(argv[i], "-leapfix") == 0) { leapfix = 0; } else if (strcmp(argv[i], "+year") == 0) { stamp_year = 1; } else if (strcmp(argv[i], "-year") == 0) { stamp_year = 0; } else if (strncmp(argv[i], "if=", strlen("if=")) == 0) { iname = argv[i] + strlen("if="); } else if (strncmp(argv[i], "beg=", strlen("beg=")) == 0) { param->beg = util_attodt(argv[i] + strlen("beg=")); } else if (strncmp(argv[i], "end=", strlen("end=")) == 0) { param->end = util_attodt(argv[i] + strlen("end=")); param->wavreq->keepup = 0; } else if (strncmp(argv[i], "db=", strlen("db=")) == 0) { dbspec = argv[i] + strlen("db="); } else if (strncmp(argv[i], "tictol=", strlen("tictol=")) == 0) { tictol = (unsigned long) atol(argv[i] + strlen("tictol=")); } else if (strncmp(argv[i], "isilog=", strlen("isilog=")) == 0) { param->IsiStatPath = strdup(argv[i] + strlen("isilog=")); } else { if (syscode != NULL) { help(argv[0]); } else { syscode = argv[i]; } } } flags = NRTS_TODISK; if (strict) flags |= NRTS_STRICT; if (addseqno) flags |= NRTS_ADDSEQNO; if (forcettst) flags |= NRTS_FORCETTST; if (syscode == NULL) { fprintf(stderr,"%s: missing system code\n", argv[0]); help(argv[0]); } /* Get file names */ file = nrts_files(&home, syscode); /* Switch from stderr to log file */ if (log == NULL) log = file->log; SYSCODE = (strcmp(log, "syslogd")==0) ? util_ucase(strdup(syscode)) : NULL; util_logopen(log, 1, NRTS_MAXLOG, debug, SYSCODE, argv[0]); util_log(1, VersionIdentString); lp = InitLogging(argv[0], log, debug == NRTS_DEFLOG ? FALSE : TRUE); /* Get system map */ map = &mmap; if (nrts_mmap(file->sys, "r+", NRTS_SYSTEM, map) != 0) { util_log(1, "%s: nrts_mmap failed", fid); return -1; } param->sys = sys = (struct nrts_sys *) map->ptr; /* Load the database */ if ((db = dbioOpen(dbspec, NULL)) == NULL) { if (dbspec == NULL) { fprintf(stderr, "%s: undefined database. Set DBIO_DATABASE or use db=spec\n", argv[0]); } else { fprintf(stderr, "%s: can't load database `%s': %s\n", argv[0], dbspec, syserrmsg(errno)); } return -1; } util_log(1, "%s database loaded OK\n", db->dbid); /* Lock system file and check for consistency */ util_log(2, "locking system"); /* This is not applicable for WIN32 */ #ifndef WIN32 if (sys->pid != 0 && kill(sys->pid, SIGHUP) == 0) { util_log(1, "system is already active (pid %d)", sys->pid); return -1; } #endif if (nrts_wlock(map) == -1) { if (errno == EACCES) { util_log(1,"exit: system is locked (pid=%d)\n", argv[0], sys->pid); exit(1); } else { util_log(1,"%s: nrts_wlock: %s", fid, syserrmsg(errno)); return -1; } } util_log(2, "checking disk loop integrity"); if ((status = nrts_chksys(sys)) != 0) { util_log(1, "correcting disk loop (status %d)", status); if ((status = nrts_fixsys(sys)) != 0) { util_log(1,"FATAL: unable to repair disk loop (status %d)", status); return -1; } } util_log(2, "disk loop is intact"); /* Store our process id */ sys->pid = getpid(); /* Load system specific information */ if (sys->type == NRTS_IDA) { info = (struct nrts_ida *) sys->info; param->wavreq->format = XFER_WAVIDA; rev = info->rev; mapname = info->map; } else if (sys->type == NRTS_IDA10) { param->wavreq->format = XFER_WAVIDA; rev = 10; mapname = NULL; } else { util_log(1, "%s: unsupported system type = %d", fid, sys->type); return -1; } if ((param->ida = idaCreateHandle(sys->sta[0].name, rev, mapname, db, NULL, flags)) == NULL) { util_log(1, "idaCreateHandle failed"); return -1; } /* Insure the binary wfdiscs are current */ if ((status = nrts_fixwd(file, map, home, syscode, param->ida)) != 0) { util_log(1, "nrts_fixwd: status %d", status); return -1; } /* Allocate read buffers */ util_log(2, "allocating read buffers"); param->len = sys->reclen; if ((param->prev = (char *) calloc(param->len, sizeof(char))) == NULL) { util_log(1, "malloc: %s", syserrmsg(errno)); return -1; } if ((param->crnt = (char *) calloc(param->len, sizeof(char))) == NULL) { util_log(1, "malloc: %s", syserrmsg(errno)); return -1; } /* Initialize data input routine */ util_log(2, "initializing data input routines"); if (init_getrec(home,syscode,iname,param,ctsrts,leapfix,&flags) != 0) { util_log(1, "%s: init_getrec failed", fid); return -1; } /* Initialize the disk writer */ util_log(2, "initializing disk i/o routines"); opt = (void *) &tictol; if (nrts_iniwrtdl(home, syscode, sys, flags, param->ida, opt) != 0) { util_log(1, "%s: nrts_iniwrtdl failed", fid); return -1; } /* For IDA rev 3 (aka MK4) data, determine initial year value */ /* This assumes that the system clock is more or less correct */ if (sys->type == NRTS_IDA && param->ida->rev.value == 3) { if (stamp_year) { now = time(NULL); gmt = gmtime(&now); info->mk4_year = gmt->tm_year + 1900; util_log(1, "IDA MK4 system... will use %hd for year", info->mk4_year); } else { info->mk4_year = -1; util_log(1, "IDA MK4 system... will believe datalogger year stamp"); } } /* Finally (sigh) announce if we are using Fels' shifts or not */ if (sys->type == NRTS_IDA && info->shift) util_log(1, "fels' shifts will be applied (sigh)"); /* Go into the background and update process id */ util_log(1, "initialization complete"); util_logclose(); #ifndef WIN32 if (background && util_bground(0, 0) < 0) { util_logopen(log, 1, NRTS_MAXLOG, debug, SYSCODE, argv[0]); util_log(1, "util_bground: %s", syserrmsg(errno)); return -1; } #endif util_logopen(log, 1, NRTS_MAXLOG, debug, SYSCODE, argv[0]); util_log(2, "updating pid"); param->req.preamble.ver01.client_id = sys->pid = getpid(); /* start signal handler and wfdisc flush threads */ SetSys(sys); StartSignalHandler(); StartFlushThread(param); /* begin data acquistion */ util_log(1, "starting data acquisition"); sys->start = time(NULL); return 0; }
static void LogEdesChnReq(int loglevel, struct edes_chnreq *chn) { util_log(loglevel, " chn name=%s, beg=%.3lf, end=%.3lf", chn->name, chn->beg, chn->end); }
int isp_readcnf(char *fname, struct isp_dascnf *cnf) { FILE *fp; int i, j, status, lineno = 0, ntoken, errors; char *token[MAX_TOKEN]; char buffer[BUFLEN]; static char *fid = "isp_readcnf"; for (i = 0; i < DAS_MAXSTREAM; i++) cnf->stream[i].active = 0; cnf->detect.nchan = 0; if ((fp = fopen(fname, "r")) == (FILE *) NULL) { util_log(1, "%s: fopen: %s: %s", fid, fname, syserrmsg(errno)); return -1; } errors = 0; while ((status = util_getline(fp, buffer, BUFLEN, '#', &lineno)) == 0) { ntoken = util_parse(buffer, token, DELIMITERS, MAX_TOKEN, 0); if (strcasecmp(token[0], "stream") == 0) { if (ntoken != 6) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } i = atoi(token[1]); if (i < 0 || i >= DAS_MAXSTREAM) { util_log(1, "%s: illegal stream code, %s line %d", fid, fname, lineno ); fclose(fp); return -2; } if (strcasecmp(token[2], "y") == 0) { cnf->stream[i].active = 1; } else if (strcasecmp(token[2], "n") == 0) { cnf->stream[i].active = 0; } else { util_log(1, "%s: illegal on/off flag, %s line %d", fid, fname, lineno ); ++errors; } if (cnf->stream[i].active) { cnf->stream[i].channel = atoi(token[3]); if ( cnf->stream[i].channel < 0 || cnf->stream[i].channel > DAS_MAXCHAN ) { util_log(1, "%s: illegal channel, %s line %d", fid, fname, lineno ); ++errors; } cnf->stream[i].filter = atoi(token[4]); if (strcasecmp(token[5], "c") == 0) { cnf->stream[i].mode = 0; } else if (strcasecmp(token[5], "t") == 0) { cnf->stream[i].mode = 1; } else { util_log(1, "%s: illegal mode flag, %s line %d", fid, fname, lineno ); ++errors; } } else { cnf->stream[i].channel = -1; cnf->stream[i].filter = -1; cnf->stream[i].mode = 0; } } else if (strcasecmp(token[0], "Chans") == 0) { cnf->detect.bitmap = 0; cnf->detect.nchan = ntoken - 1; for (i = 0; i < cnf->detect.nchan; i++) { cnf->detect.chan[i] = atoi(token[i+1]); if ( cnf->detect.chan[i] < 0 || cnf->detect.chan[i] > DAS_MAXCHAN ) { util_log(1, "%s: illegal detector chan, %s line %d", fid, fname, lineno ); ++errors; } else { cnf->detect.bitmap |= 1 << cnf->detect.chan[i]; } } } else if (strcasecmp(token[0], "Key") == 0) { if (ntoken != 2) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } cnf->detect.key = atoi(token[1]); } else if (strcasecmp(token[0], "STA") == 0) { if (ntoken != 2) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } cnf->detect.sta = atoi(token[1]); } else if (strcasecmp(token[0], "LTA") == 0) { if (ntoken != 2) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } cnf->detect.lta = atoi(token[1]); } else if (strcasecmp(token[0], "Thresh") == 0) { if (ntoken != 2) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } cnf->detect.thresh = (int) (10000.0 / atof(token[1])); } else if (strcasecmp(token[0], "Voters") == 0) { if (ntoken != 2) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } cnf->detect.voters = atoi(token[1]); } else if (strcasecmp(token[0], "MaxTrig") == 0) { if (ntoken != 2) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } cnf->detect.maxtrig = atol(token[1]); } else if (strcasecmp(token[0], "Pre-event") == 0) { if (ntoken != 2) { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); ++errors; } cnf->detect.preevent = atoi(token[1]); } else { util_log(1, "%s: syntax error near %s line %d", fid, fname, lineno ); util_log(1, "%s: unrecognized identifer `%s'\n", fid, token[0] ); ++errors; } } fclose(fp); cnf->flag = ISP_DASCNF_SET; return errors; }