Skip to content

Commit 3c75675

Browse files
committed
Use anonymous shared memory on FreeBSD
It works in Capsicum capability mode (sandbox)
1 parent b3dd16a commit 3c75675

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/platform/unix/mod.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,9 @@ fn recv(fd: c_int, blocking_mode: BlockingMode)
947947
Ok((main_data_buffer, channels, shared_memory_regions))
948948
}
949949

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

963+
#[cfg(target_os="freebsd")]
964+
fn create_shmem(_name: CString, length: usize) -> c_int {
965+
unsafe {
966+
// NB: the FreeBSD man page for shm_unlink states that it requires
967+
// write permissions, but testing shows that read-write is required.
968+
let fd = libc::shm_open(1 as *mut i8, // SHM_ANON
969+
libc::O_CREAT | libc::O_RDWR | libc::O_EXCL,
970+
0o600);
971+
assert!(fd >= 0);
972+
assert!(libc::ftruncate(fd, length as off_t) == 0);
973+
fd
974+
}
975+
}
976+
965977
#[cfg(all(feature="memfd", target_os="linux"))]
966978
fn create_shmem(name: CString, length: usize) -> c_int {
967979
unsafe {

0 commit comments

Comments
 (0)