From a379ba0dd42a8742428b546321ebcd61a59392e4 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 30 Sep 2018 03:07:08 +0200 Subject: [PATCH] Use extracted shmemfdrs crate This crate has been extracted from ipc-channel[1] and provides exactly the same create_shmem() function. Additionally it supports SHM_OPEN for FreeBSD, and has a test. The sc crate has also been removed, but it is still used by shmemfdrs. [1] https://github.com/Smithay/wayland-window/issues/14#issuecomment-350488570 --- Cargo.toml | 8 ++++++-- src/lib.rs | 8 ++++---- src/platform/unix/mod.rs | 33 ++------------------------------- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b3fb2db3d..0f179c86e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/servo/ipc-channel" [features] force-inprocess = [] -memfd = ["sc"] +memfd = [] unstable = [] async = ["futures"] @@ -23,10 +23,14 @@ uuid = {version = "0.7", features = ["v4"]} fnv = "1.0.3" tempfile = "3" +[target.'cfg(all(any(target_os = "linux", target_os = "openbsd", target_os = "freebsd"), feature = "memfd"))'.dependencies] +shmemfdrs = { version = "0.1.1", features = ["memfd"] } +[target.'cfg(all(any(target_os = "linux", target_os = "openbsd", target_os = "freebsd"), not(feature = "memfd")))'.dependencies] +shmemfdrs = { version = "0.1.1", features = [] } + [target.'cfg(any(target_os = "linux", target_os = "openbsd", target_os = "freebsd"))'.dependencies] mio = "0.6.11" -sc = { version = "0.2.2", optional = true } futures = { version = "0.1", optional = true } [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index 332c8e8ca..8d467befe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,10 +28,10 @@ extern crate mio; target_os = "openbsd", target_os = "freebsd")))] extern crate fnv; -#[cfg(all(feature = "memfd", not(feature = "force-inprocess"), - target_os="linux"))] -#[macro_use] -extern crate sc; +#[cfg(any(target_os = "linux", + target_os = "openbsd", + target_os = "freebsd"))] +extern crate shmemfdrs; #[cfg(feature = "async")] extern crate futures; diff --git a/src/platform/unix/mod.rs b/src/platform/unix/mod.rs index 33414976d..b5f171a4b 100644 --- a/src/platform/unix/mod.rs +++ b/src/platform/unix/mod.rs @@ -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; @@ -32,6 +32,7 @@ use std::thread; use mio::unix::EventedFd; use mio::{Poll, Token, Events, Ready, PollOpt}; use tempfile::{Builder, TempDir}; +use shmemfdrs::create_shmem; const MAX_FDS_IN_CMSG: u32 = 64; @@ -953,31 +954,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, @@ -1053,11 +1029,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::()) + length