Enum Signal

Source
#[non_exhaustive]
pub enum Signal { Hangup, ForceStop, Interrupt, Quit, Terminate, User1, User2, Custom(i32), }
Expand description

A notification (signals or Windows control events) sent to a process.

This signal type in Watchexec is used for any of:

  • signals sent to the main process by some external actor,
  • signals received from a sub process by the main process,
  • signals sent to a sub process by Watchexec.

On Windows, only some signals are supported, as described. Others will be ignored.

On Unix, there are several “first-class” signals which have their own variants, and a generic Custom variant which can be used to send arbitrary signals.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Hangup

Indicate that the terminal is disconnected.

On Unix, this is SIGHUP. On Windows, this is ignored for now but may be supported in the future (see #219).

Despite its nominal purpose, on Unix this signal is often used to reload configuration files.

§

ForceStop

Indicate to the kernel that the process should stop.

On Unix, this is SIGKILL. On Windows, this is TerminateProcess.

This signal is not handled by the process, but directly by the kernel, and thus cannot be intercepted. Subprocesses may exit in inconsistent states.

§

Interrupt

Indicate that the process should stop.

On Unix, this is SIGINT. On Windows, this is ignored for now but may be supported in the future (see #219).

This signal generally indicates an action taken by the user, so it may be handled differently than a termination.

§

Quit

Indicate that the process is to stop, the kernel will then dump its core.

On Unix, this is SIGQUIT. On Windows, it is ignored.

This is rarely used.

§

Terminate

Indicate that the process should stop.

On Unix, this is SIGTERM. On Windows, this is ignored for now but may be supported in the future (see #219).

On Unix, this signal generally indicates an action taken by the system, so it may be handled differently than an interruption.

§

User1

Indicate an application-defined behaviour should happen.

On Unix, this is SIGUSR1. On Windows, it is ignored.

This signal is generally used to start debugging.

§

User2

Indicate an application-defined behaviour should happen.

On Unix, this is SIGUSR2. On Windows, it is ignored.

This signal is generally used to reload configuration.

§

Custom(i32)

Indicate using a custom signal.

Internally, this is converted to a nix::Signal but for portability this variant is a raw i32.

Invalid signals on the current platform will be ignored. Does nothing on Windows.

The special value 0 is used to indicate an unknown signal. That is, a signal was received or parsed, but it is not known which. This is not a usual case, and should in general be ignored rather than hard-erroring.

§Examples

use watchexec_signals::Signal;
use nix::sys::signal::Signal as NixSignal;
assert_eq!(Signal::Custom(6), Signal::from(NixSignal::SIGABRT as i32));

On Unix the from_nix method should be preferred if converting from Nix’s Signal type:

use watchexec_signals::Signal;
use nix::sys::signal::Signal as NixSignal;
assert_eq!(Signal::Custom(6), Signal::from_nix(NixSignal::SIGABRT));

Implementations§

Source§

impl Signal

Source

pub fn to_nix(self) -> Option<NixSignal>

Converts to a nix::Signal if possible.

This will return None if the signal is not supported on the current platform (only for Custom, as the first-class ones are always supported).

Source

pub fn from_nix(sig: NixSignal) -> Self

Converts from a nix::Signal.

Source§

impl Signal

Source

pub fn from_unix_str(s: &str) -> Result<Self, SignalParseError>

Parse the input as a unix signal.

This parses the input as a signal name, or a signal number, in a case-insensitive manner. It supports integers, the short name of the signal (like INT, HUP, USR1, etc), and the long name of the signal (like SIGINT, SIGHUP, SIGUSR1, etc).

Note that this is entirely accurate only when used on unix targets; on other targets it falls back to a hardcoded approximation instead of looking up signal tables (via nix).

assert_eq!(Signal::Hangup, Signal::from_unix_str("hup").unwrap());
assert_eq!(Signal::Interrupt, Signal::from_unix_str("SIGINT").unwrap());
assert_eq!(Signal::ForceStop, Signal::from_unix_str("Kill").unwrap());

Using FromStr is recommended for practical use, as it will also parse Windows control events, see Signal::from_windows_str.

Source

pub fn from_windows_str(s: &str) -> Result<Self, SignalParseError>

Parse the input as a windows control event.

This parses the input as a control event name, in a case-insensitive manner.

The names matched are mostly made up as there’s no standard for them, but should be familiar to Windows users. They are mapped to the corresponding unix concepts as follows:

  • CTRL-CLOSE, CTRL+CLOSE, or CLOSE for a hangup
  • CTRL-BREAK, CTRL+BREAK, or BREAK for a terminate
  • CTRL-C, CTRL+C, or C for an interrupt
  • STOP, FORCE-STOP for a forced stop. This is also mapped to KILL and SIGKILL.
assert_eq!(Signal::Hangup, Signal::from_windows_str("ctrl+close").unwrap());
assert_eq!(Signal::Interrupt, Signal::from_windows_str("C").unwrap());
assert_eq!(Signal::ForceStop, Signal::from_windows_str("Stop").unwrap());

Using FromStr is recommended for practical use, as it will fall back to parsing as a unix signal, which can be helpful for portability.

Trait Implementations§

Source§

impl Clone for Signal

Source§

fn clone(&self) -> Signal

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Signal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Signal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<i32> for Signal

Source§

fn from(raw: i32) -> Self

Converts from a raw signal number.

This uses hardcoded numbers for the first-class signals.

Source§

impl FromStr for Signal

Source§

type Err = SignalParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Signal

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Signal

Source§

fn eq(&self, other: &Signal) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Signal

Source§

impl Eq for Signal

Source§

impl StructuralPartialEq for Signal

Auto Trait Implementations§

§

impl Freeze for Signal

§

impl RefUnwindSafe for Signal

§

impl Send for Signal

§

impl Sync for Signal

§

impl Unpin for Signal

§

impl UnwindSafe for Signal

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.