Skip to content

Commit

Permalink
Use anonymous shared memory on FreeBSD
Browse files Browse the repository at this point in the history
It works in Capsicum capability mode (sandbox)
  • Loading branch information
valpackett committed Nov 6, 2017
1 parent b3dd16a commit 3c75675
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,11 +947,9 @@ fn recv(fd: c_int, blocking_mode: BlockingMode)
Ok((main_data_buffer, channels, shared_memory_regions))
}

#[cfg(not(all(target_os="linux", feature="memfd")))]
#[cfg(not(any(target_os="freebsd", all(target_os="linux", feature="memfd"))))]
fn create_shmem(name: CString, length: usize) -> c_int {
unsafe {
// NB: the FreeBSD man page for shm_unlink states that it requires
// write permissions, but testing shows that read-write is required.
let fd = libc::shm_open(name.as_ptr(),
libc::O_CREAT | libc::O_RDWR | libc::O_EXCL,
0o600);
Expand All @@ -962,6 +960,20 @@ fn create_shmem(name: CString, length: usize) -> c_int {
}
}

#[cfg(target_os="freebsd")]
fn create_shmem(_name: CString, length: usize) -> c_int {
unsafe {
// NB: the FreeBSD man page for shm_unlink states that it requires
// write permissions, but testing shows that read-write is required.
let fd = libc::shm_open(1 as *mut i8, // SHM_ANON
libc::O_CREAT | libc::O_RDWR | libc::O_EXCL,
0o600);
assert!(fd >= 0);
assert!(libc::ftruncate(fd, length as off_t) == 0);
fd
}
}

#[cfg(all(feature="memfd", target_os="linux"))]
fn create_shmem(name: CString, length: usize) -> c_int {
unsafe {
Expand Down

0 comments on commit 3c75675

Please sign in to comment.