tools/testing/selftests/vfio/lib/sysfs.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/vfio/lib/sysfs.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/vfio/lib/sysfs.c- Extension
.c- Size
- 3462 bytes
- Lines
- 151
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
fcntl.hunistd.hstdlib.hstring.hlinux/limits.hlibvfio.h
Detected Declarations
function readlink_basefunction sysfs_val_get_intfunction sysfs_val_setfunction sysfs_device_val_getfunction sysfs_device_val_setfunction sysfs_device_val_set_intfunction sysfs_sriov_totalvfs_getfunction sysfs_sriov_numvfs_getfunction sysfs_sriov_numvfs_setfunction sysfs_iommu_group_get
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-only
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <libvfio.h>
#define readlink_safe(_path, _buf) ({ \
int __ret; \
\
_Static_assert(!__builtin_types_compatible_p( \
__typeof__(_buf), char *), \
"readlink_safe: _buf must be an array, not a pointer"); \
\
__ret = readlink(_path, _buf, sizeof(_buf) - 1); \
if (__ret != -1) \
_buf[__ret] = 0; \
__ret; \
})
static void readlink_base(const char *path, const char *data_fmt, void *out_data)
{
char rl_path[PATH_MAX];
int ret;
ret = readlink_safe(path, rl_path);
VFIO_ASSERT_NE(ret, -1);
ret = sscanf(basename(rl_path), data_fmt, out_data);
VFIO_ASSERT_EQ(ret, 1);
}
static int sysfs_val_get_int(const char *component, const char *name,
const char *file)
{
char path[PATH_MAX];
char buf[32];
int ret;
int fd;
snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name, file);
fd = open(path, O_RDONLY);
if (fd < 0)
return fd;
VFIO_ASSERT_GT(read(fd, buf, ARRAY_SIZE(buf)), 0);
VFIO_ASSERT_EQ(close(fd), 0);
errno = 0;
ret = strtol(buf, NULL, 0);
VFIO_ASSERT_EQ(errno, 0, "sysfs path \"%s\" is not an integer: \"%s\"\n", path, buf);
return ret;
}
static void sysfs_val_set(const char *component, const char *name,
const char *file, const char *val)
{
char path[PATH_MAX];
int fd;
snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name, file);
VFIO_ASSERT_GT(fd = open(path, O_WRONLY), 0);
VFIO_ASSERT_EQ(write(fd, val, strlen(val)), strlen(val));
VFIO_ASSERT_EQ(close(fd), 0);
}
static int sysfs_device_val_get(const char *bdf, const char *file)
{
return sysfs_val_get_int("devices", bdf, file);
}
static void sysfs_device_val_set(const char *bdf, const char *file, const char *val)
{
sysfs_val_set("devices", bdf, file, val);
}
static void sysfs_device_val_set_int(const char *bdf, const char *file, int val)
{
char val_str[32];
snprintf_assert(val_str, sizeof(val_str), "%d", val);
sysfs_device_val_set(bdf, file, val_str);
}
int sysfs_sriov_totalvfs_get(const char *bdf)
{
Annotation
- Immediate include surface: `fcntl.h`, `unistd.h`, `stdlib.h`, `string.h`, `linux/limits.h`, `libvfio.h`.
- Detected declarations: `function readlink_base`, `function sysfs_val_get_int`, `function sysfs_val_set`, `function sysfs_device_val_get`, `function sysfs_device_val_set`, `function sysfs_device_val_set_int`, `function sysfs_sriov_totalvfs_get`, `function sysfs_sriov_numvfs_get`, `function sysfs_sriov_numvfs_set`, `function sysfs_iommu_group_get`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.