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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-08-24 23:56:06 +02:00
parent 65ba2ff4a7
commit 27f63ef988
+10 -5
View File
@@ -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;