Fork

Struct Fork 

Source
pub struct Fork { /* private fields */ }
Expand description

A Spawner-like structure that executes a closure instead of another process. Specifically, it forks the current caller, runs the closure within the child, then serializes and returns the result to the parent via a pipe.

Your return type, if one exists, must implement Serialize + Deserialize from serde, as the closure is run under a separate process (the child). This means that “returning” the result requires serializing the result and sending it back to the parent. This operates identically to how standard Input/Output/Error is captured in Spawn.

Implementations§

Source§

impl Fork

Source

pub fn new() -> Self

Construct a new fork instance.

Source

pub fn mode(self, mode: Mode) -> Self

See Spawner::mode

Source

pub fn seccomp(self, seccomp: Filter) -> Self

See Spawner::seccomp

Source

pub fn cap(self, cap: Capability) -> Self

See Spawner::cap

Source

pub fn caps(self, caps: impl IntoIterator<Item = Capability>) -> Self

See Spawner::caps

Source

pub fn new_privileges(self, allow: bool) -> Self

See Spawner::new_privileges

Source

pub fn mode_i(&mut self, mode: Mode)

See Spawner::mode_i

Source

pub fn seccomp_i(&self, seccomp: Filter)

See Spawner::seccomp_i

Source

pub fn cap_i(&mut self, cap: Capability)

See Spawner::cap_i

Source

pub fn caps_i(&mut self, caps: impl IntoIterator<Item = Capability>)

See Spawner::caps_i

Source

pub fn new_privileges_i(self, allow: bool)

See Spawner::new_privileges_i

Source

pub unsafe fn fork<F, R>(self, op: F) -> Result<R, Error>
where F: FnOnce() -> R + UnwindSafe, R: Serialize + DeserializeOwned,

Run a closure within a fork.

§Example
let result = unsafe { spawn::Fork::new().fork(|| 1) }.unwrap();
assert!(result == 1);
§Safety

This function does not call execve. The closure runs within the fork, which has several considerations:

  1. The code should not make allocations. Though the default memory allocator on Linux often works in such an environment, it should not be relied upon.
  2. If you had a signal handler installed, this tries and drops all of them. Other such primitives may fail at any point, and should not be relied upon.

If your closure returns a value, it must implement Serialize, as the closure is running under a separate process, and must be transmitted to the parent through a pipe.

Source

pub unsafe fn fork_fd<F, R>(self, op: F) -> Result<OwnedFd, Error>
where F: FnOnce() -> R + UnwindSafe, R: Into<OwnedFd>,

This is a specialized version of fork() that uses the SCM-Rights of a Unix Socket to transmit a FD to the parent. This could be used to open a file under one operating mode, and send the FD to the parent under another operating mode.

§Safety

See fork()

Trait Implementations§

Source§

impl Default for Fork

Source§

fn default() -> Fork

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

Auto Trait Implementations§

§

impl !Freeze for Fork

§

impl !RefUnwindSafe for Fork

§

impl Send for Fork

§

impl Sync for Fork

§

impl Unpin for Fork

§

impl !UnwindSafe for Fork

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.