master ohmu/fastfetch-openbsd / src / detection / loadavg / loadavg_linux.c
ohmu  ·  2026-10-15
 1#include "detection/loadavg/loadavg.h"
 2#include "common/debug.h"
 3#include "common/io.h"
 4
 5#include <errno.h>
 6#include <string.h>
 7#include <sys/sysinfo.h>
 8
 9const char* ffDetectLoadavg(double result[3]) {
10#ifndef __ANDROID__ // cat: /proc/loadavg: Permission denied
11
12    // Don't use syscall for container compatibility. #620
13    char buf[64];
14    ssize_t nRead = ffReadFileData("/proc/loadavg", sizeof(buf) - 1, buf);
15    if (nRead > 0) {
16        buf[nRead] = '\0';
17
18        if (sscanf(buf, "%lf%lf%lf", &result[0], &result[1], &result[2]) == 3) {
19            return nullptr;
20        }
21    }
22
23#endif
24
25    // getloadavg requires higher ANDROID_API version
26    struct sysinfo si;
27    if (sysinfo(&si) < 0) {
28        FF_DEBUG("both /proc/loadavg and sysinfo() failed: %s", strerror(errno));
29        return "both /proc/loadavg and sysinfo() failed";
30    }
31
32    for (int i = 0; i < 3; i++) {
33        result[i] = (double) si.loads[i] / (1 << SI_LOAD_SHIFT);
34    }
35    return nullptr;
36}