How Fastfetch Detects Processes on Unix-Like Systems: A Deep Dive into the Source Code
Fastfetch detects processes on Unix-like systems through a platform abstraction layer in src/detection/processes/ that uses /proc on Linux, sysctl on BSD systems, and kvm on OpenBSD to count running processes.
The fastfetch-cli/fastfetch repository implements process detection via a modular architecture that isolates OS-specific kernel interfaces behind a unified C API. This design allows the tool to report accurate process counts across diverse Unix variants while maintaining clean separation between detection logic and presentation layers.
Platform-Specific Detection Strategies
Fastfetch selects implementation files at compile time using preprocessor guards, ensuring each platform uses its native kernel interface without runtime overhead.
Linux: Reading the /proc Pseudo-Filesystem
On Linux, fastfetch counts processes by enumerating entries in the /proc directory. In src/detection/processes/processes_linux.c, the implementation opens /proc using opendir(), iterates through directory entries with readdir(), and counts subdirectories whose names start with a digit—each representing a process ID (PID).
This approach requires no special privileges for basic counting and avoids dependencies on external libraries. The function returns the total count through the uint32_t *result parameter defined in the public API.
BSD Systems: Using sysctl with KERN_PROC
For FreeBSD and macOS, fastfetch uses the sysctl system call to query the kernel process table. The implementation in src/detection/processes/processes_bsd.c constructs a Management Information Base (MIB) array containing CTL_KERN, KERN_PROC, and KERN_PROC_ALL.
The code calls sysctl() twice: first to determine the required buffer size, then to fetch the array of kinfo_proc structures. Dividing the returned buffer length by sizeof(struct kinfo_proc) yields the exact process count without parsing text files.
NetBSD: KERN_PROC2 and kinfo_proc2
NetBSD requires a slightly different approach implemented in src/detection/processes/processes_nbsd.c. Instead of KERN_PROC, fastfetch uses the KERN_PROC2 MIB entry and the kinfo_proc2 structure, which provides additional fields and improved binary stability compared to the legacy interface.
The calculation method remains similar to other BSD systems: query the buffer size, allocate memory, fetch the process array, and compute the count from the structure size.
OpenBSD: The kvm Library
OpenBSD's implementation in src/detection/processes/processes_obsd.c utilizes the kernel virtual memory (kvm) library for process enumeration. The code calls kvm_open() to establish a handle, then kvm_getprocs() with the KERN_PROC_ALL flag to retrieve a snapshot of all processes.
Unlike the BSD sysctl method, kvm_getprocs() returns both the process array and the count directly, simplifying the calculation logic while requiring linkage against -lkvm.
Haiku and Unsupported Platforms
Haiku OS uses a dedicated get_process_info API implemented in src/detection/processes/processes_haiku.c, though the specific implementation details are abstracted within the Haiku API. For unsupported platforms, src/detection/processes/processes_nosupport.c provides a fallback that returns a "not supported" error string.
Architectural Flow: From Module to Kernel
The process detection follows a strict three-layer architecture that separates presentation from platform-specific system calls.
-
Module Entry:
ffPrintProcesses()insrc/modules/processes/processes.cinvokesffDetectProcesses(&numProcesses), passing a pointer to auint32_tvariable. -
Platform Dispatch: The build system includes the appropriate platform-specific file (e.g.,
processes_linux.corprocesses_bsd.c), each implementing the sameffDetectProcessesfunction signature declared insrc/detection/processes/processes.h. -
Kernel Interface: The selected implementation executes its OS-specific counting logic—whether scanning
/proc, callingsysctl, or usingkvm—and writes the result to the provided pointer. -
Output Generation: Upon successful detection, the module formats the count for display. If the user requested JSON output via
--json,ffGenerateProcessesJsonResult()structures the data under the"result"field; otherwise, the raw number prints with the configured formatting.
Using the Processes Module
You can verify the detection mechanism directly through fastfetch's CLI or programmatically via its C API.
Display the current process count in default format:
fastfetch --module processes
Retrieve structured data for scripting purposes:
fastfetch --module processes --json
The JSON output follows this structure:
{
"processes": {
"result": 1234
}
}
To integrate fastfetch's detection into your own C application, include the detection header and call the abstraction function:
#include "detection/processes/processes.h"
#include <stdio.h>
int main(void) {
uint32_t count;
const char *err = ffDetectProcesses(&count);
if (err) {
fprintf(stderr, "Error detecting processes: %s\n", err);
return 1;
}
printf("Running processes: %u\n", count);
return 0;
}
Summary
- Fastfetch uses
src/detection/processes/processes.hto declare the cross-platform APIffDetectProcesses(uint32_t *result). - Linux implementations scan
/procdirectories, counting numeric PID entries. - BSD variants (FreeBSD, macOS) query the kernel via
sysctlwithKERN_PROCMIBs and calculate counts fromkinfo_procstructure sizes. - NetBSD uses the modern
KERN_PROC2interface withkinfo_proc2structures. - OpenBSD relies on the
kvmlibrary (kvm_open,kvm_getprocs) for kernel process table snapshots. - The module layer in
src/modules/processes/processes.chandles formatting and JSON generation, keeping detection logic pure and reusable.
Frequently Asked Questions
What function does fastfetch use to detect processes?
Fastfetch declares const char* ffDetectProcesses(uint32_t *result); in src/detection/processes/processes.h. This function abstracts all platform-specific implementations, returning an error string if detection fails or writing the process count to the provided uint32_t pointer on success.
How does fastfetch count processes on Linux?
On Linux, fastfetch opens the /proc pseudo-filesystem in src/detection/processes/processes_linux.c and counts directory entries whose names consist entirely of digits. Each numeric entry corresponds to a running process ID, providing an accurate count without requiring system calls or external libraries.
Why does fastfetch use different methods for BSD and Linux?
Linux exposes process information through the standardized /proc virtual filesystem, while BSD systems traditionally use sysctl kernel interfaces. OpenBSD specifically deprecates certain sysctl process queries in favor of the kvm library for security and consistency. Fastfetch adapts to each platform's canonical interface to ensure accurate, privileged-free operation across Unix variants.
Can I use fastfetch's process detection in my own C program?
Yes. Include src/detection/processes/processes.h and link against the appropriate platform-specific implementation file (e.g., processes_linux.c for Linux systems). The ffDetectProcesses function provides a clean, dependency-minimal API for retrieving system process counts without parsing command output yourself.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →