Skip to content

Commit

Permalink
Made signals optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kotauskas committed Mar 5, 2021
1 parent a0bbdda commit bd002d9
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 162 deletions.
29 changes: 19 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,32 @@ keywords = ["ipc", "shared_memory", "pipe", "unix_domain_socket"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = {version = "0.2", features = ["extra_traits"]}
thiserror = "1.0"
spinning = "0.1"
intmap = "0.7"
lazy_static = "1.4"
blocking = {version = "1.0", optional = true}
futures = {version = "0.3", optional = true}
libc = { version = "0.2", features = ["extra_traits"] }
thiserror = { version = "1.0", optional = true }
spinning = { version = "0.1", optional = true }
intmap = { version = "0.7", optional = true }
once_cell = { version = "1.7", optional = true }
blocking = { version = "1.0", optional = true }
futures = { version = "0.3", optional = true }
cfg-if = "1.0"

[dev_dependencies]
tokio = {version = "1.0", features = ["rt", "macros", "rt-multi-thread"]}
tokio = { version = "1.0", features = ["rt", "macros", "rt-multi-thread"] }

[target.'cfg(windows)'.dependencies]
winapi = {version = "0.3", features = ["std", "winbase", "winerror", "processthreadsapi", "fileapi", "handleapi", "namedpipeapi"]}
winapi = { version = "0.3", features = [
"std",
"winbase",
"winerror",
"processthreadsapi",
"fileapi",
"handleapi",
"namedpipeapi",
] }

[features]
default = []
default = ["signals", "nonblocking"]
signals = ["thiserror", "spinning", "intmap", "once_cell"]
nonblocking = ["blocking", "futures"]
doc_cfg = []

Expand Down
9 changes: 9 additions & 0 deletions src/os/unix/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,12 @@ cfg_if! {
pub(super) type FdOps = ();
}
}

cfg_if! {
if #[cfg(feature = "signals")] {
pub use intmap::IntMap;
pub use once_cell::sync::Lazy;
pub use spinning::{RwLock, RwLockUpgradableReadGuard};
pub use thiserror::Error;
}
}
6 changes: 4 additions & 2 deletions src/os/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
//!
//! Unix domain sockets are not available on ARM Newlib, but are supported on all other Unix-like systems.
mod imports;

pub mod fifo_file;
#[cfg(any(doc, feature = "signals"))]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "signals")))]
pub mod signal;

mod imports;

#[cfg(any(
target_os = "linux",
target_os = "android",
Expand Down
40 changes: 18 additions & 22 deletions src/os/unix/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@
#![cfg_attr(not(unix), allow(unused_imports))]

