Skip to content

Commit 072693c

Browse files
committed
lib: Introduce krun_has_feature() API
Introduce a function to query at runtime whether a specific feature was enabled at build time. This allows applications to check for optional capabilities before attempting to use them. This is especially useful for our tests and examples. Signed-off-by: Matej Hrica <mhrica@redhat.com>
1 parent 39ede11 commit 072693c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

include/libkrun.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,20 @@ int32_t krun_set_nested_virt(uint32_t ctx_id, bool enabled);
977977
*/
978978
int32_t krun_check_nested_virt(void);
979979

980+
/**
981+
* Checks if a specific feature was enabled at build time.
982+
*
983+
* Arguments:
984+
* "feature" - a null-terminated string representing the feature name.
985+
* Supported feature names: "NET", "BLK", "GPU", "SND", "INPUT",
986+
* "EFI", "TEE", "SEV", "TDX", "NITRO", "VIRGL_RESOURCE_MAP2"
987+
*
988+
* Returns:
989+
* 1 if the feature is supported, 0 if not supported, or a negative error
990+
* number on failure (e.g., -EINVAL for invalid input).
991+
*/
992+
int32_t krun_has_feature(const char *feature);
993+
980994
/**
981995
* Get the maximum number of vCPUs supported by the hypervisor.
982996
*

src/libkrun/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,36 @@ pub unsafe extern "C" fn krun_check_nested_virt() -> i32 {
18441844
-libc::EOPNOTSUPP
18451845
}
18461846

1847+
#[allow(clippy::missing_safety_doc)]
1848+
#[no_mangle]
1849+
pub unsafe extern "C" fn krun_has_feature(feature: *const c_char) -> i32 {
1850+
if feature.is_null() {
1851+
return -libc::EINVAL;
1852+
}
1853+
1854+
let feature = match CStr::from_ptr(feature).to_str() {
1855+
Ok(f) => f,
1856+
Err(_) => return -libc::EINVAL,
1857+
};
1858+
1859+
let supported = match feature {
1860+
"NET" => cfg!(feature = "net"),
1861+
"BLK" => cfg!(feature = "blk"),
1862+
"GPU" => cfg!(feature = "gpu"),
1863+
"SND" => cfg!(feature = "snd"),
1864+
"INPUT" => cfg!(feature = "input"),
1865+
"EFI" => cfg!(feature = "efi"),
1866+
"TEE" => cfg!(feature = "tee"),
1867+
"SEV" => cfg!(feature = "amd-sev"),
1868+
"TDX" => cfg!(feature = "tdx"),
1869+
"NITRO" => cfg!(feature = "nitro"),
1870+
"VIRGL_RESOURCE_MAP2" => cfg!(feature = "virgl_resource_map2"),
1871+
_ => return -libc::EINVAL,
1872+
};
1873+
1874+
supported as i32
1875+
}
1876+
18471877
/// Gets the maximum number of vCPUs supported by the hypervisor.
18481878
///
18491879
/// Returns the maximum number of vCPUs that can be created by this hypervisor,

0 commit comments

Comments
 (0)