diff options
Diffstat (limited to 'src/detection/processes')
| -rw-r--r-- | src/detection/processes/processes.h | 5 | ||||
| -rw-r--r-- | src/detection/processes/processes_bsd.c | 23 | ||||
| -rw-r--r-- | src/detection/processes/processes_haiku.c | 14 | ||||
| -rw-r--r-- | src/detection/processes/processes_linux.c | 27 | ||||
| -rw-r--r-- | src/detection/processes/processes_nbsd.c | 15 | ||||
| -rw-r--r-- | src/detection/processes/processes_nosupport.c | 5 | ||||
| -rw-r--r-- | src/detection/processes/processes_obsd.c | 15 | ||||
| -rw-r--r-- | src/detection/processes/processes_windows.c | 34 |
8 files changed, 138 insertions, 0 deletions
diff --git a/src/detection/processes/processes.h b/src/detection/processes/processes.h new file mode 100644 index 0000000..69cf463 --- /dev/null +++ b/src/detection/processes/processes.h @@ -0,0 +1,5 @@ +#pragma once + +#include "fastfetch.h" + +const char* ffDetectProcesses(uint32_t* result); diff --git a/src/detection/processes/processes_bsd.c b/src/detection/processes/processes_bsd.c new file mode 100644 index 0000000..e3f02b8 --- /dev/null +++ b/src/detection/processes/processes_bsd.c @@ -0,0 +1,23 @@ +#include "processes.h" + +#include <sys/sysctl.h> +#ifdef __FreeBSD__ + #include <sys/types.h> + #include <sys/user.h> +#endif + +#ifndef KERN_PROC_PROC + #define KERN_PROC_PROC KERN_PROC_ALL // Apple +#endif + +const char* ffDetectProcesses(uint32_t* result) { + int request[] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC }; + size_t length; + + if (sysctl(request, ARRAY_SIZE(request), NULL, &length, NULL, 0) != 0) { + return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_PROC}) failed"; + } + + *result = (uint32_t) (length / sizeof(struct kinfo_proc)); + return NULL; +} diff --git a/src/detection/processes/processes_haiku.c b/src/detection/processes/processes_haiku.c new file mode 100644 index 0000000..c50e62d --- /dev/null +++ b/src/detection/processes/processes_haiku.c @@ -0,0 +1,14 @@ +#include "processes.h" + +#include <OS.h> + +const char* ffDetectProcesses(uint32_t* result) { + system_info info; + if (get_system_info(&info) != B_OK) { + return "Error getting system info"; + } + + *result = info.used_teams; + + return NULL; +} diff --git a/src/detection/processes/processes_linux.c b/src/detection/processes/processes_linux.c new file mode 100644 index 0000000..dee8c09 --- /dev/null +++ b/src/detection/processes/processes_linux.c @@ -0,0 +1,27 @@ +#include "processes.h" + +#include "common/io.h" +#include "common/strutil.h" + +const char* ffDetectProcesses(uint32_t* result) { + FF_AUTO_CLOSE_DIR DIR* dir = opendir("/proc"); + if (dir == NULL) { + return "opendir(\"/proc\") failed"; + } + + uint32_t num = 0; + + struct dirent* entry; + while ((entry = readdir(dir)) != NULL) { + if ( +#ifdef _DIRENT_HAVE_D_TYPE + (entry->d_type == DT_DIR || entry->d_type == DT_UNKNOWN) && +#endif + ffCharIsDigit(entry->d_name[0])) + ++num; + } + + *result = num; + + return NULL; +} diff --git a/src/detection/processes/processes_nbsd.c b/src/detection/processes/processes_nbsd.c new file mode 100644 index 0000000..efd3d6f --- /dev/null +++ b/src/detection/processes/processes_nbsd.c @@ -0,0 +1,15 @@ +#include "processes.h" + +#include <sys/sysctl.h> + +const char* ffDetectProcesses(uint32_t* result) { + int request[] = { CTL_KERN, KERN_PROC2, KERN_PROC_ALL, -1, sizeof(struct kinfo_proc2), 0 }; + size_t length = 0; + + if (sysctl(request, ARRAY_SIZE(request), NULL, &length, NULL, 0) != 0) { + return "sysctl({CTL_KERN, KERN_PROC2, KERN_PROC_ALL}) failed"; + } + + *result = (uint32_t) (length / sizeof(struct kinfo_proc2)); + return NULL; +} diff --git a/src/detection/processes/processes_nosupport.c b/src/detection/processes/processes_nosupport.c new file mode 100644 index 0000000..7da208e --- /dev/null +++ b/src/detection/processes/processes_nosupport.c @@ -0,0 +1,5 @@ +#include "processes.h" + +const char* ffDetectProcesses(uint32_t* result) { + return "Not supported on this platform"; +} diff --git a/src/detection/processes/processes_obsd.c b/src/detection/processes/processes_obsd.c new file mode 100644 index 0000000..6246d8f --- /dev/null +++ b/src/detection/processes/processes_obsd.c @@ -0,0 +1,15 @@ +#include "processes.h" + +#include <sys/param.h> +#include <sys/sysctl.h> +#include <kvm.h> + +const char* ffDetectProcesses(uint32_t* result) { + kvm_t* kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); + const void* ret = kvm_getprocs(kd, KERN_PROC_ALL, 0, 1, result); + kvm_close(kd); + if (!ret) { + return "kvm_getprocs() failed"; + } + return NULL; +} diff --git a/src/detection/processes/processes_windows.c b/src/detection/processes/processes_windows.c new file mode 100644 index 0000000..03b34db --- /dev/null +++ b/src/detection/processes/processes_windows.c @@ -0,0 +1,34 @@ +#include "processes.h" +#include "common/mallocHelper.h" + +#include <ntstatus.h> +#include <winternl.h> + +const char* ffDetectProcesses(uint32_t* result) { + SYSTEM_PROCESS_INFORMATION* FF_AUTO_FREE pstart = NULL; + + // Multiple attempts in case processes change while + // we are in the middle of querying them. + ULONG size = 0; + for (int attempts = 0;; ++attempts) { + if (size) { + pstart = (SYSTEM_PROCESS_INFORMATION*) realloc(pstart, size); + assert(pstart); + } + NTSTATUS status = NtQuerySystemInformation(SystemProcessInformation, pstart, size, &size); + if (NT_SUCCESS(status)) { + break; + } else if (status == STATUS_INFO_LENGTH_MISMATCH && attempts < 4) { + size += sizeof(SYSTEM_PROCESS_INFORMATION) * 5; + } else { + return "NtQuerySystemInformation(SystemProcessInformation) 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; +} |