use cfg_if::cfg_if;
use intmap::IntMap;
use lazy_static::lazy_static;
use spinning::{RwLock, RwLockUpgradableReadGuard};
use std::{
convert::{TryFrom, TryInto},
error::Error,
Expand All @@ -149,7 +146,6 @@ use std::{
mem::zeroed,
panic, process,
};
use thiserror::Error;

use super::imports::*;

Expand Down Expand Up @@ -200,11 +196,11 @@ pub const fn is_valid_rtsignal(rtsignal: u32) -> bool {
}

/// The first field is the current method of handling a specific signal, the second one is the flags which were set for it.
#[cfg(all(unix, feature = "signals"))]
type HandlerAndFlags = (SignalHandler, i32);
#[cfg(unix)]
lazy_static! {
static ref HANDLERS: RwLock<IntMap<HandlerAndFlags>> = RwLock::new(IntMap::new());
}

#[cfg(all(unix, feature = "signals"))]
static HANDLERS: Lazy<RwLock<IntMap<HandlerAndFlags>>> = Lazy::new(|| RwLock::new(IntMap::new()));

/// Installs the specified handler for the specified standard signal, using the default values for the flags.
///
Expand All @@ -213,7 +209,7 @@ lazy_static! {
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalType, SignalHandler};
///
/// let handler = unsafe {
Expand Down Expand Up @@ -250,7 +246,7 @@ pub fn set_handler(signal_type: SignalType, handler: SignalHandler) -> Result<()
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalType, SignalHandler};
///
/// let handler = unsafe {
Expand Down Expand Up @@ -291,7 +287,7 @@ pub unsafe fn set_unsafe_handler(
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalHandler};
///
/// let handler = unsafe {
Expand Down Expand Up @@ -338,7 +334,7 @@ unsafe fn install_hook(signum: i32, hook: usize, flags: i32) -> io::Result<()> {
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalType, SignalHandler};
///
/// let handler = unsafe {
Expand Down Expand Up @@ -579,11 +575,11 @@ impl HandlerOptions {

/// The error produced when setting a signal handler fails.
#[derive(Debug)]
#[cfg_attr(unix, derive(Error))]
#[cfg_attr(all(unix, feature = "signals"), derive(Error))]
pub enum SetHandlerError {
/// An unsafe signal was attempted to be handled using `set` instead of `set_unsafe`.
#[cfg_attr(
unix,
all(unix, feature = "signals"),
error("an unsafe signal was attempted to be handled using `set` instead of `set_unsafe`")
)]
UnsafeSignal,
Expand All @@ -592,13 +588,13 @@ pub enum SetHandlerError {
/// [`Kill`]: enum.SignalType.html#variant.Kill " "
/// [`ForceSuspend`]: enum.SignalType.html#variant.ForceSuspend " "
#[cfg_attr(
unix,
all(unix, feature = "signals"),
error("the signal {:?} cannot be handled", .0),
)]
UnblockableSignal(SignalType),
/// The specified real-time signal is not available on this OS.
#[cfg_attr(
unix,
all(unix, feature = "signals"),
error(
"the real-time signal number {} is not available ({} is the highest possible)",
.attempted,
Expand All @@ -613,10 +609,10 @@ pub enum SetHandlerError {
},
/// An unexpected OS error ocurred during signal handler setup.
#[cfg_attr(
unix,
all(unix, feature = "signals"),
error("{}", .0),
)]
UnexpectedSystemCallFailure(#[cfg_attr(unix, from)] io::Error),
UnexpectedSystemCallFailure(#[cfg_attr(all(unix, feature = "signals"), from)] io::Error),
}

/// The actual hook which is passed to `sigaction` which dispatches signals according to the global handler map (the `HANDLERS` static).
Expand Down Expand Up @@ -739,7 +735,7 @@ impl From<SignalHook> for fn() {
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalType};
/// use std::process;
///
Expand Down Expand Up @@ -768,7 +764,7 @@ pub fn send(signal: impl Into<Option<SignalType>>, pid: impl Into<u32>) -> io::R
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalType};
/// use std::process;
///
Expand Down Expand Up @@ -801,7 +797,7 @@ pub fn send_rt(signal: impl Into<Option<u32>>, pid: impl Into<u32>) -> io::Resul
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalType};
/// use std::process;
///
Expand All @@ -827,7 +823,7 @@ pub fn send_to_group(signal: impl Into<Option<SignalType>>, pid: impl Into<u32>)
/// # Example
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # #[cfg(unix)] {
/// # #[cfg(all(unix, feature = "signals"))] {
/// use interprocess::os::unix::signal::{self, SignalType};
/// use std::process;
///
Expand Down
71 changes: 71 additions & 0 deletions src/os/windows/imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#![allow(unused_imports, dead_code, unused_macros, non_camel_case_types)]
use cfg_if::cfg_if;

macro_rules! fake_consts {
($ty:ty, $($name:ident = $val:expr),+ $(,)?) => (
$(
pub const $name : $ty = $val;
)+
);
}

