Skip to content

Commit

Permalink
Extract create_shmem into the shmemfdrs crate
Browse files Browse the repository at this point in the history
(which adds support for FreeBSD SHM_ANON)
  • Loading branch information
valpackett committed Dec 9, 2017
1 parent b3dd16a commit 8258b7a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 38 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ repository = "https://github.com/servo/ipc-channel"

[features]
force-inprocess = []
memfd = ["syscall"]
memfd = ["shmemfdrs/memfd"]
unstable = []
async = ["futures"]

[dependencies]
bincode = "0.9"
lazy_static = "0.2"
libc = "0.2.12"
libc = "0.2.34"
rand = "0.3"
serde = { version="1.0", features=["rc"] }
uuid = {version = "0.5", features = ["v4"]}
fnv = "1.0.3"

[target.'cfg(any(target_os = "linux", target_os = "freebsd"))'.dependencies]
mio = "0.6.1"
shmemfdrs = "0.1"

syscall = { version = "0.2.1", optional = true }
futures = { version = "0.1", optional = true }

[dev-dependencies]
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ extern crate mio;
#[cfg(all(not(feature = "force-inprocess"), any(target_os = "linux",
target_os = "freebsd")))]
extern crate fnv;
#[cfg(all(feature = "memfd", not(feature = "force-inprocess"),
target_os="linux"))]
#[macro_use]
extern crate syscall;
#[cfg(all(not(feature = "force-inprocess"), any(target_os = "linux",
target_os = "freebsd")))]
extern crate shmemfdrs;

#[cfg(feature = "async")]
extern crate futures;
Expand Down
33 changes: 2 additions & 31 deletions src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bincode;
use fnv::FnvHasher;
use libc::{self, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE, SOCK_SEQPACKET, SOL_SOCKET};
use libc::{SO_LINGER, S_IFMT, S_IFSOCK, c_char, c_int, c_void, getsockopt};
use libc::{iovec, mode_t, msghdr, off_t, recvmsg, sendmsg};
use libc::{iovec, mode_t, msghdr, recvmsg, sendmsg};
use libc::{setsockopt, size_t, sockaddr, sockaddr_un, socketpair, socklen_t, sa_family_t};
use std::cell::Cell;
use std::cmp;
Expand All @@ -31,6 +31,7 @@ use std::time::UNIX_EPOCH;
use std::thread;
use mio::unix::EventedFd;
use mio::{Poll, Token, Events, Ready, PollOpt};
use shmemfdrs::create_shmem;

use super::incrementor::Incrementor;

Expand Down Expand Up @@ -947,31 +948,6 @@ fn recv(fd: c_int, blocking_mode: BlockingMode)
Ok((main_data_buffer, channels, shared_memory_regions))
}

#[cfg(not(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);
assert!(fd >= 0);
assert!(libc::shm_unlink(name.as_ptr()) == 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 {
let fd = memfd_create(name.as_ptr(), 0);
assert!(fd >= 0);
assert!(libc::ftruncate(fd, length as off_t) == 0);
fd
}
}

struct UnixCmsg {
cmsg_buffer: *mut cmsghdr,
msghdr: msghdr,
Expand Down Expand Up @@ -1047,11 +1023,6 @@ fn is_socket(fd: c_int) -> bool {

// FFI stuff follows:

#[cfg(all(feature="memfd", target_os="linux"))]
unsafe fn memfd_create(name: *const c_char, flags: usize) -> c_int {
syscall!(MEMFD_CREATE, name, flags) as c_int
}

#[allow(non_snake_case)]
fn CMSG_LEN(length: size_t) -> size_t {
CMSG_ALIGN(mem::size_of::<cmsghdr>()) + length
Expand Down

0 comments on commit 8258b7a

Please sign in to comment.