Skip to content

Commit 2c80cd6

Browse files
committed
Swap (Linux): support detailed info
1 parent fa4b00b commit 2c80cd6

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/detection/swap/swap_linux.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <inttypes.h>
77

8-
const char* ffDetectSwap(FFSwapResult* swap)
8+
const char* ffDetectSwap(FFlist* result)
99
{
1010
// Ref: #620
1111
char buf[PROC_FILE_BUFFSIZ];
@@ -20,17 +20,29 @@ const char* ffDetectSwap(FFSwapResult* swap)
2020
while(line && *++line)
2121
{
2222
uint64_t total, used;
23-
if(sscanf(line, "%*[^\t]%" SCNu64 "%" SCNu64, &total, &used) != 2)
23+
char name[256];
24+
if(sscanf(line, "%255s %*[^\t]%" SCNu64 "%" SCNu64, name, &total, &used) != 3)
2425
return "Invalid /proc/swaps format found";
2526

26-
swap->bytesTotal += total;
27-
swap->bytesUsed += used;
27+
uint32_t nameLen = (uint32_t) strnlen(name, sizeof(name));
28+
FFSwapResult* swap = ffListAdd(result);
29+
ffStrbufInitA(&swap->name, nameLen);
30+
for (size_t i = 0; i < nameLen; ++i)
31+
{
32+
if(name[i] == '\\')
33+
{
34+
char octal[4] = { name[i + 1], name[i + 2], name[i + 3], '\0' };
35+
ffStrbufAppendC(&swap->name, (char) strtol(octal, NULL, 8));
36+
i += 3;
37+
}
38+
else
39+
ffStrbufAppendC(&swap->name, name[i]);
40+
}
41+
swap->bytesTotal = total * 1024u;
42+
swap->bytesUsed = used * 1024u;
2843

2944
line = memchr(line, '\n', (size_t) (nRead - (line - buf)));
3045
}
3146

32-
swap->bytesTotal *= 1024u;
33-
swap->bytesUsed *= 1024u;
34-
3547
return NULL;
3648
}

0 commit comments

Comments
 (0)