cfg_if! {
if #[cfg(windows)] {
pub use winapi::{
shared::{minwindef::{DWORD, LPVOID}, ntdef::HANDLE, winerror::ERROR_PIPE_CONNECTED},
um::{
winbase::{
FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_ACCESS_DUPLEX, PIPE_ACCESS_INBOUND,
PIPE_ACCESS_OUTBOUND, PIPE_READMODE_BYTE, PIPE_READMODE_MESSAGE,
PIPE_TYPE_BYTE, PIPE_TYPE_MESSAGE,
},
winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE},
fileapi::{CreateFileW, OPEN_EXISTING, FlushFileBuffers, ReadFile, WriteFile},
handleapi::{CloseHandle, DuplicateHandle, INVALID_HANDLE_VALUE},
namedpipeapi::{
ConnectNamedPipe, DisconnectNamedPipe,
PeekNamedPipe,
CreatePipe, CreateNamedPipeW, SetNamedPipeHandleState,
},
winbase::{
GetNamedPipeClientProcessId, GetNamedPipeClientSessionId,
GetNamedPipeServerProcessId, GetNamedPipeServerSessionId,
},
minwinbase::SECURITY_ATTRIBUTES,
processthreadsapi::GetCurrentProcess,
},
};
pub use std::os::windows::{io::{AsRawHandle, FromRawHandle, IntoRawHandle}, ffi::OsStrExt};
} else {
pub type HANDLE = *mut ();
pub trait AsRawHandle {}
pub trait IntoRawHandle {}
pub unsafe trait FromRawHandle {}
pub type DWORD = u32;
pub struct SECURITY_ATTRIBUTES {}
pub type LPVOID = *mut ();

fake_consts! {u32,
PIPE_ACCESS_INBOUND = 0, PIPE_ACCESS_OUTBOUND = 1, PIPE_ACCESS_DUPLEX = 2,
PIPE_TYPE_BYTE = 1, PIPE_TYPE_MESSAGE = 2,
PIPE_READMODE_BYTE = 0, PIPE_READMODE_MESSAGE = 1,
}
}
}

cfg_if! {
if #[cfg(all(windows, feature = "signals"))] {
pub use libc::{sighandler_t, SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM};
pub use intmap::IntMap;
pub use once_cell::sync::Lazy;
pub use spinning::{RwLock, RwLockUpgradableReadGuard};
pub use thiserror::Error;

// FIXME this is not yet in libc, remove when PR #1626 on rust-lang/libc gets merged
pub const SIG_DFL: sighandler_t = 0;
} else {
fake_consts! {i32,
SIGABRT = 100, SIGFPE = 101, SIGILL = 102, SIGINT = 103, SIGSEGV = 104, SIGTERM = 105,
}
}
}
32 changes: 9 additions & 23 deletions src/os/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
//! Windows-specific functionality for various interprocess communication primitives, as well as Windows-specific ones.
pub mod named_pipe;
#[cfg(any(doc, feature = "signals"))]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "signals")))]
pub mod signal;
pub mod unnamed_pipe;
// TODO mailslots
//pub mod mailslot;
#[cfg(windows)]
pub(crate) mod local_socket;

#[cfg(windows)]
use winapi::{
shared::{minwindef::DWORD, ntdef::HANDLE},
um::{
fileapi::{FlushFileBuffers, ReadFile, WriteFile},
handleapi::{CloseHandle, DuplicateHandle, INVALID_HANDLE_VALUE},
processthreadsapi::GetCurrentProcess,
},
};
#[cfg(not(windows))]
#[doc(hidden)]
pub type HANDLE = *mut ();
#[cfg(not(windows))]
#[doc(hidden)]
pub trait AsRawHandle {}
#[cfg(not(windows))]
#[doc(hidden)]
pub trait IntoRawHandle {}
#[cfg(not(windows))]
#[doc(hidden)]
pub unsafe trait FromRawHandle {}
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle};
mod imports;
use imports::*;

use std::{io, mem, ptr};

/// Objects which own handles which can be shared with another processes.
Expand Down Expand Up @@ -66,9 +48,13 @@ pub trait ShareHandle: AsRawHandle {
}
}
}
#[cfg(windows)]
impl ShareHandle for crate::unnamed_pipe::UnnamedPipeReader {}
#[cfg(windows)]
impl ShareHandle for unnamed_pipe::UnnamedPipeReader {}
#[cfg(windows)]
impl ShareHandle for crate::unnamed_pipe::UnnamedPipeWriter {}
#[cfg(windows)]
impl ShareHandle for unnamed_pipe::UnnamedPipeWriter {}

/// Newtype wrapper which defines file I/O operations on a `HANDLE` to a file.
Expand Down
Loading

0 comments on commit bd002d9

Please sign in to comment.