diff --git a/src/detection/cpu/cpu.c b/src/detection/cpu/cpu.c index 62537aaa8..7410dbbee 100644 --- a/src/detection/cpu/cpu.c +++ b/src/detection/cpu/cpu.c @@ -1,13 +1,12 @@ #include "cpu.h" #include "detection/internal.h" -void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu); -static void detectCPU(const FFinstance* instance, FFCPUResult* cpu) -{ - ffStrbufInit(&cpu->name); - ffStrbufInit(&cpu->vendor); +const char* ffDetectCPUImpl(const FFinstance* instance, const FFCPUOptions* options, FFCPUResult* cpu); - ffDetectCPUImpl(instance, cpu); +const char* ffDetectCPU(const FFinstance* instance, const FFCPUOptions* options, FFCPUResult* cpu) +{ + const char* error = ffDetectCPUImpl(instance, options, cpu); + if (error) return error; const char* removeStrings[] = { " CPU", " FPU", " APU", " Processor", @@ -18,11 +17,5 @@ static void detectCPU(const FFinstance* instance, FFCPUResult* cpu) ffStrbufRemoveStringsA(&cpu->name, sizeof(removeStrings) / sizeof(removeStrings[0]), removeStrings); ffStrbufSubstrBeforeFirstC(&cpu->name, '@'); //Cut the speed output in the name as we append our own ffStrbufTrimRight(&cpu->name, ' '); //If we removed the @ in previous step there was most likely a space before it -} - -const FFCPUResult* ffDetectCPU(const FFinstance* instance) -{ - FF_DETECTION_INTERNAL_GUARD(FFCPUResult, - detectCPU(instance, &result); - ); + return NULL; } diff --git a/src/detection/cpu/cpu.h b/src/detection/cpu/cpu.h index afe05909e..6c5a3188c 100644 --- a/src/detection/cpu/cpu.h +++ b/src/detection/cpu/cpu.h @@ -22,6 +22,6 @@ typedef struct FFCPUResult double temperature; } FFCPUResult; -const FFCPUResult* ffDetectCPU(const FFinstance* instance); +const char* ffDetectCPU(const FFinstance* instance, const FFCPUOptions* options, FFCPUResult* cpu); #endif diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index fb53b60d2..51d485699 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -40,11 +40,10 @@ static double detectCpuTemp(const FFstrbuf* cpuName) return result; } -void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) +const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFinstance* instance, const FFCPUOptions* options, FFCPUResult* cpu) { - FF_UNUSED(instance); - - ffSysctlGetString("machdep.cpu.brand_string", &cpu->name); + if (ffSysctlGetString("machdep.cpu.brand_string", &cpu->name) != NULL) + return "sysctlbyname(machdep.cpu.brand_string) failed"; ffSysctlGetString("machdep.cpu.vendor", &cpu->vendor); cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.physicalcpu_max", 1); @@ -64,8 +63,10 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) if(cpu->frequencyMax == 0.0) cpu->frequencyMax = getFrequency("hw.cpufrequency"); - if (instance->config.cpu.temp) + if (options->temp) cpu->temperature = detectCpuTemp(&cpu->name); else cpu->temperature = FF_CPU_TEMP_UNSET; + + return NULL; } diff --git a/src/detection/cpu/cpu_bsd.c b/src/detection/cpu/cpu_bsd.c index c3c88e440..42a377b6e 100644 --- a/src/detection/cpu/cpu_bsd.c +++ b/src/detection/cpu/cpu_bsd.c @@ -1,11 +1,19 @@ #include "cpu.h" #include "common/sysctl.h" -void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) +const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFinstance* instance, const FFCPUOptions* options, FFCPUResult* cpu) { - FF_UNUSED(instance); + if (ffSysctlGetString("hw.model", &cpu->name)) + return "sysctlbyname(hw.model) failed"; - if (instance->config.cpu.temp) + cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1); + cpu->coresLogical = cpu->coresPhysical; + cpu->coresOnline = cpu->coresPhysical; + + cpu->frequencyMin = ffSysctlGetInt("hw.clockrate", 0) / 1000.0; + cpu->frequencyMax = cpu->frequencyMin; + + if (options->temp) { FF_STRBUF_AUTO_DESTROY cpuTemp = ffStrbufCreate(); if(ffSysctlGetString("temperature", &cpuTemp)) @@ -16,12 +24,5 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) else cpu->temperature = FF_CPU_TEMP_UNSET; - ffSysctlGetString("hw.model", &cpu->name); - - cpu->coresPhysical = (uint16_t) ffSysctlGetInt("hw.ncpu", 1); - cpu->coresLogical = cpu->coresPhysical; - cpu->coresOnline = cpu->coresPhysical; - - cpu->frequencyMin = ffSysctlGetInt("hw.clockrate", 0) / 1000.0; - cpu->frequencyMax = cpu->frequencyMin; + return NULL; } diff --git a/src/detection/cpu/cpu_linux.c b/src/detection/cpu/cpu_linux.c index bc14e2f18..f9abd1d32 100644 --- a/src/detection/cpu/cpu_linux.c +++ b/src/detection/cpu/cpu_linux.c @@ -7,11 +7,11 @@ #include #include -static void parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer, FFstrbuf* cpuMHz, FFstrbuf* cpuIsa, FFstrbuf* cpuUarch) +static const char* parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer, FFstrbuf* cpuMHz, FFstrbuf* cpuIsa, FFstrbuf* cpuUarch) { FILE* cpuinfo = fopen("/proc/cpuinfo", "r"); if(cpuinfo == NULL) - return; + return "fopen(\"/proc/cpuinfo\", \"r\") failed"; char* line = NULL; size_t len = 0; @@ -37,6 +37,8 @@ static void parseCpuInfo(FFCPUResult* cpu, FFstrbuf* physicalCoresBuffer, FFstrb free(line); fclose(cpuinfo); + + return NULL; } static double getGHz(const char* file) @@ -102,19 +104,17 @@ static void parseIsa(FFstrbuf* cpuIsa) } } -void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) +const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFinstance* instance, const FFCPUOptions* options, FFCPUResult* cpu) { - if(instance->config.cpu.temp) - cpu->temperature = detectCPUTemp(); - else - cpu->temperature = FF_CPU_TEMP_UNSET; + cpu->temperature = options->temp ? detectCPUTemp() : FF_CPU_TEMP_UNSET; FF_STRBUF_AUTO_DESTROY physicalCoresBuffer = ffStrbufCreate(); FF_STRBUF_AUTO_DESTROY cpuMHz = ffStrbufCreate(); FF_STRBUF_AUTO_DESTROY cpuIsa = ffStrbufCreate(); FF_STRBUF_AUTO_DESTROY cpuUarch = ffStrbufCreate(); - parseCpuInfo(cpu, &physicalCoresBuffer, &cpuMHz, &cpuIsa, &cpuUarch); + const char* error = parseCpuInfo(cpu, &physicalCoresBuffer, &cpuMHz, &cpuIsa, &cpuUarch); + if (error) return error; cpu->coresPhysical = ffStrbufToUInt16(&physicalCoresBuffer, 1); @@ -146,4 +146,6 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) ffStrbufAppendC(&cpu->name, ' '); ffStrbufAppend(&cpu->name, &cpuIsa); } + + return NULL; } diff --git a/src/detection/cpu/cpu_windows.c b/src/detection/cpu/cpu_windows.c index 13413bec3..4b814ca2f 100644 --- a/src/detection/cpu/cpu_windows.c +++ b/src/detection/cpu/cpu_windows.c @@ -3,23 +3,17 @@ #include "util/windows/registry.h" #include "util/mallocHelper.h" -void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) +const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFinstance* instance, const FFCPUOptions* options, FFCPUResult* cpu) { - FF_UNUSED(instance); - - cpu->temperature = FF_CPU_TEMP_UNSET; - cpu->coresPhysical = cpu->coresLogical = cpu->coresOnline = 0; - cpu->frequencyMax = cpu->frequencyMin = 0; - ffStrbufInit(&cpu->name); - ffStrbufInit(&cpu->vendor); - { DWORD length = 0; - GetLogicalProcessorInformationEx(RelationAll, NULL, &length); + if (GetLogicalProcessorInformationEx(RelationAll, NULL, &length) != ERROR_INSUFFICIENT_BUFFER) + return "GetLogicalProcessorInformationEx(RelationAll, NULL, &length) failed"; + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* FF_AUTO_FREE pProcessorInfo = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(length); - if(pProcessorInfo && GetLogicalProcessorInformationEx(RelationAll, pProcessorInfo, &length)) + if (pProcessorInfo && GetLogicalProcessorInformationEx(RelationAll, pProcessorInfo, &length)) { for( SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* ptr = pProcessorInfo; @@ -36,11 +30,13 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) } } } + else + return "GetLogicalProcessorInformationEx(RelationAll, pProcessorInfo, &length) failed"; } FF_HKEY_AUTO_DESTROY hKey; if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", &hKey, NULL)) - return; + return "ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L\"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\", &hKey, NULL) failed"; { uint32_t mhz; @@ -51,6 +47,8 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu) ffRegReadStrbuf(hKey, L"ProcessorNameString", &cpu->name, NULL); ffRegReadStrbuf(hKey, L"VendorIdentifier", &cpu->vendor, NULL); - if(instance->config.cpu.temp) + if(options->temp) ffDetectSmbiosTemp(&cpu->temperature, NULL); + + return NULL; } diff --git a/src/modules/cpu/cpu.c b/src/modules/cpu/cpu.c index efb467835..e71d6b8d3 100644 --- a/src/modules/cpu/cpu.c +++ b/src/modules/cpu/cpu.c @@ -7,9 +7,16 @@ void ffPrintCPU(FFinstance* instance, FFCPUOptions* options) { - const FFCPUResult* cpu = ffDetectCPU(instance); + FFCPUResult cpu; + cpu.temperature = FF_CPU_TEMP_UNSET; + cpu.coresPhysical = cpu.coresLogical = cpu.coresOnline = 0; + cpu.frequencyMax = cpu.frequencyMin = 0; + ffStrbufInit(&cpu.name); + ffStrbufInit(&cpu.vendor); - if(cpu->vendor.length == 0 && cpu->name.length == 0 && cpu->coresOnline <= 1) + ffDetectCPU(instance, options, &cpu); + + if(cpu.vendor.length == 0 && cpu.name.length == 0 && cpu.coresOnline <= 1) { ffPrintError(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "No CPU detected"); return; @@ -19,38 +26,38 @@ void ffPrintCPU(FFinstance* instance, FFCPUOptions* options) { ffPrintLogoAndKey(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs.key); - if(cpu->name.length > 0) - ffStrbufWriteTo(&cpu->name, stdout); - else if(cpu->vendor.length > 0) + if(cpu.name.length > 0) + ffStrbufWriteTo(&cpu.name, stdout); + else if(cpu.vendor.length > 0) { - ffStrbufWriteTo(&cpu->vendor, stdout); + ffStrbufWriteTo(&cpu.vendor, stdout); fputs(" CPU", stdout); } else fputs("CPU", stdout); - if(cpu->coresOnline > 1) - printf(" (%u)", cpu->coresOnline); + if(cpu.coresOnline > 1) + printf(" (%u)", cpu.coresOnline); - if(cpu->frequencyMax > 0.0) - printf(" @ %.9g GHz", cpu->frequencyMax); + if(cpu.frequencyMax > 0.0) + printf(" @ %.9g GHz", cpu.frequencyMax); - if(cpu->temperature == cpu->temperature) //FF_CPU_TEMP_UNSET - printf(" - %.1f°C", cpu->temperature); + if(cpu.temperature == cpu.temperature) //FF_CPU_TEMP_UNSET + printf(" - %.1f°C", cpu.temperature); putchar('\n'); } else { ffPrintFormat(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_CPU_NUM_FORMAT_ARGS, (FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_STRBUF, &cpu->name}, - {FF_FORMAT_ARG_TYPE_STRBUF, &cpu->vendor}, - {FF_FORMAT_ARG_TYPE_UINT16, &cpu->coresPhysical}, - {FF_FORMAT_ARG_TYPE_UINT16, &cpu->coresLogical}, - {FF_FORMAT_ARG_TYPE_UINT16, &cpu->coresOnline}, - {FF_FORMAT_ARG_TYPE_DOUBLE, &cpu->frequencyMin}, - {FF_FORMAT_ARG_TYPE_DOUBLE, &cpu->frequencyMax}, - {FF_FORMAT_ARG_TYPE_DOUBLE, &cpu->temperature} + {FF_FORMAT_ARG_TYPE_STRBUF, &cpu.name}, + {FF_FORMAT_ARG_TYPE_STRBUF, &cpu.vendor}, + {FF_FORMAT_ARG_TYPE_UINT16, &cpu.coresPhysical}, + {FF_FORMAT_ARG_TYPE_UINT16, &cpu.coresLogical}, + {FF_FORMAT_ARG_TYPE_UINT16, &cpu.coresOnline}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.frequencyMin}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.frequencyMax}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.temperature} }); } }