From d928f8d073f87fdacaf9e93b616f5b84695036e3 Mon Sep 17 00:00:00 2001 From: yangjiaqi Date: Wed, 22 Mar 2023 15:31:34 +0800 Subject: [PATCH] proc: fix /proc/diskstats output format --- src/proc_fuse.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/proc_fuse.c b/src/proc_fuse.c index fe81cad..ce22974 100644 --- a/src/proc_fuse.c +++ b/src/proc_fuse.c @@ -784,10 +784,10 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset, memset(lbuf, 0, 256); if (stats.read || stats.write || stats.read_merged || stats.write_merged || stats.read_sectors || stats.write_sectors || stats.read_ticks || - stats.write_ticks || stats.ios_pgr || stats.total_ticks || stats.rq_ticks || + stats.write_ticks || stats.ios_pgr || stats.total_ticks || stats.rq_ticks || stats.discard || stats.discard_merged || stats.discard_sectors || stats.discard_ticks) { if (need_record_diskstats(stats.major, stats.minor)) { - sscanf(line, "%u %u %71s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", + sscanf(line, "%u %u %71s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", &stats.major, &stats.minor, tmp_dev_name, @@ -802,11 +802,12 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset, &stats.ios_pgr, &stats.total_ticks, &stats.rq_ticks, + &stats.discard, &stats.discard_merged, &stats.discard_sectors, &stats.discard_ticks); } - snprintf(lbuf, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", + ret = snprintf(lbuf, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats.major, stats.minor, stats.dev_name, @@ -821,11 +822,17 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset, stats.ios_pgr, stats.total_ticks, stats.rq_ticks, + stats.discard, stats.discard_merged, stats.discard_sectors, stats.discard_ticks); + if(ret >= 256) { + lxcfs_error("Insufficient buffer for %u:%u %s diskstats", + stats.major, stats.minor, stats.dev_name); + continue; + } } else if (need_record_diskstats(stats.major, stats.minor)) { - sscanf(line, "%u %u %71s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", + sscanf(line, "%u %u %71s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", &stats.major, &stats.minor, tmp_dev_name, @@ -840,10 +847,11 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset, &stats.ios_pgr, &stats.total_ticks, &stats.rq_ticks, + &stats.discard, &stats.discard_merged, &stats.discard_sectors, &stats.discard_ticks); - snprintf(lbuf, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", + ret = snprintf(lbuf, 256, "%u %u %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", stats.major, stats.minor, stats.dev_name, @@ -858,9 +866,15 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset, stats.ios_pgr, stats.total_ticks, stats.rq_ticks, + stats.discard, stats.discard_merged, stats.discard_sectors, stats.discard_ticks); + if(ret >= 256) { + lxcfs_error("Insufficient buffer for %u:%u %s diskstats", + stats.major, stats.minor, stats.dev_name); + continue; + } } else { continue; } -- 2.30.0