From 27f63ef988f359825ca9608f63120d7da4c34b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 19 Aug 2026 08:54:48 +0200 Subject: [PATCH] basic/procfs-util: make procfs_cpu_get_ticks lenient again In review it was raised that the file is often synthesized and that this synthetization might not include all the fields, let's allow the more recently added fields to be missing to avoid a regression in compatibility. --- src/basic/procfs-util.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/basic/procfs-util.c b/src/basic/procfs-util.c index 4e80e873a8..a255153422 100644 --- a/src/basic/procfs-util.c +++ b/src/basic/procfs-util.c @@ -6,6 +6,7 @@ #include "extract-word.h" #include "fd-util.h" #include "fileio.h" +#include "log.h" #include "parse-util.h" #include "process-util.h" #include "procfs-util.h" @@ -170,10 +171,11 @@ int procfs_cpu_get_ticks(ProcfsCpuTicks *ret) { return -EINVAL; /* All eight fields exist on any kernel we support: iowait was added in 2.5.41, steal in 2.6.11. - * "guest" and "guest_nice" are not returned, the kernel already includes them in "user" and - * "nice". */ - ProcfsCpuTicks t; - if (sscanf(p, "%"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64, + * "guest" and "guest_nice" are not returned, the kernel already includes them in "user" and "nice". + * Nevertheless, this file might be synthesized slopilly, so let's not require the last three fields. + * We don't care about them in most cases anyway. */ + ProcfsCpuTicks t = {}; + r = sscanf(p, "%"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64, &t.user, &t.nice, &t.system, @@ -181,8 +183,11 @@ int procfs_cpu_get_ticks(ProcfsCpuTicks *ret) { &t.iowait, &t.irq, &t.softirq, - &t.steal) < 8) + &t.steal); + if (r < 5) return -EINVAL; + if (r < 8) + log_once(LOG_INFO, "Short read (%d fields) from /proc/stat, continuing.", r); *ret = t; return 0;