All Projects → unrelentingtech → tiny-nix-ipc

unrelentingtech / tiny-nix-ipc

Licence: Unlicense License
Minimal Rust wrapper for using sockets as IPC with file descriptor passing

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to tiny-nix-ipc

IPC.Bond
IPC.Bond is an extension of IPC library that provides inter-process communication using shared memory on Windows with Bond serialization.
Stars: ✭ 26 (+85.71%)
Mutual labels:  ipc, interprocess-communication
dwm-ipc
A DWM patch that allows IPC communication through a UNIX socket
Stars: ✭ 55 (+292.86%)
Mutual labels:  ipc, unix-socket
kitti3
Kitty drop-down service for sway & i3wm
Stars: ✭ 73 (+421.43%)
Mutual labels:  ipc
memfd-examples
Sample usage of the new memfd_create(2) Linux system call.
Stars: ✭ 64 (+357.14%)
Mutual labels:  file-descriptor
InterProcessCommunication
Inter-process Communication
Stars: ✭ 11 (-21.43%)
Mutual labels:  ipc
parity-tokio-ipc
Parity tokio-ipc
Stars: ✭ 54 (+285.71%)
Mutual labels:  unix-socket
IpcEventBus
Faster than Intents and easier than AIDLs.
Stars: ✭ 35 (+150%)
Mutual labels:  ipc
eth-provider
A Universal Ethereum Provider Client
Stars: ✭ 139 (+892.86%)
Mutual labels:  ipc
CadenceSKILL-Python
Inter Process Communication (IPC) between Python and Cadence Virtuoso
Stars: ✭ 51 (+264.29%)
Mutual labels:  ipc
SPMC Queue
A simple and efficient single producer multiple consumer queue, suititable for both ITC and IPC.
Stars: ✭ 19 (+35.71%)
Mutual labels:  ipc
httpie-unixsocket
A UNIX socket plugin for HTTPie
Stars: ✭ 40 (+185.71%)
Mutual labels:  unix-socket
ModernOperatingSystems AndrewTanenbaum
My notes after reading 'Modern Operating Systems' book by Andrew Tanenbaum and Herbert Bos.
Stars: ✭ 71 (+407.14%)
Mutual labels:  ipc
rust-hcl
A Rust library for working with the HashiCorp Configuration Language (HCL).
Stars: ✭ 14 (+0%)
Mutual labels:  serde
metacom
RPC communication protocol for Metarhia stack 🔌
Stars: ✭ 42 (+200%)
Mutual labels:  ipc
polybar-dwm-module
A dwm module for polybar
Stars: ✭ 91 (+550%)
Mutual labels:  ipc
qtalk-go
versatile stream IO and RPC based IPC stack for Go
Stars: ✭ 29 (+107.14%)
Mutual labels:  ipc
serde-ignored
Find out about keys that are ignored when deserializing data
Stars: ✭ 44 (+214.29%)
Mutual labels:  serde
electron-ipc-bus
An IPC bus for Electron.
Stars: ✭ 23 (+64.29%)
Mutual labels:  ipc
endurox-go
Application Server for Go (ASG)
Stars: ✭ 32 (+128.57%)
Mutual labels:  ipc
node-svmq
Native System V message queues in Node.js
Stars: ✭ 16 (+14.29%)
Mutual labels:  ipc

unlicense crates.io

tiny-nix-ipc

A small and convenient Rust library for using (UNIX domain) sockets for simple synchronous IPC.

  • Socket::new_socketpair makes a socketpair with the settings you want (AF_UNIX/SOCK_SEQPACKET/CLOEXEC), but you can use FromRawFd of course
  • if you want to poll (using poll, select, kqueue, epoll, abstractions like mio, etc.), get the fd using AsRawFd
  • all send/recv methods allow file descriptor (fd) passing
  • you can send/recv raw iovecs (scatter-gather vectors), buffers, structs and serde-serialized objects
  • serde is optional, select a Cargo feature for the format you want (ser_cbor, ser_json, ser_bincode)

Usage

extern crate tiny_nix_ipc;
use tiny_nix_ipc::Socket;

Create a socket pair:

let (mut prnt, mut chld) = Socket::new_socketpair().unwrap();

Make a socket non-CLOEXEC (e.g. if you want to fork/exec a different program that should inherit the socket):

chld.no_cloexec().unwrap();

Send bytes:

let data = [0xDE, 0xAD, 0xBE, 0xEF];
let sent_bytes = prnt.send_slice(&data[..], None).unwrap();
// sent_bytes == 4

Receive bytes:

let mut rdata = [0; 4];
let (recvd_bytes, rfds) = chld.recv_into_slice::<[RawFd; 0]>(&mut rdata[..]).unwrap();
// recvd_bytes == 4, rfds == None

Send bytes with a file descriptor (shmemfdrs creates an anonymous file, used as an example here, can be any descriptor of course):

let fd = shmemfdrs::create_shmem(CString::new("/test").unwrap(), 123);
let data = [0xDE, 0xAD, 0xBE, 0xEF];
let sent_bytes = prnt.send_slice(&data[..], Some(&[fd])).unwrap();
// sent_bytes == 4

Receive bytes and the file descriptor:

let mut rdata = [0; 4];
let (recvd_bytes, rfds) = chld.recv_into_slice::<[RawFd; 1]>(&mut rdata[..]).unwrap();
// recvd_bytes == 4, rfds == Some([6])

Send a struct, just as its raw bytes (does not work with pointers/references/boxes/etc.!):

struct TestStruct {
    one: i8,
    two: u32,
}

let data = TestStruct { one: -65, two: 0xDEADBEEF, };
let _ = prnt.send_struct(&data, None).unwrap();

Receive a struct:

let (rdata, rfds) = chld.recv_struct::<TestStruct, [RawFd; 0]>().unwrap();
// rdata == TestStruct { one: -65, two: 0xDEADBEEF, }, rfds == None

Send a Serde-serializable value serialized as CBOR (via serde_cbor):

tiny-nix-ipc = { version = "0", features = ["ser_cbor"] }
use serde_cbor::value::Value;
let data = Value::U64(123456); // can be your Serializable
let sent_bytes = prnt.send_cbor(&data, None).unwrap();
// sent_bytes == 4

Receive a Serde-deserializable value serialized as CBOR:

let (rdata, rfds) = chld.recv_cbor::<Value, [RawFd; 0]>(24).unwrap();
// rdata == Value::U64(123456)

Contributing

Please feel free to submit pull requests!

By participating in this project you agree to follow the Contributor Code of Conduct.

The list of contributors is available on GitHub.

License

This is free and unencumbered software released into the public domain.
For more information, please refer to the UNLICENSE file or unlicense.org.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].