vfio: selftests: Move helper to get cdev path to libvfio

Move the helper function to get the VFIO cdev path to libvfio so that it
can be used in libvfio in a subsequent commit.

No functional change intended.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-24-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
David Matlack
2025-08-22 21:25:10 +00:00
committed by Alex Williamson
parent 35b05bd962
commit 118e073ef6
3 changed files with 34 additions and 32 deletions

View File

@@ -1,8 +1,4 @@
// SPDX-License-Identifier: GPL-2.0
#include <assert.h>
#include <dirent.h>
#include <fcntl.h>
#include <uapi/linux/types.h>
#include <linux/limits.h>
#include <linux/sizes.h>
@@ -11,7 +7,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
@@ -19,32 +14,7 @@
#include "../kselftest_harness.h"
static const char iommu_dev_path[] = "/dev/iommu";
static char cdev_path[PATH_MAX] = { '\0' };
static void set_cdev_path(const char *bdf)
{
char dir_path[PATH_MAX];
DIR *dir;
struct dirent *entry;
snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf);
dir = opendir(dir_path);
assert(dir);
/* Find the file named "vfio<number>" */
while ((entry = readdir(dir)) != NULL) {
if (!strncmp("vfio", entry->d_name, 4)) {
snprintf(cdev_path, sizeof(cdev_path), "/dev/vfio/devices/%s",
entry->d_name);
break;
}
}
assert(strlen(cdev_path) > 0);
closedir(dir);
}
static const char *cdev_path;
static int vfio_device_bind_iommufd_ioctl(int cdev_fd, int iommufd)
{
@@ -150,7 +120,7 @@ int main(int argc, char *argv[])
{
const char *device_bdf = vfio_selftests_get_bdf(&argc, argv);
set_cdev_path(device_bdf);
cdev_path = vfio_pci_get_cdev_path(device_bdf);
printf("Using cdev device %s\n", cdev_path);
return test_harness_run(argc, argv);