Skip to content

Commit

Permalink
Implement fallback for when memfd is not available on Linux
Browse files Browse the repository at this point in the history
WSL is a "Linux" environment, but it does not support the memfd_create syscall. Thus we fall back to other strategies here.

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
  • Loading branch information
bwoebi committed Aug 21, 2024
1 parent df81898 commit bd19a39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 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 @@ -16,7 +16,6 @@ use std::io;
use std::num::NonZeroUsize;
use std::os::unix::fs::MetadataExt;
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(not(target_os = "linux"))]
use std::sync::atomic::{AtomicI32, Ordering};

pub(crate) fn mmap_handle<T: FileBackedHandle>(handle: T) -> io::Result<MappedMem<T>> {
Expand All @@ -42,23 +41,23 @@ pub(crate) fn munmap_handle<T: MemoryHandle>(mapped: &mut MappedMem<T>) {
}
}

#[cfg(not(target_os = "linux"))]
static ANON_SHM_ID: AtomicI32 = AtomicI32::new(0);

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

#[cfg(not(target_os = "linux"))]
fn open_anon_shm() -> anyhow::Result<RawFd> {
fn open_anon_shm_generic() -> anyhow::Result<RawFd> {
let path = format!(
"/libdatadog-shm-anon-{}-{}",
unsafe { libc::getpid() },
ANON_SHM_ID.fetch_add(1, Ordering::SeqCst)
crate::platform::unix::mem_handle::ANON_SHM_ID.fetch_add(1, Ordering::SeqCst)
);
let result = shm_open(
path.as_bytes(),
Expand All @@ -69,6 +68,11 @@ impl ShmHandle {
Ok(result?)
}

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

pub fn new(size: usize) -> anyhow::Result<ShmHandle> {
let fd = Self::open_anon_shm()?;
let handle = unsafe { PlatformHandle::from_raw_fd(fd) };
Expand Down
21 changes: 13 additions & 8 deletions spawn_worker/src/unix/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ impl SpawnWorker {

// setup final spawn

let spawn_method = match &self.spawn_method {
#[allow(unused_mut)]
let mut spawn_method = match &self.spawn_method {
Some(m) => m.clone(),
None => self.target.detect_spawn_method()?,
};
Expand Down Expand Up @@ -415,13 +416,17 @@ impl SpawnWorker {
};
#[cfg(target_os = "linux")]
if matches!(spawn_method, SpawnMethod::FdExec) {
let memfd = linux::write_memfd("trampoline_dependencies.so", bin)?;
let basefds = if fd_to_pass.is_some() { 4 } else { 3 };
argv.push(CString::new(format!(
"/proc/self/fd/{}",
temp_memfds.len() + basefds
))?);
temp_memfds.push(memfd);
if let Ok(memfd) = linux::write_memfd("trampoline_dependencies.so", bin) {
let basefds = if fd_to_pass.is_some() { 4 } else { 3 };
argv.push(CString::new(format!(
"/proc/self/fd/{}",
temp_memfds.len() + basefds
))?);
temp_memfds.push(memfd);
} else {
spawn_method = SpawnMethod::Exec;
tempfile()?;
}
} else {
tempfile()?;
}
Expand Down

0 comments on commit bd19a39

Please sign in to comment.