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 <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2022-02-28 16:30:08 +01:00
parent 2de984a26e
commit 030faee28d

View File

@ -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;
}