From 57f88d21674805f2f19380494bcd261a433d5d87 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Mon, 4 Dec 2023 17:29:20 +0900 Subject: [PATCH] fix(Uptime): fix reading procfs files and simplify data parsing --- src/detection/uptime/uptime_linux.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/detection/uptime/uptime_linux.c b/src/detection/uptime/uptime_linux.c index 7c0a46161..f4ea790a1 100644 --- a/src/detection/uptime/uptime_linux.c +++ b/src/detection/uptime/uptime_linux.c @@ -7,14 +7,17 @@ const char* ffDetectUptime(FFUptimeResult* result) { // #620 - FF_AUTO_CLOSE_FILE FILE* uptime = fopen("/proc/uptime", "r"); - if (!uptime) return "fopen(\"/proc/uptime\", \"r\") failed"; + char buf[64]; + ssize_t nRead = ffReadFileData("/proc/uptime", sizeof(buf) - 1, buf); + if(nRead < 0) + return "ffReadFileData(\"/proc/uptime\", sizeof(buf) - 1, buf) failed"; + buf[nRead] = '\0'; double sec; - if (fscanf(uptime, "%lf", &sec) > 0) + if(sscanf(buf, "%lf", &sec) > 0) result->uptime = (uint64_t) (sec * 1000); else - return "fscanf(\"%lf\", &sec) failed"; + return "sscanf(buf.chars, \"%lf\", &sec) failed"; result->bootTime = ffTimeGetNow() + result->uptime;