SRV_CONF_T *conf_parse(const char *path) { FILE_HANDLE fp; SRV_CONF_T *pConf = NULL; KEYVAL_PAIR_T kv; KEYVAL_PAIR_T *pKv = NULL; KEYVAL_PAIR_T *pKvPrev = NULL; char buf[1024]; unsigned int numEntries = 0; if(!path) { return NULL; } if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) { LOG(X_CRITICAL("Unable to open file for reading: %s"), path); return NULL; } pConf = (SRV_CONF_T *) avc_calloc(1, sizeof(SRV_CONF_T)); while(fileops_fgets(buf, sizeof(buf) - 1, fp)) { if(conf_parse_keyval(&kv, buf, strlen(buf), '=', 0) > 0) { if((pKv = (KEYVAL_PAIR_T *) avc_calloc(1, sizeof(KEYVAL_PAIR_T))) == NULL) { LOG(X_CRITICAL("Failed to allocate %d"), sizeof(KEYVAL_PAIR_T)); break; } memcpy(pKv->key, kv.key, sizeof(pKv->key)); memcpy(pKv->val, kv.val, sizeof(pKv->val)); if(pKvPrev == NULL) { pConf->pKeyvals = pKv; } else { pKvPrev->pnext = pKv; } pKvPrev = pKv; numEntries++; } } fileops_Close(fp); return pConf; }
static int cpu_snapshot(CPU_SNAPSHOT_T *pS) { int rc = -1; #if defined(__linux__) FILE_HANDLE fp; char buf[1024]; const char *path = "/proc/stat"; const char *p; int have_cpu = 0; if(!pS) { return -1; } memset(pS, 0, sizeof(CPU_SNAPSHOT_T)); if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) { LOG(X_ERROR("Unable to open %s for reading. "ERRNO_FMT_STR), path, ERRNO_FMT_ARGS); return -1; } while(fileops_fgets(buf, sizeof(buf) -1, fp)) { if(!have_cpu && !strncmp(buf, "cpu ", 4)) { p = &buf[4]; MOVE_WHILE_SPACE(p); sscanf(p, "%Lf %Lf %Lf %Lf", &pS->tmuser, &pS->tmnice, &pS->tmsys, &pS->tmidle); pS->tmtot = pS->tmuser + pS->tmnice + pS->tmsys + pS->tmidle; have_cpu = 1; } else if(!strncmp(buf, "cpu", 3)) { pS->numcpu++; } } fileops_Close(fp); if(have_cpu > 0) { rc = 0; } #else // (__linux__) LOG(X_ERROR("CPU snapshot not implemented on this architecure")); #endif // (__linux__) return rc; }
int metafile_open(const char *path, META_FILE_T *pMetaFile, int readIgnores, int readTitles) { int rc = 0; FILE_HANDLE fp; char *p; int match = 0; PARSE_ENTRY_DATA_T parseData; ENTRY_IGNORE_T *pIgnore; ENTRY_IGNORE_T *pIgnorePrev = NULL; ENTRY_META_DESCRIPTION_T *pDesc; ENTRY_META_DESCRIPTION_T *pDescPrev = NULL; char buf[1024]; if(!path || !pMetaFile) { return -1; } if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) { LOG(X_ERROR("Unable to open metafile for reading: %s (ignore list:%d)"), path, readIgnores); return -1; } VSX_DEBUG_METAFILE( LOG(X_DEBUG("META - metafile_open: '%s', readIgnores: %d, readTitles: %d"), path, readIgnores, readTitles)); pMetaFile->filestr[0] = '\0'; pMetaFile->linkstr[0] = '\0'; pMetaFile->instr[0] = '\0'; pMetaFile->xcodestr[0] = '\0'; pMetaFile->userpass[0] = '\0'; pMetaFile->methodBits = 0; memset(&parseData, 0, sizeof(parseData)); parseData.path = path; while(fileops_fgets(buf, sizeof(buf) - 1, fp)) { parseData.linenum++; p = buf; while(*p == ' ' || *p == '\t') { p++; } if(*p == '#' || *p == '\r' || *p == '\n') { continue; } // // Reset the parse context storage // reset_parsedata_ctxt(&parseData); // // We allow multiple configuration items on one line, separated by comma // rc = strutil_parse_csv(cbparse_entry_metafile, &parseData, p); if(parseData.flags == 0) { continue; } if(!(readIgnores || readTitles)) { handle_parsed_line(&parseData, pMetaFile, path, &match); } // // We're only interested in reading the ignore list // if(readIgnores && parseData.ignore[0] != '\0') { if(!(pIgnore = (ENTRY_IGNORE_T *) avc_calloc(1, sizeof(ENTRY_IGNORE_T)))) { rc = -1; break; } strncpy(pIgnore->filename, parseData.ignore, sizeof(pIgnore->filename)); if(pIgnorePrev) { pIgnorePrev->pnext = pIgnore; } else { pMetaFile->pignoreList = pIgnore; } pIgnorePrev = pIgnore; } // // We're only interested in reading the resource title name // if(readTitles && parseData.title[0] != '\0' && parseData.filename[0] != '\0') { if(!(pDesc = (ENTRY_META_DESCRIPTION_T *) avc_calloc(1, sizeof(ENTRY_META_DESCRIPTION_T)))) { rc = -1; break; } strncpy(pDesc->title, parseData.title, sizeof(pDesc->title)); strncpy(pDesc->filename, parseData.filename, sizeof(pDesc->filename)); if(pDescPrev) { pDescPrev->pnext = pDesc; } else { pMetaFile->pDescriptionList = pDesc; } pDescPrev = pDesc; } } fileops_Close(fp); VSX_DEBUG_METAFILE( LOG(X_DEBUG("META - metafile_open done '%s', meta-devicefilter:'%s', meta-profilefilter: '%s' returning rc: %d, " "file: '%s', link: '%s', input: '%s', xcode: '%s', digestauth: '%s', methods: '%s', id: '%s'" ), path, pMetaFile->devicefilterstr, pMetaFile->profilefilterstr, rc, pMetaFile->filestr, pMetaFile->linkstr, pMetaFile->instr, pMetaFile->xcodestr, pMetaFile->userpass, devtype_dump_methods(pMetaFile->methodBits, buf, sizeof(buf)), pMetaFile->id)); return rc; }
int metafile_findprofs(const char *path, const char *devname, char profs[][META_FILE_PROFILESTR_MAX], unsigned int max) { int rc = 0; FILE_HANDLE fp; char *p; unsigned int idx = 0; PARSE_ENTRY_DATA_T parseData; char buf[2048]; PROFILE_LIST_ALL_T foundProfs; if(!path || !profs) { return -1; } if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) { LOG(X_ERROR("Unable to open metafile for reading profiles: %s"), path); return -1; } VSX_DEBUG_METAFILE( LOG(X_DEBUG("META - metafile_findprofs: '%s', devname: '%s'"), path, devname)); memset(&foundProfs, 0, sizeof(foundProfs)); memset(&parseData, 0, sizeof(parseData)); parseData.path = path; while(fileops_fgets(buf, sizeof(buf) - 1, fp)) { parseData.linenum++; p = buf; while(*p == ' ' || *p == '\t') { p++; } if(*p == '#') { continue; } // // Reset the parse context storage // reset_parsedata_ctxt(&parseData); strutil_parse_csv(cbparse_entry_metafile, &parseData, p); if(parseData.flags == 0) { continue; } check_profs(&parseData, devname, &foundProfs, path); } fileops_Close(fp); rc = 0; for(idx = 0; idx < foundProfs.profs_devmatch.count && idx < max; idx++) { if(!find_prof(foundProfs.profs_devmatch.profiles[idx], profs, rc)) { strncpy(profs[rc], foundProfs.profs_devmatch.profiles[idx], META_FILE_PROFILESTR_MAX); //fprintf(stderr, "ADDING DEVMATCH[%d] '%s'\n", rc, profs[rc]); rc++; } } if(foundProfs.profs_devmatch.count == 0 || !foundProfs.profs_devmatch.haveCatchAll) { for(idx = 0; idx < foundProfs.profs_nodevmatch.count && idx < max; idx++) { if(!find_prof(foundProfs.profs_nodevmatch.profiles[idx], profs, rc)) { strncpy(profs[rc], foundProfs.profs_nodevmatch.profiles[idx], META_FILE_PROFILESTR_MAX); //fprintf(stderr, "ADDING NODEVMATCH[%d] '%s'\n", rc, profs[rc]); rc++; } } } return rc; }
static int mem_snapshot(MEM_SNAPSHOT_T *pM, TIME_VAL tmnow) { int rc = -1; #if defined(__linux__) FILE_HANDLE fp; char buf[1024]; char *p; const char *path = "/proc/meminfo"; int haveCached = 0; int haveBuffers = 0; int haveFree = 0; if(!pM) { return -1; } memset(pM, 0, sizeof(MEM_SNAPSHOT_T)); if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) { LOG(X_ERROR("Unable to open %s for reading. "ERRNO_FMT_STR), path, ERRNO_FMT_ARGS); return -1; } while(fileops_fgets(buf, sizeof(buf) -1, fp)) { if(!strncasecmp(buf, "MemTotal:", 9)) { p = &buf[9]; MOVE_WHILE_SPACE(p); pM->memTotal = strutil_read_numeric(p, 0, 0, 0); } else if(!strncasecmp(buf, "MemFree:", 8)) { p = &buf[8]; MOVE_WHILE_SPACE(p); pM->memFree = strutil_read_numeric(p, 0, 0, 0); haveFree = 1; } else if(!strncasecmp(buf, "Buffers:", 8)) { p = &buf[8]; MOVE_WHILE_SPACE(p); pM->buffers = strutil_read_numeric(p, 0, 0, 0); haveBuffers = 1; } else if(!strncasecmp(buf, "Cached:", 7)) { p = &buf[7]; MOVE_WHILE_SPACE(p); pM->cached = strutil_read_numeric(p, 0, 0, 0); haveCached = 1; } if(pM->memTotal > 0 && haveFree && haveBuffers && haveCached) { pM->memUsed = pM->memTotal - pM->memFree; pM->tvsnapshot = tmnow; rc = 0; break; } } fileops_Close(fp); #else // (__linux__) LOG(X_ERROR("Memory snapshot not implemented on this architecure")); #endif // (__linux__) return rc; }
int devtype_loadcfg(const char *path) { STREAM_DEVICE_T *pdev = NULL; STREAM_DEVICE_T *pdevprev = NULL; FILE_HANDLE fp; char buf[1024]; const char *p; int linenum = 1; int count = 0; PARSE_ENTRY_DATA_T parseData; if(!path) { return -1; } if(g_devtypes) { devtype_free(g_devtypes); g_devtypes = NULL; } if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) { LOG(X_ERROR("Unable to open metafile for reading: %s"), path); return -1; } while(fileops_fgets(buf, sizeof(buf) - 1, fp)) { p = buf; while(*p == ' ' || *p == '\t') { p++; } if(*p == '#') { continue; } memset(&parseData, 0, sizeof(parseData)); if(strutil_parse_csv(cbparse_entry_devtype, &parseData, p) < 0) { LOG(X_ERROR("Failed to parse line %d in file %s"), linenum, path); devtype_free(g_devtypes); break; } else if((parseData.flags & PARSE_FLAG_HAVE_ALL)) { if(!(pdev = create_device(parseData.devname, parseData.strmatch, parseData.strmatch2, parseData.methods, parseData.devtype))) { LOG(X_ERROR("Failed to create device config from line %d"), linenum); devtype_free(g_devtypes); break; } else if(pdevprev) { pdevprev->pnext = pdev; } else { g_devtypes = pdev; } pdevprev = pdev; count++; } else if(strlen(p) > 1) { LOG(X_WARNING("Incomplete line %d in file %s"), linenum, path); } linenum++; } fileops_Close(fp); LOG(X_DEBUG("Read %d device profiles from %s"), count, path); //devtype_dump(g_devtypes); return count; }