From cdf7a5a93be45c7c72bfdcbc5795a68cd8536c18 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sun, 10 Mar 2024 16:04:57 +0100 Subject: Avoid glibc FILE API for oom_score --- linux/LinuxProcessTable.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/linux/LinuxProcessTable.c b/linux/LinuxProcessTable.c index b7cc35a4..2285667d 100644 --- a/linux/LinuxProcessTable.c +++ b/linux/LinuxProcessTable.c @@ -933,19 +933,24 @@ static void LinuxProcessTable_readCGroupFile(LinuxProcess* process, openat_arg_t } static void LinuxProcessTable_readOomData(LinuxProcess* process, openat_arg_t procFd) { - FILE* file = fopenat(procFd, "oom_score", "r"); - if (!file) + char buffer[PROC_LINE_LENGTH + 1] = {0}; + + ssize_t oomRead = xReadfileat(procFd, "oom_score", buffer, sizeof(buffer)); + if (oomRead < 1) { return; + } - char buffer[PROC_LINE_LENGTH + 1]; - if (fgets(buffer, PROC_LINE_LENGTH, file)) { - unsigned int oom; - int ok = sscanf(buffer, "%u", &oom); - if (ok == 1) { - process->oom = oom; - } + char* oomPtr = buffer; + uint64_t oom = fast_strtoull_dec(&oomPtr, oomRead); + if (*oomPtr && *oomPtr != '\n' && *oomPtr != ' ') { + return; } - fclose(file); + + if (oom > UINT_MAX) { + return; + } + + process->oom = oom; } static void LinuxProcessTable_readAutogroup(LinuxProcess* process, openat_arg_t procFd) { -- cgit v1.2.3