[PATCH 13/17] lxcProcReadMeminfo: Fix case when @offset != 0

Michal Privoznik mprivozn at redhat.com
Mon Mar 7 09:01:03 UTC 2022


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 <mprivozn at redhat.com>
---
 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;
 }
-- 
2.34.1



More information about the libvir-list mailing list