Skip to content

Commit 6a6b407

Browse files
committed
Swap (Linux): add fallback for Android
1 parent 2c80cd6 commit 6a6b407

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/detection/swap/swap_linux.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,34 @@
55

66
#include <inttypes.h>
77

8-
const char* ffDetectSwap(FFlist* result)
8+
static const char* detectByProcMeminfo(FFlist* result)
9+
{
10+
// For Android
11+
// Ref: #620
12+
char buf[PROC_FILE_BUFFSIZ];
13+
ssize_t nRead = ffReadFileData("/proc/meminfo", ARRAY_SIZE(buf) - 1, buf);
14+
if(nRead < 0)
15+
return "ffReadFileData(\"/proc/meminfo\", ARRAY_SIZE(buf)-1, buf)";
16+
buf[nRead] = '\0';
17+
18+
uint64_t swapTotal = 0, swapFree = 0;
19+
20+
char *token = NULL;
21+
if ((token = strstr(buf, "SwapTotal:")) != NULL)
22+
swapTotal = strtoul(token + strlen("SwapTotal:"), NULL, 10);
23+
24+
if ((token = strstr(buf, "SwapFree:")) != NULL)
25+
swapFree = strtoul(token + strlen("SwapFree:"), NULL, 10);
26+
27+
FFSwapResult* swap = ffListAdd(result);
28+
ffStrbufInitStatic(&swap->name, "Total");
29+
swap->bytesTotal = swapTotal * 1024lu;
30+
swap->bytesUsed = (swapTotal - swapFree) * 1024lu;
31+
32+
return NULL;
33+
}
34+
35+
static const char* detectByProcSwaps(FFlist* result)
936
{
1037
// Ref: #620
1138
char buf[PROC_FILE_BUFFSIZ];
@@ -46,3 +73,10 @@ const char* ffDetectSwap(FFlist* result)
4673

4774
return NULL;
4875
}
76+
77+
const char* ffDetectSwap(FFlist* result)
78+
{
79+
if (detectByProcSwaps(result) == NULL)
80+
return NULL;
81+
return detectByProcMeminfo(result);
82+
}

0 commit comments

Comments
 (0)