From aea0986928e7ca200d1291745bdfb04c575e30ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 11 May 2023 19:55:31 +0800 Subject: [PATCH] CMake(Windows): remove USE_WIN_NTAPI option --- CMakeLists.txt | 10 +- src/detection/battery/battery_windows.c | 23 ++-- src/detection/cpuUsage/cpuUsage_windows.c | 25 ----- src/detection/processes/processes_windows.c | 27 +++++ src/detection/processes/processes_windows.cpp | 54 --------- .../swap/{swap_windows.cpp => swap_windows.c} | 30 ----- ...ll_windows.cpp => terminalshell_windows.c} | 105 +++--------------- 7 files changed, 55 insertions(+), 219 deletions(-) create mode 100644 src/detection/processes/processes_windows.c delete mode 100644 src/detection/processes/processes_windows.cpp rename src/detection/swap/{swap_windows.cpp => swap_windows.c} (63%) rename src/detection/terminalshell/{terminalshell_windows.cpp => terminalshell_windows.c} (78%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1784a2879..479c7cd1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,7 +73,6 @@ cmake_dependent_option(ENABLE_PULSE "Enable pulse" ON "LINUX OR BSD" OFF) cmake_dependent_option(ENABLE_LIBCJSON "Enable libcjson" ON "LINUX OR WIN32" OFF) cmake_dependent_option(ENABLE_THREADS "Enable multithreading" ON "Threads_FOUND" OFF) cmake_dependent_option(ENABLE_BUFFER "Enable stdout buffer" ON "LINUX OR APPLE OR BSD OR WIN32 OR ANDROID" OFF) -cmake_dependent_option(USE_WIN_NTAPI "Allow using internal NTAPI" ON "WIN32" OFF) option(BUILD_TESTS "Build tests" OFF) # Also create test executables option(SET_TWEAK "Add tweak to project version" ON) # This is set to off by github actions for release builds @@ -519,11 +518,11 @@ elseif(WIN32) src/detection/os/os_windows.cpp src/detection/packages/packages_windows.c src/detection/poweradapter/poweradapter_nosupport.c - src/detection/processes/processes_windows.cpp + src/detection/processes/processes_windows.c src/detection/sound/sound_windows.cpp - src/detection/swap/swap_windows.cpp + src/detection/swap/swap_windows.c src/detection/terminalfont/terminalfont_windows.c - src/detection/terminalshell/terminalshell_windows.cpp + src/detection/terminalshell/terminalshell_windows.c src/detection/temps/temps_windows.cpp src/detection/uptime/uptime_windows.c src/detection/users/users_windows.c @@ -734,9 +733,6 @@ elseif(WIN32) PRIVATE "wtsapi32" PRIVATE "powrprof" ) - if(USE_WIN_NTAPI) - target_compile_definitions(libfastfetch PRIVATE FF_USE_WIN_NTAPI) - endif() endif() target_include_directories(libfastfetch diff --git a/src/detection/battery/battery_windows.c b/src/detection/battery/battery_windows.c index af7afe892..fc89a335a 100644 --- a/src/detection/battery/battery_windows.c +++ b/src/detection/battery/battery_windows.c @@ -9,18 +9,15 @@ #include #include -#ifdef FF_USE_WIN_NTAPI - NTSYSCALLAPI - NTSTATUS - NTAPI - NtPowerInformation( - IN POWER_INFORMATION_LEVEL InformationLevel, - IN PVOID InputBuffer OPTIONAL, - IN ULONG InputBufferLength, - OUT PVOID OutputBuffer OPTIONAL, - IN ULONG OutputBufferLength); - #define CallNtPowerInformation NtPowerInformation -#endif +NTSYSCALLAPI +NTSTATUS +NTAPI +NtPowerInformation( + IN POWER_INFORMATION_LEVEL InformationLevel, + IN PVOID InputBuffer OPTIONAL, + IN ULONG InputBufferLength, + OUT PVOID OutputBuffer OPTIONAL, + IN ULONG OutputBufferLength); static inline void wrapCloseHandle(HANDLE* handle) { @@ -147,7 +144,7 @@ const char* ffDetectBatteryImpl(FFinstance* instance, FFlist* results) else { SYSTEM_BATTERY_STATE info; - if (NT_SUCCESS(CallNtPowerInformation(SystemBatteryState, NULL, 0, &info, sizeof(info))) && info.BatteryPresent) + if (NT_SUCCESS(NtPowerInformation(SystemBatteryState, NULL, 0, &info, sizeof(info))) && info.BatteryPresent) { BatteryResult* battery = (BatteryResult*)ffListAdd(results); ffStrbufInit(&battery->modelName); diff --git a/src/detection/cpuUsage/cpuUsage_windows.c b/src/detection/cpuUsage/cpuUsage_windows.c index d4ffbd2ca..60ebef4be 100644 --- a/src/detection/cpuUsage/cpuUsage_windows.c +++ b/src/detection/cpuUsage/cpuUsage_windows.c @@ -1,8 +1,6 @@ #include "fastfetch.h" #include "cpuUsage.h" -#ifdef FF_USE_WIN_NTAPI - #include "util/mallocHelper.h" #include @@ -37,26 +35,3 @@ const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) return NULL; } - -#else - -#include - -static inline uint64_t fileTimeToUint64(const FILETIME* ft) { - return (((uint64_t)ft->dwHighDateTime) << 32) | ((uint64_t)ft->dwLowDateTime); -} - -const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) -{ - FILETIME idleTime, kernelTime, userTime; - if(!GetSystemTimes(&idleTime, &kernelTime, &userTime)) - return "GetSystemTimes() failed"; - - // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getsystemtimes - // `kernelTime` also includes the amount of time the system has been idle. - *totalAll = fileTimeToUint64(&userTime) + fileTimeToUint64(&kernelTime); - *inUseAll = *totalAll - fileTimeToUint64(&idleTime); - return NULL; -} - -#endif diff --git a/src/detection/processes/processes_windows.c b/src/detection/processes/processes_windows.c new file mode 100644 index 000000000..dc784c5db --- /dev/null +++ b/src/detection/processes/processes_windows.c @@ -0,0 +1,27 @@ +#include "processes.h" +#include "util/mallocHelper.h" + +#include +#include + +const char* ffDetectProcesses(uint32_t* result) +{ + ULONG size = 0; + if(NtQuerySystemInformation(SystemProcessInformation, NULL, 0, &size) != STATUS_INFO_LENGTH_MISMATCH) + return "NtQuerySystemInformation(SystemProcessInformation, NULL) failed"; + + size += sizeof(SystemProcessInformation) * 5; //What if new processes are created during two syscalls? + + SYSTEM_PROCESS_INFORMATION* FF_AUTO_FREE pstart = (SYSTEM_PROCESS_INFORMATION*)malloc(size); + if(!pstart) + return "malloc(size) failed"; + + if(!NT_SUCCESS(NtQuerySystemInformation(SystemProcessInformation, pstart, size, NULL))) + return "NtQuerySystemInformation(SystemProcessInformation, pstart) failed"; + + *result = 1; //Init with 1 because we test for ptr->NextEntryOffset + for (SYSTEM_PROCESS_INFORMATION* ptr = pstart; ptr->NextEntryOffset; ptr = (SYSTEM_PROCESS_INFORMATION*)((uint8_t*)ptr + ptr->NextEntryOffset)) + ++*result; + + return NULL; +} diff --git a/src/detection/processes/processes_windows.cpp b/src/detection/processes/processes_windows.cpp deleted file mode 100644 index bfccff079..000000000 --- a/src/detection/processes/processes_windows.cpp +++ /dev/null @@ -1,54 +0,0 @@ -extern "C" { -#include "processes.h" -#include "util/mallocHelper.h" -} - -#ifdef FF_USE_WIN_NTAPI - -#include -#include - -const char* ffDetectProcesses(uint32_t* result) -{ - ULONG size = 0; - if(NtQuerySystemInformation(SystemProcessInformation, nullptr, 0, &size) != STATUS_INFO_LENGTH_MISMATCH) - return "NtQuerySystemInformation(SystemProcessInformation, NULL) failed"; - - size += sizeof(SystemProcessInformation) * 5; //What if new processes are created during two syscalls? - - SYSTEM_PROCESS_INFORMATION* FF_AUTO_FREE pstart = (SYSTEM_PROCESS_INFORMATION*)malloc(size); - if(!pstart) - return "malloc(size) failed"; - - if(!NT_SUCCESS(NtQuerySystemInformation(SystemProcessInformation, pstart, size, nullptr))) - return "NtQuerySystemInformation(SystemProcessInformation, pstart) failed"; - - *result = 1; //Init with 1 because we test for ptr->NextEntryOffset - for (auto ptr = pstart; ptr->NextEntryOffset; ptr = (SYSTEM_PROCESS_INFORMATION*)((uint8_t*)ptr + ptr->NextEntryOffset)) - ++*result; - - return NULL; -} - -#else - -#include "util/windows/wmi.hpp" - -const char* ffDetectProcesses(uint32_t* result) -{ - FFWmiQuery query(L"SELECT NumberOfProcesses FROM Win32_OperatingSystem", NULL); - if(!query) - return "Query WMI service failed"; - - if(FFWmiRecord record = query.next()) - { - uint64_t value = 0; - record.getUnsigned(L"NumberOfProcesses", &value); - *result = (uint32_t)value; - return NULL; - } - - return "No Wmi result returned"; -} - -#endif diff --git a/src/detection/swap/swap_windows.cpp b/src/detection/swap/swap_windows.c similarity index 63% rename from src/detection/swap/swap_windows.cpp rename to src/detection/swap/swap_windows.c index 15d6336f4..f367b0d59 100644 --- a/src/detection/swap/swap_windows.cpp +++ b/src/detection/swap/swap_windows.c @@ -1,15 +1,10 @@ -extern "C" { #include "swap.h" #include "util/mallocHelper.h" -} - -#ifdef FF_USE_WIN_NTAPI #include #include #include -extern "C" void ffDetectSwap(FFMemoryStorage* swap) { SYSTEM_INFO sysInfo; @@ -38,28 +33,3 @@ void ffDetectSwap(FFMemoryStorage* swap) swap->bytesUsed = (uint64_t)pstart->TotalUsed * sysInfo.dwPageSize; swap->bytesTotal = (uint64_t)pstart->CurrentSize * sysInfo.dwPageSize; } - -#else - -#include "util/windows/wmi.hpp" - -extern "C" -void ffDetectSwapImpl(FFMemoryStorage* swap) -{ - FFWmiQuery query(L"SELECT AllocatedBaseSize, CurrentUsage FROM Win32_PageFileUsage", &swap->error); - if(!query) - return; - - if(FFWmiRecord record = query.next()) - { - //MB - record.getUnsigned(L"AllocatedBaseSize", &swap->bytesTotal); - record.getUnsigned(L"CurrentUsage", &swap->bytesUsed); - swap->bytesTotal *= 1024 * 1024; - swap->bytesUsed *= 1024 * 1024; - } - else - ffStrbufInitS(&swap->error, "No Wmi result returned"); -} - -#endif diff --git a/src/detection/terminalshell/terminalshell_windows.cpp b/src/detection/terminalshell/terminalshell_windows.c similarity index 78% rename from src/detection/terminalshell/terminalshell_windows.cpp rename to src/detection/terminalshell/terminalshell_windows.c index 83bd37c16..84c3f6dd2 100644 --- a/src/detection/terminalshell/terminalshell_windows.cpp +++ b/src/detection/terminalshell/terminalshell_windows.c @@ -1,16 +1,11 @@ -extern "C" { #include "terminalshell.h" #include "common/processing.h" #include "common/thread.h" #include "util/mallocHelper.h" -} #include #include #include - -#ifdef FF_USE_WIN_NTAPI - #include #include @@ -59,7 +54,7 @@ static bool getProcessInfo(uint32_t pid, uint32_t* ppid, FFstrbuf* pname, FFstrb static bool getTerminalInfoByEnumeratingChildProcesses(FFTerminalShellResult* result, uint32_t ppid) { ULONG size = 0; - if(NtQuerySystemInformation(SystemProcessInformation, nullptr, 0, &size) != STATUS_INFO_LENGTH_MISMATCH) + if(NtQuerySystemInformation(SystemProcessInformation, NULL, 0, &size) != STATUS_INFO_LENGTH_MISMATCH) return false; size += sizeof(SystemProcessInformation) * 5; //What if new processes are created during two syscalls? @@ -68,12 +63,12 @@ static bool getTerminalInfoByEnumeratingChildProcesses(FFTerminalShellResult* re if(!pstart) return false; - if(!NT_SUCCESS(NtQuerySystemInformation(SystemProcessInformation, pstart, size, nullptr))) + if(!NT_SUCCESS(NtQuerySystemInformation(SystemProcessInformation, pstart, size, NULL))) return false; uint32_t currentProcessId = (uint32_t) GetCurrentProcessId(); - for (auto ptr = pstart; ptr->NextEntryOffset; ptr = (SYSTEM_PROCESS_INFORMATION*)((uint8_t*)ptr + ptr->NextEntryOffset)) + for (SYSTEM_PROCESS_INFORMATION* ptr = pstart; ptr->NextEntryOffset; ptr = (SYSTEM_PROCESS_INFORMATION*)((uint8_t*)ptr + ptr->NextEntryOffset)) { if ((uint32_t)(uintptr_t) ptr->InheritedFromUniqueProcessId != ppid) continue; @@ -82,7 +77,7 @@ static bool getTerminalInfoByEnumeratingChildProcesses(FFTerminalShellResult* re if (pid == currentProcessId) continue; - if(!getProcessInfo(pid, nullptr, &result->terminalProcessName, &result->terminalExe, &result->terminalExeName)) + if(!getProcessInfo(pid, NULL, &result->terminalProcessName, &result->terminalExe, &result->terminalExeName)) return false; result->terminalPid = pid; @@ -95,77 +90,7 @@ static bool getTerminalInfoByEnumeratingChildProcesses(FFTerminalShellResult* re return false; } -#else - -#include "util/windows/wmi.hpp" -#include - -static bool getProcessInfo(uint32_t pid, uint32_t* ppid, FFstrbuf* pname, FFstrbuf* exe, const char** exeName) -{ - if(pid == 0) - pid = GetCurrentProcessId(); - - wchar_t sql[256] = {}; - swprintf(sql, 256, L"SELECT %ls %ls ParentProcessId FROM Win32_Process WHERE ProcessId = %" PRIu32, - pname ? L"Name," : L"", - pname ? L"ExecutablePath," : L"", - pid); - - FFWmiQuery query(sql); - if(!query) - return false; - - if(FFWmiRecord record = query.next()) - { - if(ppid) - { - uint64_t value; - record.getUnsigned(L"ParentProcessId", &value); - *ppid = (uint32_t) value; - } - - if(pname) - record.getString(L"Name", pname); - - if(exe) - record.getString(L"ExecutablePath", exe); - - if(exeName) - *exeName = exe->chars + ffStrbufLastIndexC(exe, '\\') + 1; - } - return true; -} - -static bool getTerminalInfoByEnumeratingChildProcesses(FFTerminalShellResult* result, uint32_t ppid) -{ - wchar_t sql[256] = {}; - swprintf(sql, 256, L"SELECT Name, ExecutablePath, ProcessId FROM Win32_Process WHERE ProcessId <> %" PRIu32 " AND ParentProcessId = %" PRIu32, - (uint32_t) GetCurrentProcessId(), - ppid); - - FFWmiQuery query(sql); - if(!query) - return false; - - if(FFWmiRecord record = query.next()) - { - record.getString(L"Name", &result->terminalProcessName); - record.getString(L"ExecutablePath", &result->terminalExe); - result->terminalExeName = result->terminalExe.chars + ffStrbufLastIndexC(&result->terminalExe, '\\') + 1; - uint64_t pid; - record.getUnsigned(L"ProcessId", &pid); - result->terminalPid = (uint32_t) pid; - ffStrbufSet(&result->terminalPrettyName, &result->terminalProcessName); - if(ffStrbufEndsWithIgnCaseS(&result->terminalPrettyName, ".exe")) - ffStrbufSubstrBefore(&result->terminalPrettyName, result->terminalPrettyName.length - 4); - return true; - } - return false; -} - -#endif - -extern "C" bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version); +bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version); static uint32_t getShellInfo(const FFinstance* instance, FFTerminalShellResult* result, uint32_t pid) { @@ -195,7 +120,7 @@ static uint32_t getShellInfo(const FFinstance* instance, FFTerminalShellResult* ffStrbufClear(&result->shellProcessName); ffStrbufClear(&result->shellPrettyName); ffStrbufClear(&result->shellExe); - result->shellExeName = nullptr; + result->shellExeName = NULL; return getShellInfo(instance, result, ppid); } @@ -316,23 +241,23 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) ffStrbufIgnCaseCompS(&result->terminalProcessName, "explorer") != 0 ) return; - const char* term = nullptr; + const char* term = NULL; //SSH - if(getenv("SSH_CONNECTION") != nullptr) + if(getenv("SSH_CONNECTION") != NULL) term = getenv("SSH_TTY"); //Windows Terminal if(!term && ( - getenv("WT_SESSION") != nullptr || - getenv("WT_PROFILE_ID") != nullptr + getenv("WT_SESSION") != NULL || + getenv("WT_PROFILE_ID") != NULL )) term = "Windows Terminal"; //Alacritty if(!term && ( - getenv("ALACRITTY_SOCKET") != nullptr || - getenv("ALACRITTY_LOG") != nullptr || - getenv("ALACRITTY_WINDOW_ID") != nullptr + getenv("ALACRITTY_SOCKET") != NULL || + getenv("ALACRITTY_LOG") != NULL || + getenv("ALACRITTY_WINDOW_ID") != NULL )) term = "Alacritty"; if(!term) @@ -351,7 +276,7 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) } } -extern "C" bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); +bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) { @@ -386,7 +311,7 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) ffStrbufInit(&result.userShellVersion); uint32_t ppid; - if(!getProcessInfo(0, &ppid, nullptr, nullptr, nullptr)) + if(!getProcessInfo(0, &ppid, NULL, NULL, NULL)) goto exit; ppid = getShellInfo(instance, &result, ppid);