From: Michal Privoznik Date: Mon, 28 Feb 2022 15:30:08 +0000 (+0100) Subject: lxcProcReadMeminfo: Fix case when @offset != 0 X-Git-Tag: v8.2.0-rc1~177 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=v8.1.0-61-g030faee28d;p=thirdparty%2Flibvirt.git lxcProcReadMeminfo: Fix case when @offset != 0 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 --- 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; }