saluki_env/features/
detector.rs1use std::path::PathBuf;
2
3use tracing::info;
4
5use super::{has_host_mapped_cgroupfs, has_host_mapped_procfs, has_legacy_cgroupfs_root, ContainerdDetector, Feature};
6
7#[derive(Clone)]
17pub struct FeatureDetector {
18 detected_features: Feature,
19}
20
21impl FeatureDetector {
22 pub fn automatic(containerd_socket_path: Option<PathBuf>) -> Self {
27 Self::for_feature(containerd_socket_path, Feature::all_bits())
28 }
29
30 pub fn for_feature(containerd_socket_path: Option<PathBuf>, feature_mask: Feature) -> Self {
37 let detected_features = Self::detect_features(containerd_socket_path, feature_mask);
38
39 Self { detected_features }
40 }
41
42 pub fn is_feature_available(&self, feature: Feature) -> bool {
44 self.detected_features.contains(feature)
45 }
46
47 fn detect_features(containerd_socket_path: Option<PathBuf>, feature_mask: Feature) -> Feature {
48 let mut detected_features = Feature::none();
49
50 if feature_mask.contains(Feature::HostMappedProcfs) && has_host_mapped_procfs() {
51 info!("Detected presence of host-mapped procfs.");
52 detected_features |= Feature::HostMappedProcfs;
53 }
54
55 if feature_mask.contains(Feature::HostMappedCgroupfs) && has_host_mapped_cgroupfs() {
56 info!("Detected presence of host-mapped cgroupfs.");
57 detected_features |= Feature::HostMappedCgroupfs;
58 }
59
60 if feature_mask.contains(Feature::LegacyCgroupfsRoot) && has_legacy_cgroupfs_root() {
61 info!("Detected presence of legacy cgroupfs root.");
62 detected_features |= Feature::LegacyCgroupfsRoot;
63 }
64
65 if feature_mask.contains(Feature::Containerd)
66 && ContainerdDetector::detect_grpc_socket_path(containerd_socket_path).is_some()
67 {
68 info!("Detected presence of containerd.");
69 detected_features |= Feature::Containerd;
70 }
71
72 detected_features
73 }
74}
75
76#[cfg(all(test, target_os = "linux"))]
77impl FeatureDetector {
78 pub(crate) fn from_detected_features(detected_features: Feature) -> Self {
84 Self { detected_features }
85 }
86}