Commit 625646ae authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Use pread() to read binary stats header

Use pread() with an explicit offset when reading the header and the header
name for a binary stats fd so that the common helper and the binary stats
test don't subtly rely on the file effectively being untouched, e.g. to
allow multiple reads of the header, name, etc.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Message-Id: <20230711230131.648752-3-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent eed3013f
...@@ -362,8 +362,10 @@ static inline void read_stats_header(int stats_fd, struct kvm_stats_header *head ...@@ -362,8 +362,10 @@ static inline void read_stats_header(int stats_fd, struct kvm_stats_header *head
{ {
ssize_t ret; ssize_t ret;
ret = read(stats_fd, header, sizeof(*header)); ret = pread(stats_fd, header, sizeof(*header), 0);
TEST_ASSERT(ret == sizeof(*header), "Read stats header"); TEST_ASSERT(ret == sizeof(*header),
"Failed to read '%lu' header bytes, ret = '%ld'",
sizeof(*header), ret);
} }
struct kvm_stats_desc *read_stats_descriptors(int stats_fd, struct kvm_stats_desc *read_stats_descriptors(int stats_fd,
......
...@@ -43,8 +43,10 @@ static void stats_test(int stats_fd) ...@@ -43,8 +43,10 @@ static void stats_test(int stats_fd)
id = malloc(header.name_size); id = malloc(header.name_size);
TEST_ASSERT(id, "Allocate memory for id string"); TEST_ASSERT(id, "Allocate memory for id string");
ret = read(stats_fd, id, header.name_size); ret = pread(stats_fd, id, header.name_size, sizeof(header));
TEST_ASSERT(ret == header.name_size, "Read id string"); TEST_ASSERT(ret == header.name_size,
"Expected header size '%u', read '%lu' bytes",
header.name_size, ret);
/* Check id string, that should start with "kvm" */ /* Check id string, that should start with "kvm" */
TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size, TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment