aboutsummaryrefslogtreecommitdiff
path: root/samples/client.c
diff options
context:
space:
mode:
authorThanos Makatos <thanos.makatos@nutanix.com>2021-02-04 14:37:01 +0000
committerGitHub <noreply@github.com>2021-02-04 14:37:01 +0000
commit1193fa7b6b4afe00f199f5fe7e86ddff27723baa (patch)
tree89bc1f0379def0d60d7defabdb0439a8ba00eb1f /samples/client.c
parent02dda3178812eaca7384a88f078233067ca3a2f1 (diff)
downloadlibvfio-user-1193fa7b6b4afe00f199f5fe7e86ddff27723baa.zip
libvfio-user-1193fa7b6b4afe00f199f5fe7e86ddff27723baa.tar.gz
libvfio-user-1193fa7b6b4afe00f199f5fe7e86ddff27723baa.tar.bz2
client/server: move mapping sparse areas to separate function (#301)
And print file path when it fails to mmap. Signed-off-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'samples/client.c')
-rw-r--r--samples/client.c50
1 files changed, 36 insertions, 14 deletions
diff --git a/samples/client.c b/samples/client.c
index d5b3027..e7a04f5 100644
--- a/samples/client.c
+++ b/samples/client.c
@@ -44,6 +44,7 @@
#include <libgen.h>
#include <pthread.h>
#include <openssl/md5.h>
+#include <linux/limits.h>
#include "common.h"
#include "libvfio-user.h"
@@ -258,6 +259,40 @@ do_get_device_region_info(int sock, struct vfio_region_info *region_info,
}
}
+static void
+mmap_sparse_areas(int *fds, size_t nr_fds,
+ struct vfio_region_info_cap_sparse_mmap *sparse)
+{
+ size_t i;
+
+ assert(nr_fds == 2);
+ assert(sparse->nr_areas == 2);
+
+ for (i = 0; i < sparse->nr_areas; i++) {
+
+ ssize_t ret;
+ void *addr;
+ char pathname[BUFSIZ];
+ char buf[PATH_MAX];
+
+ ret = snprintf(pathname, sizeof pathname, "/proc/self/fd/%d", fds[i]);
+ assert(ret != -1 && (size_t)ret < sizeof(pathname));
+ ret = readlink(pathname, buf, sizeof(buf) - 1);
+ if (ret == -1) {
+ err(EXIT_FAILURE, "failed to resolve file descriptor %d", fds[i]);
+ }
+ buf[ret + 1] = '\0';
+ addr = mmap(NULL, sparse->areas[i].size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fds[i], sparse->areas[i].offset);
+ if (addr == MAP_FAILED) {
+ err(EXIT_FAILURE,
+ "failed to mmap sparse region #%lu in %s (%#llx-%#llx)",
+ i, buf, sparse->areas[i].offset,
+ sparse->areas[i].offset + sparse->areas[i].size - 1);
+ }
+ }
+}
+
static bool
get_device_region_info(int sock, uint32_t index)
{
@@ -296,20 +331,7 @@ get_device_region_info(int sock, uint32_t index)
if (get_region_vfio_caps((struct vfio_info_cap_header*)(region_info + 1),
sparse)) {
if (sparse != NULL) {
- size_t i;
- assert(nr_fds == 2);
- assert(sparse->nr_areas == 2);
- for (i = 0; i < sparse->nr_areas; i++) {
- void *addr = mmap(NULL, sparse->areas[i].size,
- PROT_READ | PROT_WRITE, MAP_SHARED,
- fds[i], sparse->areas[i].offset);
- if (addr == MAP_FAILED) {
- err(EXIT_FAILURE,
- "failed to mmap sparse region %lu (%#llx-%#llx)",
- i, sparse->areas[i].offset,
- sparse->areas[i].offset + sparse->areas[i].size - 1);
- }
- }
+ mmap_sparse_areas(fds, nr_fds, sparse);
}
return true;
}