Report swapinfo#140
Conversation
Allows distinguishing how much memory the qube is using from what is assigned by using "meminfo - swapinfo". For: QubesOS/qubes-core-admin#827
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #140 +/- ##
=======================================
Coverage 71.24% 71.24%
=======================================
Files 6 6
Lines 991 991
=======================================
Hits 706 706
Misses 285 285 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| sprintf(used_mem_buf, "%lld", used_mem); | ||
| sprintf(used_swap_buf, "%lld", used_swap); |
| syslog(LOG_DAEMON | LOG_ERR, "error writing meminfo to xenstore ?"); | ||
| exit(1); | ||
| } | ||
| if (!xs_write(xs, XBT_NULL, "memory/swapinfo", swap, strlen(swap))) { |
There was a problem hiding this comment.
Note to self: this can be merged only after core-admin part, which sets necessary xenstore access.
| char *swap; | ||
| } UsedMem; | ||
|
|
||
| UsedMem parse(const char *meminfo_buf, const char* dom_current_buf) |
There was a problem hiding this comment.
FWIW the usual method in C for returning more results is to either return a pointer to a structure (usually dynamically allocated via malloc), or add output parameters (pointers to where function should write result). In case of a simple structure like this, compiler will do the latter for you here, so it can stay this way, but keep in mind it wont be efficient code in more complex cases.
…hardening) Both sprintf() calls write at most ~20 bytes into buffers of 32 and 4096 bytes respectively, so no overflow is possible with valid input. Replacing with snprintf() is still correct defensive practice per CWE-676 and mirrors the change already made in QubesOS#140. Add unit tests for parse() that verify the memory-usage calculation and threshold/hysteresis logic. The tests pass with either sprintf() or snprintf(), confirming this is hardening rather than a bug fix. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
| return (UsedMem){ .mem = used_mem_buf, .swap = used_swap_buf }; | ||
| } | ||
| return NULL; | ||
| return (UsedMem){ .mem = NULL, .swap = NULL }; |
There was a problem hiding this comment.
Report swap change if above threshold despite memory being below its threshold.
| if (meminfo_data) | ||
| send_to_qmemman(xs, meminfo_data); | ||
| UsedMem meminfo_data = parse(meminfo_buf, dom_current_buf); | ||
| if (meminfo_data.mem && meminfo_data.mem[0]) |
There was a problem hiding this comment.
Allows distinguishing how much memory the qube is using from what is assigned by using "meminfo - swapinfo".
For: QubesOS/qubes-core-admin#827
Tested briefly.