Skip to content

Commit 29c0ed9

Browse files
committed
Extract create_shmem into the shmemfdrs crate
(which adds support for FreeBSD SHM_ANON)
1 parent b225f6d commit 29c0ed9

File tree

3 files changed

+9
-37
lines changed

3 files changed

+9
-37
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repository = "https://github.com/servo/ipc-channel"
88

99
[features]
1010
force-inprocess = []
11-
memfd = ["sc"]
11+
memfd = ["shmemfdrs/memfd"]
1212
unstable = []
1313
async = ["futures"]
1414

@@ -24,8 +24,8 @@ tempfile = "3"
2424

2525
[target.'cfg(any(target_os = "linux", target_os = "openbsd", target_os = "freebsd"))'.dependencies]
2626
mio = "0.6.11"
27+
shmemfdrs = "0.1"
2728

28-
sc = { version = "0.2.2", optional = true }
2929
futures = { version = "0.1", optional = true }
3030

3131
[dev-dependencies]

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ extern crate mio;
2929
target_os = "openbsd",
3030
target_os = "freebsd")))]
3131
extern crate fnv;
32-
#[cfg(all(feature = "memfd", not(feature = "force-inprocess"),
33-
target_os="linux"))]
34-
#[macro_use]
35-
extern crate sc;
32+
#[cfg(all(not(feature = "force-inprocess"),
33+
any(target_os = "linux",
34+
target_os = "openbsd",
35+
target_os = "freebsd")))]
36+
extern crate shmemfdrs;
3637

3738
#[cfg(feature = "async")]
3839
extern crate futures;

src/platform/unix/mod.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bincode;
1111
use fnv::FnvHasher;
1212
use libc::{self, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE, SOCK_SEQPACKET, SOL_SOCKET};
1313
use libc::{SO_LINGER, S_IFMT, S_IFSOCK, c_char, c_int, c_void, getsockopt};
14-
use libc::{iovec, mode_t, msghdr, off_t, recvmsg, sendmsg};
14+
use libc::{iovec, mode_t, msghdr, recvmsg, sendmsg};
1515
use libc::{setsockopt, size_t, sockaddr, sockaddr_un, socketpair, socklen_t, sa_family_t};
1616
use std::cell::Cell;
1717
use std::cmp;
@@ -32,6 +32,7 @@ use std::thread;
3232
use mio::unix::EventedFd;
3333
use mio::{Poll, Token, Events, Ready, PollOpt};
3434
use tempfile::{Builder, TempDir};
35+
use shmemfdrs::create_shmem;
3536

3637
const MAX_FDS_IN_CMSG: u32 = 64;
3738

@@ -953,31 +954,6 @@ fn recv(fd: c_int, blocking_mode: BlockingMode)
953954
Ok((main_data_buffer, channels, shared_memory_regions))
954955
}
955956

956-
#[cfg(not(all(target_os="linux", feature="memfd")))]
957-
fn create_shmem(name: CString, length: usize) -> c_int {
958-
unsafe {
959-
// NB: the FreeBSD man page for shm_unlink states that it requires
960-
// write permissions, but testing shows that read-write is required.
961-
let fd = libc::shm_open(name.as_ptr(),
962-
libc::O_CREAT | libc::O_RDWR | libc::O_EXCL,
963-
0o600);
964-
assert!(fd >= 0);
965-
assert!(libc::shm_unlink(name.as_ptr()) == 0);
966-
assert!(libc::ftruncate(fd, length as off_t) == 0);
967-
fd
968-
}
969-
}
970-
971-
#[cfg(all(feature="memfd", target_os="linux"))]
972-
fn create_shmem(name: CString, length: usize) -> c_int {
973-
unsafe {
974-
let fd = memfd_create(name.as_ptr(), 0);
975-
assert!(fd >= 0);
976-
assert!(libc::ftruncate(fd, length as off_t) == 0);
977-
fd
978-
}
979-
}
980-
981957
struct UnixCmsg {
982958
cmsg_buffer: *mut cmsghdr,
983959
msghdr: msghdr,
@@ -1053,11 +1029,6 @@ fn is_socket(fd: c_int) -> bool {
10531029

10541030
// FFI stuff follows:
10551031

1056-
#[cfg(all(feature="memfd", target_os="linux"))]
1057-
unsafe fn memfd_create(name: *const c_char, flags: usize) -> c_int {
1058-
syscall!(MEMFD_CREATE, name, flags) as c_int
1059-
}
1060-
10611032
#[allow(non_snake_case)]
10621033
fn CMSG_LEN(length: size_t) -> size_t {
10631034
CMSG_ALIGN(mem::size_of::<cmsghdr>()) + length

0 commit comments

Comments
 (0)