diff --git a/ipc/src/platform/unix/mem_handle.rs b/ipc/src/platform/unix/mem_handle.rs index 43a4b0c11..ab4691c48 100644 --- a/ipc/src/platform/unix/mem_handle.rs +++ b/ipc/src/platform/unix/mem_handle.rs @@ -44,17 +44,17 @@ static ANON_SHM_ID: AtomicI32 = AtomicI32::new(0); impl ShmHandle { #[cfg(target_os = "linux")] - fn open_anon_shm() -> anyhow::Result { - if let Ok(memfd) = memfd::MemfdOptions::default().create("anon-shm-handle") { + fn open_anon_shm(name: &str) -> anyhow::Result { + 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 { + fn open_anon_shm_generic(name: &str) -> anyhow::Result { let path = format!( - "/libdatadog-shm-anon-{}-{}", + "/libdatadog-shm-{name}-{}-{}", unsafe { libc::getpid() }, ANON_SHM_ID.fetch_add(1, Ordering::SeqCst) ); @@ -68,12 +68,16 @@ impl ShmHandle { } #[cfg(not(target_os = "linux"))] - fn open_anon_shm() -> anyhow::Result { - Self::open_anon_shm_generic() + fn open_anon_shm(name: &str) -> anyhow::Result { + Self::open_anon_shm_generic(name) } pub fn new(size: usize) -> anyhow::Result { - let fd = Self::open_anon_shm()?; + Self::new_named(size, "anon-handle") + } + + pub fn new_named(size: usize, name: &str) -> anyhow::Result { + let fd = Self::open_anon_shm(name)?; let handle: PlatformHandle = fd.into(); ftruncate(handle.as_owned_fd()?, size as off_t)?; Ok(ShmHandle { handle, size }) diff --git a/ipc/src/platform/unix/mem_handle_macos.rs b/ipc/src/platform/unix/mem_handle_macos.rs index bd26c0e50..15b82db6e 100644 --- a/ipc/src/platform/unix/mem_handle_macos.rs +++ b/ipc/src/platform/unix/mem_handle_macos.rs @@ -69,7 +69,7 @@ static ANON_SHM_ID: AtomicI32 = AtomicI32::new(0); impl ShmHandle { pub fn new(size: usize) -> anyhow::Result { let path = format!( - "dd-shm-anon-{}-{}", + "ddshm-anon-{}-{}", unsafe { libc::getpid() }, ANON_SHM_ID.fetch_add(1, Ordering::SeqCst) ); @@ -85,6 +85,10 @@ impl ShmHandle { size: size | NOT_COMMITTED, }) } + + pub fn new_named(size: usize, _name: &str) -> anyhow::Result { + Self::new(size) + } } fn path_slice(path: &CStr) -> &[u8] { assert_eq!(path.to_bytes()[0], b'/'); diff --git a/ipc/src/platform/windows/mem_handle.rs b/ipc/src/platform/windows/mem_handle.rs index 5635d3e7f..14b3102fc 100644 --- a/ipc/src/platform/windows/mem_handle.rs +++ b/ipc/src/platform/windows/mem_handle.rs @@ -91,12 +91,16 @@ static ANON_HANDLE_COUNTER: AtomicU32 = AtomicU32::new(0); impl ShmHandle { pub fn new(size: usize) -> anyhow::Result { + Self::new_named(size, "shm-handle") + } + + pub fn new_named(size: usize, name: &str) -> anyhow::Result { // 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) )) diff --git a/sidecar-ffi/src/lib.rs b/sidecar-ffi/src/lib.rs index 7f096d3c1..5cf52f00c 100644 --- a/sidecar-ffi/src/lib.rs +++ b/sidecar-ffi/src/lib.rs @@ -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}, @@ -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,