tiny-nix-ipc
A small and convenient Rust library for using (UNIX domain) sockets for simple synchronous IPC.
Socket::new_socketpair
makes asocketpair
with the settings you want (AF_UNIX/SOCK_SEQPACKET/CLOEXEC
), but you can useFromRawFd
of course- if you want to poll (using
poll
,select
,kqueue
,epoll
, abstractions like mio, etc.), get the fd usingAsRawFd
- 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 Socket;
Create a socket pair:
let = 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 = ;
let sent_bytes = prnt.send_slice.unwrap;
// sent_bytes == 4
Receive bytes:
let mut rdata = ;
let = chld..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 = create_shmem;
let data = ;
let sent_bytes = prnt.send_slice.unwrap;
// sent_bytes == 4
Receive bytes and the file descriptor:
let mut rdata = ;
let = chld..unwrap;
// recvd_bytes == 4, rfds == Some([6])
Send a struct, just as its raw bytes (does not work with pointers/references/boxes/etc.!):
let data = TestStruct ;
let _ = prnt.send_struct.unwrap;
Receive a struct:
let = chld..unwrap;
// rdata == TestStruct { one: -65, two: 0xDEADBEEF, }, rfds == None
Send a Serde-serializable value serialized as CBOR (via serde_cbor):
= { = "0", = ["ser_cbor"] }
use Value;
let data = U64; // can be your Serializable
let sent_bytes = prnt.send_cbor.unwrap;
// sent_bytes == 4
Receive a Serde-deserializable value serialized as CBOR:
let = chld..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.