Skip to content

Commit

Permalink
Add names to memfds
Browse files Browse the repository at this point in the history
I was confused while debugging what role was pertaining to what fd

Signed-off-by: Bob Weinand <[email protected]>
  • Loading branch information
bwoebi committed Nov 6, 2024
1 parent 7039e0f commit a6c810f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
20 changes: 12 additions & 8 deletions ipc/src/platform/unix/mem_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ static ANON_SHM_ID: AtomicI32 = AtomicI32::new(0);

impl ShmHandle {
#[cfg(target_os = "linux")]
fn open_anon_shm() -> anyhow::Result<OwnedFd> {
if let Ok(memfd) = memfd::MemfdOptions::default().create("anon-shm-handle") {
fn open_anon_shm(name: &str) -> anyhow::Result<OwnedFd> {
if let Ok(memfd) = memfd::MemfdOptions::default().create(name) {
Ok(memfd.into_file().into())
} else {
Self::open_anon_shm_generic()
Self::open_anon_shm_generic(name)
}
}

fn open_anon_shm_generic() -> anyhow::Result<OwnedFd> {
fn open_anon_shm_generic(name: &str) -> anyhow::Result<OwnedFd> {
let path = format!(
"/libdatadog-shm-anon-{}-{}",
"/libdatadog-shm-{name}-{}-{}",
unsafe { libc::getpid() },
ANON_SHM_ID.fetch_add(1, Ordering::SeqCst)
);
Expand All @@ -68,12 +68,16 @@ impl ShmHandle {
}

#[cfg(not(target_os = "linux"))]
fn open_anon_shm() -> anyhow::Result<OwnedFd> {
Self::open_anon_shm_generic()
fn open_anon_shm(name: &str) -> anyhow::Result<OwnedFd> {
Self::open_anon_shm_generic(name)
}

pub fn new(size: usize) -> anyhow::Result<ShmHandle> {
let fd = Self::open_anon_shm()?;
Self::new_named(size, "anon-handle")
}

pub fn new_named(size: usize, name: &str) -> anyhow::Result<ShmHandle> {
let fd = Self::open_anon_shm(name)?;
let handle: PlatformHandle<OwnedFd> = fd.into();
ftruncate(handle.as_owned_fd()?, size as off_t)?;
Ok(ShmHandle { handle, size })
Expand Down
6 changes: 5 additions & 1 deletion ipc/src/platform/unix/mem_handle_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static ANON_SHM_ID: AtomicI32 = AtomicI32::new(0);
impl ShmHandle {
pub fn new(size: usize) -> anyhow::Result<ShmHandle> {
let path = format!(
"dd-shm-anon-{}-{}",
"ddshm-anon-{}-{}",
unsafe { libc::getpid() },
ANON_SHM_ID.fetch_add(1, Ordering::SeqCst)
);
Expand All @@ -85,6 +85,10 @@ impl ShmHandle {
size: size | NOT_COMMITTED,
})
}

pub fn new_named(size: usize, _name: &str) -> anyhow::Result<ShmHandle> {
Self::new(size)
}
}
fn path_slice(path: &CStr) -> &[u8] {
assert_eq!(path.to_bytes()[0], b'/');
Expand Down
6 changes: 5 additions & 1 deletion ipc/src/platform/windows/mem_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ static ANON_HANDLE_COUNTER: AtomicU32 = AtomicU32::new(0);

impl ShmHandle {
pub fn new(size: usize) -> anyhow::Result<ShmHandle> {
Self::new_named(size, "shm-handle")
}

pub fn new_named(size: usize, name: &str) -> anyhow::Result<ShmHandle> {
// If one uses null_mut() for the name, DuplicateHandle will emit a very
// confusing "The system cannot find the file specified. (os error 2)".
// It seems like DuplicateHandle requires a name to re-open the FileMapping
// within another process. Oh well. Let's generate an unique one.
let name = CString::new(format!(
"libdatadog-anon-{}-{}",
"libdatadog-anon-{name}-{}-{}",
unsafe { libc::getpid() },
ANON_HANDLE_COUNTER.fetch_add(1, Ordering::SeqCst)
))
Expand Down
14 changes: 13 additions & 1 deletion sidecar-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use datadog_sidecar::shm_remote_config::{path_for_remote_config, RemoteConfigRea
use ddcommon::tag::Tag;
use ddcommon::Endpoint;
use ddcommon_ffi as ffi;
use ddcommon_ffi::MaybeError;
use ddcommon_ffi::{CharSlice, MaybeError};
use ddtelemetry::{
data::{self, Dependency, Integration},
worker::{LifecycleAction, TelemetryActions},
Expand Down Expand Up @@ -94,6 +94,18 @@ pub extern "C" fn ddog_alloc_anon_shm_handle(
MaybeError::None
}

#[no_mangle]
pub extern "C" fn ddog_alloc_anon_shm_handle_named(
size: usize,
handle: &mut *mut ShmHandle,
name: CharSlice,
) -> MaybeError {
let name = name.to_utf8_lossy();
*handle = Box::into_raw(Box::new(try_c!(ShmHandle::new_named(size, name.as_ref()))));

MaybeError::None
}

#[no_mangle]
pub extern "C" fn ddog_map_shm(
handle: Box<ShmHandle>,
Expand Down

0 comments on commit a6c810f

Please sign in to comment.