From df4e91476ffa3c03e62f1e7db8cb6d2a0f89691c Mon Sep 17 00:00:00 2001 From: Roberto Scolaro Date: Mon, 30 Oct 2023 17:25:44 +0100 Subject: [PATCH] chore(userspace/falco/app/actions): refactor sysinfo function Signed-off-by: Roberto Scolaro --- userspace/falco/app/actions/print_support.cpp | 107 +++++++++++------- 1 file changed, 63 insertions(+), 44 deletions(-) diff --git a/userspace/falco/app/actions/print_support.cpp b/userspace/falco/app/actions/print_support.cpp index 5a656078..698ba4e7 100644 --- a/userspace/falco/app/actions/print_support.cpp +++ b/userspace/falco/app/actions/print_support.cpp @@ -18,6 +18,7 @@ limitations under the License. #ifndef _WIN32 #include #endif +#include #include "actions.h" #include "../../versions_info.h" @@ -34,6 +35,64 @@ static std::string read_file(const std::string &filename) return str; } +#ifndef _WIN32 +static int get_unix_sysinfo(nlohmann::json &support) +{ + struct utsname sysinfo; + if(uname(&sysinfo) != 0) + { + return -1; + } + + support["system_info"]["sysname"] = sysinfo.sysname; + support["system_info"]["nodename"] = sysinfo.nodename; + support["system_info"]["release"] = sysinfo.release; + support["system_info"]["version"] = sysinfo.version; + support["system_info"]["machine"] = sysinfo.machine; + return 0; +} +#endif + +#ifdef _WIN32 +static int get_win32_sysinfo(nlohmann::json &support) +{ + OSVERSIONINFO osvi; + SYSTEM_INFO sysInfo; + TCHAR computerName[256]; + DWORD size = sizeof(computerName); + + osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + GetSystemInfo(&sysInfo); + if(!GetVersionEx(&osvi) || !GetComputerName(computerName, &size)) + { + return -1; + } + + support["system_info"]["sysname"] = "Windows"; + support["system_info"]["nodename"] = computerName; + support["system_info"]["release"] = osvi.dwMajorVersion; + support["system_info"]["version"] = osvi.dwMinorVersion; + + switch (sysInfo.wProcessorArchitecture) { + case PROCESSOR_ARCHITECTURE_AMD64: + support["system_info"]["machine"] = "x86_64"; + break; + case PROCESSOR_ARCHITECTURE_ARM: + support["system_info"]["machine"] = "ARM"; + break; + case PROCESSOR_ARCHITECTURE_ARM64: + support["system_info"]["machine"] = "ARM64"; + break; + case PROCESSOR_ARCHITECTURE_INTEL: + support["system_info"]["machine"] = "i386"; + break; + default: + support["system_info"]["machine"] = "unknown"; + } + return 0; +} +#endif + falco::app::run_result falco::app::actions::print_support(falco::app::state& s) { if(s.options.print_support) @@ -42,58 +101,18 @@ falco::app::run_result falco::app::actions::print_support(falco::app::state& s) std::string cmdline; #ifndef _WIN32 - struct utsname sysinfo; - if(uname(&sysinfo) != 0) - { - return run_result::fatal(std::string("Could not uname() to find system info: ") + strerror(errno)); - } + int rc = get_unix_sysinfo(support); #else - OSVERSIONINFO osvi; - SYSTEM_INFO sysInfo; - TCHAR computerName[256]; - DWORD size = sizeof(computerName); - - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetSystemInfo(&sysInfo); - if(!GetVersionEx(&osvi) || !GetComputerName(computerName, &size)) + int rc = get_win32_sysinfo(support); +#endif + if(rc != 0) { return run_result::fatal(std::string("Could not get system info: ") + strerror(errno)); } -#endif const falco::versions_info infos(s.offline_inspector); support["version"] = infos.falco_version; support["engine_info"] = infos.as_json(); - -#ifndef _WIN32 - support["system_info"]["sysname"] = sysinfo.sysname; - support["system_info"]["nodename"] = sysinfo.nodename; - support["system_info"]["release"] = sysinfo.release; - support["system_info"]["version"] = sysinfo.version; - support["system_info"]["machine"] = sysinfo.machine; -#else - support["system_info"]["sysname"] = "Windows"; - support["system_info"]["nodename"] = computerName; - support["system_info"]["release"] = osvi.dwMajorVersion; - support["system_info"]["version"] = osvi.dwMinorVersion; - - switch (sysInfo.wProcessorArchitecture) { - case PROCESSOR_ARCHITECTURE_AMD64: - support["system_info"]["machine"] = "x86_64"; - break; - case PROCESSOR_ARCHITECTURE_ARM: - support["system_info"]["machine"] = "ARM"; - break; - case PROCESSOR_ARCHITECTURE_ARM64: - support["system_info"]["machine"] = "ARM64"; - break; - case PROCESSOR_ARCHITECTURE_INTEL: - support["system_info"]["machine"] = "i386"; - break; - default: - support["system_info"]["machine"] = "unknown"; - } -#endif support["cmdline"] = s.cmdline; support["config"] = read_file(s.options.conf_filename); support["rules_files"] = nlohmann::json::array();