Skip to content

Commit

Permalink
Add wrapper for unix specific functions
Browse files Browse the repository at this point in the history
  • Loading branch information
VianneyRuhlmann committed Jun 11, 2024
1 parent 29c7824 commit 250a823
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
14 changes: 0 additions & 14 deletions ddcommon/src/entity_id/fallback.rs

This file was deleted.

54 changes: 44 additions & 10 deletions ddcommon/src/entity_id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,50 @@
//! 1:name=systemd:/ecs/8cd79a803caf4d2aa945152e934a5c00/8cd79a803caf4d2aa945152e934a5c00-1053176469
//! ```

#[cfg(not(unix))]
pub use fallback::{get_container_id, get_entity_id, set_cgroup_file, set_cgroup_mount_path};

#[cfg(unix)]
pub use unix::{get_container_id, get_entity_id, set_cgroup_file, set_cgroup_mount_path};

/// Fallback module used for non-unix systems
#[cfg(not(unix))]
mod fallback;

/// Unix specific module allowing the use of unix specific functions
#[cfg(unix)]
mod unix;

/// Returns the `container_id` if available in the cgroup file, otherwise returns `None`
pub fn get_container_id() -> Option<&'static str> {
#[cfg(unix)]
{
unix::get_container_id()
}
#[cfg(not(unix))]
{
None
}
}

/// Returns the `entity_id` if available, either `cid-<container_id>` or `in-<cgroup_inode>`
pub fn get_entity_id() -> Option<&'static str> {
#[cfg(unix)]
{
unix::get_entity_id()
}
#[cfg(not(unix))]
{
None
}
}

/// Set the path to cgroup file to mock it during tests
/// # Safety
/// Must not be called in multi-threaded contexts
pub unsafe fn set_cgroup_file(file: String) {
#[cfg(unix)]
{
unix::set_cgroup_file(file)
}
}

/// Set cgroup mount path to mock during tests
/// # Safety
/// Must not be called in multi-threaded contexts
pub unsafe fn set(path: String) {
#[cfg(unix)]
{
unix::set_cgroup_mount_path(path)
}
}

0 comments on commit 250a823

Please sign in to comment.