From 8849107260f09e8268b19eb046ac8f269ff49836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 6 Oct 2022 22:07:48 +0800 Subject: [PATCH] Processes: add support for Windows --- CMakeLists.txt | 3 ++ src/detection/processes/processes.h | 10 ++++++ src/detection/processes/processes_apple.c | 18 ++++++++++ src/detection/processes/processes_linux.c | 12 +++++++ src/detection/processes/processes_windows.cpp | 30 +++++++++++++++++ src/modules/processes.c | 33 ++++++------------- 6 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 src/detection/processes/processes.h create mode 100644 src/detection/processes/processes_apple.c create mode 100644 src/detection/processes/processes_linux.c create mode 100644 src/detection/processes/processes_windows.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a2078097f..2bb2f1604 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -284,6 +284,7 @@ if(LINUX OR ANDROID OR BSD) src/detection/temps/temps_linux.c src/detection/users/users_linux.c src/detection/opengl/opengl_linux.c + src/detection/processes/processes_linux.c ) endif() @@ -319,6 +320,7 @@ if(WIN_MSYS) src/detection/opengl/opengl_windows.c src/detection/users/users_windows.cpp src/detection/os/os_windows.cpp + src/detection/processes/processes_windows.cpp src/util/windows/wmi.cpp # Shared @@ -359,6 +361,7 @@ if(APPLE) src/detection/font/font_apple.m src/detection/users/users_linux.c src/detection/opengl/opengl_apple.c + src/detection/processes/processes_apple.c ) endif() diff --git a/src/detection/processes/processes.h b/src/detection/processes/processes.h new file mode 100644 index 000000000..bfc4bce6c --- /dev/null +++ b/src/detection/processes/processes.h @@ -0,0 +1,10 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_processes_processes +#define FF_INCLUDED_detection_processes_processes + +#include "fastfetch.h" + +uint32_t ffDetectProcesses(FFinstance* instance, FFstrbuf* error); + +#endif diff --git a/src/detection/processes/processes_apple.c b/src/detection/processes/processes_apple.c new file mode 100644 index 000000000..32a251b1a --- /dev/null +++ b/src/detection/processes/processes_apple.c @@ -0,0 +1,18 @@ +#include "processes.h" + +#include + +uint32_t ffDetectProcesses(FFinstance* instance, FFstrbuf* error) +{ + FF_UNUSED(instance); + + int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; + size_t length; + + if(sysctl(request, sizeof(request) / sizeof(*request), NULL, &length, NULL, 0) != 0) + { + ffStrbufAppend(error, "sysctl() failed"); + return 0; + } + return (uint32_t)(length / sizeof(struct kinfo_proc)); +} diff --git a/src/detection/processes/processes_linux.c b/src/detection/processes/processes_linux.c new file mode 100644 index 000000000..da197b03e --- /dev/null +++ b/src/detection/processes/processes_linux.c @@ -0,0 +1,12 @@ +#include "processes.h" + +uint32_t ffDetectProcesses(FFinstance* instance, FFstrbuf* error) +{ + #if FF_HAVE_SYSINFO_H + FF_UNUSED(error); + return (uint32_t) instance->state.sysinfo.procs; + #else + ffStrbufAppendS(error, "Unimplemented"); + return 0; + #endif +} diff --git a/src/detection/processes/processes_windows.cpp b/src/detection/processes/processes_windows.cpp new file mode 100644 index 000000000..e6539f370 --- /dev/null +++ b/src/detection/processes/processes_windows.cpp @@ -0,0 +1,30 @@ +extern "C" { +#include "processes.h" +} +#include "util/windows/wmi.hpp" + +uint32_t ffDetectProcesses(FFinstance* instance, FFstrbuf* error) +{ + FF_UNUSED(instance); + + IEnumWbemClassObject* pEnumerator = ffQueryWmi(L"SELECT NumberOfProcesses FROM Win32_OperatingSystem", error); + + if(!pEnumerator) + return 0; + + IWbemClassObject *pclsObj = NULL; + ULONG uReturn = 0; + + if(FAILED(pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn)) || uReturn == 0) + { + ffStrbufAppendS(error, "No Wmi result returned"); + pEnumerator->Release(); + return 0; + } + + int64_t result = 0; + ffGetWmiObjInteger(pclsObj, L"NumberOfProcesses", &result); + pclsObj->Release(); + pEnumerator->Release(); + return (uint32_t)result; +} diff --git a/src/modules/processes.c b/src/modules/processes.c index d424487d8..fa71a7611 100644 --- a/src/modules/processes.c +++ b/src/modules/processes.c @@ -1,47 +1,34 @@ #include "fastfetch.h" #include "common/printing.h" +#include "detection/processes/processes.h" #define FF_PROCESSES_MODULE_NAME "Processes" #define FF_PROCESSES_NUM_FORMAT_ARGS 1 -#ifdef __APPLE__ - #include -#endif - void ffPrintProcesses(FFinstance* instance) { - #if __APPLE__ - int request[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; - size_t length; + FFstrbuf error; + ffStrbufInit(&error); + uint32_t numProcesses = ffDetectProcesses(instance, &error); - if(sysctl(request, sizeof(request) / sizeof(*request), NULL, &length, NULL, 0) != 0) - { - ffPrintError(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processes, "sysctl() failed"); - return; - } - uint16_t numProcesses = (uint16_t)(length / sizeof(struct kinfo_proc)); - #elif FF_HAVE_SYSINFO_H - uint16_t numProcesses = (uint16_t) instance->state.sysinfo.procs; - #else - uint16_t numProcesses = 0; - #endif - - if(numProcesses == 0) + if(error.length > 0) { - ffPrintError(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processes, "Could not get number of processes"); + ffPrintError(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processes, "%*s", error.length, error.chars); + ffStrbufDestroy(&error); return; } + ffStrbufDestroy(&error); if(instance->config.processes.outputFormat.length == 0) { ffPrintLogoAndKey(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processes.key); - printf("%hu\n", numProcesses); + printf("%u\n", numProcesses); } else { ffPrintFormat(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processes, FF_PROCESSES_NUM_FORMAT_ARGS, (FFformatarg[]){ - {FF_FORMAT_ARG_TYPE_UINT16, &numProcesses} + {FF_FORMAT_ARG_TYPE_UINT, &numProcesses} }); } }