Struct Parent

Source
pub struct Parent(/* private fields */);
Expand description

The write end of a pipe to the original parent process.

The daemon can send at most one message to the parent, after which the pipe will be closed and the parent process terminated. Sending nothing and dropping the Parent object will result in an EOF on the parent’s end, which is equivalent to sending an empty error message. If the daemon encounters a fatal error during setup, it can send a custom error message to the parent with Parent::notify. This message will be printed to the parent process’s stderr. If setup succeeds, Parent::success should be called, which sends a very specific message to the parent. The message is akin to a simple “ok”, so there is very little chance of an error being misunderstood as success. Upon receiving a success message, the parent process exits with code zero without printing anything.

Objects created by Parent::default do not contain any pipe handles. They can be used to simplify code for programs that can be run as a daemon or in the foreground.

§Examples

use dmon::Parent;
use std::process::ExitCode;

fn main() -> ExitCode {
    let daemon = true;

    let mut parent = if daemon {
        dmon::options().daemonize()
    } else {
        Default::default()
    };

    match run_server(&mut parent) {
        Ok(()) => ExitCode::SUCCESS,
        Err(err) => {
            eprintln!("{err}");
            parent.notify(&err).unwrap();
            ExitCode::FAILURE
        }
    }
}

fn run_server(parent: &mut Parent) -> Result<(), String> {
    // Listen on a port or socket...
    // Returning an Err here will write the error to the parent process.

    parent.success().unwrap();

    // Run the server...

    Ok(())
}

Implementations§

Source§

impl Parent

Source

pub fn is_waiting(&self) -> bool

Returns true if the parent process is waiting for a message.

Source

pub fn notify(&mut self, message: &str) -> Result<()>

Writes the specified message to the parent process and closes the pipe.

This method should be called if the daemon encounters a fatal error during setup. The message will be displayed to the user, and the parent process will exit with a non-zero code.

It is safe to call this method after the pipe is closed or when there is no parent process at all. Such calls are no-ops and immediately returns Ok.

Source

pub fn success(&mut self) -> Result<()>

Tells the parent process that the daemon started successfully and closes the pipe.

This method should be called as soon as the daemon is considered up and running. Upon receiving a success message, the parent process will exit with code zero without printing anything to the user.

It is safe to call this method after the pipe is closed or when there is no parent process at all. Such calls are no-ops and immediately returns Ok.

Trait Implementations§

Source§

impl Debug for Parent

Source§

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

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

impl Default for Parent

Source§

fn default() -> Parent

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Parent

§

impl RefUnwindSafe for Parent

§

impl Send for Parent

§

impl Sync for Parent

§

impl Unpin for Parent

§

impl UnwindSafe for Parent

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> 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, 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.