summaryrefslogtreecommitdiffstats
path: root/src/detection/loadavg/loadavg_linux.c
diff options
context:
space:
mode:
authorsumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
committersumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
commit76424950e373d3b04ac3dd13019151bfba3e8423 (patch)
tree4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/detection/loadavg/loadavg_linux.c
Add the files
Diffstat (limited to 'src/detection/loadavg/loadavg_linux.c')
-rw-r--r--src/detection/loadavg/loadavg_linux.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/detection/loadavg/loadavg_linux.c b/src/detection/loadavg/loadavg_linux.c
new file mode 100644
index 0000000..a169312
--- /dev/null
+++ b/src/detection/loadavg/loadavg_linux.c
@@ -0,0 +1,33 @@
+#include "detection/loadavg/loadavg.h"
+#include "common/io.h"
+
+#include <sys/sysinfo.h>
+
+const char* ffDetectLoadavg(double result[3]) {
+#ifndef __ANDROID__ // cat: /proc/loadavg: Permission denied
+
+ // Don't use syscall for container compatibility. #620
+ char buf[64];
+ ssize_t nRead = ffReadFileData("/proc/loadavg", sizeof(buf) - 1, buf);
+ if (nRead > 0) {
+ buf[nRead] = '\0';
+
+ if (sscanf(buf, "%lf%lf%lf", &result[0], &result[1], &result[2]) == 3) {
+ return NULL;
+ }
+ }
+
+#endif
+#ifndef __GNU__
+ // getloadavg requires higher ANDROID_API version
+ struct sysinfo si;
+ if (sysinfo(&si) < 0) {
+ return "sysinfo() failed";
+ }
+
+ for (int i = 0; i < 3; i++) {
+ result[i] = (double) si.loads[i] / (1 << SI_LOAD_SHIFT);
+ }
+#endif
+ return NULL;
+}