From 030faee28d14ebcf4d48919b890d2f1e4ebc9e12 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Mon, 28 Feb 2022 16:30:08 +0100 Subject: [PATCH] lxcProcReadMeminfo: Fix case when @offset != 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The idea behind lxcProcReadMeminfo() is that we read the host's /proc/meminfo and copy it line by line producing the content for container, changing only those lines we need. Thus, when a process inside container opens the file and lseek()-s to a different position (or reads the content in small chunks), we mirror the seek in host's /proc/meminfo. But this doesn't work really. We are not guaranteed to end up aligned on the beginning of new line. It's better if we construct the new content and then mimic seeking in it. Signed-off-by: Michal Privoznik Reviewed-by: Ján Tomko --- src/lxc/lxc_fuse.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c index 537e16c380..b068d21cf4 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -152,6 +152,7 @@ lxcProcReadMeminfo(char *hostpath, size_t n; struct virLXCMeminfo meminfo; g_auto(virBuffer) buffer = VIR_BUFFER_INITIALIZER; + const char *new_meminfo = NULL; if (virLXCCgroupGetMeminfo(&meminfo) < 0) { virErrorSetErrnoFromLastError(); @@ -164,11 +165,6 @@ lxcProcReadMeminfo(char *hostpath, return -errno; } - if (fseek(fp, offset, SEEK_SET) < 0) { - virReportSystemError(errno, "%s", _("fseek failed")); - return -errno; - } - res = -1; while (getline(&line, &n, fp) > 0) { char *ptr = strchr(line, ':'); @@ -241,10 +237,18 @@ lxcProcReadMeminfo(char *hostpath, } } - res = strlen(virBufferCurrentContent(&buffer)); + + new_meminfo = virBufferCurrentContent(&buffer); + res = virBufferUse(&buffer); + + if (offset > res) + return 0; + + res -= offset; + if (res > size) res = size; - memcpy(buf, virBufferCurrentContent(&buffer), res); + memcpy(buf, new_meminfo + offset, res); return res; }