return 0;
}
-int convert_meminfo_value_to_uint64_bytes(char *word, uint64_t *ret) {
+int convert_meminfo_value_to_uint64_bytes(const char *word, uint64_t *ret) {
+ _cleanup_free_ char *w = NULL;
char *digits, *e;
uint64_t v;
size_t n;
assert(word);
assert(ret);
+ w = strdup(word);
+ if (!w)
+ return -ENOMEM;
+
/* Determine length of numeric value */
- n = strspn(word, WHITESPACE);
- digits = word + n;
+ n = strspn(w, WHITESPACE);
+ digits = w + n;
n = strspn(digits, DIGITS);
if (n == 0)
return -EINVAL;
if (v == UINT64_MAX)
return -EINVAL;
+ if (v > UINT64_MAX/1024)
+ return -EOVERFLOW;
+
*ret = v * 1024U;
return 0;
}
assert(threshold_permyriad <= 10000);
mem_threshold = ctx->mem_total * threshold_permyriad / (uint64_t) 10000;
- return (ctx->mem_total - ctx->mem_used) < mem_threshold;
+ return LESS_BY(ctx->mem_total, ctx->mem_used) < mem_threshold;
}
bool oomd_swap_free_below(const OomdSystemContext *ctx, int threshold_permyriad) {
uint64_t mem_free, swap_free;
int r;
+ enum {
+ MEM_TOTAL = 1U << 0,
+ MEM_FREE = 1U << 1,
+ SWAP_TOTAL = 1U << 2,
+ SWAP_FREE = 1U << 3,
+ ALL = MEM_TOTAL|MEM_FREE|SWAP_TOTAL|SWAP_FREE,
+ };
+
assert(proc_meminfo_path);
assert(ret);
return -EINVAL;
if ((word = startswith(line, "MemTotal:"))) {
- field_filled |= 1U << 0;
+ field_filled |= MEM_TOTAL;
r = convert_meminfo_value_to_uint64_bytes(word, &ctx.mem_total);
} else if ((word = startswith(line, "MemFree:"))) {
- field_filled |= 1U << 1;
+ field_filled |= MEM_FREE;
r = convert_meminfo_value_to_uint64_bytes(word, &mem_free);
} else if ((word = startswith(line, "SwapTotal:"))) {
- field_filled |= 1U << 2;
+ field_filled |= SWAP_TOTAL;
r = convert_meminfo_value_to_uint64_bytes(word, &ctx.swap_total);
} else if ((word = startswith(line, "SwapFree:"))) {
- field_filled |= 1U << 3;
+ field_filled |= SWAP_FREE;
r = convert_meminfo_value_to_uint64_bytes(word, &swap_free);
} else
continue;
if (r < 0)
return log_debug_errno(r, "Error converting '%s' from %s to uint64_t: %m", line, proc_meminfo_path);
- if (field_filled == 15U)
+ if (field_filled == ALL)
break;
}
- if (field_filled != 15U)
+ if (field_filled != ALL)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "%s is missing expected fields", proc_meminfo_path);
if (mem_free > ctx.mem_total)