[][src]Crate unix_ipc

This crate implements a minimal abstraction over UNIX domain sockets for the purpose of IPC. It lets you send both file handles and rust objects between processes.

How it works

This uses serde to serialize data over unix sockets via bincode. Thanks to the Handle abstraction you can also send any object across that is convertable into a unix file handle.

The way this works under the hood is that during serialization and deserialization encountered file descriptors are tracked. They are then sent over the unix socket separately. This lets unassociated processes share file handles.

If you only want the unix socket abstraction you can disable all default features and use the raw channels.

Example

use std::env;
use std::process;
use unix_ipc::{channel, Bootstrapper, Receiver, Sender};
use serde::{Deserialize, Serialize};

const ENV_VAR: &str = "PROC_CONNECT_TO";

#[derive(Serialize, Deserialize, Debug)]
pub enum Task {
    Sum(Vec<i64>, Sender<i64>),
    Shutdown,
}

if let Ok(path) = env::var(ENV_VAR) {
    let receiver = Receiver::<Task>::connect(path).unwrap();
    loop {
        match receiver.recv().unwrap() {
            Task::Sum(values, tx) => {
                tx.send(values.into_iter().sum::<i64>()).unwrap();
            }
            Task::Shutdown => break,
        }
    }
} else {
    let bootstrapper = Bootstrapper::new().unwrap();
    let mut child = process::Command::new(env::current_exe().unwrap())
        .env(ENV_VAR, bootstrapper.path())
        .spawn()
        .unwrap();

    let (tx, rx) = channel().unwrap();
    bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
    println!("sum: {}", rx.recv().unwrap());
    bootstrapper.send(Task::Shutdown).unwrap();
}

Feature Flags

All features are enabled by default but a lot can be turned off to cut down on dependencies. With all default features enabled only the raw types are available.

  • serde: enables serialization and deserialization.
  • bootstrap: adds the Bootstrapper type.
  • bootstrap-simple: adds the default new constructor to the bootstrapper.

Structs

Bootstrapper

A bootstrap helper.

Handle

Can transfer a unix file handle across processes.

HandleRef

A raw reference to a handle.

RawReceiver

A raw receiver.

RawSender

A raw sender.

Receiver

A typed receiver.

Sender

A typed sender.

Functions

channel

Creates a typed connected channel.

deserialize

Deserializes something for IPC communication.

raw_channel

Creates a raw connected channel.

serde_in_ipc_mode

Checks if serde is in IPC mode.

serialize

Serializes something for IPC communication.